OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.webapps; |
| 6 |
| 7 import android.annotation.SuppressLint; |
| 8 import android.content.Intent; |
| 9 import android.content.pm.PackageManager; |
| 10 import android.content.pm.ResolveInfo; |
| 11 import android.graphics.Bitmap; |
| 12 |
| 13 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.chrome.browser.ChromeApplication; |
| 15 |
| 16 import java.util.List; |
| 17 |
| 18 /** |
| 19 * This class handles the adding of shortcuts to the Android Home Screen. |
| 20 */ |
| 21 public class ChromeShortcutManager { |
| 22 // There is no public string defining this intent so if Home changes the val
ue, we |
| 23 // have to update this string. |
| 24 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.
INSTALL_SHORTCUT"; |
| 25 |
| 26 private static ChromeShortcutManager sInstance; |
| 27 |
| 28 /* Returns the singleton instance of ChromeShortcutManager, creating it if n
eeded. */ |
| 29 public static ChromeShortcutManager getInstance() { |
| 30 if (sInstance == null) { |
| 31 sInstance = ((ChromeApplication) ContextUtils.getApplicationContext(
)) |
| 32 .createChromeShortcutManager(); |
| 33 } |
| 34 return sInstance; |
| 35 } |
| 36 |
| 37 /** |
| 38 * Creates an intent that will add a shortcut to the home screen. |
| 39 * @param title Title of the shortcut. |
| 40 * @param icon Image that represents the shortcut. |
| 41 * @param shortcutIntent Intent to fire when the shortcut is activated. |
| 42 * @return Intent for the shortcut. |
| 43 */ |
| 44 public static Intent createAddToHomeIntent(String title, Bitmap icon, Intent
shortcutIntent) { |
| 45 Intent i = new Intent(INSTALL_SHORTCUT); |
| 46 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
| 47 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); |
| 48 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); |
| 49 return i; |
| 50 } |
| 51 |
| 52 /** |
| 53 * Add a shortcut to the home screen. |
| 54 * @param title Title of the shortcut. |
| 55 * @param icon Image that represents the shortcut. |
| 56 * @param shortcutIntent Intent to fire when the shortcut is activated. |
| 57 */ |
| 58 public void addShortcutToHomeScreen(String title, Bitmap icon, Intent shortc
utIntent) { |
| 59 Intent intent = createAddToHomeIntent(title, icon, shortcutIntent); |
| 60 ContextUtils.getApplicationContext().sendBroadcast(intent); |
| 61 } |
| 62 |
| 63 // TODO(crbug.com/635567): Fix this properly. |
| 64 @SuppressLint("WrongConstant") |
| 65 public boolean canAddShortcutToHomescreen() { |
| 66 PackageManager pm = ContextUtils.getApplicationContext().getPackageManag
er(); |
| 67 Intent i = new Intent(INSTALL_SHORTCUT); |
| 68 List<ResolveInfo> receivers = |
| 69 pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS)
; |
| 70 return !receivers.isEmpty(); |
| 71 } |
| 72 |
| 73 public boolean shouldShowToastWhenAddingShortcut() { |
| 74 return true; |
| 75 } |
| 76 } |
OLD | NEW |