|
|
@@ -6,6 +6,7 @@ import android.annotation.TargetApi;
|
|
|
import android.app.Activity;
|
|
|
import android.content.Context;
|
|
|
import android.os.Build;
|
|
|
+import android.os.Handler;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
@@ -17,7 +18,9 @@ import android.webkit.WebSettings;
|
|
|
import android.webkit.WebView;
|
|
|
import android.widget.FrameLayout;
|
|
|
import android.provider.MediaStore;
|
|
|
+
|
|
|
import androidx.core.content.FileProvider;
|
|
|
+
|
|
|
import android.database.Cursor;
|
|
|
import android.provider.OpenableColumns;
|
|
|
|
|
|
@@ -43,7 +46,7 @@ class WebviewManager {
|
|
|
|
|
|
private ValueCallback<Uri> mUploadMessage;
|
|
|
private ValueCallback<Uri[]> mUploadMessageArray;
|
|
|
- private final static int FILECHOOSER_RESULTCODE=1;
|
|
|
+ private final static int FILECHOOSER_RESULTCODE = 1;
|
|
|
private Uri fileUri;
|
|
|
private Uri videoUri;
|
|
|
|
|
|
@@ -56,29 +59,29 @@ class WebviewManager {
|
|
|
|
|
|
@TargetApi(7)
|
|
|
class ResultHandler {
|
|
|
- public boolean handleResult(int requestCode, int resultCode, Intent intent){
|
|
|
+ public boolean handleResult(int requestCode, int resultCode, Intent intent) {
|
|
|
boolean handled = false;
|
|
|
- if(Build.VERSION.SDK_INT >= 21){
|
|
|
- if(requestCode == FILECHOOSER_RESULTCODE){
|
|
|
+ if (Build.VERSION.SDK_INT >= 21) {
|
|
|
+ if (requestCode == FILECHOOSER_RESULTCODE) {
|
|
|
Uri[] results = null;
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
if (fileUri != null && getFileSize(fileUri) > 0) {
|
|
|
- results = new Uri[] { fileUri };
|
|
|
+ results = new Uri[]{fileUri};
|
|
|
} else if (videoUri != null && getFileSize(videoUri) > 0) {
|
|
|
- results = new Uri[] { videoUri };
|
|
|
+ results = new Uri[]{videoUri};
|
|
|
} else if (intent != null) {
|
|
|
results = getSelectedFiles(intent);
|
|
|
}
|
|
|
}
|
|
|
- if(mUploadMessageArray != null){
|
|
|
+ if (mUploadMessageArray != null) {
|
|
|
mUploadMessageArray.onReceiveValue(results);
|
|
|
mUploadMessageArray = null;
|
|
|
}
|
|
|
handled = true;
|
|
|
}
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
if (requestCode == FILECHOOSER_RESULTCODE) {
|
|
|
- Uri result = null;
|
|
|
+ Uri result = null;
|
|
|
if (resultCode == RESULT_OK && intent != null) {
|
|
|
result = intent.getData();
|
|
|
}
|
|
|
@@ -97,8 +100,8 @@ class WebviewManager {
|
|
|
// we have one files selected
|
|
|
if (data.getData() != null) {
|
|
|
String dataString = data.getDataString();
|
|
|
- if(dataString != null){
|
|
|
- return new Uri[]{ Uri.parse(dataString) };
|
|
|
+ if (dataString != null) {
|
|
|
+ return new Uri[]{Uri.parse(dataString)};
|
|
|
}
|
|
|
}
|
|
|
// we have multiple files selected
|
|
|
@@ -113,6 +116,7 @@ class WebviewManager {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private final Handler platformThreadHandler;
|
|
|
boolean closed = false;
|
|
|
WebView webView;
|
|
|
Activity activity;
|
|
|
@@ -120,11 +124,12 @@ class WebviewManager {
|
|
|
ResultHandler resultHandler;
|
|
|
Context context;
|
|
|
|
|
|
- WebviewManager(final Activity activity, final Context context) {
|
|
|
+ WebviewManager(final Activity activity, final Context context, final List<String> channelNames) {
|
|
|
this.webView = new ObservableWebView(activity);
|
|
|
this.activity = activity;
|
|
|
this.context = context;
|
|
|
this.resultHandler = new ResultHandler();
|
|
|
+ this.platformThreadHandler = new Handler(context.getMainLooper());
|
|
|
webViewClient = new BrowserClient();
|
|
|
webView.setOnKeyListener(new View.OnKeyListener() {
|
|
|
@Override
|
|
|
@@ -145,20 +150,19 @@ class WebviewManager {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- ((ObservableWebView) webView).setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback(){
|
|
|
- public void onScroll(int x, int y, int oldx, int oldy){
|
|
|
+ ((ObservableWebView) webView).setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
|
|
|
+ public void onScroll(int x, int y, int oldx, int oldy) {
|
|
|
Map<String, Object> yDirection = new HashMap<>();
|
|
|
- yDirection.put("yDirection", (double)y);
|
|
|
+ yDirection.put("yDirection", (double) y);
|
|
|
FlutterWebviewPlugin.channel.invokeMethod("onScrollYChanged", yDirection);
|
|
|
Map<String, Object> xDirection = new HashMap<>();
|
|
|
- xDirection.put("xDirection", (double)x);
|
|
|
+ xDirection.put("xDirection", (double) x);
|
|
|
FlutterWebviewPlugin.channel.invokeMethod("onScrollXChanged", xDirection);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
webView.setWebViewClient(webViewClient);
|
|
|
- webView.setWebChromeClient(new WebChromeClient()
|
|
|
- {
|
|
|
+ webView.setWebChromeClient(new WebChromeClient() {
|
|
|
//The undocumented magic method override
|
|
|
//Eclipse will swear at you if you try to put @Override here
|
|
|
// For Android 3.0+
|
|
|
@@ -168,36 +172,36 @@ class WebviewManager {
|
|
|
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
i.setType("image/*");
|
|
|
- activity.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
|
|
|
+ activity.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
|
|
|
|
|
|
}
|
|
|
|
|
|
// For Android 3.0+
|
|
|
- public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
|
|
|
+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
|
|
|
mUploadMessage = uploadMsg;
|
|
|
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
i.setType("*/*");
|
|
|
- activity.startActivityForResult(
|
|
|
+ activity.startActivityForResult(
|
|
|
Intent.createChooser(i, "File Browser"),
|
|
|
FILECHOOSER_RESULTCODE);
|
|
|
}
|
|
|
|
|
|
//For Android 4.1
|
|
|
- public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
|
|
|
+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
|
|
|
mUploadMessage = uploadMsg;
|
|
|
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
i.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
i.setType("image/*");
|
|
|
- activity.startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
|
|
|
+ activity.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
|
|
|
|
|
|
}
|
|
|
|
|
|
//For Android 5.0+
|
|
|
public boolean onShowFileChooser(
|
|
|
WebView webView, ValueCallback<Uri[]> filePathCallback,
|
|
|
- FileChooserParams fileChooserParams){
|
|
|
- if(mUploadMessageArray != null){
|
|
|
+ FileChooserParams fileChooserParams) {
|
|
|
+ if (mUploadMessageArray != null) {
|
|
|
mUploadMessageArray.onReceiveValue(null);
|
|
|
}
|
|
|
mUploadMessageArray = filePathCallback;
|
|
|
@@ -248,6 +252,7 @@ class WebviewManager {
|
|
|
callback.invoke(origin, true, false);
|
|
|
}
|
|
|
});
|
|
|
+ registerJavaScriptChannelNames(channelNames);
|
|
|
}
|
|
|
|
|
|
private Uri getOutputFilename(String intentType) {
|
|
|
@@ -334,6 +339,13 @@ class WebviewManager {
|
|
|
webView.clearFormData();
|
|
|
}
|
|
|
|
|
|
+ private void registerJavaScriptChannelNames(List<String> channelNames) {
|
|
|
+ for (String channelName : channelNames) {
|
|
|
+ webView.addJavascriptInterface(
|
|
|
+ new JavaScriptChannel(FlutterWebviewPlugin.channel, channelName, platformThreadHandler), channelName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void openUrl(
|
|
|
boolean withJavascript,
|
|
|
boolean clearCache,
|
|
|
@@ -371,7 +383,7 @@ class WebviewManager {
|
|
|
webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs);
|
|
|
|
|
|
webView.getSettings().setUseWideViewPort(useWideViewPort);
|
|
|
-
|
|
|
+
|
|
|
// Handle debugging
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
webView.setWebContentsDebuggingEnabled(debuggingEnabled);
|
|
|
@@ -403,7 +415,7 @@ class WebviewManager {
|
|
|
webView.getSettings().setUserAgentString(userAgent);
|
|
|
}
|
|
|
|
|
|
- if(!scrollBar){
|
|
|
+ if (!scrollBar) {
|
|
|
webView.setVerticalScrollBarEnabled(false);
|
|
|
}
|
|
|
|
|
|
@@ -451,25 +463,28 @@ class WebviewManager {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Reloads the Webview.
|
|
|
- */
|
|
|
+ * Reloads the Webview.
|
|
|
+ */
|
|
|
void reload(MethodCall call, MethodChannel.Result result) {
|
|
|
if (webView != null) {
|
|
|
webView.reload();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Navigates back on the Webview.
|
|
|
- */
|
|
|
+ * Navigates back on the Webview.
|
|
|
+ */
|
|
|
void back(MethodCall call, MethodChannel.Result result) {
|
|
|
if (webView != null && webView.canGoBack()) {
|
|
|
webView.goBack();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Navigates forward on the Webview.
|
|
|
- */
|
|
|
+ * Navigates forward on the Webview.
|
|
|
+ */
|
|
|
void forward(MethodCall call, MethodChannel.Result result) {
|
|
|
if (webView != null && webView.canGoForward()) {
|
|
|
webView.goForward();
|
|
|
@@ -479,31 +494,35 @@ class WebviewManager {
|
|
|
void resize(FrameLayout.LayoutParams params) {
|
|
|
webView.setLayoutParams(params);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Checks if going back on the Webview is possible.
|
|
|
- */
|
|
|
+ * Checks if going back on the Webview is possible.
|
|
|
+ */
|
|
|
boolean canGoBack() {
|
|
|
return webView.canGoBack();
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Checks if going forward on the Webview is possible.
|
|
|
- */
|
|
|
+ * Checks if going forward on the Webview is possible.
|
|
|
+ */
|
|
|
boolean canGoForward() {
|
|
|
return webView.canGoForward();
|
|
|
}
|
|
|
+
|
|
|
void hide(MethodCall call, MethodChannel.Result result) {
|
|
|
if (webView != null) {
|
|
|
webView.setVisibility(View.GONE);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
void show(MethodCall call, MethodChannel.Result result) {
|
|
|
if (webView != null) {
|
|
|
webView.setVisibility(View.VISIBLE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void stopLoading(MethodCall call, MethodChannel.Result result){
|
|
|
- if (webView != null){
|
|
|
+ void stopLoading(MethodCall call, MethodChannel.Result result) {
|
|
|
+ if (webView != null) {
|
|
|
webView.stopLoading();
|
|
|
}
|
|
|
}
|