From e2cdf3538b9741c5ec394abb14516ceaf5e12d43 Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Mon, 22 Jan 2024 08:14:34 -0800 Subject: [PATCH] feat(browser): implement close() method for android --- browser/android/src/main/AndroidManifest.xml | 6 +++ .../browser/BrowserControllerActivity.java | 52 +++++++++++++++++++ .../browser/BrowserControllerListener.java | 5 ++ .../plugins/browser/BrowserPlugin.java | 34 ++++++++++-- 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerActivity.java create mode 100644 browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerListener.java diff --git a/browser/android/src/main/AndroidManifest.xml b/browser/android/src/main/AndroidManifest.xml index 03dc0fd66..548dace96 100644 --- a/browser/android/src/main/AndroidManifest.xml +++ b/browser/android/src/main/AndroidManifest.xml @@ -1,5 +1,11 @@ + + + diff --git a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerActivity.java b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerActivity.java new file mode 100644 index 000000000..74fd599c1 --- /dev/null +++ b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerActivity.java @@ -0,0 +1,52 @@ +package com.capacitorjs.plugins.browser; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import androidx.annotation.Nullable; + +public class BrowserControllerActivity extends Activity { + + private boolean isCustomTabsOpen = false; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + isCustomTabsOpen = false; + + if (BrowserPlugin.browserControllerListener != null) { + BrowserPlugin.browserControllerListener.onControllerReady(this); + } + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + if (intent.hasExtra("close")) { + finish(); + } + } + + @Override + protected void onResume() { + super.onResume(); + if (isCustomTabsOpen) { + isCustomTabsOpen = false; + finish(); + } else { + isCustomTabsOpen = true; + } + } + + public void open(Browser implementation, Uri url, Integer toolbarColor) { + implementation.open(url, toolbarColor); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + isCustomTabsOpen = false; + BrowserPlugin.setBrowserControllerListener(null); + } +} diff --git a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerListener.java b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerListener.java new file mode 100644 index 000000000..36ebb1e2b --- /dev/null +++ b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserControllerListener.java @@ -0,0 +1,5 @@ +package com.capacitorjs.plugins.browser; + +public interface BrowserControllerListener { + void onControllerReady(BrowserControllerActivity activity); +} diff --git a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java index 2eb909c19..1fbc16852 100644 --- a/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java +++ b/browser/android/src/main/java/com/capacitorjs/plugins/browser/BrowserPlugin.java @@ -1,6 +1,7 @@ package com.capacitorjs.plugins.browser; import android.content.ActivityNotFoundException; +import android.content.Intent; import android.net.Uri; import com.getcapacitor.Logger; import com.getcapacitor.Plugin; @@ -14,6 +15,16 @@ public class BrowserPlugin extends Plugin { private Browser implementation; + public static BrowserControllerListener browserControllerListener; + private static BrowserControllerActivity browserControllerActivityInstance; + + public static void setBrowserControllerListener(BrowserControllerListener listener) { + browserControllerListener = listener; + if (listener == null) { + browserControllerActivityInstance = null; + } + } + public void load() { implementation = new Browser(getContext()); implementation.setBrowserEventListener(this::onBrowserEvent); @@ -50,18 +61,35 @@ public void open(PluginCall call) { // open the browser and finish try { - implementation.open(url, toolbarColor); + Intent intent = new Intent(getContext(), BrowserControllerActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + getContext().startActivity(intent); + + Integer finalToolbarColor = toolbarColor; + setBrowserControllerListener( + activity -> { + activity.open(implementation, url, finalToolbarColor); + browserControllerActivityInstance = activity; + call.resolve(); + } + ); } catch (ActivityNotFoundException ex) { Logger.error(getLogTag(), ex.getLocalizedMessage(), null); call.reject("Unable to display URL"); return; } - call.resolve(); } @PluginMethod public void close(PluginCall call) { - call.unimplemented(); + if (browserControllerActivityInstance != null) { + browserControllerActivityInstance = null; + Intent intent = new Intent(getContext(), BrowserControllerActivity.class); + intent.putExtra("close", true); + getContext().startActivity(intent); + } + call.resolve(); } @Override