| 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, |
| 75 createAddBookmarkCallback(bookmarkModel, snackbarManager, activi
ty, | 79 createAddBookmarkCallback(bookmarkModel, snackbarManager, activi
ty, |
| 76 webContentsToSave)); | 80 webContentsToSave)); |
| 77 } | 81 } |
| 78 | 82 |
| 79 /** | 83 /** |
| 80 * Adds a bookmark with the given title and url to the last used parent fold
er. Provides | 84 * 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. | 85 * no visual feedback that a bookmark has been added. |
| 82 * | 86 * |
| 83 * @param title The title of the bookmark. | 87 * @param title The title of the bookmark. |
| 84 * @param url The URL of the new bookmark. | 88 * @param url The URL of the new bookmark. |
| 85 */ | 89 */ |
| 86 public static BookmarkId addBookmarkSilently(Context context, | 90 public static BookmarkId addBookmarkSilently(Context context, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 RecordUserAction.record("EnhancedBookmarks.EditAfterCreateButton
Clicked"); | 224 RecordUserAction.record("EnhancedBookmarks.EditAfterCreateButton
Clicked"); |
| 221 startEditActivity(activity, bookmarkId, (WebContents) actionData
); | 225 startEditActivity(activity, bookmarkId, (WebContents) actionData
); |
| 222 } | 226 } |
| 223 }; | 227 }; |
| 224 } | 228 } |
| 225 | 229 |
| 226 /** | 230 /** |
| 227 * Gets whether bookmark manager should load offline page initially. | 231 * Gets whether bookmark manager should load offline page initially. |
| 228 */ | 232 */ |
| 229 private static boolean shouldShowOfflinePageAtFirst(OfflinePageBridge bridge
) { | 233 private static boolean shouldShowOfflinePageAtFirst(OfflinePageBridge bridge
) { |
| 230 if (bridge == null || bridge.getAllPages().isEmpty() || OfflinePageUtils
.isConnected()) { | 234 return !OfflinePageUtils.isConnected() && bridge != null && bridge.hasPa
ges(); |
| 231 return false; | |
| 232 } | |
| 233 return true; | |
| 234 } | 235 } |
| 235 | 236 |
| 236 /** | 237 /** |
| 237 * Shows bookmark main UI. | 238 * Shows bookmark main UI. |
| 238 */ | 239 */ |
| 239 public static void showBookmarkManager(Activity activity) { | 240 public static void showBookmarkManager(Activity activity) { |
| 240 String url = getFirstUrlToLoad(activity); | 241 String url = getFirstUrlToLoad(activity); |
| 241 | 242 |
| 242 if (DeviceFormFactor.isTablet(activity)) { | 243 if (DeviceFormFactor.isTablet(activity)) { |
| 243 openUrl(activity, url); | 244 openUrl(activity, url); |
| 244 } else { | 245 } else { |
| 245 Intent intent = new Intent(activity, BookmarkActivity.class); | 246 Intent intent = new Intent(activity, BookmarkActivity.class); |
| 246 intent.setData(Uri.parse(url)); | 247 intent.setData(Uri.parse(url)); |
| 247 activity.startActivity(intent); | 248 activity.startActivity(intent); |
| 248 } | 249 } |
| 249 } | 250 } |
| 250 | 251 |
| 251 /** | 252 /** |
| 252 * The initial url the bookmark manager shows depends on offline page status
and some | 253 * The initial url the bookmark manager shows depends on offline page status
and some |
| 253 * experiments we run. | 254 * experiments we run. |
| 254 */ | 255 */ |
| 255 private static String getFirstUrlToLoad(Activity activity) { | 256 private static String getFirstUrlToLoad(Activity activity) { |
| 256 BookmarkModel model = new BookmarkModel(); | 257 BookmarkModel model = new BookmarkModel(); |
| 257 OfflinePageBridge bridge = model.getOfflinePageBridge(); | 258 OfflinePageBridge bridge = model.getOfflinePageBridge(); |
| 258 try { | 259 model.destroy(); |
| 259 if (shouldShowOfflinePageAtFirst(bridge)) { | 260 |
| 260 return BookmarkUIState.createFilterUrl(BookmarkFilter.OFFLINE_PA
GES, | 261 if (shouldShowOfflinePageAtFirst(bridge)) { |
| 261 false).toString(); | 262 return BookmarkUIState.createFilterUrl(BookmarkFilter.OFFLINE_PAGES,
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 } | 263 } |
| 264 |
| 265 String lastUsedUrl = getLastUsedUrl(activity); |
| 266 return TextUtils.isEmpty(lastUsedUrl) ? UrlConstants.BOOKMARKS_URL : las
tUsedUrl; |
| 269 } | 267 } |
| 270 | 268 |
| 271 /** | 269 /** |
| 272 * Saves the last used url to preference. The saved url will be later querie
d by | 270 * Saves the last used url to preference. The saved url will be later querie
d by |
| 273 * {@link #getLastUsedUrl(Context)} | 271 * {@link #getLastUsedUrl(Context)} |
| 274 */ | 272 */ |
| 275 static void setLastUsedUrl(Context context, String url) { | 273 static void setLastUsedUrl(Context context, String url) { |
| 276 PreferenceManager.getDefaultSharedPreferences(context).edit() | 274 PreferenceManager.getDefaultSharedPreferences(context).edit() |
| 277 .putString(PREF_LAST_USED_URL, url).apply(); | 275 .putString(PREF_LAST_USED_URL, url).apply(); |
| 278 } | 276 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 } | 385 } |
| 388 | 386 |
| 389 /** | 387 /** |
| 390 * Indicates whether we should skip saving the given tab as an offline page. | 388 * 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. | 389 * A tab shouldn't be saved offline if it shows an error page or a sad tab p
age. |
| 392 */ | 390 */ |
| 393 private static boolean shouldSkipSavingTabOffline(Tab tab) { | 391 private static boolean shouldSkipSavingTabOffline(Tab tab) { |
| 394 return tab.isShowingErrorPage() || tab.isShowingSadTab(); | 392 return tab.isShowingErrorPage() || tab.isShowingSadTab(); |
| 395 } | 393 } |
| 396 } | 394 } |
| OLD | NEW |