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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/webapps/ChromeShortcutManager.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/webapps/ChromeShortcutManager.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/ChromeShortcutManager.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/ChromeShortcutManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..1bc1e745ea3c4e4e7744cede679cb24fe8a6af9e
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/ChromeShortcutManager.java
@@ -0,0 +1,76 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.webapps;
+
+import android.annotation.SuppressLint;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.graphics.Bitmap;
+
+import org.chromium.base.ContextUtils;
+import org.chromium.chrome.browser.ChromeApplication;
+
+import java.util.List;
+
+/**
+ * This class handles the adding of shortcuts to the Android Home Screen.
+ */
+public class ChromeShortcutManager {
+ // 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";
+
+ private static ChromeShortcutManager sInstance;
+
+ /* Returns the singleton instance of ChromeShortcutManager, creating it if needed. */
+ public static ChromeShortcutManager getInstance() {
+ if (sInstance == null) {
+ sInstance = ((ChromeApplication) ContextUtils.getApplicationContext())
+ .createChromeShortcutManager();
+ }
+ return sInstance;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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.
+ */
+ public void addShortcutToHomeScreen(String title, Bitmap icon, Intent shortcutIntent) {
+ Intent intent = createAddToHomeIntent(title, icon, shortcutIntent);
+ ContextUtils.getApplicationContext().sendBroadcast(intent);
+ }
+
+ // TODO(crbug.com/635567): Fix this properly.
+ @SuppressLint("WrongConstant")
+ public boolean canAddShortcutToHomescreen() {
+ PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
+ Intent i = new Intent(INSTALL_SHORTCUT);
+ List<ResolveInfo> receivers =
+ pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS);
+ return !receivers.isEmpty();
+ }
+
+ public boolean shouldShowToastWhenAddingShortcut() {
+ return true;
+ }
+}
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/banners/AppBannerManager.java ('k') | chrome/android/java_sources.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698