Chromium Code Reviews| 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..a863f5cae48f12f0d8c8d17b440971b5e1b6f611 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,16 @@ import android.os.Looper; |
| import org.chromium.base.ApplicationStatus; |
| import org.chromium.base.ContextUtils; |
| +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 |
| @@ -39,6 +43,15 @@ public class AppBannerInfoBarDelegateAndroid { |
| /** Monitors for application state changes. */ |
| private final ApplicationStatus.ApplicationStateListener mListener; |
| + /** |
| + * Indicates whether a request to install a WebPK has started. This flag is set before |
| + * the package name of the WebAPK is available. |
| + */ |
| + private boolean mIsWebApkInstalling; |
| + |
| + /** The package name of the WebAPK. */ |
| + private String mWebApkPackage; |
| + |
| /** Overrides the PackageManager for testing. */ |
| @VisibleForTesting |
| public static void setPackageManagerForTesting(PackageManager manager) { |
| @@ -79,9 +92,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 +107,17 @@ 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) { |
|
gone
2016/08/29 17:24:09
Output a Log.e here for bug reports to prevent thi
Xi Han
2016/08/29 17:55:46
Done.
|
| + return; |
| + } |
| + } |
| + } |
| + |
| private WindowAndroid.IntentCallback createIntentCallback(final AppData appData) { |
| return new WindowAndroid.IntentCallback() { |
| @Override |
| @@ -129,18 +151,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 || mIsWebApkInstalling) { |
| + 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 a installation process is started for the WebAPK. */ |
| + private void setWebApkInstallingState(boolean isInstalling) { |
| + mIsWebApkInstalling = 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) { |