| 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.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
| 11 import android.view.View; | 11 import android.view.View; |
| 12 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
| 13 | 13 |
| 14 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
| 17 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; | 17 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem; |
| 18 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; | 18 import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkModelObserve
r; |
| 19 import org.chromium.chrome.browser.bookmarks.BookmarkPromoHeader.PromoHeaderShow
ingChangeListener; | 19 import org.chromium.chrome.browser.bookmarks.BookmarkPromoHeader.PromoHeaderShow
ingChangeListener; |
| 20 import org.chromium.components.bookmarks.BookmarkId; | 20 import org.chromium.components.bookmarks.BookmarkId; |
| 21 import org.chromium.ui.base.DeviceFormFactor; |
| 21 | 22 |
| 22 import java.util.ArrayList; | 23 import java.util.ArrayList; |
| 23 import java.util.List; | 24 import java.util.List; |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * BaseAdapter for {@link RecyclerView}. It manages bookmarks to list there. | 27 * BaseAdapter for {@link RecyclerView}. It manages bookmarks to list there. |
| 27 */ | 28 */ |
| 28 class BookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements | 29 class BookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements |
| 29 BookmarkUIObserver, PromoHeaderShowingChangeListener { | 30 BookmarkUIObserver, PromoHeaderShowingChangeListener { |
| 30 private static final int PROMO_HEADER_VIEW = 0; | 31 private static final int PROMO_HEADER_VIEW = 0; |
| 31 private static final int FOLDER_VIEW = 1; | 32 private static final int FOLDER_VIEW = 1; |
| 32 private static final int DIVIDER_VIEW = 2; | 33 private static final int DIVIDER_VIEW = 2; |
| 33 private static final int BOOKMARK_VIEW = 3; | 34 private static final int BOOKMARK_VIEW = 3; |
| 34 | 35 |
| 35 private BookmarkDelegate mDelegate; | 36 private BookmarkDelegate mDelegate; |
| 36 private Context mContext; | 37 private Context mContext; |
| 37 private BookmarkPromoHeader mPromoHeaderManager; | 38 private BookmarkPromoHeader mPromoHeaderManager; |
| 39 private boolean mShouldShowDividers; |
| 38 | 40 |
| 39 private List<List<? extends Object>> mSections; | 41 private List<List<? extends Object>> mSections; |
| 40 private List<Object> mPromoHeaderSection = new ArrayList<>(); | 42 private List<Object> mPromoHeaderSection = new ArrayList<>(); |
| 41 private List<Object> mFolderDividerSection = new ArrayList<>(); | 43 private List<Object> mFolderDividerSection; |
| 42 private List<BookmarkId> mFolderSection = new ArrayList<>(); | 44 private List<BookmarkId> mFolderSection = new ArrayList<>(); |
| 43 private List<Object> mBookmarkDividerSection = new ArrayList<>(); | 45 private List<Object> mBookmarkDividerSection; |
| 44 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); | 46 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); |
| 45 | 47 |
| 48 private List<BookmarkRow> mBookmarkRows = new ArrayList<>(); |
| 49 private List<BookmarkRow> mFolderRows = new ArrayList<>(); |
| 50 |
| 46 private BookmarkModelObserver mBookmarkModelObserver = new BookmarkModelObse
rver() { | 51 private BookmarkModelObserver mBookmarkModelObserver = new BookmarkModelObse
rver() { |
| 47 @Override | 52 @Override |
| 48 public void bookmarkNodeChanged(BookmarkItem node) { | 53 public void bookmarkNodeChanged(BookmarkItem node) { |
| 49 assert mDelegate != null; | 54 assert mDelegate != null; |
| 50 int position = getPositionForBookmark(node.getId()); | 55 int position = getPositionForBookmark(node.getId()); |
| 51 if (position >= 0) notifyItemChanged(position); | 56 if (position >= 0) notifyItemChanged(position); |
| 52 } | 57 } |
| 53 | 58 |
| 54 @Override | 59 @Override |
| 55 public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookm
arkItem node, | 60 public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookm
arkItem node, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 68 @Override | 73 @Override |
| 69 public void bookmarkModelChanged() { | 74 public void bookmarkModelChanged() { |
| 70 assert mDelegate != null; | 75 assert mDelegate != null; |
| 71 mDelegate.notifyStateChange(BookmarkItemsAdapter.this); | 76 mDelegate.notifyStateChange(BookmarkItemsAdapter.this); |
| 72 } | 77 } |
| 73 }; | 78 }; |
| 74 | 79 |
| 75 BookmarkItemsAdapter(Context context) { | 80 BookmarkItemsAdapter(Context context) { |
| 76 mContext = context; | 81 mContext = context; |
| 77 | 82 |
| 83 // TODO(twellington): remove dividers entirely after the bookmarks 720dp
layout is restyled |
| 84 // to match the < 720dp style. |
| 85 mShouldShowDividers = DeviceFormFactor.isLargeTablet(context); |
| 86 |
| 78 mSections = new ArrayList<>(); | 87 mSections = new ArrayList<>(); |
| 79 mSections.add(mPromoHeaderSection); | 88 mSections.add(mPromoHeaderSection); |
| 80 mSections.add(mFolderDividerSection); | 89 |
| 90 if (mShouldShowDividers) { |
| 91 mFolderDividerSection = new ArrayList<>(); |
| 92 mSections.add(mFolderDividerSection); |
| 93 } |
| 94 |
| 81 mSections.add(mFolderSection); | 95 mSections.add(mFolderSection); |
| 82 mSections.add(mBookmarkDividerSection); | 96 |
| 97 if (mShouldShowDividers) { |
| 98 mBookmarkDividerSection = new ArrayList<>(); |
| 99 mSections.add(mBookmarkDividerSection); |
| 100 } |
| 101 |
| 83 mSections.add(mBookmarkSection); | 102 mSections.add(mBookmarkSection); |
| 84 } | 103 } |
| 85 | 104 |
| 86 BookmarkId getItem(int position) { | 105 BookmarkId getItem(int position) { |
| 87 return (BookmarkId) getSection(position).get(toSectionPosition(position)
); | 106 return (BookmarkId) getSection(position).get(toSectionPosition(position)
); |
| 88 } | 107 } |
| 89 | 108 |
| 90 private int toSectionPosition(int globalPosition) { | 109 private int toSectionPosition(int globalPosition) { |
| 91 int sectionPosition = globalPosition; | 110 int sectionPosition = globalPosition; |
| 92 for (List<?> section : mSections) { | 111 for (List<?> section : mSections) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 155 |
| 137 updateHeader(); | 156 updateHeader(); |
| 138 updateDividerSections(); | 157 updateDividerSections(); |
| 139 | 158 |
| 140 // TODO(kkimlabs): Animation is disabled due to a performance issue on b
ookmark undo. | 159 // TODO(kkimlabs): Animation is disabled due to a performance issue on b
ookmark undo. |
| 141 // http://crbug.com/484174 | 160 // http://crbug.com/484174 |
| 142 notifyDataSetChanged(); | 161 notifyDataSetChanged(); |
| 143 } | 162 } |
| 144 | 163 |
| 145 private void updateDividerSections() { | 164 private void updateDividerSections() { |
| 165 if (!mShouldShowDividers) return; |
| 166 |
| 146 mFolderDividerSection.clear(); | 167 mFolderDividerSection.clear(); |
| 147 mBookmarkDividerSection.clear(); | 168 mBookmarkDividerSection.clear(); |
| 148 | 169 |
| 149 boolean isHeaderPresent = !mPromoHeaderSection.isEmpty(); | 170 boolean isHeaderPresent = !mPromoHeaderSection.isEmpty(); |
| 150 | 171 |
| 151 if (isHeaderPresent && !mFolderSection.isEmpty()) { | 172 if (isHeaderPresent && !mFolderSection.isEmpty()) { |
| 152 mFolderDividerSection.add(null); | 173 mFolderDividerSection.add(null); |
| 153 } | 174 } |
| 154 if ((isHeaderPresent || !mFolderSection.isEmpty()) && !mBookmarkSection.
isEmpty()) { | 175 if ((isHeaderPresent || !mFolderSection.isEmpty()) && !mBookmarkSection.
isEmpty()) { |
| 155 mBookmarkDividerSection.add(null); | 176 mBookmarkDividerSection.add(null); |
| 156 } | 177 } |
| 157 } | 178 } |
| 158 | 179 |
| 159 private void removeItem(int position) { | 180 private void removeItem(int position) { |
| 160 List<?> section = getSection(position); | 181 List<?> section = getSection(position); |
| 161 assert section == mFolderSection || section == mBookmarkSection; | 182 assert section == mFolderSection || section == mBookmarkSection; |
| 162 section.remove(toSectionPosition(position)); | 183 section.remove(toSectionPosition(position)); |
| 163 notifyItemRemoved(position); | 184 notifyItemRemoved(position); |
| 185 |
| 186 if (section == mBookmarkSection && !mBookmarkSection.isEmpty()) { |
| 187 for (BookmarkRow row : mBookmarkRows) { |
| 188 BookmarkId id = row.getItem(); |
| 189 setBackgroundResourceForBookmarkRow(row, id); |
| 190 } |
| 191 } else if (!mFolderSection.isEmpty()) { |
| 192 for (BookmarkRow row : mFolderRows) { |
| 193 BookmarkId id = row.getItem(); |
| 194 setBackgroundResourceForFolderRow(row, id); |
| 195 } |
| 196 } |
| 164 } | 197 } |
| 165 | 198 |
| 166 // RecyclerView.Adapter implementation. | 199 // RecyclerView.Adapter implementation. |
| 167 | 200 |
| 168 @Override | 201 @Override |
| 169 public int getItemCount() { | 202 public int getItemCount() { |
| 170 int count = 0; | 203 int count = 0; |
| 171 for (List<?> section : mSections) { | 204 for (List<?> section : mSections) { |
| 172 count += section.size(); | 205 count += section.size(); |
| 173 } | 206 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 200 switch (viewType) { | 233 switch (viewType) { |
| 201 case PROMO_HEADER_VIEW: | 234 case PROMO_HEADER_VIEW: |
| 202 return mPromoHeaderManager.createHolder(parent); | 235 return mPromoHeaderManager.createHolder(parent); |
| 203 case DIVIDER_VIEW: | 236 case DIVIDER_VIEW: |
| 204 return new ViewHolder(LayoutInflater.from(parent.getContext()).i
nflate( | 237 return new ViewHolder(LayoutInflater.from(parent.getContext()).i
nflate( |
| 205 R.layout.bookmark_divider, parent, false)) {}; | 238 R.layout.bookmark_divider, parent, false)) {}; |
| 206 case FOLDER_VIEW: | 239 case FOLDER_VIEW: |
| 207 BookmarkFolderRow folder = (BookmarkFolderRow) LayoutInflater.fr
om( | 240 BookmarkFolderRow folder = (BookmarkFolderRow) LayoutInflater.fr
om( |
| 208 parent.getContext()).inflate(R.layout.bookmark_folder_ro
w, parent, false); | 241 parent.getContext()).inflate(R.layout.bookmark_folder_ro
w, parent, false); |
| 209 folder.onBookmarkDelegateInitialized(mDelegate); | 242 folder.onBookmarkDelegateInitialized(mDelegate); |
| 243 mFolderRows.add(folder); |
| 210 return new ItemViewHolder(folder); | 244 return new ItemViewHolder(folder); |
| 211 case BOOKMARK_VIEW: | 245 case BOOKMARK_VIEW: |
| 212 BookmarkItemRow item = (BookmarkItemRow) LayoutInflater.from( | 246 BookmarkItemRow item = (BookmarkItemRow) LayoutInflater.from( |
| 213 parent.getContext()).inflate(R.layout.bookmark_item_row,
parent, false); | 247 parent.getContext()).inflate(R.layout.bookmark_item_row,
parent, false); |
| 214 item.onBookmarkDelegateInitialized(mDelegate); | 248 item.onBookmarkDelegateInitialized(mDelegate); |
| 249 mBookmarkRows.add(item); |
| 215 return new ItemViewHolder(item); | 250 return new ItemViewHolder(item); |
| 216 default: | 251 default: |
| 217 assert false; | 252 assert false; |
| 218 return null; | 253 return null; |
| 219 } | 254 } |
| 220 } | 255 } |
| 221 | 256 |
| 222 @SuppressFBWarnings("BC_UNCONFIRMED_CAST") | 257 @SuppressFBWarnings("BC_UNCONFIRMED_CAST") |
| 223 @Override | 258 @Override |
| 224 public void onBindViewHolder(ViewHolder holder, int position) { | 259 public void onBindViewHolder(ViewHolder holder, int position) { |
| 225 BookmarkId id = getItem(position); | 260 BookmarkId id = getItem(position); |
| 226 | 261 |
| 227 switch (getItemViewType(position)) { | 262 switch (getItemViewType(position)) { |
| 228 case PROMO_HEADER_VIEW: | 263 case PROMO_HEADER_VIEW: |
| 229 case DIVIDER_VIEW: | 264 case DIVIDER_VIEW: |
| 230 break; | 265 break; |
| 231 case FOLDER_VIEW: | 266 case FOLDER_VIEW: |
| 232 ((BookmarkRow) holder.itemView).setBookmarkId(id); | 267 ((BookmarkRow) holder.itemView).setBookmarkId(id); |
| 268 setBackgroundResourceForFolderRow(((BookmarkRow) holder.itemView
), id); |
| 233 break; | 269 break; |
| 234 case BOOKMARK_VIEW: | 270 case BOOKMARK_VIEW: |
| 235 ((BookmarkRow) holder.itemView).setBookmarkId(id); | 271 ((BookmarkRow) holder.itemView).setBookmarkId(id); |
| 272 setBackgroundResourceForBookmarkRow((BookmarkRow) holder.itemVie
w, id); |
| 236 break; | 273 break; |
| 237 default: | 274 default: |
| 238 assert false : "View type not supported!"; | 275 assert false : "View type not supported!"; |
| 239 } | 276 } |
| 240 } | 277 } |
| 241 | 278 |
| 242 // PromoHeaderShowingChangeListener implementation. | 279 // PromoHeaderShowingChangeListener implementation. |
| 243 | 280 |
| 244 @Override | 281 @Override |
| 245 public void onPromoHeaderShowingChanged(boolean isShowing) { | 282 public void onPromoHeaderShowingChanged(boolean isShowing) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 assert currentUIState == BookmarkUIState.STATE_FOLDER : "Unexpected UI s
tate"; | 335 assert currentUIState == BookmarkUIState.STATE_FOLDER : "Unexpected UI s
tate"; |
| 299 if (mPromoHeaderManager.shouldShow()) { | 336 if (mPromoHeaderManager.shouldShow()) { |
| 300 mPromoHeaderSection.add(null); | 337 mPromoHeaderSection.add(null); |
| 301 } | 338 } |
| 302 } | 339 } |
| 303 | 340 |
| 304 @VisibleForTesting | 341 @VisibleForTesting |
| 305 public BookmarkDelegate getDelegateForTesting() { | 342 public BookmarkDelegate getDelegateForTesting() { |
| 306 return mDelegate; | 343 return mDelegate; |
| 307 } | 344 } |
| 345 |
| 346 private void setBackgroundResourceForBookmarkRow(BookmarkRow row, BookmarkId
id) { |
| 347 row.setBackgroundResourceForGroupPosition(id.equals(mBookmarkSection.get
(0)), |
| 348 id.equals(mBookmarkSection.get(mBookmarkSection.size() - 1))); |
| 349 } |
| 350 |
| 351 private void setBackgroundResourceForFolderRow(BookmarkRow row, BookmarkId i
d) { |
| 352 row.setBackgroundResourceForGroupPosition(id.equals(mFolderSection.get(0
)), |
| 353 id.equals(mFolderSection.get(mFolderSection.size() - 1))); |
| 354 } |
| 308 } | 355 } |
| OLD | NEW |