Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4089)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java

Issue 2129043002: Split ShortcutHelper#addShortcut() into separate functions for each type of shortcut (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into webapk_builder_impl0 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/android/shortcut_helper.h » ('j') | chrome/browser/android/shortcut_helper.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
index eccfd16e4b410642453111e2b3bd04aaf9196db3..f4e7f9ce6d990579b9937244b0085124beb47bf5 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
@@ -28,13 +28,11 @@ import android.text.TextUtils;
import android.util.Base64;
import org.chromium.base.ApiCompatibilityUtils;
-import org.chromium.base.CommandLine;
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.blink_public.platform.WebDisplayMode;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.webapps.WebApkBuilder;
import org.chromium.chrome.browser.webapps.WebappAuthenticator;
@@ -131,43 +129,41 @@ public class ShortcutHelper {
}
/**
- * Called when we have to fire an Intent to add a shortcut to the home screen.
- * If the webpage indicated that it was capable of functioning as a webapp, it is added as a
- * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the
- * home screen as soon as the shortcut is created.
- *
- * This method must not be called on the UI thread.
+ * Installs a WebAPK. Returns true if successful.
+ */
+ @SuppressWarnings("unused")
+ @CalledByNative
+ private static boolean installWebApk(String url, final String name, String shortName,
+ String iconUrl, Bitmap icon, int displayMode, int orientation, long themeColor,
+ long backgroundColor, String manifestUrl) {
+ assert !ThreadUtils.runningOnUiThread();
+ Context context = ContextUtils.getApplicationContext();
+ WebApkBuilder apkBuilder = ((ChromeApplication) context).createWebApkBuilder();
+ if (apkBuilder == null) {
+ return false;
+ }
+ apkBuilder.buildWebApkAsync(url, GURLUtils.getOrigin(url), name, shortName, iconUrl, icon,
+ displayMode, orientation, themeColor, backgroundColor, manifestUrl);
+ return true;
+ }
+
+ /**
+ * Adds home screen shortcut which opens in a {@link WebappActivity}.
*/
@SuppressWarnings("unused")
@CalledByNative
- private static void addShortcut(String id, String url, final String userTitle, String name,
- String shortName, String iconUrl, Bitmap icon, int displayMode, int orientation,
- int source, long themeColor, long backgroundColor, String manifestUrl,
+ private static void addWebappShortcut(String id, String url, final String userTitle,
+ String name, String shortName, String iconUrl, Bitmap icon, int displayMode,
+ int orientation, int source, long themeColor, long backgroundColor,
final long callbackPointer) {
assert !ThreadUtils.runningOnUiThread();
Context context = ContextUtils.getApplicationContext();
- final Intent shortcutIntent;
- boolean isWebappCapable = (displayMode == WebDisplayMode.Standalone
- || displayMode == WebDisplayMode.Fullscreen);
- if (isWebappCapable) {
- if (CommandLine.getInstance().hasSwitch(ChromeSwitches.ENABLE_WEBAPK)) {
- WebApkBuilder apkBuilder = ((ChromeApplication) context).createWebApkBuilder();
- if (apkBuilder != null) {
- apkBuilder.buildWebApkAsync(url, GURLUtils.getOrigin(url), name, shortName,
- iconUrl, icon, displayMode, orientation, themeColor, backgroundColor,
- manifestUrl);
- return;
- }
- }
- shortcutIntent = createWebappShortcutIntent(id, sDelegate.getFullscreenAction(), url,
- getScopeFromUrl(url), name, shortName, icon, WEBAPP_SHORTCUT_VERSION,
- displayMode, orientation, themeColor, backgroundColor, iconUrl.isEmpty());
- shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url));
- } else {
- // Add the shortcut as a launcher icon to open in the browser Activity.
- shortcutIntent = createShortcutIntent(url);
- }
+ final Intent shortcutIntent =
+ createWebappShortcutIntent(id, sDelegate.getFullscreenAction(), url,
+ getScopeFromUrl(url), name, shortName, icon, WEBAPP_SHORTCUT_VERSION,
+ displayMode, orientation, themeColor, backgroundColor, iconUrl.isEmpty());
+ shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url));
// Always attach a source (one of add to home screen menu item, app banner, or unknown) to
// the intent. This allows us to distinguish where a shortcut was added from in metrics.
@@ -176,19 +172,45 @@ public class ShortcutHelper {
sDelegate.sendBroadcast(
context, createAddToHomeIntent(url, userTitle, icon, shortcutIntent));
- if (isWebappCapable) {
- // Store the webapp data so that it is accessible without the intent. Once this process
- // is complete, call back to native code to start the splash image download.
- WebappRegistry.registerWebapp(context, id,
- new WebappRegistry.FetchWebappDataStorageCallback() {
- @Override
- public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
- storage.updateFromShortcutIntent(shortcutIntent);
- nativeOnWebappDataStored(callbackPointer);
- }
+ // Store the webapp data so that it is accessible without the intent. Once this process
+ // is complete, call back to native code to start the splash image download.
+ WebappRegistry.registerWebapp(context, id,
+ new WebappRegistry.FetchWebappDataStorageCallback() {
+ @Override
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
+ storage.updateFromShortcutIntent(shortcutIntent);
+ nativeOnWebappDataStored(callbackPointer);
}
- );
- }
+ });
+
+ showAddedToHomescreenToast(userTitle);
+ }
+
+ /**
+ * Adds home screen shortcut which opens in the browser Activity.
+ */
+ @SuppressWarnings("unused")
+ @CalledByNative
+ private static void addBookmarkShortcut(String url, String userTitle, Bitmap icon, int source) {
+ assert !ThreadUtils.runningOnUiThread();
+
+ Context context = ContextUtils.getApplicationContext();
+ final Intent shortcutIntent = createShortcutIntent(url);
+ // Always attach a source (one of add to home screen menu item, app banner, or unknown) to
+ // the intent. This allows us to distinguish where a shortcut was added from in metrics.
+ shortcutIntent.putExtra(EXTRA_SOURCE, source);
+ shortcutIntent.setPackage(context.getPackageName());
Xi Han 2016/07/08 19:10:38 Please move line 199 to line 202 to createShortcut
Xi Han 2016/07/11 19:07:01 Same as above.
+
+ sDelegate.sendBroadcast(
+ context, createAddToHomeIntent(url, userTitle, icon, shortcutIntent));
+ showAddedToHomescreenToast(userTitle);
+ }
+
+ /**
+ * Show toast to alert user that the shortcut was added to the home screen.
+ */
+ private static void showAddedToHomescreenToast(final String title) {
+ assert !ThreadUtils.runningOnUiThread();
// Alert the user about adding the shortcut.
Handler handler = new Handler(Looper.getMainLooper());
@@ -197,7 +219,7 @@ public class ShortcutHelper {
public void run() {
Context applicationContext = ContextUtils.getApplicationContext();
String toastText =
- applicationContext.getString(R.string.added_to_homescreen, userTitle);
+ applicationContext.getString(R.string.added_to_homescreen, title);
Toast toast = Toast.makeText(applicationContext, toastText, Toast.LENGTH_SHORT);
toast.show();
}
@@ -511,9 +533,9 @@ public class ShortcutHelper {
}
/**
- * Returns the URL with all but the last component of its path removed. This serves as a proxy
- * for scope until the scope manifest member is available. This method assumes that the URL
- * passed in is a valid URL with a path that contains at least one "/".
+ * Returns the URL with all but the last component of its path removed. This is used if the
+ * Web Manifest does not specify a scope. This method assumes that the URL passed in is a
+ * valid URL with a path that contains at least one "/".
* @param url The url to convert to a scope.
* @return The scope.
*/
« no previous file with comments | « no previous file | chrome/browser/android/shortcut_helper.h » ('j') | chrome/browser/android/shortcut_helper.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698