Skip to content

Commit

Permalink
fix #98, fix #93, fix #80
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed May 22, 2019
1 parent 76ffe41 commit da99de0
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 169 deletions.
217 changes: 120 additions & 97 deletions .idea/workspace.xml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.2.0

- Merge "Adds a transparentBackground option for iOS and Android" [#86](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/86) (thanks to [matthewlloyd](https://github.com/matthewlloyd))
- Merge "The 'open' method requires an options dictionary" [#87](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/87) (thanks to [matthewlloyd](https://github.com/matthewlloyd))
- Merge "iOS: Call setNeedsLayout() in scrollViewDidScroll()" [#88](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/88) (thanks to [matthewlloyd](https://github.com/matthewlloyd))
- Fixed "java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread." [#98](https://github.com/pichillilorenzo/flutter_inappbrowser/issues/98) (thanks to [DreamBuddy](https://github.com/DreamBuddy))
- Fixed "app force close/crash when enabling zoom and repeatedly changing orientation and zoomin zoomout" [#93](https://github.com/pichillilorenzo/flutter_inappbrowser/issues/93)
- Added `displayZoomControls` webview option for Android
- Fixed "Compatibility with other plugins" [#80](https://github.com/pichillilorenzo/flutter_inappbrowser/issues/80)

## 1.1.3

- Merge "Add null checks around calls to InAppWebView callbacks" [#85](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/85) (thanks to [matthewlloyd](https://github.com/matthewlloyd))
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ All platforms support:

- __clearSessionCache__: Set to `true` to have the session cookie cache cleared before the new window is opened.
- __builtInZoomControls__: Set to `true` if the WebView should use its built-in zoom mechanisms. The default value is `false`.
- __displayZoomControls__: Set to `true` if the WebView should display on-screen zoom controls when using the built-in zoom mechanisms. The default value is `false`.
- __supportZoom__: Set to `false` if the WebView should not support zooming using its on-screen zoom controls and gestures. The default value is `true`.
- __databaseEnabled__: Set to `true` if you want the database storage API is enabled. The default value is `false`.
- __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
Expand Down Expand Up @@ -819,6 +820,7 @@ Opens an `url` in a new `InAppBrowser` instance.
- __closeOnCannotGoBack__: Set to `false` to not close the InAppBrowser when the user click on the back button and the WebView cannot go back to the history. The default value is `true`.
- __clearSessionCache__: Set to `true` to have the session cookie cache cleared before the new window is opened.
- __builtInZoomControls__: Set to `true` if the WebView should use its built-in zoom mechanisms. The default value is `false`.
- __displayZoomControls__: Set to `true` if the WebView should display on-screen zoom controls when using the built-in zoom mechanisms. The default value is `false`.
- __supportZoom__: Set to `false` if the WebView should not support zooming using its on-screen zoom controls and gestures. The default value is `true`.
- __databaseEnabled__: Set to `true` if you want the database storage API is enabled. The default value is `false`.
- __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
public InAppWebView webView;
public MethodChannel channel;
public final Registrar registrar;
public final Context context;

public FlutterWebView(Registrar registrar, int id, HashMap<String, Object> params) {

this.registrar = registrar;
this.activity = registrar.activity();
this.context = registrar.context();

String initialUrl = (String) params.get("initialUrl");
String initialFile = (String) params.get("initialFile");
Expand All @@ -47,7 +45,7 @@ public FlutterWebView(Registrar registrar, int id, HashMap<String, Object> param
InAppWebViewOptions options = new InAppWebViewOptions();
options.parse(initialOptions);

webView = new InAppWebView(context, this, id, options);
webView = new InAppWebView(registrar, this, id, options);
webView.prepare();

channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappwebview_" + id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package com.pichillilorenzo.flutter_inappbrowser;

import android.app.Activity;
import android.content.Context;

import java.util.HashMap;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class FlutterWebViewFactory extends PlatformViewFactory {
private final BinaryMessenger messenger;
private final Registrar registrar;
private final Activity activity;

public FlutterWebViewFactory(Registrar registrar, Activity activity) {
public FlutterWebViewFactory(Registrar registrar) {
super(StandardMessageCodec.INSTANCE);
this.registrar = registrar;
this.messenger = registrar.messenger();
this.activity = activity;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pichillilorenzo.flutter_inappbrowser;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand All @@ -9,6 +10,7 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -25,6 +27,8 @@
import java.util.HashMap;
import java.util.Map;

import io.flutter.app.FlutterActivity;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.MethodChannel;

public class InAppBrowserActivity extends AppCompatActivity {
Expand All @@ -39,6 +43,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
public Map<String, String> headers;
public ProgressBar progressBar;
public boolean isHidden = false;
public String fromActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -51,6 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {

Bundle b = getIntent().getExtras();
uuid = b.getString("uuid");
fromActivity = b.getString("fromActivity");

HashMap<String, Object> optionsMap = (HashMap<String, Object>) b.getSerializable("options");

Expand Down Expand Up @@ -247,7 +253,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
if (canGoBack())
goBack();
else if (options.closeOnCannotGoBack)
InAppBrowserFlutterPlugin.close(uuid, null);
InAppBrowserFlutterPlugin.close(this, uuid, null);
return true;
}
return super.onKeyDown(keyCode, event);
Expand Down Expand Up @@ -297,15 +303,20 @@ public boolean canGoBackOrForward(int steps) {
}

public void hide() {
isHidden = true;
Intent openActivity = new Intent(this, InAppBrowserFlutterPlugin.registrar.activity().getClass());
openActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openActivity, 0);
try {
isHidden = true;
Intent openActivity = new Intent(this, Class.forName(fromActivity));
openActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openActivity, 0);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.d(LOG_TAG, e.getMessage());
}
}

public void show() {
isHidden = false;
Intent openActivity = new Intent(InAppBrowserFlutterPlugin.registrar.activity(), InAppBrowserActivity.class);
Intent openActivity = new Intent(this, InAppBrowserActivity.class);
openActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openActivity, 0);
}
Expand Down Expand Up @@ -341,7 +352,7 @@ public void reloadButtonClicked(MenuItem item) {
}

public void closeButtonClicked(MenuItem item) {
InAppBrowserFlutterPlugin.close(uuid, null);
InAppBrowserFlutterPlugin.close(this, uuid, null);
}

public byte[] takeScreenshot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.pichillilorenzo.flutter_inappbrowser;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
Expand All @@ -39,13 +40,12 @@

import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand All @@ -57,17 +57,15 @@
*/
public class InAppBrowserFlutterPlugin implements MethodCallHandler {

public static Registrar registrar;
public Activity activity;
public Registrar registrar;
public static MethodChannel channel;
public static Map<String, InAppBrowserActivity> webViewActivities = new HashMap<>();
public static Map<String, ChromeCustomTabsActivity> chromeCustomTabsActivities = new HashMap<>();

protected static final String LOG_TAG = "IABFlutterPlugin";

public InAppBrowserFlutterPlugin(Registrar r, Activity activity) {
public InAppBrowserFlutterPlugin(Registrar r) {
registrar = r;
this.activity = activity;
channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser");
}

Expand All @@ -76,23 +74,26 @@ public InAppBrowserFlutterPlugin(Registrar r, Activity activity) {
*/
public static void registerWith(Registrar registrar) {
Activity activity = registrar.activity();

final MethodChannel channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser");
channel.setMethodCallHandler(new InAppBrowserFlutterPlugin(registrar, activity));

new MyCookieManager(registrar);

registrar
.platformViewRegistry()
.registerViewFactory(
"com.pichillilorenzo/flutter_inappwebview", new FlutterWebViewFactory(registrar, activity));
// registrar.activity() may return null because of Flutter's background execution feature
// described here: https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
if (activity != null) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "com.pichillilorenzo/flutter_inappbrowser");
channel.setMethodCallHandler(new InAppBrowserFlutterPlugin(registrar));

new MyCookieManager(registrar);

registrar
.platformViewRegistry()
.registerViewFactory(
"com.pichillilorenzo/flutter_inappwebview", new FlutterWebViewFactory(registrar));
}
}

@Override
public void onMethodCall(final MethodCall call, final Result result) {
String source;
String jsWrapper;
String urlFile;
final Activity activity = registrar.activity();
final String uuid = (String) call.argument("uuid");

switch (call.method) {
Expand All @@ -107,7 +108,7 @@ public void onMethodCall(final MethodCall call, final Result result) {

Log.d(LOG_TAG, "use Chrome Custom Tabs = " + useChromeSafariBrowser);

this.activity.runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {

Expand All @@ -119,7 +120,7 @@ public void run() {

final HashMap<String, Object> optionsFallback = (HashMap<String, Object>) call.argument("optionsFallback");

open(uuid, uuidFallback, url_final, options, headers, true, optionsFallback, result);
open(activity, uuid, uuidFallback, url_final, options, headers, true, optionsFallback, result);
} else {

String url = url_final;
Expand All @@ -142,7 +143,7 @@ public void run() {
// SYSTEM
if (openWithSystemBrowser) {
Log.d(LOG_TAG, "in system");
openExternal(url, result);
openExternal(activity, url, result);
} else {
//Load the dialer
if (url.startsWith(WebView.SCHEME_TEL)) {
Expand All @@ -158,23 +159,23 @@ public void run() {
// load in InAppBrowserFlutterPlugin
else {
Log.d(LOG_TAG, "loading in InAppBrowserFlutterPlugin");
open(uuid, null, url, options, headers, false, null, result);
open(activity, uuid, null, url, options, headers, false, null, result);
}
}
}
}
});
}
else {
this.activity.runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
HashMap<String, Object> options = (HashMap<String, Object>) call.argument("options");
String data = call.argument("data").toString();
String mimeType = call.argument("mimeType").toString();
String encoding = call.argument("encoding").toString();
String baseUrl = call.argument("baseUrl").toString();
openData(uuid, options, data, mimeType, encoding, baseUrl);
openData(activity, uuid, options, data, mimeType, encoding, baseUrl);
result.success(true);
}
});
Expand Down Expand Up @@ -208,7 +209,7 @@ public void run() {
loadFile(uuid, call.argument("url").toString(), (Map<String, String>) call.argument("headers"), result);
break;
case "close":
close(uuid, result);
close(activity, uuid, result);
break;
case "injectScriptCode":
source = call.argument("source").toString();
Expand Down Expand Up @@ -349,7 +350,7 @@ public static String getMimeType(String url) {
* @param result
* @return "" if ok, or error message.
*/
public void openExternal(String url, Result result) {
public void openExternal(Activity activity, String url, Result result) {
try {
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
Expand All @@ -363,7 +364,7 @@ public void openExternal(String url, Result result) {
}
intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
// CB-10795: Avoid circular loops by preventing it from opening in the current app
this.openExternalExcludeCurrentApp(intent);
this.openExternalExcludeCurrentApp(activity, intent);
result.success(true);
// not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
} catch (java.lang.RuntimeException e) {
Expand All @@ -376,7 +377,7 @@ public void openExternal(String url, Result result) {
* Opens the intent, providing a chooser that excludes the current app to avoid
* circular loops.
*/
private void openExternalExcludeCurrentApp(Intent intent) {
private void openExternalExcludeCurrentApp(Activity activity, Intent intent) {
String currentPackage = activity.getPackageName();
boolean hasCurrentPackage = false;
PackageManager pm = activity.getPackageManager();
Expand Down Expand Up @@ -408,10 +409,11 @@ else if (targetIntents.size() > 0) {
}
}

public void open(String uuid, String uuidFallback, String url, HashMap<String, Object> options, Map<String, String> headers, boolean useChromeSafariBrowser, HashMap<String, Object> optionsFallback, Result result) {
public void open(Activity activity, String uuid, String uuidFallback, String url, HashMap<String, Object> options, Map<String, String> headers, boolean useChromeSafariBrowser, HashMap<String, Object> optionsFallback, Result result) {

Intent intent = null;
Bundle extras = new Bundle();
extras.putString("fromActivity", activity.getClass().getName());
extras.putString("url", url);
extras.putBoolean("isData", false);
extras.putString("uuid", uuid);
Expand Down Expand Up @@ -451,7 +453,7 @@ else if (!useChromeSafariBrowser) {
result.error(LOG_TAG, "No WebView fallback declared.", null);
}

public void openData(String uuid, HashMap<String, Object> options, String data, String mimeType, String encoding, String baseUrl) {
public void openData(Activity activity, String uuid, HashMap<String, Object> options, String data, String mimeType, String encoding, String baseUrl) {
Intent intent = new Intent(activity, InAppBrowserActivity.class);
Bundle extras = new Bundle();

Expand Down Expand Up @@ -597,10 +599,10 @@ public boolean canGoBackOrForward(String uuid, int steps) {
return false;
}

public static void close(final String uuid, final Result result) {
public static void close(Activity activity, final String uuid, final Result result) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
registrar.activity().runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {

Expand Down
Loading

0 comments on commit da99de0

Please sign in to comment.