From a2bfd46adf6aca5864be3d6ef2204bc18008e9c9 Mon Sep 17 00:00:00 2001
From: Satoshi Kataoka <satok@google.com>
Date: Thu, 27 Jun 2013 14:10:50 +0900
Subject: [PATCH] Use internal personal dictionary settings on JBMR2 or before

Bug: 9117704
Change-Id: I7e20b03daa23b59211235183cf48d933e32845e4
---
 .../inputmethod/latin/SettingsFragment.java   | 17 ++++++---
 .../userdictionary/UserDictionaryList.java    | 38 ++++++++++++++++---
 2 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java
index 46fa1913cb..f52e56441e 100644
--- a/java/src/com/android/inputmethod/latin/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java
@@ -25,6 +25,7 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
 import android.media.AudioManager;
+import android.os.Build;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
 import android.preference.ListPreference;
@@ -51,7 +52,10 @@ import java.util.TreeSet;
 public final class SettingsFragment extends InputMethodSettingsFragment
         implements SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String TAG = SettingsFragment.class.getSimpleName();
-    private static final boolean DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS = false;
+    private static final boolean DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS = false;
+    private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS =
+            DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS
+                    || Build.VERSION.SDK_INT <= 18 /* Build.VERSION.JELLY_BEAN_MR2 */;
 
     private ListPreference mVoicePreference;
     private ListPreference mShowCorrectionSuggestionsPreference;
@@ -212,10 +216,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment
         final Preference editPersonalDictionary =
                 findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY);
         final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent();
-        final ResolveInfo ri = context.getPackageManager().resolveActivity(
-                editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY);
-        if (DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS || ri == null) {
-            updateUserDictionaryPreference(editPersonalDictionary);
+        final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS ? null
+                : context.getPackageManager().resolveActivity(
+                        editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY);
+        if (ri == null) {
+            overwriteUserDictionaryPreference(editPersonalDictionary);
         }
 
         if (!Settings.readFromBuildConfigIfGestureInputEnabled(res)) {
@@ -470,7 +475,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment
         });
     }
 
-    private void updateUserDictionaryPreference(Preference userDictionaryPreference) {
+    private void overwriteUserDictionaryPreference(Preference userDictionaryPreference) {
         final Activity activity = getActivity();
         final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity);
         if (null == localeList) {
diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
index e7cf0d3af3..be5f11620f 100644
--- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
+++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
@@ -17,6 +17,7 @@
 package com.android.inputmethod.latin.userdictionary;
 
 import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.os.Bundle;
@@ -25,10 +26,14 @@ import android.preference.PreferenceFragment;
 import android.preference.PreferenceGroup;
 import android.provider.UserDictionary;
 import android.text.TextUtils;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.utils.LocaleUtils;
 
+import java.util.List;
 import java.util.Locale;
 import java.util.TreeSet;
 
@@ -52,7 +57,7 @@ public class UserDictionaryList extends PreferenceFragment {
         final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
                 new String[] { UserDictionary.Words.LOCALE },
                 null, null, null);
-        final TreeSet<String> localeList = new TreeSet<String>();
+        final TreeSet<String> localeSet = new TreeSet<String>();
         boolean addedAllLocale = false;
         if (null == cursor) {
             // The user dictionary service is not present or disabled. Return null.
@@ -62,7 +67,7 @@ public class UserDictionaryList extends PreferenceFragment {
             do {
                 final String locale = cursor.getString(columnIndex);
                 final boolean allLocale = TextUtils.isEmpty(locale);
-                localeList.add(allLocale ? "" : locale);
+                localeSet.add(allLocale ? "" : locale);
                 if (allLocale) {
                     addedAllLocale = true;
                 }
@@ -71,10 +76,33 @@ public class UserDictionaryList extends PreferenceFragment {
         if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED && !addedAllLocale) {
             // For ICS, we need to show "For all languages" in case that the keyboard locale
             // is different from the system locale
-            localeList.add("");
+            localeSet.add("");
         }
-        localeList.add(Locale.getDefault().toString());
-        return localeList;
+
+        final InputMethodManager imm =
+                (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
+        final List<InputMethodInfo> imis = imm.getEnabledInputMethodList();
+        for (final InputMethodInfo imi : imis) {
+            final List<InputMethodSubtype> subtypes =
+                    imm.getEnabledInputMethodSubtypeList(
+                            imi, true /* allowsImplicitlySelectedSubtypes */);
+            for (InputMethodSubtype subtype : subtypes) {
+                final String locale = subtype.getLocale();
+                if (!TextUtils.isEmpty(locale)) {
+                    localeSet.add(locale);
+                }
+            }
+        }
+
+        // We come here after we have collected locales from existing user dictionary entries and
+        // enabled subtypes. If we already have the locale-without-country version of the system
+        // locale, we don't add the system locale to avoid confusion even though it's technically
+        // correct to add it.
+        if (!localeSet.contains(Locale.getDefault().getLanguage().toString())) {
+            localeSet.add(Locale.getDefault().toString());
+        }
+
+        return localeSet;
     }
 
     /**
-- 
GitLab