Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageFreeUpSpaceDialog.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageFreeUpSpaceDialog.java b/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageFreeUpSpaceDialog.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca05c8202d205599cde7a24c4188826570dffa63 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offline_pages/OfflinePageFreeUpSpaceDialog.java |
| @@ -0,0 +1,104 @@ |
| +// 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.Dialog; |
| +import android.app.DialogFragment; |
| +import android.content.DialogInterface; |
| +import android.os.Bundle; |
| +import android.support.v7.app.AlertDialog; |
| +import android.text.format.Formatter; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.offline_pages.OfflinePageBridge.DeletePageCallback; |
| +import org.chromium.components.bookmarks.BookmarkId; |
| + |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * Helper class showing the dialog to allowing to "Free up space" taken by offline pages. |
|
jianli
2015/08/21 21:06:27
I don't think this is a helper class. How about:
fgorski
2015/08/21 22:09:39
Done.
|
| + */ |
| +public class OfflinePageFreeUpSpaceDialog |
| + extends DialogFragment implements DialogInterface.OnClickListener { |
| + OfflinePageBridge mOfflinePageBridge; |
| + List<OfflinePageItem> mOfflinePagesToDelete; |
| + final FreeUpSpaceCallback mCallback; |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
nit: private for these variables?
fgorski
2015/08/21 22:09:39
Done. Thanks for catching that.
|
| + |
| + /** |
| + * Interface for informing the caller that the clean up operation is completed. |
| + */ |
| + public interface FreeUpSpaceCallback { |
| + /** Called when clean up operation is completed, or the dialog is cancelled. */ |
| + void onCleanUpSpaceDone(); |
|
jianli
2015/08/21 21:06:27
add public
fgorski
2015/08/21 22:09:38
Interface methods are public by default.
http://d
|
| + } |
| + |
| + /** |
| + * Creates the dialog. If the passed bridge instance needs to be destroyed after the dialog |
| + * is finished, it should be taken care of in the callback implementation. |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
Hmm... this is subtle because OfflinePageBridge is
Kibeom Kim (inactive)
2015/08/21 21:15:08
Correction:
Said the last sentence backward, and w
fgorski
2015/08/21 22:09:39
This dialog will also be shown after a snackbar:
1
|
| + * |
| + * @param offlinePageBridge An object to access offline page functionality. |
| + * @param callback An object that will be called when the dialog finishes. Can be null. |
| + * @see FreeUpSpaceCallback |
| + */ |
| + public OfflinePageFreeUpSpaceDialog( |
| + OfflinePageBridge offlinePageBridge, FreeUpSpaceCallback callback) { |
| + assert offlinePageBridge != null; |
| + mOfflinePageBridge = offlinePageBridge; |
| + mCallback = callback; |
| + } |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
Android tries to call an empty constructor on Dial
fgorski
2015/08/21 22:09:38
Done.
|
| + |
| + @Override |
| + public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
I believe that this is recreated when Chrome activ
fgorski
2015/08/21 22:09:38
Done.
|
| + AlertDialog.Builder builder = |
| + new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme) |
| + .setTitle(R.string.offline_pages_free_up_space_title) |
| + .setPositiveButton(R.string.delete, this) |
| + .setNegativeButton(R.string.cancel, this); |
| + if (mOfflinePageBridge != null) { |
|
Kibeom Kim (inactive)
2015/08/21 20:19:39
I believe that this is not needed?
fgorski
2015/08/21 22:09:39
Done.
|
| + mOfflinePagesToDelete = mOfflinePageBridge.getPagesToCleanUp(); |
| + builder.setMessage(getString(R.string.offline_pages_free_up_space_message, |
| + mOfflinePagesToDelete.size(), |
| + Formatter.formatFileSize(getActivity(), getTotalSize()))); |
| + } else { |
| + builder.setMessage(getString(R.string.offline_pages_free_up_space_message, 0, "0MB")); |
| + } |
| + return builder.create(); |
| + } |
| + |
| + @Override |
| + public void onClick(DialogInterface dialog, int id) { |
| + if (id != AlertDialog.BUTTON_POSITIVE || mOfflinePageBridge == null) { |
|
jianli
2015/08/21 21:06:27
Since we already assert at line 48, the 2nd condit
fgorski
2015/08/21 22:09:39
Done.
|
| + dialog.cancel(); |
| + if (mCallback != null) mCallback.onCleanUpSpaceDone(); |
| + return; |
| + } |
| + |
| + mOfflinePageBridge.deletePages(getBookmarkIdsToDelete(), new DeletePageCallback() { |
| + @Override |
| + public void onDeletePageDone(int deletePageResult) { |
| + if (mCallback != null) mCallback.onCleanUpSpaceDone(); |
| + } |
| + }); |
| + } |
| + |
| + /** @return A list of Bookmark IDs for which the offline pages will be deleted. */ |
| + private List<BookmarkId> getBookmarkIdsToDelete() { |
| + List<BookmarkId> bookmarkIds = new ArrayList<BookmarkId>(); |
| + for (OfflinePageItem offlinePage : mOfflinePagesToDelete) { |
| + bookmarkIds.add(offlinePage.getBookmarkId()); |
| + } |
| + return bookmarkIds; |
| + } |
| + |
| + /** @return A total size of offline pages that will be deleted. */ |
| + private long getTotalSize() { |
| + long totalSize = 0; |
| + for (OfflinePageItem offlinePage : mOfflinePagesToDelete) { |
| + totalSize += offlinePage.getFileSize(); |
| + } |
| + return totalSize; |
| + } |
| +} |