diff options
author | Bai Tao <michaelbai@google.com> | 2010-01-21 08:40:45 +0800 |
---|---|---|
committer | Bai Tao <michaelbai@google.com> | 2010-01-27 09:18:57 +0800 |
commit | 4cd13c4266d8e476e1a49c4b6bcd5b18c33d0de3 (patch) | |
tree | 0fc798c6377fe2e4b63908ae091d5931dc629eb3 /src/com/android/providers/contacts/ContactLocaleUtils.java | |
parent | 8be93f4d062ad3919ba48245dd147a49715db82d (diff) | |
download | packages_providers_ContactsProvider-4cd13c4266d8e476e1a49c4b6bcd5b18c33d0de3.zip packages_providers_ContactsProvider-4cd13c4266d8e476e1a49c4b6bcd5b18c33d0de3.tar.gz packages_providers_ContactsProvider-4cd13c4266d8e476e1a49c4b6bcd5b18c33d0de3.tar.bz2 |
a. Fix the issue that the FullNameStyle is not updated when user edits the contact or the contacts being synced.
b. Add Chinese sort and search.
c. Adds a CJK check for each contact at runtime. per discussion with component owner, will tune performance later after Korean and Japanese features are added.
Diffstat (limited to 'src/com/android/providers/contacts/ContactLocaleUtils.java')
-rw-r--r-- | src/com/android/providers/contacts/ContactLocaleUtils.java | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/com/android/providers/contacts/ContactLocaleUtils.java b/src/com/android/providers/contacts/ContactLocaleUtils.java new file mode 100644 index 0000000..18d43cf --- /dev/null +++ b/src/com/android/providers/contacts/ContactLocaleUtils.java @@ -0,0 +1,134 @@ +/* + * 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 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. + * <p> + * It should be the base class for other locales' implementation. + */ + public static class ContactLocaleUtilsBase { + public String getSortKey(String displayName) { + return displayName; + } + public Iterator<String> getNameLookupKeys(String name) { + return null; + } + } + + private static class ChineseContactUtils extends ContactLocaleUtilsBase { + @Override + public String getSortKey(String displayName) { + ArrayList<Token> tokens = HanziToPinyin.getInstance().get(displayName); + if (tokens != null && tokens.size() > 0) { + StringBuilder sb = new StringBuilder(); + for (Token token : tokens) { + // Put Chinese character's pinyin, then proceed with the + // character itself. + if (Token.PINYIN == token.type) { + if (sb.length() > 0) { + sb.append(' '); + } + sb.append(token.target); + sb.append(' '); + sb.append(token.source); + } else { + if (sb.length() > 0) { + sb.append(' '); + } + sb.append(token.source); + } + } + return sb.toString(); + } + return super.getSortKey(displayName); + } + + @Override + public Iterator<String> getNameLookupKeys(String name) { + // TODO : Reduce the object allocation. + HashSet<String> keys = new HashSet<String>(); + ArrayList<Token> tokens = HanziToPinyin.getInstance().get(name); + final int tokenCount = tokens.size(); + final StringBuilder keyPinyin = new StringBuilder(); + final StringBuilder keyInitial = new StringBuilder(); + // There is no space among the Chinese Characters, the variant name + // lookup key wouldn't work for Chinese. The keyOrignal is used to + // build the lookup keys for itself. + final StringBuilder keyOrignal = new StringBuilder(); + for (int i = tokenCount - 1; i >= 0; i--) { + final Token token = tokens.get(i); + if (Token.PINYIN == token.type) { + keyPinyin.insert(0, token.target); + keyInitial.insert(0, token.target.charAt(0)); + } else if (Token.LATIN == token.type) { + // Avoid adding space at the end of String. + if (keyPinyin.length() > 0) { + keyPinyin.insert(0, ' '); + } + if (keyOrignal.length() > 0) { + keyOrignal.insert(0, ' '); + } + keyPinyin.insert(0, token.source); + keyInitial.insert(0, token.source.charAt(0)); + } + keyOrignal.insert(0, token.source); + keys.add(keyOrignal.toString()); + keys.add(keyPinyin.toString()); + keys.add(keyInitial.toString()); + } + return keys.iterator(); + } + } + + private static HashMap<Integer, ContactLocaleUtilsBase> mUtils = + new HashMap<Integer, ContactLocaleUtilsBase>(); + + private static ContactLocaleUtilsBase mBase = new ContactLocaleUtilsBase(); + public static String getSortKey(String displayName, int nameStyle) { + return get(Integer.valueOf(nameStyle)).getSortKey(displayName); + } + + public static Iterator<String> getNameLookupKeys(String name, int nameStyle) { + return get(Integer.valueOf(nameStyle)).getNameLookupKeys(name); + } + + private synchronized static 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; + } +} |