Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.bookmarks; | 5 package org.chromium.chrome.browser.bookmarks; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.SharedPreferences; | 10 import android.content.SharedPreferences; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 /** | 39 /** |
| 40 * A class holding static util functions for bookmark. | 40 * A class holding static util functions for bookmark. |
| 41 */ | 41 */ |
| 42 public class BookmarkUtils { | 42 public class BookmarkUtils { |
| 43 private static final String PREF_LAST_USED_URL = "enhanced_bookmark_last_use d_url"; | 43 private static final String PREF_LAST_USED_URL = "enhanced_bookmark_last_use d_url"; |
| 44 private static final String PREF_LAST_USED_PARENT = "enhanced_bookmark_last_ used_parent_folder"; | 44 private static final String PREF_LAST_USED_PARENT = "enhanced_bookmark_last_ used_parent_folder"; |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * If the tab has already been bookmarked, start {@link BookmarkEditActivity } for the | 47 * If the tab has already been bookmarked, start {@link BookmarkEditActivity } for the |
| 48 * bookmark. If not, add the bookmark to bookmarkmodel, and show a snackbar notifying the user. | 48 * bookmark. If not, add the bookmark to bookmarkmodel, and show a snackbar notifying the user. |
| 49 * | |
| 50 * Note: Takes ownership of bookmarkModel, and will call |destroy| on it whe n finished. | |
| 51 * | |
| 49 * @param idToAdd The bookmark ID if the tab has already been bookmarked. | 52 * @param idToAdd The bookmark ID if the tab has already been bookmarked. |
| 50 * @param bookmarkModel The bookmark model. | 53 * @param bookmarkModel The bookmark model. |
| 51 * @param tab The tab to add or edit a bookmark. | 54 * @param tab The tab to add or edit a bookmark. |
| 52 * @param snackbarManager The SnackbarManager used to show the snackbar. | 55 * @param snackbarManager The SnackbarManager used to show the snackbar. |
| 53 * @param activity Current activity. | 56 * @param activity Current activity. |
| 54 */ | 57 */ |
| 55 public static void addOrEditBookmark(long idToAdd, BookmarkModel bookmarkMod el, | 58 public static void addOrEditBookmark(long idToAdd, BookmarkModel bookmarkMod el, |
| 56 Tab tab, SnackbarManager snackbarManager, Activity activity) { | 59 Tab tab, SnackbarManager snackbarManager, Activity activity) { |
| 57 // See if the Tab's contents should be saved or not. | 60 // See if the Tab's contents should be saved or not. |
| 58 WebContents webContentsToSave = null; | 61 WebContents webContentsToSave = null; |
| 59 if (!shouldSkipSavingTabOffline(tab)) webContentsToSave = tab.getWebCont ents(); | 62 if (!shouldSkipSavingTabOffline(tab)) webContentsToSave = tab.getWebCont ents(); |
| 60 | 63 |
| 61 if (idToAdd != Tab.INVALID_BOOKMARK_ID) { | 64 if (idToAdd != Tab.INVALID_BOOKMARK_ID) { |
| 62 startEditActivity(activity, new BookmarkId(idToAdd, BookmarkType.NOR MAL), | 65 startEditActivity(activity, new BookmarkId(idToAdd, BookmarkType.NOR MAL), |
| 63 webContentsToSave); | 66 webContentsToSave); |
| 67 bookmarkModel.destroy(); | |
| 64 return; | 68 return; |
| 65 } | 69 } |
| 66 | 70 |
| 67 BookmarkId parent = getLastUsedParent(activity); | 71 BookmarkId parent = getLastUsedParent(activity); |
| 68 if (parent == null || !bookmarkModel.doesBookmarkExist(parent)) { | 72 if (parent == null || !bookmarkModel.doesBookmarkExist(parent)) { |
| 69 parent = bookmarkModel.getDefaultFolder(); | 73 parent = bookmarkModel.getDefaultFolder(); |
| 70 } | 74 } |
| 71 | 75 |
| 72 // The bookmark model will be destroyed in the created AddBookmarkCallba ck. | 76 // The bookmark model will be destroyed in the created AddBookmarkCallba ck. |
| 73 bookmarkModel.addBookmarkAsync(parent, bookmarkModel.getChildCount(paren t), tab.getTitle(), | 77 bookmarkModel.addBookmarkAsync(parent, bookmarkModel.getChildCount(paren t), tab.getTitle(), |
| 74 tab.getUrl(), webContentsToSave, | 78 tab.getUrl(), webContentsToSave, |
| 79 // bookmarkModel will be destroyed in the callback. | |
| 75 createAddBookmarkCallback(bookmarkModel, snackbarManager, activi ty, | 80 createAddBookmarkCallback(bookmarkModel, snackbarManager, activi ty, |
| 76 webContentsToSave)); | 81 webContentsToSave)); |
| 77 } | 82 } |
| 78 | 83 |
| 79 /** | 84 /** |
| 80 * Adds a bookmark with the given title and url to the last used parent fold er. Provides | 85 * Adds a bookmark with the given title and url to the last used parent fold er. Provides |
| 81 * no visual feedback that a bookmark has been added. | 86 * no visual feedback that a bookmark has been added. |
| 82 * | 87 * |
| 83 * @param title The title of the bookmark. | 88 * @param title The title of the bookmark. |
| 84 * @param url The URL of the new bookmark. | 89 * @param url The URL of the new bookmark. |
| 85 */ | 90 */ |
| 86 public static BookmarkId addBookmarkSilently(Context context, | 91 public static BookmarkId addBookmarkSilently(Context context, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 | 222 |
| 218 @Override | 223 @Override |
| 219 public void onAction(Object actionData) { | 224 public void onAction(Object actionData) { |
| 220 RecordUserAction.record("EnhancedBookmarks.EditAfterCreateButton Clicked"); | 225 RecordUserAction.record("EnhancedBookmarks.EditAfterCreateButton Clicked"); |
| 221 startEditActivity(activity, bookmarkId, (WebContents) actionData ); | 226 startEditActivity(activity, bookmarkId, (WebContents) actionData ); |
| 222 } | 227 } |
| 223 }; | 228 }; |
| 224 } | 229 } |
| 225 | 230 |
| 226 /** | 231 /** |
| 227 * Gets whether bookmark manager should load offline page initially. | |
| 228 */ | |
| 229 private static boolean shouldShowOfflinePageAtFirst(OfflinePageBridge bridge ) { | |
| 230 if (bridge == null || bridge.getAllPages().isEmpty() || OfflinePageUtils .isConnected()) { | |
| 231 return false; | |
| 232 } | |
| 233 return true; | |
| 234 } | |
| 235 | |
| 236 /** | |
| 237 * Shows bookmark main UI. | 232 * Shows bookmark main UI. |
| 233 * | |
| 234 * The initial url the bookmark manager shows depends on offline page status and some | |
| 235 * experiments we run. | |
| 238 */ | 236 */ |
| 239 public static void showBookmarkManager(Activity activity) { | 237 public static void showBookmarkManager(final Activity activity) { |
| 240 String url = getFirstUrlToLoad(activity); | 238 String lastUsedUrl = getLastUsedUrl(activity); |
| 239 final String defaultUrl = | |
| 240 TextUtils.isEmpty(lastUsedUrl) ? UrlConstants.BOOKMARKS_URL : la stUsedUrl; | |
| 241 | 241 |
| 242 BookmarkModel bookmarkModel = new BookmarkModel(); | |
| 243 OfflinePageBridge bridge = bookmarkModel.getOfflinePageBridge(); | |
|
Ian Wen
2016/04/08 00:07:45
I am a bit wary here. Is it possible that we retai
dewittj
2016/04/08 18:06:58
I reorganized the patch so that the code is laid o
| |
| 244 bookmarkModel.destroy(); | |
| 245 | |
| 246 // If we are connected show the default URL. | |
| 247 if (bridge == null || OfflinePageUtils.isConnected()) { | |
| 248 openUrlOrStartActivityWithUrl(activity, defaultUrl); | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 // Otherwise we want to show offline pages. If there aren't any, just d efault to the | |
| 253 // regular list. | |
| 254 bridge.hasPages(new OfflinePageBridge.HasPagesCallback() { | |
| 255 @Override | |
| 256 public void onResult(boolean hasPages) { | |
| 257 String url = defaultUrl; | |
| 258 if (hasPages) { | |
| 259 url = BookmarkUIState.createFilterUrl(BookmarkFilter.OFFLINE _PAGES, false) | |
| 260 .toString(); | |
| 261 } | |
| 262 openUrlOrStartActivityWithUrl(activity, url); | |
| 263 } | |
| 264 }); | |
| 265 } | |
| 266 | |
| 267 private static void openUrlOrStartActivityWithUrl(Activity activity, String url) { | |
| 242 if (DeviceFormFactor.isTablet(activity)) { | 268 if (DeviceFormFactor.isTablet(activity)) { |
| 243 openUrl(activity, url); | 269 openUrl(activity, url); |
| 244 } else { | 270 } else { |
| 245 Intent intent = new Intent(activity, BookmarkActivity.class); | 271 Intent intent = new Intent(activity, BookmarkActivity.class); |
| 246 intent.setData(Uri.parse(url)); | 272 intent.setData(Uri.parse(url)); |
| 247 activity.startActivity(intent); | 273 activity.startActivity(intent); |
| 248 } | 274 } |
| 249 } | 275 } |
| 250 | 276 |
| 251 /** | 277 /** |
| 252 * The initial url the bookmark manager shows depends on offline page status and some | |
| 253 * experiments we run. | |
| 254 */ | |
| 255 private static String getFirstUrlToLoad(Activity activity) { | |
| 256 BookmarkModel model = new BookmarkModel(); | |
| 257 OfflinePageBridge bridge = model.getOfflinePageBridge(); | |
| 258 try { | |
| 259 if (shouldShowOfflinePageAtFirst(bridge)) { | |
| 260 return BookmarkUIState.createFilterUrl(BookmarkFilter.OFFLINE_PA GES, | |
| 261 false).toString(); | |
| 262 } | |
| 263 String lastUsedUrl = getLastUsedUrl(activity); | |
| 264 if (!TextUtils.isEmpty(lastUsedUrl)) return lastUsedUrl; | |
| 265 return UrlConstants.BOOKMARKS_URL; | |
| 266 } finally { | |
| 267 model.destroy(); | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 /** | |
| 272 * Saves the last used url to preference. The saved url will be later querie d by | 278 * Saves the last used url to preference. The saved url will be later querie d by |
| 273 * {@link #getLastUsedUrl(Context)} | 279 * {@link #getLastUsedUrl(Context)} |
| 274 */ | 280 */ |
| 275 static void setLastUsedUrl(Context context, String url) { | 281 static void setLastUsedUrl(Context context, String url) { |
| 276 PreferenceManager.getDefaultSharedPreferences(context).edit() | 282 PreferenceManager.getDefaultSharedPreferences(context).edit() |
| 277 .putString(PREF_LAST_USED_URL, url).apply(); | 283 .putString(PREF_LAST_USED_URL, url).apply(); |
| 278 } | 284 } |
| 279 | 285 |
| 280 /** | 286 /** |
| 281 * Fetches url representing the user's state last time they close the bookma rk manager. | 287 * Fetches url representing the user's state last time they close the bookma rk manager. |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 } | 393 } |
| 388 | 394 |
| 389 /** | 395 /** |
| 390 * Indicates whether we should skip saving the given tab as an offline page. | 396 * Indicates whether we should skip saving the given tab as an offline page. |
| 391 * A tab shouldn't be saved offline if it shows an error page or a sad tab p age. | 397 * A tab shouldn't be saved offline if it shows an error page or a sad tab p age. |
| 392 */ | 398 */ |
| 393 private static boolean shouldSkipSavingTabOffline(Tab tab) { | 399 private static boolean shouldSkipSavingTabOffline(Tab tab) { |
| 394 return tab.isShowingErrorPage() || tab.isShowingSadTab(); | 400 return tab.isShowingErrorPage() || tab.isShowingSadTab(); |
| 395 } | 401 } |
| 396 } | 402 } |
| OLD | NEW |