| Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/AppBannerInfoBarDelegateAndroid.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/AppBannerInfoBarDelegateAndroid.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/AppBannerInfoBarDelegateAndroid.java
|
| index 989024241efeb1c31242977cc736eee29bf2dc45..eb0ced5021d1f4a345e022b304701dc42384e8d5 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/AppBannerInfoBarDelegateAndroid.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/AppBannerInfoBarDelegateAndroid.java
|
| @@ -5,6 +5,7 @@
|
| package org.chromium.chrome.browser.infobar;
|
|
|
| import android.app.Activity;
|
| +import android.content.ActivityNotFoundException;
|
| import android.content.ContentResolver;
|
| import android.content.Context;
|
| import android.content.Intent;
|
| @@ -13,13 +14,17 @@ import android.os.Looper;
|
|
|
| import org.chromium.base.ApplicationStatus;
|
| import org.chromium.base.ContextUtils;
|
| +import org.chromium.base.Log;
|
| +import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.VisibleForTesting;
|
| import org.chromium.base.annotations.CalledByNative;
|
| import org.chromium.base.annotations.JNINamespace;
|
| +import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.banners.AppData;
|
| import org.chromium.chrome.browser.banners.InstallerDelegate;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.ui.base.WindowAndroid;
|
| +import org.chromium.ui.widget.Toast;
|
|
|
| /**
|
| * Handles the promotion and installation of an app specified by the current web page. This Java
|
| @@ -27,6 +32,8 @@ import org.chromium.ui.base.WindowAndroid;
|
| */
|
| @JNINamespace("banners")
|
| public class AppBannerInfoBarDelegateAndroid {
|
| + private static final String TAG = "cr_AppBannerInfoBar";
|
| +
|
| /** PackageManager to use in place of the real one. */
|
| private static PackageManager sPackageManagerForTests;
|
|
|
| @@ -39,6 +46,15 @@ public class AppBannerInfoBarDelegateAndroid {
|
| /** Monitors for application state changes. */
|
| private final ApplicationStatus.ApplicationStateListener mListener;
|
|
|
| + /**
|
| + * Indicates whether a request to install a WebAPK has started. This flag is set while the
|
| + * WebAPK is being installed.
|
| + */
|
| + private boolean mIsInstallingWebApk;
|
| +
|
| + /** The package name of the WebAPK. */
|
| + private String mWebApkPackage;
|
| +
|
| /** Overrides the PackageManager for testing. */
|
| @VisibleForTesting
|
| public static void setPackageManagerForTesting(PackageManager manager) {
|
| @@ -79,9 +95,7 @@ public class AppBannerInfoBarDelegateAndroid {
|
|
|
| if (InstallerDelegate.isInstalled(packageManager, packageName)) {
|
| // Open the app.
|
| - Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
|
| - if (launchIntent == null) return true;
|
| - context.startActivity(launchIntent);
|
| + openApp(context, packageName);
|
| return true;
|
| } else {
|
| // Try installing the app. If the installation was kicked off, return false to prevent
|
| @@ -96,6 +110,18 @@ public class AppBannerInfoBarDelegateAndroid {
|
| }
|
| }
|
|
|
| + void openApp(Context context, String packageName) {
|
| + Intent launchIntent = getPackageManager(context).getLaunchIntentForPackage(packageName);
|
| + if (launchIntent != null) {
|
| + try {
|
| + context.startActivity(launchIntent);
|
| + } catch (ActivityNotFoundException e) {
|
| + Log.e(TAG, "Failed to open app : %s!", packageName, e);
|
| + return;
|
| + }
|
| + }
|
| + }
|
| +
|
| private WindowAndroid.IntentCallback createIntentCallback(final AppData appData) {
|
| return new WindowAndroid.IntentCallback() {
|
| @Override
|
| @@ -129,18 +155,57 @@ public class AppBannerInfoBarDelegateAndroid {
|
| }
|
|
|
| @CalledByNative
|
| + private void openWebApk(String packageName) {
|
| + Context context = ContextUtils.getApplicationContext();
|
| + PackageManager packageManager = getPackageManager(context);
|
| +
|
| + if (InstallerDelegate.isInstalled(packageManager, packageName)) {
|
| + mWebApkPackage = null;
|
| + openApp(context, packageName);
|
| + }
|
| + }
|
| +
|
| + @CalledByNative
|
| private void showAppDetails(Tab tab, AppData appData) {
|
| tab.getWindowAndroid().showIntent(appData.detailsIntent(), null, null);
|
| }
|
|
|
| @CalledByNative
|
| private int determineInstallState(AppData data) {
|
| - if (mInstallTask != null) return AppBannerInfoBarAndroid.INSTALL_STATE_INSTALLING;
|
| + if (mInstallTask != null || mIsInstallingWebApk) {
|
| + return AppBannerInfoBarAndroid.INSTALL_STATE_INSTALLING;
|
| + }
|
|
|
| PackageManager pm = getPackageManager(ContextUtils.getApplicationContext());
|
| - boolean isInstalled = InstallerDelegate.isInstalled(pm, data.packageName());
|
| + String packageName = (data != null) ? data.packageName() : mWebApkPackage;
|
| + boolean isInstalled = InstallerDelegate.isInstalled(pm, packageName);
|
| return isInstalled ? AppBannerInfoBarAndroid.INSTALL_STATE_INSTALLED
|
| - : AppBannerInfoBarAndroid.INSTALL_STATE_NOT_INSTALLED;
|
| + : AppBannerInfoBarAndroid.INSTALL_STATE_NOT_INSTALLED;
|
| + }
|
| +
|
| + @CalledByNative
|
| + /** Set the flag of whether the installation process has been started for the WebAPK. */
|
| + private void setWebApkInstallingState(boolean isInstalling) {
|
| + mIsInstallingWebApk = isInstalling;
|
| + }
|
| +
|
| + @CalledByNative
|
| + /** Sets the WebAPK package name. */
|
| + private void setWebApkPackageName(String webApkPackage) {
|
| + mWebApkPackage = webApkPackage;
|
| + }
|
| +
|
| + @CalledByNative
|
| + private static void showWebApkInstallFailureToast() {
|
| + ThreadUtils.runOnUiThread(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + Context applicationContext = ContextUtils.getApplicationContext();
|
| + Toast toast = Toast.makeText(applicationContext, R.string.fail_to_install_webapk,
|
| + Toast.LENGTH_SHORT);
|
| + toast.show();
|
| + }
|
| + });
|
| }
|
|
|
| private PackageManager getPackageManager(Context context) {
|
|
|