1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
package com.android.settings;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceScreen;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.CompoundButton;
import android.widget.Switch;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.vanir.AppMultiSelectListPreference;
import static android.hardware.Sensor.TYPE_PROXIMITY;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ActiveNotifications extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener,
DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
private static final String KEY_ENABLED = "ad_enable";
private static final String KEY_LOCKSCREEN_NOTIFICATIONS = "lock_enable";
private static final String KEY_POCKET_MODE = "pocket_mode";
private static final String KEY_HIDE_LOW_PRIORITY = "hide_low_priority";
private static final String KEY_HIDE_NON_CLEARABLE = "hide_non_clearable";
private static final String KEY_PRIVACY_MODE = "privacy_mode";
private static final String KEY_QUIET_HOURS = "quiet_hours";
private static final String KEY_ADDITIONAL = "additional_options";
private static final String KEY_EXCLUDED_APPS = "ad_excluded_apps";
private static final String KEY_EXCLUDED_NOTIF_APPS = "excluded_apps";
private Switch mEnabledSwitch;
private Preference mAdditional;
private boolean mActiveNotifications;
private boolean mDialogClicked;
private Dialog mEnableDialog;
private CheckBoxPreference mEnabledPref;
private CheckBoxPreference mLockNotif;
private ListPreference mPocketModePref;
private CheckBoxPreference mHideLowPriority;
private CheckBoxPreference mHideNonClearable;
private AppMultiSelectListPreference mExcludedAppsPref;
private AppMultiSelectListPreference mNotifAppsPref;
private CheckBoxPreference mPrivacyMode;
private CheckBoxPreference mQuietHours;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Activity activity = getActivity();
mEnabledSwitch = new Switch(activity);
final int padding = activity.getResources().getDimensionPixelSize(
R.dimen.action_bar_switch_padding);
mEnabledSwitch.setPaddingRelative(0, 0, padding, 0);
mEnabledSwitch.setOnCheckedChangeListener(this);
}
@Override
public void onStart() {
super.onStart();
final Activity activity = getActivity();
activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(mEnabledSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
mEnabledSwitch.setChecked(Settings.System.getInt(getContentResolver(),
Settings.System.ACTIVE_NOTIFICATIONS, 0) == 1);
}
@Override
public void onStop() {
super.onStop();
final Activity activity = getActivity();
activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.active_notifications);
PreferenceScreen prefs = getPreferenceScreen();
final ContentResolver cr = getActivity().getContentResolver();
mEnabledPref = (CheckBoxPreference) prefs.findPreference(KEY_ENABLED);
mEnabledPref.setChecked((Settings.System.getInt(cr,
Settings.System.ENABLE_ACTIVE_DISPLAY, 0) == 1));
mLockNotif = (CheckBoxPreference) prefs.findPreference(KEY_LOCKSCREEN_NOTIFICATIONS);
mLockNotif.setChecked((Settings.System.getInt(cr,
Settings.System.LOCKSCREEN_NOTIFICATIONS, 0) == 1));
mPocketModePref = (ListPreference) prefs.findPreference(KEY_POCKET_MODE);
mPocketModePref.setOnPreferenceChangeListener(this);
int mode = Settings.System.getInt(cr,
Settings.System.ACTIVE_NOTIFICATIONS_POCKET_MODE, 0);
mPocketModePref.setValue(String.valueOf(mode));
updatePocketModeSummary(mode);
if (!hasProximitySensor()) {
getPreferenceScreen().removePreference(mPocketModePref);
}
mHideLowPriority = (CheckBoxPreference) prefs.findPreference(KEY_HIDE_LOW_PRIORITY);
mHideLowPriority.setChecked(Settings.System.getInt(cr,
Settings.System.ACTIVE_NOTIFICATIONS_HIDE_LOW_PRIORITY, 0) == 1);
mHideNonClearable = (CheckBoxPreference) prefs.findPreference(KEY_HIDE_NON_CLEARABLE);
mHideNonClearable.setChecked(Settings.System.getInt(cr,
Settings.System.LOCKSCREEN_NOTIFICATIONS_HIDE_NON_CLEARABLE, 0) == 1);
mPrivacyMode = (CheckBoxPreference) prefs.findPreference(KEY_PRIVACY_MODE);
mPrivacyMode.setChecked(Settings.System.getInt(cr,
Settings.System.ACTIVE_NOTIFICATIONS_PRIVACY_MODE, 0) == 1);
mQuietHours = (CheckBoxPreference) prefs.findPreference(KEY_QUIET_HOURS);
mQuietHours.setChecked(Settings.System.getInt(cr,
Settings.System.ACTIVE_NOTIFICATIONS_QUIET_HOURS, 0) == 1);
mExcludedAppsPref = (AppMultiSelectListPreference) findPreference(KEY_EXCLUDED_APPS);
mExcludedAppsPref.setOnPreferenceChangeListener(this);
mNotifAppsPref = (AppMultiSelectListPreference) findPreference(KEY_EXCLUDED_NOTIF_APPS);
mNotifAppsPref.setOnPreferenceChangeListener(this);
AsyncTask.execute(new Runnable() {
public void run() {
// Active display
Set<String> excludedApps = getExcludedNotifApps(Settings.System.ACTIVE_DISPLAY_EXCLUDED_APPS);
if (excludedApps != null) mExcludedAppsPref.setValues(excludedApps);
// Lockscreen notifications
Set<String> excludedNotifApps = getExcludedNotifApps(Settings.System.LOCKSCREEN_NOTIFICATIONS_EXCLUDED_APPS);
if (excludedNotifApps != null) mNotifAppsPref.setValues(excludedNotifApps);
}
});
mAdditional = (PreferenceScreen) prefs.findPreference(KEY_ADDITIONAL);
updateDependency();
}
private boolean isKeyguardSecure() {
LockPatternUtils mLockPatternUtils = new LockPatternUtils(getActivity());
boolean isSecure = mLockPatternUtils.isSecure();
return isSecure;
}
private void updateDependency() {
mActiveNotifications = Settings.System.getInt(getContentResolver(),
Settings.System.ACTIVE_NOTIFICATIONS, 0) == 1;
mPocketModePref.setEnabled(mActiveNotifications);
mLockNotif.setEnabled(mActiveNotifications);
mEnabledPref.setEnabled(mActiveNotifications);
mHideLowPriority.setEnabled(mActiveNotifications);
mHideNonClearable.setEnabled(mActiveNotifications);
mQuietHours.setEnabled(mActiveNotifications);
mPrivacyMode.setEnabled(mActiveNotifications);
mAdditional.setEnabled(mActiveNotifications);
mNotifAppsPref.setEnabled(mActiveNotifications);
mExcludedAppsPref.setEnabled(mActiveNotifications);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
ContentResolver cr = getContentResolver();
if (preference == mEnabledPref) {
Settings.System.putInt(cr,
Settings.System.ENABLE_ACTIVE_DISPLAY, mEnabledPref.isChecked() ? 1 : 0);
} else if (preference == mLockNotif) {
Settings.System.putInt(getContentResolver(),
Settings.System.LOCKSCREEN_NOTIFICATIONS, mLockNotif.isChecked() ? 1 : 0);
} else if (preference == mHideLowPriority) {
Settings.System.putInt(cr, Settings.System.ACTIVE_NOTIFICATIONS_HIDE_LOW_PRIORITY,
mHideLowPriority.isChecked() ? 1 : 0);
} else if (preference == mHideNonClearable) {
Settings.System.putInt(cr, Settings.System.LOCKSCREEN_NOTIFICATIONS_HIDE_NON_CLEARABLE,
mHideNonClearable.isChecked() ? 1 : 0);
} else if (preference == mPrivacyMode) {
Settings.System.putInt(cr, Settings.System.ACTIVE_NOTIFICATIONS_PRIVACY_MODE,
mPrivacyMode.isChecked() ? 1 : 0);
} else if (preference == mQuietHours) {
Settings.System.putInt(cr, Settings.System.ACTIVE_NOTIFICATIONS_QUIET_HOURS,
mQuietHours.isChecked() ? 1 : 0);
} else {
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
return true;
}
@Override
public boolean onPreferenceChange(Preference pref, Object value) {
if (pref == mPocketModePref) {
int mode = Integer.valueOf((String) value);
updatePocketModeSummary(mode);
return true;
} else if (pref == mExcludedAppsPref) {
storeExcludedNotifApps((Set<String>) value, Settings.System.ACTIVE_DISPLAY_EXCLUDED_APPS);
return true;
} else if (pref == mNotifAppsPref) {
storeExcludedNotifApps((Set<String>) value, Settings.System.LOCKSCREEN_NOTIFICATIONS_EXCLUDED_APPS);
return true;
} else {
return false;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final boolean lesConditionnelles = isKeyguardSecure();
if (buttonView == mEnabledSwitch) {
if (isChecked && lesConditionnelles) {
if (isChecked) {
mDialogClicked = false;
if (mEnableDialog != null) {
dismissDialogs();
}
mEnableDialog = new AlertDialog.Builder(getActivity()).setMessage(
getActivity().getResources().getString(
R.string.lockscreen_notifications_dialog_message))
.setTitle(R.string.lockscreen_notifications_dialog_title)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setPositiveButton(android.R.string.yes, this)
.show();
mEnableDialog.setOnDismissListener(this);
}
}
boolean value = ((Boolean)isChecked).booleanValue();
Settings.System.putInt(getContentResolver(),
Settings.System.ACTIVE_NOTIFICATIONS,
value ? 1 : 0);
updateDependency();
}
}
private void dismissDialogs() {
if (mEnableDialog != null) {
mEnableDialog.dismiss();
mEnableDialog = null;
}
}
public void onClick(DialogInterface dialog, int which) {
if (dialog == mEnableDialog) {
if (which == DialogInterface.BUTTON_POSITIVE) {
mDialogClicked = true;
}
}
}
private void updatePocketModeSummary(int value) {
mPocketModePref.setSummary(
mPocketModePref.getEntries()[mPocketModePref.findIndexOfValue("" + value)]);
Settings.System.putInt(getContentResolver(),
Settings.System.ACTIVE_NOTIFICATIONS_POCKET_MODE, value);
}
private boolean hasProximitySensor() {
SensorManager sm = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
return sm.getDefaultSensor(TYPE_PROXIMITY) != null;
}
Set<String> getExcludedNotifApps(String setting) {
String excludedNotif = Settings.System.getString(getContentResolver(), setting);
if (TextUtils.isEmpty(excludedNotif)) return null;
return new HashSet<String>(Arrays.asList(excludedNotif.split("\\|")));
}
private void storeExcludedNotifApps(Set<String> values, String setting) {
StringBuilder Notifbuilder = new StringBuilder();
String delimiter = "";
for (String value : values) {
Notifbuilder.append(delimiter);
Notifbuilder.append(value);
delimiter = "|";
}
Settings.System.putString(getContentResolver(), setting, Notifbuilder.toString());
}
public void onDismiss(DialogInterface dialog) {
// ahh!
}
}
|