/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.providers.contacts; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import android.provider.ContactsContract.FullNameStyle; import com.android.internal.util.HanziToPinyin; import com.android.internal.util.HanziToPinyin.Token; /** * This utility class provides customized sort key and name lookup key according the locale. */ public class ContactLocaleUtils { /** * This class is the default implementation. *
* It should be the base class for other locales' implementation.
*/
public class ContactLocaleUtilsBase {
public String getSortKey(String displayName) {
return displayName;
}
public Iterator
* The sorting key is generated as each Chinese character' pinyin proceeding with
* space and character itself. If the character's pinyin unable to find, the character
* itself will be used.
*
* The below additional name lookup keys will be generated.
* a. Chinese character's pinyin and pinyin's initial character.
* b. Latin word and the initial character for Latin word.
* The name lookup keys are generated to make sure the name can be found by from any
* initial character.
*/
private class ChineseContactUtils extends ContactLocaleUtilsBase {
@Override
public String getSortKey(String displayName) {
ArrayList
* a. For Western style name, if the current language is Chinese, the
* ChineseContactUtils should be used.
* b. For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForNameLookup(Integer nameStyle) {
int nameStyleInt = nameStyle.intValue();
Integer adjustedUtil = Integer.valueOf(getAdjustedStyle(nameStyleInt));
if (CHINESE_LANGUAGE.equals(mLanguage) && nameStyleInt == FullNameStyle.WESTERN) {
adjustedUtil = Integer.valueOf(FullNameStyle.CHINESE);
}
return get(adjustedUtil);
}
private synchronized ContactLocaleUtilsBase get(Integer nameStyle) {
ContactLocaleUtilsBase utils = mUtils.get(nameStyle);
if (utils == null) {
if (nameStyle.intValue() == FullNameStyle.CHINESE) {
utils = new ChineseContactUtils();
mUtils.put(nameStyle, utils);
}
}
return (utils == null) ? mBase: utils;
}
/**
* Determine the which utility should be used for generating sort key.
*
* For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForSort(Integer nameStyle) {
return get(Integer.valueOf(getAdjustedStyle(nameStyle.intValue())));
}
public static synchronized ContactLocaleUtils getIntance() {
if (sSingleton == null) {
sSingleton = new ContactLocaleUtils();
}
return sSingleton;
}
private int getAdjustedStyle(int nameStyle) {
if (nameStyle == FullNameStyle.CJK && !JAPANESE_LANGUAGE.equals(mLanguage) &&
!KOREAN_LANGUAGE.equals(mLanguage)) {
return FullNameStyle.CHINESE;
} else {
return nameStyle;
}
}
}