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.zen; 18 19 import android.app.AutomaticZenRule; 20 import android.content.Context; 21 22 import androidx.annotation.VisibleForTesting; 23 import androidx.fragment.app.Fragment; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceCategory; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settingslib.core.lifecycle.Lifecycle; 29 30 import java.util.Map; 31 import java.util.Objects; 32 33 public class ZenModeAutomaticRulesPreferenceController extends 34 AbstractZenModeAutomaticRulePreferenceController { 35 36 protected static final String KEY = "zen_mode_automatic_rules"; 37 38 @VisibleForTesting 39 protected PreferenceCategory mPreferenceCategory; 40 ZenModeAutomaticRulesPreferenceController(Context context, Fragment parent, Lifecycle lifecycle)41 public ZenModeAutomaticRulesPreferenceController(Context context, Fragment parent, Lifecycle 42 lifecycle) { 43 super(context, KEY, parent, lifecycle); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return KEY; 49 } 50 51 @Override isAvailable()52 public boolean isAvailable() { 53 return true; 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 mPreferenceCategory = screen.findPreference(getPreferenceKey()); 60 mPreferenceCategory.setPersistent(false); 61 } 62 63 @Override updateState(Preference preference)64 public void updateState(Preference preference) { 65 super.updateState(preference); 66 Map.Entry<String, AutomaticZenRule>[] sortedRules = getRules(); 67 final int currNumPreferences = mPreferenceCategory.getPreferenceCount(); 68 if (currNumPreferences == sortedRules.length) { 69 for (int i = 0; i < sortedRules.length; i++) { 70 ZenRulePreference pref = (ZenRulePreference) mPreferenceCategory.getPreference(i); 71 // we are either: 72 // 1. updating everything about the rule 73 // 2. rule was added or deleted, so reload the entire list 74 if (Objects.equals(pref.mId, sortedRules[i].getKey())) { 75 AutomaticZenRule rule = sortedRules[i].getValue(); 76 pref.updatePreference(rule); 77 } else { 78 reloadAllRules(sortedRules); 79 break; 80 } 81 } 82 } else { 83 reloadAllRules(sortedRules); 84 } 85 } 86 87 @VisibleForTesting reloadAllRules(Map.Entry<String, AutomaticZenRule>[] rules)88 void reloadAllRules(Map.Entry<String, AutomaticZenRule>[] rules) { 89 mPreferenceCategory.removeAll(); 90 for (Map.Entry<String, AutomaticZenRule> rule : rules) { 91 ZenRulePreference pref = createZenRulePreference(rule); 92 mPreferenceCategory.addPreference(pref); 93 } 94 } 95 96 @VisibleForTesting createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule)97 ZenRulePreference createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule) { 98 return new ZenRulePreference(mPreferenceCategory.getContext(), 99 rule, mParent, mMetricsFeatureProvider); 100 } 101 } 102