blob: eae9a8b873fea0d30f6cb6566867be5098ab3da0 [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 Garg52d36492015-09-07 15:09:40 +020053 setDialogIcon(R.drawable.ic_sp_level_warning);
Pankaj Gargb3b57462014-11-18 17:28:30 -080054 }
55
56 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070057 protected View onCreateView(ViewGroup group) {
58 View child = super.onCreateView(group);
59 View titleView = child.findViewById(android.R.id.title);
60 if (titleView instanceof Button) {
61 Button btn = (Button) titleView;
62 final BrowserYesNoPreference pref = this;
63 btn.setOnClickListener(
64 new View.OnClickListener() {
65 @Override
66 public void onClick(View v) {
67 pref.onClick();
68 }
69 }
70 );
71 }
72
73 return child;
74 }
75
76 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070077 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
78 super.onPrepareDialogBuilder(builder);
79 if (mNeutralBtnTxt != null) {
80 builder.setNeutralButton(mNeutralBtnTxt, this);
81 }
82
83 if (mPositiveBtnTxt != null) {
84 builder.setPositiveButton(mPositiveBtnTxt, this);
85 }
86
87 if (mNegativeBtnTxt != null) {
88 builder.setNegativeButton(mNegativeBtnTxt, this);
89 }
90 }
91
92 @Override
Pankaj Garg32e1b942015-06-03 18:13:24 -070093 protected void onClick() {
94 super.onClick();
95 }
96
97 @Override
Pankaj Garg8a9eba62015-07-28 09:37:48 -070098 public void onClick(DialogInterface dialog, int which) {
99 super.onClick(dialog, which);
100 mNeutralBtnClicked = DialogInterface.BUTTON_NEUTRAL == which;
101 }
102
103 @Override
Pankaj Gargb3b57462014-11-18 17:28:30 -0800104 protected View onCreateDialogView() {
105 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
106 String dialogMessage = mContext.getString(R.string.pref_privacy_clear_selected_dlg);
107 boolean itemSelected = false;
108
109 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
110 dialogMessage = dialogMessage.concat("\n\t" +
111 mContext.getString(R.string.pref_privacy_clear_cache));
112 itemSelected = true;
113 }
114 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
115 dialogMessage = dialogMessage.concat("\n\t" +
116 mContext.getString(R.string.pref_privacy_clear_cookies));
117 itemSelected = true;
118 }
119 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
120 dialogMessage = dialogMessage.concat("\n\t" +
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700121 mContext.getString(R.string.history));
Pankaj Gargb3b57462014-11-18 17:28:30 -0800122 itemSelected = true;
123 }
124 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
125 dialogMessage = dialogMessage.concat("\n\t" +
126 mContext.getString(R.string.pref_privacy_clear_form_data));
127 itemSelected = true;
128 }
129 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
130 dialogMessage = dialogMessage.concat("\n\t" +
131 mContext.getString(R.string.pref_privacy_clear_passwords));
132 itemSelected = true;
133 }
134 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
135 false)) {
136 dialogMessage = dialogMessage.concat("\n\t" +
137 mContext.getString(R.string.pref_privacy_clear_geolocation_access));
138 itemSelected = true;
139 }
140
141 if (!itemSelected) {
142 setDialogMessage(R.string.pref_select_items);
143 } else {
144 setDialogMessage(dialogMessage);
145 }
146 }
147
148 return super.onCreateDialogView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149 }
150
151 @Override
152 protected void onDialogClosed(boolean positiveResult) {
153 super.onDialogClosed(positiveResult);
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700154 Integer result = (positiveResult) ? 1 : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700156 if (mNeutralBtnTxt != null && mNeutralBtnClicked) {
157 result = 2;
158 }
Pankaj Garge2305022015-03-20 14:11:01 -0700159
Pankaj Garg8a9eba62015-07-28 09:37:48 -0700160 if (callChangeListener(result)) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700161 setEnabled(false);
Pankaj Gargb3b57462014-11-18 17:28:30 -0800162 if (PreferenceKeys.PREF_CLEAR_SELECTED_DATA.equals(getKey())) {
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700163 BrowserSettings settings = BrowserSettings.getInstance();
Pankaj Gargb3b57462014-11-18 17:28:30 -0800164 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_CACHE, false)) {
165 settings.clearCache();
166 settings.clearDatabases();
167 }
168 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_COOKIES, false)) {
169 settings.clearCookies();
170 }
171 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY, false)) {
172 settings.clearHistory();
173 }
174 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_FORM_DATA, false)) {
175 settings.clearFormData();
176 }
177 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_PASSWORDS, false)) {
178 settings.clearPasswords();
179 }
180 if (mPrefs.getBoolean(PreferenceKeys.PREF_PRIVACY_CLEAR_GEOLOCATION_ACCESS,
181 false)) {
182 settings.clearLocationAccess();
183 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 }
Pankaj Gargf320d6e2015-08-10 09:56:57 -0700185 setEnabled(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
187 }
188}