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.
5 package org.chromium.chrome.browser;
7 import android.app.ActivityManager;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.graphics.Bitmap;
11 import android.os.Bundle;
12 import android.text.TextUtils;
13 import android.util.Base64;
14 import android.util.Log;
16 import org.chromium.base.CalledByNative;
17 import org.chromium.chrome.browser.BookmarkUtils;
19 import java.util.UUID;
22 * This is a helper class to create shortcuts on the Android home screen.
24 public class ShortcutHelper {
25 public static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_id";
26 public static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_mac";
27 public static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_url";
29 private static String sFullScreenAction;
32 * Sets the class names used when launching the shortcuts.
33 * @param browserName Class name of the browser Activity.
34 * @param fullScreenName Class name of the fullscreen Activity.
36 public static void setFullScreenAction(String fullScreenAction) {
37 sFullScreenAction = fullScreenAction;
41 * Adds a shortcut for the current Tab.
42 * @param appContext The application context.
43 * @param tab Tab to create a shortcut for.
44 * @param userRequestedTitle Updated title for the shortcut.
46 public static void addShortcut(Context appContext, TabBase tab, String userRequestedTitle) {
47 if (TextUtils.isEmpty(sFullScreenAction)) {
48 Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting.");
51 ActivityManager am = (ActivityManager) appContext.getSystemService(
52 Context.ACTIVITY_SERVICE);
53 nativeAddShortcut(tab.getNativePtr(), userRequestedTitle, am.getLauncherLargeIconSize());
57 * Called when we have to fire an Intent to add a shortcut to the homescreen.
58 * If the webpage indicated that it was capable of functioning as a webapp, it is added as a
59 * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the
60 * homescreen as soon as the shortcut is created.
62 @SuppressWarnings("unused")
64 private static void addShortcut(Context context, String url, String title, Bitmap favicon,
65 int red, int green, int blue, boolean isWebappCapable) {
66 assert sFullScreenAction != null;
68 Intent shortcutIntent = null;
69 if (isWebappCapable) {
70 // Add the shortcut as a launcher icon for a full-screen Activity.
71 shortcutIntent = new Intent();
72 shortcutIntent.setAction(sFullScreenAction);
73 shortcutIntent.putExtra(EXTRA_URL, url);
74 shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString());
76 // The only reason we convert to a String here is because Android inexplicably eats a
77 // byte[] when adding the shortcut -- the Bundle received by the launched Activity even
78 // lacks the key for the extra.
79 byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
80 String encodedMac = Base64.encodeToString(mac, Base64.DEFAULT);
81 shortcutIntent.putExtra(EXTRA_MAC, encodedMac);
83 // Add the shortcut as a launcher icon to open in the browser Activity.
84 shortcutIntent = BookmarkUtils.createShortcutIntent(context, url);
87 shortcutIntent.setPackage(context.getPackageName());
88 context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, shortcutIntent, title,
89 favicon, red, green, blue));
91 // User is sent to the homescreen as soon as the shortcut is created.
92 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
93 homeIntent.addCategory(Intent.CATEGORY_HOME);
94 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95 context.startActivity(homeIntent);
98 private static native void nativeAddShortcut(int tabAndroidPtr, String userRequestedTitle,
99 int launcherLargeIconSize);