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 16 matching lines...) Expand all Loading... | |
| 27 import android.text.TextUtils; | 27 import android.text.TextUtils; |
| 28 import android.util.Base64; | 28 import android.util.Base64; |
| 29 | 29 |
| 30 import org.chromium.base.ApiCompatibilityUtils; | 30 import org.chromium.base.ApiCompatibilityUtils; |
| 31 import org.chromium.base.CommandLine; | 31 import org.chromium.base.CommandLine; |
| 32 import org.chromium.base.ContextUtils; | 32 import org.chromium.base.ContextUtils; |
| 33 import org.chromium.base.Log; | 33 import org.chromium.base.Log; |
| 34 import org.chromium.base.ThreadUtils; | 34 import org.chromium.base.ThreadUtils; |
| 35 import org.chromium.base.VisibleForTesting; | 35 import org.chromium.base.VisibleForTesting; |
| 36 import org.chromium.base.annotations.CalledByNative; | 36 import org.chromium.base.annotations.CalledByNative; |
| 37 import org.chromium.blink_public.platform.WebDisplayMode; | |
| 38 import org.chromium.chrome.R; | 37 import org.chromium.chrome.R; |
| 39 import org.chromium.chrome.browser.webapps.WebApkBuilder; | 38 import org.chromium.chrome.browser.webapps.WebApkBuilder; |
| 40 import org.chromium.chrome.browser.webapps.WebappAuthenticator; | 39 import org.chromium.chrome.browser.webapps.WebappAuthenticator; |
| 41 import org.chromium.chrome.browser.webapps.WebappDataStorage; | 40 import org.chromium.chrome.browser.webapps.WebappDataStorage; |
| 42 import org.chromium.chrome.browser.webapps.WebappLauncherActivity; | 41 import org.chromium.chrome.browser.webapps.WebappLauncherActivity; |
| 43 import org.chromium.chrome.browser.webapps.WebappRegistry; | 42 import org.chromium.chrome.browser.webapps.WebappRegistry; |
| 44 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | 43 import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
| 45 import org.chromium.content_public.common.ScreenOrientationConstants; | 44 import org.chromium.content_public.common.ScreenOrientationConstants; |
| 46 import org.chromium.net.GURLUtils; | 45 import org.chromium.net.GURLUtils; |
| 47 import org.chromium.ui.widget.Toast; | 46 import org.chromium.ui.widget.Toast; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 | 124 |
| 126 /** | 125 /** |
| 127 * Sets the delegate to use. | 126 * Sets the delegate to use. |
| 128 */ | 127 */ |
| 129 @VisibleForTesting | 128 @VisibleForTesting |
| 130 public static void setDelegateForTests(Delegate delegate) { | 129 public static void setDelegateForTests(Delegate delegate) { |
| 131 sDelegate = delegate; | 130 sDelegate = delegate; |
| 132 } | 131 } |
| 133 | 132 |
| 134 /** | 133 /** |
| 135 * Called when we have to fire an Intent to add a shortcut to the home scree n. | 134 * Installs a WebAPK. |
|
dominickn
2016/07/15 00:18:59
Nit: "This method must not be called on the UI thr
| |
| 136 * If the webpage indicated that it was capable of functioning as a webapp, it is added as a | |
| 137 * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the | |
| 138 * home screen as soon as the shortcut is created. | |
| 139 * | |
| 140 * This method must not be called on the UI thread. | |
| 141 */ | 135 */ |
| 142 @SuppressWarnings("unused") | 136 @SuppressWarnings("unused") |
| 143 @CalledByNative | 137 @CalledByNative |
| 144 private static void addShortcut(String id, String url, String scopeUrl, | 138 private static void installWebApk(String url, String scopeUrl, final String name, |
| 139 String shortName, String iconUrl, Bitmap icon, int displayMode, int orientation, | |
| 140 long themeColor, long backgroundColor, String manifestUrl) { | |
| 141 assert !ThreadUtils.runningOnUiThread(); | |
| 142 Context context = ContextUtils.getApplicationContext(); | |
|
dominickn
2016/07/15 00:18:59
Super nit: newline after the assert for consistenc
| |
| 143 WebApkBuilder apkBuilder = ((ChromeApplication) context).createWebApkBui lder(); | |
| 144 if (apkBuilder == null) { | |
| 145 // TODO(pkotwicz): Figure out what to do when building WebAPK fails. (crbug.com/626950) | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 if (TextUtils.isEmpty(scopeUrl)) { | |
| 150 scopeUrl = GURLUtils.getOrigin(url); | |
| 151 } | |
| 152 apkBuilder.buildWebApkAsync(url, scopeUrl, name, shortName, iconUrl, ico n, displayMode, | |
| 153 orientation, themeColor, backgroundColor, manifestUrl); | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * Adds home screen shortcut which opens in a {@link WebappActivity}. | |
| 158 */ | |
|
dominickn
2016/07/15 00:18:59
Nit: This method must not be called on the UI thre
| |
| 159 @SuppressWarnings("unused") | |
| 160 @CalledByNative | |
| 161 private static void addWebappShortcut(String id, String url, String scopeUrl , | |
|
dominickn
2016/07/15 00:18:59
Rename: "addWebapp" as per comment in shortcut_hel
| |
| 145 final String userTitle, String name, String shortName, String iconUr l, Bitmap icon, | 162 final String userTitle, String name, String shortName, String iconUr l, Bitmap icon, |
| 146 int displayMode, int orientation, int source, long themeColor, long backgroundColor, | 163 int displayMode, int orientation, int source, long themeColor, long backgroundColor, |
| 147 String manifestUrl, final long callbackPointer) { | 164 final long callbackPointer) { |
| 148 assert !ThreadUtils.runningOnUiThread(); | 165 assert !ThreadUtils.runningOnUiThread(); |
| 149 | 166 |
| 150 Context context = ContextUtils.getApplicationContext(); | 167 Context context = ContextUtils.getApplicationContext(); |
| 151 final Intent shortcutIntent; | 168 if (TextUtils.isEmpty(scopeUrl)) { |
| 152 boolean isWebappCapable = (displayMode == WebDisplayMode.Standalone | 169 scopeUrl = getScopeFromUrl(url); |
|
dominickn
2016/07/15 00:18:59
This variable isn't used. Replace getScopeFromUrl(
| |
| 153 || displayMode == WebDisplayMode.Fullscreen); | |
| 154 if (isWebappCapable) { | |
| 155 if (CommandLine.getInstance().hasSwitch(ChromeSwitches.ENABLE_WEBAPK )) { | |
| 156 WebApkBuilder apkBuilder = ((ChromeApplication) context).createW ebApkBuilder(); | |
| 157 if (apkBuilder != null) { | |
| 158 if (TextUtils.isEmpty(scopeUrl)) { | |
| 159 scopeUrl = GURLUtils.getOrigin(url); | |
| 160 } | |
| 161 apkBuilder.buildWebApkAsync(url, scopeUrl, name, shortName, iconUrl, icon, | |
| 162 displayMode, orientation, themeColor, backgroundColo r, manifestUrl); | |
| 163 return; | |
| 164 } | |
| 165 } | |
| 166 if (TextUtils.isEmpty(scopeUrl)) { | |
| 167 scopeUrl = getScopeFromUrl(url); | |
| 168 } | |
| 169 shortcutIntent = createWebappShortcutIntent(id, sDelegate.getFullscr eenAction(), url, | |
| 170 scopeUrl, name, shortName, icon, WEBAPP_SHORTCUT_VERSION, di splayMode, | |
| 171 orientation, themeColor, backgroundColor, iconUrl.isEmpty()) ; | |
| 172 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); | |
| 173 } else { | |
| 174 // Add the shortcut as a launcher icon to open in the browser Activi ty. | |
| 175 shortcutIntent = createShortcutIntent(url); | |
| 176 } | 170 } |
| 177 | 171 |
| 172 final Intent shortcutIntent = createWebappShortcutIntent(id, | |
| 173 sDelegate.getFullscreenAction(), url, getScopeFromUrl(url), name , shortName, icon, | |
| 174 WEBAPP_SHORTCUT_VERSION, displayMode, orientation, themeColor, b ackgroundColor, | |
| 175 iconUrl.isEmpty()); | |
| 176 shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url)); | |
| 178 // Always attach a source (one of add to home screen menu item, app bann er, or unknown) to | 177 // Always attach a source (one of add to home screen menu item, app bann er, or unknown) to |
| 179 // the intent. This allows us to distinguish where a shortcut was added from in metrics. | 178 // the intent. This allows us to distinguish where a shortcut was added from in metrics. |
|
dominickn
2016/07/15 00:18:59
This source comment is duplicated - you can probab
pkotwicz
2016/07/15 01:21:56
Ok, removed
| |
| 180 shortcutIntent.putExtra(EXTRA_SOURCE, source); | 179 shortcutIntent.putExtra(EXTRA_SOURCE, source); |
| 181 shortcutIntent.setPackage(context.getPackageName()); | 180 shortcutIntent.setPackage(context.getPackageName()); |
| 182 sDelegate.sendBroadcast( | 181 sDelegate.sendBroadcast( |
| 183 context, createAddToHomeIntent(url, userTitle, icon, shortcutInt ent)); | 182 context, createAddToHomeIntent(url, userTitle, icon, shortcutInt ent)); |
| 184 | 183 |
| 185 if (isWebappCapable) { | 184 // Store the webapp data so that it is accessible without the intent. On ce this process |
| 186 // Store the webapp data so that it is accessible without the intent . Once this process | 185 // is complete, call back to native code to start the splash image downl oad. |
| 187 // is complete, call back to native code to start the splash image d ownload. | 186 WebappRegistry.registerWebapp(context, id, |
| 188 WebappRegistry.registerWebapp(context, id, | 187 new WebappRegistry.FetchWebappDataStorageCallback() { |
| 189 new WebappRegistry.FetchWebappDataStorageCallback() { | 188 @Override |
| 190 @Override | 189 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) { |
| 191 public void onWebappDataStorageRetrieved(WebappDataStora ge storage) { | 190 storage.updateFromShortcutIntent(shortcutIntent); |
| 192 storage.updateFromShortcutIntent(shortcutIntent); | 191 nativeOnWebappDataStored(callbackPointer); |
| 193 nativeOnWebappDataStored(callbackPointer); | |
| 194 } | |
| 195 } | 192 } |
| 196 ); | 193 }); |
| 197 } | 194 |
| 195 showAddedToHomescreenToast(userTitle); | |
| 196 } | |
| 197 | |
| 198 /** | |
| 199 * Adds home screen shortcut which opens in the browser Activity. | |
| 200 */ | |
| 201 @SuppressWarnings("unused") | |
| 202 @CalledByNative | |
| 203 private static void addBookmarkShortcut(String url, String userTitle, Bitmap icon, int source) { | |
|
dominickn
2016/07/15 00:19:00
Rename: "addShortcut" as per comment in shortcut_h
| |
| 204 assert !ThreadUtils.runningOnUiThread(); | |
| 205 | |
| 206 Context context = ContextUtils.getApplicationContext(); | |
| 207 final Intent shortcutIntent = createShortcutIntent(url); | |
| 208 // Always attach a source (one of add to home screen menu item, app bann er, or unknown) to | |
| 209 // the intent. This allows us to distinguish where a shortcut was added from in metrics. | |
| 210 shortcutIntent.putExtra(EXTRA_SOURCE, source); | |
| 211 shortcutIntent.setPackage(context.getPackageName()); | |
| 212 sDelegate.sendBroadcast( | |
| 213 context, createAddToHomeIntent(url, userTitle, icon, shortcutInt ent)); | |
| 214 showAddedToHomescreenToast(userTitle); | |
| 215 } | |
| 216 | |
| 217 /** | |
| 218 * Show toast to alert user that the shortcut was added to the home screen. | |
| 219 */ | |
| 220 private static void showAddedToHomescreenToast(final String title) { | |
| 221 assert !ThreadUtils.runningOnUiThread(); | |
|
dominickn
2016/07/15 00:18:59
I don't think this method needs the assert - it do
pkotwicz
2016/07/15 01:21:56
Removed the assert and changed the Handler logic t
| |
| 198 | 222 |
| 199 // Alert the user about adding the shortcut. | 223 // Alert the user about adding the shortcut. |
|
dominickn
2016/07/15 00:19:00
Remove redundant comment now this is a method.
| |
| 200 Handler handler = new Handler(Looper.getMainLooper()); | 224 Handler handler = new Handler(Looper.getMainLooper()); |
| 201 handler.post(new Runnable() { | 225 handler.post(new Runnable() { |
| 202 @Override | 226 @Override |
| 203 public void run() { | 227 public void run() { |
| 204 Context applicationContext = ContextUtils.getApplicationContext( ); | 228 Context applicationContext = ContextUtils.getApplicationContext( ); |
| 205 String toastText = | 229 String toastText = |
| 206 applicationContext.getString(R.string.added_to_homescree n, userTitle); | 230 applicationContext.getString(R.string.added_to_homescree n, title); |
| 207 Toast toast = Toast.makeText(applicationContext, toastText, Toas t.LENGTH_SHORT); | 231 Toast toast = Toast.makeText(applicationContext, toastText, Toas t.LENGTH_SHORT); |
| 208 toast.show(); | 232 toast.show(); |
| 209 } | 233 } |
| 210 }); | 234 }); |
| 211 } | 235 } |
| 212 | 236 |
| 213 /** | 237 /** |
| 214 * Creates a storage location and stores the data for a web app using {@link WebappDataStorage}. | 238 * Creates a storage location and stores the data for a web app using {@link WebappDataStorage}. |
| 215 * @param id ID of the webapp which is storing data. | 239 * @param id ID of the webapp which is storing data. |
| 216 * @param splashImage Image which should be displayed on the splash screen o f | 240 * @param splashImage Image which should be displayed on the splash screen o f |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 if (drawable instanceof BitmapDrawable) { | 601 if (drawable instanceof BitmapDrawable) { |
| 578 BitmapDrawable bd = (BitmapDrawable) drawable; | 602 BitmapDrawable bd = (BitmapDrawable) drawable; |
| 579 return bd.getBitmap(); | 603 return bd.getBitmap(); |
| 580 } | 604 } |
| 581 assert false : "The drawable was not a bitmap drawable as expected"; | 605 assert false : "The drawable was not a bitmap drawable as expected"; |
| 582 return null; | 606 return null; |
| 583 } | 607 } |
| 584 | 608 |
| 585 private static native void nativeOnWebappDataStored(long callbackPointer); | 609 private static native void nativeOnWebappDataStored(long callbackPointer); |
| 586 } | 610 } |
| OLD | NEW |