blob: d73ea08b169c23d1dc63636ad25796ac5226ba3e [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
Pankaj Garg8a9eba62015-07-28 09:37:48 -070019import android.app.AlertDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.content.Context;
Pankaj Garg8a9eba62015-07-28 09:37:48 -070021import android.content.DialogInterface;
Pankaj Gargb3b57462014-11-18 17:28:30 -080022import android.content.SharedPreferences;
Pankaj Garg8a9eba62015-07-28 09:37:48 -070023import android.content.res.TypedArray;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080024import android.preference.DialogPreference;
Pankaj Gargb3b57462014-11-18 17:28:30 -080025import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.util.AttributeSet;
Pankaj Gargb3b57462014-11-18 17:28:30 -080027import android.view.View;
Pankaj Garg32e1b942015-06-03 18:13:24 -070028import android.view.ViewGroup;
29import android.widget.Button;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030
Pankaj Gargf320d6e2015-08-10 09:56:57 -070031public class BrowserYesNoPreference extends DialogPreference {
Pankaj Gargb3b57462014-11-18 17:28:30 -080032 private SharedPreferences mPrefs;
33 private Context mContext;
Pankaj Garg8a9eba62015-07-28 09:37:48 -070034 private String mNeutralBtnTxt;
35 private String mPositiveBtnTxt;
36 private String mNegativeBtnTxt;
37 private boolean mNeutralBtnClicked = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038
Pankaj Gargf320d6e2015-08-10 09:56:57 -070039 public static final int CANCEL_BTN = 0;
40 public static final int OK_BTN = 1;
41 public static final int OTHER_BTN = 2;
42
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 // This is the constructor called by the inflater
44 public BrowserYesNoPreference(Context context, AttributeSet attrs) {
45 super(context, attrs);
Pankaj Gargb3b57462014-11-18 17:28:30 -080046 mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
47 mContext = context;
Pankaj Garg8a9eba62015-07-28 09:37:48 -070048 final TypedArray a = mContext.obtainStyledAttributes(attrs,
49 R.styleable.BrowserYesNoPreference, 0, 0);
50 mNeutralBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_neutralButtonText);
51 mPositiveBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_positiveButtonText);
52 mNegativeBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_negativeButtonText);
Pankaj Gargb3b57462014-11-18 17:28:30 -080053 }
54
55 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070056 protected View onCreateView(ViewGroup group) {
57 View child = super.onCreateView(group);
58 View titleView = child.findViewById(android.R.id.title);
59 if (titleView instanceof Button) {
60 Button btn = (Button) titleView;
61 final BrowserYesNoPreference pref = this;
62 btn.setOnClickListener(
63 new View.OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 pref.onClick();
67 }
68 }
69 );
70 }
71
72 return child;
73 }
74
75 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070076 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
77 super.onPrepareDialogBuilder(builder);
78 if (mNeutralBtnTxt != null) {
79 builder.setNeutralButton(mNeutralBtnTxt, this);
80 }
81
82 if (mPositiveBtnTxt != null) {
83 builder.setPositiveButton(mPositiveBtnTxt, this);
84 }
85
86 if (mNegativeBtnTxt != null) {
87 builder.setNegativeButton(mNegativeBtnTxt, this);
88 }
89 }
90
91 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070092 protected void onClick() {
93 super.onClick();
94 }
95
96 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070097 public void onClick(DialogInterface dialog, int which) {
98 super.onClick(dialog, which);
99 mNeutralBtnClicked = DialogInterface.BUTTON_NEUTRAL == which;
100 }
101
102 @Override
Pankaj Gargb3b57462014-11-18 17:28:30 -0800103 protected View onCreateDialogView() {
104 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
105 String dialogMessage = mContext.getString(R.string.pref_privacy_clear_selected_dlg);
106 boolean itemSelected = false;
107
108 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
109 dialogMessage = dialogMessage.concat("\n\t" +
110 mContext.getString(R.string.pref_privacy_clear_cache));
111 itemSelected = true;
112 }
113 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
114 dialogMessage = dialogMessage.concat("\n\t" +
115 mContext.getString(R.string.pref_privacy_clear_cookies));
116 itemSelected = true;
117 }
118 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
119 dialogMessage = dialogMessage.concat("\n\t" +
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700120 mContext.getString(R.string.history));
Pankaj Gargb3b57462014-11-18 17:28:30 -0800121 itemSelected = true;
122 }
123 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
124 dialogMessage = dialogMessage.concat("\n\t" +
125 mContext.getString(R.string.pref_privacy_clear_form_data));
126 itemSelected = true;
127 }
128 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
129 dialogMessage = dialogMessage.concat("\n\t" +
130 mContext.getString(R.string.pref_privacy_clear_passwords));
131 itemSelected = true;
132 }
133 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
134 false)) {
135 dialogMessage = dialogMessage.concat("\n\t" +
136 mContext.getString(R.string.pref_privacy_clear_geolocation_access));
137 itemSelected = true;
138 }
139
140 if (!itemSelected) {
141 setDialogMessage(R.string.pref_select_items);
142 } else {
143 setDialogMessage(dialogMessage);
144 }
145 }
146
147 return super.onCreateDialogView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 }
149
150 @Override
151 protected void onDialogClosed(boolean positiveResult) {
152 super.onDialogClosed(positiveResult);
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700153 Integer result = (positiveResult) ? 1 : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700155 if (mNeutralBtnTxt != null && mNeutralBtnClicked) {
156 result = 2;
157 }
Pankaj Garge2305022015-03-20 14:11:01 -0700158
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700159 if (callChangeListener(result)) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700160 setEnabled(false);
Pankaj Gargb3b57462014-11-18 17:28:30 -0800161 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700162 BrowserSettings settings = BrowserSettings.getInstance();
Pankaj Gargb3b57462014-11-18 17:28:30 -0800163 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
164 settings.clearCache();
165 settings.clearDatabases();
166 }
167 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
168 settings.clearCookies();
169 }
170 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
171 settings.clearHistory();
172 }
173 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
174 settings.clearFormData();
175 }
176 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
177 settings.clearPasswords();
178 }
179 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
180 false)) {
181 settings.clearLocationAccess();
182 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700184 setEnabled(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 }
186 }
187}