• 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 
17 package com.android.settings.notification.zen;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.service.notification.ZenPolicy;
22 import android.text.TextUtils;
23 import android.util.Pair;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.ListPreference;
27 import androidx.preference.Preference;
28 
29 import com.android.internal.logging.nano.MetricsProto;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 
32 public class ZenRuleCallsPreferenceController extends AbstractZenCustomRulePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
35     private final String[] mListValues;
36 
ZenRuleCallsPreferenceController(Context context, String key, Lifecycle lifecycle)37     public ZenRuleCallsPreferenceController(Context context, String key, Lifecycle lifecycle) {
38         super(context, key, lifecycle);
39         mListValues = context.getResources().getStringArray(
40                 com.android.settings.R.array.zen_mode_contacts_values);
41     }
42 
43     @Override
updateState(Preference preference)44     public void updateState(Preference preference) {
45         super.updateState(preference);
46         updateFromContactsValue(preference);
47     }
48 
49     @Override
onPreferenceChange(Preference preference, Object selectedContactsFrom)50     public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
51         int allowCalls = ZenModeBackend.getZenPolicySettingFromPrefKey(
52                 selectedContactsFrom.toString());
53         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ZEN_ALLOW_CALLS,
54                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_TOGGLE_EXCEPTION, allowCalls),
55                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_RULE_ID, mId));
56         mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
57                 .allowCalls(allowCalls)
58                 .build());
59         mBackend.updateZenRule(mId, mRule);
60         updateFromContactsValue(preference);
61         return true;
62     }
63 
updateFromContactsValue(Preference preference)64     private void updateFromContactsValue(Preference preference) {
65         if (mRule == null || mRule.getZenPolicy() == null) {
66             return;
67         }
68         ListPreference listPreference = (ListPreference) preference;
69         listPreference.setSummary(mBackend.getContactsCallsSummary(mRule.getZenPolicy()));
70         final String currentVal = ZenModeBackend.getKeyFromZenPolicySetting(
71                 mRule.getZenPolicy().getPriorityCallSenders());
72         listPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
73 
74     }
75 
76     @VisibleForTesting
getIndexOfSendersValue(String currentVal)77     protected int getIndexOfSendersValue(String currentVal) {
78         int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
79         for (int i = 0; i < mListValues.length; i++) {
80             if (TextUtils.equals(currentVal, mListValues[i])) {
81                 return i;
82             }
83         }
84 
85         return index;
86     }
87 }
88