Skip to content

Commit

Permalink
Merge pull request #178 from immutable/fix/android-on-dismissed
Browse files Browse the repository at this point in the history
[DX-2757] fix: android custom tabs dismiss callback
  • Loading branch information
nattb8 authored Mar 14, 2024
2 parents d2f4527 + 7fe8bb1 commit 79988f2
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -61,6 +59,16 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
@Override
protected void onResume() {
super.onResume();
Uri uri = getUri();
// Determine whether user returned to this activity from a redirect or because the user cancelled
// the auth flow. If there is no response data (from RedirectActivity), it's because the
// user dismissed custom tabs, pressed the back button, or the auth flow finished without invoking
// RedirectActivity.
if (customTabsLaunched && uri != null && getIntent().getData() == null && callbackInstance != null) {
// User cancelled auth flow
callbackInstance.onCustomTabsDismissed(uri.toString());
}

Intent authenticationIntent = getIntent();
if (!customTabsLaunched && authenticationIntent.getExtras() == null) {
// This activity was launched in an unexpected way
@@ -85,34 +93,29 @@ protected void onDestroy() {
}
}

private void launchCustomTabs() {
@Nullable
private Uri getUri() {
Bundle extras = getIntent().getExtras();
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
uri = extras.getParcelable(EXTRA_URI, Uri.class);
if (extras != null) {
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
uri = extras.getParcelable(EXTRA_URI, Uri.class);
} else {
uri = extras.getParcelable(EXTRA_URI);
}
return uri;
} else {
uri = extras.getParcelable(EXTRA_URI);
return null;
}
}

private void launchCustomTabs() {
Uri uri = getUri();
if (uri != null) {
customTabsController = new CustomTabsController(this, new CustomTabsCallback());
customTabsController.bindService();
customTabsController.launch(uri);
}
customTabsController = new CustomTabsController(this, new CustomTabsCallback() {
@Override
public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {
if (navigationEvent == CustomTabsCallback.TAB_HIDDEN && callbackInstance != null) {
// Adding some delay before calling onCustomTabsDismissed as sometimes this gets called
// before the PKCE deeplink is triggered (by 100ms). This means pkceCompletionSource will be
// set to null before the SDK can use it to notify the consumer of the PKCE result.
// See PassportImpl.OnLoginPKCEDismissed and PassportImpl.OnDeepLinkActivated
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
callbackInstance.onCustomTabsDismissed(uri.toString());
}
}, 1000);
}
}
});
customTabsController.bindService();
customTabsController.launch(uri);
}

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

0 comments on commit 79988f2

Please sign in to comment.