Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8cf3188 authored by YuanQY's avatar YuanQY Committed by Gerrit Code Review
Browse files

Add T9 search support in Chinese

Patch Set 2
       Add English name search support in Chinese T9.
Patch Set 3
       Add full PingYin search support in Chinese T9.
Patch Set 4
       1. Add locale changed event listener to reinitialize the T9 search to fix the bug that can not switch to locale search.
       2. Fix the highlight name problem.
Patch Set 5
       Change the comment from Chinese to English.

Change-Id: Ic6db849be094c24d932e6a8ea591811a966f6c5e
parent 735b2df2
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -23,9 +23,11 @@ import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.ContentObserver;
@@ -229,6 +231,27 @@ public class DialpadFragment extends Fragment

    private static final String PREF_DIGITS_FILLED_BY_INTENT = "pref_digits_filled_by_intent";

    // Add LOCALE_CHAGNED event receiver.
    private final BroadcastReceiver mLocaleChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v(TAG, "mIntentReceiver  onReceive  intent.getAction(): " + intent.getAction());
            if (intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {
                if (isT9On()) {
                    sT9Search = new T9Search(getActivity());
                }
            }
        }
    };

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        getActivity().unregisterReceiver(mLocaleChangedReceiver);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mWasEmptyBeforeTextChange = TextUtils.isEmpty(s);
@@ -288,6 +311,10 @@ public class DialpadFragment extends Fragment
        if (state != null) {
            mDigitsFilledByIntent = state.getBoolean(PREF_DIGITS_FILLED_BY_INTENT);
        }

        // Add LOCALE_CHAGNED event receiver.
        IntentFilter localeChangedfilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
        getActivity().registerReceiver(mLocaleChangedReceiver, localeChangedfilter);
    }

    private ContentObserver mContactObserver = new ContentObserver(new Handler()) {
+4 −2
Original line number Diff line number Diff line
@@ -287,8 +287,10 @@ class T9Search {
                if (o.nameMatchId != -1) {
                    Spannable s = (Spannable) holder.name.getText();
                    int nameStart = o.normalName.indexOf(mPrevInput);
                    if (nameStart <= o.name.length() && nameStart + mPrevInput.length() <= o.name.length()) {
                        s.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(android.R.color.holo_blue_dark)),
                                nameStart, nameStart + mPrevInput.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    }
                    holder.name.setText(s);
                }
                if (o.numberMatchId != -1) {
+52 −0
Original line number Diff line number Diff line

package com.android.contacts.dialpad.util;

import com.android.contacts.util.HanziToPinyin;

/**
 * @author Barami Implementation for Korean normalization. This will change
 *         Hangul character to number by Choseong(Korean word of initial
 *         character).
 */
public class NameToNumberChinese extends NameToNumber {
    public NameToNumberChinese(String t9Chars, String t9Digits) {
        super(t9Chars, t9Digits);
        // TODO Auto-generated constructor stub
    }

    private String convertToT9(String hzPinYin) {
        StringBuilder sb = new StringBuilder(hzPinYin.length());
        int iTotal = hzPinYin.length();
        for (int i = 0; i < iTotal; i++) {
            int pos = t9Chars.indexOf(hzPinYin.charAt(i));
            if (-1 == pos) {
                pos = 0;
            }
            sb.append(t9Digits.charAt(pos));
        }

        return sb.toString();
    }

    @Override
    public String convert(String name) {
        String t9 = null;

        String hzPinYin = HanziToPinyin.getInstance().getFirstPinYin(name).toLowerCase();
        
        if (hzPinYin != null && !hzPinYin.isEmpty()) {
            t9 = convertToT9(hzPinYin);
            //Append the full ping yin at the end of the first ping yin
            hzPinYin = HanziToPinyin.getInstance().getFullPinYin(name).toLowerCase();
            if (hzPinYin != null && !hzPinYin.isEmpty()) {
                t9 += " " + convertToT9(hzPinYin);
            }
        } else {
            // Add English name search support
            t9 = convertToT9(name.toLowerCase());
        }
        
        return t9;

    }
}
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ public class NameToNumberFactory {
        NameToNumber instance;
        if (lc.getLanguage().equalsIgnoreCase("ko")) {
            instance = new NameToNumberKorean(t9Chars, t9Digits);
        } else if (lc.equals(Locale.CHINA)) {
            instance = new NameToNumberChinese(t9Chars, t9Digits);
        } else {
            instance = new NameToNumber(t9Chars, t9Digits);
        }