1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.settings.notification; 18 19 import android.app.NotificationChannel; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.util.Log; 23 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settingslib.core.AbstractPreferenceController; 28 29 import java.util.ArrayList; 30 import java.util.Collections; 31 import java.util.List; 32 33 public class ChannelGroupNotificationSettings extends NotificationSettingsBase { 34 private static final String TAG = "ChannelGroupSettings"; 35 36 @Override getMetricsCategory()37 public int getMetricsCategory() { 38 return SettingsEnums.NOTIFICATION_CHANNEL_GROUP; 39 } 40 41 @Override onResume()42 public void onResume() { 43 super.onResume(); 44 if (mAppRow == null || mChannelGroup == null) { 45 Log.w(TAG, "Missing package or uid or packageinfo or group"); 46 finish(); 47 return; 48 } 49 50 populateChannelList(); 51 for (NotificationPreferenceController controller : mControllers) { 52 controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin); 53 controller.displayPreference(getPreferenceScreen()); 54 } 55 updatePreferenceStates(); 56 } 57 58 @Override getLogTag()59 protected String getLogTag() { 60 return TAG; 61 } 62 63 @Override getPreferenceScreenResId()64 protected int getPreferenceScreenResId() { 65 return R.xml.notification_group_settings; 66 } 67 68 @Override createPreferenceControllers(Context context)69 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 70 mControllers = new ArrayList<>(); 71 mControllers.add(new HeaderPreferenceController(context, this)); 72 mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend)); 73 mControllers.add(new AppLinkPreferenceController(context)); 74 mControllers.add(new NotificationsOffPreferenceController(context)); 75 mControllers.add(new DescriptionPreferenceController(context)); 76 return new ArrayList<>(mControllers); 77 } 78 populateChannelList()79 private void populateChannelList() { 80 if (!mDynamicPreferences.isEmpty()) { 81 // If there's anything in mDynamicPreferences, we've called populateChannelList twice. 82 // Clear out existing channels and log. 83 Log.w(TAG, "Notification channel group posted twice to settings - old size " + 84 mDynamicPreferences.size() + ", new size " + mDynamicPreferences.size()); 85 for (Preference p : mDynamicPreferences) { 86 getPreferenceScreen().removePreference(p); 87 } 88 } 89 if (mChannelGroup.getChannels().isEmpty()) { 90 Preference empty = new Preference(getPrefContext()); 91 empty.setTitle(R.string.no_channels); 92 empty.setEnabled(false); 93 getPreferenceScreen().addPreference(empty); 94 mDynamicPreferences.add(empty); 95 96 } else { 97 final List<NotificationChannel> channels = mChannelGroup.getChannels(); 98 Collections.sort(channels, mChannelComparator); 99 for (NotificationChannel channel : channels) { 100 mDynamicPreferences.add(populateSingleChannelPrefs( 101 getPreferenceScreen(), channel, mChannelGroup.isBlocked())); 102 } 103 104 } 105 } 106 } 107