1 /*
2  * Copyright (C) 2017 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.content.Context;
20 import android.service.notification.ZenPolicy;
21 import android.util.Log;
22 import android.util.Pair;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.SwitchPreference;
26 
27 import com.android.internal.logging.nano.MetricsProto;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 public class ZenRuleCustomSwitchPreferenceController extends
31         AbstractZenCustomRulePreferenceController implements Preference.OnPreferenceChangeListener {
32 
33     private @ZenPolicy.PriorityCategory int mCategory;
34     private int mMetricsCategory;
35 
ZenRuleCustomSwitchPreferenceController(Context context, Lifecycle lifecycle, String key, @ZenPolicy.PriorityCategory int category, int metricsCategory)36     public ZenRuleCustomSwitchPreferenceController(Context context, Lifecycle lifecycle,
37             String key, @ZenPolicy.PriorityCategory int category, int metricsCategory) {
38         super(context, key, lifecycle);
39         mCategory = category;
40         mMetricsCategory = metricsCategory;
41     }
42 
43     @Override
updateState(Preference preference)44     public void updateState(Preference preference) {
45         super.updateState(preference);
46         if (mRule == null || mRule.getZenPolicy() == null) {
47             return;
48         }
49 
50         SwitchPreference pref = (SwitchPreference) preference;
51         pref.setChecked(mRule.getZenPolicy().isCategoryAllowed(mCategory, false));
52     }
53 
54     @Override
onPreferenceChange(Preference preference, Object newValue)55     public boolean onPreferenceChange(Preference preference, Object newValue) {
56         final boolean allow = (Boolean) newValue;
57         if (ZenModeSettingsBase.DEBUG) {
58             Log.d(TAG, KEY + " onPrefChange mRule=" + mRule + " mCategory=" + mCategory
59                     + " allow=" + allow);
60         }
61         mMetricsFeatureProvider.action(mContext, mMetricsCategory,
62                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_TOGGLE_EXCEPTION, allow ? 1 : 0),
63                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_RULE_ID, mId));
64         mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
65                 .allowCategory(mCategory, allow)
66                 .build());
67         mBackend.updateZenRule(mId, mRule);
68         return true;
69     }
70 }
71