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 helps to add a shortcut to the Android Home Screen. It also tells
if |
| 20 * addShortcutToHomeScreen is supported or not. |
| 21 */ |
| 22 public class ChromeShortcutManager { |
| 23 private static ChromeShortcutManager sInstance; |
| 24 |
| 25 // There is no public string defining this intent so if Home changes the val
ue, we |
| 26 // have to update this string. |
| 27 private static final String INSTALL_SHORTCUT = "com.android.launcher.action.
INSTALL_SHORTCUT"; |
| 28 |
| 29 /* Returns the singleton instance of ChromeShortcutManager, creating it if n
eeded. */ |
| 30 public static ChromeShortcutManager getInstance() { |
| 31 if (sInstance == null) { |
| 32 sInstance = ((ChromeApplication) ContextUtils.getApplicationContext(
) |
| 33 .getApplicationContext()) |
| 34 .createChromeShortcutManager(); |
| 35 } |
| 36 return sInstance; |
| 37 } |
| 38 |
| 39 /** |
| 40 * Creates an intent that will add a shortcut to the home screen. |
| 41 * @param title Title of the shortcut. |
| 42 * @param icon Image that represents the shortcut. |
| 43 * @param shortcutIntent Intent to fire when the shortcut is activated. |
| 44 * @return Intent for the shortcut. |
| 45 */ |
| 46 public static Intent createAddToHomeIntent(String title, Bitmap icon, Intent
shortcutIntent) { |
| 47 Intent i = new Intent(INSTALL_SHORTCUT); |
| 48 i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); |
| 49 i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title); |
| 50 i.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); |
| 51 return i; |
| 52 } |
| 53 |
| 54 public void addShortcutToHomeScreen(String title, Bitmap icon, Intent shortc
utIntent) { |
| 55 Intent intent = createAddToHomeIntent(title, icon, shortcutIntent); |
| 56 ContextUtils.getApplicationContext().sendBroadcast(intent); |
| 57 } |
| 58 |
| 59 // TODO(crbug.com/635567): Fix this properly. |
| 60 @SuppressLint("WrongConstant") |
| 61 public boolean canAddShortcutToHomescreen() { |
| 62 PackageManager pm = ContextUtils.getApplicationContext().getPackageManag
er(); |
| 63 Intent i = new Intent(INSTALL_SHORTCUT); |
| 64 List<ResolveInfo> receivers = |
| 65 pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS)
; |
| 66 return !receivers.isEmpty(); |
| 67 } |
| 68 |
| 69 public boolean shouldShowAddedToHomescreenToast() { |
| 70 return true; |
| 71 } |
| 72 } |
OLD | NEW |