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