Index: shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java |
diff --git a/shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java b/shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java |
index 00af1e2189019aeabbcfcc4d498e22d09a2a073c..d5d70865f1ef98a88dcc3304ee61777eb4ed1e05 100644 |
--- a/shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java |
+++ b/shared/src/main/java/org/chromium/customtabsclient/shared/CustomTabsHelper.java |
@@ -19,6 +19,7 @@ import android.content.Intent; |
import android.content.IntentFilter; |
import android.content.pm.PackageManager; |
import android.content.pm.ResolveInfo; |
+import android.graphics.Color; |
import android.net.Uri; |
import android.text.TextUtils; |
import android.util.Log; |
@@ -60,9 +61,14 @@ public class CustomTabsHelper { |
* @param context {@link Context} to use for accessing {@link PackageManager}. |
* @return The package name recommended to use for connecting to custom tabs related components. |
*/ |
- public static String getPackageNameToUse(Context context) { |
+ public static String getPackageNameToUse(Context context, |
+ List<String> packagesSupportingCustomTabs) { |
if (sPackageNameToUse != null) return sPackageNameToUse; |
+ if (packagesSupportingCustomTabs == null) { |
+ packagesSupportingCustomTabs = getAllPackagesSupportingCustomTabs(context); |
+ } |
+ |
PackageManager pm = context.getPackageManager(); |
// Get default VIEW intent handler. |
Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); |
@@ -72,18 +78,6 @@ public class CustomTabsHelper { |
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()) { |
@@ -107,6 +101,13 @@ public class CustomTabsHelper { |
} |
/** |
+ * See getPackageNameToUse(Context, List<String). |
+ */ |
+ public static String getPackageNameToUse(Context context) { |
+ return getPackageNameToUse(context, null); |
+ } |
+ |
+ /** |
* Used to check whether there is a specialized handler for a given intent. |
* @param intent The intent to check with. |
* @return Whether there is a specialized handler for the given intent. |
@@ -139,4 +140,65 @@ public class CustomTabsHelper { |
public static String[] getPackages() { |
return new String[]{"", STABLE_PACKAGE, BETA_PACKAGE, DEV_PACKAGE, LOCAL_PACKAGE}; |
} |
+ |
+ /** |
+ * @return All possible colors. |
+ */ |
+ public static String[] getColors() { |
Ian Wen
2016/02/10 02:36:06
This class is copied to many 3p apps, and thus it
BigBossZhiling
2016/02/17 05:18:10
Done.
|
+ ArrayList<String> fieldList = new ArrayList<String>(); |
+ String colorName; |
+ for (int i = 0; i < Color.class.getFields().length; i ++) { |
+ colorName = Color.class.getFields()[i].getName(); |
+ // Make only the first character capitalized. |
+ fieldList.add(i, colorName.charAt(0) + colorName.substring(1).toLowerCase()); |
+ } |
+ return fieldList.toArray(new String[fieldList.size()]); |
+ } |
+ |
+ /** |
+ * Get all apps that support custom tabs. |
+ * @param context |
+ * @return |
+ */ |
+ public static ArrayList<String> getAllPackagesSupportingCustomTabs(Context context) { |
+ PackageManager pm = context.getPackageManager(); |
+ // Get default VIEW intent handler. |
+ Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); |
+ // Get all apps that can handle VIEW intents. |
+ List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0); |
+ ArrayList<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); |
+ } |
+ } |
+ return packagesSupportingCustomTabs; |
+ } |
+ |
+ public static int stringToColor(String color) { |
+ try { |
+ return (int) Color.class.getField(color.toUpperCase()).get(new Color()); |
+ } catch (Exception e) { |
+ return 0; // Transparent. |
+ } |
+ } |
+ |
+ public static String chromeStable() { |
Ian Wen
2016/02/10 02:36:06
Only make APIs if they are used immediately by our
BigBossZhiling
2016/02/17 05:18:10
Done.
|
+ return STABLE_PACKAGE; |
+ } |
+ |
+ public static String chromeBeta() { |
+ return BETA_PACKAGE; |
+ } |
+ |
+ public static String chromeDev() { |
+ return DEV_PACKAGE; |
+ } |
+ |
+ public static String chromeLocal() { |
+ return LOCAL_PACKAGE; |
+ } |
} |