Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 641c524

Browse files
committed
Use activity name as text action ID
1 parent 30c0c13 commit 641c524

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

shell/platform/android/io/flutter/embedding/engine/systemchannels/ProcessTextChannel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
6060
switch (method) {
6161
case METHOD_QUERY_TEXT_ACTIONS:
6262
try {
63-
Map<Integer, String> actions = processTextMethodHandler.queryTextActions();
63+
Map<String, String> actions = processTextMethodHandler.queryTextActions();
6464
result.success(actions);
6565
} catch (IllegalStateException exception) {
6666
result.error("error", exception.getMessage(), null);
@@ -69,7 +69,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
6969
case METHOD_PROCESS_TEXT_ACTION:
7070
try {
7171
final ArrayList<Object> argumentList = (ArrayList<Object>) args;
72-
int id = (int) (argumentList.get(0));
72+
String id = (String) (argumentList.get(0));
7373
String text = (String) (argumentList.get(1));
7474
boolean readOnly = (boolean) (argumentList.get(2));
7575
processTextMethodHandler.processTextAction(id, text, readOnly, result);
@@ -101,7 +101,7 @@ public void setMethodHandler(@Nullable ProcessTextMethodHandler processTextMetho
101101

102102
public interface ProcessTextMethodHandler {
103103
/** Requests the map of text actions. Each text action has a unique id and a localized label. */
104-
Map<Integer, String> queryTextActions();
104+
Map<String, String> queryTextActions();
105105

106106
/**
107107
* Requests to run a text action on a given input text.
@@ -114,7 +114,7 @@ public interface ProcessTextMethodHandler {
114114
* @param result The method channel result instance used to reply.
115115
*/
116116
void processTextAction(
117-
@NonNull int id,
117+
@NonNull String id,
118118
@NonNull String input,
119119
@NonNull boolean readOnly,
120120
@NonNull MethodChannel.Result result);

shell/platform/android/io/flutter/plugin/text/ProcessTextPlugin.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ProcessTextPlugin
3333
@NonNull private final ProcessTextChannel processTextChannel;
3434
@NonNull private final PackageManager packageManager;
3535
@Nullable private ActivityPluginBinding activityBinding;
36-
private Map<Integer, ResolveInfo> resolveInfosById;
36+
private Map<String, ResolveInfo> resolveInfosById;
3737

3838
@NonNull
3939
private Map<Integer, MethodChannel.Result> requestsByCode =
@@ -47,13 +47,13 @@ public ProcessTextPlugin(@NonNull ProcessTextChannel processTextChannel) {
4747
}
4848

4949
@Override
50-
public Map<Integer, String> queryTextActions() {
50+
public Map<String, String> queryTextActions() {
5151
if (resolveInfosById == null) {
52-
resolveInfosById = new HashMap<Integer, ResolveInfo>();
52+
resolveInfosById = new HashMap<String, ResolveInfo>();
5353
cacheResolveInfos();
5454
}
55-
Map<Integer, String> result = new HashMap<Integer, String>();
56-
for (Integer id : resolveInfosById.keySet()) {
55+
Map<String, String> result = new HashMap<String, String>();
56+
for (String id : resolveInfosById.keySet()) {
5757
final ResolveInfo info = resolveInfosById.get(id);
5858
result.put(id, info.loadLabel(packageManager).toString());
5959
}
@@ -62,7 +62,7 @@ public Map<Integer, String> queryTextActions() {
6262

6363
@Override
6464
public void processTextAction(
65-
@NonNull int id,
65+
@NonNull String id,
6666
@NonNull String text,
6767
@NonNull boolean readOnly,
6868
@NonNull MethodChannel.Result result) {
@@ -97,7 +97,8 @@ public void processTextAction(
9797
intent.putExtra(Intent.EXTRA_PROCESS_TEXT, text);
9898
intent.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, readOnly);
9999

100-
// Start the text processing activity. When the activity complets, the onActivityResult callback
100+
// Start the text processing activity. When the activity completes, the onActivityResult
101+
// callback
101102
// is called.
102103
activityBinding.getActivity().startActivityForResult(intent, requestCode);
103104
}
@@ -116,12 +117,11 @@ private void cacheResolveInfos() {
116117
infos = packageManager.queryIntentActivities(intent, 0);
117118
}
118119

119-
// Assign an internal id for communication between the engine and the framework.
120-
int index = 0;
121120
resolveInfosById.clear();
122121
for (ResolveInfo info : infos) {
122+
final String id = info.activityInfo.name;
123123
final String label = info.loadLabel(packageManager).toString();
124-
resolveInfosById.put(index++, info);
124+
resolveInfosById.put(id, info);
125125
}
126126
}
127127

@@ -131,7 +131,7 @@ private void cacheResolveInfos() {
131131
* <p>When an activity returns a value, the request is completed successfully and returns the
132132
* processed text.
133133
*
134-
* <p>When an activity does not return a valuen. the request is completed successfully and returns
134+
* <p>When an activity does not return a value. the request is completed successfully and returns
135135
* null.
136136
*/
137137
@TargetApi(Build.VERSION_CODES.M)
@@ -168,7 +168,7 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
168168

169169
// ActivityAware interface implementation.
170170
//
171-
// Store the binding and manage the activity result listerner.
171+
// Store the binding and manage the activity result listener.
172172

173173
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
174174
this.activityBinding = binding;

shell/platform/android/test/io/flutter/plugin/text/ProcessTextPluginTest.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ public void performQueryTextActions() {
8484
new ProcessTextChannel(mockBinaryMessenger, mockPackageManager);
8585

8686
// Set up mocked result for PackageManager.queryIntentActivities.
87-
ResolveInfo action1 = mock(ResolveInfo.class);
88-
when(action1.loadLabel(mockPackageManager)).thenReturn("Action1");
89-
ResolveInfo action2 = mock(ResolveInfo.class);
90-
when(action2.loadLabel(mockPackageManager)).thenReturn("Action2");
87+
ResolveInfo action1 = createFakeResolveInfo("Action1", mockPackageManager);
88+
ResolveInfo action2 = createFakeResolveInfo("Action2", mockPackageManager);
9189
List<ResolveInfo> infos = new ArrayList<ResolveInfo>(Arrays.asList(action1, action2));
9290
Intent intent = new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain");
9391
when(mockPackageManager.queryIntentActivities(
@@ -96,9 +94,9 @@ public void performQueryTextActions() {
9694

9795
// ProcessTextPlugin should retrieve the mocked text actions.
9896
ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
99-
Map<Integer, String> textActions = processTextPlugin.queryTextActions();
100-
final int action1Id = 0;
101-
final int action2Id = 1;
97+
Map<String, String> textActions = processTextPlugin.queryTextActions();
98+
final String action1Id = "mockActivityName.Action1";
99+
final String action2Id = "mockActivityName.Action2";
102100
assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
103101
}
104102

@@ -121,9 +119,9 @@ public void performProcessTextActionWithNoReturnedValue() {
121119

122120
// ProcessTextPlugin should retrieve the mocked text actions.
123121
ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
124-
Map<Integer, String> textActions = processTextPlugin.queryTextActions();
125-
final int action1Id = 0;
126-
final int action2Id = 1;
122+
Map<String, String> textActions = processTextPlugin.queryTextActions();
123+
final String action1Id = "mockActivityName.Action1";
124+
final String action2Id = "mockActivityName.Action2";
127125
assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
128126

129127
// Set up the activity binding.
@@ -170,9 +168,9 @@ public void performProcessTextActionWithReturnedValue() {
170168

171169
// ProcessTextPlugin should retrieve the mocked text actions.
172170
ProcessTextPlugin processTextPlugin = new ProcessTextPlugin(processTextChannel);
173-
Map<Integer, String> textActions = processTextPlugin.queryTextActions();
174-
final int action1Id = 0;
175-
final int action2Id = 1;
171+
Map<String, String> textActions = processTextPlugin.queryTextActions();
172+
final String action1Id = "mockActivityName.Action1";
173+
final String action2Id = "mockActivityName.Action2";
176174
assertEquals(textActions, Map.of(action1Id, "Action1", action2Id, "Action2"));
177175

178176
// Set up the activity binding.
@@ -217,7 +215,7 @@ private ResolveInfo createFakeResolveInfo(String label, PackageManager mockPacka
217215
packageNameField.set(activityInfo, "mockActivityPackageName");
218216
Field nameField = PackageItemInfo.class.getDeclaredField("name");
219217
nameField.setAccessible(true);
220-
nameField.set(activityInfo, "mockActivityName");
218+
nameField.set(activityInfo, "mockActivityName." + label);
221219
} catch (Exception ex) {
222220
// Test will failed if reflection APIs throw.
223221
}

0 commit comments

Comments
 (0)