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 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 ZenRuleMessagesPreferenceController extends AbstractZenCustomRulePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
35     private final String[] mListValues;
36 
ZenRuleMessagesPreferenceController(Context context, String key, Lifecycle lifecycle)37     public ZenRuleMessagesPreferenceController(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 allowMessages = ZenModeBackend.getZenPolicySettingFromPrefKey(
52                 selectedContactsFrom.toString());
53         mMetricsFeatureProvider.action(mContext,
54                 SettingsEnums.ACTION_ZEN_ALLOW_MESSAGES,
55                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_TOGGLE_EXCEPTION, allowMessages),
56                 Pair.create(MetricsProto.MetricsEvent.FIELD_ZEN_RULE_ID, mId));
57         mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
58                 .allowMessages(allowMessages)
59                 .build());
60         mBackend.updateZenRule(mId, mRule);
61         updateFromContactsValue(preference);
62         return true;
63     }
64 
updateFromContactsValue(Preference preference)65     private void updateFromContactsValue(Preference preference) {
66         if (mRule == null || mRule.getZenPolicy() == null) {
67             return;
68         }
69         ListPreference listPreference = (ListPreference) preference;
70         listPreference.setSummary(mBackend.getContactsMessagesSummary(mRule.getZenPolicy()));
71         final String currentVal = ZenModeBackend.getKeyFromZenPolicySetting(
72                 mRule.getZenPolicy().getPriorityMessageSenders());
73         listPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
74 
75     }
76 
77     @VisibleForTesting
getIndexOfSendersValue(String currentVal)78     protected int getIndexOfSendersValue(String currentVal) {
79         int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
80         for (int i = 0; i < mListValues.length; i++) {
81             if (TextUtils.equals(currentVal, mListValues[i])) {
82                 return i;
83             }
84         }
85 
86         return index;
87     }
88 }
89