• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.notification.zen;
17 
18 import android.app.settings.SettingsEnums;
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.PreferenceScreen;
26 import androidx.preference.SwitchPreference;
27 
28 import com.android.internal.logging.nano.MetricsProto;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 
31 public class ZenRuleRepeatCallersPreferenceController extends
32         AbstractZenCustomRulePreferenceController implements Preference.OnPreferenceChangeListener {
33 
34     private final int mRepeatCallersThreshold;
35 
ZenRuleRepeatCallersPreferenceController(Context context, String key, Lifecycle lifecycle, int repeatCallersThreshold)36     public ZenRuleRepeatCallersPreferenceController(Context context,
37             String key, Lifecycle lifecycle, int repeatCallersThreshold) {
38         super(context, key, lifecycle);
39         mRepeatCallersThreshold = repeatCallersThreshold;
40     }
41 
42     @Override
displayPreference(PreferenceScreen screen)43     public void displayPreference(PreferenceScreen screen) {
44         super.displayPreference(screen);
45         setRepeatCallerSummary(screen.findPreference(KEY));
46     }
47 
48     @Override
updateState(Preference preference)49     public void updateState(Preference preference) {
50         super.updateState(preference);
51         if (mRule == null || mRule.getZenPolicy() == null) {
52             return;
53         }
54 
55         SwitchPreference pref = (SwitchPreference) preference;
56         boolean anyCallersCanBypassDnd = mRule.getZenPolicy().getPriorityCallSenders()
57                 == ZenPolicy.PEOPLE_TYPE_ANYONE;
58 
59         // if any caller can bypass dnd then repeat callers preference is disabled
60         if (anyCallersCanBypassDnd) {
61             pref.setEnabled(false);
62             pref.setChecked(true);
63         } else {
64             pref.setEnabled(true);
65             pref.setChecked(mRule.getZenPolicy().getPriorityCategoryRepeatCallers()
66                     == ZenPolicy.STATE_ALLOW);
67         }
68     }
69 
70     @Override
onPreferenceChange(Preference preference, Object newValue)71     public boolean onPreferenceChange(Preference preference, Object newValue) {
72         final boolean allow = (Boolean) newValue;
73         if (ZenModeSettingsBase.DEBUG) {
74             Log.d(TAG, KEY + " onPrefChange mRule=" + mRule + " mCategory="
75                     + ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS + " allow=" + allow);
76         }
77         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ZEN_ALLOW_REPEAT_CALLS,
78                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_TOGGLE_EXCEPTION, allow ? 1 : 0),
79                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_RULE_ID, mId));
80         mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
81                 .allowRepeatCallers(allow)
82                 .build());
83         mBackend.updateZenRule(mId, mRule);
84         return true;
85     }
86 
setRepeatCallerSummary(Preference preference)87     private void setRepeatCallerSummary(Preference preference) {
88         preference.setSummary(mContext.getString(
89                 com.android.settings.R.string.zen_mode_repeat_callers_summary,
90                 mRepeatCallersThreshold));
91     }
92 }
93