|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugin.text; |
| 6 | + |
| 7 | +import android.app.Activity; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.pm.PackageManager; |
| 10 | +import android.content.pm.ResolveInfo; |
| 11 | +import android.os.Build; |
| 12 | +import androidx.annotation.NonNull; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 15 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 16 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| 17 | +import io.flutter.embedding.engine.systemchannels.ProcessTextChannel; |
| 18 | +import io.flutter.plugin.common.MethodChannel; |
| 19 | +import io.flutter.plugin.common.PluginRegistry.ActivityResultListener; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +public class ProcessTextPlugin implements FlutterPlugin, ActivityAware, ActivityResultListener { |
| 25 | + private static final String TAG = "ProcessTextPlugin"; |
| 26 | + |
| 27 | + @NonNull private final ProcessTextChannel processTextChannel; |
| 28 | + @NonNull private final PackageManager packageManager; |
| 29 | + @Nullable private ActivityPluginBinding activityBinding; |
| 30 | + @NonNull private Map<Integer, ResolveInfo> resolveInfosById = new HashMap<Integer, ResolveInfo>(); |
| 31 | + |
| 32 | + @NonNull |
| 33 | + private Map<Integer, MethodChannel.Result> requestsByCode = |
| 34 | + new HashMap<Integer, MethodChannel.Result>(); |
| 35 | + |
| 36 | + public ProcessTextPlugin(@NonNull ProcessTextChannel processTextChannel) { |
| 37 | + this.processTextChannel = processTextChannel; |
| 38 | + this.packageManager = processTextChannel.packageManager; |
| 39 | + |
| 40 | + this.cacheResolveInfos(); |
| 41 | + |
| 42 | + processTextChannel.setMethodHandler( |
| 43 | + new ProcessTextChannel.ProcessTextMethodHandler() { |
| 44 | + @Override |
| 45 | + public Map<Integer, String> queryTextActions() { |
| 46 | + Map<Integer, String> result = new HashMap<Integer, String>(); |
| 47 | + for (Integer id : resolveInfosById.keySet()) { |
| 48 | + final ResolveInfo info = resolveInfosById.get(id); |
| 49 | + result.put(id, info.loadLabel(packageManager).toString()); |
| 50 | + } |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void processTextAction( |
| 56 | + @NonNull int id, |
| 57 | + @NonNull String text, |
| 58 | + @NonNull boolean readOnly, |
| 59 | + @NonNull MethodChannel.Result result) { |
| 60 | + if (activityBinding == null) { |
| 61 | + result.error("error", "Plugin not bound to an Activity", null); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { |
| 66 | + result.error("error", "Android version not supported", null); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + final ResolveInfo info = resolveInfosById.get(id); |
| 71 | + if (info == null) { |
| 72 | + result.error("error", "Text processing activity not found", null); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + Integer requestCode = result.hashCode(); |
| 77 | + requestsByCode.put(requestCode, result); |
| 78 | + |
| 79 | + Intent intent = |
| 80 | + new Intent() |
| 81 | + .setClassName(info.activityInfo.packageName, info.activityInfo.name) |
| 82 | + .setAction(Intent.ACTION_PROCESS_TEXT) |
| 83 | + .setType("text/plain") |
| 84 | + .putExtra(Intent.EXTRA_PROCESS_TEXT, text) |
| 85 | + .putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, readOnly); |
| 86 | + |
| 87 | + // Start the text processing activity. onActivityResult callback is called |
| 88 | + // when the activity completes. |
| 89 | + activityBinding.getActivity().startActivityForResult(intent, requestCode); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + private void cacheResolveInfos() { |
| 95 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + Intent intent = new Intent().setAction(Intent.ACTION_PROCESS_TEXT).setType("text/plain"); |
| 100 | + |
| 101 | + List<ResolveInfo> infos; |
| 102 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 103 | + infos = packageManager.queryIntentActivities(intent, PackageManager.ResolveInfoFlags.of(0)); |
| 104 | + } else { |
| 105 | + infos = packageManager.queryIntentActivities(intent, 0); |
| 106 | + } |
| 107 | + |
| 108 | + // Assign an internal id for communication between the engine and the framework. |
| 109 | + int index = 0; |
| 110 | + resolveInfosById.clear(); |
| 111 | + for (ResolveInfo info : infos) { |
| 112 | + final String label = info.loadLabel(packageManager).toString(); |
| 113 | + resolveInfosById.put(index++, info); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Executed when a text processing activity terminates. |
| 119 | + * |
| 120 | + * <p>The result is null when an activity does not return an updated text. |
| 121 | + */ |
| 122 | + public boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) { |
| 123 | + String result = null; |
| 124 | + if (resultCode == Activity.RESULT_OK) { |
| 125 | + result = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT); |
| 126 | + } |
| 127 | + requestsByCode.remove(requestCode).success(result); |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Unregisters this {@code ProcessTextPlugin} as the {@code |
| 133 | + * ProcessTextChannel.ProcessTextMethodHandler}, for the {@link |
| 134 | + * io.flutter.embedding.engine.systemchannels.ProcessTextChannel}. |
| 135 | + * |
| 136 | + * <p>Do not invoke any methods on a {@code ProcessTextPlugin} after invoking this method. |
| 137 | + */ |
| 138 | + public void destroy() { |
| 139 | + processTextChannel.setMethodHandler(null); |
| 140 | + } |
| 141 | + |
| 142 | + // FlutterPlugin interface implementation. |
| 143 | + |
| 144 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { |
| 145 | + // Nothing to do because this plugin is instantiated by the engine. |
| 146 | + } |
| 147 | + |
| 148 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
| 149 | + // Nothing to do because this plugin is instantiated by the engine. |
| 150 | + } |
| 151 | + |
| 152 | + // ActivityAware interface implementation. |
| 153 | + // |
| 154 | + // Store the binding and manage the activity result listerner. |
| 155 | + |
| 156 | + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { |
| 157 | + this.activityBinding = binding; |
| 158 | + this.activityBinding.addActivityResultListener(this); |
| 159 | + }; |
| 160 | + |
| 161 | + public void onDetachedFromActivityForConfigChanges() { |
| 162 | + this.activityBinding.removeActivityResultListener(this); |
| 163 | + this.activityBinding = null; |
| 164 | + } |
| 165 | + |
| 166 | + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { |
| 167 | + this.activityBinding = binding; |
| 168 | + this.activityBinding.addActivityResultListener(this); |
| 169 | + } |
| 170 | + |
| 171 | + public void onDetachedFromActivity() { |
| 172 | + this.activityBinding.removeActivityResultListener(this); |
| 173 | + this.activityBinding = null; |
| 174 | + } |
| 175 | +} |
0 commit comments