1 /*
2  * Copyright (C) 2018 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.settings.SettingsEnums;
20 import android.content.Context;
21 import android.service.notification.ZenPolicy;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.SubSettingLauncher;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 
29 public class ZenRuleCustomPolicyPreferenceController extends
30         AbstractZenCustomRulePreferenceController {
31 
32     private ZenCustomRadioButtonPreference mPreference;
33 
ZenRuleCustomPolicyPreferenceController(Context context, Lifecycle lifecycle, String key)34     public ZenRuleCustomPolicyPreferenceController(Context context, Lifecycle lifecycle,
35             String key) {
36         super(context, key, lifecycle);
37     }
38 
39     @Override
displayPreference(PreferenceScreen screen)40     public void displayPreference(PreferenceScreen screen) {
41         super.displayPreference(screen);
42         mPreference = screen.findPreference(getPreferenceKey());
43 
44         mPreference.setOnGearClickListener(p -> {
45             setCustomPolicy();
46             launchCustomSettings();
47 
48         });
49 
50         mPreference.setOnRadioButtonClickListener(p -> {
51             setCustomPolicy();
52             launchCustomSettings();
53         });
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         super.updateState(preference);
59         if (mId == null || mRule == null) {
60             return;
61         }
62 
63         mPreference.setChecked(mRule.getZenPolicy() != null);
64     }
65 
setCustomPolicy()66     private void setCustomPolicy() {
67         if (mRule.getZenPolicy() == null) {
68             mRule.setZenPolicy(mBackend.setDefaultZenPolicy(new ZenPolicy()));
69             mBackend.updateZenRule(mId, mRule);
70         }
71     }
72 
launchCustomSettings()73     private void launchCustomSettings() {
74         new SubSettingLauncher(mContext)
75                 .setDestination(ZenCustomRuleConfigSettings.class.getName())
76                 .setArguments(createBundle())
77                 .setSourceMetricsCategory(SettingsEnums.ZEN_CUSTOM_RULE_SOUND_SETTINGS)
78                 .launch();
79     }
80 }