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