Commit ef9df5c5 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Download home v2: Show storage used by downloads.

This CL changes the storage string to [storage used by download] of
[total disk size].

Bug: 892328
Change-Id: I6516c89e4f6bf71101c56b43171375a037cc36bf
Reviewed-on: https://chromium-review.googlesource.com/c/1277917
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#599703}
parent 0e821c2f
...@@ -14,7 +14,9 @@ import org.chromium.chrome.browser.download.DirectoryOption; ...@@ -14,7 +14,9 @@ import org.chromium.chrome.browser.download.DirectoryOption;
import org.chromium.chrome.browser.download.DownloadUtils; import org.chromium.chrome.browser.download.DownloadUtils;
import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterObserver; import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterObserver;
import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterSource; import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterSource;
import org.chromium.chrome.browser.download.ui.DownloadHistoryAdapter;
import org.chromium.components.offline_items_collection.OfflineItem; import org.chromium.components.offline_items_collection.OfflineItem;
import org.chromium.components.offline_items_collection.OfflineItemState;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
...@@ -31,8 +33,12 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver { ...@@ -31,8 +33,12 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver {
private final Context mContext; private final Context mContext;
private final Delegate mDelegate; private final Delegate mDelegate;
// Contains total space and available space of the file system.
private DirectoryOption mDirectoryOption; private DirectoryOption mDirectoryOption;
// The total size in bytes used by downloads.
private long mTotalDownloadSize;
/** /**
* Asynchronous task to query the default download directory option on primary storage. * Asynchronous task to query the default download directory option on primary storage.
* Pass one String parameter as the name of the directory option. * Pass one String parameter as the name of the directory option.
...@@ -55,25 +61,47 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver { ...@@ -55,25 +61,47 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver {
mContext = context; mContext = context;
mDelegate = delegate; mDelegate = delegate;
if (filterSource != null) filterSource.addObserver(this); if (filterSource != null) {
computeStorage(); filterSource.addObserver(this);
mTotalDownloadSize = getTotalSize(filterSource.getItems());
}
computeTotalStorage();
}
/**
* Sets the total size used by downloads. Used to support legacy download home UI, see
* {@link DownloadHistoryAdapter}.
* @param totalSize
*/
public void setUsedStorage(long totalSize) {
mTotalDownloadSize = totalSize;
update();
} }
// OfflineItemFilterObserver implementation. // OfflineItemFilterObserver implementation.
@Override @Override
public void onItemsAdded(Collection<OfflineItem> items) { public void onItemsAdded(Collection<OfflineItem> items) {
computeStorage(); mTotalDownloadSize += getTotalSize(items);
update();
} }
@Override @Override
public void onItemsRemoved(Collection<OfflineItem> items) { public void onItemsRemoved(Collection<OfflineItem> items) {
computeStorage(); mTotalDownloadSize -= getTotalSize(items);
update();
} }
@Override @Override
public void onItemUpdated(OfflineItem oldItem, OfflineItem item) {} public void onItemUpdated(OfflineItem oldItem, OfflineItem item) {
// Computes the delta of storage used by downloads.
mTotalDownloadSize -= oldItem.receivedBytes;
mTotalDownloadSize += oldItem.receivedBytes;
if (item.state != OfflineItemState.IN_PROGRESS) update();
}
private void computeStorage() { private void computeTotalStorage() {
DefaultDirectoryTask task = new DefaultDirectoryTask() { DefaultDirectoryTask task = new DefaultDirectoryTask() {
@Override @Override
protected void onPostExecute(DirectoryOption directoryOption) { protected void onPostExecute(DirectoryOption directoryOption) {
...@@ -84,14 +112,19 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver { ...@@ -84,14 +112,19 @@ public class StorageSummaryProvider implements OfflineItemFilterObserver {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
private long getTotalSize(Collection<OfflineItem> items) {
long totalSize = 0;
for (OfflineItem item : items) totalSize += item.receivedBytes;
return totalSize;
}
private void update() { private void update() {
if (mDirectoryOption == null) return; if (mDirectoryOption == null) return;
// Build the storage summary string. // Build the storage summary string.
long usedSpace = mDirectoryOption.totalSpace - mDirectoryOption.availableSpace; assert(mTotalDownloadSize >= 0);
if (usedSpace < 0) usedSpace = 0;
String storageSummary = mContext.getString(R.string.download_manager_ui_space_using, String storageSummary = mContext.getString(R.string.download_manager_ui_space_using,
DownloadUtils.getStringForBytes(mContext, usedSpace), DownloadUtils.getStringForBytes(mContext, mTotalDownloadSize),
DownloadUtils.getStringForBytes(mContext, mDirectoryOption.totalSpace)); DownloadUtils.getStringForBytes(mContext, mDirectoryOption.totalSpace));
mDelegate.onStorageInfoChanged(storageSummary); mDelegate.onStorageInfoChanged(storageSummary);
} }
......
...@@ -407,7 +407,7 @@ public class DownloadHistoryAdapter ...@@ -407,7 +407,7 @@ public class DownloadHistoryAdapter
@Override @Override
protected void bindViewHolderForHeaderItem(ViewHolder viewHolder, HeaderItem headerItem) { protected void bindViewHolderForHeaderItem(ViewHolder viewHolder, HeaderItem headerItem) {
super.bindViewHolderForHeaderItem(viewHolder, headerItem); super.bindViewHolderForHeaderItem(viewHolder, headerItem);
mSpaceDisplay.onChanged(); updateStorageSummary();
} }
/** /**
...@@ -500,7 +500,7 @@ public class DownloadHistoryAdapter ...@@ -500,7 +500,7 @@ public class DownloadHistoryAdapter
if (TextUtils.equals(item.getId(), wrapper.getId())) { if (TextUtils.equals(item.getId(), wrapper.getId())) {
view.displayItem(mBackendProvider, existingWrapper); view.displayItem(mBackendProvider, existingWrapper);
if (item.getDownloadInfo().state() == DownloadState.COMPLETE) { if (item.getDownloadInfo().state() == DownloadState.COMPLETE) {
mSpaceDisplay.onChanged(); updateStorageSummary();
} }
} }
} }
...@@ -901,7 +901,7 @@ public class DownloadHistoryAdapter ...@@ -901,7 +901,7 @@ public class DownloadHistoryAdapter
if (TextUtils.equals(item.id.id, view.getItem().getId())) { if (TextUtils.equals(item.id.id, view.getItem().getId())) {
view.displayItem(mBackendProvider, existingWrapper); view.displayItem(mBackendProvider, existingWrapper);
if (item.state == OfflineItemState.COMPLETE) { if (item.state == OfflineItemState.COMPLETE) {
mSpaceDisplay.onChanged(); updateStorageSummary();
} }
} }
} }
...@@ -929,4 +929,11 @@ public class DownloadHistoryAdapter ...@@ -929,4 +929,11 @@ public class DownloadHistoryAdapter
return mTimeThresholdForRecentBadgeMs; return mTimeThresholdForRecentBadgeMs;
} }
private void updateStorageSummary() {
if (mSpaceDisplay != null) mSpaceDisplay.onChanged();
if (mStorageSummaryProvider != null) {
mStorageSummaryProvider.setUsedStorage(getTotalDownloadSize());
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment