Save & restore additional subtype dialog state when orientation is changed
Bug: 6384416 Change-Id: Id5fa4742ad900fafb82cc37a0bbb9f380b99254dmain
parent
d454fcc2cf
commit
9f0a137f82
|
@ -19,6 +19,7 @@ package com.android.inputmethod.latin;
|
||||||
import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.ASCII_CAPABLE;
|
import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.ASCII_CAPABLE;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
@ -29,9 +30,7 @@ import android.preference.DialogPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceFragment;
|
import android.preference.PreferenceFragment;
|
||||||
import android.preference.PreferenceGroup;
|
import android.preference.PreferenceGroup;
|
||||||
import android.preference.PreferenceScreen;
|
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
@ -50,9 +49,10 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
private SubtypeLocaleAdapter mSubtypeLocaleAdapter;
|
private SubtypeLocaleAdapter mSubtypeLocaleAdapter;
|
||||||
private KeyboardLayoutSetAdapter mKeyboardLayoutSetAdapter;
|
private KeyboardLayoutSetAdapter mKeyboardLayoutSetAdapter;
|
||||||
|
|
||||||
private PreferenceGroup mSubtypePrefGroup;
|
private boolean mIsAddingNewSubtype;
|
||||||
|
|
||||||
private static final int MENU_ADD_SUBTYPE = Menu.FIRST;
|
private static final int MENU_ADD_SUBTYPE = Menu.FIRST;
|
||||||
|
private static final String SAVE_IS_ADDING_NEW_SUBTYPE = "is_adding_new_subtype";
|
||||||
|
|
||||||
static class SubtypeLocaleItem extends Pair<String, String>
|
static class SubtypeLocaleItem extends Pair<String, String>
|
||||||
implements Comparable<SubtypeLocaleItem> {
|
implements Comparable<SubtypeLocaleItem> {
|
||||||
|
@ -142,15 +142,24 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class SubtypePreference extends DialogPreference {
|
static class SubtypePreference extends DialogPreference {
|
||||||
|
private static final String KEY_PREFIX = "subtype_pref_";
|
||||||
|
private static final String KEY_NEW_SUBTYPE = KEY_PREFIX + "new";
|
||||||
|
|
||||||
private InputMethodSubtype mSubtype;
|
private InputMethodSubtype mSubtype;
|
||||||
|
|
||||||
private final SubtypeDialogProxy mProxy;
|
private final SubtypeDialogProxy mProxy;
|
||||||
private Spinner mSubtypeLocaleSpinner;
|
private Spinner mSubtypeLocaleSpinner;
|
||||||
private Spinner mKeyboardLayoutSetSpinner;
|
private Spinner mKeyboardLayoutSetSpinner;
|
||||||
|
|
||||||
|
public static SubtypePreference newIncompleteSubtypePreference(
|
||||||
|
Context context, SubtypeDialogProxy proxy) {
|
||||||
|
return new SubtypePreference(context, null, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
public SubtypePreference(Context context, InputMethodSubtype subtype,
|
public SubtypePreference(Context context, InputMethodSubtype subtype,
|
||||||
SubtypeDialogProxy proxy) {
|
SubtypeDialogProxy proxy) {
|
||||||
super(context, null);
|
super(context, null);
|
||||||
|
setDialogLayoutResource(R.layout.additional_subtype_dialog);
|
||||||
setPersistent(false);
|
setPersistent(false);
|
||||||
mProxy = proxy;
|
mProxy = proxy;
|
||||||
setSubtype(subtype);
|
setSubtype(subtype);
|
||||||
|
@ -160,34 +169,43 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
showDialog(null);
|
showDialog(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final boolean isIncomplete() {
|
||||||
|
return mSubtype == null;
|
||||||
|
}
|
||||||
|
|
||||||
public InputMethodSubtype getSubtype() {
|
public InputMethodSubtype getSubtype() {
|
||||||
return mSubtype;
|
return mSubtype;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSubtype(InputMethodSubtype subtype) {
|
public void setSubtype(InputMethodSubtype subtype) {
|
||||||
mSubtype = subtype;
|
mSubtype = subtype;
|
||||||
if (subtype == null) {
|
if (isIncomplete()) {
|
||||||
setTitle(null);
|
setTitle(null);
|
||||||
setDialogTitle(R.string.add_style);
|
setDialogTitle(R.string.add_style);
|
||||||
|
setKey(KEY_NEW_SUBTYPE);
|
||||||
} else {
|
} else {
|
||||||
final String displayName = SubtypeLocale.getFullDisplayName(subtype);
|
final String displayName = SubtypeLocale.getFullDisplayName(subtype);
|
||||||
setTitle(displayName);
|
setTitle(displayName);
|
||||||
setDialogTitle(displayName);
|
setDialogTitle(displayName);
|
||||||
|
setKey(KEY_PREFIX + subtype.getLocale() + "_"
|
||||||
|
+ SubtypeLocale.getKeyboardLayoutSetName(subtype));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected View onCreateDialogView() {
|
||||||
|
final View v = super.onCreateDialogView();
|
||||||
|
mSubtypeLocaleSpinner = (Spinner) v.findViewById(R.id.subtype_locale_spinner);
|
||||||
|
mSubtypeLocaleSpinner.setAdapter(mProxy.getSubtypeLocaleAdapter());
|
||||||
|
mKeyboardLayoutSetSpinner = (Spinner) v.findViewById(R.id.keyboard_layout_set_spinner);
|
||||||
|
mKeyboardLayoutSetSpinner.setAdapter(mProxy.getKeyboardLayoutSetAdapter());
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
|
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
|
||||||
final Context context = builder.getContext();
|
final Context context = builder.getContext();
|
||||||
final View v = LayoutInflater.from(context).inflate(
|
if (isIncomplete()) {
|
||||||
R.layout.additional_subtype_dialog, null);
|
|
||||||
builder.setView(v);
|
|
||||||
mSubtypeLocaleSpinner = (Spinner) v.findViewById(R.id.subtype_locale_spinner);
|
|
||||||
mSubtypeLocaleSpinner.setAdapter(mProxy.getSubtypeLocaleAdapter());
|
|
||||||
mKeyboardLayoutSetSpinner = (Spinner) v.findViewById(R.id.keyboard_layout_set_spinner);
|
|
||||||
mKeyboardLayoutSetSpinner.setAdapter(mProxy.getKeyboardLayoutSetAdapter());
|
|
||||||
|
|
||||||
if (mSubtype == null) {
|
|
||||||
builder.setPositiveButton(R.string.add, this)
|
builder.setPositiveButton(R.string.add, this)
|
||||||
.setNegativeButton(android.R.string.cancel, this);
|
.setNegativeButton(android.R.string.cancel, this);
|
||||||
} else {
|
} else {
|
||||||
|
@ -237,28 +255,49 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int getSpinnerPosition(Spinner spinner) {
|
||||||
|
if (spinner == null) return -1;
|
||||||
|
return spinner.getSelectedItemPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setSpinnerPosition(Spinner spinner, int position) {
|
||||||
|
if (spinner == null || position < 0) return;
|
||||||
|
spinner.setSelection(position);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Parcelable onSaveInstanceState() {
|
protected Parcelable onSaveInstanceState() {
|
||||||
final SavedState myState = new SavedState(super.onSaveInstanceState());
|
final Parcelable superState = super.onSaveInstanceState();
|
||||||
|
final Dialog dialog = getDialog();
|
||||||
|
if (dialog == null || !dialog.isShowing()) {
|
||||||
|
return superState;
|
||||||
|
}
|
||||||
|
|
||||||
|
final SavedState myState = new SavedState(superState);
|
||||||
myState.mSubtype = mSubtype;
|
myState.mSubtype = mSubtype;
|
||||||
|
myState.mSubtypeLocaleSelectedPos = getSpinnerPosition(mSubtypeLocaleSpinner);
|
||||||
|
myState.mKeyboardLayoutSetSelectedPos = getSpinnerPosition(mKeyboardLayoutSetSpinner);
|
||||||
return myState;
|
return myState;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onRestoreInstanceState(Parcelable state) {
|
protected void onRestoreInstanceState(Parcelable state) {
|
||||||
if (state instanceof SavedState) {
|
if (!(state instanceof SavedState)) {
|
||||||
final SavedState myState = (SavedState) state;
|
|
||||||
super.onRestoreInstanceState(state);
|
|
||||||
setSubtype(myState.mSubtype);
|
|
||||||
} else {
|
|
||||||
super.onRestoreInstanceState(state);
|
super.onRestoreInstanceState(state);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final SavedState myState = (SavedState) state;
|
||||||
|
super.onRestoreInstanceState(myState.getSuperState());
|
||||||
|
setSpinnerPosition(mSubtypeLocaleSpinner, myState.mSubtypeLocaleSelectedPos);
|
||||||
|
setSpinnerPosition(mKeyboardLayoutSetSpinner, myState.mKeyboardLayoutSetSelectedPos);
|
||||||
|
setSubtype(myState.mSubtype);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class SavedState extends Preference.BaseSavedState {
|
static class SavedState extends Preference.BaseSavedState {
|
||||||
InputMethodSubtype mSubtype;
|
InputMethodSubtype mSubtype;
|
||||||
private static final byte VALID = 1;
|
int mSubtypeLocaleSelectedPos;
|
||||||
private static final byte INVALID = 0;
|
int mKeyboardLayoutSetSelectedPos;
|
||||||
|
|
||||||
public SavedState(Parcelable superState) {
|
public SavedState(Parcelable superState) {
|
||||||
super(superState);
|
super(superState);
|
||||||
|
@ -267,23 +306,19 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
@Override
|
@Override
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
super.writeToParcel(dest, flags);
|
super.writeToParcel(dest, flags);
|
||||||
if (mSubtype != null) {
|
dest.writeInt(mSubtypeLocaleSelectedPos);
|
||||||
dest.writeByte(VALID);
|
dest.writeInt(mKeyboardLayoutSetSelectedPos);
|
||||||
mSubtype.writeToParcel(dest, 0);
|
dest.writeParcelable(mSubtype, 0);
|
||||||
} else {
|
|
||||||
dest.writeByte(INVALID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SavedState(Parcel source) {
|
public SavedState(Parcel source) {
|
||||||
super(source);
|
super(source);
|
||||||
if (source.readByte() == VALID) {
|
mSubtypeLocaleSelectedPos = source.readInt();
|
||||||
mSubtype = source.readParcelable(null);
|
mKeyboardLayoutSetSelectedPos = source.readInt();
|
||||||
} else {
|
mSubtype = (InputMethodSubtype)source.readParcelable(null);
|
||||||
mSubtype = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("hiding")
|
||||||
public static final Parcelable.Creator<SavedState> CREATOR =
|
public static final Parcelable.Creator<SavedState> CREATOR =
|
||||||
new Parcelable.Creator<SavedState>() {
|
new Parcelable.Creator<SavedState>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -309,26 +344,43 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.additional_subtype_settings);
|
addPreferencesFromResource(R.xml.additional_subtype_settings);
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
mSubtypePrefGroup = getPreferenceScreen();
|
|
||||||
|
|
||||||
mPrefs = getPreferenceManager().getSharedPreferences();
|
mPrefs = getPreferenceManager().getSharedPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(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);
|
||||||
|
|
||||||
// TODO: Restore editing dialog if any.
|
final String prefSubtypes =
|
||||||
|
SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources());
|
||||||
|
setPrefSubtypes(prefSubtypes, context);
|
||||||
|
|
||||||
|
mIsAddingNewSubtype = (savedInstanceState != null)
|
||||||
|
&& savedInstanceState.containsKey(SAVE_IS_ADDING_NEW_SUBTYPE);
|
||||||
|
if (mIsAddingNewSubtype) {
|
||||||
|
getPreferenceScreen().addPreference(
|
||||||
|
SubtypePreference.newIncompleteSubtypePreference(context, mSubtypeProxy));
|
||||||
|
}
|
||||||
|
|
||||||
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
if (mIsAddingNewSubtype) {
|
||||||
|
outState.putBoolean(SAVE_IS_ADDING_NEW_SUBTYPE, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final SubtypeDialogProxy mSubtypeProxy = new SubtypeDialogProxy() {
|
private final SubtypeDialogProxy mSubtypeProxy = new SubtypeDialogProxy() {
|
||||||
@Override
|
@Override
|
||||||
public void onRemovePressed(SubtypePreference subtypePref) {
|
public void onRemovePressed(SubtypePreference subtypePref) {
|
||||||
final PreferenceGroup group = mSubtypePrefGroup;
|
mIsAddingNewSubtype = false;
|
||||||
|
final PreferenceGroup group = getPreferenceScreen();
|
||||||
if (group != null) {
|
if (group != null) {
|
||||||
group.removePreference(subtypePref);
|
group.removePreference(subtypePref);
|
||||||
}
|
}
|
||||||
|
@ -346,7 +398,7 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
};
|
};
|
||||||
|
|
||||||
private void setPrefSubtypes(String prefSubtypes, Context context) {
|
private void setPrefSubtypes(String prefSubtypes, Context context) {
|
||||||
final PreferenceGroup group = mSubtypePrefGroup;
|
final PreferenceGroup group = getPreferenceScreen();
|
||||||
group.removeAll();
|
group.removeAll();
|
||||||
final InputMethodSubtype[] subtypesArray =
|
final InputMethodSubtype[] subtypesArray =
|
||||||
AdditionalSubtype.createAdditionalSubtypesArray(prefSubtypes);
|
AdditionalSubtype.createAdditionalSubtypesArray(prefSubtypes);
|
||||||
|
@ -358,13 +410,16 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPrefSubtypes() {
|
private String getPrefSubtypes() {
|
||||||
|
final PreferenceGroup group = getPreferenceScreen();
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
final int count = mSubtypePrefGroup.getPreferenceCount();
|
final int count = group.getPreferenceCount();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
final Preference pref = mSubtypePrefGroup.getPreference(i);
|
final Preference pref = group.getPreference(i);
|
||||||
if (pref instanceof SubtypePreference) {
|
if (pref instanceof SubtypePreference) {
|
||||||
final InputMethodSubtype subtype = ((SubtypePreference)pref).getSubtype();
|
final SubtypePreference subtypePref = (SubtypePreference)pref;
|
||||||
if (subtype == null) continue;
|
// We should not save newly adding subtype to preference because it is incomplete.
|
||||||
|
if (subtypePref.isIncomplete()) continue;
|
||||||
|
final InputMethodSubtype subtype = subtypePref.getSubtype();
|
||||||
if (sb.length() > 0) {
|
if (sb.length() > 0) {
|
||||||
sb.append(AdditionalSubtype.PREF_SUBTYPE_SEPARATOR);
|
sb.append(AdditionalSubtype.PREF_SUBTYPE_SEPARATOR);
|
||||||
}
|
}
|
||||||
|
@ -374,15 +429,6 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
|
|
||||||
final String prefSubtypes =
|
|
||||||
SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources());
|
|
||||||
setPrefSubtypes(prefSubtypes, getActivity());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
@ -403,20 +449,6 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), subtypes);
|
ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), subtypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
|
||||||
super.onSaveInstanceState(outState);
|
|
||||||
// TODO: save editing dialog state.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPreferenceTreeClick(PreferenceScreen prefScreen, Preference pref) {
|
|
||||||
if (pref instanceof SubtypePreference) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return super.onPreferenceTreeClick(prefScreen, pref);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, 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);
|
||||||
|
@ -427,10 +459,11 @@ public class AdditionalSubtypeSettings extends PreferenceFragment {
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
final int itemId = item.getItemId();
|
final int itemId = item.getItemId();
|
||||||
if (itemId == MENU_ADD_SUBTYPE) {
|
if (itemId == MENU_ADD_SUBTYPE) {
|
||||||
final SubtypePreference subtypePref = new SubtypePreference(
|
final SubtypePreference newSubtype =
|
||||||
getActivity(), null, mSubtypeProxy);
|
SubtypePreference.newIncompleteSubtypePreference(getActivity(), mSubtypeProxy);
|
||||||
mSubtypePrefGroup.addPreference(subtypePref);
|
getPreferenceScreen().addPreference(newSubtype);
|
||||||
subtypePref.show();
|
newSubtype.show();
|
||||||
|
mIsAddingNewSubtype = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
|
|
Loading…
Reference in New Issue