Commit 09c286ca authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android WebAPK Refactor] Group all of the public methods in HostBrowserUtils together

BUG=900464

Change-Id: Iabdce98f4f3a362d1376860277fc9d1c31757c12
Reviewed-on: https://chromium-review.googlesource.com/c/1309296
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarXi Han <hanxi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604728}
parent 1f39ce90
...@@ -12,4 +12,4 @@ ...@@ -12,4 +12,4 @@
# //chrome/android/webapk/shell_apk:webapk is changed. This includes # //chrome/android/webapk/shell_apk:webapk is changed. This includes
# Java files, Android resource files and AndroidManifest.xml. Does not affect # Java files, Android resource files and AndroidManifest.xml. Does not affect
# Chrome.apk # Chrome.apk
current_shell_apk_version = 71 current_shell_apk_version = 72
...@@ -90,55 +90,6 @@ public class HostBrowserUtils { ...@@ -90,55 +90,6 @@ public class HostBrowserUtils {
return sHostPackage; return sHostPackage;
} }
/**
* Returns the package name of the host browser to launch the WebAPK, or null if we did not find
* one.
*/
private static String getHostBrowserPackageNameInternal(Context context) {
PackageManager packageManager = context.getPackageManager();
// Gets the package name of the host browser if it is stored in the SharedPreference.
String cachedHostBrowser = getHostBrowserFromSharedPreference(context);
if (!TextUtils.isEmpty(cachedHostBrowser)
&& WebApkUtils.isInstalled(packageManager, cachedHostBrowser)) {
return cachedHostBrowser;
}
// Gets the package name of the host browser if it is specified in AndroidManifest.xml.
String hostBrowserFromManifest =
WebApkUtils.readMetaDataFromManifest(context, WebApkMetaDataKeys.RUNTIME_HOST);
if (!TextUtils.isEmpty(hostBrowserFromManifest)) {
if (WebApkUtils.isInstalled(packageManager, hostBrowserFromManifest)) {
return hostBrowserFromManifest;
}
return null;
}
// Gets the package name of the default browser on the Android device.
// TODO(hanxi): Investigate the best way to know which browser supports WebAPKs.
String defaultBrowser = getDefaultBrowserPackageName(context.getPackageManager());
if (!TextUtils.isEmpty(defaultBrowser) && sBrowsersSupportingWebApk.contains(defaultBrowser)
&& WebApkUtils.isInstalled(packageManager, defaultBrowser)) {
return defaultBrowser;
}
// If there is only one browser supporting WebAPK, and we can't decide which browser to use
// by looking up cache, metadata and default browser, open with that browser.
int availableBrowserCounter = 0;
String lastSupportedBrowser = null;
for (String packageName : sBrowsersSupportingWebApk) {
if (availableBrowserCounter > 1) break;
if (WebApkUtils.isInstalled(packageManager, packageName)) {
availableBrowserCounter++;
lastSupportedBrowser = packageName;
}
}
if (availableBrowserCounter == 1) {
return lastSupportedBrowser;
}
return null;
}
/** /**
* Returns the uid for the host browser that was specified when building the WebAPK. * Returns the uid for the host browser that was specified when building the WebAPK.
* @param context A context. * @param context A context.
...@@ -160,23 +111,6 @@ public class HostBrowserUtils { ...@@ -160,23 +111,6 @@ public class HostBrowserUtils {
return -1; return -1;
} }
/** Returns the package name of the host browser cached in the SharedPreferences. */
public static String getHostBrowserFromSharedPreference(Context context) {
SharedPreferences sharedPref =
context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE);
return sharedPref.getString(SHARED_PREF_RUNTIME_HOST, null);
}
/** Returns the package name of the default browser on the Android device. */
private static String getDefaultBrowserPackageName(PackageManager packageManager) {
Intent browserIntent = WebApkUtils.getQueryInstalledBrowsersIntent();
ResolveInfo resolveInfo =
packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
return resolveInfo.activityInfo.packageName;
}
/** /**
* Writes the package name of the host browser to the SharedPreferences. If the host browser is * Writes the package name of the host browser to the SharedPreferences. If the host browser is
* different than the previous one stored, delete the SharedPreference before storing the new * different than the previous one stored, delete the SharedPreference before storing the new
...@@ -226,4 +160,70 @@ public class HostBrowserUtils { ...@@ -226,4 +160,70 @@ public class HostBrowserUtils {
return hostBrowserChromiumMajorVersion < MINIMUM_REQUIRED_CHROME_VERSION; return hostBrowserChromiumMajorVersion < MINIMUM_REQUIRED_CHROME_VERSION;
} }
/**
* Returns the package name of the host browser to launch the WebAPK, or null if we did not find
* one.
*/
private static String getHostBrowserPackageNameInternal(Context context) {
PackageManager packageManager = context.getPackageManager();
// Gets the package name of the host browser if it is stored in the SharedPreference.
String cachedHostBrowser = getHostBrowserFromSharedPreference(context);
if (!TextUtils.isEmpty(cachedHostBrowser)
&& WebApkUtils.isInstalled(packageManager, cachedHostBrowser)) {
return cachedHostBrowser;
}
// Gets the package name of the host browser if it is specified in AndroidManifest.xml.
String hostBrowserFromManifest =
WebApkUtils.readMetaDataFromManifest(context, WebApkMetaDataKeys.RUNTIME_HOST);
if (!TextUtils.isEmpty(hostBrowserFromManifest)) {
if (WebApkUtils.isInstalled(packageManager, hostBrowserFromManifest)) {
return hostBrowserFromManifest;
}
return null;
}
// Gets the package name of the default browser on the Android device.
// TODO(hanxi): Investigate the best way to know which browser supports WebAPKs.
String defaultBrowser = getDefaultBrowserPackageName(context.getPackageManager());
if (!TextUtils.isEmpty(defaultBrowser) && sBrowsersSupportingWebApk.contains(defaultBrowser)
&& WebApkUtils.isInstalled(packageManager, defaultBrowser)) {
return defaultBrowser;
}
// If there is only one browser supporting WebAPK, and we can't decide which browser to use
// by looking up cache, metadata and default browser, open with that browser.
int availableBrowserCounter = 0;
String lastSupportedBrowser = null;
for (String packageName : sBrowsersSupportingWebApk) {
if (availableBrowserCounter > 1) break;
if (WebApkUtils.isInstalled(packageManager, packageName)) {
availableBrowserCounter++;
lastSupportedBrowser = packageName;
}
}
if (availableBrowserCounter == 1) {
return lastSupportedBrowser;
}
return null;
}
/** Returns the package name of the host browser cached in the SharedPreferences. */
public static String getHostBrowserFromSharedPreference(Context context) {
SharedPreferences sharedPref =
context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE);
return sharedPref.getString(SHARED_PREF_RUNTIME_HOST, null);
}
/** Returns the package name of the default browser on the Android device. */
private static String getDefaultBrowserPackageName(PackageManager packageManager) {
Intent browserIntent = WebApkUtils.getQueryInstalledBrowsersIntent();
ResolveInfo resolveInfo =
packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
return resolveInfo.activityInfo.packageName;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment