| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.offlinepages; | |
| 6 | |
| 7 import android.app.Dialog; | |
| 8 import android.app.DialogFragment; | |
| 9 import android.content.Context; | |
| 10 import android.content.DialogInterface; | |
| 11 import android.os.Bundle; | |
| 12 import android.support.v7.app.AlertDialog; | |
| 13 import android.text.format.Formatter; | |
| 14 | |
| 15 import org.chromium.base.metrics.RecordUserAction; | |
| 16 import org.chromium.chrome.R; | |
| 17 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.DeletePageCall
back; | |
| 18 import org.chromium.chrome.browser.snackbar.Snackbar; | |
| 19 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | |
| 20 | |
| 21 import java.util.ArrayList; | |
| 22 import java.util.List; | |
| 23 | |
| 24 /** | |
| 25 * Shows a "Free up space" dialog to clean up storage for offline pages. | |
| 26 */ | |
| 27 public class OfflinePageFreeUpSpaceDialog | |
| 28 extends DialogFragment implements DialogInterface.OnClickListener { | |
| 29 private OfflinePageBridge mOfflinePageBridge; | |
| 30 private List<OfflinePageItem> mOfflinePagesToDelete; | |
| 31 private OfflinePageFreeUpSpaceCallback mCallback; | |
| 32 | |
| 33 /** | |
| 34 * Creates the dialog. If the passed bridge instance needs to be destroyed a
fter the dialog | |
| 35 * is finished, it should be taken care of in the callback implementation. | |
| 36 * | |
| 37 * @param offlinePageBridge An object to access offline page functionality. | |
| 38 * @param callback An object that will be called when the dialog finishes. C
an be null. | |
| 39 * @see OfflinePageFreeUpSpaceCallback | |
| 40 */ | |
| 41 public static OfflinePageFreeUpSpaceDialog newInstance( | |
| 42 OfflinePageBridge offlinePageBridge, OfflinePageFreeUpSpaceCallback
callback) { | |
| 43 assert offlinePageBridge != null; | |
| 44 OfflinePageFreeUpSpaceDialog dialog = new OfflinePageFreeUpSpaceDialog()
; | |
| 45 dialog.mOfflinePageBridge = offlinePageBridge; | |
| 46 dialog.mCallback = callback; | |
| 47 return dialog; | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Creates a snackbar informing user that the storage has been cleared. | |
| 52 */ | |
| 53 public static Snackbar createStorageClearedSnackbar(Context context) { | |
| 54 return Snackbar.make(context.getString(OfflinePageUtils.getStringId( | |
| 55 R.string.offline_pages_storage_cleared)), | |
| 56 new SnackbarController() { | |
| 57 @Override | |
| 58 public void onDismissNoAction(Object actionData) {} | |
| 59 @Override | |
| 60 public void onAction(Object actionData) {} | |
| 61 }, Snackbar.TYPE_ACTION); | |
| 62 } | |
| 63 | |
| 64 @Override | |
| 65 public Dialog onCreateDialog(Bundle savedInstanceState) { | |
| 66 if (savedInstanceState != null) dismiss(); | |
| 67 | |
| 68 mOfflinePagesToDelete = mOfflinePageBridge.getPagesToCleanUp(); | |
| 69 AlertDialog.Builder builder = | |
| 70 new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme) | |
| 71 .setTitle(OfflinePageUtils.getStringId( | |
| 72 R.string.offline_pages_free_up_space_title)) | |
| 73 .setPositiveButton(R.string.delete, this) | |
| 74 .setNegativeButton(R.string.cancel, this) | |
| 75 .setMessage(getString(OfflinePageUtils.getStringId( | |
| 76 R.string.offline_pages_free_up_space_message), | |
| 77 mOfflinePagesToDelete.size(), | |
| 78 Formatter.formatFileSize(getActivity(), getTotal
Size()))); | |
| 79 return builder.create(); | |
| 80 } | |
| 81 | |
| 82 @Override | |
| 83 public void onClick(DialogInterface dialog, int id) { | |
| 84 if (id == AlertDialog.BUTTON_NEGATIVE) { | |
| 85 RecordUserAction.record("OfflinePages.FreeUpSpaceDialogButtonNotClic
ked"); | |
| 86 dialog.cancel(); | |
| 87 if (mCallback != null) mCallback.onFreeUpSpaceCancelled(); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 mOfflinePageBridge.deletePages(getOfflineIdsToDelete(), new DeletePageCa
llback() { | |
| 92 @Override | |
| 93 public void onDeletePageDone(int deletePageResult) { | |
| 94 RecordUserAction.record("OfflinePages.FreeUpSpaceDialogButtonCli
cked"); | |
| 95 if (mCallback != null) mCallback.onFreeUpSpaceDone(); | |
| 96 } | |
| 97 }); | |
| 98 } | |
| 99 | |
| 100 /** Returns a list of IDs for which the offline pages will be deleted. */ | |
| 101 private List<Long> getOfflineIdsToDelete() { | |
| 102 List<Long> offlineIds = new ArrayList<>(); | |
| 103 for (OfflinePageItem offlinePage : mOfflinePagesToDelete) { | |
| 104 offlineIds.add(offlinePage.getOfflineId()); | |
| 105 } | |
| 106 return offlineIds; | |
| 107 } | |
| 108 | |
| 109 /** Returns a total size of offline pages that will be deleted. */ | |
| 110 private long getTotalSize() { | |
| 111 long totalSize = 0; | |
| 112 for (OfflinePageItem offlinePage : mOfflinePagesToDelete) { | |
| 113 totalSize += offlinePage.getFileSize(); | |
| 114 } | |
| 115 return totalSize; | |
| 116 } | |
| 117 } | |
| OLD | NEW |