Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.inputmethod;
     18 
     19 import com.android.settings.R;
     20 
     21 import android.app.AlertDialog;
     22 import android.content.ActivityNotFoundException;
     23 import android.content.DialogInterface;
     24 import android.content.Intent;
     25 import android.content.res.Resources;
     26 import android.preference.Preference;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.view.View.OnClickListener;
     31 import android.view.textservice.SpellCheckerInfo;
     32 import android.view.textservice.SpellCheckerSubtype;
     33 import android.view.textservice.TextServicesManager;
     34 import android.widget.ImageView;
     35 import android.widget.RadioButton;
     36 import android.widget.TextView;
     37 import android.widget.Toast;
     38 
     39 public class SingleSpellCheckerPreference extends Preference {
     40     private static final float DISABLED_ALPHA = 0.4f;
     41     private static final String TAG = SingleSpellCheckerPreference.class.getSimpleName();
     42     private static final boolean DBG = false;
     43 
     44     private final SpellCheckerInfo mSpellCheckerInfo;
     45 
     46     private final SpellCheckersSettings mFragment;
     47     private final Resources mRes;
     48     private final TextServicesManager mTsm;
     49     private AlertDialog mDialog = null;
     50     private TextView mTitleText;
     51     private TextView mSummaryText;
     52     private View mPrefAll;
     53     private RadioButton mRadioButton;
     54     private View mPrefLeftButton;
     55     private View mSettingsButton;
     56     private ImageView mSubtypeButton;
     57     private Intent mSettingsIntent;
     58     private boolean mSelected;
     59 
     60     public SingleSpellCheckerPreference(SpellCheckersSettings fragment, Intent settingsIntent,
     61             SpellCheckerInfo sci, TextServicesManager tsm) {
     62         super(fragment.getActivity(), null, 0);
     63         mFragment = fragment;
     64         mRes = fragment.getActivity().getResources();
     65         mTsm = tsm;
     66         setLayoutResource(R.layout.preference_spellchecker);
     67         mSpellCheckerInfo = sci;
     68         mSelected = false;
     69         final String settingsActivity = mSpellCheckerInfo.getSettingsActivity();
     70         if (!TextUtils.isEmpty(settingsActivity)) {
     71             mSettingsIntent = new Intent(Intent.ACTION_MAIN);
     72             mSettingsIntent.setClassName(mSpellCheckerInfo.getPackageName(), settingsActivity);
     73         } else {
     74             mSettingsIntent = null;
     75         }
     76     }
     77 
     78     @Override
     79     protected void onBindView(View view) {
     80         super.onBindView(view);
     81         mPrefAll = view.findViewById(R.id.pref_all);
     82         mRadioButton = (RadioButton)view.findViewById(R.id.pref_radio);
     83         mPrefLeftButton = view.findViewById(R.id.pref_left_button);
     84         mPrefLeftButton.setOnClickListener(
     85                 new OnClickListener() {
     86                     @Override
     87                     public void onClick(View arg0) {
     88                         onLeftButtonClicked(arg0);
     89                     }
     90                 });
     91         mTitleText = (TextView)view.findViewById(android.R.id.title);
     92         mSummaryText = (TextView)view.findViewById(android.R.id.summary);
     93         mSubtypeButton = (ImageView)view.findViewById(R.id.pref_right_button2);
     94         mSubtypeButton.setOnClickListener(
     95                 new OnClickListener() {
     96                     @Override
     97                     public void onClick(View arg0) {
     98                         onSubtypeButtonClicked(arg0);
     99                     }
    100                 });
    101         mSettingsButton = view.findViewById(R.id.pref_right_button1);
    102         mSettingsButton.setOnClickListener(
    103                 new OnClickListener() {
    104                     @Override
    105                     public void onClick(View arg0) {
    106                         onSettingsButtonClicked(arg0);
    107                     }
    108                 });
    109         updateSelectedState(mSelected);
    110     }
    111 
    112     private void onLeftButtonClicked(View arg0) {
    113         mFragment.onPreferenceClick(this);
    114     }
    115 
    116     public SpellCheckerInfo getSpellCheckerInfo() {
    117         return mSpellCheckerInfo;
    118     }
    119 
    120     private void updateSelectedState(boolean selected) {
    121         if (mPrefAll != null) {
    122             mRadioButton.setChecked(selected);
    123             enableButtons(selected);
    124         }
    125     }
    126 
    127     public void setSelected(boolean selected) {
    128         mSelected = selected;
    129         updateSelectedState(selected);
    130     }
    131 
    132     private void onSubtypeButtonClicked(View arg0) {
    133         if (mDialog != null && mDialog.isShowing()) {
    134             mDialog.dismiss();
    135         }
    136         final AlertDialog.Builder builder = new AlertDialog.Builder(mFragment.getActivity());
    137         builder.setTitle(R.string.phone_language);
    138         final int size = mSpellCheckerInfo.getSubtypeCount();
    139         final CharSequence[] items = new CharSequence[size + 1];
    140         items[0] = mRes.getString(R.string.use_system_language_to_select_input_method_subtypes);
    141         for (int i = 0; i < size; ++i) {
    142             final SpellCheckerSubtype subtype = mSpellCheckerInfo.getSubtypeAt(i);
    143             final CharSequence label = subtype.getDisplayName(
    144                     mFragment.getActivity(), mSpellCheckerInfo.getPackageName(),
    145                     mSpellCheckerInfo.getServiceInfo().applicationInfo);
    146             items[i + 1] = label;
    147         }
    148         // default: "Use system language"
    149         int checkedItem = 0;
    150         // Allow no implicitly selected subtypes
    151         final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype(false);
    152         if (currentScs != null) {
    153             for (int i = 0; i < size; ++i) {
    154                 if (mSpellCheckerInfo.getSubtypeAt(i).equals(currentScs)) {
    155                     checkedItem = i + 1;
    156                     break;
    157                 }
    158             }
    159         }
    160         builder.setSingleChoiceItems(items, checkedItem, new AlertDialog.OnClickListener() {
    161             @Override
    162             public void onClick(DialogInterface dialog, int which) {
    163                 if (which == 0) {
    164                     mTsm.setSpellCheckerSubtype(null);
    165                 } else {
    166                     mTsm.setSpellCheckerSubtype(mSpellCheckerInfo.getSubtypeAt(which - 1));
    167                 }
    168                 if (DBG) {
    169                     final SpellCheckerSubtype subtype = mTsm.getCurrentSpellCheckerSubtype(true);
    170                     Log.d(TAG, "Current spell check locale is "
    171                             + subtype == null ? "null" : subtype.getLocale());
    172                 }
    173                 dialog.dismiss();
    174             }
    175         });
    176         mDialog = builder.create();
    177         mDialog.show();
    178     }
    179 
    180     private void onSettingsButtonClicked(View arg0) {
    181         if (mFragment != null && mSettingsIntent != null) {
    182             try {
    183                 mFragment.startActivity(mSettingsIntent);
    184             } catch (ActivityNotFoundException e) {
    185                 final String msg = mFragment.getString(R.string.failed_to_open_app_settings_toast,
    186                         mSpellCheckerInfo.loadLabel(mFragment.getActivity().getPackageManager()));
    187                 Toast.makeText(mFragment.getActivity(), msg, Toast.LENGTH_LONG).show();
    188             }
    189         }
    190     }
    191 
    192     private void enableButtons(boolean enabled) {
    193         if (mSettingsButton != null) {
    194             if (mSettingsIntent == null) {
    195                 mSettingsButton.setVisibility(View.GONE);
    196             } else {
    197                 mSettingsButton.setEnabled(enabled);
    198                 mSettingsButton.setClickable(enabled);
    199                 mSettingsButton.setFocusable(enabled);
    200                 if (!enabled) {
    201                     mSettingsButton.setAlpha(DISABLED_ALPHA);
    202                 }
    203             }
    204         }
    205         if (mSubtypeButton != null) {
    206             if (mSpellCheckerInfo.getSubtypeCount() <= 0) {
    207                 mSubtypeButton.setVisibility(View.GONE);
    208             } else {
    209                 mSubtypeButton.setEnabled(enabled);
    210                 mSubtypeButton.setClickable(enabled);
    211                 mSubtypeButton.setFocusable(enabled);
    212                 if (!enabled) {
    213                     mSubtypeButton.setAlpha(DISABLED_ALPHA);
    214                 }
    215             }
    216         }
    217     }
    218 }
    219