- add sources.
[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.os.Bundle;
12 import android.text.TextUtils;
13 import android.util.Base64;
14 import android.util.Log;
15
16 import org.chromium.base.CalledByNative;
17 import org.chromium.chrome.browser.BookmarkUtils;
18
19 import java.util.UUID;
20
21 /**
22  * This is a helper class to create shortcuts on the Android home screen.
23  */
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";
28
29     private static String sFullScreenAction;
30
31     /**
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.
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, TabBase 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 = 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());
75
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);
82         } else {
83             // Add the shortcut as a launcher icon to open in the browser Activity.
84             shortcutIntent = BookmarkUtils.createShortcutIntent(context, url);
85         }
86
87         shortcutIntent.setPackage(context.getPackageName());
88         context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, shortcutIntent, title,
89                 favicon, red, green, blue));
90
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);
96     }
97
98     private static native void nativeAddShortcut(int tabAndroidPtr, String userRequestedTitle,
99             int launcherLargeIconSize);
100 }