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.app.NotificationManager.IMPORTANCE_LOW;
20 import static android.app.NotificationManager.IMPORTANCE_NONE;
21 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
23 import android.app.Activity;
24 import android.app.NotificationChannel;
25 import android.app.NotificationChannelGroup;
26 import android.app.NotificationManager;
27 import android.content.Intent;
28 import android.os.AsyncTask;
29 import android.os.Bundle;
30 import android.provider.Settings;
31 import android.support.v7.preference.Preference;
32 import android.support.v7.preference.PreferenceCategory;
33 import android.text.TextUtils;
34 import android.util.ArrayMap;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.widget.Switch;
40 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
41 import com.android.settings.AppHeader;
42 import com.android.settings.R;
43 import com.android.settings.Utils;
44 import com.android.settings.applications.AppHeaderController;
45 import com.android.settings.applications.AppInfoBase;
46 import com.android.settings.applications.LayoutPreference;
47 import com.android.settings.notification.NotificationBackend.AppRow;
48 import com.android.settings.overlay.FeatureFactory;
49 import com.android.settings.widget.FooterPreference;
50 import com.android.settings.widget.MasterSwitchPreference;
51 import com.android.settings.widget.SwitchBar;
52 import com.android.settingslib.RestrictedSwitchPreference;
54 import java.text.Collator;
55 import java.util.ArrayList;
56 import java.util.Collections;
57 import java.util.Comparator;
58 import java.util.List;
60 /** These settings are per app, so should not be returned in global search results. */
61 public class AppNotificationSettings extends NotificationSettingsBase {
62 private static final String TAG = "AppNotificationSettings";
63 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
65 private static String KEY_GENERAL_CATEGORY = "categories";
66 private static String KEY_DELETED = "deleted";
68 private List<NotificationChannelGroup> mChannelGroupList;
69 private List<PreferenceCategory> mChannelGroups = new ArrayList();
70 private FooterPreference mDeletedChannels;
73 public int getMetricsCategory() {
74 return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
78 public void onResume() {
81 if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
82 Log.w(TAG, "Missing package or uid or packageinfo");
87 if (getPreferenceScreen() != null) {
88 getPreferenceScreen().removeAll();
89 mChannelGroups.clear();
90 mDeletedChannels = null;
91 mShowLegacyChannelConfig = false;
94 addPreferencesFromResource(R.xml.notification_settings);
95 getPreferenceScreen().setOrderingAsAdded(true);
100 mShowLegacyChannelConfig = mBackend.onlyHasDefaultChannel(mAppRow.pkg, mAppRow.uid);
101 if (mShowLegacyChannelConfig) {
102 mChannel = mBackend.getChannel(
103 mAppRow.pkg, mAppRow.uid, NotificationChannel.DEFAULT_CHANNEL_ID);
104 populateDefaultChannelPrefs();
106 addPreferencesFromResource(R.xml.upgraded_app_notification_settings);
108 // Load channel settings
109 new AsyncTask<Void, Void, Void>() {
111 protected Void doInBackground(Void... unused) {
112 mChannelGroupList = mBackend.getChannelGroups(mPkg, mUid).getList();
113 Collections.sort(mChannelGroupList, mChannelGroupComparator);
118 protected void onPostExecute(Void unused) {
119 if (getHost() == null) {
122 populateChannelList();
127 updateDependents(mAppRow.banned);
130 private void addHeaderPref() {
131 ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
132 rows.put(mAppRow.pkg, mAppRow);
133 collectConfigActivities(rows);
134 final Activity activity = getActivity();
135 final Preference pref = FeatureFactory.getFactory(activity)
136 .getApplicationFeatureProvider(activity)
137 .newAppHeaderController(this /* fragment */, null /* appHeader */)
138 .setIcon(mAppRow.icon)
139 .setLabel(mAppRow.label)
140 .setPackageName(mAppRow.pkg)
142 .setButtonActions(AppHeaderController.ActionType.ACTION_APP_INFO,
143 AppHeaderController.ActionType.ACTION_NOTIF_PREFERENCE)
144 .done(activity, getPrefContext());
145 pref.setKey(KEY_HEADER);
146 getPreferenceScreen().addPreference(pref);
149 private void populateChannelList() {
150 if (mChannelGroupList.isEmpty()) {
151 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
152 groupCategory.setTitle(R.string.notification_channels);
153 groupCategory.setKey(KEY_GENERAL_CATEGORY);
154 getPreferenceScreen().addPreference(groupCategory);
155 mChannelGroups.add(groupCategory);
157 Preference empty = new Preference(getPrefContext());
158 empty.setTitle(R.string.no_channels);
159 empty.setEnabled(false);
160 groupCategory.addPreference(empty);
162 for (NotificationChannelGroup group : mChannelGroupList) {
163 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
164 if (group.getId() == null) {
165 groupCategory.setTitle(mChannelGroupList.size() > 1
166 ? R.string.notification_channels_other
167 : R.string.notification_channels);
168 groupCategory.setKey(KEY_GENERAL_CATEGORY);
170 groupCategory.setTitle(group.getName());
171 groupCategory.setKey(group.getId());
173 groupCategory.setOrderingAsAdded(true);
174 getPreferenceScreen().addPreference(groupCategory);
175 mChannelGroups.add(groupCategory);
177 final List<NotificationChannel> channels = group.getChannels();
178 Collections.sort(channels, mChannelComparator);
179 int N = channels.size();
180 for (int i = 0; i < N; i++) {
181 final NotificationChannel channel = channels.get(i);
182 populateSingleChannelPrefs(groupCategory, channel);
186 int deletedChannelCount = mBackend.getDeletedChannelCount(mAppRow.pkg, mAppRow.uid);
187 if (deletedChannelCount > 0) {
188 mDeletedChannels = new FooterPreference(getPrefContext());
189 mDeletedChannels.setSelectable(false);
190 mDeletedChannels.setTitle(getResources().getQuantityString(
191 R.plurals.deleted_channels, deletedChannelCount, deletedChannelCount));
192 mDeletedChannels.setEnabled(false);
193 mDeletedChannels.setKey(KEY_DELETED);
194 mDeletedChannels.setOrder(ORDER_LAST);
195 getPreferenceScreen().addPreference(mDeletedChannels);
199 updateDependents(mAppRow.banned);
202 private void populateSingleChannelPrefs(PreferenceCategory groupCategory,
203 final NotificationChannel channel) {
204 MasterSwitchPreference channelPref = new MasterSwitchPreference(
206 channelPref.setSwitchEnabled(mSuspendedAppsAdmin == null
207 && isChannelBlockable(mAppRow.systemApp, channel));
208 channelPref.setKey(channel.getId());
209 channelPref.setTitle(channel.getName());
210 channelPref.setChecked(channel.getImportance() != IMPORTANCE_NONE);
211 channelPref.setSummary(getImportanceSummary(channel));
212 Bundle channelArgs = new Bundle();
213 channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid);
214 channelArgs.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true);
215 channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg);
216 channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId());
217 Intent channelIntent = Utils.onBuildStartFragmentIntent(getActivity(),
218 ChannelNotificationSettings.class.getName(),
219 channelArgs, null, R.string.notification_channel_title, null, false,
220 getMetricsCategory());
221 channelPref.setIntent(channelIntent);
223 channelPref.setOnPreferenceChangeListener(
224 new Preference.OnPreferenceChangeListener() {
226 public boolean onPreferenceChange(Preference preference,
228 boolean value = (Boolean) o;
229 int importance = value ? IMPORTANCE_LOW : IMPORTANCE_NONE;
230 channel.setImportance(importance);
232 NotificationChannel.USER_LOCKED_IMPORTANCE);
233 channelPref.setSummary(getImportanceSummary(channel));
234 mBackend.updateChannel(mPkg, mUid, channel);
239 groupCategory.addPreference(channelPref);
243 mBadge = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BADGE);
244 mBadge.setDisabledByAdmin(mSuspendedAppsAdmin);
245 if (mChannel == null) {
246 mBadge.setChecked(mAppRow.showBadge);
248 mBadge.setChecked(mChannel.canShowBadge());
250 mBadge.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
252 public boolean onPreferenceChange(Preference preference, Object newValue) {
253 final boolean value = (Boolean) newValue;
254 if (mChannel == null) {
255 mBackend.setShowBadge(mPkg, mUid, value);
257 mChannel.setShowBadge(value);
258 mChannel.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
259 mBackend.updateChannel(mPkg, mUid, mChannel);
266 protected void setupBlock() {
267 View switchBarContainer = LayoutInflater.from(
268 getPrefContext()).inflate(R.layout.styled_switch_bar, null);
269 mSwitchBar = switchBarContainer.findViewById(R.id.switch_bar);
271 mSwitchBar.setDisabledByAdmin(mSuspendedAppsAdmin);
272 mSwitchBar.setChecked(!mAppRow.banned);
273 mSwitchBar.addOnSwitchChangeListener(new SwitchBar.OnSwitchChangeListener() {
275 public void onSwitchChanged(Switch switchView, boolean isChecked) {
276 if (mShowLegacyChannelConfig && mChannel != null) {
277 final int importance = isChecked ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE;
278 mImportanceToggle.setChecked(importance == IMPORTANCE_UNSPECIFIED);
279 mChannel.setImportance(importance);
280 mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
281 mBackend.updateChannel(mPkg, mUid, mChannel);
283 mBackend.setNotificationsEnabledForPackage(mPkgInfo.packageName, mUid, isChecked);
284 mAppRow.banned = true;
285 updateDependents(!isChecked);
289 mBlockBar = new LayoutPreference(getPrefContext(), switchBarContainer);
290 mBlockBar.setOrder(ORDER_FIRST);
291 mBlockBar.setKey(KEY_BLOCK);
292 getPreferenceScreen().addPreference(mBlockBar);
294 if (mAppRow.systemApp && !mAppRow.banned) {
295 setVisible(mBlockBar, false);
298 setupBlockDesc(R.string.app_notifications_off_desc);
301 protected void updateDependents(boolean banned) {
302 for (PreferenceCategory category : mChannelGroups) {
303 setVisible(category, !banned);
305 if (mDeletedChannels != null) {
306 setVisible(mDeletedChannels, !banned);
308 setVisible(mBlockedDesc, banned);
309 setVisible(mBadge, !banned);
310 if (mShowLegacyChannelConfig) {
311 setVisible(mImportanceToggle, !banned);
312 setVisible(mPriority, checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT)
313 || (checkCanBeVisible(NotificationManager.IMPORTANCE_LOW)
314 && mDndVisualEffectsSuppressed));
315 setVisible(mVisibilityOverride, !banned &&
316 checkCanBeVisible(NotificationManager.IMPORTANCE_LOW) && isLockScreenSecure());
318 if (mAppLink != null) {
319 setVisible(mAppLink, !banned);
321 if (mAppRow.systemApp && !mAppRow.banned) {
322 setVisible(mBlockBar, false);
326 private String getImportanceSummary(NotificationChannel channel) {
327 switch (channel.getImportance()) {
328 case NotificationManager.IMPORTANCE_UNSPECIFIED:
329 return getContext().getString(R.string.notification_importance_unspecified);
330 case NotificationManager.IMPORTANCE_NONE:
331 return getContext().getString(R.string.notification_toggle_off);
332 case NotificationManager.IMPORTANCE_MIN:
333 return getContext().getString(R.string.notification_importance_min_title);
334 case NotificationManager.IMPORTANCE_LOW:
335 return getContext().getString(R.string.notification_importance_low_title);
336 case NotificationManager.IMPORTANCE_DEFAULT:
337 return getContext().getString(R.string.notification_importance_default_title);
338 case NotificationManager.IMPORTANCE_HIGH:
339 case NotificationManager.IMPORTANCE_MAX:
341 return getContext().getString(R.string.notification_importance_high_title);
346 private Comparator<NotificationChannel> mChannelComparator =
347 new Comparator<NotificationChannel>() {
350 public int compare(NotificationChannel left, NotificationChannel right) {
351 if (left.isDeleted() != right.isDeleted()) {
352 return Boolean.compare(left.isDeleted(), right.isDeleted());
354 return left.getId().compareTo(right.getId());
358 private Comparator<NotificationChannelGroup> mChannelGroupComparator =
359 new Comparator<NotificationChannelGroup>() {
362 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
363 // Non-grouped channels (in placeholder group with a null id) come last
364 if (left.getId() == null && right.getId() != null) {
366 } else if (right.getId() == null && left.getId() != null) {
369 return left.getId().compareTo(right.getId());