Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java |
index 2b3a2c9fb66f47a8b1437241f5ee51515d29292a..bfcafdc462d7e873a37f8143b25006c02408fd00 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java |
@@ -4,21 +4,28 @@ |
package org.chromium.chrome.browser.webapps; |
+import android.app.PendingIntent; |
import android.content.Context; |
import android.content.Intent; |
import android.content.pm.PackageManager; |
import android.net.Uri; |
import android.os.Build; |
+import android.os.Bundle; |
import android.os.Looper; |
+import android.text.TextUtils; |
import org.chromium.base.ApplicationState; |
import org.chromium.base.ApplicationStatus; |
import org.chromium.base.ContentUriUtils; |
import org.chromium.base.ContextUtils; |
import org.chromium.base.annotations.CalledByNative; |
+import org.chromium.chrome.browser.ChromeApplication; |
+import org.chromium.chrome.browser.ChromeSwitches; |
import org.chromium.chrome.browser.ShortcutHelper; |
import org.chromium.chrome.browser.banners.InstallerDelegate; |
+import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
import org.chromium.chrome.browser.util.IntentUtils; |
+import org.chromium.components.variations.VariationsAssociatedData; |
import java.io.File; |
@@ -27,9 +34,12 @@ import java.io.File; |
* Contains functionality to install / update WebAPKs. |
* This Java object is created by and owned by the native WebApkInstaller. |
*/ |
-public class WebApkInstaller { |
+public class WebApkInstaller implements WebApkInstallClientDelegate.Observer { |
private static final String TAG = "WebApkInstaller"; |
+ /** Flag to enable installing WebAPKs from Play Store. */ |
+ private static final String PLAY_INSTALL_WEBAPKS = "play_install_webapks"; |
+ |
/** The WebAPK's package name. */ |
private String mWebApkPackageName; |
@@ -42,11 +52,45 @@ public class WebApkInstaller { |
/** Indicates whether to install or update a WebAPK. */ |
private boolean mIsInstall; |
+ /** Talks to Play Store to install WebAPKs. */ |
+ private static WebApkInstallClientDelegate sWebApkInstallClientDelegate; |
+ |
+ /** |
+ * Indicates whether the static {@link sWebApkInstallClientDelegate} has been initialized. It |
+ * prevents from initializing the client delegate multiple time when the delegate is null. |
+ */ |
+ private static boolean sIsWebApkInstallClientDelegateInitialized; |
+ |
/** Weak pointer to the native WebApkInstaller. */ |
private long mNativePointer; |
private WebApkInstaller(long nativePtr) { |
mNativePointer = nativePtr; |
+ if (!sIsWebApkInstallClientDelegateInitialized) { |
+ sIsWebApkInstallClientDelegateInitialized = true; |
+ ChromeApplication application = |
+ (ChromeApplication) ContextUtils.getApplicationContext(); |
+ sWebApkInstallClientDelegate = application.createWebApkInstallClientDelegate(); |
+ } |
+ } |
+ |
+ /** Gets the WebApkInstallClientDelegate. */ |
+ public static WebApkInstallClientDelegate getWebApkInstallClientDelegate() { |
+ return sWebApkInstallClientDelegate; |
+ } |
+ |
+ /** Return whether installing WebAPKs from Play Store is enabled. */ |
+ public static boolean enablePlayInstallWebApk() { |
+ if (!ChromeWebApkHost.isEnabled()) return false; |
+ return TextUtils.equals(VariationsAssociatedData.getVariationParamValue( |
+ ChromeSwitches.ENABLE_WEBAPK, PLAY_INSTALL_WEBAPKS), "true"); |
+ } |
+ |
+ @Override |
+ public void onInstallStateChanged(boolean success) { |
+ if (mNativePointer != 0) { |
+ nativeOnInstallFinished(mNativePointer, success); |
+ } |
} |
@CalledByNative |
@@ -55,6 +99,11 @@ public class WebApkInstaller { |
} |
@CalledByNative |
+ private boolean hasWebApkInstallClientDelegate() { |
+ return sWebApkInstallClientDelegate != null; |
+ } |
+ |
+ @CalledByNative |
private void destroy() { |
ApplicationStatus.unregisterApplicationStateListener(mListener); |
mNativePointer = 0; |
@@ -86,6 +135,46 @@ public class WebApkInstaller { |
} |
/** |
+ * Install a WebAPK from Play Store and monitors the installation. |
+ * @param packageName The package name of the WebAPK to install. |
+ * @param version The version of WebAPK to install. |
+ * @param title The title of the WebAPK to display during installation. |
+ * @param wamToken The token from WebAPK Minter Server. |
+ */ |
+ @CalledByNative |
+ private void installWebApkFromPlayStoreAsyncAndMonitorInstallation( |
+ final String packageName, final int version, final String title, |
+ final String wamToken) { |
+ if (sWebApkInstallClientDelegate == null) return; |
+ |
+ mWebApkPackageName = packageName; |
+ sWebApkInstallClientDelegate.installAsync(this, packageName, createInstallOptions( |
+ version, title, createNotificationPendingIntent(), wamToken)); |
+ } |
+ |
+ /** Returns a bundle with all the installation options. **/ |
+ private Bundle createInstallOptions(int version, String title, PendingIntent notificationIntent, |
+ String wamToken) { |
+ Bundle installOptions = new Bundle(); |
+ installOptions.putInt("version_number", version); |
+ installOptions.putString("title", title); |
+ installOptions.putParcelable("notification_intent", notificationIntent); |
+ installOptions.putString("wam_token", wamToken); |
+ return installOptions; |
+ } |
+ |
+ /** |
+ * Creates a PendingIntent used to redirect users if notifications are clicked during the |
+ * installation. |
+ */ |
+ private PendingIntent createNotificationPendingIntent() { |
+ Context context = ContextUtils.getApplicationContext(); |
+ Intent intent = new Intent(context, ChromeLauncherActivity.class); |
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
+ return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
+ } |
+ |
+ /** |
* Send intent to Android to show prompt and install downloaded WebAPK. |
* @param filePath File to install. |
*/ |