Skip to content

[DX-2757] fix: android custom tabs dismiss callback #178

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

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Binary file not shown.