From 027711904ba478bc89a66a749d4d00db067223ec Mon Sep 17 00:00:00 2001 From: Siyamed Sinir Date: Fri, 5 Feb 2016 16:08:59 -0800 Subject: [PATCH] Do not save TextView text when freezesText is false Currently TextView state is saved whenever selection is set even if freezesText is false. This causes inconsistencies with the described behavior for the attribute. This CL updates the behavior as: - Always save the text for EditText - Always save the selection if there are any. - Do not save the text for TextView if freezesText is false. - During onRestoreInstanceState if selection is out of the text boundaries, do not restore the selection. Bug: 22076905 Change-Id: I5e05d343e752a7d106c8881993e4d95ae21a38ce (cherry picked from commit 8535836673c801effce097c54f55814ef88448e7) --- core/java/android/widget/EditText.java | 5 ++++ core/java/android/widget/TextView.java | 48 ++++++++++++++++++++-------------- core/res/res/values/attrs.xml | 4 ++- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/core/java/android/widget/EditText.java b/core/java/android/widget/EditText.java index e31bbe9e6002..1d242d38ab58 100644 --- a/core/java/android/widget/EditText.java +++ b/core/java/android/widget/EditText.java @@ -65,6 +65,11 @@ public class EditText extends TextView { } @Override + public boolean getFreezesText() { + return true; + } + + @Override protected boolean getDefaultEditable() { return true; } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 712a04bf6fae..692b39df9ac8 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -4101,36 +4101,42 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener Parcelable superState = super.onSaveInstanceState(); // Save state if we are forced to - boolean save = mFreezesText; - int start = 0; - int end = 0; + final boolean freezesText = getFreezesText(); + boolean hasSelection = false; + int start = -1; + int end = -1; if (mText != null) { start = getSelectionStart(); end = getSelectionEnd(); if (start >= 0 || end >= 0) { // Or save state if there is a selection - save = true; + hasSelection = true; } } - if (save) { + if (freezesText || hasSelection) { SavedState ss = new SavedState(superState); - // XXX Should also save the current scroll position! - ss.selStart = start; - ss.selEnd = end; - if (mText instanceof Spanned) { - Spannable sp = new SpannableStringBuilder(mText); + if (freezesText) { + if (mText instanceof Spanned) { + final Spannable sp = new SpannableStringBuilder(mText); - if (mEditor != null) { - removeMisspelledSpans(sp); - sp.removeSpan(mEditor.mSuggestionRangeSpan); + if (mEditor != null) { + removeMisspelledSpans(sp); + sp.removeSpan(mEditor.mSuggestionRangeSpan); + } + + ss.text = sp; + } else { + ss.text = mText.toString(); } + } - ss.text = sp; - } else { - ss.text = mText.toString(); + if (hasSelection) { + // XXX Should also save the current scroll position! + ss.selStart = start; + ss.selEnd = end; } if (isFocused() && start >= 0 && end >= 0) { @@ -4224,7 +4230,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * position. By default this is false, not saving the text. Set to true * if the text in the text view is not being saved somewhere else in * persistent storage (such as in a content provider) so that if the - * view is later thawed the user will not lose their data. + * view is later thawed the user will not lose their data. For + * {@link android.widget.EditText} it is always enabled, regardless of + * the value of the attribute. * * @param freezesText Controls whether a frozen icicle should include the * entire text data: true to include it, false to not. @@ -4238,7 +4246,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener /** * Return whether this text view is including its entire text contents - * in frozen icicles. + * in frozen icicles. For {@link android.widget.EditText} it always returns true. * * @return Returns true if text is included, false if it isn't. * @@ -10111,8 +10119,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * {@link View#onSaveInstanceState}. */ public static class SavedState extends BaseSavedState { - int selStart; - int selEnd; + int selStart = -1; + int selEnd = -1; CharSequence text; boolean frozenWithFocus; CharSequence error; diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index b9d86617b1de..be8577ae9b65 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -4430,7 +4430,9 @@ i inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored - in a persistent place such as a content provider. --> + in a persistent place such as a content provider. For + {@link android.widget.EditText} it is always enabled, regardless + of the value of the attribute. -->