satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 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.inputmethodcommon; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.preference.Preference; |
| 23 | import android.preference.Preference.OnPreferenceClickListener; |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 24 | import android.preference.PreferenceScreen; |
| 25 | import android.provider.Settings; |
| 26 | import android.text.TextUtils; |
| 27 | import android.view.inputmethod.InputMethodInfo; |
| 28 | import android.view.inputmethod.InputMethodManager; |
satok | f332097 | 2011-09-12 16:55:28 +0900 | [diff] [blame] | 29 | import android.view.inputmethod.InputMethodSubtype; |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /* package private */ class InputMethodSettingsImpl implements InputMethodSettingsInterface { |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 34 | private Preference mSubtypeEnablerPreference; |
| 35 | private int mInputMethodSettingsCategoryTitleRes; |
| 36 | private CharSequence mInputMethodSettingsCategoryTitle; |
| 37 | private int mSubtypeEnablerTitleRes; |
| 38 | private CharSequence mSubtypeEnablerTitle; |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 39 | private int mSubtypeEnablerIconRes; |
| 40 | private Drawable mSubtypeEnablerIcon; |
satok | f332097 | 2011-09-12 16:55:28 +0900 | [diff] [blame] | 41 | private InputMethodManager mImm; |
| 42 | private InputMethodInfo mImi; |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Initialize internal states of this object. |
| 46 | * @param context the context for this application. |
| 47 | * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment. |
| 48 | * @return true if this application is an IME and has two or more subtypes, false otherwise. |
| 49 | */ |
| 50 | public boolean init(final Context context, final PreferenceScreen prefScreen) { |
satok | f332097 | 2011-09-12 16:55:28 +0900 | [diff] [blame] | 51 | mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 52 | mImi = getMyImi(context, mImm); |
| 53 | if (mImi == null || mImi.getSubtypeCount() <= 1) { |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 54 | return false; |
| 55 | } |
Tadashi G. Takaoka | 990fcb1 | 2014-07-23 10:40:47 -0700 | [diff] [blame] | 56 | final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
| 57 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId()); |
| 58 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 59 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 60 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 61 | mSubtypeEnablerPreference = new Preference(context); |
Tadashi G. Takaoka | 990fcb1 | 2014-07-23 10:40:47 -0700 | [diff] [blame] | 62 | mSubtypeEnablerPreference.setIntent(intent); |
satok | 9ce9a54 | 2011-09-05 19:02:07 +0900 | [diff] [blame] | 63 | prefScreen.addPreference(mSubtypeEnablerPreference); |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 64 | updateSubtypeEnabler(); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | private static InputMethodInfo getMyImi(Context context, InputMethodManager imm) { |
| 69 | final List<InputMethodInfo> imis = imm.getInputMethodList(); |
| 70 | for (int i = 0; i < imis.size(); ++i) { |
| 71 | final InputMethodInfo imi = imis.get(i); |
| 72 | if (imis.get(i).getPackageName().equals(context.getPackageName())) { |
| 73 | return imi; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
satok | f332097 | 2011-09-12 16:55:28 +0900 | [diff] [blame] | 79 | private static String getEnabledSubtypesLabel( |
| 80 | Context context, InputMethodManager imm, InputMethodInfo imi) { |
| 81 | if (context == null || imm == null || imi == null) return null; |
| 82 | final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); |
| 83 | final StringBuilder sb = new StringBuilder(); |
| 84 | final int N = subtypes.size(); |
| 85 | for (int i = 0; i < N; ++i) { |
| 86 | final InputMethodSubtype subtype = subtypes.get(i); |
| 87 | if (sb.length() > 0) { |
| 88 | sb.append(", "); |
| 89 | } |
| 90 | sb.append(subtype.getDisplayName(context, imi.getPackageName(), |
| 91 | imi.getServiceInfo().applicationInfo)); |
| 92 | } |
| 93 | return sb.toString(); |
| 94 | } |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 95 | /** |
| 96 | * {@inheritDoc} |
| 97 | */ |
| 98 | @Override |
| 99 | public void setInputMethodSettingsCategoryTitle(int resId) { |
| 100 | mInputMethodSettingsCategoryTitleRes = resId; |
| 101 | updateSubtypeEnabler(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | */ |
| 107 | @Override |
| 108 | public void setInputMethodSettingsCategoryTitle(CharSequence title) { |
| 109 | mInputMethodSettingsCategoryTitleRes = 0; |
| 110 | mInputMethodSettingsCategoryTitle = title; |
| 111 | updateSubtypeEnabler(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * {@inheritDoc} |
| 116 | */ |
| 117 | @Override |
| 118 | public void setSubtypeEnablerTitle(int resId) { |
| 119 | mSubtypeEnablerTitleRes = resId; |
| 120 | updateSubtypeEnabler(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * {@inheritDoc} |
| 125 | */ |
| 126 | @Override |
| 127 | public void setSubtypeEnablerTitle(CharSequence title) { |
| 128 | mSubtypeEnablerTitleRes = 0; |
| 129 | mSubtypeEnablerTitle = title; |
| 130 | updateSubtypeEnabler(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritDoc} |
| 135 | */ |
| 136 | @Override |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 137 | public void setSubtypeEnablerIcon(int resId) { |
| 138 | mSubtypeEnablerIconRes = resId; |
| 139 | updateSubtypeEnabler(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * {@inheritDoc} |
| 144 | */ |
| 145 | @Override |
| 146 | public void setSubtypeEnablerIcon(Drawable drawable) { |
| 147 | mSubtypeEnablerIconRes = 0; |
| 148 | mSubtypeEnablerIcon = drawable; |
| 149 | updateSubtypeEnabler(); |
| 150 | } |
| 151 | |
satok | f332097 | 2011-09-12 16:55:28 +0900 | [diff] [blame] | 152 | public void updateSubtypeEnabler() { |
Tadashi G. Takaoka | 990fcb1 | 2014-07-23 10:40:47 -0700 | [diff] [blame] | 153 | final Preference pref = mSubtypeEnablerPreference; |
| 154 | if (pref == null) { |
| 155 | return; |
| 156 | } |
| 157 | final Context context = pref.getContext(); |
| 158 | final CharSequence title; |
| 159 | if (mSubtypeEnablerTitleRes != 0) { |
| 160 | title = context.getString(mSubtypeEnablerTitleRes); |
| 161 | } else { |
| 162 | title = mSubtypeEnablerTitle; |
| 163 | } |
| 164 | pref.setTitle(title); |
| 165 | final Intent intent = pref.getIntent(); |
| 166 | if (intent != null) { |
| 167 | intent.putExtra(Intent.EXTRA_TITLE, title); |
| 168 | } |
| 169 | final String summary = getEnabledSubtypesLabel(context, mImm, mImi); |
| 170 | if (!TextUtils.isEmpty(summary)) { |
| 171 | pref.setSummary(summary); |
| 172 | } |
| 173 | if (mSubtypeEnablerIconRes != 0) { |
| 174 | pref.setIcon(mSubtypeEnablerIconRes); |
| 175 | } else { |
| 176 | pref.setIcon(mSubtypeEnablerIcon); |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 177 | } |
satok | c9a8771 | 2011-07-11 07:42:47 +0900 | [diff] [blame] | 178 | } |
| 179 | } |