| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.app.ActivityManager; | 7 import android.app.ActivityManager; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 115 } |
| 116 | 116 |
| 117 /** | 117 /** |
| 118 * Called when we have to fire an Intent to add a shortcut to the homescreen
. | 118 * Called when we have to fire an Intent to add a shortcut to the homescreen
. |
| 119 * If the webpage indicated that it was capable of functioning as a webapp,
it is added as a | 119 * If the webpage indicated that it was capable of functioning as a webapp,
it is added as a |
| 120 * shortcut to a webapp Activity rather than as a general bookmark. User is
sent to the | 120 * shortcut to a webapp Activity rather than as a general bookmark. User is
sent to the |
| 121 * homescreen as soon as the shortcut is created. | 121 * homescreen as soon as the shortcut is created. |
| 122 */ | 122 */ |
| 123 @SuppressWarnings("unused") | 123 @SuppressWarnings("unused") |
| 124 @CalledByNative | 124 @CalledByNative |
| 125 private static void addShortcut(Context context, String url, String title, B
itmap favicon, | 125 private static void addShortcut(Context context, String url, String title, B
itmap icon, |
| 126 int red, int green, int blue, boolean isWebappCapable, int orientati
on) { | 126 int red, int green, int blue, boolean isWebappCapable, int orientati
on) { |
| 127 assert sFullScreenAction != null; | 127 assert sFullScreenAction != null; |
| 128 | 128 |
| 129 Intent shortcutIntent; | 129 Intent shortcutIntent; |
| 130 if (isWebappCapable) { | 130 if (isWebappCapable) { |
| 131 // Encode the favicon as a base64 string (Launcher drops Bitmaps in
the Intent). | 131 // Encode the icon as a base64 string (Launcher drops Bitmaps in the
Intent). |
| 132 String encodedIcon = ""; | 132 String encodedIcon = ""; |
| 133 if (favicon != null) { | 133 if (icon != null) { |
| 134 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutpu
tStream(); | 134 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutpu
tStream(); |
| 135 favicon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutput
Stream); | 135 icon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStr
eam); |
| 136 byte[] byteArray = byteArrayOutputStream.toByteArray(); | 136 byte[] byteArray = byteArrayOutputStream.toByteArray(); |
| 137 encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT); | 137 encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT); |
| 138 } | 138 } |
| 139 | 139 |
| 140 // Add the shortcut as a launcher icon for a full-screen Activity. | 140 // Add the shortcut as a launcher icon for a full-screen Activity. |
| 141 shortcutIntent = new Intent(); | 141 shortcutIntent = new Intent(); |
| 142 shortcutIntent.setAction(sFullScreenAction); | 142 shortcutIntent.setAction(sFullScreenAction); |
| 143 shortcutIntent.putExtra(EXTRA_ICON, encodedIcon); | 143 shortcutIntent.putExtra(EXTRA_ICON, encodedIcon); |
| 144 shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString()); | 144 shortcutIntent.putExtra(EXTRA_ID, UUID.randomUUID().toString()); |
| 145 shortcutIntent.putExtra(EXTRA_TITLE, title); | 145 shortcutIntent.putExtra(EXTRA_TITLE, title); |
| 146 shortcutIntent.putExtra(EXTRA_URL, url); | 146 shortcutIntent.putExtra(EXTRA_URL, url); |
| 147 shortcutIntent.putExtra(EXTRA_ORIENTATION, orientation); | 147 shortcutIntent.putExtra(EXTRA_ORIENTATION, orientation); |
| 148 | 148 |
| 149 // The only reason we convert to a String here is because Android in
explicably eats a | 149 // The only reason we convert to a String here is because Android in
explicably eats a |
| 150 // byte[] when adding the shortcut -- the Bundle received by the lau
nched Activity even | 150 // byte[] when adding the shortcut -- the Bundle received by the lau
nched Activity even |
| 151 // lacks the key for the extra. | 151 // lacks the key for the extra. |
| 152 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); | 152 byte[] mac = WebappAuthenticator.getMacForUrl(context, url); |
| 153 String encodedMac = Base64.encodeToString(mac, Base64.DEFAULT); | 153 String encodedMac = Base64.encodeToString(mac, Base64.DEFAULT); |
| 154 shortcutIntent.putExtra(EXTRA_MAC, encodedMac); | 154 shortcutIntent.putExtra(EXTRA_MAC, encodedMac); |
| 155 } else { | 155 } else { |
| 156 // Add the shortcut as a launcher icon to open in the browser Activi
ty. | 156 // Add the shortcut as a launcher icon to open in the browser Activi
ty. |
| 157 shortcutIntent = BookmarkUtils.createShortcutIntent(context, url); | 157 shortcutIntent = BookmarkUtils.createShortcutIntent(context, url); |
| 158 } | 158 } |
| 159 | 159 |
| 160 shortcutIntent.setPackage(context.getPackageName()); | 160 shortcutIntent.setPackage(context.getPackageName()); |
| 161 context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, short
cutIntent, title, | 161 context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, short
cutIntent, title, |
| 162 favicon, red, green, blue)); | 162 icon, red, green, blue)); |
| 163 | 163 |
| 164 // User is sent to the homescreen as soon as the shortcut is created. | 164 // User is sent to the homescreen as soon as the shortcut is created. |
| 165 Intent homeIntent = new Intent(Intent.ACTION_MAIN); | 165 Intent homeIntent = new Intent(Intent.ACTION_MAIN); |
| 166 homeIntent.addCategory(Intent.CATEGORY_HOME); | 166 homeIntent.addCategory(Intent.CATEGORY_HOME); |
| 167 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 167 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 168 context.startActivity(homeIntent); | 168 context.startActivity(homeIntent); |
| 169 } | 169 } |
| 170 | 170 |
| 171 private native long nativeInitialize(long tabAndroidPtr); | 171 private native long nativeInitialize(long tabAndroidPtr); |
| 172 private native void nativeAddShortcut(long nativeShortcutHelper, String user
RequestedTitle, | 172 private native void nativeAddShortcut(long nativeShortcutHelper, String user
RequestedTitle, |
| 173 int launcherLargeIconSize); | 173 int launcherLargeIconSize); |
| 174 private native void nativeTearDown(long nativeShortcutHelper); | 174 private native void nativeTearDown(long nativeShortcutHelper); |
| 175 } | 175 } |
| OLD | NEW |