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; 18 19 import android.app.AutomaticZenRule; 20 import android.app.NotificationManager; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.service.notification.ConditionProviderService; 26 import android.util.Log; 27 import android.widget.Toast; 28 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.R; 33 import com.android.settings.core.SubSettingLauncher; 34 35 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase { 36 37 protected static final String TAG = ZenModeSettingsBase.TAG; 38 protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG; 39 40 private final String CUSTOM_BEHAVIOR_KEY = "zen_custom_setting"; 41 42 protected Context mContext; 43 protected boolean mDisableListeners; 44 protected AutomaticZenRule mRule; 45 protected String mId; 46 47 protected ZenAutomaticRuleHeaderPreferenceController mHeader; 48 protected ZenRuleButtonsPreferenceController mActionButtons; 49 protected ZenAutomaticRuleSwitchPreferenceController mSwitch; 50 protected Preference mCustomBehaviorPreference; 51 onCreateInternal()52 abstract protected void onCreateInternal(); setRule(AutomaticZenRule rule)53 abstract protected boolean setRule(AutomaticZenRule rule); updateControlsInternal()54 abstract protected void updateControlsInternal(); 55 56 @Override onCreate(Bundle icicle)57 public void onCreate(Bundle icicle) { 58 mContext = getActivity(); 59 60 final Intent intent = getActivity().getIntent(); 61 if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent); 62 if (intent == null) { 63 Log.w(TAG, "No intent"); 64 toastAndFinish(); 65 return; 66 } 67 68 mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID); 69 if (mId == null) { 70 Log.w(TAG, "rule id is null"); 71 toastAndFinish(); 72 return; 73 } 74 75 if (DEBUG) Log.d(TAG, "mId=" + mId); 76 if (refreshRuleOrFinish()) { 77 return; 78 } 79 80 super.onCreate(icicle); 81 mCustomBehaviorPreference = getPreferenceScreen().findPreference(CUSTOM_BEHAVIOR_KEY); 82 mCustomBehaviorPreference.setOnPreferenceClickListener( 83 new Preference.OnPreferenceClickListener() { 84 @Override 85 public boolean onPreferenceClick(Preference preference) { 86 Bundle bundle = new Bundle(); 87 bundle.putString(ZenCustomRuleSettings.RULE_ID, mId); 88 new SubSettingLauncher(mContext) 89 .setDestination(ZenCustomRuleSettings.class.getName()) 90 .setArguments(bundle) 91 .setSourceMetricsCategory(0) // TODO 92 .launch(); 93 return true; 94 } 95 }); 96 onCreateInternal(); 97 } 98 99 @Override onResume()100 public void onResume() { 101 super.onResume(); 102 if (isUiRestricted()) { 103 return; 104 } 105 if (!refreshRuleOrFinish()) { 106 updateControls(); 107 } 108 } 109 110 @Override getHelpResource()111 public int getHelpResource() { 112 return R.string.help_uri_interruptions; 113 } 114 115 /** 116 * Update state of header preference managed by PreferenceController. 117 */ updateHeader()118 protected void updateHeader() { 119 final PreferenceScreen screen = getPreferenceScreen(); 120 121 mSwitch.onResume(mRule, mId); 122 mSwitch.displayPreference(screen); 123 updatePreference(mSwitch); 124 125 mHeader.onResume(mRule, mId); 126 mHeader.displayPreference(screen); 127 updatePreference(mHeader); 128 129 mActionButtons.onResume(mRule, mId); 130 mActionButtons.displayPreference(screen); 131 updatePreference(mActionButtons); 132 } 133 updateRule(Uri newConditionId)134 protected void updateRule(Uri newConditionId) { 135 mRule.setConditionId(newConditionId); 136 mBackend.updateZenRule(mId, mRule); 137 } 138 139 @Override onZenModeConfigChanged()140 protected void onZenModeConfigChanged() { 141 super.onZenModeConfigChanged(); 142 if (!refreshRuleOrFinish()) { 143 updateControls(); 144 } 145 } 146 refreshRuleOrFinish()147 private boolean refreshRuleOrFinish() { 148 mRule = getZenRule(); 149 if (DEBUG) Log.d(TAG, "mRule=" + mRule); 150 if (!setRule(mRule)) { 151 toastAndFinish(); 152 return true; 153 } 154 return false; 155 } 156 toastAndFinish()157 private void toastAndFinish() { 158 Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT) 159 .show(); 160 161 getActivity().finish(); 162 } 163 getZenRule()164 private AutomaticZenRule getZenRule() { 165 return NotificationManager.from(mContext).getAutomaticZenRule(mId); 166 } 167 updateControls()168 private void updateControls() { 169 mDisableListeners = true; 170 updateControlsInternal(); 171 updateHeader(); 172 if (mRule.getZenPolicy() == null) { 173 mCustomBehaviorPreference.setSummary(R.string.zen_mode_custom_behavior_summary_default); 174 } else { 175 mCustomBehaviorPreference.setSummary(R.string.zen_mode_custom_behavior_summary); 176 } 177 mDisableListeners = false; 178 } 179 } 180