44
55package io .flutter .plugin .text ;
66
7+ import android .annotation .TargetApi ;
78import android .app .Activity ;
89import android .content .Intent ;
910import android .content .pm .PackageManager ;
1011import android .content .pm .ResolveInfo ;
1112import android .os .Build ;
1213import androidx .annotation .NonNull ;
1314import androidx .annotation .Nullable ;
15+ import androidx .annotation .RequiresApi ;
1416import io .flutter .embedding .engine .plugins .FlutterPlugin ;
1517import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
1618import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
2123import java .util .List ;
2224import java .util .Map ;
2325
24- public class ProcessTextPlugin implements FlutterPlugin , ActivityAware , ActivityResultListener {
26+ public class ProcessTextPlugin
27+ implements FlutterPlugin ,
28+ ActivityAware ,
29+ ActivityResultListener ,
30+ ProcessTextChannel .ProcessTextMethodHandler {
2531 private static final String TAG = "ProcessTextPlugin" ;
2632
2733 @ NonNull private final ProcessTextChannel processTextChannel ;
@@ -37,65 +43,63 @@ public ProcessTextPlugin(@NonNull ProcessTextChannel processTextChannel) {
3743 this .processTextChannel = processTextChannel ;
3844 this .packageManager = processTextChannel .packageManager ;
3945
40- processTextChannel .setMethodHandler (
41- new ProcessTextChannel .ProcessTextMethodHandler () {
42- @ Override
43- public Map <Integer , String > queryTextActions () {
44- if (resolveInfosById == null ) {
45- resolveInfosById = new HashMap <Integer , ResolveInfo >();
46- cacheResolveInfos ();
47- }
48- Map <Integer , String > result = new HashMap <Integer , String >();
49- for (Integer id : resolveInfosById .keySet ()) {
50- final ResolveInfo info = resolveInfosById .get (id );
51- result .put (id , info .loadLabel (packageManager ).toString ());
52- }
53- return result ;
54- }
55-
56- @ Override
57- public void processTextAction (
58- @ NonNull int id ,
59- @ NonNull String text ,
60- @ NonNull boolean readOnly ,
61- @ NonNull MethodChannel .Result result ) {
62- if (activityBinding == null ) {
63- result .error ("error" , "Plugin not bound to an Activity" , null );
64- return ;
65- }
66-
67- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
68- result .error ("error" , "Android version not supported" , null );
69- return ;
70- }
71-
72- if (resolveInfosById == null ) {
73- result .error ("error" , "Can not process text actions before calling queryTextActions" , null );
74- return ;
75- }
76-
77- final ResolveInfo info = resolveInfosById .get (id );
78- if (info == null ) {
79- result .error ("error" , "Text processing activity not found" , null );
80- return ;
81- }
82-
83- Integer requestCode = result .hashCode ();
84- requestsByCode .put (requestCode , result );
85-
86- Intent intent =
87- new Intent ()
88- .setClassName (info .activityInfo .packageName , info .activityInfo .name )
89- .setAction (Intent .ACTION_PROCESS_TEXT )
90- .setType ("text/plain" )
91- .putExtra (Intent .EXTRA_PROCESS_TEXT , text )
92- .putExtra (Intent .EXTRA_PROCESS_TEXT_READONLY , readOnly );
93-
94- // Start the text processing activity. onActivityResult callback is called
95- // when the activity completes.
96- activityBinding .getActivity ().startActivityForResult (intent , requestCode );
97- }
98- });
46+ processTextChannel .setMethodHandler (this );
47+ }
48+
49+ @ Override
50+ public Map <Integer , String > queryTextActions () {
51+ if (resolveInfosById == null ) {
52+ resolveInfosById = new HashMap <Integer , ResolveInfo >();
53+ cacheResolveInfos ();
54+ }
55+ Map <Integer , String > result = new HashMap <Integer , String >();
56+ for (Integer id : resolveInfosById .keySet ()) {
57+ final ResolveInfo info = resolveInfosById .get (id );
58+ result .put (id , info .loadLabel (packageManager ).toString ());
59+ }
60+ return result ;
61+ }
62+
63+ @ Override
64+ public void processTextAction (
65+ @ NonNull int id ,
66+ @ NonNull String text ,
67+ @ NonNull boolean readOnly ,
68+ @ NonNull MethodChannel .Result result ) {
69+ if (activityBinding == null ) {
70+ result .error ("error" , "Plugin not bound to an Activity" , null );
71+ return ;
72+ }
73+
74+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
75+ result .error ("error" , "Android version not supported" , null );
76+ return ;
77+ }
78+
79+ if (resolveInfosById == null ) {
80+ result .error ("error" , "Can not process text actions before calling queryTextActions" , null );
81+ return ;
82+ }
83+
84+ final ResolveInfo info = resolveInfosById .get (id );
85+ if (info == null ) {
86+ result .error ("error" , "Text processing activity not found" , null );
87+ return ;
88+ }
89+
90+ Integer requestCode = result .hashCode ();
91+ requestsByCode .put (requestCode , result );
92+
93+ Intent intent = new Intent ();
94+ intent .setClassName (info .activityInfo .packageName , info .activityInfo .name );
95+ intent .setAction (Intent .ACTION_PROCESS_TEXT );
96+ intent .setType ("text/plain" );
97+ intent .putExtra (Intent .EXTRA_PROCESS_TEXT , text );
98+ intent .putExtra (Intent .EXTRA_PROCESS_TEXT_READONLY , readOnly );
99+
100+ // Start the text processing activity. onActivityResult callback is called
101+ // when the activity completes.
102+ activityBinding .getActivity ().startActivityForResult (intent , requestCode );
99103 }
100104
101105 private void cacheResolveInfos () {
@@ -126,6 +130,8 @@ private void cacheResolveInfos() {
126130 *
127131 * <p>The result is null when an activity does not return an updated text.
128132 */
133+ @ TargetApi (Build .VERSION_CODES .M )
134+ @ RequiresApi (Build .VERSION_CODES .M )
129135 public boolean onActivityResult (int requestCode , int resultCode , @ Nullable Intent intent ) {
130136 String result = null ;
131137 if (resultCode == Activity .RESULT_OK ) {
0 commit comments