Merge "Fix Cancelling a text action activity deletes the selected text" into mnc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
e94561ecbc
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user