@ Nullable public String getPackageNameToUse ( Context context ) { if ( packageNameToUse != null ) return packageNameToUse ; PackageManager pm = context . getPackageManager ( ) ; // Get default VIEW intent handler. Intent activityIntent = new Intent ( Intent . ACTION_VIEW , Uri . parse ( "http://www.example.com" ) ) ; ResolveInfo defaultViewHandlerInfo = pm . resolveActivity ( activityIntent , 0 ) ; String defaultViewHandlerPackageName = null ; if ( defaultViewHandlerInfo != null ) { defaultViewHandlerPackageName = defaultViewHandlerInfo . activityInfo . packageName ; } // Get all apps that can handle VIEW intents. List < ResolveInfo > resolvedActivityList = pm . queryIntentActivities ( activityIntent , 0 ) ; List < String > packagesSupportingCustomTabs = new ArrayList <> ( ) ; for ( ResolveInfo info : resolvedActivityList ) { Intent serviceIntent = new Intent ( ) ; serviceIntent . setAction ( ACTION_CUSTOM_TABS_CONNECTION ) ; serviceIntent . setPackage ( info . activityInfo . packageName ) ; if ( pm . resolveService ( serviceIntent , 0 ) != null ) { packagesSupportingCustomTabs . add ( info . activityInfo . packageName ) ; } } // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents // and service calls. if ( packagesSupportingCustomTabs . isEmpty ( ) ) { packageNameToUse = null ; } else if ( packagesSupportingCustomTabs . size ( ) == 1 ) { packageNameToUse = packagesSupportingCustomTabs . get ( 0 ) ; } else if ( ! TextUtils . isEmpty ( defaultViewHandlerPackageName ) && ! hasSpecializedHandlerIntents ( context , activityIntent ) && packagesSupportingCustomTabs . contains ( defaultViewHandlerPackageName ) ) { packageNameToUse = defaultViewHandlerPackageName ; } else if ( packagesSupportingCustomTabs . contains ( STABLE_PACKAGE ) ) { packageNameToUse = STABLE_PACKAGE ; } else if ( packagesSupportingCustomTabs . contains ( BETA_PACKAGE ) ) { packageNameToUse = BETA_PACKAGE ; } else if ( packagesSupportingCustomTabs . contains ( DEV_PACKAGE ) ) { packageNameToUse = DEV_PACKAGE ; } else if ( packagesSupportingCustomTabs . contains ( LOCAL_PACKAGE ) ) { packageNameToUse = LOCAL_PACKAGE ; } return packageNameToUse ; }
Goes through all apps that handle VIEW intents and have a warmup service . Picks the one chosen by the user if there is one otherwise makes a best effort to return a valid package name .