The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 18 | |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 19 | import android.app.AlertDialog; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 21 | import android.content.DialogInterface; |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 24 | import android.preference.DialogPreference; |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 25 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 27 | import android.view.View; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
| 29 | import android.widget.Button; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 31 | class BrowserYesNoPreference extends DialogPreference { |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 32 | private SharedPreferences mPrefs; |
| 33 | private Context mContext; |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 34 | private String mNeutralBtnTxt; |
| 35 | private String mPositiveBtnTxt; |
| 36 | private String mNegativeBtnTxt; |
| 37 | private boolean mNeutralBtnClicked = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | |
| 39 | // This is the constructor called by the inflater |
| 40 | public BrowserYesNoPreference(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 42 | mPrefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 43 | mContext = context; |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 44 | 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 Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 52 | 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 Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 72 | 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 Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 88 | protected void onClick() { |
| 89 | super.onClick(); |
| 90 | } |
| 91 | |
| 92 | @Override |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 93 | public void onClick(DialogInterface dialog, int which) { |
| 94 | super.onClick(dialog, which); |
| 95 | mNeutralBtnClicked = DialogInterface.BUTTON_NEUTRAL == which; |
| 96 | } |
| 97 | |
| 98 | @Override |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 99 | 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 Dhawan | 53b2ba7 | 2015-08-05 15:58:20 -0700 | [diff] [blame] | 116 | mContext.getString(R.string.history)); |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 117 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void onDialogClosed(boolean positiveResult) { |
| 148 | super.onDialogClosed(positiveResult); |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 149 | Integer result = (positiveResult) ? 1 : 0; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 150 | |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 151 | if (mNeutralBtnTxt != null && mNeutralBtnClicked) { |
| 152 | result = 2; |
| 153 | } |
Pankaj Garg | e230502 | 2015-03-20 14:11:01 -0700 | [diff] [blame] | 154 | |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 155 | if (callChangeListener(result)) { |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 156 | setEnabled(false); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 157 | BrowserSettings settings = BrowserSettings.getInstance(); |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 158 | 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 Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 181 | } else if (PreferenceKeys.PREF_RESET_DEFAULT_PREFERENCES.equals( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 182 | getKey())) { |
Pankaj Garg | 8a9eba6 | 2015-07-28 09:37:48 -0700 | [diff] [blame] | 183 | 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 Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 193 | settings.resetDefaultPreferences(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 194 | setEnabled(true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |