6
6
import android .app .Activity ;
7
7
import android .content .Context ;
8
8
import android .os .Build ;
9
+ import android .os .Handler ;
9
10
import android .view .KeyEvent ;
10
11
import android .view .View ;
11
12
import android .view .ViewGroup ;
17
18
import android .webkit .WebView ;
18
19
import android .widget .FrameLayout ;
19
20
import android .provider .MediaStore ;
21
+
20
22
import androidx .core .content .FileProvider ;
23
+
21
24
import android .database .Cursor ;
22
25
import android .provider .OpenableColumns ;
23
26
@@ -43,7 +46,7 @@ class WebviewManager {
43
46
44
47
private ValueCallback <Uri > mUploadMessage ;
45
48
private ValueCallback <Uri []> mUploadMessageArray ;
46
- private final static int FILECHOOSER_RESULTCODE = 1 ;
49
+ private final static int FILECHOOSER_RESULTCODE = 1 ;
47
50
private Uri fileUri ;
48
51
private Uri videoUri ;
49
52
@@ -56,29 +59,29 @@ private long getFileSize(Uri fileUri) {
56
59
57
60
@ TargetApi (7 )
58
61
class ResultHandler {
59
- public boolean handleResult (int requestCode , int resultCode , Intent intent ){
62
+ public boolean handleResult (int requestCode , int resultCode , Intent intent ) {
60
63
boolean handled = false ;
61
- if (Build .VERSION .SDK_INT >= 21 ){
62
- if (requestCode == FILECHOOSER_RESULTCODE ){
64
+ if (Build .VERSION .SDK_INT >= 21 ) {
65
+ if (requestCode == FILECHOOSER_RESULTCODE ) {
63
66
Uri [] results = null ;
64
67
if (resultCode == Activity .RESULT_OK ) {
65
68
if (fileUri != null && getFileSize (fileUri ) > 0 ) {
66
- results = new Uri [] { fileUri };
69
+ results = new Uri []{ fileUri };
67
70
} else if (videoUri != null && getFileSize (videoUri ) > 0 ) {
68
- results = new Uri [] { videoUri };
71
+ results = new Uri []{ videoUri };
69
72
} else if (intent != null ) {
70
73
results = getSelectedFiles (intent );
71
74
}
72
75
}
73
- if (mUploadMessageArray != null ){
76
+ if (mUploadMessageArray != null ) {
74
77
mUploadMessageArray .onReceiveValue (results );
75
78
mUploadMessageArray = null ;
76
79
}
77
80
handled = true ;
78
81
}
79
- }else {
82
+ } else {
80
83
if (requestCode == FILECHOOSER_RESULTCODE ) {
81
- Uri result = null ;
84
+ Uri result = null ;
82
85
if (resultCode == RESULT_OK && intent != null ) {
83
86
result = intent .getData ();
84
87
}
@@ -97,8 +100,8 @@ private Uri[] getSelectedFiles(Intent data) {
97
100
// we have one files selected
98
101
if (data .getData () != null ) {
99
102
String dataString = data .getDataString ();
100
- if (dataString != null ){
101
- return new Uri []{ Uri .parse (dataString ) };
103
+ if (dataString != null ) {
104
+ return new Uri []{Uri .parse (dataString )};
102
105
}
103
106
}
104
107
// we have multiple files selected
@@ -113,18 +116,20 @@ private Uri[] getSelectedFiles(Intent data) {
113
116
return null ;
114
117
}
115
118
119
+ private final Handler platformThreadHandler ;
116
120
boolean closed = false ;
117
121
WebView webView ;
118
122
Activity activity ;
119
123
BrowserClient webViewClient ;
120
124
ResultHandler resultHandler ;
121
125
Context context ;
122
126
123
- WebviewManager (final Activity activity , final Context context ) {
127
+ WebviewManager (final Activity activity , final Context context , final List < String > channelNames ) {
124
128
this .webView = new ObservableWebView (activity );
125
129
this .activity = activity ;
126
130
this .context = context ;
127
131
this .resultHandler = new ResultHandler ();
132
+ this .platformThreadHandler = new Handler (context .getMainLooper ());
128
133
webViewClient = new BrowserClient ();
129
134
webView .setOnKeyListener (new View .OnKeyListener () {
130
135
@ Override
@@ -145,20 +150,19 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
145
150
}
146
151
});
147
152
148
- ((ObservableWebView ) webView ).setOnScrollChangedCallback (new ObservableWebView .OnScrollChangedCallback (){
149
- public void onScroll (int x , int y , int oldx , int oldy ){
153
+ ((ObservableWebView ) webView ).setOnScrollChangedCallback (new ObservableWebView .OnScrollChangedCallback () {
154
+ public void onScroll (int x , int y , int oldx , int oldy ) {
150
155
Map <String , Object > yDirection = new HashMap <>();
151
- yDirection .put ("yDirection" , (double )y );
156
+ yDirection .put ("yDirection" , (double ) y );
152
157
FlutterWebviewPlugin .channel .invokeMethod ("onScrollYChanged" , yDirection );
153
158
Map <String , Object > xDirection = new HashMap <>();
154
- xDirection .put ("xDirection" , (double )x );
159
+ xDirection .put ("xDirection" , (double ) x );
155
160
FlutterWebviewPlugin .channel .invokeMethod ("onScrollXChanged" , xDirection );
156
161
}
157
162
});
158
163
159
164
webView .setWebViewClient (webViewClient );
160
- webView .setWebChromeClient (new WebChromeClient ()
161
- {
165
+ webView .setWebChromeClient (new WebChromeClient () {
162
166
//The undocumented magic method override
163
167
//Eclipse will swear at you if you try to put @Override here
164
168
// For Android 3.0+
@@ -168,36 +172,36 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg) {
168
172
Intent i = new Intent (Intent .ACTION_GET_CONTENT );
169
173
i .addCategory (Intent .CATEGORY_OPENABLE );
170
174
i .setType ("image/*" );
171
- activity .startActivityForResult (Intent .createChooser (i ,"File Chooser" ), FILECHOOSER_RESULTCODE );
175
+ activity .startActivityForResult (Intent .createChooser (i , "File Chooser" ), FILECHOOSER_RESULTCODE );
172
176
173
177
}
174
178
175
179
// For Android 3.0+
176
- public void openFileChooser ( ValueCallback uploadMsg , String acceptType ) {
180
+ public void openFileChooser (ValueCallback uploadMsg , String acceptType ) {
177
181
mUploadMessage = uploadMsg ;
178
182
Intent i = new Intent (Intent .ACTION_GET_CONTENT );
179
183
i .addCategory (Intent .CATEGORY_OPENABLE );
180
184
i .setType ("*/*" );
181
- activity .startActivityForResult (
185
+ activity .startActivityForResult (
182
186
Intent .createChooser (i , "File Browser" ),
183
187
FILECHOOSER_RESULTCODE );
184
188
}
185
189
186
190
//For Android 4.1
187
- public void openFileChooser (ValueCallback <Uri > uploadMsg , String acceptType , String capture ){
191
+ public void openFileChooser (ValueCallback <Uri > uploadMsg , String acceptType , String capture ) {
188
192
mUploadMessage = uploadMsg ;
189
193
Intent i = new Intent (Intent .ACTION_GET_CONTENT );
190
194
i .addCategory (Intent .CATEGORY_OPENABLE );
191
195
i .setType ("image/*" );
192
- activity .startActivityForResult ( Intent .createChooser ( i , "File Chooser" ), FILECHOOSER_RESULTCODE );
196
+ activity .startActivityForResult (Intent .createChooser (i , "File Chooser" ), FILECHOOSER_RESULTCODE );
193
197
194
198
}
195
199
196
200
//For Android 5.0+
197
201
public boolean onShowFileChooser (
198
202
WebView webView , ValueCallback <Uri []> filePathCallback ,
199
- FileChooserParams fileChooserParams ){
200
- if (mUploadMessageArray != null ){
203
+ FileChooserParams fileChooserParams ) {
204
+ if (mUploadMessageArray != null ) {
201
205
mUploadMessageArray .onReceiveValue (null );
202
206
}
203
207
mUploadMessageArray = filePathCallback ;
@@ -248,6 +252,7 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
248
252
callback .invoke (origin , true , false );
249
253
}
250
254
});
255
+ registerJavaScriptChannelNames (channelNames );
251
256
}
252
257
253
258
private Uri getOutputFilename (String intentType ) {
@@ -334,6 +339,13 @@ private void clearCache() {
334
339
webView .clearFormData ();
335
340
}
336
341
342
+ private void registerJavaScriptChannelNames (List <String > channelNames ) {
343
+ for (String channelName : channelNames ) {
344
+ webView .addJavascriptInterface (
345
+ new JavaScriptChannel (FlutterWebviewPlugin .channel , channelName , platformThreadHandler ), channelName );
346
+ }
347
+ }
348
+
337
349
void openUrl (
338
350
boolean withJavascript ,
339
351
boolean clearCache ,
@@ -371,7 +383,7 @@ void openUrl(
371
383
webView .getSettings ().setAllowUniversalAccessFromFileURLs (allowFileURLs );
372
384
373
385
webView .getSettings ().setUseWideViewPort (useWideViewPort );
374
-
386
+
375
387
// Handle debugging
376
388
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
377
389
webView .setWebContentsDebuggingEnabled (debuggingEnabled );
@@ -403,7 +415,7 @@ void openUrl(
403
415
webView .getSettings ().setUserAgentString (userAgent );
404
416
}
405
417
406
- if (!scrollBar ){
418
+ if (!scrollBar ) {
407
419
webView .setVerticalScrollBarEnabled (false );
408
420
}
409
421
@@ -451,25 +463,28 @@ public void onReceiveValue(String value) {
451
463
}
452
464
});
453
465
}
466
+
454
467
/**
455
- * Reloads the Webview.
456
- */
468
+ * Reloads the Webview.
469
+ */
457
470
void reload (MethodCall call , MethodChannel .Result result ) {
458
471
if (webView != null ) {
459
472
webView .reload ();
460
473
}
461
474
}
475
+
462
476
/**
463
- * Navigates back on the Webview.
464
- */
477
+ * Navigates back on the Webview.
478
+ */
465
479
void back (MethodCall call , MethodChannel .Result result ) {
466
480
if (webView != null && webView .canGoBack ()) {
467
481
webView .goBack ();
468
482
}
469
483
}
484
+
470
485
/**
471
- * Navigates forward on the Webview.
472
- */
486
+ * Navigates forward on the Webview.
487
+ */
473
488
void forward (MethodCall call , MethodChannel .Result result ) {
474
489
if (webView != null && webView .canGoForward ()) {
475
490
webView .goForward ();
@@ -479,31 +494,35 @@ void forward(MethodCall call, MethodChannel.Result result) {
479
494
void resize (FrameLayout .LayoutParams params ) {
480
495
webView .setLayoutParams (params );
481
496
}
497
+
482
498
/**
483
- * Checks if going back on the Webview is possible.
484
- */
499
+ * Checks if going back on the Webview is possible.
500
+ */
485
501
boolean canGoBack () {
486
502
return webView .canGoBack ();
487
503
}
504
+
488
505
/**
489
- * Checks if going forward on the Webview is possible.
490
- */
506
+ * Checks if going forward on the Webview is possible.
507
+ */
491
508
boolean canGoForward () {
492
509
return webView .canGoForward ();
493
510
}
511
+
494
512
void hide (MethodCall call , MethodChannel .Result result ) {
495
513
if (webView != null ) {
496
514
webView .setVisibility (View .GONE );
497
515
}
498
516
}
517
+
499
518
void show (MethodCall call , MethodChannel .Result result ) {
500
519
if (webView != null ) {
501
520
webView .setVisibility (View .VISIBLE );
502
521
}
503
522
}
504
523
505
- void stopLoading (MethodCall call , MethodChannel .Result result ){
506
- if (webView != null ){
524
+ void stopLoading (MethodCall call , MethodChannel .Result result ) {
525
+ if (webView != null ) {
507
526
webView .stopLoading ();
508
527
}
509
528
}
0 commit comments