Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataDialogFragment.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataDialogFragment.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataDialogFragment.java |
| index 1f763520c030c36594c2255aa216bee03d364622..cf9ccc2bb612e4d5e1bb25e34866b19fe1c4239a 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataDialogFragment.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/privacy/ClearBrowsingDataDialogFragment.java |
| @@ -22,10 +22,12 @@ import android.widget.AdapterView.OnItemClickListener; |
| import android.widget.ArrayAdapter; |
| import android.widget.Button; |
| import android.widget.CheckedTextView; |
| +import android.widget.LinearLayout; |
| import android.widget.TextView; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.ClankBrowsingDataType; |
| import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| import org.chromium.chrome.browser.preferences.Preferences; |
| import org.chromium.chrome.browser.signin.AccountManagementFragment; |
| @@ -41,16 +43,82 @@ import java.util.EnumSet; |
| */ |
| public class ClearBrowsingDataDialogFragment extends DialogFragment |
| implements PrefServiceBridge.OnClearBrowsingDataListener, DialogInterface.OnClickListener { |
| - private class ClearBrowsingDataAdapter extends ArrayAdapter<String> { |
| - final DialogOption[] mOptions; |
| - final EnumSet<DialogOption> mDisabledOptions; |
| - |
| - private ClearBrowsingDataAdapter(DialogOption[] options, String[] optionNames, |
| - EnumSet<DialogOption> disabledOptions) { |
| - super(getActivity(), R.layout.select_dialog_multichoice_material, optionNames); |
| - assert options.length == optionNames.length; |
| - mOptions = options; |
| - mDisabledOptions = disabledOptions; |
| + /** |
| + * Represents a single item in the dialog. |
| + */ |
| + private static class Item implements BrowsingDataCounterCallback { |
| + private final DialogOption mOption; |
| + private String mTitle; |
| + private String mCounterText; |
| + private boolean mSelected; |
| + private boolean mEnabled; |
| + private ArrayAdapter<Item> mParentAdapter; |
| + private BrowsingDataCounterBridge mCounter; |
| + |
| + public Item(DialogOption option, |
| + String title, |
| + boolean selected, |
| + boolean enabled) { |
| + super(); |
| + mOption = option; |
| + mTitle = title; |
| + mSelected = selected; |
| + mEnabled = enabled; |
| + mCounterText = ""; |
| + mParentAdapter = null; |
| + mCounter = new BrowsingDataCounterBridge(this, mOption.getDataType()); |
| + } |
| + |
| + public void destroy() { |
| + mCounter.destroy(); |
| + } |
| + |
| + public DialogOption getOption() { |
| + return mOption; |
| + } |
| + |
| + @Override |
| + public String toString() { |
| + return mTitle; |
| + } |
| + |
| + public boolean isSelected() { |
| + return mSelected; |
| + } |
| + |
| + public void toggle() { |
| + mSelected = !mSelected; |
| + PrefServiceBridge.getInstance().setBrowsingDataDeletionPreference( |
| + mOption.getDataType(), mSelected); |
| + |
| + // Counter text is only shown with selected items. |
| + if (!mSelected) mCounterText = ""; |
| + } |
| + |
| + public boolean isEnabled() { |
| + return mEnabled; |
| + } |
| + |
| + public String getCounterText() { |
| + return mCounterText; |
| + } |
| + |
| + public void setParentAdapter(ArrayAdapter<Item> adapter) { |
| + mParentAdapter = adapter; |
| + } |
| + |
| + @Override |
| + public void onCounterFinished(String result) { |
| + mCounterText = result; |
| + |
| + if (mParentAdapter != null) mParentAdapter.notifyDataSetChanged(); |
| + } |
| + } |
| + |
| + private class ClearBrowsingDataAdapter extends ArrayAdapter<Item> { |
| + private ClearBrowsingDataAdapter(Item[] items) { |
| + super(getActivity(), R.layout.clear_browsing_data_dialog_item, R.id.title, items); |
| + for (Item item : items) item.setParentAdapter(this); |
| } |
| @Override |
| @@ -60,25 +128,29 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| @Override |
| public View getView(int position, View convertView, ViewGroup parent) { |
| - CheckedTextView view = (CheckedTextView) super.getView(position, convertView, parent); |
| - DialogOption option = mOptions[position]; |
| - view.setChecked(mSelectedOptions.contains(option)); |
| - view.setEnabled(!mDisabledOptions.contains(option)); |
| + LinearLayout view = (LinearLayout) super.getView(position, convertView, parent); |
| + CheckedTextView radioButton = (CheckedTextView) view.findViewById(R.id.title); |
| + radioButton.setChecked(getItem(position).isSelected()); |
| + radioButton.setEnabled(getItem(position).isEnabled()); |
| + TextView counter = (TextView) view.findViewById(R.id.summary); |
| + String counterText = getItem(position).getCounterText(); |
| + counter.setText(counterText); |
| + |
| + // Remove the counter if the text is empty, so when the counters experiment is |
| + // disabled, it doesn't break alignment. |
| + counter.setVisibility(counterText.isEmpty() ? View.GONE : View.VISIBLE); |
| + |
| return view; |
| } |
| public void onClick(int position) { |
| - DialogOption selectedOption = mOptions[position]; |
| - if (mDisabledOptions.contains(selectedOption)) { |
| + if (!getItem(position).isEnabled()) { |
| + // Currently only the history deletion can be disabled. |
| Toast.makeText(getActivity(), R.string.can_not_clear_browsing_history_toast, |
| Toast.LENGTH_SHORT).show(); |
| return; |
| } |
| - if (mSelectedOptions.contains(selectedOption)) { |
| - mSelectedOptions.remove(selectedOption); |
| - } else { |
| - mSelectedOptions.add(selectedOption); |
| - } |
| + getItem(position).toggle(); |
| updateButtonState(); |
| notifyDataSetChanged(); |
| } |
| @@ -91,20 +163,38 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| * Enum for Dialog options to be displayed in the dialog. |
| */ |
| public enum DialogOption { |
| - CLEAR_HISTORY(R.string.clear_history_title), |
| - CLEAR_CACHE(R.string.clear_cache_title), |
| - CLEAR_COOKIES_AND_SITE_DATA(R.string.clear_cookies_and_site_data_title), |
| - CLEAR_PASSWORDS(R.string.clear_passwords_title), |
| - CLEAR_FORM_DATA(R.string.clear_formdata_title), |
| + CLEAR_HISTORY( |
| + ClankBrowsingDataType.HISTORY, |
| + R.string.clear_history_title), |
| + CLEAR_CACHE( |
| + ClankBrowsingDataType.CACHE, |
| + R.string.clear_cache_title), |
| + CLEAR_COOKIES_AND_SITE_DATA( |
| + ClankBrowsingDataType.COOKIES, |
| + R.string.clear_cookies_and_site_data_title), |
| + CLEAR_PASSWORDS( |
| + ClankBrowsingDataType.PASSWORDS, |
| + R.string.clear_passwords_title), |
| + CLEAR_FORM_DATA( |
| + ClankBrowsingDataType.FORM_DATA, |
| + R.string.clear_formdata_title), |
| // Clear bookmarks is only used by ClearSyncData dialog. |
| - CLEAR_BOOKMARKS_DATA(R.string.clear_bookmarks_title); |
| + CLEAR_BOOKMARKS_DATA( |
| + ClankBrowsingDataType.BOOKMARKS, |
| + R.string.clear_bookmarks_title); |
| + private final int mDataType; |
| private final int mResourceId; |
| - private DialogOption(int resourceId) { |
| + private DialogOption(int dataType, int resourceId) { |
| + mDataType = dataType; |
| mResourceId = resourceId; |
| } |
| + public int getDataType() { |
| + return mDataType; |
| + } |
| + |
| /** |
| * @return resource id of the Dialog option. |
| */ |
| @@ -117,15 +207,27 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| private ProgressDialog mProgressDialog; |
| private ClearBrowsingDataAdapter mAdapter; |
| private boolean mCanDeleteBrowsingHistory; |
| - private EnumSet<DialogOption> mSelectedOptions; |
| - |
| - protected final void clearBrowsingData(EnumSet<DialogOption> selectedOptions) { |
| - PrefServiceBridge.getInstance().clearBrowsingData(this, |
| - selectedOptions.contains(DialogOption.CLEAR_HISTORY), |
| - selectedOptions.contains(DialogOption.CLEAR_CACHE), |
| - selectedOptions.contains(DialogOption.CLEAR_COOKIES_AND_SITE_DATA), |
| - selectedOptions.contains(DialogOption.CLEAR_PASSWORDS), |
| - selectedOptions.contains(DialogOption.CLEAR_FORM_DATA)); |
| + private Item[] mItems; |
| + |
| + protected final EnumSet<DialogOption> getSelectedOptions() { |
| + EnumSet<DialogOption> selected = EnumSet.noneOf(DialogOption.class); |
| + for (Item item : mItems) { |
| + if (item.isSelected()) selected.add(item.getOption()); |
| + } |
| + return selected; |
| + } |
| + |
| + protected final void clearBrowsingData() { |
| + EnumSet<DialogOption> options = getSelectedOptions(); |
| + int[] dataTypes = new int[options.size()]; |
| + |
| + int i = 0; |
| + for (DialogOption option : options) { |
| + dataTypes[i] = option.getDataType(); |
| + ++i; |
| + } |
| + |
| + PrefServiceBridge.getInstance().clearBrowsingData(this, dataTypes); |
| } |
| protected void dismissProgressDialog() { |
| @@ -154,24 +256,16 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| * Get the default selections for the dialog. |
| * @return EnumSet containing dialog options to be selected. |
| */ |
| - protected EnumSet<DialogOption> getDefaultDialogOptionsSelections() { |
| - EnumSet<DialogOption> defaultOptions = EnumSet.of(DialogOption.CLEAR_CACHE, |
| - DialogOption.CLEAR_COOKIES_AND_SITE_DATA, DialogOption.CLEAR_HISTORY); |
| - if (!mCanDeleteBrowsingHistory) { |
| - defaultOptions.remove(DialogOption.CLEAR_HISTORY); |
| + protected boolean isOptionSelectedByDefault(DialogOption option) { |
| + switch (option) { |
| + case CLEAR_HISTORY: |
| + return mCanDeleteBrowsingHistory; |
| + case CLEAR_COOKIES_AND_SITE_DATA: |
| + case CLEAR_CACHE: |
| + return true; |
| + default: |
| + return false; |
| } |
| - return defaultOptions; |
| - } |
| - |
| - /** |
| - * Get the selections that have been disabled for the dialog. |
| - * @return EnumSet containing dialog options that have been disabled. |
| - */ |
| - protected EnumSet<DialogOption> getDisabledDialogOptions() { |
| - if (!mCanDeleteBrowsingHistory) { |
| - return EnumSet.of(DialogOption.CLEAR_HISTORY); |
| - } |
| - return EnumSet.noneOf(DialogOption.class); |
| } |
| // Called when "clear browsing data" completes. |
| @@ -185,7 +279,7 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| public void onClick(DialogInterface dialog, int whichButton) { |
| if (whichButton == AlertDialog.BUTTON_POSITIVE) { |
| dismissProgressDialog(); |
| - onOptionSelected(mSelectedOptions); |
| + onOptionSelected(); |
| } |
| } |
| @@ -195,7 +289,7 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| private void updateButtonState() { |
| Button clearButton = mDialog.getButton(AlertDialog.BUTTON_POSITIVE); |
| if (clearButton == null) return; |
| - boolean isEnabled = !mSelectedOptions.isEmpty(); |
| + boolean isEnabled = !getSelectedOptions().isEmpty(); |
| clearButton.setEnabled(isEnabled); |
| // Work around a bug in the app compat library where disabled buttons in alert dialogs |
| @@ -209,13 +303,17 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| mCanDeleteBrowsingHistory = PrefServiceBridge.getInstance().canDeleteBrowsingHistory(); |
| DialogOption[] options = getDialogOptions(); |
| - String[] optionNames = new String[options.length]; |
| + mItems = new Item[options.length]; |
| Resources resources = getResources(); |
| for (int i = 0; i < options.length; i++) { |
| - optionNames[i] = resources.getString(options[i].getResourceId()); |
| + mItems[i] = new Item( |
| + options[i], |
| + resources.getString(options[i].getResourceId()), |
| + PrefServiceBridge.getInstance().getBrowsingDataDeletionPreference( |
| + options[i].getDataType()), |
| + options[i] != DialogOption.CLEAR_HISTORY || mCanDeleteBrowsingHistory); |
|
newt (away)
2016/01/12 18:55:26
I'd make this line more readable by first creating
msramek
2016/01/13 15:27:53
Done.
|
| } |
| - mSelectedOptions = getDefaultDialogOptionsSelections(); |
| - mAdapter = new ClearBrowsingDataAdapter(options, optionNames, getDisabledDialogOptions()); |
| + mAdapter = new ClearBrowsingDataAdapter(mItems); |
| final AlertDialog.Builder builder = |
| new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme) |
| .setTitle(R.string.clear_browsing_data_title) |
| @@ -270,13 +368,20 @@ public class ClearBrowsingDataDialogFragment extends DialogFragment |
| updateButtonState(); |
| } |
| + @Override |
| + public void onDestroy() { |
| + super.onDestroy(); |
| + for (Item item : mItems) { |
| + item.destroy(); |
| + } |
| + } |
| + |
| /** |
| * Called when PositiveButton is clicked for the dialog. |
| - * @param selectedOptions options which were selected. |
| */ |
| - protected void onOptionSelected(final EnumSet<DialogOption> selectedOptions) { |
| + protected void onOptionSelected() { |
| showProgressDialog(); |
| - clearBrowsingData(selectedOptions); |
| + clearBrowsingData(); |
| } |
| protected final void showProgressDialog() { |