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