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.os.Bundle;
24 import android.support.v14.preference.SwitchPreference;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceCategory;
27 import android.support.v7.preference.PreferenceGroup;
28 import android.support.v7.preference.PreferenceScreen;
29 import android.text.TextUtils;
30 import android.util.Log;
32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
33 import com.android.internal.widget.LockPatternUtils;
34 import com.android.settings.R;
35 import com.android.settings.widget.MasterCheckBoxPreference;
36 import com.android.settingslib.RestrictedSwitchPreference;
37 import com.android.settingslib.core.AbstractPreferenceController;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.List;
44 /** These settings are per app, so should not be returned in global search results. */
45 public class AppNotificationSettings extends NotificationSettingsBase {
46 private static final String TAG = "AppNotificationSettings";
47 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
49 private static String KEY_GENERAL_CATEGORY = "categories";
50 private static String KEY_ADVANCED_CATEGORY = "app_advanced";
51 private static String KEY_BADGE = "badge";
52 private static String KEY_APP_LINK = "app_link";
54 private List<NotificationChannelGroup> mChannelGroupList;
57 public int getMetricsCategory() {
58 return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
62 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64 final PreferenceScreen screen = getPreferenceScreen();
65 if (mShowLegacyChannelConfig && screen != null) {
66 // if showing legacy settings, pull advanced settings out of the advanced category
67 Preference badge = findPreference(KEY_BADGE);
68 Preference appLink = findPreference(KEY_APP_LINK);
69 removePreference(KEY_ADVANCED_CATEGORY);
71 screen.addPreference(badge);
74 if (appLink != null) {
75 screen.addPreference(appLink);
81 public void onResume() {
84 if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
85 Log.w(TAG, "Missing package or uid or packageinfo");
90 if (!mShowLegacyChannelConfig) {
91 // Load channel settings
92 new AsyncTask<Void, Void, Void>() {
94 protected Void doInBackground(Void... unused) {
95 mChannelGroupList = mBackend.getGroups(mPkg, mUid).getList();
96 Collections.sort(mChannelGroupList, mChannelGroupComparator);
101 protected void onPostExecute(Void unused) {
102 if (getHost() == null) {
110 for (NotificationPreferenceController controller : mControllers) {
111 controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin);
112 controller.displayPreference(getPreferenceScreen());
114 updatePreferenceStates();
118 protected String getLogTag() {
123 protected int getPreferenceScreenResId() {
124 return R.xml.app_notification_settings;
128 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
129 mControllers = new ArrayList<>();
130 mControllers.add(new HeaderPreferenceController(context, this));
131 mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend));
132 mControllers.add(new BadgePreferenceController(context, mBackend));
133 mControllers.add(new AllowSoundPreferenceController(
134 context, mImportanceListener, mBackend));
135 mControllers.add(new ImportancePreferenceController(
136 context, mImportanceListener, mBackend));
137 mControllers.add(new SoundPreferenceController(context, this,
138 mImportanceListener, mBackend));
139 mControllers.add(new LightsPreferenceController(context, mBackend));
140 mControllers.add(new VibrationPreferenceController(context, mBackend));
141 mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
143 mControllers.add(new DndPreferenceController(context, mBackend));
144 mControllers.add(new AppLinkPreferenceController(context));
145 mControllers.add(new DescriptionPreferenceController(context));
146 mControllers.add(new NotificationsOffPreferenceController(context));
147 mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
148 return new ArrayList<>(mControllers);
151 private void populateList() {
152 if (!mDynamicPreferences.isEmpty()) {
153 for (Preference p : mDynamicPreferences) {
154 getPreferenceScreen().removePreference(p);
156 mDynamicPreferences.clear();
158 if (mChannelGroupList.isEmpty()) {
159 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
160 groupCategory.setTitle(R.string.notification_channels);
161 groupCategory.setKey(KEY_GENERAL_CATEGORY);
162 getPreferenceScreen().addPreference(groupCategory);
163 mDynamicPreferences.add(groupCategory);
165 Preference empty = new Preference(getPrefContext());
166 empty.setTitle(R.string.no_channels);
167 empty.setEnabled(false);
168 groupCategory.addPreference(empty);
171 mImportanceListener.onImportanceChanged();
175 private void populateGroupList() {
176 for (NotificationChannelGroup group : mChannelGroupList) {
177 PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
178 groupCategory.setOrderingAsAdded(true);
179 getPreferenceScreen().addPreference(groupCategory);
180 mDynamicPreferences.add(groupCategory);
181 if (group.getId() == null) {
182 if (mChannelGroupList.size() > 1) {
183 groupCategory.setTitle(R.string.notification_channels_other);
185 groupCategory.setKey(KEY_GENERAL_CATEGORY);
187 groupCategory.setTitle(group.getName());
188 groupCategory.setKey(group.getId());
189 populateGroupToggle(groupCategory, group);
191 if (!group.isBlocked()) {
192 final List<NotificationChannel> channels = group.getChannels();
193 Collections.sort(channels, mChannelComparator);
194 int N = channels.size();
195 for (int i = 0; i < N; i++) {
196 final NotificationChannel channel = channels.get(i);
197 populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
203 protected void populateGroupToggle(final PreferenceGroup parent,
204 NotificationChannelGroup group) {
205 RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
206 preference.setTitle(R.string.notification_switch_label);
207 preference.setEnabled(mSuspendedAppsAdmin == null
208 && isChannelGroupBlockable(group));
209 preference.setChecked(!group.isBlocked());
210 preference.setOnPreferenceClickListener(preference1 -> {
211 final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
212 group.setBlocked(!allowGroup);
213 mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
215 onGroupBlockStateChanged(group);
219 parent.addPreference(preference);
222 private Comparator<NotificationChannelGroup> mChannelGroupComparator =
223 new Comparator<NotificationChannelGroup>() {
226 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
227 // Non-grouped channels (in placeholder group with a null id) come last
228 if (left.getId() == null && right.getId() != null) {
230 } else if (right.getId() == null && left.getId() != null) {
233 return left.getId().compareTo(right.getId());
237 protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
241 PreferenceGroup groupGroup = (
242 PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
244 if (groupGroup != null) {
245 if (group.isBlocked()) {
246 List<Preference> toRemove = new ArrayList<>();
247 int childCount = groupGroup.getPreferenceCount();
248 for (int i = 0; i < childCount; i++) {
249 Preference pref = groupGroup.getPreference(i);
250 if (pref instanceof MasterCheckBoxPreference) {
254 for (Preference pref : toRemove) {
255 groupGroup.removePreference(pref);
258 final List<NotificationChannel> channels = group.getChannels();
259 Collections.sort(channels, mChannelComparator);
260 int N = channels.size();
261 for (int i = 0; i < N; i++) {
262 final NotificationChannel channel = channels.get(i);
263 populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());