@@ -39,7 +39,7 @@ public class CustomTabsHelper {
3939 "android.support.customtabs.extra.KEEP_ALIVE" ;
4040 private static final String ACTION_CUSTOM_TABS_CONNECTION =
4141 "android.support.customtabs.action.CustomTabsService" ;
42-
42+ private static final Intent activityIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( "http://www.example.com" ));
4343 private static String sPackageNameToUse ;
4444
4545 private CustomTabsHelper () {}
@@ -64,25 +64,8 @@ public static String getPackageNameToUse(Context context) {
6464 if (sPackageNameToUse != null ) return sPackageNameToUse ;
6565
6666 PackageManager pm = context .getPackageManager ();
67- // Get default VIEW intent handler.
68- Intent activityIntent = new Intent (Intent .ACTION_VIEW , Uri .parse ("http://www.example.com" ));
69- ResolveInfo defaultViewHandlerInfo = pm .resolveActivity (activityIntent , 0 );
70- String defaultViewHandlerPackageName = null ;
71- if (defaultViewHandlerInfo != null ) {
72- defaultViewHandlerPackageName = defaultViewHandlerInfo .activityInfo .packageName ;
73- }
74-
75- // Get all apps that can handle VIEW intents.
76- List <ResolveInfo > resolvedActivityList = pm .queryIntentActivities (activityIntent , PackageManager .MATCH_ALL );
77- List <String > packagesSupportingCustomTabs = new ArrayList <>();
78- for (ResolveInfo info : resolvedActivityList ) {
79- Intent serviceIntent = new Intent ();
80- serviceIntent .setAction (ACTION_CUSTOM_TABS_CONNECTION );
81- serviceIntent .setPackage (info .activityInfo .packageName );
82- if (pm .resolveService (serviceIntent , 0 ) != null ) {
83- packagesSupportingCustomTabs .add (info .activityInfo .packageName );
84- }
85- }
67+ final String defaultViewHandlerPackageName = getDefaultViewHandlerPackageName (context );
68+ List <String > packagesSupportingCustomTabs = getPackagesSupportingCustomTabs (context );
8669
8770 // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
8871 // and service calls.
@@ -106,6 +89,28 @@ public static String getPackageNameToUse(Context context) {
10689 return sPackageNameToUse ;
10790 }
10891
92+ public static String getDefaultViewHandlerPackageName (Context context ) {
93+ PackageManager pm = context .getPackageManager ();
94+ // Get default VIEW intent handler.
95+ ResolveInfo defaultViewHandlerInfo = pm .resolveActivity (activityIntent , 0 );
96+ return defaultViewHandlerInfo == null ? null : defaultViewHandlerInfo .activityInfo .packageName ;
97+ }
98+
99+ public static List <String > getPackagesSupportingCustomTabs (Context context ) {
100+ PackageManager pm = context .getPackageManager ();
101+ List <ResolveInfo > resolvedActivityList = pm .queryIntentActivities (activityIntent , PackageManager .MATCH_ALL );
102+ List <String > packagesSupportingCustomTabs = new ArrayList <>();
103+ for (ResolveInfo info : resolvedActivityList ) {
104+ Intent serviceIntent = new Intent ();
105+ serviceIntent .setAction (ACTION_CUSTOM_TABS_CONNECTION );
106+ serviceIntent .setPackage (info .activityInfo .packageName );
107+ if (pm .resolveService (serviceIntent , 0 ) != null ) {
108+ packagesSupportingCustomTabs .add (info .activityInfo .packageName );
109+ }
110+ }
111+ return packagesSupportingCustomTabs ;
112+ }
113+
109114 /**
110115 * Used to check whether there is a specialized handler for a given intent.
111116 * @param intent The intent to check with.
@@ -139,4 +144,18 @@ private static boolean hasSpecializedHandlerIntents(Context context, Intent inte
139144 public static String [] getPackages () {
140145 return new String []{"" , STABLE_PACKAGE , BETA_PACKAGE , DEV_PACKAGE , LOCAL_PACKAGE };
141146 }
147+
148+ public static void setPackageNameToUse (String packageName , Context context ) throws InvalidPackageException {
149+ if (getPackagesSupportingCustomTabs (context ).contains (packageName )) {
150+ sPackageNameToUse = packageName ;
151+ } else {
152+ throw new InvalidPackageException (packageName );
153+ }
154+ }
155+
156+ public static class InvalidPackageException extends Exception {
157+ public InvalidPackageException (String packageName ) {
158+ super (packageName + " has no Custom Tabs support." );
159+ }
160+ }
142161}
0 commit comments