Skip to content

Commit

Permalink
Download home v2: Show storage used by downloads.
Browse files Browse the repository at this point in the history
This CL changes the storage string to [storage used by download] of
[total disk size].

TBR=xingliu@chromium.org

(cherry picked from commit ef9df5c)

Bug: 892328
Change-Id: I6516c89e4f6bf71101c56b43171375a037cc36bf
Reviewed-on: https://chromium-review.googlesource.com/c/1277917
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: David Trainor <dtrainor@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#599703}
Reviewed-on: https://chromium-review.googlesource.com/c/1284210
Reviewed-by: Xing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/branch-heads/3578@{#56}
Cr-Branched-From: 4226ddf-refs/heads/master@{#599034}
  • Loading branch information
Xing Liu committed Oct 16, 2018
1 parent a2dbce6 commit 45e0261
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.chromium.chrome.browser.download.DownloadUtils;
import org.chromium.chrome.browser.download.home.filter.OfflineItemFilterObserver;
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.OfflineItemState;

import java.io.File;
import java.util.Collection;
Expand All @@ -31,8 +33,12 @@ public interface Delegate { void onStorageInfoChanged(String storageInfo); }
private final Context mContext;
private final Delegate mDelegate;

// Contains total space and available space of the file system.
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.
* Pass one String parameter as the name of the directory option.
Expand All @@ -55,25 +61,47 @@ public StorageSummaryProvider(
mContext = context;
mDelegate = delegate;

if (filterSource != null) filterSource.addObserver(this);
computeStorage();
if (filterSource != null) {
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.
@Override
public void onItemsAdded(Collection<OfflineItem> items) {
computeStorage();
mTotalDownloadSize += getTotalSize(items);
update();
}

@Override
public void onItemsRemoved(Collection<OfflineItem> items) {
computeStorage();
mTotalDownloadSize -= getTotalSize(items);
update();
}

@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() {
@Override
protected void onPostExecute(DirectoryOption directoryOption) {
Expand All @@ -84,14 +112,19 @@ protected void onPostExecute(DirectoryOption directoryOption) {
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() {
if (mDirectoryOption == null) return;

// Build the storage summary string.
long usedSpace = mDirectoryOption.totalSpace - mDirectoryOption.availableSpace;
if (usedSpace < 0) usedSpace = 0;
assert(mTotalDownloadSize >= 0);
String storageSummary = mContext.getString(R.string.download_manager_ui_space_using,
DownloadUtils.getStringForBytes(mContext, usedSpace),
DownloadUtils.getStringForBytes(mContext, mTotalDownloadSize),
DownloadUtils.getStringForBytes(mContext, mDirectoryOption.totalSpace));
mDelegate.onStorageInfoChanged(storageSummary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void bindViewHolderForTimedItem(ViewHolder current, TimedItem timedItem)
@Override
protected void bindViewHolderForHeaderItem(ViewHolder viewHolder, HeaderItem headerItem) {
super.bindViewHolderForHeaderItem(viewHolder, headerItem);
mSpaceDisplay.onChanged();
updateStorageSummary();
}

/**
Expand Down Expand Up @@ -500,7 +500,7 @@ public void onDownloadItemUpdated(DownloadItem item) {
if (TextUtils.equals(item.getId(), wrapper.getId())) {
view.displayItem(mBackendProvider, existingWrapper);
if (item.getDownloadInfo().state() == DownloadState.COMPLETE) {
mSpaceDisplay.onChanged();
updateStorageSummary();
}
}
}
Expand Down Expand Up @@ -901,7 +901,7 @@ public void onItemUpdated(OfflineItem item) {
if (TextUtils.equals(item.id.id, view.getItem().getId())) {
view.displayItem(mBackendProvider, existingWrapper);
if (item.state == OfflineItemState.COMPLETE) {
mSpaceDisplay.onChanged();
updateStorageSummary();
}
}
}
Expand Down Expand Up @@ -929,4 +929,11 @@ private int getRecentBadgeTimeThreshold() {

return mTimeThresholdForRecentBadgeMs;
}

private void updateStorageSummary() {
if (mSpaceDisplay != null) mSpaceDisplay.onChanged();
if (mStorageSummaryProvider != null) {
mStorageSummaryProvider.setUsedStorage(getTotalDownloadSize());
}
}
}

0 comments on commit 45e0261

Please sign in to comment.