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