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