Make InputMethodAndSubtypeUtil and share utilities by LanguageSettings and InputMethodAndSubtypeUtil

Change-Id: I70b9ce62a87a38054be0fce4234104aee9be9a87
This commit is contained in:
satok
2010-11-01 17:09:15 +09:00
parent 3c672b4254
commit 9cd11a9aa5
3 changed files with 156 additions and 193 deletions

View File

@@ -19,23 +19,18 @@ package com.android.settings;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
@@ -44,10 +39,6 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
private List<InputMethodInfo> mInputMethodProperties;
private final TextUtils.SimpleStringSplitter mStringColonSplitter
= new TextUtils.SimpleStringSplitter(':');
private String mLastInputMethodId;
private String mLastTickedInputMethodId;
private AlertDialog mDialog = null;
@@ -64,13 +55,15 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
@Override
public void onResume() {
super.onResume();
loadInputMethodSubtypeList();
InputMethodAndSubtypeUtil.loadInputMethodSubtypeList(this, mInputMethodProperties);
mLastTickedInputMethodId = null;
}
@Override
public void onPause() {
super.onPause();
saveInputMethodSubtypeList();
InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(this, mInputMethodProperties,
mHaveHardKeyboard, mLastTickedInputMethodId);
}
@Override
@@ -88,8 +81,9 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
InputMethodInfo imi = mInputMethodProperties.get(i);
if (id.equals(imi.getId())) {
selImi = imi;
if (isSystemIme(imi)) {
setSubtypesPreferenceEnabled(id, true);
if (InputMethodAndSubtypeUtil.isSystemIme(imi)) {
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
this, mInputMethodProperties, id, true);
// This is a built-in IME, so no need to warn.
mLastTickedInputMethodId = id;
return super.onPreferenceTreeClick(preferenceScreen, preference);
@@ -110,7 +104,9 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
chkPref.setChecked(true);
setSubtypesPreferenceEnabled(id, true);
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
InputMethodAndSubtypeEnabler.this,
mInputMethodProperties, id, true);
mLastTickedInputMethodId = id;
}
@@ -135,7 +131,8 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
if (id.equals(mLastTickedInputMethodId)) {
mLastTickedInputMethodId = null;
}
setSubtypesPreferenceEnabled(id, false);
InputMethodAndSubtypeUtil.setSubtypesPreferenceEnabled(
this, mInputMethodProperties, id, false);
}
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
@@ -156,9 +153,6 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
// TODO: Change mInputMethodProperties to Map
mInputMethodProperties = imm.getInputMethodList();
mLastInputMethodId = Settings.Secure.getString(getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD);
}
private PreferenceScreen createPreferenceHierarchy() {
@@ -175,7 +169,7 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
PackageManager pm = getPackageManager();
CharSequence label = property.loadLabel(pm);
boolean systemIME = isSystemIme(property);
boolean systemIME = InputMethodAndSubtypeUtil.isSystemIme(property);
keyboardSettingsCategory.setTitle(label);
@@ -220,100 +214,4 @@ public class InputMethodAndSubtypeEnabler extends SettingsPreferenceFragment {
}
return root;
}
private void loadInputMethodSubtypeList() {
final HashSet<String> enabled = new HashSet<String>();
String enabledStr = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
if (enabledStr != null) {
final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(enabledStr);
while (splitter.hasNext()) {
enabled.add(splitter.next());
}
}
// Update the statuses of the Check Boxes.
int N = mInputMethodProperties.size();
// TODO: Use iterator.
for (int i = 0; i < N; ++i) {
final String id = mInputMethodProperties.get(i).getId();
CheckBoxPreference pref = (CheckBoxPreference) findPreference(
mInputMethodProperties.get(i).getId());
if (pref != null) {
boolean isEnabled = enabled.contains(id);
pref.setChecked(isEnabled);
setSubtypesPreferenceEnabled(id, isEnabled);
}
}
mLastTickedInputMethodId = null;
}
private void saveInputMethodSubtypeList() {
StringBuilder builder = new StringBuilder();
StringBuilder disabledSysImes = new StringBuilder();
int firstEnabled = -1;
int N = mInputMethodProperties.size();
for (int i = 0; i < N; ++i) {
final InputMethodInfo property = mInputMethodProperties.get(i);
final String id = property.getId();
CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
boolean currentInputMethod = id.equals(mLastInputMethodId);
boolean systemIme = isSystemIme(property);
// TODO: Append subtypes by using the separator ";"
if (((N == 1 || systemIme) && !mHaveHardKeyboard)
|| (pref != null && pref.isChecked())) {
if (builder.length() > 0) builder.append(':');
builder.append(id);
if (firstEnabled < 0) {
firstEnabled = i;
}
} else if (currentInputMethod) {
mLastInputMethodId = mLastTickedInputMethodId;
}
// If it's a disabled system ime, add it to the disabled list so that it
// doesn't get enabled automatically on any changes to the package list
if (pref != null && !pref.isChecked() && systemIme && mHaveHardKeyboard) {
if (disabledSysImes.length() > 0) disabledSysImes.append(":");
disabledSysImes.append(id);
}
}
// If the last input method is unset, set it as the first enabled one.
if (TextUtils.isEmpty(mLastInputMethodId)) {
if (firstEnabled >= 0) {
mLastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
} else {
mLastInputMethodId = null;
}
}
Settings.Secure.putString(getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
Settings.Secure.putString(getContentResolver(),
Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, disabledSysImes.toString());
Settings.Secure.putString(getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,
mLastInputMethodId != null ? mLastInputMethodId : "");
}
private void setSubtypesPreferenceEnabled(String id, boolean enabled) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
final int N = mInputMethodProperties.size();
// TODO: Use iterator.
for (int i = 0; i < N; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
if (id.equals(imi.getId())) {
for (InputMethodSubtype subtype: imi.getSubtypes()) {
preferenceScreen.findPreference(id + subtype.hashCode()).setEnabled(enabled);
}
}
}
}
private boolean isSystemIme(InputMethodInfo property) {
return (property.getServiceInfo().applicationInfo.flags
& ApplicationInfo.FLAG_SYSTEM) != 0;
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2010 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;
import android.content.pm.ApplicationInfo;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import java.util.HashSet;
import java.util.List;
public class InputMethodAndSubtypeUtil {
private static final TextUtils.SimpleStringSplitter sStringColonSplitter
= new TextUtils.SimpleStringSplitter(':');
public static void saveInputMethodSubtypeList(
SettingsPreferenceFragment context, List<InputMethodInfo> inputMethodProperties,
boolean hasHardKeyboard, String lastTickedInputMethodId) {
String lastInputMethodId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD);
StringBuilder builder = new StringBuilder();
StringBuilder disabledSysImes = new StringBuilder();
int firstEnabled = -1;
int N = inputMethodProperties.size();
for (int i = 0; i < N; ++i) {
final InputMethodInfo property = inputMethodProperties.get(i);
final String id = property.getId();
CheckBoxPreference pref = (CheckBoxPreference) context.findPreference(id);
boolean currentInputMethod = id.equals(lastInputMethodId);
boolean systemIme = isSystemIme(property);
// TODO: Append subtypes by using the separator ";"
if (((N == 1 || systemIme) && !hasHardKeyboard)
|| (pref != null && pref.isChecked())) {
if (builder.length() > 0) builder.append(':');
builder.append(id);
if (firstEnabled < 0) {
firstEnabled = i;
}
} else if (currentInputMethod) {
lastInputMethodId = lastTickedInputMethodId;
}
// If it's a disabled system ime, add it to the disabled list so that it
// doesn't get enabled automatically on any changes to the package list
if (pref != null && !pref.isChecked() && systemIme && hasHardKeyboard) {
if (disabledSysImes.length() > 0) disabledSysImes.append(":");
disabledSysImes.append(id);
}
}
// If the last input method is unset, set it as the first enabled one.
if (TextUtils.isEmpty(lastInputMethodId)) {
if (firstEnabled >= 0) {
lastInputMethodId = inputMethodProperties.get(firstEnabled).getId();
} else {
lastInputMethodId = null;
}
}
Settings.Secure.putString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
Settings.Secure.putString(context.getContentResolver(),
Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, disabledSysImes.toString());
Settings.Secure.putString(context.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,
lastInputMethodId != null ? lastInputMethodId : "");
}
public static void loadInputMethodSubtypeList(SettingsPreferenceFragment context,
List<InputMethodInfo> inputMethodProperties) {
final HashSet<String> enabled = new HashSet<String>();
String enabledStr = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
if (enabledStr != null) {
final TextUtils.SimpleStringSplitter splitter = sStringColonSplitter;
splitter.setString(enabledStr);
while (splitter.hasNext()) {
enabled.add(splitter.next());
}
}
// Update the statuses of the Check Boxes.
int N = inputMethodProperties.size();
// TODO: Use iterator.
for (int i = 0; i < N; ++i) {
final String id = inputMethodProperties.get(i).getId();
CheckBoxPreference pref = (CheckBoxPreference) context.findPreference(
inputMethodProperties.get(i).getId());
if (pref != null) {
boolean isEnabled = enabled.contains(id);
pref.setChecked(isEnabled);
setSubtypesPreferenceEnabled(context, inputMethodProperties, id, isEnabled);
}
}
}
public static void setSubtypesPreferenceEnabled(SettingsPreferenceFragment context,
List<InputMethodInfo> inputMethodProperties, String id, boolean enabled) {
PreferenceScreen preferenceScreen = context.getPreferenceScreen();
final int N = inputMethodProperties.size();
// TODO: Use iterator.
for (int i = 0; i < N; i++) {
InputMethodInfo imi = inputMethodProperties.get(i);
if (id.equals(imi.getId())) {
for (InputMethodSubtype subtype: imi.getSubtypes()) {
preferenceScreen.findPreference(id + subtype.hashCode()).setEnabled(enabled);
}
}
}
}
public static boolean isSystemIme(InputMethodInfo property) {
return (property.getServiceInfo().applicationInfo.flags
& ApplicationInfo.FLAG_SYSTEM) != 0;
}
}

View File

@@ -16,7 +16,6 @@
package com.android.settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
@@ -28,14 +27,11 @@ import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class LanguageSettings extends SettingsPreferenceFragment {
@@ -92,7 +88,8 @@ public class LanguageSettings extends SettingsPreferenceFragment {
}
private void onCreateIMM() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
mInputMethodProperties = imm.getInputMethodList();
@@ -143,27 +140,7 @@ public class LanguageSettings extends SettingsPreferenceFragment {
public void onResume() {
super.onResume();
final HashSet<String> enabled = new HashSet<String>();
String enabledStr = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
if (enabledStr != null) {
final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(enabledStr);
while (splitter.hasNext()) {
enabled.add(splitter.next());
}
}
// Update the statuses of the Check Boxes.
int N = mInputMethodProperties.size();
for (int i = 0; i < N; ++i) {
final String id = mInputMethodProperties.get(i).getId();
CheckBoxPreference pref = (CheckBoxPreference) findPreference(mInputMethodProperties
.get(i).getId());
if (pref != null) {
pref.setChecked(enabled.contains(id));
}
}
InputMethodAndSubtypeUtil.loadInputMethodSubtypeList(this, mInputMethodProperties);
mLastTickedInputMethodId = null;
if (mLanguagePref != null) {
@@ -179,55 +156,8 @@ public class LanguageSettings extends SettingsPreferenceFragment {
@Override
public void onPause() {
super.onPause();
String lastInputMethodId = Settings.Secure.getString(getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD);
StringBuilder builder = new StringBuilder();
StringBuilder disabledSysImes = new StringBuilder();
int firstEnabled = -1;
int N = mInputMethodProperties.size();
for (int i = 0; i < N; ++i) {
final InputMethodInfo property = mInputMethodProperties.get(i);
final String id = property.getId();
CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
boolean hasIt = id.equals(lastInputMethodId);
boolean systemIme = isSystemIme(property);
if (((N == 1 || systemIme) && !mHaveHardKeyboard)
|| (pref != null && pref.isChecked())) {
if (builder.length() > 0) builder.append(':');
builder.append(id);
if (firstEnabled < 0) {
firstEnabled = i;
}
} else if (hasIt) {
lastInputMethodId = mLastTickedInputMethodId;
}
// If it's a disabled system ime, add it to the disabled list so that it
// doesn't get enabled automatically on any changes to the package list
if (pref != null && !pref.isChecked() && systemIme && mHaveHardKeyboard) {
if (disabledSysImes.length() > 0) disabledSysImes.append(":");
disabledSysImes.append(id);
}
}
// If the last input method is unset, set it as the first enabled one.
if (null == lastInputMethodId || "".equals(lastInputMethodId)) {
if (firstEnabled >= 0) {
lastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
} else {
lastInputMethodId = null;
}
}
Settings.Secure.putString(getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
Settings.Secure.putString(getContentResolver(),
Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, disabledSysImes.toString());
Settings.Secure.putString(getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,
lastInputMethodId != null ? lastInputMethodId : "");
InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(this, mInputMethodProperties,
mHaveHardKeyboard, mLastTickedInputMethodId);
}
@Override