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.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.util.Log; 29 import android.widget.Toast; 30 31 import com.android.settings.R; 32 import com.android.settingslib.core.AbstractPreferenceController; 33 34 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase { 35 36 protected static final String TAG = ZenModeSettingsBase.TAG; 37 protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG; 38 39 protected Context mContext; 40 protected boolean mDisableListeners; 41 protected AutomaticZenRule mRule; 42 protected String mId; 43 44 protected ZenAutomaticRuleHeaderPreferenceController mHeader; 45 protected ZenAutomaticRuleSwitchPreferenceController mSwitch; 46 47 abstract protected void onCreateInternal(); 48 abstract protected boolean setRule(AutomaticZenRule rule); 49 abstract protected void updateControlsInternal(); 50 51 @Override 52 public void onCreate(Bundle icicle) { 53 mContext = getActivity(); 54 55 final Intent intent = getActivity().getIntent(); 56 if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent); 57 if (intent == null) { 58 Log.w(TAG, "No intent"); 59 toastAndFinish(); 60 return; 61 } 62 63 mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID); 64 if (mId == null) { 65 Log.w(TAG, "rule id is null"); 66 toastAndFinish(); 67 return; 68 } 69 70 if (DEBUG) Log.d(TAG, "mId=" + mId); 71 if (refreshRuleOrFinish()) { 72 return; 73 } 74 75 super.onCreate(icicle); 76 onCreateInternal(); 77 } 78 79 @Override 80 public void onResume() { 81 super.onResume(); 82 if (isUiRestricted()) { 83 return; 84 } 85 updateControls(); 86 } 87 88 @Override 89 public int getHelpResource() { 90 return R.string.help_uri_interruptions; 91 } 92 93 /** 94 * Update state of header preference managed by PreferenceController. 95 */ 96 protected void updateHeader() { 97 final PreferenceScreen screen = getPreferenceScreen(); 98 99 mSwitch.onResume(mRule, mId); 100 mSwitch.displayPreference(screen); 101 updatePreference(mSwitch); 102 103 mHeader.onResume(mRule, mId); 104 mHeader.displayPreference(screen); 105 updatePreference(mHeader); 106 } 107 108 private void updatePreference(AbstractPreferenceController controller) { 109 final PreferenceScreen screen = getPreferenceScreen(); 110 if (!controller.isAvailable()) { 111 return; 112 } 113 final String key = controller.getPreferenceKey(); 114 115 final Preference preference = screen.findPreference(key); 116 if (preference == null) { 117 Log.d(TAG, String.format("Cannot find preference with key %s in Controller %s", 118 key, controller.getClass().getSimpleName())); 119 return; 120 } 121 controller.updateState(preference); 122 } 123 124 protected void updateRule(Uri newConditionId) { 125 mRule.setConditionId(newConditionId); 126 mBackend.setZenRule(mId, mRule); 127 } 128 129 @Override 130 protected void onZenModeConfigChanged() { 131 super.onZenModeConfigChanged(); 132 if (!refreshRuleOrFinish()) { 133 updateControls(); 134 } 135 } 136 137 private boolean refreshRuleOrFinish() { 138 mRule = getZenRule(); 139 if (DEBUG) Log.d(TAG, "mRule=" + mRule); 140 if (!setRule(mRule)) { 141 toastAndFinish(); 142 return true; 143 } 144 return false; 145 } 146 147 private void toastAndFinish() { 148 Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT) 149 .show(); 150 getActivity().finish(); 151 } 152 153 private AutomaticZenRule getZenRule() { 154 return NotificationManager.from(mContext).getAutomaticZenRule(mId); 155 } 156 157 private void updateControls() { 158 mDisableListeners = true; 159 updateControlsInternal(); 160 updateHeader(); 161 mDisableListeners = false; 162 } 163 } 164