OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.download.ui; | 5 package org.chromium.chrome.browser.download.ui; |
6 | 6 |
7 import android.content.ComponentName; | 7 import android.content.ComponentName; |
8 import android.support.v7.widget.RecyclerView.ViewHolder; | 8 import android.support.v7.widget.RecyclerView.ViewHolder; |
9 import android.text.TextUtils; | 9 import android.text.TextUtils; |
10 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
(...skipping 10 matching lines...) Expand all Loading... | |
21 import org.chromium.chrome.browser.download.ui.DownloadHistoryItemWrapper.Downlo adItemWrapper; | 21 import org.chromium.chrome.browser.download.ui.DownloadHistoryItemWrapper.Downlo adItemWrapper; |
22 import org.chromium.chrome.browser.download.ui.DownloadHistoryItemWrapper.Offlin ePageItemWrapper; | 22 import org.chromium.chrome.browser.download.ui.DownloadHistoryItemWrapper.Offlin ePageItemWrapper; |
23 import org.chromium.chrome.browser.download.ui.DownloadManagerUi.DownloadUiObser ver; | 23 import org.chromium.chrome.browser.download.ui.DownloadManagerUi.DownloadUiObser ver; |
24 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBri dge; | 24 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadBri dge; |
25 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadIte m; | 25 import org.chromium.chrome.browser.offlinepages.downloads.OfflinePageDownloadIte m; |
26 import org.chromium.chrome.browser.widget.DateDividedAdapter; | 26 import org.chromium.chrome.browser.widget.DateDividedAdapter; |
27 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 27 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
28 import org.chromium.content_public.browser.DownloadState; | 28 import org.chromium.content_public.browser.DownloadState; |
29 | 29 |
30 import java.util.ArrayList; | 30 import java.util.ArrayList; |
31 import java.util.Calendar; | |
32 import java.util.Date; | |
33 import java.util.HashMap; | |
31 import java.util.List; | 34 import java.util.List; |
35 import java.util.Map; | |
32 import java.util.Set; | 36 import java.util.Set; |
33 | 37 |
34 /** Bridges the user's download history and the UI used to display it. */ | 38 /** Bridges the user's download history and the UI used to display it. */ |
35 public class DownloadHistoryAdapter extends DateDividedAdapter | 39 public class DownloadHistoryAdapter extends DateDividedAdapter |
36 implements DownloadUiObserver, DownloadSharedPreferenceHelper.Observer { | 40 implements DownloadUiObserver, DownloadSharedPreferenceHelper.Observer { |
37 | 41 |
38 /** Alerted about changes to internal state. */ | 42 /** Alerted about changes to internal state. */ |
39 static interface TestObserver { | 43 static interface TestObserver { |
40 abstract void onDownloadItemCreated(DownloadItem item); | 44 abstract void onDownloadItemCreated(DownloadItem item); |
41 abstract void onDownloadItemUpdated(DownloadItem item); | 45 abstract void onDownloadItemUpdated(DownloadItem item); |
42 } | 46 } |
43 | 47 |
44 private class BackendItemsImpl extends BackendItems { | 48 private class BackendItemsImpl extends BackendItems { |
45 @Override | 49 @Override |
46 public DownloadHistoryItemWrapper removeItem(String guid) { | 50 public DownloadHistoryItemWrapper removeItem(String guid) { |
47 DownloadHistoryItemWrapper wrapper = super.removeItem(guid); | 51 DownloadHistoryItemWrapper wrapper = super.removeItem(guid); |
48 | 52 |
49 if (wrapper != null) { | 53 if (wrapper != null) { |
50 mFilePathsToItemsMap.removeItem(wrapper); | 54 mFilePathsToItemsMap.removeItem(wrapper); |
51 if (getSelectionDelegate().isItemSelected(wrapper)) { | 55 if (getSelectionDelegate().isItemSelected(wrapper)) { |
52 getSelectionDelegate().toggleSelectionForItem(wrapper); | 56 getSelectionDelegate().toggleSelectionForItem(wrapper); |
53 } | 57 } |
54 } | 58 } |
55 | 59 |
56 return wrapper; | 60 return wrapper; |
57 } | 61 } |
58 } | 62 } |
59 | 63 |
64 /** Represents the subsection header of the suggested pages for a given date . */ | |
65 public class SubsectionHeader extends TimedItem { | |
66 private final Date mDate; | |
67 private final int mItemCount; | |
68 private final long mTotalFileSize; | |
69 private final Long mStableId; | |
70 | |
71 public SubsectionHeader(Date date, int itemCount, long totalFileSize) { | |
72 mDate = date; | |
73 mItemCount = itemCount; | |
74 mTotalFileSize = totalFileSize; | |
75 | |
76 // Generate a stable ID based on timestamp. | |
77 mStableId = 0xFFFFFFFF00000000L + (getTimestamp() & 0x0FFFFFFFF); | |
78 } | |
79 | |
80 @Override | |
81 public long getTimestamp() { | |
82 return mDate.getTime(); | |
83 } | |
84 | |
85 public int getItemCount() { | |
86 return mItemCount; | |
87 } | |
88 | |
89 public long getTotalFileSize() { | |
90 return mTotalFileSize; | |
91 } | |
92 | |
93 @Override | |
94 public long getStableId() { | |
95 return mStableId; | |
96 } | |
97 } | |
98 | |
60 /** | 99 /** |
61 * Tracks externally deleted items that have been removed from downloads his tory. | 100 * Tracks externally deleted items that have been removed from downloads his tory. |
62 * Shared across instances. | 101 * Shared across instances. |
63 */ | 102 */ |
64 private static final DeletedFileTracker sDeletedFileTracker = new DeletedFil eTracker(); | 103 private static final DeletedFileTracker sDeletedFileTracker = new DeletedFil eTracker(); |
65 | 104 |
66 private static final String EMPTY_QUERY = null; | 105 private static final String EMPTY_QUERY = null; |
67 | 106 |
68 private final BackendItems mRegularDownloadItems = new BackendItemsImpl(); | 107 private final BackendItems mRegularDownloadItems = new BackendItemsImpl(); |
69 private final BackendItems mIncognitoDownloadItems = new BackendItemsImpl(); | 108 private final BackendItems mIncognitoDownloadItems = new BackendItemsImpl(); |
70 private final BackendItems mOfflinePageItems = new BackendItemsImpl(); | 109 private final BackendItems mOfflinePageItems = new BackendItemsImpl(); |
71 | 110 |
72 private final BackendItems mFilteredItems = new BackendItemsImpl(); | |
73 private final FilePathsToDownloadItemsMap mFilePathsToItemsMap = | 111 private final FilePathsToDownloadItemsMap mFilePathsToItemsMap = |
74 new FilePathsToDownloadItemsMap(); | 112 new FilePathsToDownloadItemsMap(); |
75 | 113 |
114 private final Map<Date, Boolean> mSubSectionExpanded = new HashMap<>(); | |
76 private final ComponentName mParentComponent; | 115 private final ComponentName mParentComponent; |
77 private final boolean mShowOffTheRecord; | 116 private final boolean mShowOffTheRecord; |
78 private final LoadingStateDelegate mLoadingDelegate; | 117 private final LoadingStateDelegate mLoadingDelegate; |
79 private final ObserverList<TestObserver> mObservers = new ObserverList<>(); | 118 private final ObserverList<TestObserver> mObservers = new ObserverList<>(); |
80 private final List<DownloadItemView> mViews = new ArrayList<>(); | 119 private final List<DownloadItemView> mViews = new ArrayList<>(); |
81 | 120 |
82 private BackendProvider mBackendProvider; | 121 private BackendProvider mBackendProvider; |
83 private OfflinePageDownloadBridge.Observer mOfflinePageObserver; | 122 private OfflinePageDownloadBridge.Observer mOfflinePageObserver; |
84 private int mFilter = DownloadFilter.FILTER_ALL; | 123 private int mFilter = DownloadFilter.FILTER_ALL; |
85 private String mSearchQuery = EMPTY_QUERY; | 124 private String mSearchQuery = EMPTY_QUERY; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 totalSize += mOfflinePageItems.getTotalBytes(); | 240 totalSize += mOfflinePageItems.getTotalBytes(); |
202 return totalSize; | 241 return totalSize; |
203 } | 242 } |
204 | 243 |
205 @Override | 244 @Override |
206 protected int getTimedItemViewResId() { | 245 protected int getTimedItemViewResId() { |
207 return R.layout.date_view; | 246 return R.layout.date_view; |
208 } | 247 } |
209 | 248 |
210 @Override | 249 @Override |
250 protected SubsectionHeaderViewHolder createSubsectionHeader(ViewGroup parent ) { | |
251 OfflineGroupHeaderView offlineHeader = | |
252 (OfflineGroupHeaderView) LayoutInflater.from(parent.getContext() ) | |
253 .inflate(R.layout.offline_download_header, parent, false ); | |
254 offlineHeader.setAdapter(this); | |
255 return new SubsectionHeaderViewHolder(offlineHeader); | |
256 } | |
257 | |
258 @Override | |
259 protected void bindViewHolderForSubsectionHeader( | |
260 SubsectionHeaderViewHolder holder, TimedItem timedItem) { | |
261 SubsectionHeader headerItem = (SubsectionHeader) timedItem; | |
262 Date date = new Date(headerItem.getTimestamp()); | |
263 OfflineGroupHeaderView headerView = (OfflineGroupHeaderView) holder.getV iew(); | |
264 headerView.update(date, isSubsectionExpanded(date), headerItem.getItemCo unt(), | |
265 headerItem.getTotalFileSize()); | |
266 } | |
267 | |
268 @Override | |
211 public ViewHolder createViewHolder(ViewGroup parent) { | 269 public ViewHolder createViewHolder(ViewGroup parent) { |
212 DownloadItemView v = (DownloadItemView) LayoutInflater.from(parent.getCo ntext()).inflate( | 270 DownloadItemView v = (DownloadItemView) LayoutInflater.from(parent.getCo ntext()).inflate( |
213 R.layout.download_item_view, parent, false); | 271 R.layout.download_item_view, parent, false); |
214 v.setSelectionDelegate(getSelectionDelegate()); | 272 v.setSelectionDelegate(getSelectionDelegate()); |
215 mViews.add(v); | 273 mViews.add(v); |
216 return new DownloadHistoryItemViewHolder(v); | 274 return new DownloadHistoryItemViewHolder(v); |
217 } | 275 } |
218 | 276 |
219 @Override | 277 @Override |
220 public void bindViewHolderForTimedItem(ViewHolder current, TimedItem timedIt em) { | 278 public void bindViewHolderForTimedItem(ViewHolder current, TimedItem timedIt em) { |
221 final DownloadHistoryItemWrapper item = (DownloadHistoryItemWrapper) tim edItem; | 279 final DownloadHistoryItemWrapper item = (DownloadHistoryItemWrapper) tim edItem; |
222 | 280 |
223 DownloadHistoryItemViewHolder holder = (DownloadHistoryItemViewHolder) c urrent; | 281 DownloadHistoryItemViewHolder holder = (DownloadHistoryItemViewHolder) c urrent; |
224 holder.getItemView().displayItem(mBackendProvider, item); | 282 holder.getItemView().displayItem(mBackendProvider, item); |
225 } | 283 } |
226 | 284 |
285 @Override | |
286 protected ItemGroup createGroup(long timeStamp) { | |
287 return new DownloadItemGroup(timeStamp); | |
288 } | |
289 | |
227 /** Called when a new DownloadItem has been created by the native DownloadMa nager. */ | 290 /** Called when a new DownloadItem has been created by the native DownloadMa nager. */ |
228 public void onDownloadItemCreated(DownloadItem item) { | 291 public void onDownloadItemCreated(DownloadItem item) { |
229 boolean isOffTheRecord = item.getDownloadInfo().isOffTheRecord(); | 292 boolean isOffTheRecord = item.getDownloadInfo().isOffTheRecord(); |
230 if (isOffTheRecord && !mShowOffTheRecord) return; | 293 if (isOffTheRecord && !mShowOffTheRecord) return; |
231 | 294 |
232 BackendItems list = getDownloadItemList(isOffTheRecord); | 295 BackendItems list = getDownloadItemList(isOffTheRecord); |
233 assert list.findItemIndex(item.getId()) == BackendItems.INVALID_INDEX; | 296 assert list.findItemIndex(item.getId()) == BackendItems.INVALID_INDEX; |
234 | 297 |
235 DownloadItemWrapper wrapper = createDownloadItemWrapper(item); | 298 DownloadItemWrapper wrapper = createDownloadItemWrapper(item); |
236 boolean wasAdded = addDownloadHistoryItemWrapper(wrapper); | 299 boolean wasAdded = addDownloadHistoryItemWrapper(wrapper); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 return mBackendProvider.getOfflinePageBridge(); | 448 return mBackendProvider.getOfflinePageBridge(); |
386 } | 449 } |
387 | 450 |
388 private SelectionDelegate<DownloadHistoryItemWrapper> getSelectionDelegate() { | 451 private SelectionDelegate<DownloadHistoryItemWrapper> getSelectionDelegate() { |
389 return mBackendProvider.getSelectionDelegate(); | 452 return mBackendProvider.getSelectionDelegate(); |
390 } | 453 } |
391 | 454 |
392 /** Filters the list of downloads to show only files of a specific type. */ | 455 /** Filters the list of downloads to show only files of a specific type. */ |
393 private void filter(int filterType) { | 456 private void filter(int filterType) { |
394 mFilter = filterType; | 457 mFilter = filterType; |
395 mFilteredItems.clear(); | 458 |
396 mRegularDownloadItems.filter(mFilter, mSearchQuery, mFilteredItems); | 459 BackendItems filteredDownloadItems = new BackendItemsImpl(); |
397 mIncognitoDownloadItems.filter(mFilter, mSearchQuery, mFilteredItems); | 460 mRegularDownloadItems.filter(mFilter, mSearchQuery, filteredDownloadItem s); |
398 mOfflinePageItems.filter(mFilter, mSearchQuery, mFilteredItems); | 461 mIncognitoDownloadItems.filter(mFilter, mSearchQuery, filteredDownloadIt ems); |
462 | |
463 List<TimedItem> filteredTimedItems = new ArrayList<>(); | |
464 filteredTimedItems.addAll(filteredDownloadItems); | |
465 filteredTimedItems.addAll(filterOfflinePageItems()); | |
466 | |
399 clear(false); | 467 clear(false); |
400 loadItems(mFilteredItems); | 468 loadItems(filteredTimedItems); |
469 } | |
470 | |
471 /** | |
472 * Filters the offline pages based on the current filter and search text. | |
473 * If there are suggested pages, they are filtered based on whether or not t he subsection for | |
474 * that date is expanded. Also a TimedItem is added to each subsection to re present the header | |
475 * for the suggested pages. | |
476 */ | |
477 private List<TimedItem> filterOfflinePageItems() { | |
478 List<TimedItem> filteredTimedItems = new ArrayList<>(); | |
479 Map<Date, Integer> suggestedPageCountMap = new HashMap<>(); | |
480 Map<Date, Long> suggestedPageTotalSizeMap = new HashMap<>(); | |
481 | |
482 BackendItems filteredOfflinePageItems = new BackendItemsImpl(); | |
483 mOfflinePageItems.filter(mFilter, mSearchQuery, filteredOfflinePageItems ); | |
484 | |
485 for (DownloadHistoryItemWrapper item : filteredOfflinePageItems) { | |
486 OfflinePageItemWrapper offlineItem = (OfflinePageItemWrapper) item; | |
487 | |
488 // Add the suggested pages to the adapter only if the section is exp anded for that date. | |
489 if (offlineItem.isSuggested()) { | |
490 incrementSuggestedPageCount( | |
491 offlineItem, suggestedPageCountMap, suggestedPageTotalSi zeMap); | |
492 if (!isSubsectionExpanded(getDateWithoutTime(offlineItem.getTime stamp()))) continue; | |
493 } | |
494 filteredTimedItems.add(offlineItem); | |
495 } | |
496 | |
497 // Add a TimedItem for each subsection. | |
498 for (Date date : suggestedPageCountMap.keySet()) { | |
499 filteredTimedItems.add(new SubsectionHeader( | |
Theresa
2017/02/16 17:48:17
Instead of constructing the SubsectionHeader each
shaktisahu
2017/02/16 21:15:11
I think this is okay, since they aren't that many
Theresa
2017/02/17 23:56:34
The backend is going to automatically remove sugge
shaktisahu
2017/02/18 00:39:46
I would think so. I will check this with the UX if
| |
500 date, suggestedPageCountMap.get(date), suggestedPageTotalSiz eMap.get(date))); | |
501 } | |
502 | |
503 return filteredTimedItems; | |
504 } | |
505 | |
506 // Updates the total number of suggested pages and file size grouped by date . | |
507 private void incrementSuggestedPageCount(OfflinePageItemWrapper offlineItem, | |
508 Map<Date, Integer> pageCountMap, Map<Date, Long> fileSizeMap) { | |
509 Date date = getDateWithoutTime(offlineItem.getTimestamp()); | |
510 | |
511 int count = pageCountMap.containsKey(date) ? pageCountMap.get(date) : 0; | |
512 pageCountMap.put(date, count + 1); | |
513 | |
514 long fileSize = fileSizeMap.containsKey(date) ? fileSizeMap.get(date) : 0; | |
515 fileSizeMap.put(date, fileSize + offlineItem.getFileSize()); | |
516 } | |
517 | |
518 /** | |
519 * Whether the suggested pages section is expanded for a given date. | |
520 * @param date The download date. | |
521 * @return Whether the suggested pages section is expanded. | |
522 */ | |
523 public boolean isSubsectionExpanded(Date date) { | |
524 // Default state is collpased. | |
525 if (mSubSectionExpanded.get(date) == null) return false; | |
526 | |
527 return mSubSectionExpanded.get(date); | |
528 } | |
529 | |
530 /** | |
531 * Sets the state of a subsection for a particular date. | |
532 * @param date The download date. | |
533 * @param expanded Whether the suggested pages should be expanded. | |
534 */ | |
535 public void setSubsectionExpanded(Date date, boolean expanded) { | |
536 mSubSectionExpanded.put(date, expanded); | |
537 clear(false); | |
538 filter(mFilter); | |
539 } | |
540 | |
541 @Override | |
542 protected boolean isSubsectionHeader(TimedItem timedItem) { | |
543 return timedItem instanceof SubsectionHeader; | |
401 } | 544 } |
402 | 545 |
403 private void initializeOfflinePageBridge() { | 546 private void initializeOfflinePageBridge() { |
404 mOfflinePageObserver = new OfflinePageDownloadBridge.Observer() { | 547 mOfflinePageObserver = new OfflinePageDownloadBridge.Observer() { |
405 @Override | 548 @Override |
406 public void onItemsLoaded() { | 549 public void onItemsLoaded() { |
407 onAllOfflinePagesRetrieved(getOfflinePageBridge().getAllItems()) ; | 550 onAllOfflinePagesRetrieved(getOfflinePageBridge().getAllItems()) ; |
408 } | 551 } |
409 | 552 |
410 @Override | 553 @Override |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 RecordHistogram.recordCountHistogram("Android.DownloadManager.InitialCou nt.Video", | 613 RecordHistogram.recordCountHistogram("Android.DownloadManager.InitialCou nt.Video", |
471 itemCounts[DownloadFilter.FILTER_VIDEO]); | 614 itemCounts[DownloadFilter.FILTER_VIDEO]); |
472 } | 615 } |
473 | 616 |
474 private void recordTotalDownloadCountHistogram() { | 617 private void recordTotalDownloadCountHistogram() { |
475 // The total count intentionally leaves out incognito downloads. This sh ould be revisited | 618 // The total count intentionally leaves out incognito downloads. This sh ould be revisited |
476 // if/when incognito downloads are persistently available in downloads h ome. | 619 // if/when incognito downloads are persistently available in downloads h ome. |
477 RecordHistogram.recordCountHistogram("Android.DownloadManager.InitialCou nt.Total", | 620 RecordHistogram.recordCountHistogram("Android.DownloadManager.InitialCou nt.Total", |
478 mRegularDownloadItems.size() + mOfflinePageItems.size()); | 621 mRegularDownloadItems.size() + mOfflinePageItems.size()); |
479 } | 622 } |
623 | |
624 /** | |
625 * Calculates the {@link Date} for midnight of the date represented by the t imestamp. | |
626 */ | |
627 private Date getDateWithoutTime(long timestamp) { | |
628 Calendar cal = Calendar.getInstance(); | |
629 cal.setTimeInMillis(timestamp); | |
630 cal.set(Calendar.HOUR_OF_DAY, 0); | |
631 cal.set(Calendar.MINUTE, 0); | |
632 cal.set(Calendar.SECOND, 0); | |
633 cal.set(Calendar.MILLISECOND, 0); | |
634 return cal.getTime(); | |
635 } | |
480 } | 636 } |
OLD | NEW |