2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.content.Intent;
22 import android.content.pm.ActivityInfo;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.ResolveInfo;
25 import android.content.pm.UserInfo;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.service.notification.NotificationListenerService.Ranking;
29 import android.util.ArrayMap;
30 import android.util.Log;
32 import com.android.internal.logging.MetricsProto.MetricsEvent;
33 import com.android.internal.widget.LockPatternUtils;
34 import com.android.settings.AppHeader;
35 import com.android.settings.R;
36 import com.android.settings.notification.NotificationBackend.AppRow;
37 import com.android.settingslib.RestrictedSwitchPreference;
40 import java.util.List;
42 /** These settings are per app, so should not be returned in global search results. */
43 public class AppNotificationSettings extends NotificationSettingsBase {
44 private static final String TAG = "AppNotificationSettings";
45 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
47 private static final Intent APP_NOTIFICATION_PREFS_CATEGORY_INTENT
48 = new Intent(Intent.ACTION_MAIN)
49 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES);
51 private AppRow mAppRow;
52 private boolean mDndVisualEffectsSuppressed;
55 public void onActivityCreated(Bundle savedInstanceState) {
56 super.onActivityCreated(savedInstanceState);
57 if (mAppRow == null) return;
58 AppHeader.createAppHeader(this, mAppRow.icon, mAppRow.label, mAppRow.pkg, mAppRow.uid,
59 mAppRow.settingsIntent);
63 protected int getMetricsCategory() {
64 return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
68 public void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
71 addPreferencesFromResource(R.xml.app_notification_settings);
72 mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE);
74 (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BYPASS_DND);
76 (RestrictedDropDownPreference) getPreferenceScreen().findPreference(
77 KEY_VISIBILITY_OVERRIDE);
78 mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK);
79 mSilent = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SILENT);
81 if (mPkgInfo != null) {
82 mAppRow = mBackend.loadAppRow(mContext, mPm, mPkgInfo);
84 NotificationManager.Policy policy =
85 NotificationManager.from(mContext).getNotificationPolicy();
86 mDndVisualEffectsSuppressed = policy == null ? false : policy.suppressedVisualEffects != 0;
88 // load settings intent
89 ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
90 rows.put(mAppRow.pkg, mAppRow);
91 collectConfigActivities(rows);
93 setupImportancePrefs(mAppRow.systemApp, mAppRow.appImportance, mAppRow.banned);
94 setupPriorityPref(mAppRow.appBypassDnd);
95 setupVisOverridePref(mAppRow.appVisOverride);
96 updateDependents(mAppRow.appImportance);
101 protected void updateDependents(int importance) {
102 LockPatternUtils utils = new LockPatternUtils(getActivity());
103 boolean lockscreenSecure = utils.isSecure(UserHandle.myUserId());
104 UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
105 if (parentUser != null){
106 lockscreenSecure |= utils.isSecure(parentUser.id);
109 if (getPreferenceScreen().findPreference(mBlock.getKey()) != null) {
110 setVisible(mSilent, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance));
111 mSilent.setChecked(importance == Ranking.IMPORTANCE_LOW);
113 setVisible(mPriority, checkCanBeVisible(Ranking.IMPORTANCE_DEFAULT, importance)
114 || (checkCanBeVisible(Ranking.IMPORTANCE_LOW, importance)
115 && mDndVisualEffectsSuppressed));
116 setVisible(mVisibilityOverride,
117 checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance) && lockscreenSecure);
120 protected boolean checkCanBeVisible(int minImportanceVisible, int importance) {
121 if (importance == Ranking.IMPORTANCE_UNSPECIFIED) {
124 return importance >= minImportanceVisible;
127 private List<ResolveInfo> queryNotificationConfigActivities() {
128 if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is "
129 + APP_NOTIFICATION_PREFS_CATEGORY_INTENT);
130 final List<ResolveInfo> resolveInfos = mPm.queryIntentActivities(
131 APP_NOTIFICATION_PREFS_CATEGORY_INTENT,
132 0 //PackageManager.MATCH_DEFAULT_ONLY
137 private void collectConfigActivities(ArrayMap<String, AppRow> rows) {
138 final List<ResolveInfo> resolveInfos = queryNotificationConfigActivities();
139 applyConfigActivities(rows, resolveInfos);
142 private void applyConfigActivities(ArrayMap<String, AppRow> rows,
143 List<ResolveInfo> resolveInfos) {
144 if (DEBUG) Log.d(TAG, "Found " + resolveInfos.size() + " preference activities"
145 + (resolveInfos.size() == 0 ? " ;_;" : ""));
146 for (ResolveInfo ri : resolveInfos) {
147 final ActivityInfo activityInfo = ri.activityInfo;
148 final ApplicationInfo appInfo = activityInfo.applicationInfo;
149 final AppRow row = rows.get(appInfo.packageName);
151 if (DEBUG) Log.v(TAG, "Ignoring notification preference activity ("
152 + activityInfo.name + ") for unknown package "
153 + activityInfo.packageName);
156 if (row.settingsIntent != null) {
157 if (DEBUG) Log.v(TAG, "Ignoring duplicate notification preference activity ("
158 + activityInfo.name + ") for package "
159 + activityInfo.packageName);
162 row.settingsIntent = new Intent(APP_NOTIFICATION_PREFS_CATEGORY_INTENT)
163 .setClassName(activityInfo.packageName, activityInfo.name);