1 | // Copyright 2013 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; |
6 | |
7 | import android.content.Context; |
8 | import android.content.Intent; |
9 | import android.graphics.Bitmap; |
10 | import android.util.Log; |
11 | |
12 | import org.chromium.base.CalledByNative; |
13 | import org.chromium.chrome.browser.BookmarkUtils; |
14 | |
15 | import java.util.UUID; |
16 | |
17 | /** |
18 | * This is a helper class to create shortcuts on the Android home screen. |
19 | */ |
20 | public class ShortcutHelper { |
21 | public static final String EXTRA_ID = "webapp_id"; |
22 | public static final String EXTRA_URL = "webapp_url"; |
23 | |
24 | private static String sWebappActivityPackageName; |
25 | private static String sWebappActivityClassName; |
26 | |
27 | /** |
28 | * Sets the Activity that gets launched when a webapp shortcut is clicked. |
29 | * @param packageName Package of the Activity to launch. |
30 | * @param className Class name of the Activity to launch. |
31 | */ |
32 | public static void setWebappActivityInfo(String packageName, String className) { |
33 | sWebappActivityPackageName = packageName; |
34 | sWebappActivityClassName = className; |
35 | } |
36 | |
37 | /** |
38 | * Adds a shortcut for the current Tab. |
39 | * @param tab Tab to create a shortcut for. |
40 | */ |
41 | public static void addShortcut(TabBase tab) { |
42 | if (sWebappActivityPackageName == null || sWebappActivityClassName == null) { |
43 | Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting."); |
44 | return; |
45 | } |
46 | nativeAddShortcut(tab.getNativePtr()); |
47 | } |
48 | |
49 | /** |
50 | * Called when we have to fire an Intent to add a shortcut to the homescreen. |
51 | * If the webpage indicated that it was capable of functioning as a webapp, it is added as a |
52 | * shortcut to a webapp Activity rather than as a general bookmark. |
53 | */ |
54 | @SuppressWarnings("unused") |
55 | @CalledByNative |
56 | private static void addShortcut(Context context, String url, String title, Bitmap favicon, |
57 | int red, int green, int blue, boolean isWebappCapable) { |
58 | Intent addIntent; |
59 | if (isWebappCapable && !sWebappActivityPackageName.isEmpty() |
60 | && !sWebappActivityClassName.isEmpty()) { |
61 | // Add the shortcut as a launcher icon for a Webapp. |
62 | String id = UUID.randomUUID().toString(); |
63 | |
64 | Intent shortcutIntent = new Intent(); |
65 | shortcutIntent.setClassName(sWebappActivityPackageName, sWebappActivityClassName); |
66 | shortcutIntent.putExtra(EXTRA_URL, url); |
67 | shortcutIntent.putExtra(EXTRA_ID, id); |
68 | |
69 | addIntent = BookmarkUtils.createAddToHomeIntent(context, shortcutIntent, title, favicon, |
70 | red, green, blue); |
71 | } else { |
72 | // Add the shortcut as a regular bookmark. |
73 | addIntent = BookmarkUtils.createAddToHomeIntent(context, url, title, favicon, red, |
74 | green, blue); |
75 | } |
76 | context.sendBroadcast(addIntent); |
77 | } |
78 | |
79 | private static native void nativeAddShortcut(int tabAndroidPtr); |
80 | } |