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 static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
21 import android.app.Notification;
22 import android.app.NotificationManager;
23 import android.content.Intent;
24 import android.content.pm.ActivityInfo;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.ResolveInfo;
27 import android.content.pm.UserInfo;
28 import android.os.Bundle;
29 import android.os.UserHandle;
30 import android.service.notification.NotificationListenerService.Ranking;
31 import android.util.ArrayMap;
32 import android.util.Log;
33 import android.view.Window;
34 import android.view.WindowManager;
35 import com.android.internal.logging.MetricsProto.MetricsEvent;
36 import com.android.internal.widget.LockPatternUtils;
37 import com.android.settings.AppHeader;
38 import com.android.settings.R;
39 import com.android.settings.notification.NotificationBackend.AppRow;
40 import com.android.settingslib.RestrictedSwitchPreference;
41 import java.util.List;
43 /** These settings are per app, so should not be returned in global search results. */
44 public class AppNotificationSettings extends NotificationSettingsBase {
45 private static final String TAG = "AppNotificationSettings";
46 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
48 private static final Intent APP_NOTIFICATION_PREFS_CATEGORY_INTENT
49 = new Intent(Intent.ACTION_MAIN)
50 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES);
52 private AppRow mAppRow;
53 private boolean mDndVisualEffectsSuppressed;
56 public void onActivityCreated(Bundle savedInstanceState) {
57 super.onActivityCreated(savedInstanceState);
58 if (mAppRow == null) return;
59 AppHeader.createAppHeader(this, mAppRow.icon, mAppRow.label, mAppRow.pkg, mAppRow.uid,
60 mAppRow.settingsIntent);
64 protected int getMetricsCategory() {
65 return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
69 public void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
72 addPreferencesFromResource(R.xml.app_notification_settings);
73 mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE);
75 (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BYPASS_DND);
77 (RestrictedDropDownPreference) getPreferenceScreen().findPreference(
78 KEY_VISIBILITY_OVERRIDE);
79 mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK);
80 mSilent = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SILENT);
82 if (mPkgInfo != null) {
83 mAppRow = mBackend.loadAppRow(mContext, mPm, mPkgInfo);
85 NotificationManager.Policy policy =
86 NotificationManager.from(mContext).getNotificationPolicy();
87 mDndVisualEffectsSuppressed =
88 policy == null ? false : policy.suppressedVisualEffects != 0;
90 // load settings intent
91 ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
92 rows.put(mAppRow.pkg, mAppRow);
93 collectConfigActivities(rows);
95 setupImportancePrefs(mAppRow.cantBlock, mAppRow.cantSilence,
96 mAppRow.appImportance, mAppRow.banned);
97 setupPriorityPref(mAppRow.appBypassDnd);
98 setupVisOverridePref(mAppRow.appVisOverride);
99 updateDependents(mAppRow.appImportance);
104 public void onResume() {
106 getActivity().getWindow().addPrivateFlags(PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
107 android.util.EventLog.writeEvent(0x534e4554, "119115683", -1, "");
110 public void onPause() {
112 final Window window = getActivity().getWindow();
113 final WindowManager.LayoutParams attrs = window.getAttributes();
114 attrs.privateFlags &= ~PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
115 window.setAttributes(attrs);
119 protected void updateDependents(int importance) {
120 LockPatternUtils utils = new LockPatternUtils(getActivity());
121 boolean lockscreenSecure = utils.isSecure(UserHandle.myUserId());
122 UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
123 if (parentUser != null) {
124 lockscreenSecure |= utils.isSecure(parentUser.id);
127 if (getPreferenceScreen().findPreference(mBlock.getKey()) != null) {
128 setVisible(mSilent, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance));
129 mSilent.setChecked(importance == Ranking.IMPORTANCE_LOW);
131 setVisible(mPriority, checkCanBeVisible(Ranking.IMPORTANCE_DEFAULT, importance)
132 || (checkCanBeVisible(Ranking.IMPORTANCE_LOW, importance)
133 && mDndVisualEffectsSuppressed));
134 setVisible(mVisibilityOverride,
135 checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance) && lockscreenSecure);
138 protected boolean checkCanBeVisible(int minImportanceVisible, int importance) {
139 if (importance == Ranking.IMPORTANCE_UNSPECIFIED) {
142 return importance >= minImportanceVisible;
145 private List<ResolveInfo> queryNotificationConfigActivities() {
146 if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is "
147 + APP_NOTIFICATION_PREFS_CATEGORY_INTENT);
148 final List<ResolveInfo> resolveInfos = mPm.queryIntentActivities(
149 APP_NOTIFICATION_PREFS_CATEGORY_INTENT,
150 0 //PackageManager.MATCH_DEFAULT_ONLY
155 private void collectConfigActivities(ArrayMap<String, AppRow> rows) {
156 final List<ResolveInfo> resolveInfos = queryNotificationConfigActivities();
157 applyConfigActivities(rows, resolveInfos);
160 private void applyConfigActivities(ArrayMap<String, AppRow> rows,
161 List<ResolveInfo> resolveInfos) {
162 if (DEBUG) Log.d(TAG, "Found " + resolveInfos.size() + " preference activities"
163 + (resolveInfos.size() == 0 ? " ;_;" : ""));
164 for (ResolveInfo ri : resolveInfos) {
165 final ActivityInfo activityInfo = ri.activityInfo;
166 final ApplicationInfo appInfo = activityInfo.applicationInfo;
167 final AppRow row = rows.get(appInfo.packageName);
169 if (DEBUG) Log.v(TAG, "Ignoring notification preference activity ("
170 + activityInfo.name + ") for unknown package "
171 + activityInfo.packageName);
174 if (row.settingsIntent != null) {
175 if (DEBUG) Log.v(TAG, "Ignoring duplicate notification preference activity ("
176 + activityInfo.name + ") for package "
177 + activityInfo.packageName);
180 row.settingsIntent = new Intent(APP_NOTIFICATION_PREFS_CATEGORY_INTENT)
181 .setClassName(activityInfo.packageName, activityInfo.name);