| 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.app.ActivityManager; | 8 import android.app.ActivityManager; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.support.graphics.drawable.VectorDrawableCompat; | 10 import android.support.graphics.drawable.VectorDrawableCompat; |
| 11 import android.support.v4.view.GravityCompat; | |
| 12 import android.support.v4.widget.DrawerLayout; | |
| 13 import android.support.v7.widget.RecyclerView; | 11 import android.support.v7.widget.RecyclerView; |
| 14 import android.support.v7.widget.Toolbar; | 12 import android.support.v7.widget.Toolbar; |
| 15 import android.text.TextUtils; | 13 import android.text.TextUtils; |
| 16 import android.view.View; | 14 import android.view.View; |
| 17 import android.view.ViewGroup; | 15 import android.view.ViewGroup; |
| 18 | 16 |
| 19 import org.chromium.base.ContextUtils; | 17 import org.chromium.base.ContextUtils; |
| 20 import org.chromium.base.ObserverList; | 18 import org.chromium.base.ObserverList; |
| 19 import org.chromium.base.VisibleForTesting; |
| 21 import org.chromium.base.metrics.RecordUserAction; | 20 import org.chromium.base.metrics.RecordUserAction; |
| 22 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
| 23 import org.chromium.chrome.browser.BasicNativePage; | 22 import org.chromium.chrome.browser.BasicNativePage; |
| 24 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; | 23 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; |
| 25 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; | 24 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; |
| 26 import org.chromium.chrome.browser.favicon.LargeIconBridge; | 25 import org.chromium.chrome.browser.favicon.LargeIconBridge; |
| 27 import org.chromium.chrome.browser.partnerbookmarks.PartnerBookmarksShim; | 26 import org.chromium.chrome.browser.partnerbookmarks.PartnerBookmarksShim; |
| 28 import org.chromium.chrome.browser.profiles.Profile; | 27 import org.chromium.chrome.browser.profiles.Profile; |
| 29 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 28 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 30 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | 29 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 52 private ViewGroup mMainView; | 51 private ViewGroup mMainView; |
| 53 private BookmarkModel mBookmarkModel; | 52 private BookmarkModel mBookmarkModel; |
| 54 private BookmarkUndoController mUndoController; | 53 private BookmarkUndoController mUndoController; |
| 55 private final ObserverList<BookmarkUIObserver> mUIObservers = | 54 private final ObserverList<BookmarkUIObserver> mUIObservers = |
| 56 new ObserverList<BookmarkUIObserver>(); | 55 new ObserverList<BookmarkUIObserver>(); |
| 57 private BasicNativePage mNativePage; | 56 private BasicNativePage mNativePage; |
| 58 private SelectableListLayout<BookmarkId> mSelectableListLayout; | 57 private SelectableListLayout<BookmarkId> mSelectableListLayout; |
| 59 private RecyclerView mRecyclerView; | 58 private RecyclerView mRecyclerView; |
| 60 private BookmarkItemsAdapter mAdapter; | 59 private BookmarkItemsAdapter mAdapter; |
| 61 private BookmarkActionBar mToolbar; | 60 private BookmarkActionBar mToolbar; |
| 62 private DrawerLayout mDrawer; | |
| 63 private BookmarkDrawerListView mDrawerListView; | |
| 64 private SelectionDelegate<BookmarkId> mSelectionDelegate; | 61 private SelectionDelegate<BookmarkId> mSelectionDelegate; |
| 65 private final Stack<BookmarkUIState> mStateStack = new Stack<>(); | 62 private final Stack<BookmarkUIState> mStateStack = new Stack<>(); |
| 66 private LargeIconBridge mLargeIconBridge; | 63 private LargeIconBridge mLargeIconBridge; |
| 67 private String mInitialUrl; | 64 private String mInitialUrl; |
| 68 private boolean mIsDialogUi; | 65 private boolean mIsDialogUi; |
| 69 | 66 |
| 70 private final BookmarkModelObserver mBookmarkModelObserver = new BookmarkMod
elObserver() { | 67 private final BookmarkModelObserver mBookmarkModelObserver = new BookmarkMod
elObserver() { |
| 71 | 68 |
| 72 @Override | 69 @Override |
| 73 public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookm
arkItem node, | 70 public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookm
arkItem node, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 99 if (getCurrentState() == BookmarkUIState.STATE_FOLDER) { | 96 if (getCurrentState() == BookmarkUIState.STATE_FOLDER) { |
| 100 setState(mStateStack.peek()); | 97 setState(mStateStack.peek()); |
| 101 } | 98 } |
| 102 mSelectionDelegate.clearSelection(); | 99 mSelectionDelegate.clearSelection(); |
| 103 } | 100 } |
| 104 }; | 101 }; |
| 105 | 102 |
| 106 private final Runnable mModelLoadedRunnable = new Runnable() { | 103 private final Runnable mModelLoadedRunnable = new Runnable() { |
| 107 @Override | 104 @Override |
| 108 public void run() { | 105 public void run() { |
| 109 mDrawerListView.onBookmarkDelegateInitialized(BookmarkManager.this); | |
| 110 mAdapter.onBookmarkDelegateInitialized(BookmarkManager.this); | 106 mAdapter.onBookmarkDelegateInitialized(BookmarkManager.this); |
| 111 mToolbar.onBookmarkDelegateInitialized(BookmarkManager.this); | 107 mToolbar.onBookmarkDelegateInitialized(BookmarkManager.this); |
| 112 if (!TextUtils.isEmpty(mInitialUrl)) { | 108 if (!TextUtils.isEmpty(mInitialUrl)) { |
| 113 setState(BookmarkUIState.createStateFromUrl(mInitialUrl, | 109 setState(BookmarkUIState.createStateFromUrl(mInitialUrl, |
| 114 mBookmarkModel)); | 110 mBookmarkModel)); |
| 115 } | 111 } |
| 116 } | 112 } |
| 117 }; | 113 }; |
| 118 | 114 |
| 119 /** | 115 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 130 mSelectionDelegate = new SelectionDelegate<BookmarkId>() { | 126 mSelectionDelegate = new SelectionDelegate<BookmarkId>() { |
| 131 @Override | 127 @Override |
| 132 public boolean toggleSelectionForItem(BookmarkId bookmark) { | 128 public boolean toggleSelectionForItem(BookmarkId bookmark) { |
| 133 if (!mBookmarkModel.getBookmarkById(bookmark).isEditable()) retu
rn false; | 129 if (!mBookmarkModel.getBookmarkById(bookmark).isEditable()) retu
rn false; |
| 134 return super.toggleSelectionForItem(bookmark); | 130 return super.toggleSelectionForItem(bookmark); |
| 135 } | 131 } |
| 136 }; | 132 }; |
| 137 | 133 |
| 138 mBookmarkModel = new BookmarkModel(); | 134 mBookmarkModel = new BookmarkModel(); |
| 139 mMainView = (ViewGroup) mActivity.getLayoutInflater().inflate(R.layout.b
ookmark_main, null); | 135 mMainView = (ViewGroup) mActivity.getLayoutInflater().inflate(R.layout.b
ookmark_main, null); |
| 140 mDrawer = (DrawerLayout) mMainView.findViewById(R.id.bookmark_drawer_lay
out); | |
| 141 mDrawerListView = (BookmarkDrawerListView) mMainView.findViewById( | |
| 142 R.id.bookmark_drawer_list); | |
| 143 | 136 |
| 144 @SuppressWarnings("unchecked") | 137 @SuppressWarnings("unchecked") |
| 145 SelectableListLayout<BookmarkId> selectableList = | 138 SelectableListLayout<BookmarkId> selectableList = |
| 146 (SelectableListLayout<BookmarkId>) mMainView.findViewById(R.id.s
electable_list); | 139 (SelectableListLayout<BookmarkId>) mMainView.findViewById(R.id.s
electable_list); |
| 147 mSelectableListLayout = selectableList; | 140 mSelectableListLayout = selectableList; |
| 148 mSelectableListLayout.initializeEmptyView( | 141 mSelectableListLayout.initializeEmptyView( |
| 149 VectorDrawableCompat.create( | 142 VectorDrawableCompat.create( |
| 150 mActivity.getResources(), R.drawable.bookmark_big, mActi
vity.getTheme()), | 143 mActivity.getResources(), R.drawable.bookmark_big, mActi
vity.getTheme()), |
| 151 R.string.bookmarks_folder_empty, R.string.bookmark_no_result); | 144 R.string.bookmarks_folder_empty, R.string.bookmark_no_result); |
| 152 | 145 |
| 153 mAdapter = new BookmarkItemsAdapter(activity); | 146 mAdapter = new BookmarkItemsAdapter(activity); |
| 154 | 147 |
| 155 mRecyclerView = mSelectableListLayout.initializeRecyclerView(mAdapter); | 148 mRecyclerView = mSelectableListLayout.initializeRecyclerView(mAdapter); |
| 156 | 149 |
| 157 mToolbar = (BookmarkActionBar) mSelectableListLayout.initializeToolbar( | 150 mToolbar = (BookmarkActionBar) mSelectableListLayout.initializeToolbar( |
| 158 R.layout.bookmark_action_bar, mSelectionDelegate, 0, mDrawer, | 151 R.layout.bookmark_action_bar, mSelectionDelegate, 0, null, R.id.
normal_menu_group, |
| 159 R.id.normal_menu_group, R.id.selection_mode_menu_group, null, tr
ue, null); | 152 R.id.selection_mode_menu_group, R.color.default_primary_color, f
alse, null); |
| 160 mToolbar.initializeSearchView( | 153 mToolbar.initializeSearchView( |
| 161 this, R.string.bookmark_action_bar_search, R.id.search_menu_id); | 154 this, R.string.bookmark_action_bar_search, R.id.search_menu_id); |
| 162 | 155 |
| 163 mUndoController = new BookmarkUndoController(activity, mBookmarkModel, s
nackbarManager); | 156 mUndoController = new BookmarkUndoController(activity, mBookmarkModel, s
nackbarManager); |
| 164 mBookmarkModel.addObserver(mBookmarkModelObserver); | 157 mBookmarkModel.addObserver(mBookmarkModelObserver); |
| 165 initializeToLoadingState(); | 158 initializeToLoadingState(); |
| 166 mBookmarkModel.runAfterBookmarkModelLoaded(mModelLoadedRunnable); | 159 mBookmarkModel.runAfterBookmarkModelLoaded(mModelLoadedRunnable); |
| 167 | 160 |
| 168 // Load partner bookmarks explicitly. We load partner bookmarks in the d
eferred startup | 161 // Load partner bookmarks explicitly. We load partner bookmarks in the d
eferred startup |
| 169 // code, but that might be executed much later. Especially on L, showing
loading | 162 // code, but that might be executed much later. Especially on L, showing
loading |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 mBookmarkModel = null; | 200 mBookmarkModel = null; |
| 208 mLargeIconBridge.destroy(); | 201 mLargeIconBridge.destroy(); |
| 209 mLargeIconBridge = null; | 202 mLargeIconBridge = null; |
| 210 } | 203 } |
| 211 | 204 |
| 212 /** | 205 /** |
| 213 * Called when the user presses the back key. This is only going to be calle
d on Phone. | 206 * Called when the user presses the back key. This is only going to be calle
d on Phone. |
| 214 * @return True if manager handles this event, false if it decides to ignore
. | 207 * @return True if manager handles this event, false if it decides to ignore
. |
| 215 */ | 208 */ |
| 216 public boolean onBackPressed() { | 209 public boolean onBackPressed() { |
| 217 if (doesDrawerExist()) { | |
| 218 if (mDrawer.isDrawerVisible(GravityCompat.START)) { | |
| 219 mDrawer.closeDrawer(GravityCompat.START); | |
| 220 return true; | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 // TODO(twellington): replicate this behavior for other list UIs during
unification. | 210 // TODO(twellington): replicate this behavior for other list UIs during
unification. |
| 225 if (mSelectionDelegate.isSelectionEnabled()) { | 211 if (mSelectionDelegate.isSelectionEnabled()) { |
| 226 mSelectionDelegate.clearSelection(); | 212 mSelectionDelegate.clearSelection(); |
| 227 return true; | 213 return true; |
| 228 } | 214 } |
| 229 | 215 |
| 230 if (!mStateStack.empty()) { | 216 if (!mStateStack.empty()) { |
| 231 mStateStack.pop(); | 217 mStateStack.pop(); |
| 232 if (!mStateStack.empty()) { | 218 if (!mStateStack.empty()) { |
| 233 setState(mStateStack.pop()); | 219 setState(mStateStack.pop()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 mInitialUrl = url; | 275 mInitialUrl = url; |
| 290 } | 276 } |
| 291 } | 277 } |
| 292 | 278 |
| 293 /** | 279 /** |
| 294 * Puts all UI elements to loading state. This state might be overridden syn
chronously by | 280 * Puts all UI elements to loading state. This state might be overridden syn
chronously by |
| 295 * {@link #updateForUrl(String)}, if the bookmark model is already loaded. | 281 * {@link #updateForUrl(String)}, if the bookmark model is already loaded. |
| 296 */ | 282 */ |
| 297 private void initializeToLoadingState() { | 283 private void initializeToLoadingState() { |
| 298 mToolbar.showLoadingUi(); | 284 mToolbar.showLoadingUi(); |
| 299 mDrawerListView.showLoadingUi(); | |
| 300 assert mStateStack.isEmpty(); | 285 assert mStateStack.isEmpty(); |
| 301 setState(BookmarkUIState.createLoadingState()); | 286 setState(BookmarkUIState.createLoadingState()); |
| 302 } | 287 } |
| 303 | 288 |
| 304 /** | 289 /** |
| 305 * This is the ultimate internal method that updates UI and controls backsta
ck. And it is the | 290 * This is the ultimate internal method that updates UI and controls backsta
ck. And it is the |
| 306 * only method that pushes states to {@link #mStateStack}. | 291 * only method that pushes states to {@link #mStateStack}. |
| 307 * <p> | 292 * <p> |
| 308 * If the given state is not valid, all_bookmark state will be shown. Afterw
ards, this method | 293 * If the given state is not valid, all_bookmark state will be shown. Afterw
ards, this method |
| 309 * checks the current state: if currently in loading state, it pops it out a
nd adds the new | 294 * checks the current state: if currently in loading state, it pops it out a
nd adds the new |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 case BookmarkUIState.STATE_SEARCHING: | 366 case BookmarkUIState.STATE_SEARCHING: |
| 382 observer.onSearchStateSet(); | 367 observer.onSearchStateSet(); |
| 383 break; | 368 break; |
| 384 default: | 369 default: |
| 385 assert false : "State not valid"; | 370 assert false : "State not valid"; |
| 386 break; | 371 break; |
| 387 } | 372 } |
| 388 } | 373 } |
| 389 | 374 |
| 390 @Override | 375 @Override |
| 391 public boolean doesDrawerExist() { | |
| 392 return mDrawer != null; | |
| 393 } | |
| 394 | |
| 395 @Override | |
| 396 public void closeDrawer() { | |
| 397 if (!doesDrawerExist()) return; | |
| 398 | |
| 399 mDrawer.closeDrawer(GravityCompat.START); | |
| 400 } | |
| 401 | |
| 402 @Override | |
| 403 public DrawerLayout getDrawerLayout() { | |
| 404 return mDrawer; | |
| 405 } | |
| 406 | |
| 407 @Override | |
| 408 public void openBookmark(BookmarkId bookmark, int launchLocation) { | 376 public void openBookmark(BookmarkId bookmark, int launchLocation) { |
| 409 mSelectionDelegate.clearSelection(); | 377 mSelectionDelegate.clearSelection(); |
| 410 if (BookmarkUtils.openBookmark( | 378 if (BookmarkUtils.openBookmark( |
| 411 mBookmarkModel, mActivity, bookmark, launchLocation)) { | 379 mBookmarkModel, mActivity, bookmark, launchLocation)) { |
| 412 BookmarkUtils.finishActivityOnPhone(mActivity); | 380 BookmarkUtils.finishActivityOnPhone(mActivity); |
| 413 } | 381 } |
| 414 } | 382 } |
| 415 | 383 |
| 416 @Override | 384 @Override |
| 417 public void openSearchUI() { | 385 public void openSearchUI() { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 431 |
| 464 @Override | 432 @Override |
| 465 public void onSearchTextChanged(String query) { | 433 public void onSearchTextChanged(String query) { |
| 466 mAdapter.search(query); | 434 mAdapter.search(query); |
| 467 } | 435 } |
| 468 | 436 |
| 469 @Override | 437 @Override |
| 470 public void onEndSearch() { | 438 public void onEndSearch() { |
| 471 closeSearchUI(); | 439 closeSearchUI(); |
| 472 } | 440 } |
| 441 |
| 442 // Testing methods |
| 443 |
| 444 @VisibleForTesting |
| 445 public BookmarkActionBar getToolbarForTests() { |
| 446 return mToolbar; |
| 447 } |
| 473 } | 448 } |
| OLD | NEW |