diff options
| author | Clara Bayarri <clarabayarri@google.com> | 2015-05-19 10:42:17 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-05-19 10:42:20 +0000 |
| commit | e94561ecbcb7f6df2446b811de9ad5cfaff29d99 (patch) | |
| tree | 0adada166e2e2e7dbd1953fb6ed3e189ff991447 | |
| parent | aba6cccd40e0393d7bcff7f6edb5ca8cfdd4c7b1 (diff) | |
| parent | 5b7665a1f2403a03c1f9baf73a4aa8f5a66b1352 (diff) | |
| download | frameworks_base-e94561ecbcb7f6df2446b811de9ad5cfaff29d99.zip frameworks_base-e94561ecbcb7f6df2446b811de9ad5cfaff29d99.tar.gz frameworks_base-e94561ecbcb7f6df2446b811de9ad5cfaff29d99.tar.bz2 | |
Merge "Fix Cancelling a text action activity deletes the selected text" into mnc-dev
| -rw-r--r-- | core/java/android/widget/TextView.java | 24 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/widget/TextViewTest.java | 66 |
2 files changed, 81 insertions, 9 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 5acd79f..a93e7ef 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -24,6 +24,7 @@ import android.annotation.Nullable; import android.annotation.StringRes; import android.annotation.StyleRes; import android.annotation.XmlRes; +import android.app.Activity; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -1465,16 +1466,22 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PROCESS_TEXT_REQUEST_CODE) { - CharSequence result = data != null - ? data.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT) - : ""; - if (isTextEditable()) { - replaceSelectionWithText(result); - } else { - if (result.length() > 0) { - Toast.makeText(getContext(), String.valueOf(result), Toast.LENGTH_LONG).show(); + if (resultCode == Activity.RESULT_OK && data != null) { + CharSequence result = data.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT); + if (result != null) { + if (isTextEditable()) { + replaceSelectionWithText(result); + } else { + if (result.length() > 0) { + Toast.makeText(getContext(), String.valueOf(result), Toast.LENGTH_LONG) + .show(); + } + } } } + if (mEditor.hasSelectionController()) { + mEditor.startSelectionActionModeWithSelection(); + } } } @@ -9279,7 +9286,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener void replaceSelectionWithText(CharSequence text) { ((Editable) mText).replace(getSelectionStart(), getSelectionEnd(), text); - mEditor.startSelectionActionModeWithSelection(); } /** diff --git a/core/tests/coretests/src/android/widget/TextViewTest.java b/core/tests/coretests/src/android/widget/TextViewTest.java index af6df1a..0b94f8b 100644 --- a/core/tests/coretests/src/android/widget/TextViewTest.java +++ b/core/tests/coretests/src/android/widget/TextViewTest.java @@ -16,9 +16,13 @@ package android.widget; +import android.app.Activity; +import android.content.Intent; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.text.GetChars; +import android.text.Selection; +import android.text.Spannable; /** * TextViewTest tests {@link TextView}. @@ -54,4 +58,66 @@ public class TextViewTest extends AndroidTestCase { assertEquals('o', c2[4]); assertEquals('\0', c2[5]); } + + public void testProcessTextActivityResultNonEditable() { + TextView tv = new TextView(mContext); + CharSequence originalText = "This is some text."; + tv.setText(originalText, TextView.BufferType.SPANNABLE); + assertEquals(originalText, tv.getText().toString()); + tv.setTextIsSelectable(true); + Selection.setSelection((Spannable) tv.getText(), 0, tv.getText().length()); + + CharSequence newText = "Text is replaced."; + Intent data = new Intent(); + data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText); + tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, data); + + // This is a TextView, which can't be modified. Hence no change should have been made. + assertEquals(originalText, tv.getText().toString()); + } + + public void testProcessTextActivityResultEditable() { + EditText tv = new EditText(mContext); + CharSequence originalText = "This is some text."; + tv.setText(originalText, TextView.BufferType.SPANNABLE); + assertEquals(originalText, tv.getText().toString()); + tv.setTextIsSelectable(true); + Selection.setSelection(tv.getText(), 0, tv.getText().length()); + + CharSequence newText = "Text is replaced."; + Intent data = new Intent(); + data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText); + tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, data); + + assertEquals(newText, tv.getText().toString()); + } + + public void testProcessTextActivityResultCancel() { + EditText tv = new EditText(mContext); + CharSequence originalText = "This is some text."; + tv.setText(originalText, TextView.BufferType.SPANNABLE); + assertEquals(originalText, tv.getText().toString()); + tv.setTextIsSelectable(true); + Selection.setSelection(tv.getText(), 0, tv.getText().length()); + + CharSequence newText = "Text is replaced."; + Intent data = new Intent(); + data.putExtra(Intent.EXTRA_PROCESS_TEXT, newText); + tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_CANCELED, data); + + assertEquals(originalText, tv.getText().toString()); + } + + public void testProcessTextActivityNoData() { + EditText tv = new EditText(mContext); + CharSequence originalText = "This is some text."; + tv.setText(originalText, TextView.BufferType.SPANNABLE); + assertEquals(originalText, tv.getText().toString()); + tv.setTextIsSelectable(true); + Selection.setSelection(tv.getText(), 0, tv.getText().length()); + + tv.onActivityResult(TextView.PROCESS_TEXT_REQUEST_CODE, Activity.RESULT_OK, null); + + assertEquals(originalText, tv.getText().toString()); + } } |
