blob: cfa1a659614bc18f5e252a616f890a12f85cc3b1 [file] [log] [blame]
satokc9a87712011-07-11 07:42:47 +09001/*
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;
satokc9a87712011-07-11 07:42:47 +090024import android.preference.PreferenceScreen;
25import android.provider.Settings;
26import android.text.TextUtils;
27import android.view.inputmethod.InputMethodInfo;
28import android.view.inputmethod.InputMethodManager;
satokf3320972011-09-12 16:55:28 +090029import android.view.inputmethod.InputMethodSubtype;
satokc9a87712011-07-11 07:42:47 +090030
31import java.util.List;
32
33/* package private */ class InputMethodSettingsImpl implements InputMethodSettingsInterface {
satokc9a87712011-07-11 07:42:47 +090034 private Preference mSubtypeEnablerPreference;
35 private int mInputMethodSettingsCategoryTitleRes;
36 private CharSequence mInputMethodSettingsCategoryTitle;
37 private int mSubtypeEnablerTitleRes;
38 private CharSequence mSubtypeEnablerTitle;
satokc9a87712011-07-11 07:42:47 +090039 private int mSubtypeEnablerIconRes;
40 private Drawable mSubtypeEnablerIcon;
satokf3320972011-09-12 16:55:28 +090041 private InputMethodManager mImm;
42 private InputMethodInfo mImi;
satokc9a87712011-07-11 07:42:47 +090043
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) {
satokf3320972011-09-12 16:55:28 +090051 mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
52 mImi = getMyImi(context, mImm);
53 if (mImi == null || mImi.getSubtypeCount() <= 1) {
satokc9a87712011-07-11 07:42:47 +090054 return false;
55 }
Tadashi G. Takaoka990fcb12014-07-23 10:40:47 -070056 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);
satokc9a87712011-07-11 07:42:47 +090061 mSubtypeEnablerPreference = new Preference(context);
Tadashi G. Takaoka990fcb12014-07-23 10:40:47 -070062 mSubtypeEnablerPreference.setIntent(intent);
satok9ce9a542011-09-05 19:02:07 +090063 prefScreen.addPreference(mSubtypeEnablerPreference);
satokc9a87712011-07-11 07:42:47 +090064 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
satokf3320972011-09-12 16:55:28 +090079 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 }
satokc9a87712011-07-11 07:42:47 +090095 /**
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
satokc9a87712011-07-11 07:42:47 +0900137 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
satokf3320972011-09-12 16:55:28 +0900152 public void updateSubtypeEnabler() {
Tadashi G. Takaoka990fcb12014-07-23 10:40:47 -0700153 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);
satokc9a87712011-07-11 07:42:47 +0900177 }
satokc9a87712011-07-11 07:42:47 +0900178 }
179}