1 /* 2 * Copyright (C) 2015 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 static android.app.NotificationManager.EXTRA_AUTOMATIC_RULE_ID; 20 21 import android.app.AutomaticZenRule; 22 import android.app.Flags; 23 import android.app.NotificationManager; 24 import android.app.settings.SettingsEnums; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.Bundle; 28 import android.service.notification.ConditionProviderService; 29 import android.service.notification.SystemZenRules; 30 import android.service.notification.ZenModeConfig; 31 import android.util.Log; 32 import android.view.View; 33 import android.widget.Toast; 34 35 import androidx.annotation.NonNull; 36 import androidx.annotation.Nullable; 37 import androidx.preference.Preference; 38 import androidx.preference.PreferenceScreen; 39 40 import com.android.settings.R; 41 import com.android.settings.Utils; 42 import com.android.settings.core.SubSettingLauncher; 43 44 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase { 45 46 protected static final String TAG = ZenModeSettingsBase.TAG; 47 protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG; 48 49 private final String CUSTOM_BEHAVIOR_KEY = "zen_custom_setting"; 50 51 protected boolean mDisableListeners; 52 protected AutomaticZenRule mRule; 53 protected String mId; 54 private boolean mRuleRemoved; 55 56 protected ZenAutomaticRuleHeaderPreferenceController mHeader; 57 protected ZenRuleButtonsPreferenceController mActionButtons; 58 protected ZenAutomaticRuleSwitchPreferenceController mSwitch; 59 protected Preference mCustomBehaviorPreference; 60 onCreateInternal()61 abstract protected void onCreateInternal(); setRule(AutomaticZenRule rule)62 abstract protected boolean setRule(AutomaticZenRule rule); updateControlsInternal()63 abstract protected void updateControlsInternal(); 64 65 @Override onAttach(Context context)66 public void onAttach(Context context) { 67 super.onAttach(context); 68 69 final Intent intent = getActivity().getIntent(); 70 if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent); 71 if (intent == null) { 72 Log.w(TAG, "No intent"); 73 toastAndFinish(); 74 return; 75 } 76 77 mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID); 78 if (mId == null) { 79 mId = intent.getStringExtra(EXTRA_AUTOMATIC_RULE_ID); 80 if (mId == null) { 81 Log.w(TAG, "rule id is null"); 82 toastAndFinish(); 83 return; 84 } 85 } 86 87 if (DEBUG) Log.d(TAG, "mId=" + mId); 88 refreshRuleOrFinish(); 89 } 90 91 @Override onCreate(Bundle icicle)92 public void onCreate(Bundle icicle) { 93 super.onCreate(icicle); 94 95 if (isFinishingOrDestroyed()) { 96 return; 97 } 98 99 mCustomBehaviorPreference = getPreferenceScreen().findPreference(CUSTOM_BEHAVIOR_KEY); 100 mCustomBehaviorPreference.setOnPreferenceClickListener( 101 new Preference.OnPreferenceClickListener() { 102 @Override 103 public boolean onPreferenceClick(Preference preference) { 104 Bundle bundle = new Bundle(); 105 bundle.putString(ZenCustomRuleSettings.RULE_ID, mId); 106 107 // Skip the radio button screen distinguishing between "default" and 108 // "custom" and take users directly to the custom settings screen. 109 // From ZenRuleCustomPolicyPreferenceController#launchCustomSettings 110 String destination = ZenCustomRuleConfigSettings.class.getName(); 111 int sourceMetricsCategory = SettingsEnums.ZEN_CUSTOM_RULE_SOUND_SETTINGS; 112 new SubSettingLauncher(mContext) 113 .setDestination(destination) 114 .setArguments(bundle) 115 .setSourceMetricsCategory(sourceMetricsCategory) 116 .launch(); 117 return true; 118 } 119 }); 120 onCreateInternal(); 121 } 122 123 @Override onResume()124 public void onResume() { 125 super.onResume(); 126 if (isUiRestricted()) { 127 return; 128 } 129 if (!refreshRuleOrFinish()) { 130 updateControls(); 131 } 132 } 133 134 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)135 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 136 super.onViewCreated(view, savedInstanceState); 137 Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), getListView()); 138 } 139 140 @Override getHelpResource()141 public int getHelpResource() { 142 return R.string.help_uri_interruptions; 143 } 144 145 /** 146 * Update state of header preference managed by PreferenceController. 147 */ updateHeader()148 protected void updateHeader() { 149 final PreferenceScreen screen = getPreferenceScreen(); 150 151 mSwitch.displayPreference(screen); 152 updatePreference(mSwitch); 153 154 mHeader.displayPreference(screen); 155 updatePreference(mHeader); 156 157 mActionButtons.displayPreference(screen); 158 updatePreference(mActionButtons); 159 } 160 updateScheduleRule(ZenModeConfig.ScheduleInfo schedule)161 protected void updateScheduleRule(ZenModeConfig.ScheduleInfo schedule) { 162 mRule.setConditionId(ZenModeConfig.toScheduleConditionId(schedule)); 163 if (Flags.modesUi()) { 164 mRule.setTriggerDescription( 165 SystemZenRules.getTriggerDescriptionForScheduleTime(mContext, schedule)); 166 } 167 mBackend.updateZenRule(mId, mRule); 168 } 169 updateEventRule(ZenModeConfig.EventInfo event)170 protected void updateEventRule(ZenModeConfig.EventInfo event) { 171 mRule.setConditionId(ZenModeConfig.toEventConditionId(event)); 172 if (Flags.modesUi()) { 173 mRule.setTriggerDescription( 174 SystemZenRules.getTriggerDescriptionForScheduleEvent(mContext, event)); 175 } 176 mBackend.updateZenRule(mId, mRule); 177 } 178 179 @Override onZenModeConfigChanged()180 protected void onZenModeConfigChanged() { 181 super.onZenModeConfigChanged(); 182 if (!refreshRuleOrFinish()) { 183 updateControls(); 184 } 185 } 186 refreshRuleOrFinish()187 private boolean refreshRuleOrFinish() { 188 if (mRuleRemoved && getActivity() != null) { 189 getActivity().finish(); 190 return true; 191 } 192 mRule = getZenRule(); 193 if (DEBUG) Log.d(TAG, "mRule=" + mRule); 194 mHeader.setRule(mRule); 195 mSwitch.setIdAndRule(mId, mRule); 196 mActionButtons.setIdAndRule(mId, mRule); 197 if (!setRule(mRule)) { 198 toastAndFinish(); 199 return true; 200 } 201 return false; 202 } 203 toastAndFinish()204 private void toastAndFinish() { 205 Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT) 206 .show(); 207 208 getActivity().finish(); 209 } 210 getZenRule()211 private AutomaticZenRule getZenRule() { 212 return NotificationManager.from(mContext).getAutomaticZenRule(mId); 213 } 214 updateControls()215 private void updateControls() { 216 mDisableListeners = true; 217 updateControlsInternal(); 218 updateHeader(); 219 if (mRule.getZenPolicy() == null) { 220 mCustomBehaviorPreference.setSummary(R.string.zen_mode_custom_behavior_summary_default); 221 } else { 222 mCustomBehaviorPreference.setSummary(R.string.zen_mode_custom_behavior_summary); 223 } 224 mDisableListeners = false; 225 } 226 onRuleRemoved()227 void onRuleRemoved() { 228 mRuleRemoved = true; 229 } 230 } 231