Home | History | Annotate | Download | only in inputmethodcommon
      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 /**
     18  * This is a part of the inputmethod-common static Java library.
     19  * The original source code can be found at frameworks/opt/inputmethodcommon of Android Open Source
     20  * Project.
     21  */
     22 
     23 package com.android.inputmethodcommon;
     24 
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.graphics.drawable.Drawable;
     28 import android.preference.Preference;
     29 import android.preference.Preference.OnPreferenceClickListener;
     30 import android.preference.PreferenceScreen;
     31 import android.provider.Settings;
     32 import android.text.TextUtils;
     33 import android.view.inputmethod.InputMethodInfo;
     34 import android.view.inputmethod.InputMethodManager;
     35 import android.view.inputmethod.InputMethodSubtype;
     36 
     37 import java.util.List;
     38 
     39 /* package private */ class InputMethodSettingsImpl implements InputMethodSettingsInterface {
     40     private Preference mSubtypeEnablerPreference;
     41     private int mInputMethodSettingsCategoryTitleRes;
     42     private CharSequence mInputMethodSettingsCategoryTitle;
     43     private int mSubtypeEnablerTitleRes;
     44     private CharSequence mSubtypeEnablerTitle;
     45     private int mSubtypeEnablerIconRes;
     46     private Drawable mSubtypeEnablerIcon;
     47     private InputMethodManager mImm;
     48     private InputMethodInfo mImi;
     49     private Context mContext;
     50 
     51     /**
     52      * Initialize internal states of this object.
     53      * @param context the context for this application.
     54      * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment.
     55      * @return true if this application is an IME and has two or more subtypes, false otherwise.
     56      */
     57     public boolean init(final Context context, final PreferenceScreen prefScreen) {
     58         mContext = context;
     59         mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
     60         mImi = getMyImi(context, mImm);
     61         if (mImi == null || mImi.getSubtypeCount() <= 1) {
     62             return false;
     63         }
     64         mSubtypeEnablerPreference = new Preference(context);
     65         mSubtypeEnablerPreference
     66                 .setOnPreferenceClickListener(new OnPreferenceClickListener() {
     67                     @Override
     68                     public boolean onPreferenceClick(Preference preference) {
     69                         final CharSequence title = getSubtypeEnablerTitle(context);
     70                         final Intent intent =
     71                                 new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
     72                         intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId());
     73                         if (!TextUtils.isEmpty(title)) {
     74                             intent.putExtra(Intent.EXTRA_TITLE, title);
     75                         }
     76                         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     77                                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
     78                                 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
     79                         context.startActivity(intent);
     80                         return true;
     81                     }
     82                 });
     83         prefScreen.addPreference(mSubtypeEnablerPreference);
     84         updateSubtypeEnabler();
     85         return true;
     86     }
     87 
     88     private static InputMethodInfo getMyImi(Context context, InputMethodManager imm) {
     89         final List<InputMethodInfo> imis = imm.getInputMethodList();
     90         for (int i = 0; i < imis.size(); ++i) {
     91             final InputMethodInfo imi = imis.get(i);
     92             if (imis.get(i).getPackageName().equals(context.getPackageName())) {
     93                 return imi;
     94             }
     95         }
     96         return null;
     97     }
     98 
     99     private static String getEnabledSubtypesLabel(
    100             Context context, InputMethodManager imm, InputMethodInfo imi) {
    101         if (context == null || imm == null || imi == null) return null;
    102         final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
    103         final StringBuilder sb = new StringBuilder();
    104         final int N = subtypes.size();
    105         for (int i = 0; i < N; ++i) {
    106             final InputMethodSubtype subtype = subtypes.get(i);
    107             if (sb.length() > 0) {
    108                 sb.append(", ");
    109             }
    110             sb.append(subtype.getDisplayName(context, imi.getPackageName(),
    111                     imi.getServiceInfo().applicationInfo));
    112         }
    113         return sb.toString();
    114     }
    115     /**
    116      * {@inheritDoc}
    117      */
    118     @Override
    119     public void setInputMethodSettingsCategoryTitle(int resId) {
    120         mInputMethodSettingsCategoryTitleRes = resId;
    121         updateSubtypeEnabler();
    122     }
    123 
    124     /**
    125      * {@inheritDoc}
    126      */
    127     @Override
    128     public void setInputMethodSettingsCategoryTitle(CharSequence title) {
    129         mInputMethodSettingsCategoryTitleRes = 0;
    130         mInputMethodSettingsCategoryTitle = title;
    131         updateSubtypeEnabler();
    132     }
    133 
    134     /**
    135      * {@inheritDoc}
    136      */
    137     @Override
    138     public void setSubtypeEnablerTitle(int resId) {
    139         mSubtypeEnablerTitleRes = resId;
    140         updateSubtypeEnabler();
    141     }
    142 
    143     /**
    144      * {@inheritDoc}
    145      */
    146     @Override
    147     public void setSubtypeEnablerTitle(CharSequence title) {
    148         mSubtypeEnablerTitleRes = 0;
    149         mSubtypeEnablerTitle = title;
    150         updateSubtypeEnabler();
    151     }
    152 
    153     /**
    154      * {@inheritDoc}
    155      */
    156     @Override
    157     public void setSubtypeEnablerIcon(int resId) {
    158         mSubtypeEnablerIconRes = resId;
    159         updateSubtypeEnabler();
    160     }
    161 
    162     /**
    163      * {@inheritDoc}
    164      */
    165     @Override
    166     public void setSubtypeEnablerIcon(Drawable drawable) {
    167         mSubtypeEnablerIconRes = 0;
    168         mSubtypeEnablerIcon = drawable;
    169         updateSubtypeEnabler();
    170     }
    171 
    172     private CharSequence getSubtypeEnablerTitle(Context context) {
    173         if (mSubtypeEnablerTitleRes != 0) {
    174             return context.getString(mSubtypeEnablerTitleRes);
    175         } else {
    176             return mSubtypeEnablerTitle;
    177         }
    178     }
    179 
    180     public void updateSubtypeEnabler() {
    181         if (mSubtypeEnablerPreference != null) {
    182             if (mSubtypeEnablerTitleRes != 0) {
    183                 mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitleRes);
    184             } else if (!TextUtils.isEmpty(mSubtypeEnablerTitle)) {
    185                 mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitle);
    186             }
    187             final String summary = getEnabledSubtypesLabel(mContext, mImm, mImi);
    188             if (!TextUtils.isEmpty(summary)) {
    189                 mSubtypeEnablerPreference.setSummary(summary);
    190             }
    191             if (mSubtypeEnablerIconRes != 0) {
    192                 mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIconRes);
    193             } else if (mSubtypeEnablerIcon != null) {
    194                 mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIcon);
    195             }
    196         }
    197     }
    198 }
    199