am da3ba3e2: am 8ec3a42d: Merge "Check duplicated entry of "custom input style"" into jb-dev

* commit 'da3ba3e2549cc69c360fede58b7e170d92e67b0a':
  Check duplicated entry of "custom input style"
main
Tadashi G. Takaoka 2012-05-21 11:52:36 -07:00 committed by Android Git Automerger
commit 9d0c15d9b4
4 changed files with 88 additions and 25 deletions

View File

@ -293,6 +293,8 @@
<string name="enable">Enable</string> <string name="enable">Enable</string>
<!-- Title of the button to postpone enabling a custom input style entry in the settings dialog [CHAR LIMIT=12] --> <!-- Title of the button to postpone enabling a custom input style entry in the settings dialog [CHAR LIMIT=12] -->
<string name="not_now">Not now</string> <string name="not_now">Not now</string>
<!-- Toast text to describe the same input style already exists [CHAR LIMIT=64]-->
<string name="custom_input_style_already_exists">"The same input style already exists: <xliff:g id="input_style_name">%s</xliff:g>"</string>
<!-- Title of an option for usability study mode --> <!-- Title of an option for usability study mode -->
<string name="prefs_usability_study_mode">Usability study mode</string> <string name="prefs_usability_study_mode">Usability study mode</string>

View File

@ -24,6 +24,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@ -41,9 +42,11 @@ import android.view.inputmethod.InputMethodSubtype;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.SpinnerAdapter; import android.widget.SpinnerAdapter;
import android.widget.Toast;
import com.android.inputmethod.compat.CompatUtils; import com.android.inputmethod.compat.CompatUtils;
import java.util.ArrayList;
import java.util.TreeSet; import java.util.TreeSet;
public class AdditionalSubtypeSettings extends PreferenceFragment { public class AdditionalSubtypeSettings extends PreferenceFragment {
@ -60,7 +63,6 @@ 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 class SubtypeLocaleItem extends Pair<String, String>
implements Comparable<SubtypeLocaleItem> { implements Comparable<SubtypeLocaleItem> {
public SubtypeLocaleItem(String localeString, String displayName) { public SubtypeLocaleItem(String localeString, String displayName) {
@ -139,6 +141,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
private interface SubtypeDialogProxy { private interface SubtypeDialogProxy {
public void onRemovePressed(SubtypePreference subtypePref); public void onRemovePressed(SubtypePreference subtypePref);
public void onSavePressed(SubtypePreference subtypePref);
public void onAddPressed(SubtypePreference subtypePref); public void onAddPressed(SubtypePreference subtypePref);
public SubtypeLocaleAdapter getSubtypeLocaleAdapter(); public SubtypeLocaleAdapter getSubtypeLocaleAdapter();
public KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter(); public KeyboardLayoutSetAdapter getKeyboardLayoutSetAdapter();
@ -150,6 +153,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new"; private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new";
private InputMethodSubtype mSubtype; private InputMethodSubtype mSubtype;
private InputMethodSubtype mPreviousSubtype;
private final SubtypeDialogProxy mProxy; private final SubtypeDialogProxy mProxy;
private Spinner mSubtypeLocaleSpinner; private Spinner mSubtypeLocaleSpinner;
@ -182,6 +186,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
public void setSubtype(InputMethodSubtype subtype) { public void setSubtype(InputMethodSubtype subtype) {
mPreviousSubtype = mSubtype;
mSubtype = subtype; mSubtype = subtype;
if (isIncomplete()) { if (isIncomplete()) {
setTitle(null); setTitle(null);
@ -197,6 +202,14 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
public void revert() {
setSubtype(mPreviousSubtype);
}
public boolean hasBeenModified() {
return mSubtype != null && !mSubtype.equals(mPreviousSubtype);
}
@Override @Override
protected View onCreateDialogView() { protected View onCreateDialogView() {
final View v = super.onCreateDialogView(); final View v = super.onCreateDialogView();
@ -250,7 +263,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
super.onClick(dialog, which); super.onClick(dialog, which);
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
final boolean addPressed = isIncomplete(); final boolean isEditing = !isIncomplete();
final SubtypeLocaleItem locale = final SubtypeLocaleItem locale =
(SubtypeLocaleItem) mSubtypeLocaleSpinner.getSelectedItem(); (SubtypeLocaleItem) mSubtypeLocaleSpinner.getSelectedItem();
final KeyboardLayoutSetItem layout = final KeyboardLayoutSetItem layout =
@ -259,7 +272,9 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
locale.first, layout.first, ASCII_CAPABLE); locale.first, layout.first, ASCII_CAPABLE);
setSubtype(subtype); setSubtype(subtype);
notifyChanged(); notifyChanged();
if (addPressed) { if (isEditing) {
mProxy.onSavePressed(this);
} else {
mProxy.onAddPressed(this); mProxy.onAddPressed(this);
} }
break; break;
@ -414,18 +429,45 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
public void onRemovePressed(SubtypePreference subtypePref) { public void onRemovePressed(SubtypePreference subtypePref) {
mIsAddingNewSubtype = false; mIsAddingNewSubtype = false;
final PreferenceGroup group = getPreferenceScreen(); final PreferenceGroup group = getPreferenceScreen();
if (group != null) {
group.removePreference(subtypePref); group.removePreference(subtypePref);
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
} }
@Override
public void onSavePressed(SubtypePreference subtypePref) {
final InputMethodSubtype subtype = subtypePref.getSubtype();
if (!subtypePref.hasBeenModified()) {
return;
}
if (findDuplicatedSubtype(subtype) == null) {
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
return;
}
// Saved subtype is duplicated.
final PreferenceGroup group = getPreferenceScreen();
group.removePreference(subtypePref);
subtypePref.revert();
group.addPreference(subtypePref);
showSubtypeAlreadyExistsToast(subtype);
} }
@Override @Override
public void onAddPressed(SubtypePreference subtypePref) { public void onAddPressed(SubtypePreference subtypePref) {
mIsAddingNewSubtype = false; mIsAddingNewSubtype = false;
setAdditionalInputMethodSubtypes(getPrefSubtypes()); final InputMethodSubtype subtype = subtypePref.getSubtype();
if (findDuplicatedSubtype(subtype) == null) {
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
mSubtypePreferenceKeyForSubtypeEnabler = subtypePref.getKey(); mSubtypePreferenceKeyForSubtypeEnabler = subtypePref.getKey();
mSubtypeEnablerNotificationDialog = createDialog(subtypePref); mSubtypeEnablerNotificationDialog = createDialog(subtypePref);
mSubtypeEnablerNotificationDialog.show(); mSubtypeEnablerNotificationDialog.show();
return;
}
// Newly added subtype is duplicated.
final PreferenceGroup group = getPreferenceScreen();
group.removePreference(subtypePref);
showSubtypeAlreadyExistsToast(subtype);
} }
@Override @Override
@ -439,6 +481,21 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
}; };
private void showSubtypeAlreadyExistsToast(InputMethodSubtype subtype) {
final Context context = getActivity();
final Resources res = context.getResources();
final String message = res.getString(R.string.custom_input_style_already_exists,
SubtypeLocale.getSubtypeDisplayName(subtype, res));
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
private InputMethodSubtype findDuplicatedSubtype(InputMethodSubtype subtype) {
final String localeString = subtype.getLocale();
final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
return ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
getActivity(), localeString, keyboardLayoutSetName);
}
private AlertDialog createDialog(SubtypePreference subtypePref) { private AlertDialog createDialog(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)
@ -474,9 +531,9 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} }
} }
private String getPrefSubtypes() { private InputMethodSubtype[] getSubtypes() {
final PreferenceGroup group = getPreferenceScreen(); final PreferenceGroup group = getPreferenceScreen();
final StringBuilder sb = new StringBuilder(); final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
final int count = group.getPreferenceCount(); final int count = group.getPreferenceCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
final Preference pref = group.getPreference(i); final Preference pref = group.getPreference(i);
@ -484,13 +541,20 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
final SubtypePreference subtypePref = (SubtypePreference)pref; final SubtypePreference subtypePref = (SubtypePreference)pref;
// We should not save newly adding subtype to preference because it is incomplete. // We should not save newly adding subtype to preference because it is incomplete.
if (subtypePref.isIncomplete()) continue; if (subtypePref.isIncomplete()) continue;
final InputMethodSubtype subtype = subtypePref.getSubtype(); subtypes.add(subtypePref.getSubtype());
}
}
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) { if (sb.length() > 0) {
sb.append(AdditionalSubtype.PREF_SUBTYPE_SEPARATOR); sb.append(AdditionalSubtype.PREF_SUBTYPE_SEPARATOR);
} }
sb.append(AdditionalSubtype.getPrefSubtype(subtype)); sb.append(AdditionalSubtype.getPrefSubtype(subtype));
} }
}
return sb.toString(); return sb.toString();
} }
@ -498,7 +562,8 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
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 String prefSubtypes = getPrefSubtypes(); final InputMethodSubtype[] subtypes = getSubtypes();
final String prefSubtypes = getPrefSubtypes(subtypes);
if (prefSubtypes.equals(oldSubtypes)) { if (prefSubtypes.equals(oldSubtypes)) {
return; return;
} }
@ -509,12 +574,6 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
} finally { } finally {
editor.apply(); editor.apply();
} }
setAdditionalInputMethodSubtypes(prefSubtypes);
}
private void setAdditionalInputMethodSubtypes(final String prefSubtypes) {
final InputMethodSubtype[] subtypes =
AdditionalSubtype.createAdditionalSubtypesArray(prefSubtypes);
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), subtypes); ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), subtypes);
} }

View File

@ -167,8 +167,7 @@ public class ImfUtils {
return subtype; return subtype;
} }
} }
throw new RuntimeException("Can't find subtype for locale " + localeString return null;
+ " and keyboard layout " + keyboardLayoutSetName);
} }
public static void setAdditionalInputMethodSubtypes(Context context, public static void setAdditionalInputMethodSubtypes(Context context,

View File

@ -101,6 +101,9 @@ public class SubtypeSwitcher {
mCurrentSubtype = mImm.getCurrentInputMethodSubtype(); mCurrentSubtype = mImm.getCurrentInputMethodSubtype();
mNoLanguageSubtype = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet( mNoLanguageSubtype = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
service, SubtypeLocale.NO_LANGUAGE, SubtypeLocale.QWERTY); service, SubtypeLocale.NO_LANGUAGE, SubtypeLocale.QWERTY);
if (mNoLanguageSubtype == null) {
throw new RuntimeException("Can't find no lanugage with QWERTY subtype");
}
final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected()); mIsNetworkConnected = (info != null && info.isConnected());