Skip to content

Commit

Permalink
fix #1155
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Apr 26, 2022
1 parent f885a05 commit 442b51d
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.4.2+1

- Fixed "Latest version 5.4.2 crashes on Android - HeadlessInAppWebView.dispose" [#1155](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1155)

## 5.4.2

- Added `setActionButton` method to `ChromeSafariBrowser` class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.Build;
import android.webkit.ValueCallback;

import androidx.annotation.Nullable;

import com.pichillilorenzo.flutter_inappwebview.chrome_custom_tabs.ChromeSafariBrowserManager;
import com.pichillilorenzo.flutter_inappwebview.credential_database.CredentialDatabaseHandler;
import com.pichillilorenzo.flutter_inappwebview.in_app_browser.InAppBrowserManager;
Expand Down Expand Up @@ -41,7 +43,9 @@ public class InAppWebViewFlutterPlugin implements FlutterPlugin, ActivityAware {
public PluginRegistry.Registrar registrar;
public BinaryMessenger messenger;
public FlutterPlugin.FlutterAssets flutterAssets;
@Nullable
public ActivityPluginBinding activityPluginBinding;
@Nullable
public Activity activity;
@SuppressWarnings("deprecation")
public FlutterView flutterView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void onReceiveValue(Boolean value) {
result.success(false);
break;
case "getCurrentWebViewPackage":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && plugin != null && plugin.activity != null) {
result.success(convertWebViewPackageToMap(WebViewCompat.getCurrentWebViewPackage(plugin.activity)));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//with Android Lollipop (API 21) they started to update the WebView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ChromeCustomTabsActivity extends Activity implements MethodChannel.
protected final int CHROME_CUSTOM_TAB_REQUEST_CODE = 100;
protected boolean onChromeSafariBrowserOpened = false;
protected boolean onChromeSafariBrowserCompletedInitialLoad = false;
@Nullable
public ChromeSafariBrowserManager manager;
public String initialUrl;
public List<CustomTabsMenuItem> menuItems = new ArrayList<>();
Expand Down Expand Up @@ -137,11 +138,13 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result resul
this.onDestroy();
this.close();

// https://stackoverflow.com/a/41596629/4637638
Intent myIntent = new Intent(manager.plugin.activity, manager.plugin.activity.getClass());
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
manager.plugin.activity.startActivity(myIntent);
if (manager != null && manager.plugin != null && manager.plugin.activity != null) {
// https://stackoverflow.com/a/41596629/4637638
Intent myIntent = new Intent(manager.plugin.activity, manager.plugin.activity.getClass());
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
manager.plugin.activity.startActivity(myIntent);
}

dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result resul

switch (call.method) {
case "open":
if (plugin != null) {
if (plugin != null && plugin.activity != null) {
String url = (String) call.argument("url");
HashMap<String, Object> options = (HashMap<String, Object>) call.argument("options");
HashMap<String, Object> actionButton = (HashMap<String, Object>) call.argument("actionButton");
Expand All @@ -52,7 +52,7 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result resul
}
break;
case "isAvailable":
if (plugin != null) {
if (plugin != null && plugin.activity != null) {
result.success(CustomTabActivityHelper.isAvailable(plugin.activity));
} else {
result.success(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ public void onWebViewCreated() {
}

public void prepare(Map<String, Object> params) {
// Add the headless WebView to the view hierarchy.
// This way is also possible to take screenshots.
ViewGroup contentView = (ViewGroup) plugin.activity.findViewById(android.R.id.content);
ViewGroup mainView = (ViewGroup) (contentView).getChildAt(0);
if (mainView != null) {
View view = flutterWebView.getView();
final Map<String, Object> initialSize = (Map<String, Object>) params.get("initialSize");
Size2D size = Size2D.fromMap(initialSize);
if (size != null) {
setSize(size);
} else {
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
if (plugin != null && plugin.activity != null) {
// Add the headless WebView to the view hierarchy.
// This way is also possible to take screenshots.
ViewGroup contentView = (ViewGroup) plugin.activity.findViewById(android.R.id.content);
ViewGroup mainView = (ViewGroup) (contentView).getChildAt(0);
if (mainView != null && flutterWebView != null) {
View view = flutterWebView.getView();
final Map<String, Object> initialSize = (Map<String, Object>) params.get("initialSize");
Size2D size = Size2D.fromMap(initialSize);
if (size != null) {
setSize(size);
} else {
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
mainView.addView(view, 0);
view.setVisibility(View.INVISIBLE);
}
mainView.addView(view, 0);
view.setVisibility(View.INVISIBLE);
}
}

Expand All @@ -110,7 +112,7 @@ public Size2D getSize() {
public void dispose() {
channel.setMethodCallHandler(null);
HeadlessInAppWebViewManager.webViews.remove(id);
if (plugin != null) {
if (plugin != null && plugin.activity != null) {
ViewGroup contentView = (ViewGroup) plugin.activity.findViewById(android.R.id.content);
ViewGroup mainView = (ViewGroup) (contentView).getChildAt(0);
if (mainView != null && flutterWebView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ public InAppBrowserManager(final InAppWebViewFlutterPlugin plugin) {
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
switch (call.method) {
case "open":
open(plugin.activity, (Map<String, Object>) call.arguments());
result.success(true);
if (plugin != null && plugin.activity != null) {
open(plugin.activity, (Map<String, Object>) call.arguments());
result.success(true);
} else {
result.success(false);
}
break;
case "openWithSystemBrowser":
{
if (plugin != null && plugin.activity != null) {
String url = (String) call.argument("url");
openWithSystemBrowser(plugin.activity, url, result);
} else {
result.success(false);
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public FlutterWebView(final InAppWebViewFlutterPlugin plugin, final Context cont
InAppWebViewOptions options = new InAppWebViewOptions();
options.parse(initialOptions);

if (plugin.activity == null) {
if (plugin == null || plugin.activity == null) {
Log.e(LOG_TAG, "\n\n\nERROR: You need to upgrade your Flutter project to use the new Java Embedding API:\n\n" +
"- Take a look at the \"IMPORTANT Note for Android\" section here: https://github.com/pichillilorenzo/flutter_inappwebview#important-note-for-android\n" +
"- See the official wiki here: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects\n\n\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public InAppWebView(Context context, InAppWebViewFlutterPlugin plugin,
this.options = options;
this.contextMenu = contextMenu;
this.userContentController.addUserOnlyScripts(userScripts);
plugin.activity.registerForContextMenu(this);
if (plugin != null && plugin.activity != null) {
plugin.activity.registerForContextMenu(this);
}
}

public void prepare() {
Expand Down Expand Up @@ -1216,20 +1218,22 @@ public void setDesktopMode(final boolean enabled) {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void printCurrentPage() {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) plugin.activity.getSystemService(Context.PRINT_SERVICE);
if (plugin != null && plugin.activity != null) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) plugin.activity.getSystemService(Context.PRINT_SERVICE);

if (printManager != null) {
String jobName = getTitle() + " Document";
if (printManager != null) {
String jobName = getTitle() + " Document";

// Get a printCurrentPage adapter instance
PrintDocumentAdapter printAdapter = createPrintDocumentAdapter(jobName);
// Get a printCurrentPage adapter instance
PrintDocumentAdapter printAdapter = createPrintDocumentAdapter(jobName);

// Create a printCurrentPage job with name and adapter instance
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
} else {
Log.e(LOG_TAG, "No PrintManager available");
// Create a printCurrentPage job with name and adapter instance
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
} else {
Log.e(LOG_TAG, "No PrintManager available");
}
}
}

Expand Down
Loading

0 comments on commit 442b51d

Please sign in to comment.