SpellChecker settings

Change-Id: I0d7af408d2b70e8e593e490484526dd8fffdc725
diff --git a/res/layout/preference_spellchecker.xml b/res/layout/preference_spellchecker.xml
index e804f17..d8b4a75 100644
--- a/res/layout/preference_spellchecker.xml
+++ b/res/layout/preference_spellchecker.xml
@@ -13,7 +13,6 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/pref_all"
@@ -30,10 +29,15 @@
         android:clickable="true"
         android:focusable="true"
         android:background="?android:attr/selectableItemBackground">
+        <RadioButton
+            android:id="@+id/pref_radio"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:clickable="false"
+            android:focusable="false" />
         <RelativeLayout
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginLeft="15dip"
             android:layout_marginRight="6dip"
             android:layout_marginTop="6dip"
             android:layout_marginBottom="6dip"
@@ -45,7 +49,7 @@
                 android:singleLine="true"
                 android:textAppearance="?android:attr/textAppearanceMedium"
                 android:ellipsize="marquee"
-                android:fadingEdge="horizontal"/>
+                android:fadingEdge="horizontal" />
             <TextView
                 android:id="@android:id/summary"
                 android:layout_width="wrap_content"
@@ -59,22 +63,44 @@
                 android:maxLines="4" />
         </RelativeLayout>
     </LinearLayout>
+    <LinearLayout
+        android:id="@+id/pref_right_button1"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:gravity="center_vertical"
+        android:clickable="true"
+        android:focusable="true"
+        android:background="?android:attr/selectableItemBackground" >
+        <View
+            android:layout_width="2dip"
+            android:layout_height="match_parent"
+            android:layout_marginTop="5dip"
+            android:layout_marginBottom="5dip"
+            android:background="@android:drawable/divider_horizontal_dark" />
+        <ImageView
+            android:layout_width="40dip"
+            android:layout_height="fill_parent"
+            android:paddingLeft="5dip"
+            android:paddingRight="5dip"
+            android:src="@drawable/ic_sysbar_quicksettings"
+            android:layout_gravity="center" />
+    </LinearLayout>
     <View
+        android:id="@+id/pref_right_separator2"
         android:layout_width="2dip"
         android:layout_height="match_parent"
         android:layout_marginTop="5dip"
         android:layout_marginBottom="5dip"
         android:background="@android:drawable/divider_horizontal_dark" />
     <ImageView
-        android:id="@+id/pref_right_button"
-        android:layout_width="wrap_content"
+        android:id="@+id/pref_right_button2"
+        android:layout_width="40dip"
         android:layout_height="fill_parent"
-        android:paddingLeft="15dip"
-        android:paddingRight="?android:attr/scrollbarSize"
-        android:src="@drawable/ic_sysbar_quicksettings"
-        android:contentDescription="@string/input_method_settings_button"
-        android:layout_gravity="center"
+        android:paddingLeft="5dip"
+        android:paddingRight="5dip"
+        android:src="@drawable/ic_menu_3d_globe"
+        android:layout_gravity="right"
         android:clickable="true"
         android:focusable="true"
         android:background="?android:attr/selectableItemBackground" />
-</LinearLayout>
+</LinearLayout>
\ No newline at end of file
diff --git a/src/com/android/settings/inputmethod/SingleSpellCheckerPreference.java b/src/com/android/settings/inputmethod/SingleSpellCheckerPreference.java
index 98ca3af..510d15f 100644
--- a/src/com/android/settings/inputmethod/SingleSpellCheckerPreference.java
+++ b/src/com/android/settings/inputmethod/SingleSpellCheckerPreference.java
@@ -17,15 +17,21 @@
 package com.android.settings.inputmethod;
 
 import com.android.settings.R;
-import com.android.settings.SettingsPreferenceFragment;
 
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 import android.content.Intent;
-
+import android.content.res.Resources;
 import android.preference.Preference;
+import android.provider.Settings;
+import android.text.TextUtils;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.textservice.SpellCheckerInfo;
+import android.view.textservice.SpellCheckerSubtype;
+import android.view.textservice.TextServicesManager;
 import android.widget.ImageView;
+import android.widget.RadioButton;
 import android.widget.TextView;
 
 public class SingleSpellCheckerPreference extends Preference {
@@ -33,27 +39,42 @@
 
     private final SpellCheckerInfo mSpellCheckerInfo;
 
-    private SettingsPreferenceFragment mFragment;
+    private final SpellCheckersSettings mFragment;
+    private final Resources mRes;
+    private final TextServicesManager mTsm;
     private TextView mTitleText;
     private TextView mSummaryText;
     private View mPrefAll;
+    private RadioButton mRadioButton;
     private View mPrefLeftButton;
-    private ImageView mSetingsButton;
+    private View mSettingsButton;
+    private ImageView mSubtypeButton;
     private Intent mSettingsIntent;
     private boolean mSelected;
 
-    public SingleSpellCheckerPreference(SettingsPreferenceFragment fragment, Intent settingsIntent,
-            SpellCheckerInfo sci) {
+    public SingleSpellCheckerPreference(SpellCheckersSettings fragment, Intent settingsIntent,
+            SpellCheckerInfo sci, TextServicesManager tsm) {
         super(fragment.getActivity(), null, 0);
+        mFragment = fragment;
+        mRes = fragment.getActivity().getResources();
+        mTsm = tsm;
         setLayoutResource(R.layout.preference_spellchecker);
         mSpellCheckerInfo = sci;
         mSelected = false;
+        final String settingsActivity = mSpellCheckerInfo.getSettingsActivity();
+        if (!TextUtils.isEmpty(settingsActivity)) {
+            mSettingsIntent = new Intent(Intent.ACTION_MAIN);
+            mSettingsIntent.setClassName(mSpellCheckerInfo.getPackageName(), settingsActivity);
+        } else {
+            mSettingsIntent = null;
+        }
     }
 
     @Override
     protected void onBindView(View view) {
         super.onBindView(view);
         mPrefAll = view.findViewById(R.id.pref_all);
+        mRadioButton = (RadioButton)view.findViewById(R.id.pref_radio);
         mPrefLeftButton = view.findViewById(R.id.pref_left_button);
         mPrefLeftButton.setOnClickListener(
                 new OnClickListener() {
@@ -62,10 +83,18 @@
                         onLeftButtonClicked(arg0);
                     }
                 });
-        mSetingsButton = (ImageView)view.findViewById(R.id.pref_right_button);
         mTitleText = (TextView)view.findViewById(android.R.id.title);
         mSummaryText = (TextView)view.findViewById(android.R.id.summary);
-        mSetingsButton.setOnClickListener(
+        mSubtypeButton = (ImageView)view.findViewById(R.id.pref_right_button2);
+        mSubtypeButton.setOnClickListener(
+                new OnClickListener() {
+                    @Override
+                    public void onClick(View arg0) {
+                        onSubtypeButtonClicked(arg0);
+                    }
+                });
+        mSettingsButton = view.findViewById(R.id.pref_right_button1);
+        mSettingsButton.setOnClickListener(
                 new OnClickListener() {
                     @Override
                     public void onClick(View arg0) {
@@ -76,48 +105,92 @@
     }
 
     private void onLeftButtonClicked(View arg0) {
-        final OnPreferenceClickListener listener = getOnPreferenceClickListener();
-        if (listener != null) {
-            listener.onPreferenceClick(this);
-        }
+        mFragment.onPreferenceClick(this);
     }
 
     public SpellCheckerInfo getSpellCheckerInfo() {
         return mSpellCheckerInfo;
     }
 
-    public void updateSelectedState(boolean selected) {
+    private void updateSelectedState(boolean selected) {
         if (mPrefAll != null) {
-            if (selected) {
-                // TODO: Use a color defined by the design guideline.
-                mPrefAll.setBackgroundColor(0x88006666);
-            } else {
-                mPrefAll.setBackgroundColor(0);
-            }
-            enableSettingsButton(selected);
+            mRadioButton.setChecked(selected);
+            enableButtons(selected);
         }
     }
 
     public void setSelected(boolean selected) {
         mSelected = selected;
+        updateSelectedState(selected);
     }
 
-    protected void onSettingsButtonClicked(View arg0) {
+    private void onSubtypeButtonClicked(View arg0) {
+        final AlertDialog.Builder builder = new AlertDialog.Builder(mFragment.getActivity());
+        builder.setTitle(R.string.phone_language);
+        final int size = mSpellCheckerInfo.getSubtypeCount();
+        final CharSequence[] items = new CharSequence[size + 1];
+        items[0] = mRes.getString(R.string.use_system_language_to_select_input_method_subtypes);
+        for (int i = 0; i < size; ++i) {
+            final SpellCheckerSubtype subtype = mSpellCheckerInfo.getSubtypeAt(i);
+            final CharSequence label = subtype.getDisplayName(
+                    mFragment.getActivity(), mSpellCheckerInfo.getPackageName(),
+                    mSpellCheckerInfo.getServiceInfo().applicationInfo);
+            items[i + 1] = label;
+        }
+        // default: "Use system language"
+        int checkedItem = 0;
+        // Allow no implicitly selected subtypes
+        final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype(false);
+        if (currentScs != null) {
+            for (int i = 0; i < size; ++i) {
+                if (mSpellCheckerInfo.getSubtypeAt(i).equals(currentScs)) {
+                    checkedItem = i + 1;
+                    break;
+                }
+            }
+        }
+        builder.setSingleChoiceItems(items, checkedItem, new AlertDialog.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                if (which == 0) {
+                    mTsm.setSpellCheckerSubtype(null);
+                } else {
+                    mTsm.setSpellCheckerSubtype(mSpellCheckerInfo.getSubtypeAt(which - 1));
+                }
+                dialog.dismiss();
+            }
+        });
+        builder.show();
+    }
+
+    private void onSettingsButtonClicked(View arg0) {
         if (mFragment != null && mSettingsIntent != null) {
             mFragment.startActivity(mSettingsIntent);
         }
     }
 
-    private void enableSettingsButton(boolean enabled) {
-        if (mSetingsButton != null) {
+    private void enableButtons(boolean enabled) {
+        if (mSettingsButton != null) {
             if (mSettingsIntent == null) {
-                mSetingsButton.setVisibility(View.GONE);
+                mSettingsButton.setVisibility(View.GONE);
             } else {
-                mSetingsButton.setEnabled(enabled);
-                mSetingsButton.setClickable(enabled);
-                mSetingsButton.setFocusable(enabled);
+                mSettingsButton.setEnabled(enabled);
+                mSettingsButton.setClickable(enabled);
+                mSettingsButton.setFocusable(enabled);
                 if (!enabled) {
-                    mSetingsButton.setAlpha(DISABLED_ALPHA);
+                    mSettingsButton.setAlpha(DISABLED_ALPHA);
+                }
+            }
+        }
+        if (mSubtypeButton != null) {
+            if (mSpellCheckerInfo.getSubtypeCount() <= 0) {
+                mSubtypeButton.setVisibility(View.GONE);
+            } else {
+                mSubtypeButton.setEnabled(enabled);
+                mSubtypeButton.setClickable(enabled);
+                mSubtypeButton.setFocusable(enabled);
+                if (!enabled) {
+                    mSubtypeButton.setAlpha(DISABLED_ALPHA);
                 }
             }
         }
diff --git a/src/com/android/settings/inputmethod/SpellCheckersSettings.java b/src/com/android/settings/inputmethod/SpellCheckersSettings.java
index d6c0b1c..9a85fc6 100644
--- a/src/com/android/settings/inputmethod/SpellCheckersSettings.java
+++ b/src/com/android/settings/inputmethod/SpellCheckersSettings.java
@@ -20,6 +20,7 @@
 import com.android.settings.SettingsPreferenceFragment;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.PreferenceScreen;
@@ -72,6 +73,7 @@
     }
 
     private void updateEnabledSpellCheckers() {
+        final PackageManager pm = getPackageManager();
         mCurrentSci = SpellCheckerUtils.getCurrentSpellChecker(mTsm);
         mEnabledScis = SpellCheckerUtils.getEnabledSpellCheckers(mTsm);
         if (mCurrentSci == null || mEnabledScis == null) {
@@ -81,9 +83,9 @@
         for (int i = 0; i < mEnabledScis.length; ++i) {
             final SpellCheckerInfo sci = mEnabledScis[i];
             final SingleSpellCheckerPreference scPref = new SingleSpellCheckerPreference(
-                    this, null, sci);
+                    this, null, sci, mTsm);
             mSpellCheckers.add(scPref);
-            scPref.setTitle(sci.getId());
+            scPref.setTitle(sci.loadLabel(pm));
             scPref.setSelected(mCurrentSci != null && mCurrentSci.getId().equals(sci.getId()));
             getPreferenceScreen().addPreference(scPref);
         }
@@ -93,12 +95,10 @@
     public boolean onPreferenceClick(Preference arg0) {
         for (SingleSpellCheckerPreference scp : mSpellCheckers) {
             if (arg0.equals(scp)) {
-                scp.setSelected(true);
                 mTsm.setCurrentSpellChecker(scp.getSpellCheckerInfo());
-            } else {
-                scp.setSelected(false);
             }
         }
+        updateScreen();
         return true;
     }
 }