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