Skip to content

Commit

Permalink
feat(browser): implement close() method for android
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD committed Jan 22, 2024
1 parent f9afbe9 commit e2cdf35
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
6 changes: 6 additions & 0 deletions browser/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name=".BrowserControllerActivity"
android:exported="false"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:launchMode="singleTask"/>
</application>
<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.capacitorjs.plugins.browser;

public interface BrowserControllerListener {
void onControllerReady(BrowserControllerActivity activity);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e2cdf35

Please sign in to comment.