Skip to content

Commit 3ad6015

Browse files
authored
Fix social-sign-in in web-ui when default browser does not support custom-tabs (#2721)
* allows fallback to launch the default browser if custom-tabs are not supported * updates androidx.browser:browser to 1.4.0 * fixes determing the preferred Custom Tabs service and uses the preferred one * adjusts pre-warming of Custom Tabs to use preferred Custom Tabs Service
1 parent 9afa4e8 commit 3ad6015

File tree

4 files changed

+40
-85
lines changed

4 files changed

+40
-85
lines changed

aws-android-sdk-cognitoauth/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies {
2323
api (project(':aws-android-sdk-core')) {
2424
exclude group: 'com.google.android', module: 'android'
2525
}
26-
implementation 'androidx.browser:browser:1.2.0'
26+
implementation 'androidx.browser:browser:1.4.0'
2727
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider-asf:1.0.0'
2828
}
2929

aws-android-sdk-cognitoauth/src/main/java/com/amazonaws/mobileconnectors/cognitoauth/AuthClient.java

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException;
4242
import com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthServiceException;
4343
import com.amazonaws.mobileconnectors.cognitoauth.exceptions.BrowserNotInstalledException;
44-
import com.amazonaws.mobileconnectors.cognitoauth.exceptions.CustomTabsNotSupportedException;
4544
import com.amazonaws.mobileconnectors.cognitoauth.util.AuthHttpResponseParser;
4645
import com.amazonaws.mobileconnectors.cognitoauth.handlers.AuthHandler;
4746
import com.amazonaws.mobileconnectors.cognitoauth.util.ClientConstants;
@@ -52,7 +51,6 @@
5251
import java.net.URL;
5352
import java.security.InvalidParameterException;
5453
import java.util.ArrayList;
55-
import java.util.Collection;
5654
import java.util.HashMap;
5755
import java.util.List;
5856
import java.util.Map;
@@ -94,11 +92,6 @@ public class AuthClient {
9492
*/
9593
private static final long REDIRECT_TIMEOUT_SECONDS = 10;
9694

97-
/**
98-
* Specifies what browser package to default to if one isn't specified.
99-
*/
100-
private static final String DEFAULT_BROWSER_PACKAGE = ClientConstants.CHROME_PACKAGE;
101-
10295
/**
10396
* Android application context.
10497
*/
@@ -149,6 +142,10 @@ public class AuthClient {
149142
*/
150143
private boolean isCustomTabSupported;
151144

145+
/**
146+
* Cache the packageName of the custom-tabs-service that should be used.
147+
*/
148+
private String customTabsPackageName;
152149

153150
// - Chrome Custom Tabs Controls
154151
private CustomTabsClient mCustomTabsClient;
@@ -180,7 +177,9 @@ protected AuthClient(final Context context, final Auth pool, final String userna
180177
this.isRedirectActivityDeclared = false;
181178
this.isBrowserInstalled = false;
182179
this.isCustomTabSupported = false;
183-
preWarmChrome();
180+
if (isCustomTabSupported()) {
181+
preWarmCustomTabs();
182+
}
184183
}
185184

186185
/**
@@ -747,42 +746,39 @@ private boolean isBrowserInstalled() {
747746
}
748747

749748
/**
750-
* Get list of browser packages that support Custom Tabs Service.
749+
* Get list of packages that support Custom Tabs Service.
751750
* @return list of package names that support Custom Tabs.
752751
*/
753-
private Collection<String> getSupportedBrowserPackage(){
752+
private List<String> getSupportedCustomTabsPackages() {
754753
PackageManager packageManager = context.getPackageManager();
755-
// Get default VIEW intent handler.
756-
Intent activityIntent = new Intent()
757-
.setAction(Intent.ACTION_VIEW)
758-
.addCategory(Intent.CATEGORY_BROWSABLE)
759-
.setData(Uri.fromParts("http", "", null));
760-
761-
// Get all apps that can handle VIEW intents.
762-
List<ResolveInfo> resolvedActivityList = packageManager.queryIntentActivities(activityIntent, 0);
754+
Intent serviceIntent = new Intent()
755+
.setAction(ACTION_CUSTOM_TABS_CONNECTION);
756+
757+
// Get all services that can handle ACTION_CUSTOM_TABS_CONNECTION intents.
758+
List<ResolveInfo> resolvedServicesList = packageManager.queryIntentServices(serviceIntent, 0);
763759
List<String> packageNamesSupportingCustomTabs = new ArrayList<>();
764-
for (ResolveInfo info : resolvedActivityList) {
765-
Intent serviceIntent = new Intent()
766-
.setAction(ACTION_CUSTOM_TABS_CONNECTION)
767-
.setPackage(info.activityInfo.packageName);
768-
// Check if this package also resolves the Custom Tabs service.
769-
if(packageManager.resolveService(serviceIntent, 0) != null) {
770-
packageNamesSupportingCustomTabs.add(info.activityInfo.packageName);
771-
}
760+
for (ResolveInfo info : resolvedServicesList) {
761+
packageNamesSupportingCustomTabs.add(info.serviceInfo.packageName);
772762
}
773763
return packageNamesSupportingCustomTabs;
774764
}
775765

776766
/***
777-
* Check if there are any browsers on the deivce that support custom tabs.
767+
* Check if there are any browsers on the device that support custom tabs.
778768
* @return true if custom tabs is supported by any browsers on the device else false.
779769
*/
780770
private boolean isCustomTabSupported() {
781771
if (isCustomTabSupported) {
782772
return true;
783773
}
784-
if (getSupportedBrowserPackage().size() > 0) {
774+
List<String> supportedCustomTabsPackages = getSupportedCustomTabsPackages();
775+
if (supportedCustomTabsPackages.size() > 0) {
785776
isCustomTabSupported = true;
777+
// get the preferred Custom Tabs package
778+
customTabsPackageName = CustomTabsClient.getPackageName(
779+
context,
780+
supportedCustomTabsPackages
781+
);
786782
return true;
787783
}
788784
return false;
@@ -801,16 +797,15 @@ private void launchCustomTabs(final Uri uri, final Activity activity, final Stri
801797
userHandler.onFailure(new BrowserNotInstalledException("No browsers installed."));
802798
return;
803799
}
804-
if(!isCustomTabSupported()) {
805-
userHandler.onFailure(new CustomTabsNotSupportedException("Browser with custom tabs support not found."));
806-
return;
807-
}
808800
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(mCustomTabsSession);
809801
mCustomTabsIntent = builder.build();
810-
if(pool.getCustomTabExtras() != null)
802+
if(pool.getCustomTabExtras() != null) {
811803
mCustomTabsIntent.intent.putExtras(pool.getCustomTabExtras());
804+
}
812805
if(browserPackage != null) {
813806
mCustomTabsIntent.intent.setPackage(browserPackage);
807+
} else if (customTabsPackageName != null) {
808+
mCustomTabsIntent.intent.setPackage(customTabsPackageName);
814809
}
815810
mCustomTabsIntent.intent.setData(uri);
816811
if (activity != null) {
@@ -839,9 +834,12 @@ private String getUserContextData() {
839834
}
840835

841836
/**
842-
* Connects to Chrome Service on the device.
837+
* Connects to Custom Tabs Service on the device.
843838
*/
844-
private void preWarmChrome() {
839+
private void preWarmCustomTabs() {
840+
if (customTabsPackageName == null) {
841+
return;
842+
}
845843
mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
846844
@Override
847845
public void onCustomTabsServiceConnected(final ComponentName name, final CustomTabsClient client) {
@@ -855,6 +853,11 @@ public void onServiceDisconnected(final ComponentName name) {
855853
mCustomTabsClient = null;
856854
}
857855
};
856+
CustomTabsClient.bindCustomTabsService(
857+
context,
858+
customTabsPackageName,
859+
mCustomTabsServiceConnection
860+
);
858861
}
859862

860863
// Inspects context to determine whether HostedUIRedirectActivity is declared in

aws-android-sdk-cognitoauth/src/main/java/com/amazonaws/mobileconnectors/cognitoauth/exceptions/CustomTabsNotSupportedException.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

aws-android-sdk-mobile-client/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies {
4747
compileOnly project(':aws-android-sdk-auth-google')
4848
compileOnly project(':aws-android-sdk-auth-userpools')
4949
compileOnly project(':aws-android-sdk-cognitoauth')
50-
compileOnly 'androidx.browser:browser:1.2.0'
50+
compileOnly 'androidx.browser:browser:1.4.0'
5151

5252
testImplementation 'junit:junit:4.13.2'
5353
testImplementation 'org.mockito:mockito-core:3.2.4'

0 commit comments

Comments
 (0)