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.NotificationChannel;
20 import android.app.NotificationChannelGroup;
21 import android.content.Context;
22 import android.os.AsyncTask;
23 import android.support.v14.preference.SwitchPreference;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceCategory;
26 import android.support.v7.preference.PreferenceGroup;
27 import android.text.TextUtils;
28 import android.util.Log;
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settings.R;
33 import com.android.settings.widget.MasterCheckBoxPreference;
34 import com.android.settingslib.RestrictedSwitchPreference;
35 import com.android.settingslib.core.AbstractPreferenceController;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Comparator;
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 String KEY_GENERAL_CATEGORY = "categories";
49 private List<NotificationChannelGroup> mChannelGroupList;
52 public int getMetricsCategory() {
53 return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
57 public void onResume() {
60 if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
61 Log.w(TAG, "Missing package or uid or packageinfo");
66 if (getPreferenceScreen() != null) {
67 getPreferenceScreen().removeAll();
68 mDynamicPreferences.clear();
71 if (mShowLegacyChannelConfig) {
72 addPreferencesFromResource(R.xml.channel_notification_settings);
74 addPreferencesFromResource(R.xml.app_notification_settings);
75 // Load channel settings
76 new AsyncTask<Void, Void, Void>() {
78 protected Void doInBackground(Void... unused) {
79 mChannelGroupList = mBackend.getGroups(mPkg, mUid).getList();
80 Collections.sort(mChannelGroupList, mChannelGroupComparator);
85 protected void onPostExecute(Void unused) {
86 if (getHost() == null) {
93 getPreferenceScreen().setOrderingAsAdded(true);
95 for (NotificationPreferenceController controller : mControllers) {
96 controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin);
97 controller.displayPreference(getPreferenceScreen());
99 updatePreferenceStates();
103 protected String getLogTag() {
108 protected int getPreferenceScreenResId() {
109 return R.xml.notification_settings;
113 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
114 mControllers = new ArrayList<>();
115 mControllers.add(new HeaderPreferenceController(context, this));
116 mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend));
117 mControllers.add(new BadgePreferenceController(context, mBackend));
118 mControllers.add(new AllowSoundPreferenceController(
119 context, mImportanceListener, mBackend));
120 mControllers.add(new ImportancePreferenceController(
121 context, mImportanceListener, mBackend));
122 mControllers.add(new SoundPreferenceController(context, this,
123 mImportanceListener, mBackend));
124 mControllers.add(new LightsPreferenceController(context, mBackend));
125 mControllers.add(new VibrationPreferenceController(context, mBackend));
126 mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
128 mControllers.add(new DndPreferenceController(context, getLifecycle(), mBackend));
129 mControllers.add(new AppLinkPreferenceController(context));
130 mControllers.add(new DescriptionPreferenceController(context));
131 mControllers.add(new NotificationsOffPreferenceController(context));
132 mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
133 return new ArrayList<>(mControllers);
136 private void populateList() {
137 if (!mDynamicPreferences.isEmpty()) {
138 // If there's anything in mChannelGroups, we've called populateChannelList twice.
139 // Clear out existing channels and log.
140 Log.w(TAG, "Notification channel group posted twice to settings - old size " +
141 mDynamicPreferences.size() + ", new size " + mChannelGroupList.size());
142 for (Preference p : mDynamicPreferences) {
143 getPreferenceScreen().removePreference(p);
146 if (mChannelGroupList.isEmpty()) {
147 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
148 groupCategory.setTitle(R.string.notification_channels);
149 groupCategory.setKey(KEY_GENERAL_CATEGORY);
150 getPreferenceScreen().addPreference(groupCategory);
151 mDynamicPreferences.add(groupCategory);
153 Preference empty = new Preference(getPrefContext());
154 empty.setTitle(R.string.no_channels);
155 empty.setEnabled(false);
156 groupCategory.addPreference(empty);
159 mImportanceListener.onImportanceChanged();
163 private void populateGroupList() {
164 for (NotificationChannelGroup group : mChannelGroupList) {
165 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
166 groupCategory.setOrderingAsAdded(true);
167 getPreferenceScreen().addPreference(groupCategory);
168 mDynamicPreferences.add(groupCategory);
169 if (group.getId() == null) {
170 if (mChannelGroupList.size() > 1) {
171 groupCategory.setTitle(R.string.notification_channels_other);
173 groupCategory.setKey(KEY_GENERAL_CATEGORY);
175 groupCategory.setTitle(group.getName());
176 groupCategory.setKey(group.getId());
177 populateGroupToggle(groupCategory, group);
179 if (!group.isBlocked()) {
180 final List<NotificationChannel> channels = group.getChannels();
181 Collections.sort(channels, mChannelComparator);
182 int N = channels.size();
183 for (int i = 0; i < N; i++) {
184 final NotificationChannel channel = channels.get(i);
185 populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
191 protected void populateGroupToggle(final PreferenceGroup parent,
192 NotificationChannelGroup group) {
193 RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
194 preference.setTitle(R.string.notification_switch_label);
195 preference.setEnabled(mSuspendedAppsAdmin == null
196 && isChannelGroupBlockable(group));
197 preference.setChecked(!group.isBlocked());
198 preference.setOnPreferenceClickListener(preference1 -> {
199 final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
200 group.setBlocked(!allowGroup);
201 mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
203 onGroupBlockStateChanged(group);
207 parent.addPreference(preference);
210 private Comparator<NotificationChannelGroup> mChannelGroupComparator =
211 new Comparator<NotificationChannelGroup>() {
214 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
215 // Non-grouped channels (in placeholder group with a null id) come last
216 if (left.getId() == null && right.getId() != null) {
218 } else if (right.getId() == null && left.getId() != null) {
221 return left.getId().compareTo(right.getId());
225 protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
229 PreferenceGroup groupGroup = (
230 PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
232 if (groupGroup != null) {
233 if (group.isBlocked()) {
234 List<Preference> toRemove = new ArrayList<>();
235 int childCount = groupGroup.getPreferenceCount();
236 for (int i = 0; i < childCount; i++) {
237 Preference pref = groupGroup.getPreference(i);
238 if (pref instanceof MasterCheckBoxPreference) {
242 for (Preference pref : toRemove) {
243 groupGroup.removePreference(pref);
246 final List<NotificationChannel> channels = group.getChannels();
247 Collections.sort(channels, mChannelComparator);
248 int N = channels.size();
249 for (int i = 0; i < N; i++) {
250 final NotificationChannel channel = channels.get(i);
251 populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());