1 /*
2  * Copyright (C) 2020 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 static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
20 import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
21 import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
22 
23 import android.content.Context;
24 import android.widget.ImageView;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.widget.LayoutPreference;
32 
33 /**
34  * Common preference controller functionality shared by
35  * ZenModeCallsSettings and ZenModeMessagesSettings.
36  *
37  * Changes the image resource based on the selected senders allowed to bypass DND option for
38  * calls or messages.
39  */
40 public class ZenModeSendersImagePreferenceController
41         extends AbstractZenModePreferenceController {
42 
43     private final boolean mIsMessages; // if this is false, then this preference is for calls
44 
45     private ImageView mImageView;
46 
ZenModeSendersImagePreferenceController(Context context, String key, Lifecycle lifecycle, boolean isMessages)47     public ZenModeSendersImagePreferenceController(Context context, String key,
48             Lifecycle lifecycle, boolean isMessages) {
49         super(context, key, lifecycle);
50         mIsMessages = isMessages;
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56         LayoutPreference pref = (LayoutPreference) screen.findPreference(KEY);
57         mImageView = (ImageView) pref.findViewById(R.id.zen_mode_settings_senders_image);
58     }
59 
60     @Override
isAvailable()61     public boolean isAvailable() {
62         return true;
63     }
64 
65     @Override
getPreferenceKey()66     public String getPreferenceKey() {
67         return KEY;
68     }
69 
70     @Override
updateState(Preference preference)71     public void updateState(Preference preference) {
72         final int currSetting = getPrioritySenders();
73         int newImageRes;
74         CharSequence newContentDescription = "";
75         if (PRIORITY_SENDERS_ANY == currSetting) {
76             newImageRes = mIsMessages
77                     ? R.drawable.zen_messages_any
78                     : R.drawable.zen_calls_any;
79             newContentDescription = mContext.getString(R.string.zen_mode_from_anyone);
80         } else if (PRIORITY_SENDERS_CONTACTS == currSetting) {
81             newImageRes = mIsMessages
82                     ? R.drawable.zen_messages_contacts
83                     : R.drawable.zen_calls_contacts;
84             newContentDescription = mContext.getString(R.string.zen_mode_from_contacts);
85         } else if (PRIORITY_SENDERS_STARRED == currSetting) {
86             newImageRes = mIsMessages
87                     ? R.drawable.zen_messages_starred
88                     : R.drawable.zen_calls_starred;
89             newContentDescription = mContext.getString(R.string.zen_mode_from_starred);
90         } else {
91             newImageRes = mIsMessages
92                     ? R.drawable.zen_messages_none
93                     : R.drawable.zen_calls_none;
94             newContentDescription =
95                     mContext.getString(mIsMessages
96                             ? R.string.zen_mode_none_messages
97                             : R.string.zen_mode_none_calls);
98         }
99 
100         mImageView.setImageResource(newImageRes);
101         mImageView.setContentDescription(newContentDescription);
102     }
103 
getPrioritySenders()104     private int getPrioritySenders() {
105         if (mIsMessages) {
106             return mBackend.getPriorityMessageSenders();
107         } else {
108             return mBackend.getPriorityCallSenders();
109         }
110     }
111 }
112