Chromium Code Reviews| 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.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 */ | 51 */ |
| 52 public class ShortcutHelper { | 52 public class ShortcutHelper { |
| 53 public static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp_ icon"; | 53 public static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp_ icon"; |
| 54 public static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_id "; | 54 public static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_id "; |
| 55 public static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_m ac"; | 55 public static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_m ac"; |
| 56 // EXTRA_TITLE is present for backward compatibility reasons | 56 // EXTRA_TITLE is present for backward compatibility reasons |
| 57 public static final String EXTRA_TITLE = "org.chromium.chrome.browser.webapp _title"; | 57 public static final String EXTRA_TITLE = "org.chromium.chrome.browser.webapp _title"; |
| 58 public static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp_ name"; | 58 public static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp_ name"; |
| 59 public static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser.w ebapp_short_name"; | 59 public static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser.w ebapp_short_name"; |
| 60 public static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_u rl"; | 60 public static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_u rl"; |
| 61 public static final String EXTRA_SCOPE = "org.chromium.chrome.browser.webapp _scope"; | |
| 61 public static final String EXTRA_ORIENTATION = ScreenOrientationConstants.EX TRA_ORIENTATION; | 62 public static final String EXTRA_ORIENTATION = ScreenOrientationConstants.EX TRA_ORIENTATION; |
| 62 public static final String EXTRA_SOURCE = "org.chromium.chrome.browser.webap p_source"; | 63 public static final String EXTRA_SOURCE = "org.chromium.chrome.browser.webap p_source"; |
| 63 public static final String EXTRA_THEME_COLOR = "org.chromium.chrome.browser. theme_color"; | 64 public static final String EXTRA_THEME_COLOR = "org.chromium.chrome.browser. theme_color"; |
| 64 public static final String EXTRA_BACKGROUND_COLOR = | 65 public static final String EXTRA_BACKGROUND_COLOR = |
| 65 "org.chromium.chrome.browser.background_color"; | 66 "org.chromium.chrome.browser.background_color"; |
| 66 public static final String EXTRA_IS_ICON_GENERATED = | 67 public static final String EXTRA_IS_ICON_GENERATED = |
| 67 "org.chromium.chrome.browser.is_icon_generated"; | 68 "org.chromium.chrome.browser.is_icon_generated"; |
| 68 public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = | 69 public static final String REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB = |
| 69 "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; | 70 "REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB"; |
| 70 | 71 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 /** | 118 /** |
| 118 * Called when we have to fire an Intent to add a shortcut to the home scree n. | 119 * Called when we have to fire an Intent to add a shortcut to the home scree n. |
| 119 * If the webpage indicated that it was capable of functioning as a webapp, it is added as a | 120 * 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 | 121 * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the |
| 121 * home screen as soon as the shortcut is created. | 122 * home screen as soon as the shortcut is created. |
| 122 */ | 123 */ |
| 123 @SuppressWarnings("unused") | 124 @SuppressWarnings("unused") |
| 124 @CalledByNative | 125 @CalledByNative |
| 125 private static void addShortcut(Context context, String id, String url, fina l String userTitle, | 126 private static void addShortcut(Context context, String id, String url, fina l String userTitle, |
| 126 String name, String shortName, Bitmap icon, boolean isWebappCapable, int orientation, | 127 String name, String shortName, Bitmap icon, boolean isWebappCapable, int orientation, |
| 127 int source, long themeColor, long backgroundColor, boolean isIconGen erated) { | 128 int source, long themeColor, long backgroundColor, boolean isIconGen erated, |
| 129 final long callbackPointer) { | |
| 128 Intent shortcutIntent; | 130 Intent shortcutIntent; |
| 129 if (isWebappCapable) { | 131 if (isWebappCapable) { |
| 130 // Encode the icon as a base64 string (Launcher drops Bitmaps in the Intent). | 132 shortcutIntent = createWebappShortcutIntent(id, sDelegate.getFullscr eenAction(), url, |
| 131 String encodedIcon = encodeBitmapAsString(icon); | 133 name, shortName, icon, orientation, themeColor, backgroundCo lor, |
| 132 | 134 isIconGenerated); |
| 133 // Add the shortcut as a launcher icon for a full-screen Activity. | 135 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); |
| 134 shortcutIntent = new Intent(); | |
| 135 shortcutIntent.setAction(sDelegate.getFullscreenAction()) | |
| 136 .putExtra(EXTRA_ICON, encodedIcon) | |
| 137 .putExtra(EXTRA_ID, id) | |
| 138 .putExtra(EXTRA_NAME, name) | |
| 139 .putExtra(EXTRA_SHORT_NAME, shortName) | |
| 140 .putExtra(EXTRA_URL, url) | |
| 141 .putExtra(EXTRA_ORIENTATION, orientation) | |
| 142 .putExtra(EXTRA_MAC, getEncodedMac(context, url)) | |
| 143 .putExtra(EXTRA_THEME_COLOR, themeColor) | |
| 144 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) | |
| 145 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated); | |
| 146 } else { | 136 } else { |
| 147 // Add the shortcut as a launcher icon to open in the browser Activi ty. | 137 // Add the shortcut as a launcher icon to open in the browser Activi ty. |
| 148 shortcutIntent = createShortcutIntent(url); | 138 shortcutIntent = createShortcutIntent(url); |
| 149 } | 139 } |
| 150 | 140 |
| 151 // Always attach a source (one of add to home screen menu item, app bann er, or unknown) to | 141 // Always attach a source (one of add to home screen menu item, app bann er, or unknown) to |
| 152 // the intent. This allows us to distinguish where a shortcut was added from in metrics. | 142 // the intent. This allows us to distinguish where a shortcut was added from in metrics. |
| 153 shortcutIntent.putExtra(EXTRA_SOURCE, source); | 143 shortcutIntent.putExtra(EXTRA_SOURCE, source); |
| 154 shortcutIntent.setPackage(context.getPackageName()); | 144 shortcutIntent.setPackage(context.getPackageName()); |
| 155 sDelegate.sendBroadcast( | 145 sDelegate.sendBroadcast( |
| 156 context, createAddToHomeIntent(url, userTitle, icon, shortcutInt ent)); | 146 context, createAddToHomeIntent(url, userTitle, icon, shortcutInt ent)); |
| 157 | 147 |
| 148 if (isWebappCapable) { | |
| 149 // Store the webapp data so that we can access it. | |
| 150 WebappRegistry.registerWebapp(context, id).updateFromShortcutIntent( shortcutIntent, | |
| 151 new Runnable() { | |
| 152 @Override | |
| 153 public void run() { | |
| 154 nativeOnWebappDataStored(callbackPointer, true); | |
| 155 } | |
| 156 }); | |
| 157 } else { | |
| 158 nativeOnWebappDataStored(callbackPointer, false); | |
| 159 } | |
| 160 | |
| 158 // Alert the user about adding the shortcut. | 161 // Alert the user about adding the shortcut. |
| 159 Handler handler = new Handler(Looper.getMainLooper()); | 162 Handler handler = new Handler(Looper.getMainLooper()); |
| 160 handler.post(new Runnable() { | 163 handler.post(new Runnable() { |
| 161 @Override | 164 @Override |
| 162 public void run() { | 165 public void run() { |
| 163 Context applicationContext = ApplicationStatus.getApplicationCon text(); | 166 Context applicationContext = ApplicationStatus.getApplicationCon text(); |
| 164 String toastText = | 167 String toastText = |
| 165 applicationContext.getString(R.string.added_to_homescree n, userTitle); | 168 applicationContext.getString(R.string.added_to_homescree n, userTitle); |
| 166 Toast toast = Toast.makeText(applicationContext, toastText, Toas t.LENGTH_SHORT); | 169 Toast toast = Toast.makeText(applicationContext, toastText, Toas t.LENGTH_SHORT); |
| 167 toast.show(); | 170 toast.show(); |
| 168 } | 171 } |
| 169 }); | 172 }); |
| 170 } | 173 } |
| 171 | 174 |
| 172 /** | 175 /** |
| 173 * Creates a storage location and stores the data for a web app using {@link WebappDataStorage}. | 176 * Creates a storage location and stores the data for a web app using {@link WebappDataStorage}. |
| 174 * @param context Context to open the WebappDataStorage with. | 177 * @param context Context to open the WebappDataStorage with. |
| 175 * @param id ID of the webapp which is storing data. | 178 * @param id ID of the webapp which is storing data. |
| 176 * @param scope scope of the webapp which is storing data. | |
| 177 * @param splashImage Image which should be displayed on the splash screen o f | 179 * @param splashImage Image which should be displayed on the splash screen o f |
| 178 * the webapp. This can be null of there is no image to s how. | 180 * the webapp. This can be null of there is no image to s how. |
| 179 */ | 181 */ |
| 180 @SuppressWarnings("unused") | 182 @SuppressWarnings("unused") |
| 181 @CalledByNative | 183 @CalledByNative |
| 182 private static void storeWebappData(Context context, String id, String scope , | 184 private static void storeWebappSplashImage(final Context context, final Stri ng id, |
| 183 Bitmap splashImage) { | 185 final Bitmap splashImage) { |
| 184 WebappRegistry.registerWebapp(context, id, scope); | 186 WebappRegistry.getWebappDataStorage(context, id, |
| 185 WebappDataStorage.open(context, id).updateSplashScreenImage(splashImage) ; | 187 new WebappRegistry.FetchWebappDataStorageCallback() { |
| 188 @Override | |
| 189 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) { | |
| 190 if (storage == null) return; | |
| 191 | |
| 192 storage.updateSplashScreenImage(splashImage); | |
| 193 } | |
| 194 | |
| 195 } | |
| 196 ); | |
| 186 } | 197 } |
| 187 | 198 |
| 188 /** | 199 /** |
| 189 * Creates an intent that will add a shortcut to the home screen. | 200 * Creates an intent that will add a shortcut to the home screen. |
| 190 * @param shortcutIntent Intent to fire when the shortcut is activated. | 201 * @param shortcutIntent Intent to fire when the shortcut is activated. |
| 191 * @param url URL of the shortcut. | 202 * @param url URL of the shortcut. |
| 192 * @param title Title of the shortcut. | 203 * @param title Title of the shortcut. |
| 193 * @param icon Image that represents the shortcut. | 204 * @param icon Image that represents the shortcut. |
| 194 * @return Intent for the shortcut. | 205 * @return Intent for the shortcut. |
| 195 */ | 206 */ |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 208 * @param title Title of the shortcut. | 219 * @param title Title of the shortcut. |
| 209 * @param icon Image that represents the shortcut. | 220 * @param icon Image that represents the shortcut. |
| 210 * @return Intent for the shortcut. | 221 * @return Intent for the shortcut. |
| 211 */ | 222 */ |
| 212 public static Intent createAddToHomeIntent(String url, String title, Bitmap icon) { | 223 public static Intent createAddToHomeIntent(String url, String title, Bitmap icon) { |
| 213 Intent shortcutIntent = createShortcutIntent(url); | 224 Intent shortcutIntent = createShortcutIntent(url); |
| 214 return createAddToHomeIntent(url, title, icon, shortcutIntent); | 225 return createAddToHomeIntent(url, title, icon, shortcutIntent); |
| 215 } | 226 } |
| 216 | 227 |
| 217 /** | 228 /** |
| 229 * Webapp-capable shortcut intent for icon on home screen. | |
| 230 * @param action Intent action to open a full screen activity. | |
| 231 * @param id Id of the webapp. | |
|
gone
2016/04/01 23:44:48
nit: Align these maybe? It's kind of blobby atm.
dominickn
2016/04/04 07:26:23
Done.
| |
| 232 * @param url Url of the webapp. | |
| 233 * @param name Name of the webapp. | |
| 234 * @param shortName Short name of the webapp. @param icon Icon of the webapp . | |
| 235 * @param orientation Orientation of the webapp. | |
| 236 * @param themeColor Theme color of the webapp. | |
| 237 * @param backgroundColor Background color of the webapp. | |
| 238 * @param isIconGenerated True if the icon is generated by Chromium. | |
| 239 * @return Intent for onclick action of the shortcut. | |
| 240 */ | |
| 241 public static Intent createWebappShortcutIntent(String id, String action, St ring url, | |
| 242 String name, String shortName, Bitmap icon, int orientation, long th emeColor, | |
| 243 long backgroundColor, boolean isIconGenerated) { | |
| 244 // Encode the icon as a base64 string (Launcher drops Bitmaps in the Int ent). | |
| 245 String encodedIcon = encodeBitmapAsString(icon); | |
| 246 | |
| 247 // Create an intent as a launcher icon for a full-screen Activity. | |
| 248 // The scope is set to the URL until the manifest scope member is availa ble. | |
| 249 Intent shortcutIntent = new Intent(); | |
| 250 shortcutIntent.setAction(action) | |
| 251 .putExtra(EXTRA_ID, id) | |
| 252 .putExtra(EXTRA_URL, url) | |
| 253 .putExtra(EXTRA_SCOPE, url) | |
| 254 .putExtra(EXTRA_NAME, name) | |
| 255 .putExtra(EXTRA_SHORT_NAME, shortName) | |
| 256 .putExtra(EXTRA_ICON, encodedIcon) | |
| 257 .putExtra(EXTRA_ORIENTATION, orientation) | |
| 258 .putExtra(EXTRA_THEME_COLOR, themeColor) | |
| 259 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) | |
| 260 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated); | |
| 261 return shortcutIntent; | |
| 262 } | |
| 263 | |
| 264 /** | |
| 218 * Shortcut intent for icon on home screen. | 265 * Shortcut intent for icon on home screen. |
| 219 * @param url Url of the shortcut. | 266 * @param url Url of the shortcut. |
| 220 * @return Intent for onclick action of the shortcut. | 267 * @return Intent for onclick action of the shortcut. |
| 221 */ | 268 */ |
| 222 public static Intent createShortcutIntent(String url) { | 269 public static Intent createShortcutIntent(String url) { |
| 223 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | 270 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 224 shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true); | 271 shortcutIntent.putExtra(REUSE_URL_MATCHING_TAB_ELSE_NEW_TAB, true); |
| 225 return shortcutIntent; | 272 return shortcutIntent; |
| 226 } | 273 } |
| 227 | 274 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( | 501 Drawable drawable = ApiCompatibilityUtils.getDrawableForDensity( |
| 455 context.getResources(), id, density); | 502 context.getResources(), id, density); |
| 456 | 503 |
| 457 if (drawable instanceof BitmapDrawable) { | 504 if (drawable instanceof BitmapDrawable) { |
| 458 BitmapDrawable bd = (BitmapDrawable) drawable; | 505 BitmapDrawable bd = (BitmapDrawable) drawable; |
| 459 return bd.getBitmap(); | 506 return bd.getBitmap(); |
| 460 } | 507 } |
| 461 assert false : "The drawable was not a bitmap drawable as expected"; | 508 assert false : "The drawable was not a bitmap drawable as expected"; |
| 462 return null; | 509 return null; |
| 463 } | 510 } |
| 511 | |
| 512 private static native void nativeOnWebappDataStored(long callbackPointer, bo olean wasStored); | |
| 464 } | 513 } |
| OLD | NEW |