Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): implement close() method for android #1972

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 @@
package com.capacitorjs.plugins.browser;

import android.content.ActivityNotFoundException;
import android.content.Context;
ItsChaceD marked this conversation as resolved.
Show resolved Hide resolved
import android.content.Intent;
import android.net.Uri;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
Expand All @@ -14,6 +16,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 +62,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
Loading