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 // Load channel settings
73 new AsyncTask<Void, Void, Void>() {
75 protected Void doInBackground(Void... unused) {
76 mChannelGroupList = mBackend.getGroups(mPkg, mUid).getList();
77 Collections.sort(mChannelGroupList, mChannelGroupComparator);
82 protected void onPostExecute(Void unused) {
83 if (getHost() == null) {
90 getPreferenceScreen().setOrderingAsAdded(true);
92 for (NotificationPreferenceController controller : mControllers) {
93 controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin);
94 controller.displayPreference(getPreferenceScreen());
96 updatePreferenceStates();
100 protected String getLogTag() {
105 protected int getPreferenceScreenResId() {
106 return R.xml.app_notification_settings;
110 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
111 mControllers = new ArrayList<>();
112 mControllers.add(new HeaderPreferenceController(context, this));
113 mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend));
114 mControllers.add(new BadgePreferenceController(context, mBackend));
115 mControllers.add(new AllowSoundPreferenceController(
116 context, mImportanceListener, mBackend));
117 mControllers.add(new ImportancePreferenceController(
118 context, mImportanceListener, mBackend));
119 mControllers.add(new SoundPreferenceController(context, this,
120 mImportanceListener, mBackend));
121 mControllers.add(new LightsPreferenceController(context, mBackend));
122 mControllers.add(new VibrationPreferenceController(context, mBackend));
123 mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
125 mControllers.add(new DndPreferenceController(context, mBackend));
126 mControllers.add(new AppLinkPreferenceController(context));
127 mControllers.add(new DescriptionPreferenceController(context));
128 mControllers.add(new NotificationsOffPreferenceController(context));
129 mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
130 return new ArrayList<>(mControllers);
133 private void populateList() {
134 if (!mDynamicPreferences.isEmpty()) {
135 // If there's anything in mChannelGroups, we've called populateChannelList twice.
136 // Clear out existing channels and log.
137 Log.w(TAG, "Notification channel group posted twice to settings - old size " +
138 mDynamicPreferences.size() + ", new size " + mChannelGroupList.size());
139 for (Preference p : mDynamicPreferences) {
140 getPreferenceScreen().removePreference(p);
143 if (mChannelGroupList.isEmpty()) {
144 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
145 groupCategory.setTitle(R.string.notification_channels);
146 groupCategory.setKey(KEY_GENERAL_CATEGORY);
147 getPreferenceScreen().addPreference(groupCategory);
148 mDynamicPreferences.add(groupCategory);
150 Preference empty = new Preference(getPrefContext());
151 empty.setTitle(R.string.no_channels);
152 empty.setEnabled(false);
153 groupCategory.addPreference(empty);
156 mImportanceListener.onImportanceChanged();
160 private void populateGroupList() {
161 for (NotificationChannelGroup group : mChannelGroupList) {
162 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
163 groupCategory.setOrderingAsAdded(true);
164 getPreferenceScreen().addPreference(groupCategory);
165 mDynamicPreferences.add(groupCategory);
166 if (group.getId() == null) {
167 if (mChannelGroupList.size() > 1) {
168 groupCategory.setTitle(R.string.notification_channels_other);
170 groupCategory.setKey(KEY_GENERAL_CATEGORY);
172 groupCategory.setTitle(group.getName());
173 groupCategory.setKey(group.getId());
174 populateGroupToggle(groupCategory, group);
176 if (!group.isBlocked()) {
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, group.isBlocked());
188 protected void populateGroupToggle(final PreferenceGroup parent,
189 NotificationChannelGroup group) {
190 RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
191 preference.setTitle(R.string.notification_switch_label);
192 preference.setEnabled(mSuspendedAppsAdmin == null
193 && isChannelGroupBlockable(group));
194 preference.setChecked(!group.isBlocked());
195 preference.setOnPreferenceClickListener(preference1 -> {
196 final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
197 group.setBlocked(!allowGroup);
198 mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
200 onGroupBlockStateChanged(group);
204 parent.addPreference(preference);
207 private Comparator<NotificationChannelGroup> mChannelGroupComparator =
208 new Comparator<NotificationChannelGroup>() {
211 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
212 // Non-grouped channels (in placeholder group with a null id) come last
213 if (left.getId() == null && right.getId() != null) {
215 } else if (right.getId() == null && left.getId() != null) {
218 return left.getId().compareTo(right.getId());
222 protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
226 PreferenceGroup groupGroup = (
227 PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
229 if (groupGroup != null) {
230 if (group.isBlocked()) {
231 List<Preference> toRemove = new ArrayList<>();
232 int childCount = groupGroup.getPreferenceCount();
233 for (int i = 0; i < childCount; i++) {
234 Preference pref = groupGroup.getPreference(i);
235 if (pref instanceof MasterCheckBoxPreference) {
239 for (Preference pref : toRemove) {
240 groupGroup.removePreference(pref);
243 final List<NotificationChannel> channels = group.getChannels();
244 Collections.sort(channels, mChannelComparator);
245 int N = channels.size();
246 for (int i = 0; i < N; i++) {
247 final NotificationChannel channel = channels.get(i);
248 populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());