主页 > 原创 > 安卓联系人名称为数字空格时显示变化

安卓联系人名称为数字空格时显示变化

安卓联系人会根据输入的名称判断姓氏、姓名等,当输入数字时被当作西文姓名格式,所以会出现后边的数字倒置的情况,在中文环境下,输入如”1 2 3″的姓名会变为”321″。

Screenshot_2013-09-17-12-54-24

Screenshot_2013-09-17-12-54-30

安卓在分割联系人名称时做了五种区分,WESTERN/CHINESE/JAPANESE/KOREAN/UNDEFINED,具体定义在frameworks/base/core/java/android/provider/ContactsContract.java中,如下所示:

    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;
    }

分割的控制在ContactsProvider中,packages/providers/ContactsProvider/src/com/android/providers/contacts/NameSplitter.java
如所示:

    /**
     * Makes the best guess at the expected full name style based on the character set
     * used in the supplied name.
     */

    private void guessFullNameStyle(NameSplitter.Name name) {
        if (name.fullNameStyle != FullNameStyle.UNDEFINED) {
            return;
        }

        int bestGuess = guessFullNameStyle(name.givenNames);
        // A mix of Hanzi and latin chars are common in China, so we have to go through all names
        // if the name is not JANPANESE or KOREAN.
        if (bestGuess != FullNameStyle.UNDEFINED && bestGuess != FullNameStyle.CJK
                && bestGuess != FullNameStyle.WESTERN) {
            name.fullNameStyle = bestGuess;
            return;
        }

        int guess = guessFullNameStyle(name.familyName);
        if (guess != FullNameStyle.UNDEFINED) {
            if (guess != FullNameStyle.CJK && guess != FullNameStyle.WESTERN) {
                name.fullNameStyle = guess;
                return;
            }
            bestGuess = guess;
        }

        guess = guessFullNameStyle(name.middleName);
        if (guess != FullNameStyle.UNDEFINED) {
            if (guess != FullNameStyle.CJK && guess != FullNameStyle.WESTERN) {
                name.fullNameStyle = guess;
                return;
            }
            bestGuess = guess;
        }

        guess = guessFullNameStyle(name.prefix);
        if (guess != FullNameStyle.UNDEFINED) {
            if (guess != FullNameStyle.CJK && guess != FullNameStyle.WESTERN) {
                name.fullNameStyle = guess;
                return;
            }
            bestGuess = guess;
        }

        guess = guessFullNameStyle(name.suffix);
        if (guess != FullNameStyle.UNDEFINED) {
            if (guess != FullNameStyle.CJK && guess != FullNameStyle.WESTERN) {
                name.fullNameStyle = guess;
                return;
            }
            bestGuess = guess;
        }

        name.fullNameStyle = bestGuess;
    }

由于单纯的数字不满足任何条件,被置为UNDEFINED,根据下面代码被判断为使用默认西文格式:

    /**
     * Parses a full name and returns parsed components in the Name object
     * with a given fullNameStyle.
     */

    public void split(Name name, String fullName, int fullNameStyle) {
        if (fullName == null) {
            return;
        }

        name.fullNameStyle = fullNameStyle;

        switch (fullNameStyle) {
            case FullNameStyle.CHINESE:
                splitChineseName(name, fullName);
                break;

            case FullNameStyle.JAPANESE:
                splitJapaneseName(name, fullName);
                break;

            case FullNameStyle.KOREAN:
                splitKoreanName(name, fullName);
                break;

            default:
                splitWesternName(name, fullName);
        }
    }

所以可以在split方法中添加西文的支持,并将默认置为splitChineseName,这样就可以使”1 2 3″显示为”1 2 3″(存储在数据库中的格式变化,ヽ(`⌒´)ノ不会有人使用数字的名字),修改如下:

        switch (fullNameStyle) {
            case FullNameStyle.CHINESE:
                splitChineseName(name, fullName);
                break;

            case FullNameStyle.JAPANESE:
                splitJapaneseName(name, fullName);
                break;

            case FullNameStyle.KOREAN:
                splitKoreanName(name, fullName);
                break;
            // Begin: [ty, 2013-09-17] Modified for Num name @{
            //default:
            // splitWesternName(name, fullName);
            case FullNameStyle.WESTERN:
                splitWesternName(name, fullName);
                break;
            default:
                splitChineseName(name, fullName);
            // End: [ty, 2013-09-17] Modified for Num name @}
        }

发表评论

电子邮件地址不会被公开。 必填项已用*标注