Created
November 22, 2015 11:37
-
-
Save brainail/0135bf2f140e0046f680 to your computer and use it in GitHub Desktop.
Helper class for Custom Tabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class CustomTabsHelper { | |
public static abstract class ChromePackages { | |
public static final String STABLE_PACKAGE = "com.android.chrome"; | |
public static final String BETA_PACKAGE = "com.chrome.beta"; | |
public static final String DEV_PACKAGE = "com.chrome.dev"; | |
public static final String LOCAL_PACKAGE = "com.google.android.apps.chrome"; | |
} | |
public static String sPackageNameToUse; | |
public static String getPackageNameToUse (final Context context) { | |
if (null != sPackageNameToUse) return sPackageNameToUse; | |
final PackageManager packageManager = context.getPackageManager (); | |
// Get default VIEW intent handler. | |
final Intent activityIntent = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://www.example.com")); | |
final ResolveInfo defaultViewHandlerInfo = packageManager.resolveActivity (activityIntent, 0); | |
String defaultViewHandlerPackageName = null; | |
if (null != defaultViewHandlerInfo) { | |
defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName; | |
} | |
// Get all apps that can handle VIEW intents and had warmup service | |
final List<ResolveInfo> resolvedActivityList = packageManager.queryIntentActivities (activityIntent, 0); | |
final List<String> packagesSupportingCustomTabs = new ArrayList<> (); | |
for (final ResolveInfo resolveInfo : resolvedActivityList) { | |
final Intent serviceIntent = new Intent (); | |
serviceIntent.setAction (CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); | |
serviceIntent.setPackage (resolveInfo.activityInfo.packageName); | |
if (null != packageManager.resolveService (serviceIntent, 0)) { | |
packagesSupportingCustomTabs.add (resolveInfo.activityInfo.packageName); | |
} | |
} | |
// No handlers | |
if (packagesSupportingCustomTabs.isEmpty ()) { | |
sPackageNameToUse = null; | |
} else | |
// Only one | |
if (1 == packagesSupportingCustomTabs.size ()) { | |
sPackageNameToUse = packagesSupportingCustomTabs.get (0); | |
} else | |
// Try to use the default | |
if (! TextUtils.isEmpty (defaultViewHandlerPackageName) | |
&& ! hasSpecializedHandlerIntents (context, activityIntent) | |
&& packagesSupportingCustomTabs.contains (defaultViewHandlerPackageName)) { | |
sPackageNameToUse = defaultViewHandlerPackageName; | |
} else | |
// Stable | |
if (packagesSupportingCustomTabs.contains (ChromePackages.STABLE_PACKAGE)) { | |
sPackageNameToUse = ChromePackages.STABLE_PACKAGE; | |
} else | |
// Beta | |
if (packagesSupportingCustomTabs.contains (ChromePackages.BETA_PACKAGE)) { | |
sPackageNameToUse = ChromePackages.BETA_PACKAGE; | |
} else | |
// Dev | |
if (packagesSupportingCustomTabs.contains (ChromePackages.DEV_PACKAGE)) { | |
sPackageNameToUse = ChromePackages.DEV_PACKAGE; | |
} else | |
// Local | |
if (packagesSupportingCustomTabs.contains (ChromePackages.LOCAL_PACKAGE)) { | |
sPackageNameToUse = ChromePackages.LOCAL_PACKAGE; | |
} | |
return sPackageNameToUse; | |
} | |
private static boolean hasSpecializedHandlerIntents (Context context, Intent intent) { | |
try { | |
final PackageManager pm = context.getPackageManager (); | |
final List<ResolveInfo> handlers = pm.queryIntentActivities (intent, PackageManager.GET_RESOLVED_FILTER); | |
if (! (null != handlers && ! handlers.isEmpty ())) return false; | |
for (final ResolveInfo resolveInfo : handlers) { | |
final IntentFilter filter = resolveInfo.filter; | |
if (null == filter || filter.countDataAuthorities () == 0 || filter.countDataPaths () == 0) continue; | |
if (null == resolveInfo.activityInfo) continue; | |
return true; | |
} | |
} catch (final Exception exception) { | |
Log.e ("[CustomTabsHelper]", "Runtime exception while getting specialized handlers"); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment