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

Commit fcffaad1 authored by emancebo's avatar emancebo Committed by Ed Carrigan
Browse files

Read option for t9 search input locale and refresh smart dial db when locale changes

Change-Id: I3cd942741310f16678ed9eb675bbba2cbc72d5e8
parent 007c74cd
Loading
Loading
Loading
Loading
+23 −2
Original line number Diff line number Diff line
@@ -29,12 +29,14 @@ import android.app.FragmentTransaction;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.PreferenceManager;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents;
@@ -87,6 +89,7 @@ import com.android.internal.telephony.ITelephony;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * The dialer tab's title is 'phone', a more common name (see strings.xml).
@@ -103,6 +106,7 @@ public class DialtactsActivity extends TransactionSafeActivity implements View.O
    public static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    public static final String SHARED_PREFS_NAME = "com.android.dialer_preferences";
    private static final String PREF_LAST_T9_LOCALE = "smart_dial_prefix_last_t9_locale";

    /** Used to open Call Setting */
    private static final String PHONE_PACKAGE = "com.android.phone";
@@ -362,7 +366,6 @@ public class DialtactsActivity extends TransactionSafeActivity implements View.O
        hideDialpadFragment(false, false);

        mDialerDatabaseHelper = DatabaseHelperManager.getDatabaseHelper(this);
        SmartDialPrefix.initializeNanpSettings(this);
    }

    @Override
@@ -376,8 +379,26 @@ public class DialtactsActivity extends TransactionSafeActivity implements View.O
        }
        prepareVoiceSearchButton();
        mFirstLaunch = false;

        // make this call on resume in case user changed t9 locale in settings
        SmartDialPrefix.initializeNanpSettings(this);

        // if locale has changed since last time, refresh the smart dial db
        Locale locale = SmartDialPrefix.getT9SearchInputLocale(this);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String prevLocale = prefs.getString(PREF_LAST_T9_LOCALE, null);

        if (!TextUtils.equals(locale.toString(), prevLocale)) {
            mDialerDatabaseHelper.recreateSmartDialDatabaseInBackground();
            if (mDialpadFragment != null)
                mDialpadFragment.refreshKeypad();

            prefs.edit().putString(PREF_LAST_T9_LOCALE, locale.toString()).apply();
        }
        else {
            mDialerDatabaseHelper.startSmartDialUpdateThread();
        }
    }

    @Override
    protected void onPause() {
+42 −0
Original line number Diff line number Diff line
@@ -512,6 +512,48 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
            super.onPostExecute(o);
        }
    }

    /**
     * Deletes all smart dial data and recreates it from contacts
     */
    public void recreateSmartDialDatabaseInBackground() { new SmartDialRecreateAsyncTask().execute(); }

    private class SmartDialRecreateAsyncTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] objects) {
            if (DEBUG) {
                Log.v(TAG, "Recreating database");
            }

            // reset last updated so that we query for all contacts
            resetSmartDialLastUpdatedTime();

            // clear all contacts
            final SQLiteDatabase db = getWritableDatabase();
            removeAllContacts(db);

            // repopulate
            updateSmartDialDatabase();
            return null;
        }

        @Override
        protected void onCancelled() {
            if (DEBUG) {
                Log.v(TAG, "Recreate Cancelled");
            }
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Object o) {
            if (DEBUG) {
                Log.v(TAG, "Recreate Finished");
            }
            super.onPostExecute(o);
        }
    }

    /**
     * Removes rows in the smartdial database that matches the contacts that have been deleted
     * by other apps since last update.
+25 −6
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
@@ -87,6 +88,7 @@ import com.android.phone.common.HapticFeedback;
import com.google.common.annotations.VisibleForTesting;

import java.util.HashSet;
import java.util.Locale;

/**
 * Fragment that displays a twelve-key phone dialpad.
@@ -410,11 +412,7 @@ public class DialpadFragment extends Fragment
        mDigits.setOnLongClickListener(this);
        mDigits.addTextChangedListener(this);
        PhoneNumberFormatter.setPhoneNumberFormattingTextWatcher(getActivity(), mDigits);
        // Check for the presence of the keypad
        View oneButton = fragmentView.findViewById(R.id.one);
        if (oneButton != null) {
        setupKeypad(fragmentView);
        }

        mDelete = fragmentView.findViewById(R.id.deleteButton);
        if (mDelete != null) {
@@ -605,6 +603,11 @@ public class DialpadFragment extends Fragment
    }

    private void setupKeypad(View fragmentView) {
        // make sure keypad is there
        View oneButton = fragmentView.findViewById(R.id.one);
        if (oneButton == null)
            return;

        final int[] buttonIds = new int[] {R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
                R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine, R.id.star, R.id.pound};

@@ -626,7 +629,9 @@ public class DialpadFragment extends Fragment
                R.string.dialpad_8_2_letters, R.string.dialpad_9_2_letters,
                R.string.dialpad_star_2_letters, R.string.dialpad_pound_2_letters};

        final Resources resources = getResources();
        // load the dialpad resources based on the t9 serach input locale
        Locale t9SearchInputLocale = SmartDialPrefix.getT9SearchInputLocale(getActivity());
        final Resources resources = getResourcesForLocale(t9SearchInputLocale);

        DialpadKeyButton dialpadKey;
        TextView numberView;
@@ -664,6 +669,12 @@ public class DialpadFragment extends Fragment

    }

    public void refreshKeypad() {
        View fragmentView = getView();
        if (fragmentView != null)
            setupKeypad(fragmentView);
    }

    @Override
    public void onResume() {
        super.onResume();
@@ -1752,4 +1763,12 @@ public class DialpadFragment extends Fragment
    public void setYFraction(float yFraction) {
        ((DialpadSlidingLinearLayout) getView()).setYFraction(yFraction);
    }

    private Resources getResourcesForLocale(Locale locale) {
        Configuration defaultConfig = getResources().getConfiguration();
        Configuration overrideConfig = new Configuration(defaultConfig);
        overrideConfig.setLocale(locale);
        Context localeContext = getActivity().createConfigurationContext(overrideConfig);
        return localeContext.getResources();
    }
}
+50 −14
Original line number Diff line number Diff line
@@ -23,12 +23,19 @@ import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.android.dialer.database.DialerDatabaseHelper;
import com.android.dialerbind.DatabaseHelperManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;

import android.util.Log;

/**
 * Smart Dial utility class to find prefixes of contacts. It contains both methods to find supported
@@ -69,6 +76,24 @@ public class SmartDialPrefix {

    private static boolean sNanpInitialized = false;

    private static final Map<String, SmartDialMap> languageToSmartDialMap = new HashMap<String, SmartDialMap>();
    static {
        languageToSmartDialMap.put("ko", new KoreanSmartDialMap());
        languageToSmartDialMap.put("el", new GreekSmartDialMap());
        languageToSmartDialMap.put("ru", new RussianSmartDialMap());
        languageToSmartDialMap.put("he", new HebrewSmartDialMap());
        languageToSmartDialMap.put("zh", new ChineseSmartDialMap());
    }

    private static final Map<String, SmartDialMap> countryToSmartDialMap = new HashMap<String, SmartDialMap>();
    static {
        languageToSmartDialMap.put("KR", new KoreanSmartDialMap());
        languageToSmartDialMap.put("GR", new GreekSmartDialMap());
        languageToSmartDialMap.put("RU", new RussianSmartDialMap());
        languageToSmartDialMap.put("IL", new HebrewSmartDialMap());
        languageToSmartDialMap.put("CN", new ChineseSmartDialMap());
    }

    /** Initializes the Nanp settings, and finds out whether user is in a NANP region.*/
    public static void initializeNanpSettings(Context context){
        final TelephonyManager manager = (TelephonyManager) context.getSystemService(
@@ -90,25 +115,36 @@ public class SmartDialPrefix {
        /** Queries the NANP country list to find out whether user is in a NANP region.*/
        sUserInNanpRegion = isCountryNanp(sUserSimCountryCode);

        /** Sets a layout for SmartDial depending on current UI language.*/
        String locale = context.getResources().getConfiguration().locale.getCountry();
        if (locale.equals("RU")) {
            mMap = new RussianSmartDialMap();
        } else if (locale.equals("GR")) {
            mMap = new GreekSmartDialMap();
        } else if (locale.equals("IL")) {
            mMap = new HebrewSmartDialMap();
        } else if (locale.equals("KR")) {
            mMap = new KoreanSmartDialMap();
        } else if (locale.equals("CN")) {
            mMap = new ChineseSmartDialMap();
        } else {
        /** Sets a layout for SmartDial based on locale.  Lookup by language first and fallback to country */
        Locale locale = getT9SearchInputLocale(context);
        mMap = languageToSmartDialMap.get(locale.getLanguage());
        if (mMap == null)
            mMap = countryToSmartDialMap.get(locale.getCountry());
        if (mMap == null)
            mMap = new LatinSmartDialMap();
        }

        sNanpInitialized = true;
    }

    public static Locale getT9SearchInputLocale(Context context) {
        // Use system locale by default
        Locale locale = context.getResources().getConfiguration().locale;

        // Override with t9 search input locale from settings if provided
        String overrideLocaleString = android.provider.Settings.System.getString(
                context.getContentResolver(),
                android.provider.Settings.System.T9_SEARCH_INPUT_LOCALE);
        if (overrideLocaleString != null && !overrideLocaleString.isEmpty()) {
            String[] tokens = overrideLocaleString.split("_");
            String lang = tokens.length > 0 ? tokens[0] : "";
            String country = tokens.length > 1 ? tokens[1] : "";
            String variant = tokens.length > 2 ? tokens[2] : "";
            locale = new Locale(lang, country, variant);
        }

        return locale;
    }

    /**
     * Explicitly setting the user Nanp to the given boolean
     */