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.app.AlertDialog.Builder;
     20 import android.content.ActivityNotFoundException;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.support.v7.preference.ListPreference;
     25 import android.support.v7.preference.PreferenceViewHolder;
     26 import android.text.TextUtils;
     27 import android.view.View;
     28 import android.view.View.OnClickListener;
     29 import android.view.textservice.SpellCheckerInfo;
     30 
     31 import com.android.settings.CustomListPreference;
     32 import com.android.settings.R;
     33 
     34 /**
     35  * Spell checker service preference.
     36  *
     37  * This preference represents a spell checker service. It is used for two purposes. 1) A radio
     38  * button on the left side is used to choose the current spell checker service. 2) A settings
     39  * icon on the right side is used to invoke the setting activity of the spell checker service.
     40  */
     41 class SpellCheckerPreference extends CustomListPreference {
     42 
     43     private final SpellCheckerInfo[] mScis;
     44     private Intent mIntent;
     45 
     46     public SpellCheckerPreference(final Context context, final SpellCheckerInfo[] scis) {
     47         super(context, null);
     48         mScis = scis;
     49         setWidgetLayoutResource(R.layout.preference_widget_settings);
     50         CharSequence[] labels = new CharSequence[scis.length];
     51         CharSequence[] values = new CharSequence[scis.length];
     52         for (int i = 0 ; i < scis.length; i++) {
     53             labels[i] = scis[i].loadLabel(context.getPackageManager());
     54             // Use values as indexing since ListPreference doesn't support generic objects.
     55             values[i] = String.valueOf(i);
     56         }
     57         setEntries(labels);
     58         setEntryValues(values);
     59     }
     60 
     61     @Override
     62     protected void onPrepareDialogBuilder(Builder builder,
     63             DialogInterface.OnClickListener listener) {
     64         builder.setTitle(R.string.choose_spell_checker);
     65         builder.setSingleChoiceItems(getEntries(), findIndexOfValue(getValue()), listener);
     66     }
     67 
     68     public void setSelected(SpellCheckerInfo currentSci) {
     69         if (currentSci == null) {
     70             setValue(null);
     71             return;
     72         }
     73         for (int i = 0; i < mScis.length; i++) {
     74             if (mScis[i].getId().equals(currentSci.getId())) {
     75                 setValueIndex(i);
     76                 return;
     77             }
     78         }
     79     }
     80 
     81     @Override
     82     public void setValue(String value) {
     83         super.setValue(value);
     84         int index = value != null ? Integer.parseInt(value) : -1;
     85         if (index == -1) {
     86             mIntent = null;
     87             return;
     88         }
     89         SpellCheckerInfo sci = mScis[index];
     90         final String settingsActivity = sci.getSettingsActivity();
     91         if (TextUtils.isEmpty(settingsActivity)) {
     92             mIntent = null;
     93         } else {
     94             mIntent = new Intent(Intent.ACTION_MAIN);
     95             mIntent.setClassName(sci.getPackageName(), settingsActivity);
     96         }
     97     }
     98 
     99     @Override
    100     public boolean callChangeListener(Object newValue) {
    101         newValue = newValue != null ? mScis[Integer.parseInt((String) newValue)] : null;
    102         return super.callChangeListener(newValue);
    103     }
    104 
    105     @Override
    106     public void onBindViewHolder(PreferenceViewHolder view) {
    107         super.onBindViewHolder(view);
    108         View settingsButton = view.findViewById(R.id.settings_button);
    109         settingsButton.setVisibility(mIntent != null ? View.VISIBLE : View.INVISIBLE);
    110         settingsButton.setOnClickListener(new OnClickListener() {
    111             @Override
    112             public void onClick(View v) {
    113                 onSettingsButtonClicked();
    114             }
    115         });
    116     }
    117 
    118     private void onSettingsButtonClicked() {
    119         final Context context = getContext();
    120         try {
    121             final Intent intent = mIntent;
    122             if (intent != null) {
    123                 // Invoke a settings activity of an spell checker.
    124                 context.startActivity(intent);
    125             }
    126         } catch (final ActivityNotFoundException e) {
    127         }
    128     }
    129 }
    130