最近做一个项目,需要实现输入人名得到对应的汉语拼音,在网上查了下,也有不少方案,但或多或少都有些问题(有个开源项目pinyin4j倒是挺好,有兴趣可以试试,这里只讲android自带的功能)。后来一想,android的通讯录不就有这样的功能吗,看看它是怎么做的不就行了。
经研究,发现和拼音转换相关的文件位于:
packages\providers\ContactsProvider\src\com\android\providers\contacts\ContactLocaleUtils.java
packages\providers\ContactsProvider\src\com\android\providers\contacts\HanziToPinyin.java
把这两个文件拷贝到自己的工程中,稍作修改就可以用了,代码如下:
/*
* 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.test;
import android.util.SparseArray;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import com.test.HanziToPinyin.Token;
/**
* This utility class provides customized sort key and name lookup key according the locale.
*/
public class ContactLocaleUtils {
public interface FullNameStyle {
public static final int UNDEFINED = 0;
public static final int WESTERN = 1;
/**
* Used if the name is written in Hanzi/Kanji/Hanja and we could not determine
* which specific language it belongs to: Chinese, Japanese or Korean.
*/
public static final int CJK = 2;
public static final int CHINESE = 3;
public static final int JAPANESE = 4;
public static final int KOREAN = 5;
}
/**
* This class is the default implementation.
* <p>
* It should be the base class for other locales' implementation.
*/
public class ContactLocaleUtilsBase {
public String getSortKey(String displayName) {
return displayName;
}
@SuppressWarnings("unused")
public Iterator<String> getNameLookupKeys(String name) {
return null;
}
}
/**
* The classes to generate the Chinese style sort and search keys.
* <p>
* 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.
* <p>
* 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<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 final String CHINESE_LANGUAGE = Locale.CHINESE.getLanguage().toLowerCase();
private static final String JAPANESE_LANGUAGE = Locale.JAPANESE.getLanguage().toLowerCase();
private static final String KOREAN_LANGUAGE = Locale.KOREAN.getLanguage().toLowerCase();
private static ContactLocaleUtils sSingleton;
private final SparseArray<ContactLocaleUtilsBase> mUtils =
new SparseArray<ContactLocaleUtilsBase>();
private final ContactLocaleUtilsBase mBase = new ContactLocaleUtilsBase();
private String mLanguage;
private ContactLocaleUtils() {
setLocale(null);
}
public void setLocale(Locale currentLocale) {
if (currentLocale == null) {
mLanguage = Locale.getDefault().getLanguage().toLowerCase();
} else {
mLanguage = currentLocale.getLanguage().toLowerCase();
}
}
public String getSortKey(String displayName, int nameStyle) {
return getForSort(Integer.valueOf(nameStyle)).getSortKey(displayName);
}
public Iterator<String> getNameLookupKeys(String name, int nameStyle) {
return getForNameLookup(Integer.valueOf(nameStyle)).getNameLookupKeys(name);
}
/**
* Determine which utility should be used for generating NameLookupKey.
* <p>
* 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.
* <p>
* For Chinese and CJK style name if current language is neither Japanese or Korean,
* the ChineseContactUtils should be used.
*/
private ContactLocaleUtilsBase getForSort