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.enhancedbookmarks; | 5 package org.chromium.chrome.browser.enhancedbookmarks; |
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.annotations.SuppressFBWarnings; | 14 import org.chromium.base.annotations.SuppressFBWarnings; |
15 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
16 import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem; | 16 import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem; |
17 import org.chromium.chrome.browser.BookmarksBridge.BookmarkModelObserver; | 17 import org.chromium.chrome.browser.BookmarksBridge.BookmarkModelObserver; |
| 18 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkManager.UIS
tate; |
18 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkPromoHeader
.PromoHeaderShowingChangeListener; | 19 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkPromoHeader
.PromoHeaderShowingChangeListener; |
| 20 import org.chromium.chrome.browser.offline_pages.OfflinePageBridge; |
| 21 import org.chromium.chrome.browser.offline_pages.OfflinePageFreeUpSpaceCallback; |
| 22 import org.chromium.chrome.browser.offline_pages.OfflinePageStorageSpaceHeader; |
19 import org.chromium.components.bookmarks.BookmarkId; | 23 import org.chromium.components.bookmarks.BookmarkId; |
20 | 24 |
21 import java.util.ArrayList; | 25 import java.util.ArrayList; |
22 import java.util.List; | 26 import java.util.List; |
23 | 27 |
24 /** | 28 /** |
25 * BaseAdapter for EnhancedBookmarkItemsContainer. It manages bookmarks to list
there. | 29 * BaseAdapter for EnhancedBookmarkItemsContainer. It manages bookmarks to list
there. |
26 */ | 30 */ |
27 class EnhancedBookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.Vie
wHolder> implements | 31 class EnhancedBookmarkItemsAdapter extends RecyclerView.Adapter<RecyclerView.Vie
wHolder> implements |
28 EnhancedBookmarkUIObserver, PromoHeaderShowingChangeListener { | 32 EnhancedBookmarkUIObserver, PromoHeaderShowingChangeListener { |
29 | 33 |
30 private static final int PROMO_HEADER_VIEW = 0; | 34 private static final int PROMO_HEADER_VIEW = 0; |
31 private static final int FOLDER_VIEW = 1; | 35 private static final int FOLDER_VIEW = 1; |
32 private static final int DIVIDER_VIEW = 2; | 36 private static final int DIVIDER_VIEW = 2; |
33 private static final int BOOKMARK_VIEW = 3; | 37 private static final int BOOKMARK_VIEW = 3; |
| 38 private static final int OFFLINE_PAGES_STORAGE_VIEW = 4; |
34 | 39 |
35 private EnhancedBookmarkDelegate mDelegate; | 40 private EnhancedBookmarkDelegate mDelegate; |
36 private Context mContext; | 41 private Context mContext; |
37 private EnhancedBookmarkPromoHeader mPromoHeaderManager; | 42 private EnhancedBookmarkPromoHeader mPromoHeaderManager; |
| 43 private OfflinePageStorageSpaceHeader mOfflineStorageHeader; |
38 | 44 |
39 private List<List<? extends Object>> mSections; | 45 private List<List<? extends Object>> mSections; |
40 private List<Object> mPromoHeaderSection = new ArrayList<>(); | 46 private List<Object> mPromoHeaderSection = new ArrayList<>(); |
| 47 private List<Object> mOfflineStorageSection = new ArrayList<>(); |
41 private List<Object> mFolderDividerSection = new ArrayList<>(); | 48 private List<Object> mFolderDividerSection = new ArrayList<>(); |
42 private List<BookmarkId> mFolderSection = new ArrayList<>(); | 49 private List<BookmarkId> mFolderSection = new ArrayList<>(); |
43 private List<Object> mBookmarkDividerSection = new ArrayList<>(); | 50 private List<Object> mBookmarkDividerSection = new ArrayList<>(); |
44 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); | 51 private List<BookmarkId> mBookmarkSection = new ArrayList<>(); |
45 | 52 |
46 private BookmarkModelObserver mBookmarkModelObserver = new BookmarkModelObse
rver() { | 53 private BookmarkModelObserver mBookmarkModelObserver = new BookmarkModelObse
rver() { |
47 @Override | 54 @Override |
48 public void bookmarkNodeChanged(BookmarkItem node) { | 55 public void bookmarkNodeChanged(BookmarkItem node) { |
49 int position = getPositionForBookmark(node.getId()); | 56 int position = getPositionForBookmark(node.getId()); |
50 if (position >= 0) notifyItemChanged(position); | 57 if (position >= 0) notifyItemChanged(position); |
(...skipping 16 matching lines...) Expand all Loading... |
67 public void bookmarkModelChanged() { | 74 public void bookmarkModelChanged() { |
68 mDelegate.notifyStateChange(EnhancedBookmarkItemsAdapter.this); | 75 mDelegate.notifyStateChange(EnhancedBookmarkItemsAdapter.this); |
69 } | 76 } |
70 }; | 77 }; |
71 | 78 |
72 EnhancedBookmarkItemsAdapter(Context context) { | 79 EnhancedBookmarkItemsAdapter(Context context) { |
73 mContext = context; | 80 mContext = context; |
74 | 81 |
75 mSections = new ArrayList<>(); | 82 mSections = new ArrayList<>(); |
76 mSections.add(mPromoHeaderSection); | 83 mSections.add(mPromoHeaderSection); |
| 84 mSections.add(mOfflineStorageSection); |
77 mSections.add(mFolderDividerSection); | 85 mSections.add(mFolderDividerSection); |
78 mSections.add(mFolderSection); | 86 mSections.add(mFolderSection); |
79 mSections.add(mBookmarkDividerSection); | 87 mSections.add(mBookmarkDividerSection); |
80 mSections.add(mBookmarkSection); | 88 mSections.add(mBookmarkSection); |
81 } | 89 } |
82 | 90 |
83 BookmarkId getItem(int position) { | 91 BookmarkId getItem(int position) { |
84 return (BookmarkId) getSection(position).get(toSectionPosition(position)
); | 92 return (BookmarkId) getSection(position).get(toSectionPosition(position)
); |
85 } | 93 } |
86 | 94 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 * @param folders This can be null if there is no folders to show. | 132 * @param folders This can be null if there is no folders to show. |
125 */ | 133 */ |
126 private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmar
ks) { | 134 private void setBookmarks(List<BookmarkId> folders, List<BookmarkId> bookmar
ks) { |
127 if (folders == null) folders = new ArrayList<BookmarkId>(); | 135 if (folders == null) folders = new ArrayList<BookmarkId>(); |
128 | 136 |
129 mFolderSection.clear(); | 137 mFolderSection.clear(); |
130 mFolderSection.addAll(folders); | 138 mFolderSection.addAll(folders); |
131 mBookmarkSection.clear(); | 139 mBookmarkSection.clear(); |
132 mBookmarkSection.addAll(bookmarks); | 140 mBookmarkSection.addAll(bookmarks); |
133 | 141 |
| 142 updateHeader(); |
134 updateDividerSections(); | 143 updateDividerSections(); |
135 | 144 |
136 // TODO(kkimlabs): Animation is disabled due to a performance issue on b
ookmark undo. | 145 // TODO(kkimlabs): Animation is disabled due to a performance issue on b
ookmark undo. |
137 // http://crbug.com/484174 | 146 // http://crbug.com/484174 |
138 notifyDataSetChanged(); | 147 notifyDataSetChanged(); |
139 } | 148 } |
140 | 149 |
141 private void updateDividerSections() { | 150 private void updateDividerSections() { |
142 mFolderDividerSection.clear(); | 151 mFolderDividerSection.clear(); |
143 mBookmarkDividerSection.clear(); | 152 mBookmarkDividerSection.clear(); |
144 if (!mPromoHeaderSection.isEmpty() && !mFolderSection.isEmpty()) { | 153 |
| 154 boolean isHeaderPresent = |
| 155 !mPromoHeaderSection.isEmpty() || !mOfflineStorageSection.isEmpt
y(); |
| 156 |
| 157 if (isHeaderPresent && !mFolderSection.isEmpty()) { |
145 mFolderDividerSection.add(null); | 158 mFolderDividerSection.add(null); |
146 } | 159 } |
147 if ((!mPromoHeaderSection.isEmpty() || !mFolderSection.isEmpty()) | 160 if ((isHeaderPresent || !mFolderSection.isEmpty()) && !mBookmarkSection.
isEmpty()) { |
148 && !mBookmarkSection.isEmpty()) { | |
149 mBookmarkDividerSection.add(null); | 161 mBookmarkDividerSection.add(null); |
150 } | 162 } |
151 } | 163 } |
152 | 164 |
153 private void removeItem(int position) { | 165 private void removeItem(int position) { |
154 List<?> section = getSection(position); | 166 List<?> section = getSection(position); |
155 assert section == mFolderSection || section == mBookmarkSection; | 167 assert section == mFolderSection || section == mBookmarkSection; |
156 section.remove(toSectionPosition(position)); | 168 section.remove(toSectionPosition(position)); |
157 notifyItemRemoved(position); | 169 notifyItemRemoved(position); |
158 } | 170 } |
159 | 171 |
160 // RecyclerView.Adapter implementation. | 172 // RecyclerView.Adapter implementation. |
161 | 173 |
162 @Override | 174 @Override |
163 public int getItemCount() { | 175 public int getItemCount() { |
164 int count = 0; | 176 int count = 0; |
165 for (List<?> section : mSections) { | 177 for (List<?> section : mSections) { |
166 count += section.size(); | 178 count += section.size(); |
167 } | 179 } |
168 return count; | 180 return count; |
169 } | 181 } |
170 | 182 |
171 @Override | 183 @Override |
172 public int getItemViewType(int position) { | 184 public int getItemViewType(int position) { |
173 List<?> section = getSection(position); | 185 List<?> section = getSection(position); |
174 | 186 |
175 if (section == mPromoHeaderSection) { | 187 if (section == mPromoHeaderSection) { |
176 return PROMO_HEADER_VIEW; | 188 return PROMO_HEADER_VIEW; |
| 189 } else if (section == mOfflineStorageSection) { |
| 190 return OFFLINE_PAGES_STORAGE_VIEW; |
177 } else if (section == mFolderDividerSection | 191 } else if (section == mFolderDividerSection |
178 || section == mBookmarkDividerSection) { | 192 || section == mBookmarkDividerSection) { |
179 return DIVIDER_VIEW; | 193 return DIVIDER_VIEW; |
180 } else if (section == mFolderSection) { | 194 } else if (section == mFolderSection) { |
181 return FOLDER_VIEW; | 195 return FOLDER_VIEW; |
182 } else if (section == mBookmarkSection) { | 196 } else if (section == mBookmarkSection) { |
183 return BOOKMARK_VIEW; | 197 return BOOKMARK_VIEW; |
184 } | 198 } |
185 | 199 |
186 assert false : "Invalid position requested"; | 200 assert false : "Invalid position requested"; |
187 return -1; | 201 return -1; |
188 } | 202 } |
189 | 203 |
190 @Override | 204 @Override |
191 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | 205 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
192 switch (viewType) { | 206 switch (viewType) { |
193 case PROMO_HEADER_VIEW: | 207 case PROMO_HEADER_VIEW: |
194 return mPromoHeaderManager.createHolder(parent); | 208 return mPromoHeaderManager.createHolder(parent); |
| 209 case OFFLINE_PAGES_STORAGE_VIEW: |
| 210 return mOfflineStorageHeader.createHolder(parent); |
195 case DIVIDER_VIEW: | 211 case DIVIDER_VIEW: |
196 return new ViewHolder(LayoutInflater.from(parent.getContext()).i
nflate( | 212 return new ViewHolder(LayoutInflater.from(parent.getContext()).i
nflate( |
197 R.layout.eb_divider, parent, false)) {}; | 213 R.layout.eb_divider, parent, false)) {}; |
198 case FOLDER_VIEW: | 214 case FOLDER_VIEW: |
199 EnhancedBookmarkFolderRow folder = (EnhancedBookmarkFolderRow) L
ayoutInflater | 215 EnhancedBookmarkFolderRow folder = (EnhancedBookmarkFolderRow) L
ayoutInflater |
200 .from(parent.getContext()).inflate(R.layout.eb_folder_ro
w, parent, false); | 216 .from(parent.getContext()).inflate(R.layout.eb_folder_ro
w, parent, false); |
201 folder.onEnhancedBookmarkDelegateInitialized(mDelegate); | 217 folder.onEnhancedBookmarkDelegateInitialized(mDelegate); |
202 return new ItemViewHolder(folder); | 218 return new ItemViewHolder(folder); |
203 case BOOKMARK_VIEW: | 219 case BOOKMARK_VIEW: |
204 EnhancedBookmarkBookmarkRow item = (EnhancedBookmarkBookmarkRow)
LayoutInflater | 220 EnhancedBookmarkBookmarkRow item = (EnhancedBookmarkBookmarkRow)
LayoutInflater |
205 .from(parent.getContext()).inflate(R.layout.eb_bookmark_
row, parent, false); | 221 .from(parent.getContext()).inflate(R.layout.eb_bookmark_
row, parent, false); |
206 item.onEnhancedBookmarkDelegateInitialized(mDelegate); | 222 item.onEnhancedBookmarkDelegateInitialized(mDelegate); |
207 return new ItemViewHolder(item); | 223 return new ItemViewHolder(item); |
208 default: | 224 default: |
209 assert false; | 225 assert false; |
210 return null; | 226 return null; |
211 } | 227 } |
212 } | 228 } |
213 | 229 |
214 @SuppressFBWarnings("BC_UNCONFIRMED_CAST") | 230 @SuppressFBWarnings("BC_UNCONFIRMED_CAST") |
215 @Override | 231 @Override |
216 public void onBindViewHolder(ViewHolder holder, int position) { | 232 public void onBindViewHolder(ViewHolder holder, int position) { |
217 BookmarkId id = getItem(position); | 233 BookmarkId id = getItem(position); |
218 switch (getItemViewType(position)) { | 234 switch (getItemViewType(position)) { |
219 case PROMO_HEADER_VIEW: | 235 case PROMO_HEADER_VIEW: |
| 236 case OFFLINE_PAGES_STORAGE_VIEW: |
220 case DIVIDER_VIEW: | 237 case DIVIDER_VIEW: |
221 break; | 238 break; |
222 case FOLDER_VIEW: | 239 case FOLDER_VIEW: |
223 ((EnhancedBookmarkRow) holder.itemView).setBookmarkId(id); | 240 ((EnhancedBookmarkRow) holder.itemView).setBookmarkId(id); |
224 break; | 241 break; |
225 case BOOKMARK_VIEW: | 242 case BOOKMARK_VIEW: |
226 ((EnhancedBookmarkRow) holder.itemView).setBookmarkId(id); | 243 ((EnhancedBookmarkRow) holder.itemView).setBookmarkId(id); |
227 break; | 244 break; |
228 default: | 245 default: |
229 assert false : "View type not supported!"; | 246 assert false : "View type not supported!"; |
230 } | 247 } |
231 } | 248 } |
232 | 249 |
233 // PromoHeaderShowingChangeListener implementation. | 250 // PromoHeaderShowingChangeListener implementation. |
234 | 251 |
235 @Override | 252 @Override |
236 public void onPromoHeaderShowingChanged(boolean isShowing) { | 253 public void onPromoHeaderShowingChanged(boolean isShowing) { |
237 mPromoHeaderSection.clear(); | 254 if (mDelegate.getCurrentState() != UIState.STATE_ALL_BOOKMARKS |
238 if (isShowing) mPromoHeaderSection.add(null); | 255 && mDelegate.getCurrentState() != UIState.STATE_FOLDER) { |
| 256 return; |
| 257 } |
239 | 258 |
| 259 updateHeader(); |
240 updateDividerSections(); | 260 updateDividerSections(); |
241 notifyDataSetChanged(); | 261 notifyDataSetChanged(); |
242 } | 262 } |
243 | 263 |
244 // EnhancedBookmarkUIObserver implementations. | 264 // EnhancedBookmarkUIObserver implementations. |
245 | 265 |
246 @Override | 266 @Override |
247 public void onEnhancedBookmarkDelegateInitialized(EnhancedBookmarkDelegate d
elegate) { | 267 public void onEnhancedBookmarkDelegateInitialized(EnhancedBookmarkDelegate d
elegate) { |
248 mDelegate = delegate; | 268 mDelegate = delegate; |
249 mDelegate.addUIObserver(this); | 269 mDelegate.addUIObserver(this); |
250 mDelegate.getModel().addObserver(mBookmarkModelObserver); | 270 mDelegate.getModel().addObserver(mBookmarkModelObserver); |
251 mPromoHeaderManager = new EnhancedBookmarkPromoHeader(mContext, this); | 271 mPromoHeaderManager = new EnhancedBookmarkPromoHeader(mContext, this); |
252 if (mPromoHeaderManager.shouldShow()) mPromoHeaderSection.add(null); | 272 OfflinePageBridge offlinePageBridge = mDelegate.getModel().getOfflinePag
eBridge(); |
| 273 if (offlinePageBridge != null) { |
| 274 mOfflineStorageHeader = new OfflinePageStorageSpaceHeader( |
| 275 mContext, offlinePageBridge, new OfflinePageFreeUpSpaceCallb
ack() { |
| 276 @Override |
| 277 public void onFreeUpSpaceDone() { |
| 278 refreshOfflinePagesFilterView(); |
| 279 } |
253 | 280 |
254 updateDividerSections(); | 281 @Override |
| 282 public void onFreeUpSpaceCancelled() { |
| 283 // No need to refresh, as result outcome should be t
he same here. |
| 284 } |
| 285 }); |
| 286 } |
255 } | 287 } |
256 | 288 |
257 @Override | 289 @Override |
258 public void onDestroy() { | 290 public void onDestroy() { |
259 mDelegate.removeUIObserver(this); | 291 mDelegate.removeUIObserver(this); |
260 mDelegate.getModel().removeObserver(mBookmarkModelObserver); | 292 mDelegate.getModel().removeObserver(mBookmarkModelObserver); |
261 mPromoHeaderManager.destroy(); | 293 mPromoHeaderManager.destroy(); |
262 } | 294 } |
263 | 295 |
264 @Override | 296 @Override |
(...skipping 14 matching lines...) Expand all Loading... |
279 } | 311 } |
280 | 312 |
281 @Override | 313 @Override |
282 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) {} | 314 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) {} |
283 | 315 |
284 private static class ItemViewHolder extends RecyclerView.ViewHolder { | 316 private static class ItemViewHolder extends RecyclerView.ViewHolder { |
285 private ItemViewHolder(View view) { | 317 private ItemViewHolder(View view) { |
286 super(view); | 318 super(view); |
287 } | 319 } |
288 } | 320 } |
| 321 |
| 322 private void updateHeader() { |
| 323 int currentUIState = mDelegate.getCurrentState(); |
| 324 if (currentUIState == UIState.STATE_LOADING) return; |
| 325 |
| 326 mPromoHeaderSection.clear(); |
| 327 mOfflineStorageSection.clear(); |
| 328 if (currentUIState == UIState.STATE_FILTER) { |
| 329 if (mOfflineStorageHeader != null && mOfflineStorageHeader.shouldSho
w()) { |
| 330 mOfflineStorageSection.add(null); |
| 331 } |
| 332 } else { |
| 333 assert currentUIState == UIState.STATE_ALL_BOOKMARKS |
| 334 || currentUIState == UIState.STATE_FOLDER : "Unexpected UI s
tate"; |
| 335 if (mPromoHeaderManager.shouldShow()) { |
| 336 mPromoHeaderSection.add(null); |
| 337 } |
| 338 } |
| 339 } |
| 340 |
| 341 private void refreshOfflinePagesFilterView() { |
| 342 if (mDelegate.getCurrentState() != UIState.STATE_FILTER) return; |
| 343 setBookmarks(null, |
| 344 mDelegate.getModel().getBookmarkIDsByFilter(EnhancedBookmarkFilt
er.OFFLINE_PAGES)); |
| 345 } |
289 } | 346 } |
OLD | NEW |