From 00b49cad9cc8a350bde8210d50c4b638908ef1c4 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Sun, 27 Jul 2014 15:28:48 +0900
Subject: [PATCH] Use fragment for text correction preference screen

Bug: 16522808
Change-Id: Ia8ce83b0e69cc8157f7b8cefcfeb863eb85ee4b4
---
 java/res/xml/prefs.xml                        |  58 +--------
 java/res/xml/prefs_screen_correction.xml      |  76 +++++++++++
 .../settings/CorrectionSettingsFragment.java  | 123 ++++++++++++++++++
 .../latin/settings/SettingsFragment.java      |  70 ----------
 .../latin/utils/FragmentUtils.java            |   2 +
 5 files changed, 203 insertions(+), 126 deletions(-)
 create mode 100644 java/res/xml/prefs_screen_correction.xml
 create mode 100644 java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java

diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index 494cdc91dd..09c6f74eac 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -37,63 +37,9 @@
         android:title="@string/settings_screen_gesture"
         android:key="screen_gesture" />
     <PreferenceScreen
+        android:fragment="com.android.inputmethod.latin.settings.CorrectionSettingsFragment"
         android:title="@string/settings_screen_correction"
-        android:key="screen_correction">
-        <PreferenceScreen
-            android:key="edit_personal_dictionary"
-            android:title="@string/edit_personal_dictionary">
-            <intent android:action="android.settings.USER_DICTIONARY_SETTINGS" />
-        </PreferenceScreen>
-        <PreferenceScreen
-            android:key="configure_dictionaries_key"
-            android:title="@string/configure_dictionaries_title">
-           <intent
-              android:action="android.intent.action.MAIN"
-              android:targetClass="@string/dictionary_pack_settings_activity">
-             <extra
-                 android:name="clientId"
-                 android:value="@string/dictionary_pack_client_id" />
-           </intent>
-        </PreferenceScreen>
-        <CheckBoxPreference
-            android:key="pref_key_block_potentially_offensive"
-            android:title="@string/prefs_block_potentially_offensive_title"
-            android:summary="@string/prefs_block_potentially_offensive_summary"
-            android:defaultValue="@bool/config_block_potentially_offensive"
-            android:persistent="true" />
-        <ListPreference
-            android:key="auto_correction_threshold"
-            android:title="@string/auto_correction"
-            android:summary="@string/auto_correction_summary"
-            android:entryValues="@array/auto_correction_threshold_mode_indexes"
-            android:entries="@array/auto_correction_threshold_modes"
-            android:defaultValue="@string/auto_correction_threshold_mode_index_modest"
-            android:persistent="true" />
-        <CheckBoxPreference
-            android:key="show_suggestions"
-            android:summary="@string/prefs_show_suggestions_summary"
-            android:title="@string/prefs_show_suggestions"
-            android:defaultValue="true"
-            android:persistent="true" />
-        <CheckBoxPreference
-            android:key="pref_key_use_personalized_dicts"
-            android:title="@string/use_personalized_dicts"
-            android:summary="@string/use_personalized_dicts_summary"
-            android:defaultValue="true"
-            android:persistent="true" />
-        <CheckBoxPreference
-            android:key="pref_key_use_contacts_dict"
-            android:title="@string/use_contacts_dict"
-            android:summary="@string/use_contacts_dict_summary"
-            android:defaultValue="true"
-            android:persistent="true" />
-        <CheckBoxPreference
-            android:key="next_word_prediction"
-            android:title="@string/bigram_prediction"
-            android:summary="@string/bigram_prediction_summary"
-            android:defaultValue="true"
-            android:persistent="true" />
-        </PreferenceScreen>
+        android:key="screen_correction" />
     <PreferenceScreen
         android:title="@string/settings_screen_advanced"
         android:key="screen_advanced">
diff --git a/java/res/xml/prefs_screen_correction.xml b/java/res/xml/prefs_screen_correction.xml
new file mode 100644
index 0000000000..dd5ba540c5
--- /dev/null
+++ b/java/res/xml/prefs_screen_correction.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 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.
+-->
+
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+    android:title="@string/settings_screen_correction"
+    android:key="screen_correction">
+    <PreferenceScreen
+        android:key="edit_personal_dictionary"
+        android:title="@string/edit_personal_dictionary">
+        <intent android:action="android.settings.USER_DICTIONARY_SETTINGS" />
+    </PreferenceScreen>
+    <PreferenceScreen
+        android:key="configure_dictionaries_key"
+        android:title="@string/configure_dictionaries_title">
+        <intent
+            android:action="android.intent.action.MAIN"
+            android:targetClass="@string/dictionary_pack_settings_activity">
+            <extra
+                android:name="clientId"
+                android:value="@string/dictionary_pack_client_id" />
+        </intent>
+    </PreferenceScreen>
+    <CheckBoxPreference
+        android:key="pref_key_block_potentially_offensive"
+        android:title="@string/prefs_block_potentially_offensive_title"
+        android:summary="@string/prefs_block_potentially_offensive_summary"
+        android:defaultValue="@bool/config_block_potentially_offensive"
+        android:persistent="true" />
+    <ListPreference
+        android:key="auto_correction_threshold"
+        android:title="@string/auto_correction"
+        android:summary="@string/auto_correction_summary"
+        android:entryValues="@array/auto_correction_threshold_mode_indexes"
+        android:entries="@array/auto_correction_threshold_modes"
+        android:defaultValue="@string/auto_correction_threshold_mode_index_modest"
+        android:persistent="true" />
+    <CheckBoxPreference
+        android:key="show_suggestions"
+        android:summary="@string/prefs_show_suggestions_summary"
+        android:title="@string/prefs_show_suggestions"
+        android:defaultValue="true"
+        android:persistent="true" />
+    <CheckBoxPreference
+        android:key="pref_key_use_personalized_dicts"
+        android:title="@string/use_personalized_dicts"
+        android:summary="@string/use_personalized_dicts_summary"
+        android:defaultValue="true"
+        android:persistent="true" />
+    <CheckBoxPreference
+        android:key="pref_key_use_contacts_dict"
+        android:title="@string/use_contacts_dict"
+        android:summary="@string/use_contacts_dict_summary"
+        android:defaultValue="true"
+        android:persistent="true" />
+    <CheckBoxPreference
+        android:key="next_word_prediction"
+        android:title="@string/bigram_prediction"
+        android:summary="@string/bigram_prediction_summary"
+        android:defaultValue="true"
+        android:persistent="true" />
+</PreferenceScreen>
diff --git a/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java
new file mode 100644
index 0000000000..ec29a7eb22
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/settings/CorrectionSettingsFragment.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2014 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.android.inputmethod.latin.settings;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Build;
+import android.os.Bundle;
+import android.preference.ListPreference;
+import android.preference.Preference;
+
+import com.android.inputmethod.dictionarypack.DictionarySettingsActivity;
+import com.android.inputmethod.latin.R;
+import com.android.inputmethod.latin.userdictionary.UserDictionaryList;
+import com.android.inputmethod.latin.userdictionary.UserDictionarySettings;
+
+import java.util.TreeSet;
+
+/**
+ * "Text correction" settings sub screen.
+ *
+ * This settings sub screen handles the following text correction preferences.
+ * - Personal dictionary
+ * - Add-on dictionaries
+ * - Block offensive words
+ * - Auto-correction
+ * - Show correction suggestions
+ * - Personalized suggestions
+ * - Suggest Contact names
+ * - Next-word suggestions
+ */
+public final class CorrectionSettingsFragment extends SubScreenFragment {
+    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 <= Build.VERSION_CODES.JELLY_BEAN_MR2;
+
+    @Override
+    public void onCreate(final Bundle icicle) {
+        super.onCreate(icicle);
+        addPreferencesFromResource(R.xml.prefs_screen_correction);
+
+        final Context context = getActivity();
+        final PackageManager pm = context.getPackageManager();
+
+        ensureConsistencyOfAutoCorrectionSettings();
+
+        final Preference dictionaryLink = findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY);
+        final Intent intent = dictionaryLink.getIntent();
+        intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName());
+        final int number = pm.queryIntentActivities(intent, 0).size();
+        if (0 >= number) {
+            removePreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY);
+        }
+
+        final Preference editPersonalDictionary =
+                findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY);
+        final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent();
+        final ResolveInfo ri = USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS ? null
+                : pm.resolveActivity(
+                        editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY);
+        if (ri == null) {
+            overwriteUserDictionaryPreference(editPersonalDictionary);
+        }
+    }
+
+    @Override
+    public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
+        ensureConsistencyOfAutoCorrectionSettings();
+    }
+
+    private void ensureConsistencyOfAutoCorrectionSettings() {
+        final String autoCorrectionOff = getString(
+                R.string.auto_correction_threshold_mode_index_off);
+        final ListPreference autoCorrectionThresholdPref = (ListPreference)findPreference(
+                Settings.PREF_AUTO_CORRECTION_THRESHOLD);
+        final String currentSetting = autoCorrectionThresholdPref.getValue();
+        setPreferenceEnabled(
+                Settings.PREF_BIGRAM_PREDICTIONS, !currentSetting.equals(autoCorrectionOff));
+    }
+
+    private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) {
+        final Activity activity = getActivity();
+        final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity);
+        if (null == localeList) {
+            // The locale list is null if and only if the user dictionary service is
+            // not present or disabled. In this case we need to remove the preference.
+            getPreferenceScreen().removePreference(userDictionaryPreference);
+        } else if (localeList.size() <= 1) {
+            userDictionaryPreference.setFragment(UserDictionarySettings.class.getName());
+            // If the size of localeList is 0, we don't set the locale parameter in the
+            // extras. This will be interpreted by the UserDictionarySettings class as
+            // meaning "the current locale".
+            // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet()
+            // the locale list always has at least one element, since it always includes the current
+            // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet().
+            if (localeList.size() == 1) {
+                final String locale = (String)localeList.toArray()[0];
+                userDictionaryPreference.getExtras().putString("locale", locale);
+            }
+        } else {
+            userDictionaryPreference.setFragment(UserDictionaryList.class.getName());
+        }
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
index 363cb498d5..0f0166143a 100644
--- a/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/SettingsFragment.java
@@ -21,11 +21,8 @@ import android.app.backup.BackupManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
-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.ListPreference;
 import android.preference.Preference;
@@ -37,27 +34,18 @@ import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 
-import com.android.inputmethod.dictionarypack.DictionarySettingsActivity;
 import com.android.inputmethod.keyboard.KeyboardTheme;
 import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.define.ProductionFlags;
 import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager;
-import com.android.inputmethod.latin.userdictionary.UserDictionaryList;
-import com.android.inputmethod.latin.userdictionary.UserDictionarySettings;
 import com.android.inputmethod.latin.utils.ApplicationUtils;
 import com.android.inputmethod.latin.utils.FeedbackUtils;
 import com.android.inputmethodcommon.InputMethodSettingsFragment;
 
-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_PERSONAL_DICTIONARY_SETTINGS = false;
-    private static final boolean USE_INTERNAL_PERSONAL_DICTIONARY_SETTIGS =
-            DBG_USE_INTERNAL_PERSONAL_DICTIONARY_SETTINGS
-            || Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2;
 
     private static final int NO_MENU_GROUP = Menu.NONE; // We don't care about menu grouping.
     private static final int MENU_FEEDBACK = Menu.FIRST; // The first menu item id and order.
@@ -115,10 +103,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment
         final SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
         prefs.registerOnSharedPreferenceChangeListener(this);
 
-        ensureConsistencyOfAutoCorrectionSettings();
-
-        final PreferenceScreen correctionScreen =
-                (PreferenceScreen) findPreference(Settings.SCREEN_CORRECTION);
         final PreferenceScreen advancedScreen =
                 (PreferenceScreen) findPreference(Settings.SCREEN_ADVANCED);
         final PreferenceScreen debugScreen =
@@ -160,15 +144,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment
             removePreference(Settings.PREF_SHOW_SETUP_WIZARD_ICON, advancedScreen);
         }
 
-        final PreferenceScreen dictionaryLink =
-                (PreferenceScreen) findPreference(Settings.PREF_CONFIGURE_DICTIONARIES_KEY);
-        final Intent intent = dictionaryLink.getIntent();
-        intent.setClassName(context.getPackageName(), DictionarySettingsActivity.class.getName());
-        final int number = context.getPackageManager().queryIntentActivities(intent, 0).size();
-        if (0 >= number) {
-            correctionScreen.removePreference(dictionaryLink);
-        }
-
         if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) {
             final Preference enableMetricsLogging =
                     findPreference(Settings.PREF_ENABLE_METRICS_LOGGING);
@@ -183,16 +158,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment
             removePreference(Settings.PREF_ENABLE_METRICS_LOGGING, advancedScreen);
         }
 
-        final Preference editPersonalDictionary =
-                findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY);
-        final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent();
-        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)) {
             getPreferenceScreen().removePreference(findPreference(Settings.SCREEN_GESTURE));
         }
@@ -261,22 +226,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment
         } else if (key.equals(Settings.PREF_SHOW_SETUP_WIZARD_ICON)) {
             LauncherIconVisibilityManager.updateSetupWizardIconVisibility(getActivity());
         }
-        ensureConsistencyOfAutoCorrectionSettings();
         updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY);
         updateListPreferenceSummaryToCurrentValue(Settings.PREF_KEYBOARD_THEME);
         refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, getResources());
     }
 
-    private void ensureConsistencyOfAutoCorrectionSettings() {
-        final String autoCorrectionOff = getResources().getString(
-                R.string.auto_correction_threshold_mode_index_off);
-        final ListPreference autoCorrectionThresholdPref = (ListPreference)findPreference(
-                Settings.PREF_AUTO_CORRECTION_THRESHOLD);
-        final String currentSetting = autoCorrectionThresholdPref.getValue();
-        setPreferenceEnabled(
-                Settings.PREF_BIGRAM_PREDICTIONS, !currentSetting.equals(autoCorrectionOff));
-    }
-
     private void refreshEnablingsOfKeypressSoundAndVibrationSettings(
             final SharedPreferences sp, final Resources res) {
         setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS,
@@ -382,30 +336,6 @@ public final class SettingsFragment extends InputMethodSettingsFragment
         });
     }
 
-    private void overwriteUserDictionaryPreference(Preference userDictionaryPreference) {
-        final Activity activity = getActivity();
-        final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity);
-        if (null == localeList) {
-            // The locale list is null if and only if the user dictionary service is
-            // not present or disabled. In this case we need to remove the preference.
-            getPreferenceScreen().removePreference(userDictionaryPreference);
-        } else if (localeList.size() <= 1) {
-            userDictionaryPreference.setFragment(UserDictionarySettings.class.getName());
-            // If the size of localeList is 0, we don't set the locale parameter in the
-            // extras. This will be interpreted by the UserDictionarySettings class as
-            // meaning "the current locale".
-            // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet()
-            // the locale list always has at least one element, since it always includes the current
-            // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet().
-            if (localeList.size() == 1) {
-                final String locale = (String)localeList.toArray()[0];
-                userDictionaryPreference.getExtras().putString("locale", locale);
-            }
-        } else {
-            userDictionaryPreference.setFragment(UserDictionaryList.class.getName());
-        }
-    }
-
     @Override
     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
         if (FeedbackUtils.isFeedbackFormSupported()) {
diff --git a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java b/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java
index 002231aea0..bb26b575cd 100644
--- a/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/FragmentUtils.java
@@ -18,6 +18,7 @@ package com.android.inputmethod.latin.utils;
 
 import com.android.inputmethod.dictionarypack.DictionarySettingsFragment;
 import com.android.inputmethod.latin.about.AboutPreferences;
+import com.android.inputmethod.latin.settings.CorrectionSettingsFragment;
 import com.android.inputmethod.latin.settings.CustomInputStyleSettingsFragment;
 import com.android.inputmethod.latin.settings.DebugSettings;
 import com.android.inputmethod.latin.settings.GestureSettingsFragment;
@@ -41,6 +42,7 @@ public class FragmentUtils {
         sLatinImeFragments.add(MultiLingualSettingsFragment.class.getName());
         sLatinImeFragments.add(CustomInputStyleSettingsFragment.class.getName());
         sLatinImeFragments.add(GestureSettingsFragment.class.getName());
+        sLatinImeFragments.add(CorrectionSettingsFragment.class.getName());
         sLatinImeFragments.add(DebugSettings.class.getName());
         sLatinImeFragments.add(SettingsFragment.class.getName());
         sLatinImeFragments.add(SpellCheckerSettingsFragment.class.getName());
-- 
GitLab