Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / ShortcutHelper.java
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.io.ByteArrayOutputStream;
18 import java.util.UUID;
19
20 /**
21  * This is a helper class to create shortcuts on the Android home screen.
22  */
23 public class ShortcutHelper {
24     public static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp_icon";
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_TITLE = "org.chromium.chrome.browser.webapp_title";
28     public static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_url";
29
30     private static String sFullScreenAction;
31
32     /**
33      * Sets the class name used when launching the shortcuts.
34      * @param fullScreenAction Class name of the fullscreen Activity.
35      */
36     public static void setFullScreenAction(String fullScreenAction) {
37         sFullScreenAction = fullScreenAction;
38     }
39
40     /**
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.
45      */
46     public static void addShortcut(Context appContext, Tab tab, String userRequestedTitle) {
47         if (TextUtils.isEmpty(sFullScreenAction)) {
48             Log.e("ShortcutHelper", "ShortcutHelper is uninitialized.  Aborting.");
49             return;
50         }
51         ActivityManager am = (ActivityManager) appContext.getSystemService(
52                 Context.ACTIVITY_SERVICE);
53         nativeAddShortcut(tab.getNativePtr(), userRequestedTitle, am.getLauncherLargeIconSize());
54     }
55
56     /**
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.
61      */
62     @SuppressWarnings("unused")
63     @CalledByNative
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;
67
68         Intent shortcutIntent;
69         if (isWebappCapable) {
70             // Encode the favicon as a base64 string (Launcher drops Bitmaps in the Intent).
71             String encodedIcon = "";
72             if (favicon != null) {
73                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
74                 favicon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
75                 byte[] byteArray = byteArrayOutputStream.toByteArray();
76                 encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT);
77             }
78
79             // Add the shortcut as a launcher icon for a full-screen Activity.
80             shortcutIntent = new Intent();
81             shortcutIntent.setAction(sFullScreenAction);
82             shortcutIntent.putExtra(EXTRA_ICON, encodedIcon);
83             shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString());
84             shortcutIntent.putExtra(EXTRA_TITLE, title);
85             shortcutIntent.putExtra(EXTRA_URL, url);
86
87             // The only reason we convert to a String here is because Android inexplicably eats a
88             // byte[] when adding the shortcut -- the Bundle received by the launched Activity even
89             // lacks the key for the extra.
90             byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
91             String encodedMac = Base64.encodeToString(mac, Base64.DEFAULT);
92             shortcutIntent.putExtra(EXTRA_MAC, encodedMac);
93         } else {
94             // Add the shortcut as a launcher icon to open in the browser Activity.
95             shortcutIntent = BookmarkUtils.createShortcutIntent(context, url);
96         }
97
98         shortcutIntent.setPackage(context.getPackageName());
99         context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, shortcutIntent, title,
100                 favicon, red, green, blue));
101
102         // User is sent to the homescreen as soon as the shortcut is created.
103         Intent homeIntent = new Intent(Intent.ACTION_MAIN);
104         homeIntent.addCategory(Intent.CATEGORY_HOME);
105         homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
106         context.startActivity(homeIntent);
107     }
108
109     private static native void nativeAddShortcut(long tabAndroidPtr, String userRequestedTitle,
110             int launcherLargeIconSize);
111 }