Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webapk.lib.client; | 5 package org.chromium.webapk.lib.client; |
| 6 | 6 |
| 7 import static org.chromium.webapk.lib.common.WebApkConstants.WEBAPK_PACKAGE_PREF IX; | 7 import static org.chromium.webapk.lib.common.WebApkConstants.WEBAPK_PACKAGE_PREF IX; |
| 8 | 8 |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.Intent; | 10 import android.content.Intent; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 * handler is the WebAPK. | 33 * handler is the WebAPK. |
| 34 * | 34 * |
| 35 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url. | 35 * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url. |
| 36 * | 36 * |
| 37 * @param context The application context. | 37 * @param context The application context. |
| 38 * @param url The url to check. | 38 * @param url The url to check. |
| 39 * @return Package name of WebAPK which can handle the URL. Null if the url should not be | 39 * @return Package name of WebAPK which can handle the URL. Null if the url should not be |
| 40 * handled by a WebAPK. | 40 * handled by a WebAPK. |
| 41 */ | 41 */ |
| 42 public static String queryWebApkPackage(Context context, String url) { | 42 public static String queryWebApkPackage(Context context, String url) { |
| 43 List<ResolveInfo> resolveInfos = resolveInfosForUrl(context, url); | |
| 44 if (resolveInfos == null) { | |
| 45 return null; | |
| 46 } | |
| 47 return findWebApkPackage(context, resolveInfos); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Queries the PackageManager to determine whether a WebAPK can handle the U RL. Ignores | |
| 52 * whether the user has selected a default handler for the URL and whether t he default | |
| 53 * handler is the WebAPK. | |
| 54 * | |
| 55 * NOTE: This can fail if multiple WebAPKs can match the supplied url. | |
| 56 * | |
| 57 * @param context The application context. | |
| 58 * @param url The url to check. | |
| 59 * @return Resolve Info of a WebAPK which can handle the URL. Null if the ur l should not be | |
| 60 * handled by a WebAPK. | |
| 61 */ | |
| 62 public static ResolveInfo queryWebApk(Context context, String url) { | |
| 63 List<ResolveInfo> resolveInfos = resolveInfosForUrl(context, url); | |
| 64 if (resolveInfos == null) { | |
| 65 return null; | |
| 66 } | |
| 67 | |
| 68 for (ResolveInfo info : resolveInfos) { | |
| 69 if (info.activityInfo != null | |
| 70 && isValidWebApk(context, info.activityInfo.packageName)) { | |
| 71 return info; | |
| 72 } | |
| 73 } | |
| 74 return null; | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Fetches the list of resolve infos from the PackageManager that can handle the URL. | |
| 79 * | |
| 80 * @param context The application context. | |
| 81 * @param url The url to check. | |
| 82 * @return The list of resolve infos found that can handle the URL. | |
| 83 */ | |
| 84 private static List<ResolveInfo> resolveInfosForUrl(Context context, String url) { | |
|
dominickn
2017/02/21 23:04:42
It would be nice if this method never returned nul
gonzalon
2017/02/22 15:50:49
Done.
| |
| 43 Intent intent; | 85 Intent intent; |
| 44 try { | 86 try { |
| 45 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | 87 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 46 } catch (Exception e) { | 88 } catch (Exception e) { |
| 47 return null; | 89 return null; |
| 48 } | 90 } |
| 49 | 91 |
| 50 intent.addCategory(Intent.CATEGORY_BROWSABLE); | 92 intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 51 intent.setComponent(null); | 93 intent.setComponent(null); |
| 52 Intent selector = intent.getSelector(); | 94 Intent selector = intent.getSelector(); |
| 53 if (selector != null) { | 95 if (selector != null) { |
| 54 selector.addCategory(Intent.CATEGORY_BROWSABLE); | 96 selector.addCategory(Intent.CATEGORY_BROWSABLE); |
| 55 selector.setComponent(null); | 97 selector.setComponent(null); |
| 56 } | 98 } |
| 57 | 99 return context.getPackageManager().queryIntentActivities( |
| 58 List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntent Activities( | |
| 59 intent, PackageManager.GET_RESOLVED_FILTER); | 100 intent, PackageManager.GET_RESOLVED_FILTER); |
| 60 return findWebApkPackage(context, resolveInfos); | |
| 61 } | 101 } |
| 62 | 102 |
| 63 /** | 103 /** |
| 64 * @param context The context to use to check whether WebAPK is valid. | 104 * @param context The context to use to check whether WebAPK is valid. |
| 65 * @param infos The ResolveInfos to search. | 105 * @param infos The ResolveInfos to search. |
| 66 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the | 106 * @return Package name of the ResolveInfo which corresponds to a WebAPK. Nu ll if none of the |
| 67 * ResolveInfos corresponds to a WebAPK. | 107 * ResolveInfos corresponds to a WebAPK. |
| 68 */ | 108 */ |
| 69 public static String findWebApkPackage(Context context, List<ResolveInfo> in fos) { | 109 public static String findWebApkPackage(Context context, List<ResolveInfo> in fos) { |
| 70 for (ResolveInfo info : infos) { | 110 for (ResolveInfo info : infos) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 * with for the current host. | 159 * with for the current host. |
| 120 * @param expectedSignature | 160 * @param expectedSignature |
| 121 */ | 161 */ |
| 122 public static void initWithBrowserHostSignature(byte[] expectedSignature) { | 162 public static void initWithBrowserHostSignature(byte[] expectedSignature) { |
| 123 if (sExpectedSignature != null) { | 163 if (sExpectedSignature != null) { |
| 124 return; | 164 return; |
| 125 } | 165 } |
| 126 sExpectedSignature = Arrays.copyOf(expectedSignature, expectedSignature. length); | 166 sExpectedSignature = Arrays.copyOf(expectedSignature, expectedSignature. length); |
| 127 } | 167 } |
| 128 } | 168 } |
| OLD | NEW |