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

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

Issue 2689993002: Refactor the INSTALL_SHORTCUT broadcast code into ChromeShortcutManager (Closed)
Patch Set: Change according to review comments. Created 3 years, 10 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
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 c8356b73ffd55880f02a18bf3581ac97bab3d0c8..dac06697673f0c74635c5823fdecf9d1082d2981 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
@@ -4,7 +4,6 @@
package org.chromium.chrome.browser;
-import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
@@ -12,7 +11,6 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
@@ -35,6 +33,8 @@ 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.util.IntentUtils;
+import org.chromium.chrome.browser.webapps.ChromeShortcutManager;
import org.chromium.chrome.browser.webapps.ChromeWebApkHost;
import org.chromium.chrome.browser.webapps.WebApkInfo;
import org.chromium.chrome.browser.webapps.WebappActivity;
@@ -60,7 +60,7 @@ public class ShortcutHelper {
public static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp_icon";
public static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_id";
public static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_mac";
- // EXTRA_TITLE is present for backward compatibility reasons
+ // EXTRA_TITLE is present for backward compatibility reasons.
public static final String EXTRA_TITLE = "org.chromium.chrome.browser.webapp_title";
public static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp_name";
public static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser.webapp_short_name";
@@ -82,6 +82,10 @@ public class ShortcutHelper {
public static final String EXTRA_WEBAPK_PACKAGE_NAME =
"org.chromium.chrome.browser.webapk_package_name";
+ /** Used for the callback intent when using the new shortcut API. */
+ public static final String SHORTCUT_TOAST_CATEGORY =
+ "com.google.intent.category.SHORTCUT_TOAST";
+
// When a new field is added to the intent, this version should be incremented so that it will
// be correctly populated into the WebappRegistry/WebappDataStorage.
public static final int WEBAPP_SHORTCUT_VERSION = 2;
@@ -91,10 +95,6 @@ public class ShortcutHelper {
private static final String TAG = "ShortcutHelper";
- // There is no public string defining this intent so if Home changes the value, we
- // have to update this string.
- private static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
-
// The activity class used for launching a WebApk.
private static final String WEBAPK_MAIN_ACTIVITY = "org.chromium.webapk.shell_apk.MainActivity";
@@ -106,15 +106,17 @@ public class ShortcutHelper {
private static final float GENERATED_ICON_PADDING_RATIO = 1.0f / 12.0f;
private static final float GENERATED_ICON_FONT_SIZE_RATIO = 1.0f / 3.0f;
- /** Broadcasts Intents out Android for adding the shortcut. */
+ /** Helper for generating home screen shortcuts. */
public static class Delegate {
/**
- * Broadcasts an intent to all interested BroadcastReceivers.
- * @param context The Context to use.
- * @param intent The intent to broadcast.
+ * Request Android to add a shortcut to the home screen.
+ * @param title Title of the shortcut.
+ * @param icon Image that represents the shortcut.
+ * @param intent Intent to fire when the shortcut is activated.
*/
- public void sendBroadcast(Context context, Intent intent) {
- context.sendBroadcast(intent);
+ public void addShortcutToHomescreen(String title, Bitmap icon, Intent shortcutIntent) {
+ ChromeShortcutManager.getInstance().addShortcutToHomeScreen(
+ title, icon, shortcutIntent);
}
/**
@@ -165,9 +167,7 @@ public class ShortcutHelper {
}
@Override
protected void onPostExecute(final Intent resultIntent) {
- Context context = ContextUtils.getApplicationContext();
- sDelegate.sendBroadcast(
- context, createAddToHomeIntent(userTitle, icon, resultIntent));
+ sDelegate.addShortcutToHomescreen(userTitle, icon, resultIntent);
// 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
@@ -180,8 +180,9 @@ public class ShortcutHelper {
nativeOnWebappDataStored(callbackPointer);
}
});
-
- showAddedToHomescreenToast(userTitle);
+ if (ChromeShortcutManager.getInstance().shouldShowToastWhenAddingShortcut()) {
+ showAddedToHomescreenToast(userTitle);
+ }
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@@ -198,7 +199,8 @@ public class ShortcutHelper {
Intent i = new Intent();
i.setClassName(packageName, WEBAPK_MAIN_ACTIVITY);
i.addCategory(Intent.CATEGORY_LAUNCHER);
- context.sendBroadcast(createAddToHomeIntent(shortcutTitle, bitmap, i));
+ context.sendBroadcast(
+ ChromeShortcutManager.createAddToHomeIntent(shortcutTitle, bitmap, i));
} catch (NameNotFoundException e) {
e.printStackTrace();
}
@@ -209,14 +211,17 @@ public class ShortcutHelper {
*/
@SuppressWarnings("unused")
@CalledByNative
- private static void addShortcut(String url, String userTitle, Bitmap icon, int source) {
+ private static void addShortcut(
+ String id, String url, String userTitle, Bitmap icon, int source) {
Context context = ContextUtils.getApplicationContext();
final Intent shortcutIntent = createShortcutIntent(url);
+ shortcutIntent.putExtra(EXTRA_ID, id);
shortcutIntent.putExtra(EXTRA_SOURCE, source);
shortcutIntent.setPackage(context.getPackageName());
- sDelegate.sendBroadcast(
- context, createAddToHomeIntent(userTitle, icon, shortcutIntent));
- showAddedToHomescreenToast(userTitle);
+ sDelegate.addShortcutToHomescreen(userTitle, icon, shortcutIntent);
+ if (ChromeShortcutManager.getInstance().shouldShowToastWhenAddingShortcut()) {
+ showAddedToHomescreenToast(userTitle);
+ }
}
/**
@@ -229,6 +234,24 @@ public class ShortcutHelper {
}
/**
+ * Show toast when getting the callback intent by the launcher after adding shortcut by using
+ * the new shortcut API.
+ */
+ public static void showAddedToHomescreenToastFromIntent(Intent intent) {
+ String title = IntentUtils.safeGetStringExtra(intent, Intent.EXTRA_SHORTCUT_NAME);
+ showAddedToHomescreenToast(title);
+ }
+
+ /**
+ * Determine if it is a callback intent (which requests for a show-toast), used in the new
+ * shortcut API.
+ */
+ public static boolean isShowToastIntent(Intent intent) {
+ if (intent == null || intent.getCategories() == null) return false;
+ return intent.getCategories().contains(SHORTCUT_TOAST_CATEGORY);
+ }
+
+ /**
* Shows toast notifying user that a WebAPK install is already in progress when user tries to
* queue a new install for the same WebAPK.
*/
@@ -273,22 +296,6 @@ public class ShortcutHelper {
}
/**
- * Creates an intent that will add a shortcut to the home screen.
- * @param title Title of the shortcut.
- * @param icon Image that represents the shortcut.
- * @param shortcutIntent Intent to fire when the shortcut is activated.
- * @return Intent for the shortcut.
- */
- public static Intent createAddToHomeIntent(String title, Bitmap icon,
- Intent shortcutIntent) {
- Intent i = new Intent(INSTALL_SHORTCUT);
- i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
- i.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
- return i;
- }
-
- /**
* Creates a shortcut to launch a web app on the home screen.
* @param id Id of the web app.
* @param action Intent action to open a full screen activity.
@@ -358,22 +365,15 @@ public class ShortcutHelper {
/**
* Utility method to check if a shortcut can be added to the home screen.
- * @param context Context used to get the package manager.
* @return if a shortcut can be added to the home screen under the current profile.
*/
- // TODO(crbug.com/635567): Fix this properly.
- @SuppressLint("WrongConstant")
- public static boolean isAddToHomeIntentSupported(Context context) {
- PackageManager pm = context.getPackageManager();
- Intent i = new Intent(INSTALL_SHORTCUT);
- List<ResolveInfo> receivers = pm.queryBroadcastReceivers(
- i, PackageManager.GET_INTENT_FILTERS);
- return !receivers.isEmpty();
+ public static boolean isAddToHomeIntentSupported() {
+ return ChromeShortcutManager.getInstance().canAddShortcutToHomescreen();
}
/**
* Returns whether the given icon matches the size requirements to be used on the home screen.
- * @param width Icon width, in pixels.
+ * @param width Icon width, in pixels.
* @param height Icon height, in pixels.
* @return whether the given icon matches the size requirements to be used on the home screen.
*/
@@ -436,10 +436,10 @@ public class ShortcutHelper {
* Generates a generic icon to be used in the launcher. This is just a rounded rectangle with
* a letter in the middle taken from the website's domain name.
*
- * @param url URL of the shortcut.
- * @param red Red component of the dominant icon color.
+ * @param url URL of the shortcut.
+ * @param red Red component of the dominant icon color.
* @param green Green component of the dominant icon color.
- * @param blue Blue component of the dominant icon color.
+ * @param blue Blue component of the dominant icon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
@CalledByNative

Powered by Google App Engine
This is Rietveld 408576698