5package io.flutter.plugin.text;
7import static io.flutter.Build.API_LEVELS;
9import android.annotation.TargetApi;
12import android.content.pm.PackageManager;
13import android.content.pm.ResolveInfo;
15import androidx.annotation.NonNull;
16import androidx.annotation.Nullable;
17import androidx.annotation.RequiresApi;
18import io.flutter.embedding.engine.plugins.FlutterPlugin;
19import io.flutter.embedding.engine.plugins.activity.ActivityAware;
20import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
21import io.flutter.embedding.engine.systemchannels.ProcessTextChannel;
22import io.flutter.plugin.common.MethodChannel;
23import io.flutter.plugin.common.PluginRegistry.ActivityResultListener;
24import java.util.HashMap;
33 private static final String TAG =
"ProcessTextPlugin";
36 @NonNull
private final PackageManager packageManager;
38 private Map<String, ResolveInfo> resolveInfosById;
45 this.processTextChannel = processTextChannel;
46 this.packageManager = processTextChannel.packageManager;
48 processTextChannel.setMethodHandler(
this);
53 if (resolveInfosById ==
null) {
56 Map<String, String>
result =
new HashMap<String, String>();
57 for (String
id : resolveInfosById.keySet()) {
58 final ResolveInfo
info = resolveInfosById.get(
id);
59 result.put(
id,
info.loadLabel(packageManager).toString());
68 @NonNull
boolean readOnly,
70 if (activityBinding ==
null) {
71 result.error(
"error",
"Plugin not bound to an Activity",
null);
76 result.error(
"error",
"Android version not supported",
null);
80 if (resolveInfosById ==
null) {
81 result.error(
"error",
"Can not process text actions before calling queryTextActions",
null);
85 final ResolveInfo
info = resolveInfosById.get(
id);
87 result.error(
"error",
"Text processing activity not found",
null);
91 Integer requestCode =
result.hashCode();
92 requestsByCode.put(requestCode,
result);
94 Intent intent =
new Intent();
95 intent.setClassName(
info.activityInfo.packageName,
info.activityInfo.name);
96 intent.setAction(Intent.ACTION_PROCESS_TEXT);
97 intent.setType(
"text/plain");
98 intent.putExtra(Intent.EXTRA_PROCESS_TEXT,
text);
99 intent.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, readOnly);
104 activityBinding.getActivity().startActivityForResult(intent, requestCode);
107 private void cacheResolveInfos() {
108 resolveInfosById =
new HashMap<String, ResolveInfo>();
114 Intent intent =
new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType(
"text/plain");
116 List<ResolveInfo> infos;
117 if (
Build.VERSION.SDK_INT >= API_LEVELS.API_33) {
118 infos = packageManager.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(0));
120 infos = packageManager.queryIntentActivities(intent, 0);
123 for (ResolveInfo
info : infos) {
124 final String
id =
info.activityInfo.name;
125 final String label =
info.loadLabel(packageManager).toString();
126 resolveInfosById.put(
id,
info);
139 @TargetApi(API_LEVELS.API_23)
140 @RequiresApi(API_LEVELS.API_23)
141 public
boolean onActivityResult(
int requestCode,
int resultCode, @Nullable Intent intent) {
143 if (!requestsByCode.containsKey(requestCode)) {
148 if (resultCode == Activity.RESULT_OK) {
149 result = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT);
151 requestsByCode.remove(requestCode).success(
result);
163 processTextChannel.setMethodHandler(
null);
181 this.activityBinding = binding;
182 this.activityBinding.addActivityResultListener(
this);
186 this.activityBinding.removeActivityResultListener(
this);
187 this.activityBinding =
null;
191 this.activityBinding = binding;
192 this.activityBinding.addActivityResultListener(
this);
196 this.activityBinding.removeActivityResultListener(
this);
197 this.activityBinding =
null;
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
void processTextAction( @NonNull String id, @NonNull String text, @NonNull boolean readOnly, @NonNull MethodChannel.Result result)
void onDetachedFromActivity()
void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding)
void onAttachedToActivity(@NonNull ActivityPluginBinding binding)
boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent intent)
ProcessTextPlugin(@NonNull ProcessTextChannel processTextChannel)
Map< String, String > queryTextActions()
void onAttachedToEngine(@NonNull FlutterPluginBinding binding)
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)
void onDetachedFromActivityForConfigChanges()
def Build(configs, env, options)