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..0098f853ece2b7cc094158f1b2deb527e83f77ae 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 |
| @@ -13,13 +13,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 +42,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 = false; |
|
gone
2016/08/26 21:12:26
booleans default to false, so you don't need to in
Xi Han
2016/08/29 14:44:52
Done.
|
| + |
| + /** The package name of the WebAPK. */ |
| + private String mWebApkPackage; |
| + |
| /** Overrides the PackageManager for testing. */ |
| @VisibleForTesting |
| public static void setPackageManagerForTesting(PackageManager manager) { |
| @@ -79,9 +91,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 +106,11 @@ public class AppBannerInfoBarDelegateAndroid { |
| } |
| } |
| + void openApp(Context context, String packageName) { |
| + Intent launchIntent = getPackageManager(context).getLaunchIntentForPackage(packageName); |
| + if (launchIntent != null) context.startActivity(launchIntent); |
|
gone
2016/08/26 21:12:26
To be safe, use a try/catch when starting the Acti
Xi Han
2016/08/29 14:44:52
Done.
|
| + } |
| + |
| private WindowAndroid.IntentCallback createIntentCallback(final AppData appData) { |
| return new WindowAndroid.IntentCallback() { |
| @Override |
| @@ -129,18 +144,58 @@ public class AppBannerInfoBarDelegateAndroid { |
| } |
| @CalledByNative |
|
pkotwicz
2016/08/26 22:31:25
Can this function return void too?
Xi Han
2016/08/29 14:44:52
Yes, this function doesn't need to return a boolea
|
| + private boolean openWebApk(String packageName) { |
| + Context context = ContextUtils.getApplicationContext(); |
| + PackageManager packageManager = getPackageManager(context); |
| + |
| + if (InstallerDelegate.isInstalled(packageManager, packageName)) { |
| + mWebApkPackage = null; |
| + openApp(context, packageName); |
| + } |
| + return true; |
| + } |
| + |
| + @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) { |