Chromium Code Reviews| 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..54c12843919cc5f6db25cc1d97f33255ecbc3185 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/ChromeShortcutManager.java |
| @@ -0,0 +1,74 @@ |
| +// 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.Context; |
| +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 helps to add a shortcut to the Android Home Screen. It also tells if |
| + * addShortcutToHomeScreen is supported or not. |
| + */ |
| +public class ChromeShortcutManager { |
| + private static ChromeShortcutManager sInstance; |
| + protected static Context sContext; |
|
dominickn
2017/02/14 03:04:21
I had a look, and no other Java file in the reposi
Marti Wong
2017/02/14 04:37:51
Done.
|
| + |
| + // There is no public string defining this intent so if Home changes the value, we |
| + // have to update this string. |
| + public static final String INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; |
|
dominickn
2017/02/14 03:04:21
Can this be private / protected?
Marti Wong
2017/02/14 04:37:51
changed to private. thanks~!
|
| + |
| + /* Returns the singleton instance of ChromeShortcutManager, creating it if needed. */ |
| + public static ChromeShortcutManager getInstance() { |
| + if (sInstance == null) { |
| + sContext = ContextUtils.getApplicationContext(); |
| + sInstance = ((ChromeApplication) sContext.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; |
| + } |
| + |
| + public void addShortcutToHomeScreen(String title, Bitmap icon, Intent shortcutIntent) { |
| + Intent intent = createAddToHomeIntent(title, icon, shortcutIntent); |
| + sContext.sendBroadcast(intent); |
| + } |
| + |
| + // TODO(crbug.com/635567): Fix this properly. |
| + @SuppressLint("WrongConstant") |
| + public boolean canAddShortcutToHomescreen() { |
| + PackageManager pm = sContext.getPackageManager(); |
| + Intent i = new Intent(INSTALL_SHORTCUT); |
| + List<ResolveInfo> receivers = |
| + pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS); |
| + return !receivers.isEmpty(); |
| + } |
| + |
| + public boolean shouldShowAddedToHomescreenToast() { |
| + return true; |
| + } |
| +} |