Mechanism to clear data through "Reset to default" setting
- Added hooks in BrowserYesNoPreference to support a third
button.
- Enabled the third button for "Reset to default" setting
- If user clicks on this new button, it'll also clear the
private data (history, cache, etc)
Change-Id: I7b224805d2ab8ef440a13948821c871b0b4a791e
diff --git a/src/com/android/browser/BrowserYesNoPreference.java b/src/com/android/browser/BrowserYesNoPreference.java
index f47ff3d..289ddf3 100644
--- a/src/com/android/browser/BrowserYesNoPreference.java
+++ b/src/com/android/browser/BrowserYesNoPreference.java
@@ -16,14 +16,14 @@
package com.android.browser;
-import android.app.Activity;
+import android.app.AlertDialog;
import android.content.Context;
-import android.content.Intent;
+import android.content.DialogInterface;
import android.content.SharedPreferences;
+import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
-import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@@ -31,12 +31,21 @@
class BrowserYesNoPreference extends DialogPreference {
private SharedPreferences mPrefs;
private Context mContext;
+ private String mNeutralBtnTxt;
+ private String mPositiveBtnTxt;
+ private String mNegativeBtnTxt;
+ private boolean mNeutralBtnClicked = false;
// This is the constructor called by the inflater
public BrowserYesNoPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
mContext = context;
+ final TypedArray a = mContext.obtainStyledAttributes(attrs,
+ R.styleable.BrowserYesNoPreference, 0, 0);
+ mNeutralBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_neutralButtonText);
+ mPositiveBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_positiveButtonText);
+ mNegativeBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_negativeButtonText);
}
@Override
@@ -60,11 +69,33 @@
}
@Override
+ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+ super.onPrepareDialogBuilder(builder);
+ if (mNeutralBtnTxt != null) {
+ builder.setNeutralButton(mNeutralBtnTxt, this);
+ }
+
+ if (mPositiveBtnTxt != null) {
+ builder.setPositiveButton(mPositiveBtnTxt, this);
+ }
+
+ if (mNegativeBtnTxt != null) {
+ builder.setNegativeButton(mNegativeBtnTxt, this);
+ }
+ }
+
+ @Override
protected void onClick() {
super.onClick();
}
@Override
+ public void onClick(DialogInterface dialog, int which) {
+ super.onClick(dialog, which);
+ mNeutralBtnClicked = DialogInterface.BUTTON_NEUTRAL == which;
+ }
+
+ @Override
protected View onCreateDialogView() {
if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
String dialogMessage = mContext.getString(R.string.pref_privacy_clear_selected_dlg);
@@ -115,11 +146,13 @@
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
+ Integer result = (positiveResult) ? 1 : 0;
- if (!positiveResult)
- return;
+ if (mNeutralBtnTxt != null && mNeutralBtnClicked) {
+ result = 2;
+ }
- if (callChangeListener(positiveResult)) {
+ if (callChangeListener(result)) {
setEnabled(false);
BrowserSettings settings = BrowserSettings.getInstance();
if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
@@ -147,6 +180,16 @@
setEnabled(true);
} else if (PreferenceKeys.PREF_RESET_DEFAULT_PREFERENCES.equals(
getKey())) {
+ if (mNeutralBtnClicked) {
+ settings.clearCache();
+ settings.clearDatabases();
+ settings.clearCookies();
+ settings.clearHistory();
+ settings.clearFormData();
+ settings.clearPasswords();
+ settings.clearLocationAccess();
+ }
+
settings.resetDefaultPreferences();
setEnabled(true);
}