blob: 37e84319f1b1488873a958265382420918095cf5 [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
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080031class 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
39 // This is the constructor called by the inflater
40 public BrowserYesNoPreference(Context context, AttributeSet attrs) {
41 super(context, attrs);
Pankaj Gargb3b57462014-11-18 17:28:30 -080042 mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
43 mContext = context;
Pankaj Garg8a9eba62015-07-28 09:37:48 -070044 final TypedArray a = mContext.obtainStyledAttributes(attrs,
45 R.styleable.BrowserYesNoPreference, 0, 0);
46 mNeutralBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_neutralButtonText);
47 mPositiveBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_positiveButtonText);
48 mNegativeBtnTxt = a.getString(R.styleable.BrowserYesNoPreference_negativeButtonText);
Pankaj Gargb3b57462014-11-18 17:28:30 -080049 }
50
51 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070052 protected View onCreateView(ViewGroup group) {
53 View child = super.onCreateView(group);
54 View titleView = child.findViewById(android.R.id.title);
55 if (titleView instanceof Button) {
56 Button btn = (Button) titleView;
57 final BrowserYesNoPreference pref = this;
58 btn.setOnClickListener(
59 new View.OnClickListener() {
60 @Override
61 public void onClick(View v) {
62 pref.onClick();
63 }
64 }
65 );
66 }
67
68 return child;
69 }
70
71 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070072 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
73 super.onPrepareDialogBuilder(builder);
74 if (mNeutralBtnTxt != null) {
75 builder.setNeutralButton(mNeutralBtnTxt, this);
76 }
77
78 if (mPositiveBtnTxt != null) {
79 builder.setPositiveButton(mPositiveBtnTxt, this);
80 }
81
82 if (mNegativeBtnTxt != null) {
83 builder.setNegativeButton(mNegativeBtnTxt, this);
84 }
85 }
86
87 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070088 protected void onClick() {
89 super.onClick();
90 }
91
92 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070093 public void onClick(DialogInterface dialog, int which) {
94 super.onClick(dialog, which);
95 mNeutralBtnClicked = DialogInterface.BUTTON_NEUTRAL == which;
96 }
97
98 @Override
Pankaj Gargb3b57462014-11-18 17:28:30 -080099 protected View onCreateDialogView() {
100 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
101 String dialogMessage = mContext.getString(R.string.pref_privacy_clear_selected_dlg);
102 boolean itemSelected = false;
103
104 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
105 dialogMessage = dialogMessage.concat("\n\t" +
106 mContext.getString(R.string.pref_privacy_clear_cache));
107 itemSelected = true;
108 }
109 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
110 dialogMessage = dialogMessage.concat("\n\t" +
111 mContext.getString(R.string.pref_privacy_clear_cookies));
112 itemSelected = true;
113 }
114 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
115 dialogMessage = dialogMessage.concat("\n\t" +
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700116 mContext.getString(R.string.history));
Pankaj Gargb3b57462014-11-18 17:28:30 -0800117 itemSelected = true;
118 }
119 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
120 dialogMessage = dialogMessage.concat("\n\t" +
121 mContext.getString(R.string.pref_privacy_clear_form_data));
122 itemSelected = true;
123 }
124 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
125 dialogMessage = dialogMessage.concat("\n\t" +
126 mContext.getString(R.string.pref_privacy_clear_passwords));
127 itemSelected = true;
128 }
129 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
130 false)) {
131 dialogMessage = dialogMessage.concat("\n\t" +
132 mContext.getString(R.string.pref_privacy_clear_geolocation_access));
133 itemSelected = true;
134 }
135
136 if (!itemSelected) {
137 setDialogMessage(R.string.pref_select_items);
138 } else {
139 setDialogMessage(dialogMessage);
140 }
141 }
142
143 return super.onCreateDialogView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 }
145
146 @Override
147 protected void onDialogClosed(boolean positiveResult) {
148 super.onDialogClosed(positiveResult);
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700149 Integer result = (positiveResult) ? 1 : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700151 if (mNeutralBtnTxt != null && mNeutralBtnClicked) {
152 result = 2;
153 }
Pankaj Garge2305022015-03-20 14:11:01 -0700154
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700155 if (callChangeListener(result)) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700156 setEnabled(false);
John Reck35e9dd62011-04-25 09:01:54 -0700157 BrowserSettings settings = BrowserSettings.getInstance();
Pankaj Gargb3b57462014-11-18 17:28:30 -0800158 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
159 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
160 settings.clearCache();
161 settings.clearDatabases();
162 }
163 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
164 settings.clearCookies();
165 }
166 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
167 settings.clearHistory();
168 }
169 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
170 settings.clearFormData();
171 }
172 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
173 settings.clearPasswords();
174 }
175 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
176 false)) {
177 settings.clearLocationAccess();
178 }
179
180 setEnabled(true);
John Reck35e9dd62011-04-25 09:01:54 -0700181 } else if (PreferenceKeys.PREF_RESET_DEFAULT_PREFERENCES.equals(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 getKey())) {
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700183 if (mNeutralBtnClicked) {
184 settings.clearCache();
185 settings.clearDatabases();
186 settings.clearCookies();
187 settings.clearHistory();
188 settings.clearFormData();
189 settings.clearPasswords();
190 settings.clearLocationAccess();
191 }
192
John Reck35e9dd62011-04-25 09:01:54 -0700193 settings.resetDefaultPreferences();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 setEnabled(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195 }
196 }
197 }
198}