Use string-array resource to define predefined-subtypes

Bug: 6809603
Change-Id: Icafe4652ec17844838b28c04977deb88bcb9aa8f
main
Tadashi G. Takaoka 2012-09-04 16:54:01 +09:00
parent 147a62a8a8
commit 848331222e
4 changed files with 126 additions and 102 deletions

View File

@ -18,6 +18,9 @@
*/ */
--> -->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Predefined subtypes (language:layout[:extraValue]) in semicolon separated format --> <!-- Predefined subtypes (language:layout[:extraValue]) -->
<string name="predefined_subtypes" translatable="false">de:qwerty:AsciiCapable;fr:qwertz:AsciiCapable</string> <string-array name="predefined_subtypes" translatable="false">
<item>de:qwerty:AsciiCapable</item>
<item>fr:qwertz:AsciiCapable</item>
</string-array>
</resources> </resources>

View File

@ -27,22 +27,22 @@ import android.view.inputmethod.InputMethodSubtype;
import java.util.ArrayList; import java.util.ArrayList;
public class AdditionalSubtype { public final class AdditionalSubtype {
private static final InputMethodSubtype[] EMPTY_SUBTYPE_ARRAY = new InputMethodSubtype[0]; private static final InputMethodSubtype[] EMPTY_SUBTYPE_ARRAY = new InputMethodSubtype[0];
private AdditionalSubtype() { private AdditionalSubtype() {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
} }
public static boolean isAdditionalSubtype(InputMethodSubtype subtype) { public static boolean isAdditionalSubtype(final InputMethodSubtype subtype) {
return subtype.containsExtraValueKey(IS_ADDITIONAL_SUBTYPE); return subtype.containsExtraValueKey(IS_ADDITIONAL_SUBTYPE);
} }
private static final String LOCALE_AND_LAYOUT_SEPARATOR = ":"; private static final String LOCALE_AND_LAYOUT_SEPARATOR = ":";
public static final String PREF_SUBTYPE_SEPARATOR = ";"; private static final String PREF_SUBTYPE_SEPARATOR = ";";
public static InputMethodSubtype createAdditionalSubtype( public static InputMethodSubtype createAdditionalSubtype(final String localeString,
String localeString, String keyboardLayoutSetName, String extraValue) { final String keyboardLayoutSetName, final String extraValue) {
final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName; final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName;
final String layoutDisplayNameExtraValue; final String layoutDisplayNameExtraValue;
if (Build.VERSION.SDK_INT >= /* JELLY_BEAN */ 15 if (Build.VERSION.SDK_INT >= /* JELLY_BEAN */ 15
@ -62,7 +62,7 @@ public class AdditionalSubtype {
layoutExtraValue + "," + additionalSubtypeExtraValue, false, false); layoutExtraValue + "," + additionalSubtypeExtraValue, false, false);
} }
public static String getPrefSubtype(InputMethodSubtype subtype) { public static String getPrefSubtype(final InputMethodSubtype subtype) {
final String localeString = subtype.getLocale(); final String localeString = subtype.getLocale();
final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype); final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName; final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName;
@ -74,7 +74,7 @@ public class AdditionalSubtype {
: basePrefSubtype + LOCALE_AND_LAYOUT_SEPARATOR + extraValue; : basePrefSubtype + LOCALE_AND_LAYOUT_SEPARATOR + extraValue;
} }
public static InputMethodSubtype createAdditionalSubtype(String prefSubtype) { public static InputMethodSubtype createAdditionalSubtype(final String prefSubtype) {
final String elems[] = prefSubtype.split(LOCALE_AND_LAYOUT_SEPARATOR); final String elems[] = prefSubtype.split(LOCALE_AND_LAYOUT_SEPARATOR);
if (elems.length < 2 || elems.length > 3) { if (elems.length < 2 || elems.length > 3) {
throw new RuntimeException("Unknown additional subtype specified: " + prefSubtype); throw new RuntimeException("Unknown additional subtype specified: " + prefSubtype);
@ -85,7 +85,7 @@ public class AdditionalSubtype {
return createAdditionalSubtype(localeString, keyboardLayoutSetName, extraValue); return createAdditionalSubtype(localeString, keyboardLayoutSetName, extraValue);
} }
public static InputMethodSubtype[] createAdditionalSubtypesArray(String prefSubtypes) { public static InputMethodSubtype[] createAdditionalSubtypesArray(final String prefSubtypes) {
if (TextUtils.isEmpty(prefSubtypes)) { if (TextUtils.isEmpty(prefSubtypes)) {
return EMPTY_SUBTYPE_ARRAY; return EMPTY_SUBTYPE_ARRAY;
} }
@ -103,4 +103,32 @@ public class AdditionalSubtype {
} }
return subtypesList.toArray(new InputMethodSubtype[subtypesList.size()]); return subtypesList.toArray(new InputMethodSubtype[subtypesList.size()]);
} }
public static String createPrefSubtypes(final InputMethodSubtype[] subtypes) {
if (subtypes == null || subtypes.length == 0) {
return "";
}
final StringBuilder sb = new StringBuilder();
for (final InputMethodSubtype subtype : subtypes) {
if (sb.length() > 0) {
sb.append(PREF_SUBTYPE_SEPARATOR);
}
sb.append(getPrefSubtype(subtype));
}
return sb.toString();
}
public static String createPrefSubtypes(final String[] prefSubtypes) {
if (prefSubtypes == null || prefSubtypes.length == 0) {
return "";
}
final StringBuilder sb = new StringBuilder();
for (final String prefSubtype : prefSubtypes) {
if (sb.length() > 0) {
sb.append(PREF_SUBTYPE_SEPARATOR);
}
sb.append(prefSubtype);
}
return sb.toString();
}
} }

View File

@ -63,13 +63,13 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
private static final String KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN = private static final String KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN =
"is_subtype_enabler_notification_dialog_open"; "is_subtype_enabler_notification_dialog_open";
private static final String KEY_SUBTYPE_FOR_SUBTYPE_ENABLER = "subtype_for_subtype_enabler"; private static final String KEY_SUBTYPE_FOR_SUBTYPE_ENABLER = "subtype_for_subtype_enabler";
static class SubtypeLocaleItem extends Pair<String, String> static final class SubtypeLocaleItem extends Pair<String, String>
implements Comparable<SubtypeLocaleItem> { implements Comparable<SubtypeLocaleItem> {
public SubtypeLocaleItem(String localeString, String displayName) { public SubtypeLocaleItem(final String localeString, final String displayName) {
super(localeString, displayName); super(localeString, displayName);
} }
public SubtypeLocaleItem(String localeString) { public SubtypeLocaleItem(final String localeString) {
this(localeString, SubtypeLocale.getSubtypeLocaleDisplayName(localeString)); this(localeString, SubtypeLocale.getSubtypeLocaleDisplayName(localeString));
} }
@ -79,13 +79,13 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public int compareTo(SubtypeLocaleItem o) { public int compareTo(final SubtypeLocaleItem o) {
return first.compareTo(o.first); return first.compareTo(o.first);
} }
} }
static class SubtypeLocaleAdapter extends ArrayAdapter<SubtypeLocaleItem> { static final class SubtypeLocaleAdapter extends ArrayAdapter<SubtypeLocaleItem> {
public SubtypeLocaleAdapter(Context context) { public SubtypeLocaleAdapter(final Context context) {
super(context, android.R.layout.simple_spinner_item); super(context, android.R.layout.simple_spinner_item);
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@ -102,7 +102,8 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
addAll(items); addAll(items);
} }
public static SubtypeLocaleItem createItem(Context context, String localeString) { public static SubtypeLocaleItem createItem(final Context context,
final String localeString) {
if (localeString.equals(SubtypeLocale.NO_LANGUAGE)) { if (localeString.equals(SubtypeLocale.NO_LANGUAGE)) {
final String displayName = context.getString(R.string.subtype_no_language); final String displayName = context.getString(R.string.subtype_no_language);
return new SubtypeLocaleItem(localeString, displayName); return new SubtypeLocaleItem(localeString, displayName);
@ -112,8 +113,8 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
static class KeyboardLayoutSetItem extends Pair<String, String> { static final class KeyboardLayoutSetItem extends Pair<String, String> {
public KeyboardLayoutSetItem(InputMethodSubtype subtype) { public KeyboardLayoutSetItem(final InputMethodSubtype subtype) {
super(SubtypeLocale.getKeyboardLayoutSetName(subtype), super(SubtypeLocale.getKeyboardLayoutSetName(subtype),
SubtypeLocale.getKeyboardLayoutSetDisplayName(subtype)); SubtypeLocale.getKeyboardLayoutSetDisplayName(subtype));
} }
@ -124,8 +125,8 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
static class KeyboardLayoutSetAdapter extends ArrayAdapter<KeyboardLayoutSetItem> { static final class KeyboardLayoutSetAdapter extends ArrayAdapter<KeyboardLayoutSetItem> {
public KeyboardLayoutSetAdapter(Context context) { public KeyboardLayoutSetAdapter(final Context context) {
super(context, android.R.layout.simple_spinner_item); super(context, android.R.layout.simple_spinner_item);
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@ -147,7 +148,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
public KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter(); public KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter();
} }
static class SubtypePreference extends DialogPreference static final class SubtypePreference extends DialogPreference
implements DialogInterface.OnCancelListener { implements DialogInterface.OnCancelListener {
private static final String KEY_PREFIX = "subtype_pref_"; private static final String KEY_PREFIX = "subtype_pref_";
private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new"; private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new";
@ -159,13 +160,13 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
private Spinner mSubtypeLocaleSpinner; private Spinner mSubtypeLocaleSpinner;
private Spinner mKeyboardLayoutSetSpinner; private Spinner mKeyboardLayoutSetSpinner;
public static SubtypePreference newIncompleteSubtypePreference( public static SubtypePreference newIncompleteSubtypePreference(final Context context,
Context context, SubtypeDialogProxy proxy) { final SubtypeDialogProxy proxy) {
return new SubtypePreference(context, null, proxy); return new SubtypePreference(context, null, proxy);
} }
public SubtypePreference(Context context, InputMethodSubtype subtype, public SubtypePreference(final Context context, final InputMethodSubtype subtype,
SubtypeDialogProxy proxy) { final SubtypeDialogProxy proxy) {
super(context, null); super(context, null);
setDialogLayoutResource(R.layout.additional_subtype_dialog); setDialogLayoutResource(R.layout.additional_subtype_dialog);
setPersistent(false); setPersistent(false);
@ -185,7 +186,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
return mSubtype; return mSubtype;
} }
public void setSubtype(InputMethodSubtype subtype) { public void setSubtype(final InputMethodSubtype subtype) {
mPreviousSubtype = mSubtype; mPreviousSubtype = mSubtype;
mSubtype = subtype; mSubtype = subtype;
if (isIncomplete()) { if (isIncomplete()) {
@ -221,7 +222,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { protected void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
final Context context = builder.getContext(); final Context context = builder.getContext();
builder.setCancelable(true).setOnCancelListener(this); builder.setCancelable(true).setOnCancelListener(this);
if (isIncomplete()) { if (isIncomplete()) {
@ -239,7 +240,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
private static void setSpinnerPosition(Spinner spinner, Object itemToSelect) { private static void setSpinnerPosition(final Spinner spinner, final Object itemToSelect) {
final SpinnerAdapter adapter = spinner.getAdapter(); final SpinnerAdapter adapter = spinner.getAdapter();
final int count = adapter.getCount(); final int count = adapter.getCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@ -252,14 +253,14 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onCancel(DialogInterface dialog) { public void onCancel(final DialogInterface dialog) {
if (isIncomplete()) { if (isIncomplete()) {
mProxy.onRemovePressed(this); mProxy.onRemovePressed(this);
} }
} }
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(final DialogInterface dialog, final int which) {
super.onClick(dialog, which); super.onClick(dialog, which);
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
@ -287,12 +288,12 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
private static int getSpinnerPosition(Spinner spinner) { private static int getSpinnerPosition(final Spinner spinner) {
if (spinner == null) return -1; if (spinner == null) return -1;
return spinner.getSelectedItemPosition(); return spinner.getSelectedItemPosition();
} }
private static void setSpinnerPosition(Spinner spinner, int position) { private static void setSpinnerPosition(final Spinner spinner, final int position) {
if (spinner == null || position < 0) return; if (spinner == null || position < 0) return;
spinner.setSelection(position); spinner.setSelection(position);
} }
@ -313,7 +314,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
protected void onRestoreInstanceState(Parcelable state) { protected void onRestoreInstanceState(final Parcelable state) {
if (!(state instanceof SavedState)) { if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state); super.onRestoreInstanceState(state);
return; return;
@ -326,24 +327,24 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
setSubtype(myState.mSubtype); setSubtype(myState.mSubtype);
} }
static class SavedState extends Preference.BaseSavedState { static final class SavedState extends Preference.BaseSavedState {
InputMethodSubtype mSubtype; InputMethodSubtype mSubtype;
int mSubtypeLocaleSelectedPos; int mSubtypeLocaleSelectedPos;
int mKeyboardLayoutSetSelectedPos; int mKeyboardLayoutSetSelectedPos;
public SavedState(Parcelable superState) { public SavedState(final Parcelable superState) {
super(superState); super(superState);
} }
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(final Parcel dest, final int flags) {
super.writeToParcel(dest, flags); super.writeToParcel(dest, flags);
dest.writeInt(mSubtypeLocaleSelectedPos); dest.writeInt(mSubtypeLocaleSelectedPos);
dest.writeInt(mKeyboardLayoutSetSelectedPos); dest.writeInt(mKeyboardLayoutSetSelectedPos);
dest.writeParcelable(mSubtype, 0); dest.writeParcelable(mSubtype, 0);
} }
public SavedState(Parcel source) { public SavedState(final Parcel source) {
super(source); super(source);
mSubtypeLocaleSelectedPos = source.readInt(); mSubtypeLocaleSelectedPos = source.readInt();
mKeyboardLayoutSetSelectedPos = source.readInt(); mKeyboardLayoutSetSelectedPos = source.readInt();
@ -354,12 +355,12 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
public static final Parcelable.Creator<SavedState> CREATOR = public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() { new Parcelable.Creator<SavedState>() {
@Override @Override
public SavedState createFromParcel(Parcel source) { public SavedState createFromParcel(final Parcel source) {
return new SavedState(source); return new SavedState(source);
} }
@Override @Override
public SavedState[] newArray(int size) { public SavedState[] newArray(final int size) {
return new SavedState[size]; return new SavedState[size];
} }
}; };
@ -371,7 +372,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.additional_subtype_settings); addPreferencesFromResource(R.xml.additional_subtype_settings);
@ -381,7 +382,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(final Bundle savedInstanceState) {
final Context context = getActivity(); final Context context = getActivity();
mSubtypeLocaleAdapter = new SubtypeLocaleAdapter(context); mSubtypeLocaleAdapter = new SubtypeLocaleAdapter(context);
mKeyboardLayoutSetAdapter = new KeyboardLayoutSetAdapter(context); mKeyboardLayoutSetAdapter = new KeyboardLayoutSetAdapter(context);
@ -411,7 +412,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if (mIsAddingNewSubtype) { if (mIsAddingNewSubtype) {
outState.putBoolean(KEY_IS_ADDING_NEW_SUBTYPE, true); outState.putBoolean(KEY_IS_ADDING_NEW_SUBTYPE, true);
@ -426,7 +427,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
private final SubtypeDialogProxy mSubtypeProxy = new SubtypeDialogProxy() { private final SubtypeDialogProxy mSubtypeProxy = new SubtypeDialogProxy() {
@Override @Override
public void onRemovePressed(SubtypePreference subtypePref) { public void onRemovePressed(final SubtypePreference subtypePref) {
mIsAddingNewSubtype = false; mIsAddingNewSubtype = false;
final PreferenceGroup group = getPreferenceScreen(); final PreferenceGroup group = getPreferenceScreen();
group.removePreference(subtypePref); group.removePreference(subtypePref);
@ -434,7 +435,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onSavePressed(SubtypePreference subtypePref) { public void onSavePressed(final SubtypePreference subtypePref) {
final InputMethodSubtype subtype = subtypePref.getSubtype(); final InputMethodSubtype subtype = subtypePref.getSubtype();
if (!subtypePref.hasBeenModified()) { if (!subtypePref.hasBeenModified()) {
return; return;
@ -453,7 +454,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onAddPressed(SubtypePreference subtypePref) { public void onAddPressed(final SubtypePreference subtypePref) {
mIsAddingNewSubtype = false; mIsAddingNewSubtype = false;
final InputMethodSubtype subtype = subtypePref.getSubtype(); final InputMethodSubtype subtype = subtypePref.getSubtype();
if (findDuplicatedSubtype(subtype) == null) { if (findDuplicatedSubtype(subtype) == null) {
@ -481,7 +482,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
}; };
private void showSubtypeAlreadyExistsToast(InputMethodSubtype subtype) { private void showSubtypeAlreadyExistsToast(final InputMethodSubtype subtype) {
final Context context = getActivity(); final Context context = getActivity();
final Resources res = context.getResources(); final Resources res = context.getResources();
final String message = res.getString(R.string.custom_input_style_already_exists, final String message = res.getString(R.string.custom_input_style_already_exists,
@ -489,14 +490,15 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} }
private InputMethodSubtype findDuplicatedSubtype(InputMethodSubtype subtype) { private InputMethodSubtype findDuplicatedSubtype(final InputMethodSubtype subtype) {
final String localeString = subtype.getLocale(); final String localeString = subtype.getLocale();
final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype); final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
return ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( return ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
getActivity(), localeString, keyboardLayoutSetName); getActivity(), localeString, keyboardLayoutSetName);
} }
private AlertDialog createDialog(SubtypePreference subtypePref) { private AlertDialog createDialog(
@SuppressWarnings("unused") final SubtypePreference subtypePref) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.custom_input_styles_title) builder.setTitle(R.string.custom_input_styles_title)
.setMessage(R.string.custom_input_style_note_message) .setMessage(R.string.custom_input_style_note_message)
@ -519,7 +521,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
return builder.create(); return builder.create();
} }
private void setPrefSubtypes(String prefSubtypes, Context context) { private void setPrefSubtypes(final String prefSubtypes, final Context context) {
final PreferenceGroup group = getPreferenceScreen(); final PreferenceGroup group = getPreferenceScreen();
group.removeAll(); group.removeAll();
final InputMethodSubtype[] subtypesArray = final InputMethodSubtype[] subtypesArray =
@ -547,23 +549,12 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
return subtypes.toArray(new InputMethodSubtype[subtypes.size()]); return subtypes.toArray(new InputMethodSubtype[subtypes.size()]);
} }
private String getPrefSubtypes(InputMethodSubtype[] subtypes) {
final StringBuilder sb = new StringBuilder();
for (final InputMethodSubtype subtype : subtypes) {
if (sb.length() > 0) {
sb.append(AdditionalSubtype.PREF_SUBTYPE_SEPARATOR);
}
sb.append(AdditionalSubtype.getPrefSubtype(subtype));
}
return sb.toString();
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
final String oldSubtypes = SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources()); final String oldSubtypes = SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources());
final InputMethodSubtype[] subtypes = getSubtypes(); final InputMethodSubtype[] subtypes = getSubtypes();
final String prefSubtypes = getPrefSubtypes(subtypes); final String prefSubtypes = AdditionalSubtype.createPrefSubtypes(subtypes);
if (prefSubtypes.equals(oldSubtypes)) { if (prefSubtypes.equals(oldSubtypes)) {
return; return;
} }
@ -578,13 +569,13 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
final MenuItem addSubtypeMenu = menu.add(0, MENU_ADD_SUBTYPE, 0, R.string.add_style); final MenuItem addSubtypeMenu = menu.add(0, MENU_ADD_SUBTYPE, 0, R.string.add_style);
addSubtypeMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); addSubtypeMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
final int itemId = item.getItemId(); final int itemId = item.getItemId();
if (itemId == MENU_ADD_SUBTYPE) { if (itemId == MENU_ADD_SUBTYPE) {
final SubtypePreference newSubtype = final SubtypePreference newSubtype =

View File

@ -35,7 +35,7 @@ import java.util.HashMap;
* When you call the constructor of this class, you may want to change the current system locale by * When you call the constructor of this class, you may want to change the current system locale by
* using {@link LocaleUtils.RunInLocale}. * using {@link LocaleUtils.RunInLocale}.
*/ */
public class SettingsValues { public final class SettingsValues {
private static final String TAG = SettingsValues.class.getSimpleName(); private static final String TAG = SettingsValues.class.getSimpleName();
private static final int SUGGESTION_VISIBILITY_SHOW_VALUE private static final int SUGGESTION_VISIBILITY_SHOW_VALUE
@ -246,64 +246,65 @@ public class SettingsValues {
&& orientation == Configuration.ORIENTATION_PORTRAIT); && orientation == Configuration.ORIENTATION_PORTRAIT);
} }
public boolean isWordSeparator(int code) { public boolean isWordSeparator(final int code) {
return mWordSeparators.contains(String.valueOf((char)code)); return mWordSeparators.contains(String.valueOf((char)code));
} }
public boolean isSymbolExcludedFromWordSeparators(int code) { public boolean isSymbolExcludedFromWordSeparators(final int code) {
return mSymbolsExcludedFromWordSeparators.contains(String.valueOf((char)code)); return mSymbolsExcludedFromWordSeparators.contains(String.valueOf((char)code));
} }
public boolean isWeakSpaceStripper(int code) { public boolean isWeakSpaceStripper(final int code) {
// TODO: this does not work if the code does not fit in a char // TODO: this does not work if the code does not fit in a char
return mWeakSpaceStrippers.contains(String.valueOf((char)code)); return mWeakSpaceStrippers.contains(String.valueOf((char)code));
} }
public boolean isWeakSpaceSwapper(int code) { public boolean isWeakSpaceSwapper(final int code) {
// TODO: this does not work if the code does not fit in a char // TODO: this does not work if the code does not fit in a char
return mWeakSpaceSwappers.contains(String.valueOf((char)code)); return mWeakSpaceSwappers.contains(String.valueOf((char)code));
} }
public boolean isPhantomSpacePromotingSymbol(int code) { public boolean isPhantomSpacePromotingSymbol(final int code) {
// TODO: this does not work if the code does not fit in a char // TODO: this does not work if the code does not fit in a char
return mPhantomSpacePromotingSymbols.contains(String.valueOf((char)code)); return mPhantomSpacePromotingSymbols.contains(String.valueOf((char)code));
} }
private static boolean isAutoCorrectEnabled(final Resources resources, private static boolean isAutoCorrectEnabled(final Resources res,
final String currentAutoCorrectionSetting) { final String currentAutoCorrectionSetting) {
final String autoCorrectionOff = resources.getString( final String autoCorrectionOff = res.getString(
R.string.auto_correction_threshold_mode_index_off); R.string.auto_correction_threshold_mode_index_off);
return !currentAutoCorrectionSetting.equals(autoCorrectionOff); return !currentAutoCorrectionSetting.equals(autoCorrectionOff);
} }
// Public to access from KeyboardSwitcher. Should it have access to some // Public to access from KeyboardSwitcher. Should it have access to some
// process-global instance instead? // process-global instance instead?
public static boolean isKeyPreviewPopupEnabled(SharedPreferences sp, Resources resources) { public static boolean isKeyPreviewPopupEnabled(final SharedPreferences prefs,
final boolean showPopupOption = resources.getBoolean( final Resources res) {
final boolean showPopupOption = res.getBoolean(
R.bool.config_enable_show_popup_on_keypress_option); R.bool.config_enable_show_popup_on_keypress_option);
if (!showPopupOption) return resources.getBoolean(R.bool.config_default_popup_preview); if (!showPopupOption) return res.getBoolean(R.bool.config_default_popup_preview);
return sp.getBoolean(Settings.PREF_POPUP_ON, return prefs.getBoolean(Settings.PREF_POPUP_ON,
resources.getBoolean(R.bool.config_default_popup_preview)); res.getBoolean(R.bool.config_default_popup_preview));
} }
// Likewise // Likewise
public static int getKeyPreviewPopupDismissDelay(SharedPreferences sp, public static int getKeyPreviewPopupDismissDelay(final SharedPreferences prefs,
Resources resources) { final Resources res) {
// TODO: use mKeyPreviewPopupDismissDelayRawValue instead of reading it again here. // TODO: use mKeyPreviewPopupDismissDelayRawValue instead of reading it again here.
return Integer.parseInt(sp.getString(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, return Integer.parseInt(prefs.getString(Settings.PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY,
Integer.toString(resources.getInteger( Integer.toString(res.getInteger(
R.integer.config_key_preview_linger_timeout)))); R.integer.config_key_preview_linger_timeout))));
} }
private static boolean isBigramPredictionEnabled(final SharedPreferences sp, private static boolean isBigramPredictionEnabled(final SharedPreferences prefs,
final Resources resources) { final Resources res) {
return sp.getBoolean(Settings.PREF_BIGRAM_PREDICTIONS, resources.getBoolean( return prefs.getBoolean(Settings.PREF_BIGRAM_PREDICTIONS, res.getBoolean(
R.bool.config_default_next_word_prediction)); R.bool.config_default_next_word_prediction));
} }
private static float getAutoCorrectionThreshold(final Resources resources, private static float getAutoCorrectionThreshold(final Resources res,
final String currentAutoCorrectionSetting) { final String currentAutoCorrectionSetting) {
final String[] autoCorrectionThresholdValues = resources.getStringArray( final String[] autoCorrectionThresholdValues = res.getStringArray(
R.array.auto_correction_threshold_values); R.array.auto_correction_threshold_values);
// When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off. // When autoCorrectionThreshold is greater than 1.0, it's like auto correction is off.
float autoCorrectionThreshold = Float.MAX_VALUE; float autoCorrectionThreshold = Float.MAX_VALUE;
@ -335,11 +336,11 @@ public class SettingsValues {
return mVoiceKeyOnMain; return mVoiceKeyOnMain;
} }
public static boolean isLanguageSwitchKeySupressed(SharedPreferences sp) { public static boolean isLanguageSwitchKeySupressed(final SharedPreferences prefs) {
return sp.getBoolean(Settings.PREF_SUPPRESS_LANGUAGE_SWITCH_KEY, false); return prefs.getBoolean(Settings.PREF_SUPPRESS_LANGUAGE_SWITCH_KEY, false);
} }
public boolean isLanguageSwitchKeyEnabled(Context context) { public boolean isLanguageSwitchKeyEnabled(final Context context) {
if (mIsLanguageSwitchKeySuppressed) { if (mIsLanguageSwitchKeySuppressed) {
return false; return false;
} }
@ -352,7 +353,7 @@ public class SettingsValues {
} }
} }
public boolean isFullscreenModeAllowed(Resources res) { public boolean isFullscreenModeAllowed(final Resources res) {
return res.getBoolean(R.bool.config_use_fullscreen_mode); return res.getBoolean(R.bool.config_use_fullscreen_mode);
} }
@ -362,15 +363,16 @@ public class SettingsValues {
public static String getPrefAdditionalSubtypes(final SharedPreferences prefs, public static String getPrefAdditionalSubtypes(final SharedPreferences prefs,
final Resources res) { final Resources res) {
final String prefSubtypes = res.getString(R.string.predefined_subtypes, ""); final String predefinedPrefSubtypes = AdditionalSubtype.createPrefSubtypes(
return prefs.getString(Settings.PREF_CUSTOM_INPUT_STYLES, prefSubtypes); res.getStringArray(R.array.predefined_subtypes));
return prefs.getString(Settings.PREF_CUSTOM_INPUT_STYLES, predefinedPrefSubtypes);
} }
// Accessed from the settings interface, hence public // Accessed from the settings interface, hence public
public static float getCurrentKeypressSoundVolume(final SharedPreferences sp, public static float getCurrentKeypressSoundVolume(final SharedPreferences prefs,
final Resources res) { final Resources res) {
// TODO: use mVibrationDurationSettingsRawValue instead of reading it again here // TODO: use mVibrationDurationSettingsRawValue instead of reading it again here
final float volume = sp.getFloat(Settings.PREF_KEYPRESS_SOUND_VOLUME, -1.0f); final float volume = prefs.getFloat(Settings.PREF_KEYPRESS_SOUND_VOLUME, -1.0f);
if (volume >= 0) { if (volume >= 0) {
return volume; return volume;
} }
@ -380,10 +382,10 @@ public class SettingsValues {
} }
// Likewise // Likewise
public static int getCurrentVibrationDuration(final SharedPreferences sp, public static int getCurrentVibrationDuration(final SharedPreferences prefs,
final Resources res) { final Resources res) {
// TODO: use mKeypressVibrationDuration instead of reading it again here // TODO: use mKeypressVibrationDuration instead of reading it again here
final int ms = sp.getInt(Settings.PREF_VIBRATION_DURATION_SETTINGS, -1); final int ms = prefs.getInt(Settings.PREF_VIBRATION_DURATION_SETTINGS, -1);
if (ms >= 0) { if (ms >= 0) {
return ms; return ms;
} }
@ -398,8 +400,8 @@ public class SettingsValues {
return prefs.getBoolean(Settings.PREF_USABILITY_STUDY_MODE, true); return prefs.getBoolean(Settings.PREF_USABILITY_STUDY_MODE, true);
} }
public static long getLastUserHistoryWriteTime( public static long getLastUserHistoryWriteTime(final SharedPreferences prefs,
final SharedPreferences prefs, final String locale) { final String locale) {
final String str = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, ""); final String str = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(str); final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(str);
if (map.containsKey(locale)) { if (map.containsKey(locale)) {
@ -408,8 +410,8 @@ public class SettingsValues {
return 0; return 0;
} }
public static void setLastUserHistoryWriteTime( public static void setLastUserHistoryWriteTime(final SharedPreferences prefs,
final SharedPreferences prefs, final String locale) { final String locale) {
final String oldStr = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, ""); final String oldStr = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(oldStr); final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(oldStr);
map.put(locale, System.currentTimeMillis()); map.put(locale, System.currentTimeMillis());