Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapter.java |
| index d0229505dd0382bcff8dc3a7609d4723119487bd..311705e84d3b56cd93d8bb4d1a937b254d0658fe 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapter.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ui/DownloadHistoryAdapter.java |
| @@ -28,7 +28,11 @@ import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| import org.chromium.content_public.browser.DownloadState; |
| import java.util.ArrayList; |
| +import java.util.Calendar; |
| +import java.util.Date; |
| +import java.util.HashMap; |
| import java.util.List; |
| +import java.util.Map; |
| import java.util.Set; |
| /** Bridges the user's download history and the UI used to display it. */ |
| @@ -57,6 +61,41 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| } |
| } |
| + /** Represents the subsection header of the suggested pages for a given date. */ |
| + public class SubsectionHeader extends TimedItem { |
| + private final Date mDate; |
| + private final int mItemCount; |
| + private final long mTotalFileSize; |
| + private final Long mStableId; |
| + |
| + public SubsectionHeader(Date date, int itemCount, long totalFileSize) { |
| + mDate = date; |
| + mItemCount = itemCount; |
| + mTotalFileSize = totalFileSize; |
| + |
| + // Generate a stable ID based on timestamp. |
| + mStableId = 0xFFFFFFFF00000000L + (getTimestamp() & 0x0FFFFFFFF); |
| + } |
| + |
| + @Override |
| + public long getTimestamp() { |
| + return mDate.getTime(); |
| + } |
| + |
| + public int getItemCount() { |
| + return mItemCount; |
| + } |
| + |
| + public long getTotalFileSize() { |
| + return mTotalFileSize; |
| + } |
| + |
| + @Override |
| + public long getStableId() { |
| + return mStableId; |
| + } |
| + } |
| + |
| /** |
| * Tracks externally deleted items that have been removed from downloads history. |
| * Shared across instances. |
| @@ -69,10 +108,10 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| private final BackendItems mIncognitoDownloadItems = new BackendItemsImpl(); |
| private final BackendItems mOfflinePageItems = new BackendItemsImpl(); |
| - private final BackendItems mFilteredItems = new BackendItemsImpl(); |
| private final FilePathsToDownloadItemsMap mFilePathsToItemsMap = |
| new FilePathsToDownloadItemsMap(); |
| + private final Map<Date, Boolean> mSubSectionExpanded = new HashMap<>(); |
| private final ComponentName mParentComponent; |
| private final boolean mShowOffTheRecord; |
| private final LoadingStateDelegate mLoadingDelegate; |
| @@ -208,6 +247,25 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| } |
| @Override |
| + protected SubsectionHeaderViewHolder createSubsectionHeader(ViewGroup parent) { |
| + OfflineGroupHeaderView offlineHeader = |
| + (OfflineGroupHeaderView) LayoutInflater.from(parent.getContext()) |
| + .inflate(R.layout.offline_download_header, parent, false); |
| + offlineHeader.setAdapter(this); |
| + return new SubsectionHeaderViewHolder(offlineHeader); |
| + } |
| + |
| + @Override |
| + protected void bindViewHolderForSubsectionHeader( |
| + SubsectionHeaderViewHolder holder, TimedItem timedItem) { |
| + SubsectionHeader headerItem = (SubsectionHeader) timedItem; |
| + Date date = new Date(headerItem.getTimestamp()); |
| + OfflineGroupHeaderView headerView = (OfflineGroupHeaderView) holder.getView(); |
| + headerView.update(date, isSubsectionExpanded(date), headerItem.getItemCount(), |
| + headerItem.getTotalFileSize()); |
| + } |
| + |
| + @Override |
| public ViewHolder createViewHolder(ViewGroup parent) { |
| DownloadItemView v = (DownloadItemView) LayoutInflater.from(parent.getContext()).inflate( |
| R.layout.download_item_view, parent, false); |
| @@ -224,6 +282,11 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| holder.getItemView().displayItem(mBackendProvider, item); |
| } |
| + @Override |
| + protected ItemGroup createGroup(long timeStamp) { |
| + return new DownloadItemGroup(timeStamp); |
| + } |
| + |
| /** Called when a new DownloadItem has been created by the native DownloadManager. */ |
| public void onDownloadItemCreated(DownloadItem item) { |
| boolean isOffTheRecord = item.getDownloadInfo().isOffTheRecord(); |
| @@ -392,12 +455,92 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| /** Filters the list of downloads to show only files of a specific type. */ |
| private void filter(int filterType) { |
| mFilter = filterType; |
| - mFilteredItems.clear(); |
| - mRegularDownloadItems.filter(mFilter, mSearchQuery, mFilteredItems); |
| - mIncognitoDownloadItems.filter(mFilter, mSearchQuery, mFilteredItems); |
| - mOfflinePageItems.filter(mFilter, mSearchQuery, mFilteredItems); |
| + |
| + BackendItems filteredDownloadItems = new BackendItemsImpl(); |
| + mRegularDownloadItems.filter(mFilter, mSearchQuery, filteredDownloadItems); |
| + mIncognitoDownloadItems.filter(mFilter, mSearchQuery, filteredDownloadItems); |
| + |
| + List<TimedItem> filteredTimedItems = new ArrayList<>(); |
| + filteredTimedItems.addAll(filteredDownloadItems); |
| + filteredTimedItems.addAll(filterOfflinePageItems()); |
| + |
| + clear(false); |
| + loadItems(filteredTimedItems); |
| + } |
| + |
| + /** |
| + * Filters the offline pages based on the current filter and search text. |
| + * If there are suggested pages, they are filtered based on whether or not the subsection for |
| + * that date is expanded. Also a TimedItem is added to each subsection to represent the header |
| + * for the suggested pages. |
| + */ |
| + private List<TimedItem> filterOfflinePageItems() { |
| + List<TimedItem> filteredTimedItems = new ArrayList<>(); |
| + Map<Date, Integer> suggestedPageCountMap = new HashMap<>(); |
| + Map<Date, Long> suggestedPageTotalSizeMap = new HashMap<>(); |
| + |
| + BackendItems filteredOfflinePageItems = new BackendItemsImpl(); |
| + mOfflinePageItems.filter(mFilter, mSearchQuery, filteredOfflinePageItems); |
| + |
| + for (DownloadHistoryItemWrapper item : filteredOfflinePageItems) { |
| + OfflinePageItemWrapper offlineItem = (OfflinePageItemWrapper) item; |
| + |
| + // Add the suggested pages to the adapter only if the section is expanded for that date. |
| + if (offlineItem.isSuggested()) { |
| + incrementSuggestedPageCount( |
| + offlineItem, suggestedPageCountMap, suggestedPageTotalSizeMap); |
| + if (!isSubsectionExpanded(getDateWithoutTime(offlineItem.getTimestamp()))) continue; |
| + } |
| + filteredTimedItems.add(offlineItem); |
| + } |
| + |
| + // Add a TimedItem for each subsection. |
| + for (Date date : suggestedPageCountMap.keySet()) { |
| + 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
|
| + date, suggestedPageCountMap.get(date), suggestedPageTotalSizeMap.get(date))); |
| + } |
| + |
| + return filteredTimedItems; |
| + } |
| + |
| + // Updates the total number of suggested pages and file size grouped by date. |
| + private void incrementSuggestedPageCount(OfflinePageItemWrapper offlineItem, |
| + Map<Date, Integer> pageCountMap, Map<Date, Long> fileSizeMap) { |
| + Date date = getDateWithoutTime(offlineItem.getTimestamp()); |
| + |
| + int count = pageCountMap.containsKey(date) ? pageCountMap.get(date) : 0; |
| + pageCountMap.put(date, count + 1); |
| + |
| + long fileSize = fileSizeMap.containsKey(date) ? fileSizeMap.get(date) : 0; |
| + fileSizeMap.put(date, fileSize + offlineItem.getFileSize()); |
| + } |
| + |
| + /** |
| + * Whether the suggested pages section is expanded for a given date. |
| + * @param date The download date. |
| + * @return Whether the suggested pages section is expanded. |
| + */ |
| + public boolean isSubsectionExpanded(Date date) { |
| + // Default state is collpased. |
| + if (mSubSectionExpanded.get(date) == null) return false; |
| + |
| + return mSubSectionExpanded.get(date); |
| + } |
| + |
| + /** |
| + * Sets the state of a subsection for a particular date. |
| + * @param date The download date. |
| + * @param expanded Whether the suggested pages should be expanded. |
| + */ |
| + public void setSubsectionExpanded(Date date, boolean expanded) { |
| + mSubSectionExpanded.put(date, expanded); |
| clear(false); |
| - loadItems(mFilteredItems); |
| + filter(mFilter); |
| + } |
| + |
| + @Override |
| + protected boolean isSubsectionHeader(TimedItem timedItem) { |
| + return timedItem instanceof SubsectionHeader; |
| } |
| private void initializeOfflinePageBridge() { |
| @@ -477,4 +620,17 @@ public class DownloadHistoryAdapter extends DateDividedAdapter |
| RecordHistogram.recordCountHistogram("Android.DownloadManager.InitialCount.Total", |
| mRegularDownloadItems.size() + mOfflinePageItems.size()); |
| } |
| + |
| + /** |
| + * Calculates the {@link Date} for midnight of the date represented by the timestamp. |
| + */ |
| + private Date getDateWithoutTime(long timestamp) { |
| + Calendar cal = Calendar.getInstance(); |
| + cal.setTimeInMillis(timestamp); |
| + cal.set(Calendar.HOUR_OF_DAY, 0); |
| + cal.set(Calendar.MINUTE, 0); |
| + cal.set(Calendar.SECOND, 0); |
| + cal.set(Calendar.MILLISECOND, 0); |
| + return cal.getTime(); |
| + } |
| } |