Skip to content

Commit 7fe8bb1

Browse files
committed
fix: android custom tabs dismiss callback
1 parent d2f4527 commit 7fe8bb1

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

Plugins/Android/ImmutableAndroid/ImmutableAndroid/src/main/java/com/immutable/unity/ImmutableActivity.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import android.net.Uri;
77
import android.os.Build;
88
import android.os.Bundle;
9-
import android.os.Handler;
10-
import android.os.Looper;
119

1210
import androidx.annotation.NonNull;
1311
import androidx.annotation.Nullable;
@@ -61,6 +59,16 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
6159
@Override
6260
protected void onResume() {
6361
super.onResume();
62+
Uri uri = getUri();
63+
// Determine whether user returned to this activity from a redirect or because the user cancelled
64+
// the auth flow. If there is no response data (from RedirectActivity), it's because the
65+
// user dismissed custom tabs, pressed the back button, or the auth flow finished without invoking
66+
// RedirectActivity.
67+
if (customTabsLaunched && uri != null && getIntent().getData() == null && callbackInstance != null) {
68+
// User cancelled auth flow
69+
callbackInstance.onCustomTabsDismissed(uri.toString());
70+
}
71+
6472
Intent authenticationIntent = getIntent();
6573
if (!customTabsLaunched && authenticationIntent.getExtras() == null) {
6674
// This activity was launched in an unexpected way
@@ -85,34 +93,29 @@ protected void onDestroy() {
8593
}
8694
}
8795

88-
private void launchCustomTabs() {
96+
@Nullable
97+
private Uri getUri() {
8998
Bundle extras = getIntent().getExtras();
90-
Uri uri;
91-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
92-
uri = extras.getParcelable(EXTRA_URI, Uri.class);
99+
if (extras != null) {
100+
Uri uri;
101+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
102+
uri = extras.getParcelable(EXTRA_URI, Uri.class);
103+
} else {
104+
uri = extras.getParcelable(EXTRA_URI);
105+
}
106+
return uri;
93107
} else {
94-
uri = extras.getParcelable(EXTRA_URI);
108+
return null;
109+
}
110+
}
111+
112+
private void launchCustomTabs() {
113+
Uri uri = getUri();
114+
if (uri != null) {
115+
customTabsController = new CustomTabsController(this, new CustomTabsCallback());
116+
customTabsController.bindService();
117+
customTabsController.launch(uri);
95118
}
96-
customTabsController = new CustomTabsController(this, new CustomTabsCallback() {
97-
@Override
98-
public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {
99-
if (navigationEvent == CustomTabsCallback.TAB_HIDDEN && callbackInstance != null) {
100-
// Adding some delay before calling onCustomTabsDismissed as sometimes this gets called
101-
// before the PKCE deeplink is triggered (by 100ms). This means pkceCompletionSource will be
102-
// set to null before the SDK can use it to notify the consumer of the PKCE result.
103-
// See PassportImpl.OnLoginPKCEDismissed and PassportImpl.OnDeepLinkActivated
104-
final Handler handler = new Handler(Looper.getMainLooper());
105-
handler.postDelayed(new Runnable() {
106-
@Override
107-
public void run() {
108-
callbackInstance.onCustomTabsDismissed(uri.toString());
109-
}
110-
}, 1000);
111-
}
112-
}
113-
});
114-
customTabsController.bindService();
115-
customTabsController.launch(uri);
116119
}
117120

118121
private void onDeeplinkResult(@Nullable Intent intent) {
Binary file not shown.

0 commit comments

Comments
 (0)