| 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.content.Context; | 7 import android.content.Context; |
| 8 import android.support.v7.widget.RecyclerView; | 8 import android.support.v7.widget.RecyclerView; |
| 9 import android.support.v7.widget.RecyclerView.ViewHolder; | 9 import android.support.v7.widget.RecyclerView.ViewHolder; |
| 10 import android.text.TextUtils; |
| 10 import android.view.LayoutInflater; | 11 import android.view.LayoutInflater; |
| 11 import android.view.View; | 12 import android.view.View; |
| 12 import android.view.ViewGroup; | 13 import android.view.ViewGroup; |
| 13 | 14 |
| 14 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | 16 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
| 17 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; | 18 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; |
| 18 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; | 19 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; |
| 19 import org.chromium.chrome.browser.bookmarks.BookmarkPromoHeader.PromoHeaderShow
ingChangeListener; | 20 import org.chromium.chrome.browser.bookmarks.BookmarkPromoHeader.PromoHeaderShow
ingChangeListener; |
| 20 import org.chromium.components.bookmarks.BookmarkId; | 21 import org.chromium.components.bookmarks.BookmarkId; |
| 21 import org.chromium.ui.base.DeviceFormFactor; | 22 import org.chromium.ui.base.DeviceFormFactor; |
| 22 | 23 |
| 23 import java.util.ArrayList; | 24 import java.util.ArrayList; |
| 24 import java.util.List; | 25 import java.util.List; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * BaseAdapter for {@link RecyclerView}. It manages bookmarks to list there. | 28 * BaseAdapter for {@link RecyclerView}. It manages bookmarks to list there. |
| 28 */ | 29 */ |
| 29 class BookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements | 30 class BookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements |
| 30 BookmarkUIObserver, PromoHeaderShowingChangeListener { | 31 BookmarkUIObserver, PromoHeaderShowingChangeListener { |
| 31 private static final int PROMO_HEADER_VIEW = 0; | 32 private static final int PROMO_HEADER_VIEW = 0; |
| 32 private static final int FOLDER_VIEW = 1; | 33 private static final int FOLDER_VIEW = 1; |
| 33 private static final int DIVIDER_VIEW = 2; | 34 private static final int DIVIDER_VIEW = 2; |
| 34 private static final int BOOKMARK_VIEW = 3; | 35 private static final int BOOKMARK_VIEW = 3; |
| 35 | 36 |
| 37 private static final int MAXIMUM_NUMBER_OF_SEARCH_RESULTS = 500; |
| 38 private static final String EMPTY_QUERY = null; |
| 39 |
| 36 private BookmarkDelegate mDelegate; | 40 private BookmarkDelegate mDelegate; |
| 37 private Context mContext; | 41 private Context mContext; |
| 38 private BookmarkPromoHeader mPromoHeaderManager; | 42 private BookmarkPromoHeader mPromoHeaderManager; |
| 39 private boolean mShouldShowDividers; | 43 private boolean mShouldShowDividers; |
| 44 private String mSearchText; |
| 40 | 45 |
| 41 private List<List<? extends Object>> mSections; | 46 private List<List<? extends Object>> mSections; |
| 42 private List<Object> mPromoHeaderSection = new ArrayList<>(); | 47 private List<Object> mPromoHeaderSection = new ArrayList<>(); |
| 43 private List<Object> mFolderDividerSection; | 48 private List<Object> mFolderDividerSection; |
| 44 private List<BookmarkId> mFolderSection = new ArrayList<>(); | 49 private List<BookmarkId> mFolderSection = new ArrayList<>(); |
| 45 private List<Object> mBookmarkDividerSection; | 50 private List<Object> mBookmarkDividerSection; |
| 46 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); | 51 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); |
| 47 | 52 |
| 48 private List<BookmarkRow> mBookmarkRows = new ArrayList<>(); | 53 private List<BookmarkRow> mBookmarkRows = new ArrayList<>(); |
| 49 private List<BookmarkRow> mFolderRows = new ArrayList<>(); | 54 private List<BookmarkRow> mFolderRows = new ArrayList<>(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 67 if (deletedPosition >= 0) { | 72 if (deletedPosition >= 0) { |
| 68 removeItem(deletedPosition); | 73 removeItem(deletedPosition); |
| 69 } | 74 } |
| 70 } | 75 } |
| 71 } | 76 } |
| 72 | 77 |
| 73 @Override | 78 @Override |
| 74 public void bookmarkModelChanged() { | 79 public void bookmarkModelChanged() { |
| 75 assert mDelegate != null; | 80 assert mDelegate != null; |
| 76 mDelegate.notifyStateChange(BookmarkItemsAdapter.this); | 81 mDelegate.notifyStateChange(BookmarkItemsAdapter.this); |
| 82 |
| 83 if (mDelegate.getCurrentState() == BookmarkUIState.STATE_SEARCHING |
| 84 && !TextUtils.equals(mSearchText, EMPTY_QUERY)) { |
| 85 search(mSearchText); |
| 86 } |
| 77 } | 87 } |
| 78 }; | 88 }; |
| 79 | 89 |
| 80 BookmarkItemsAdapter(Context context) { | 90 BookmarkItemsAdapter(Context context) { |
| 81 mContext = context; | 91 mContext = context; |
| 82 | 92 |
| 83 // TODO(twellington): remove dividers entirely after the bookmarks 720dp
layout is restyled | 93 // TODO(twellington): remove dividers entirely after the bookmarks 720dp
layout is restyled |
| 84 // to match the < 720dp style. | 94 // to match the < 720dp style. |
| 85 mShouldShowDividers = DeviceFormFactor.isLargeTablet(context); | 95 mShouldShowDividers = DeviceFormFactor.isLargeTablet(context); |
| 86 | 96 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 * @param folders This can be null if there is no folders to show. | 156 * @param folders This can be null if there is no folders to show. |
| 147 */ | 157 */ |
| 148 private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmar
ks) { | 158 private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmar
ks) { |
| 149 if (folders == null) folders = new ArrayList<BookmarkId>(); | 159 if (folders == null) folders = new ArrayList<BookmarkId>(); |
| 150 | 160 |
| 151 mFolderSection.clear(); | 161 mFolderSection.clear(); |
| 152 mFolderSection.addAll(folders); | 162 mFolderSection.addAll(folders); |
| 153 mBookmarkSection.clear(); | 163 mBookmarkSection.clear(); |
| 154 mBookmarkSection.addAll(bookmarks); | 164 mBookmarkSection.addAll(bookmarks); |
| 155 | 165 |
| 156 updateHeader(); | 166 updateHeaderAndNotify(); |
| 157 updateDividerSections(); | |
| 158 | |
| 159 // TODO(kkimlabs): Animation is disabled due to a performance issue on b
ookmark undo. | |
| 160 // http://crbug.com/484174 | |
| 161 notifyDataSetChanged(); | |
| 162 } | 167 } |
| 163 | 168 |
| 164 private void updateDividerSections() { | 169 private void updateDividerSections() { |
| 165 if (!mShouldShowDividers) return; | 170 if (!mShouldShowDividers) return; |
| 166 | 171 |
| 167 mFolderDividerSection.clear(); | 172 mFolderDividerSection.clear(); |
| 168 mBookmarkDividerSection.clear(); | 173 mBookmarkDividerSection.clear(); |
| 169 | 174 |
| 170 boolean isHeaderPresent = !mPromoHeaderSection.isEmpty(); | 175 boolean isHeaderPresent = !mPromoHeaderSection.isEmpty(); |
| 171 | 176 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 283 |
| 279 // PromoHeaderShowingChangeListener implementation. | 284 // PromoHeaderShowingChangeListener implementation. |
| 280 | 285 |
| 281 @Override | 286 @Override |
| 282 public void onPromoHeaderShowingChanged(boolean isShowing) { | 287 public void onPromoHeaderShowingChanged(boolean isShowing) { |
| 283 assert mDelegate != null; | 288 assert mDelegate != null; |
| 284 if (mDelegate.getCurrentState() != BookmarkUIState.STATE_FOLDER) { | 289 if (mDelegate.getCurrentState() != BookmarkUIState.STATE_FOLDER) { |
| 285 return; | 290 return; |
| 286 } | 291 } |
| 287 | 292 |
| 288 updateHeader(); | 293 updateHeaderAndNotify(); |
| 289 updateDividerSections(); | |
| 290 notifyDataSetChanged(); | |
| 291 } | 294 } |
| 292 | 295 |
| 293 // BookmarkUIObserver implementations. | 296 // BookmarkUIObserver implementations. |
| 294 | 297 |
| 295 @Override | 298 @Override |
| 296 public void onBookmarkDelegateInitialized(BookmarkDelegate delegate) { | 299 public void onBookmarkDelegateInitialized(BookmarkDelegate delegate) { |
| 297 mDelegate = delegate; | 300 mDelegate = delegate; |
| 298 mDelegate.addUIObserver(this); | 301 mDelegate.addUIObserver(this); |
| 299 mDelegate.getModel().addObserver(mBookmarkModelObserver); | 302 mDelegate.getModel().addObserver(mBookmarkModelObserver); |
| 300 mPromoHeaderManager = new BookmarkPromoHeader(mContext, this); | 303 mPromoHeaderManager = new BookmarkPromoHeader(mContext, this); |
| 301 } | 304 } |
| 302 | 305 |
| 303 @Override | 306 @Override |
| 304 public void onDestroy() { | 307 public void onDestroy() { |
| 305 mDelegate.removeUIObserver(this); | 308 mDelegate.removeUIObserver(this); |
| 306 mDelegate.getModel().removeObserver(mBookmarkModelObserver); | 309 mDelegate.getModel().removeObserver(mBookmarkModelObserver); |
| 307 mDelegate = null; | 310 mDelegate = null; |
| 308 | 311 |
| 309 mPromoHeaderManager.destroy(); | 312 mPromoHeaderManager.destroy(); |
| 310 } | 313 } |
| 311 | 314 |
| 312 @Override | 315 @Override |
| 313 public void onFolderStateSet(BookmarkId folder) { | 316 public void onFolderStateSet(BookmarkId folder) { |
| 314 assert mDelegate != null; | 317 assert mDelegate != null; |
| 315 setBookmarks(mDelegate.getModel().getChildIDs(folder, true, false), | 318 setBookmarks(mDelegate.getModel().getChildIDs(folder, true, false), |
| 316 mDelegate.getModel().getChildIDs(folder, false, true)); | 319 mDelegate.getModel().getChildIDs(folder, false, true)); |
| 320 |
| 321 mSearchText = EMPTY_QUERY; |
| 322 } |
| 323 |
| 324 @Override |
| 325 public void onSearchStateSet() { |
| 326 updateHeaderAndNotify(); |
| 317 } | 327 } |
| 318 | 328 |
| 319 @Override | 329 @Override |
| 320 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) {} | 330 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) {} |
| 321 | 331 |
| 332 /** |
| 333 * Synchronously searches for the given query. |
| 334 * @param query The query text to search for. |
| 335 */ |
| 336 void search(String query) { |
| 337 mSearchText = query.toString().trim(); |
| 338 List<BookmarkId> results = |
| 339 mDelegate.getModel().searchBookmarks(mSearchText, MAXIMUM_NUMBER
_OF_SEARCH_RESULTS); |
| 340 setBookmarks(null, results); |
| 341 } |
| 342 |
| 322 private static class ItemViewHolder extends RecyclerView.ViewHolder { | 343 private static class ItemViewHolder extends RecyclerView.ViewHolder { |
| 323 private ItemViewHolder(View view) { | 344 private ItemViewHolder(View view) { |
| 324 super(view); | 345 super(view); |
| 325 } | 346 } |
| 326 } | 347 } |
| 327 | 348 |
| 349 private void updateHeaderAndNotify() { |
| 350 updateHeader(); |
| 351 updateDividerSections(); |
| 352 notifyDataSetChanged(); |
| 353 } |
| 354 |
| 328 private void updateHeader() { | 355 private void updateHeader() { |
| 329 if (mDelegate == null) return; | 356 if (mDelegate == null) return; |
| 330 | 357 |
| 331 int currentUIState = mDelegate.getCurrentState(); | 358 int currentUIState = mDelegate.getCurrentState(); |
| 332 if (currentUIState == BookmarkUIState.STATE_LOADING) return; | 359 if (currentUIState == BookmarkUIState.STATE_LOADING) return; |
| 333 | 360 |
| 334 mPromoHeaderSection.clear(); | 361 mPromoHeaderSection.clear(); |
| 362 |
| 363 if (currentUIState == BookmarkUIState.STATE_SEARCHING) return; |
| 364 |
| 335 assert currentUIState == BookmarkUIState.STATE_FOLDER : "Unexpected UI s
tate"; | 365 assert currentUIState == BookmarkUIState.STATE_FOLDER : "Unexpected UI s
tate"; |
| 336 if (mPromoHeaderManager.shouldShow()) { | 366 if (mPromoHeaderManager.shouldShow()) { |
| 337 mPromoHeaderSection.add(null); | 367 mPromoHeaderSection.add(null); |
| 338 } | 368 } |
| 339 } | 369 } |
| 340 | 370 |
| 341 @VisibleForTesting | 371 @VisibleForTesting |
| 342 public BookmarkDelegate getDelegateForTesting() { | 372 public BookmarkDelegate getDelegateForTesting() { |
| 343 return mDelegate; | 373 return mDelegate; |
| 344 } | 374 } |
| 345 | 375 |
| 346 private void setBackgroundResourceForBookmarkRow(BookmarkRow row, BookmarkId
id) { | 376 private void setBackgroundResourceForBookmarkRow(BookmarkRow row, BookmarkId
id) { |
| 347 row.setBackgroundResourceForGroupPosition(id.equals(mBookmarkSection.get
(0)), | 377 row.setBackgroundResourceForGroupPosition(id.equals(mBookmarkSection.get
(0)), |
| 348 id.equals(mBookmarkSection.get(mBookmarkSection.size() - 1))); | 378 id.equals(mBookmarkSection.get(mBookmarkSection.size() - 1))); |
| 349 } | 379 } |
| 350 | 380 |
| 351 private void setBackgroundResourceForFolderRow(BookmarkRow row, BookmarkId i
d) { | 381 private void setBackgroundResourceForFolderRow(BookmarkRow row, BookmarkId i
d) { |
| 352 row.setBackgroundResourceForGroupPosition(id.equals(mFolderSection.get(0
)), | 382 row.setBackgroundResourceForGroupPosition(id.equals(mFolderSection.get(0
)), |
| 353 id.equals(mFolderSection.get(mFolderSection.size() - 1))); | 383 id.equals(mFolderSection.get(mFolderSection.size() - 1))); |
| 354 } | 384 } |
| 355 } | 385 } |
| OLD | NEW |