2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
31 import com.android.settings.R;
32 import com.android.settingslib.core.AbstractPreferenceController;
34 public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase {
36 protected static final String TAG = ZenModeSettingsBase.TAG;
37 protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
39 protected Context mContext;
40 protected boolean mDisableListeners;
41 protected AutomaticZenRule mRule;
44 protected ZenAutomaticRuleHeaderPreferenceController mHeader;
45 protected ZenAutomaticRuleSwitchPreferenceController mSwitch;
47 abstract protected void onCreateInternal();
48 abstract protected boolean setRule(AutomaticZenRule rule);
49 abstract protected void updateControlsInternal();
52 public void onCreate(Bundle icicle) {
53 mContext = getActivity();
55 final Intent intent = getActivity().getIntent();
56 if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
58 Log.w(TAG, "No intent");
63 mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID);
65 Log.w(TAG, "rule id is null");
70 if (DEBUG) Log.d(TAG, "mId=" + mId);
71 if (refreshRuleOrFinish()) {
75 super.onCreate(icicle);
80 public void onResume() {
82 if (isUiRestricted()) {
89 public int getHelpResource() {
90 return R.string.help_uri_interruptions;
94 * Update state of header preference managed by PreferenceController.
96 protected void updateHeader() {
97 final PreferenceScreen screen = getPreferenceScreen();
99 mSwitch.onResume(mRule, mId);
100 mSwitch.displayPreference(screen);
101 updatePreference(mSwitch);
103 mHeader.onResume(mRule, mId);
104 mHeader.displayPreference(screen);
105 updatePreference(mHeader);
108 private void updatePreference(AbstractPreferenceController controller) {
109 final PreferenceScreen screen = getPreferenceScreen();
110 if (!controller.isAvailable()) {
113 final String key = controller.getPreferenceKey();
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()));
121 controller.updateState(preference);
124 protected void updateRule(Uri newConditionId) {
125 mRule.setConditionId(newConditionId);
126 mBackend.setZenRule(mId, mRule);
130 protected void onZenModeConfigChanged() {
131 super.onZenModeConfigChanged();
132 if (!refreshRuleOrFinish()) {
137 private boolean refreshRuleOrFinish() {
138 mRule = getZenRule();
139 if (DEBUG) Log.d(TAG, "mRule=" + mRule);
140 if (!setRule(mRule)) {
147 private void toastAndFinish() {
148 Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
150 getActivity().finish();
153 private AutomaticZenRule getZenRule() {
154 return NotificationManager.from(mContext).getAutomaticZenRule(mId);
157 private void updateControls() {
158 mDisableListeners = true;
159 updateControlsInternal();
161 mDisableListeners = false;