From 1ecdb22b4e7b97e34a0b5e557732b65aa00af4b7 Mon Sep 17 00:00:00 2001 From: Satoshi Kataoka Date: Thu, 4 Jul 2013 20:07:30 +0900 Subject: [PATCH] Use InputMethodUtils in input method settings Change-Id: Ifd054c86b7099db3984e559feb5fadf1df5ca227 --- .../InputMethodAndLanguageSettings.java | 25 ++-- .../InputMethodAndSubtypeUtil.java | 22 ---- .../InputMethodSettingValuesWrapper.java | 107 ++++++++++++++++++ 3 files changed, 123 insertions(+), 31 deletions(-) create mode 100644 src/com/android/settings/inputmethod/InputMethodSettingValuesWrapper.java diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java index 48acac8efcd..465895541a0 100644 --- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java +++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java @@ -83,12 +83,12 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment new ArrayList(); private InputManager mIm; private InputMethodManager mImm; - private List mImis; private boolean mIsOnlyImeSettings; private Handler mHandler; @SuppressWarnings("unused") private SettingsObserver mSettingsObserver; private Intent mIntentWaitingForResult; + private InputMethodSettingValuesWrapper mInputMethodSettingValues; @Override public void onCreate(Bundle icicle) { @@ -140,7 +140,7 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment // Build IME preference category. mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - mImis = mImm.getInputMethodList(); + mInputMethodSettingValues = InputMethodSettingValuesWrapper.getInstance(getActivity()); mKeyboardSettingsCategory.removeAll(); if (!mIsOnlyImeSettings) { @@ -151,9 +151,10 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment } mInputMethodPreferenceList.clear(); - final int N = (mImis == null ? 0 : mImis.size()); + final List imis = mInputMethodSettingValues.getInputMethodList(); + final int N = (imis == null ? 0 : imis.size()); for (int i = 0; i < N; ++i) { - final InputMethodInfo imi = mImis.get(i); + final InputMethodInfo imi = imis.get(i); final InputMethodPreference pref = getInputMethodPreference(imi, N); mInputMethodPreferenceList.add(pref); } @@ -260,9 +261,13 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment updateInputDevices(); - // IME + // Refresh internal states in mInputMethodSettingValues to keep the latest + // "InputMethodInfo"s and "InputMethodSubtype"s + mInputMethodSettingValues.refreshAllInputMethodAndSubtypes(); + // TODO: Consolidate the logic to InputMethodSettingsWrapper InputMethodAndSubtypeUtil.loadInputMethodSubtypeList( - this, getContentResolver(), mImis, null); + this, getContentResolver(), + mInputMethodSettingValues.getInputMethodList(), null); updateActiveInputMethodsSummary(); } @@ -276,8 +281,10 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment if (SHOW_INPUT_METHOD_SWITCHER_SETTINGS) { mShowInputMethodSelectorPref.setOnPreferenceChangeListener(null); } + // TODO: Consolidate the logic to InputMethodSettingsWrapper InputMethodAndSubtypeUtil.saveInputMethodSubtypeList( - this, getContentResolver(), mImis, !mHardKeyboardPreferenceList.isEmpty()); + this, getContentResolver(), mInputMethodSettingValues.getInputMethodList(), + !mHardKeyboardPreferenceList.isEmpty()); } @Override @@ -385,8 +392,8 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment if (context == null || mImm == null) return; final Preference curPref = getPreferenceScreen().findPreference(KEY_CURRENT_INPUT_METHOD); if (curPref != null) { - final CharSequence curIme = InputMethodAndSubtypeUtil.getCurrentInputMethodName( - context, getContentResolver(), mImm, mImis, getPackageManager()); + final CharSequence curIme = + mInputMethodSettingValues.getCurrentInputMethodName(context); if (!TextUtils.isEmpty(curIme)) { synchronized(this) { curPref.setSummary(curIme); diff --git a/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java b/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java index c7d8c8941a3..918b87e7748 100644 --- a/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java +++ b/src/com/android/settings/inputmethod/InputMethodAndSubtypeUtil.java @@ -154,28 +154,6 @@ public class InputMethodAndSubtypeUtil { return set; } - public static CharSequence getCurrentInputMethodName(Context context, ContentResolver resolver, - InputMethodManager imm, List imis, PackageManager pm) { - if (resolver == null || imis == null) return null; - final String currentInputMethodId = Settings.Secure.getString(resolver, - Settings.Secure.DEFAULT_INPUT_METHOD); - if (TextUtils.isEmpty(currentInputMethodId)) return null; - for (InputMethodInfo imi : imis) { - if (currentInputMethodId.equals(imi.getId())) { - final InputMethodSubtype subtype = imm.getCurrentInputMethodSubtype(); - final CharSequence imiLabel = imi.loadLabel(pm); - final CharSequence summary = subtype != null - ? TextUtils.concat(subtype.getDisplayName(context, - imi.getPackageName(), imi.getServiceInfo().applicationInfo), - (TextUtils.isEmpty(imiLabel) ? - "" : " - " + imiLabel)) - : imiLabel; - return summary; - } - } - return null; - } - public static void saveInputMethodSubtypeList(SettingsPreferenceFragment context, ContentResolver resolver, List inputMethodInfos, boolean hasHardKeyboard) { diff --git a/src/com/android/settings/inputmethod/InputMethodSettingValuesWrapper.java b/src/com/android/settings/inputmethod/InputMethodSettingValuesWrapper.java new file mode 100644 index 00000000000..9575f722d36 --- /dev/null +++ b/src/com/android/settings/inputmethod/InputMethodSettingValuesWrapper.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.inputmethod; + +import com.android.internal.inputmethod.InputMethodUtils; +import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings; + +import android.app.ActivityManagerNative; +import android.content.Context; +import android.os.RemoteException; +import android.util.Log; +import android.util.Slog; +import android.view.inputmethod.InputMethodInfo; +import android.view.inputmethod.InputMethodManager; +import android.view.inputmethod.InputMethodSubtype; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * This class is a wrapper for InputMethodSettings. You need to refresh internal states + * manually on some events when "InputMethodInfo"s and "InputMethodSubtype"s can be + * changed. + */ +public class InputMethodSettingValuesWrapper { + private static final String TAG = InputMethodSettingValuesWrapper.class.getSimpleName(); + private static volatile InputMethodSettingValuesWrapper sInstance; + private final ArrayList mMethodList = new ArrayList(); + private final HashMap mMethodMap = + new HashMap(); + private final InputMethodSettings mSettings; + private final InputMethodManager mImm; + + public static InputMethodSettingValuesWrapper getInstance(Context context) { + if (sInstance == null) { + synchronized(TAG) { + if (sInstance == null) { + sInstance = new InputMethodSettingValuesWrapper(context); + } + } + } + return sInstance; + } + + private static int getDefaultCurrentUserId() { + try { + return ActivityManagerNative.getDefault().getCurrentUser().id; + } catch (RemoteException e) { + Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e); + } + return 0; + } + + // Ensure singleton + private InputMethodSettingValuesWrapper(Context context) { + mSettings = + new InputMethodSettings(context.getResources(), context.getContentResolver(), + mMethodMap, mMethodList, getDefaultCurrentUserId()); + mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + refreshAllInputMethodAndSubtypes(); + } + + public void refreshAllInputMethodAndSubtypes() { + synchronized (mMethodMap) { + mMethodList.clear(); + mMethodMap.clear(); + final List imms = mImm.getInputMethodList(); + mMethodList.addAll(imms); + for (InputMethodInfo imi : imms) { + mMethodMap.put(imi.getId(), imi); + } + } + } + + public List getInputMethodList() { + synchronized (mMethodMap) { + return mMethodList; + } + } + + public CharSequence getCurrentInputMethodName(Context context) { + synchronized (mMethodMap) { + final InputMethodInfo imi = mMethodMap.get(mSettings.getSelectedInputMethod()); + if (imi == null) { + Log.w(TAG, "Invalid selected imi: " + mSettings.getSelectedInputMethod()); + return ""; + } + final InputMethodSubtype subtype = mImm.getCurrentInputMethodSubtype(); + return InputMethodUtils.getImeAndSubtypeDisplayName(context, imi, subtype); + } + } +}