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