Skip to content

Commit

Permalink
Merge pull request #183 from crazecoder/master
Browse files Browse the repository at this point in the history
handle choose file callback in android
  • Loading branch information
pichillilorenzo authored Nov 7, 2019
2 parents 31dc73c + e18e691 commit f80bf88
Showing 1 changed file with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pichillilorenzo.flutter_inappbrowser.InAppWebView;

import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -33,7 +35,10 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class InAppWebChromeClient extends WebChromeClient {
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

public class InAppWebChromeClient extends WebChromeClient implements PluginRegistry.ActivityResultListener {

protected static final String LOG_TAG = "IABWebChromeClient";
private PluginRegistry.Registrar registrar;
Expand All @@ -56,18 +61,18 @@ public InAppWebChromeClient(Object obj, PluginRegistry.Registrar registrar) {
this.inAppBrowserActivity = (InAppBrowserActivity) obj;
else if (obj instanceof FlutterWebView)
this.flutterWebView = (FlutterWebView) obj;

registrar.addActivityResultListener(this);
}

public Bitmap getDefaultVideoPoster()
{
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(this.registrar.activeContext().getResources(), 2130837573);
}

public void onHideCustomView()
{
public void onHideCustomView() {
View decorView = this.registrar.activity().getWindow().getDecorView();
((FrameLayout) decorView).removeView(this.mCustomView);
this.mCustomView = null;
Expand All @@ -77,10 +82,8 @@ public void onHideCustomView()
this.mCustomViewCallback = null;
}

public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
Expand Down Expand Up @@ -343,7 +346,7 @@ public void createPromptDialog(WebView view, String message, String defaultValue
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);

layout.setPaddingRelative(45,15,45,0);
layout.setPaddingRelative(45, 15, 45, 0);
layout.addView(input);

String alertMessage = (responseMessage != null && !responseMessage.isEmpty()) ? responseMessage : message;
Expand Down Expand Up @@ -392,8 +395,7 @@ public void onCancel(DialogInterface dialog) {
}

@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
{
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
Map<String, Object> obj = new HashMap<>();
Expand All @@ -405,7 +407,7 @@ public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture,
}

@Override
public void onGeolocationPermissionsShowPrompt (final String origin, final GeolocationPermissions.Callback callback) {
public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) {
Map<String, Object> obj = new HashMap<>();
if (inAppBrowserActivity != null)
obj.put("uuid", inAppBrowserActivity.uuid);
Expand All @@ -415,19 +417,19 @@ public void onGeolocationPermissionsShowPrompt (final String origin, final Geolo
public void success(Object o) {
Map<String, Object> response = (Map<String, Object>) o;
if (response != null)
callback.invoke((String) response.get("origin"),(Boolean) response.get("allow"),(Boolean) response.get("retain"));
callback.invoke((String) response.get("origin"), (Boolean) response.get("allow"), (Boolean) response.get("retain"));
else
callback.invoke(origin,false,false);
callback.invoke(origin, false, false);
}

@Override
public void error(String s, String s1, Object o) {
callback.invoke(origin,false,false);
callback.invoke(origin, false, false);
}

@Override
public void notImplemented() {
callback.invoke(origin,false,false);
callback.invoke(origin, false, false);
}
});
}
Expand Down Expand Up @@ -518,25 +520,35 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, Str
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (mUploadMessageArray != null) {
mUploadMessageArray.onReceiveValue(null);
}
mUploadMessageArray = filePathCallback;

Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
Intent[] intentArray;
intentArray = new Intent[0];

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
try {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
Intent[] intentArray;
intentArray = new Intent[0];

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
((inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity).startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_RESULTCODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
mUploadMessageArray.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
}
return false;
}

private MethodChannel getChannel() {
return (inAppBrowserActivity != null) ? InAppBrowserFlutterPlugin.inAppBrowser.channel : flutterWebView.channel;
}
Expand Down

0 comments on commit f80bf88

Please sign in to comment.