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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.UserHandle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.AppHeader;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.applications.AppInfoWithHeader;
import com.android.settings.notification.NotificationBackend.AppRow;
import java.util.List;
/** These settings are per app, so should not be returned in global search results. */
public class AppNotificationSettings extends SettingsPreferenceFragment {
private static final String TAG = "AppNotificationSettings";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private static final String KEY_BLOCK = "block";
private static final String KEY_PRIORITY = "priority";
private static final String KEY_PEEKABLE = "peekable";
private static final String KEY_SENSITIVE = "sensitive";
private static final String KEY_APP_SETTINGS = "app_settings";
private static final String KEY_SHOW_ON_KEYGUARD = "show_on_keyguard";
private static final String KEY_NO_ONGOING_ON_KEYGUARD = "no_ongoing_on_keyguard";
private static final String KEY_SOUND_TIMEOUT = "sound_timeout";
private static final Intent APP_NOTIFICATION_PREFS_CATEGORY_INTENT
= new Intent(Intent.ACTION_MAIN)
.addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES);
private final NotificationBackend mBackend = new NotificationBackend();
private Context mContext;
private SwitchPreference mBlock;
private SwitchPreference mPriority;
private SwitchPreference mPeekable;
private SwitchPreference mSensitive;
private SwitchPreference mShowOnKeyguard;
private SwitchPreference mShowNoOngoingOnKeyguard;
private ListPreference mSoundTimeout;
private AppRow mAppRow;
private boolean mCreated;
private boolean mIsSystemPackage;
private int mUid;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (DEBUG) Log.d(TAG, "onActivityCreated mCreated=" + mCreated);
if (mCreated) {
Log.w(TAG, "onActivityCreated: ignoring duplicate call");
return;
}
mCreated = true;
if (mAppRow == null) return;
AppHeader.createAppHeader(this, mAppRow.icon, mAppRow.label,
AppInfoWithHeader.getInfoIntent(this, mAppRow.pkg));
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.NOTIFICATION_APP_NOTIFICATION;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
Intent intent = getActivity().getIntent();
Bundle args = getArguments();
if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
if (intent == null && args == null) {
Log.w(TAG, "No intent");
toastAndFinish();
return;
}
final String pkg = args != null && args.containsKey(AppInfoBase.ARG_PACKAGE_NAME)
? args.getString(AppInfoBase.ARG_PACKAGE_NAME)
: intent.getStringExtra(Settings.EXTRA_APP_PACKAGE);
mUid = args != null && args.containsKey(AppInfoBase.ARG_PACKAGE_UID)
? args.getInt(AppInfoBase.ARG_PACKAGE_UID)
: intent.getIntExtra(Settings.EXTRA_APP_UID, -1);
if (mUid == -1 || TextUtils.isEmpty(pkg)) {
Log.w(TAG, "Missing extras: " + Settings.EXTRA_APP_PACKAGE + " was " + pkg + ", "
+ Settings.EXTRA_APP_UID + " was " + mUid);
toastAndFinish();
return;
}
if (DEBUG) Log.d(TAG, "Load details for pkg=" + pkg + " uid=" + mUid);
final PackageManager pm = getPackageManager();
final PackageInfo info = findPackageInfo(pm, pkg, mUid);
if (info == null) {
Log.w(TAG, "Failed to find package info: " + Settings.EXTRA_APP_PACKAGE + " was " + pkg
+ ", " + Settings.EXTRA_APP_UID + " was " + mUid);
toastAndFinish();
return;
}
mIsSystemPackage = Utils.isSystemPackage(pm, info);
addPreferencesFromResource(R.xml.app_notification_settings);
mBlock = (SwitchPreference) findPreference(KEY_BLOCK);
mPriority = (SwitchPreference) findPreference(KEY_PRIORITY);
mPeekable = (SwitchPreference) findPreference(KEY_PEEKABLE);
mSensitive = (SwitchPreference) findPreference(KEY_SENSITIVE);
mShowOnKeyguard = (SwitchPreference) findPreference(KEY_SHOW_ON_KEYGUARD);
mShowNoOngoingOnKeyguard = (SwitchPreference) findPreference(KEY_NO_ONGOING_ON_KEYGUARD);
mSoundTimeout = (ListPreference) findPreference(KEY_SOUND_TIMEOUT);
mAppRow = mBackend.loadAppRow(pm, info.applicationInfo);
// load settings intent
ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
rows.put(mAppRow.pkg, mAppRow);
collectConfigActivities(getPackageManager(), rows);
mBlock.setChecked(mAppRow.banned);
updateDependents(mAppRow.banned);
mPriority.setChecked(mAppRow.priority);
mPeekable.setChecked(mAppRow.peekable);
mSensitive.setChecked(mAppRow.sensitive);
mSoundTimeout.setValue(Long.toString(mAppRow.soundTimeout));
updateSoundTimeoutSummary(mAppRow.soundTimeout);
mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean banned = (Boolean) newValue;
if (banned) {
MetricsLogger.action(getActivity(), MetricsLogger.ACTION_BAN_APP_NOTES, pkg);
}
final boolean success = mBackend.setNotificationsBanned(pkg, mUid, banned);
if (success) {
updateDependents(banned);
}
return success;
}
});
mPriority.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean priority = (Boolean) newValue;
return mBackend.setHighPriority(pkg, mUid, priority);
}
});
mPeekable.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean peekable = (Boolean) newValue;
return mBackend.setPeekable(pkg, mUid, peekable);
}
});
mSensitive.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean sensitive = (Boolean) newValue;
return mBackend.setSensitive(pkg, mUid, sensitive);
}
});
mSoundTimeout.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
long value = Long.valueOf((String) newValue);
if (!mBackend.setNotificationSoundTimeout(pkg, mUid, value)) {
return false;
}
updateSoundTimeoutSummary(value);
return true;
}
});
if (mAppRow.settingsIntent != null) {
findPreference(KEY_APP_SETTINGS).setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
mContext.startActivity(mAppRow.settingsIntent);
return true;
}
});
} else {
removePreference(KEY_APP_SETTINGS);
}
int keyguard = mBackend.getShowNotificationForPackageOnKeyguard(pkg, mUid);
mShowOnKeyguard.setChecked((keyguard & Notification.SHOW_ALL_NOTI_ON_KEYGUARD) != 0);
mShowNoOngoingOnKeyguard.setChecked(
(keyguard & Notification.SHOW_NO_ONGOING_NOTI_ON_KEYGUARD) != 0);
mShowOnKeyguard.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean showOnKeyguard = (Boolean) newValue;
int keyguard = mBackend.getShowNotificationForPackageOnKeyguard(pkg, mUid);
if (showOnKeyguard && (keyguard & Notification.SHOW_ALL_NOTI_ON_KEYGUARD) == 0) {
keyguard |= Notification.SHOW_ALL_NOTI_ON_KEYGUARD;
} else {
keyguard &= ~Notification.SHOW_ALL_NOTI_ON_KEYGUARD;
}
return mBackend.setShowNotificationForPackageOnKeyguard(pkg, mUid, keyguard);
}
});
mShowNoOngoingOnKeyguard.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean showNoOngoingOnKeyguard = (Boolean) newValue;
int keyguard = mBackend.getShowNotificationForPackageOnKeyguard(pkg, mUid);
if (showNoOngoingOnKeyguard
&& (keyguard & Notification.SHOW_NO_ONGOING_NOTI_ON_KEYGUARD) == 0) {
keyguard |= Notification.SHOW_NO_ONGOING_NOTI_ON_KEYGUARD;
} else {
keyguard &= ~Notification.SHOW_NO_ONGOING_NOTI_ON_KEYGUARD;
}
return mBackend.setShowNotificationForPackageOnKeyguard(pkg, mUid, keyguard);
}
});
// Users cannot block notifications from system/signature packages
if (mIsSystemPackage || !getLockscreenNotificationsEnabled()) {
getPreferenceScreen().removePreference(mShowNoOngoingOnKeyguard);
getPreferenceScreen().removePreference(mShowOnKeyguard);
}
}
@Override
public void onResume() {
super.onResume();
if (mUid != -1 && getPackageManager().getPackagesForUid(mUid) == null) {
// App isn't around anymore, must have been removed.
finish();
}
}
private void updateSoundTimeoutSummary(long value) {
if (value == 0) {
mSoundTimeout.setSummary(R.string.app_notification_sound_timeout_value_none);
} else {
final CharSequence[] entries = mSoundTimeout.getEntries();
final CharSequence[] values = mSoundTimeout.getEntryValues();
CharSequence summary = null;
for (int i = 0; i < values.length; i++) {
long timeout = Long.parseLong(values[i].toString());
if (timeout == value) {
summary = getString(R.string.app_notification_sound_timeout_summary_template,
entries[i]);
break;
}
}
mSoundTimeout.setSummary(summary);
}
}
private void updateDependents(boolean banned) {
final boolean lockscreenSecure = new LockPatternUtils(getActivity()).isSecure(
UserHandle.myUserId());
final boolean lockscreenNotificationsEnabled = getLockscreenNotificationsEnabled();
final boolean allowPrivate = getLockscreenAllowPrivateNotifications();
final boolean headsUpEnabled = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.HEADS_UP_NOTIFICATIONS_ENABLED, 0) != 0;
setVisible(mBlock, !mIsSystemPackage);
setVisible(mPriority, mIsSystemPackage || !banned);
setVisible(mPeekable, mIsSystemPackage || !banned && headsUpEnabled);
setVisible(mSensitive, mIsSystemPackage || !banned && lockscreenSecure
&& lockscreenNotificationsEnabled && allowPrivate);
setVisible(mShowOnKeyguard, mIsSystemPackage || !banned);
setVisible(mShowNoOngoingOnKeyguard, mIsSystemPackage || !banned);
}
private void setVisible(Preference p, boolean visible) {
final boolean isVisible = getPreferenceScreen().findPreference(p.getKey()) != null;
if (isVisible == visible) return;
if (visible) {
getPreferenceScreen().addPreference(p);
} else {
getPreferenceScreen().removePreference(p);
}
}
private boolean getLockscreenNotificationsEnabled() {
return Settings.Secure.getInt(getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0;
}
private boolean getLockscreenAllowPrivateNotifications() {
return Settings.Secure.getInt(getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0;
}
private void toastAndFinish() {
Toast.makeText(mContext, R.string.app_not_found_dlg_text, Toast.LENGTH_SHORT).show();
getActivity().finish();
}
private static PackageInfo findPackageInfo(PackageManager pm, String pkg, int uid) {
final String[] packages = pm.getPackagesForUid(uid);
if (packages != null && pkg != null) {
final int N = packages.length;
for (int i = 0; i < N; i++) {
final String p = packages[i];
if (pkg.equals(p)) {
try {
return pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
} catch (NameNotFoundException e) {
Log.w(TAG, "Failed to load package " + pkg, e);
}
}
}
}
return null;
}
public static List<ResolveInfo> queryNotificationConfigActivities(PackageManager pm) {
if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is "
+ APP_NOTIFICATION_PREFS_CATEGORY_INTENT);
final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
APP_NOTIFICATION_PREFS_CATEGORY_INTENT,
0 //PackageManager.MATCH_DEFAULT_ONLY
);
return resolveInfos;
}
public static void collectConfigActivities(PackageManager pm, ArrayMap<String, AppRow> rows) {
final List<ResolveInfo> resolveInfos = queryNotificationConfigActivities(pm);
applyConfigActivities(pm, rows, resolveInfos);
}
public static void applyConfigActivities(PackageManager pm, ArrayMap<String, AppRow> rows,
List<ResolveInfo> resolveInfos) {
if (DEBUG) Log.d(TAG, "Found " + resolveInfos.size() + " preference activities"
+ (resolveInfos.size() == 0 ? " ;_;" : ""));
for (ResolveInfo ri : resolveInfos) {
final ActivityInfo activityInfo = ri.activityInfo;
final ApplicationInfo appInfo = activityInfo.applicationInfo;
final AppRow row = rows.get(appInfo.packageName);
if (row == null) {
if (DEBUG) Log.v(TAG, "Ignoring notification preference activity ("
+ activityInfo.name + ") for unknown package "
+ activityInfo.packageName);
continue;
}
if (row.settingsIntent != null) {
if (DEBUG) Log.v(TAG, "Ignoring duplicate notification preference activity ("
+ activityInfo.name + ") for package "
+ activityInfo.packageName);
continue;
}
row.settingsIntent = new Intent(APP_NOTIFICATION_PREFS_CATEGORY_INTENT)
.setClassName(activityInfo.packageName, activityInfo.name);
}
}
}
|