Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageStorageSpaceHeader.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageStorageSpaceHeader.java b/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageStorageSpaceHeader.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..042dae056247d8a3b5fa1b2ca3fbdb4279721ddb |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageStorageSpaceHeader.java |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.offline_pages; |
| + |
| +import android.app.Activity; |
| +import android.content.Context; |
| +import android.support.v7.widget.RecyclerView.ViewHolder; |
| +import android.text.format.Formatter; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.offline_pages.OfflinePageFreeUpSpaceDialog.FreeUpSpaceCallback; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * Header shown in saved pages view to inform user of total used storage and offer freeing up |
| + * space. |
| + */ |
| +public class OfflinePageStorageSpaceHeader { |
| + private static final long MINIMUM_TOTAL_SIZE_TO_OFFER = 10 * (1 << 20); // 10MB |
|
jianli
2015/08/21 21:06:27
I found out the suffix "TO_OFFER" was hard to unde
fgorski
2015/08/21 22:09:39
Done.
|
| + private static final long MINIMUM_CEALNUP_SIZE_TO_OFFER = 5 * (1 << 20); // 5MB |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
suggestion: include unit (BYTE) in the constant na
fgorski
2015/08/21 22:09:39
Done.
|
| + |
| + OfflinePageBridge mOfflinePageBridge; |
| + Context mContext; |
| + StorageSpaceHeaderCallback mCallback; |
| + |
| + /** Interface for consumers of the "free up space" functionality. */ |
| + public interface StorageSpaceHeaderCallback { |
| + /** Callback called when the clean up completes. */ |
| + void onStorageSpaceCleanUpDone(); |
|
jianli
2015/08/21 21:06:27
add public
fgorski
2015/08/21 22:09:39
not necessary per previous comment.
|
| + } |
| + |
| + /** |
| + * @param offlinePageBridge An object to access offline page functionality. |
| + */ |
| + public OfflinePageStorageSpaceHeader(Context context, OfflinePageBridge offlinePageBridge, |
| + StorageSpaceHeaderCallback callback) { |
| + assert offlinePageBridge != null; |
| + mOfflinePageBridge = offlinePageBridge; |
| + mContext = context; |
| + mCallback = callback; |
| + } |
| + |
| + /** @return Whether the header should be shown. */ |
| + public boolean shouldShow() { |
| + return getSizeOfAllPages() > MINIMUM_TOTAL_SIZE_TO_OFFER |
| + && getSizeOfPagesToCleanUp() > MINIMUM_CEALNUP_SIZE_TO_OFFER; |
| + } |
| + |
| + /** @return A view holder with the contents of the header. */ |
| + public ViewHolder createHolder(ViewGroup parent) { |
| + // TODO(fgorski): Enable recalculation in case some pages were deleted. |
| + ViewGroup header = (ViewGroup) LayoutInflater.from(mContext).inflate( |
| + R.layout.eb_offline_pages_storage_space_header, parent, false); |
| + |
| + ((TextView) header.findViewById(R.id.offline_pages_storage_size)) |
| + .setText(mContext.getString(R.string.offline_pages_storage_space_message, |
| + Formatter.formatFileSize(mContext, getSizeOfAllPages()))); |
| + |
| + header.findViewById(R.id.offline_pages_free_up_space_btn) |
| + .setOnClickListener(new View.OnClickListener() { |
| + @Override |
| + public void onClick(View view) { |
| + OfflinePageFreeUpSpaceDialog dialog = new OfflinePageFreeUpSpaceDialog( |
| + mOfflinePageBridge, new FreeUpSpaceCallback() { |
| + @Override |
| + public void onCleanUpSpaceDone() { |
| + if (mCallback != null) { |
| + mCallback.onStorageSpaceCleanUpDone(); |
| + } |
| + } |
| + }); |
| + dialog.show(((Activity) mContext).getFragmentManager(), null); |
| + } |
| + }); |
| + |
| + return new ViewHolder(header) {}; |
| + } |
| + |
| + private long getSizeOfAllPages() { |
| + return getTotalSize(mOfflinePageBridge.getAllPages()); |
| + } |
| + |
| + private long getSizeOfPagesToCleanUp() { |
| + return getTotalSize(mOfflinePageBridge.getPagesToCleanUp()); |
| + } |
| + |
| + private long getTotalSize(List<OfflinePageItem> offlinePages) { |
| + long totalSize = 0; |
| + for (OfflinePageItem page : offlinePages) { |
| + totalSize += page.getFileSize(); |
| + } |
| + return totalSize; |
| + } |
| +} |