4141import com .amazonaws .mobileconnectors .cognitoauth .exceptions .AuthNavigationException ;
4242import com .amazonaws .mobileconnectors .cognitoauth .exceptions .AuthServiceException ;
4343import com .amazonaws .mobileconnectors .cognitoauth .exceptions .BrowserNotInstalledException ;
44- import com .amazonaws .mobileconnectors .cognitoauth .exceptions .CustomTabsNotSupportedException ;
4544import com .amazonaws .mobileconnectors .cognitoauth .util .AuthHttpResponseParser ;
4645import com .amazonaws .mobileconnectors .cognitoauth .handlers .AuthHandler ;
4746import com .amazonaws .mobileconnectors .cognitoauth .util .ClientConstants ;
5251import java .net .URL ;
5352import java .security .InvalidParameterException ;
5453import java .util .ArrayList ;
55- import java .util .Collection ;
5654import java .util .HashMap ;
5755import java .util .List ;
5856import 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
0 commit comments