1 /* 2 * Copyright (C) 2024 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.settingslib.notification.modes; 18 19 import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY; 20 import static android.service.notification.ZenModeConfig.ORIGIN_UNKNOWN; 21 import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI; 22 23 import android.app.AutomaticZenRule; 24 import android.app.NotificationManager; 25 import android.content.ComponentName; 26 import android.net.Uri; 27 import android.service.notification.Condition; 28 import android.service.notification.SystemZenRules; 29 import android.service.notification.ZenDeviceEffects; 30 import android.service.notification.ZenModeConfig; 31 import android.service.notification.ZenPolicy; 32 33 import androidx.annotation.DrawableRes; 34 import androidx.annotation.Nullable; 35 36 import java.util.concurrent.atomic.AtomicInteger; 37 38 public class TestModeBuilder { 39 40 private static final AtomicInteger sNextId = new AtomicInteger(0); 41 42 private String mId; 43 private AutomaticZenRule mRule; 44 private ZenModeConfig.ZenRule mConfigZenRule; 45 private boolean mIsManualDnd; 46 47 public static final ZenMode EXAMPLE = new TestModeBuilder().build(); 48 public static final ZenMode MANUAL_DND = new TestModeBuilder().makeManualDnd().build(); 49 TestModeBuilder()50 public TestModeBuilder() { 51 // Reasonable defaults 52 int id = sNextId.incrementAndGet(); 53 mId = "rule_" + id; 54 mRule = new AutomaticZenRule.Builder("Test Rule #" + id, Uri.parse("rule://" + id)) 55 .setPackage("some_package") 56 .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) 57 .setZenPolicy(new ZenPolicy.Builder().disallowAllSounds().build()) 58 .build(); 59 mConfigZenRule = new ZenModeConfig.ZenRule(); 60 mConfigZenRule.enabled = true; 61 mConfigZenRule.pkg = "some_package"; 62 } 63 TestModeBuilder(ZenMode previous)64 public TestModeBuilder(ZenMode previous) { 65 mId = previous.getId(); 66 mRule = new AutomaticZenRule.Builder(previous.getRule()).build(); 67 68 mConfigZenRule = new ZenModeConfig.ZenRule(); 69 mConfigZenRule.enabled = previous.getRule().isEnabled(); 70 mConfigZenRule.pkg = previous.getRule().getPackageName(); 71 setActive(previous.isActive()); 72 73 if (previous.isManualDnd()) { 74 makeManualDnd(); 75 } 76 } 77 setId(String id)78 public TestModeBuilder setId(String id) { 79 mId = id; 80 return this; 81 } 82 setAzr(AutomaticZenRule rule)83 public TestModeBuilder setAzr(AutomaticZenRule rule) { 84 mRule = rule; 85 mConfigZenRule.pkg = rule.getPackageName(); 86 mConfigZenRule.conditionId = rule.getConditionId(); 87 mConfigZenRule.enabled = rule.isEnabled(); 88 return this; 89 } 90 setConfigZenRule(ZenModeConfig.ZenRule configZenRule)91 public TestModeBuilder setConfigZenRule(ZenModeConfig.ZenRule configZenRule) { 92 mConfigZenRule = configZenRule; 93 return this; 94 } 95 setName(String name)96 public TestModeBuilder setName(String name) { 97 mRule.setName(name); 98 mConfigZenRule.name = name; 99 return this; 100 } 101 setPackage(String pkg)102 public TestModeBuilder setPackage(String pkg) { 103 mRule.setPackageName(pkg); 104 mConfigZenRule.pkg = pkg; 105 return this; 106 } 107 setOwner(ComponentName owner)108 public TestModeBuilder setOwner(ComponentName owner) { 109 mRule.setOwner(owner); 110 mConfigZenRule.component = owner; 111 return this; 112 } 113 setConfigurationActivity(ComponentName configActivity)114 public TestModeBuilder setConfigurationActivity(ComponentName configActivity) { 115 mRule.setConfigurationActivity(configActivity); 116 mConfigZenRule.configurationActivity = configActivity; 117 return this; 118 } 119 setConditionId(Uri conditionId)120 public TestModeBuilder setConditionId(Uri conditionId) { 121 mRule.setConditionId(conditionId); 122 mConfigZenRule.conditionId = conditionId; 123 return this; 124 } 125 setType(@utomaticZenRule.Type int type)126 public TestModeBuilder setType(@AutomaticZenRule.Type int type) { 127 mRule.setType(type); 128 mConfigZenRule.type = type; 129 return this; 130 } 131 setInterruptionFilter( @otificationManager.InterruptionFilter int interruptionFilter)132 public TestModeBuilder setInterruptionFilter( 133 @NotificationManager.InterruptionFilter int interruptionFilter) { 134 mRule.setInterruptionFilter(interruptionFilter); 135 mConfigZenRule.zenMode = NotificationManager.zenModeFromInterruptionFilter( 136 interruptionFilter, INTERRUPTION_FILTER_PRIORITY); 137 return this; 138 } 139 setZenPolicy(@ullable ZenPolicy policy)140 public TestModeBuilder setZenPolicy(@Nullable ZenPolicy policy) { 141 mRule.setZenPolicy(policy); 142 mConfigZenRule.zenPolicy = policy; 143 return this; 144 } 145 setDeviceEffects(@ullable ZenDeviceEffects deviceEffects)146 public TestModeBuilder setDeviceEffects(@Nullable ZenDeviceEffects deviceEffects) { 147 mRule.setDeviceEffects(deviceEffects); 148 mConfigZenRule.zenDeviceEffects = deviceEffects; 149 return this; 150 } 151 setVisualEffect(int effect, boolean allowed)152 public TestModeBuilder setVisualEffect(int effect, boolean allowed) { 153 ZenPolicy newPolicy = new ZenPolicy.Builder(mRule.getZenPolicy()) 154 .showVisualEffect(effect, allowed).build(); 155 setZenPolicy(newPolicy); 156 return this; 157 } 158 setEnabled(boolean enabled)159 public TestModeBuilder setEnabled(boolean enabled) { 160 return setEnabled(enabled, /* byUser= */ false); 161 } 162 setEnabled(boolean enabled, boolean byUser)163 public TestModeBuilder setEnabled(boolean enabled, boolean byUser) { 164 mRule.setEnabled(enabled); 165 mConfigZenRule.enabled = enabled; 166 if (!enabled) { 167 mConfigZenRule.disabledOrigin = byUser ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_UNKNOWN; 168 } 169 return this; 170 } 171 setManualInvocationAllowed(boolean allowed)172 public TestModeBuilder setManualInvocationAllowed(boolean allowed) { 173 mRule.setManualInvocationAllowed(allowed); 174 mConfigZenRule.allowManualInvocation = allowed; 175 return this; 176 } 177 setTriggerDescription(@ullable String triggerDescription)178 public TestModeBuilder setTriggerDescription(@Nullable String triggerDescription) { 179 mRule.setTriggerDescription(triggerDescription); 180 mConfigZenRule.triggerDescription = triggerDescription; 181 return this; 182 } 183 setIconResId(@rawableRes int iconResId)184 public TestModeBuilder setIconResId(@DrawableRes int iconResId) { 185 mRule.setIconResId(iconResId); 186 return this; 187 } 188 implicitForPackage(String pkg)189 public TestModeBuilder implicitForPackage(String pkg) { 190 setPackage(pkg); 191 setId(ZenModeConfig.implicitRuleId(pkg)); 192 setName("Do Not Disturb (" + pkg + ")"); 193 return this; 194 } 195 setActive(boolean active)196 public TestModeBuilder setActive(boolean active) { 197 if (active) { 198 mConfigZenRule.enabled = true; 199 mConfigZenRule.condition = new Condition(mRule.getConditionId(), "...", 200 Condition.STATE_TRUE); 201 } else { 202 mConfigZenRule.condition = null; 203 } 204 return this; 205 } 206 makeManualDnd()207 public TestModeBuilder makeManualDnd() { 208 mIsManualDnd = true; 209 // Set the "fixed" properties of a DND mode. Other things, such as policy/filter may be set 210 // separately or copied from a preexisting DND, so they are not overwritten here. 211 setId(ZenMode.MANUAL_DND_MODE_ID); 212 setName("Do Not Disturb"); 213 setType(AutomaticZenRule.TYPE_OTHER); 214 setManualInvocationAllowed(true); 215 setPackage(SystemZenRules.PACKAGE_ANDROID); 216 setConditionId(Uri.EMPTY); 217 return this; 218 } 219 build()220 public ZenMode build() { 221 if (mIsManualDnd) { 222 return ZenMode.manualDndMode(mRule, mConfigZenRule.condition != null 223 && mConfigZenRule.condition.state == Condition.STATE_TRUE); 224 } else { 225 return new ZenMode(mId, mRule, mConfigZenRule); 226 } 227 } 228 } 229