Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2014 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 android.content.ActivityNotFoundException;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.preference.Preference;
     23 import android.text.TextUtils;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 import android.view.textservice.SpellCheckerInfo;
     27 import android.widget.RadioButton;
     28 import android.widget.Toast;
     29 
     30 import com.android.settings.R;
     31 import com.android.settings.Utils;
     32 
     33 /**
     34  * Spell checker service preference.
     35  *
     36  * This preference represents a spell checker service. It is used for two purposes. 1) A radio
     37  * button on the left side is used to choose the current spell checker service. 2) A settings
     38  * icon on the right side is used to invoke the setting activity of the spell checker service.
     39  */
     40 class SpellCheckerPreference extends Preference implements OnClickListener {
     41     interface OnRadioButtonPreferenceListener {
     42         /**
     43          * Called when this preference needs to be saved its state.
     44          *
     45          * Note that this preference is non-persistent and needs explicitly to be saved its state.
     46          * Because changing one IME state may change other IMEs' state, this is a place to update
     47          * other IMEs' state as well.
     48          *
     49          * @param pref This preference.
     50          */
     51         public void onRadioButtonClicked(SpellCheckerPreference pref);
     52     }
     53 
     54     private final SpellCheckerInfo mSci;
     55     private final OnRadioButtonPreferenceListener mOnRadioButtonListener;
     56 
     57     private RadioButton mRadioButton;
     58     private View mPrefLeftButton;
     59     private View mSettingsButton;
     60     private boolean mSelected;
     61 
     62     public SpellCheckerPreference(final Context context, final SpellCheckerInfo sci,
     63             final OnRadioButtonPreferenceListener onRadioButtonListener) {
     64         super(context, null, 0);
     65         setPersistent(false);
     66         setLayoutResource(R.layout.preference_spellchecker);
     67         setWidgetLayoutResource(R.layout.preference_spellchecker_widget);
     68         mSci = sci;
     69         mOnRadioButtonListener = onRadioButtonListener;
     70         setKey(sci.getId());
     71         setTitle(sci.loadLabel(context.getPackageManager()));
     72         final String settingsActivity = mSci.getSettingsActivity();
     73         if (TextUtils.isEmpty(settingsActivity)) {
     74             setIntent(null);
     75         } else {
     76             final Intent intent = new Intent(Intent.ACTION_MAIN);
     77             intent.setClassName(mSci.getPackageName(), settingsActivity);
     78             setIntent(intent);
     79         }
     80     }
     81 
     82     @Override
     83     protected void onBindView(View view) {
     84         super.onBindView(view);
     85         mRadioButton = (RadioButton)view.findViewById(R.id.pref_radio);
     86         mPrefLeftButton = view.findViewById(R.id.pref_left_button);
     87         mPrefLeftButton.setOnClickListener(this);
     88         mSettingsButton = view.findViewById(R.id.pref_right_button);
     89         mSettingsButton.setOnClickListener(this);
     90         updateSelectedState(mSelected);
     91     }
     92 
     93     @Override
     94     public void onClick(final View v) {
     95         if (v == mPrefLeftButton) {
     96             mOnRadioButtonListener.onRadioButtonClicked(this);
     97             return;
     98         }
     99         if (v == mSettingsButton) {
    100             onSettingsButtonClicked();
    101             return;
    102         }
    103     }
    104 
    105     private void onSettingsButtonClicked() {
    106         final Context context = getContext();
    107         try {
    108             final Intent intent = getIntent();
    109             if (intent != null) {
    110                 // Invoke a settings activity of an spell checker.
    111                 context.startActivity(intent);
    112             }
    113         } catch (final ActivityNotFoundException e) {
    114             final String message = context.getString(R.string.failed_to_open_app_settings_toast,
    115                     mSci.loadLabel(context.getPackageManager()));
    116             Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    117         }
    118     }
    119 
    120     public SpellCheckerInfo getSpellCheckerInfo() {
    121         return mSci;
    122     }
    123 
    124     public void setSelected(final boolean selected) {
    125         mSelected = selected;
    126         updateSelectedState(selected);
    127     }
    128 
    129     private void updateSelectedState(final boolean selected) {
    130         if (mRadioButton != null) {
    131             mRadioButton.setChecked(selected);
    132             enableSettingsButton(isEnabled() && selected);
    133         }
    134     }
    135 
    136     private void enableSettingsButton(final boolean enabled) {
    137         if (mSettingsButton == null) {
    138             return;
    139         }
    140         if (getIntent() == null) {
    141             mSettingsButton.setVisibility(View.GONE);
    142         } else {
    143             mSettingsButton.setEnabled(enabled);
    144             mSettingsButton.setClickable(enabled);
    145             mSettingsButton.setFocusable(enabled);
    146             if (!enabled) {
    147                 mSettingsButton.setAlpha(Utils.DISABLED_ALPHA);
    148             }
    149         }
    150     }
    151 }
    152