1 /*
2  * Copyright (C) 2019 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.car.developeroptions.notification;
18 
19 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20 import static android.app.NotificationManager.IMPORTANCE_HIGH;
21 import static android.app.NotificationManager.IMPORTANCE_LOW;
22 import static android.app.NotificationManager.IMPORTANCE_MIN;
23 import static android.app.NotificationManager.IMPORTANCE_NONE;
24 
25 import android.content.Context;
26 import android.graphics.Color;
27 import android.graphics.drawable.Drawable;
28 import android.graphics.drawable.GradientDrawable;
29 import android.graphics.drawable.LayerDrawable;
30 import android.util.ArrayMap;
31 import android.util.AttributeSet;
32 import android.view.View;
33 import android.widget.ImageButton;
34 
35 import com.android.settingslib.R;
36 
37 import androidx.preference.Preference;
38 import androidx.preference.PreferenceViewHolder;
39 
40 public class ImportancePreference extends Preference {
41 
42     boolean mIsBlockable = true;
43     boolean mIsConfigurable = true;
44     int mImportance;
45     ImageButton blockButton;
46     ImageButton silenceButton;
47     ImageButton alertButton;
48     ArrayMap<ImageButton, Integer> mImageButtons = new ArrayMap<>();
49     Context mContext;
50 
ImportancePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)51     public ImportancePreference(Context context, AttributeSet attrs,
52             int defStyleAttr, int defStyleRes) {
53         super(context, attrs, defStyleAttr, defStyleRes);
54         init(context);
55     }
56 
ImportancePreference(Context context, AttributeSet attrs, int defStyleAttr)57     public ImportancePreference(Context context, AttributeSet attrs, int defStyleAttr) {
58         super(context, attrs, defStyleAttr);
59         init(context);
60     }
61 
ImportancePreference(Context context, AttributeSet attrs)62     public ImportancePreference(Context context, AttributeSet attrs) {
63         super(context, attrs);
64         init(context);
65     }
66 
ImportancePreference(Context context)67     public ImportancePreference(Context context) {
68         super(context);
69         init(context);
70     }
71 
init(Context context)72     private void init(Context context) {
73         mContext = context;
74         setLayoutResource(R.layout.notif_importance_preference);
75     }
76 
setImportance(int importance)77     public void setImportance(int importance) {
78         mImportance = importance;
79     }
80 
setBlockable(boolean blockable)81     public void setBlockable(boolean blockable) {
82         mIsBlockable = blockable;
83     }
84 
setConfigurable(boolean configurable)85     public void setConfigurable(boolean configurable) {
86         mIsConfigurable = configurable;
87     }
88 
89     @Override
onBindViewHolder(PreferenceViewHolder holder)90     public void onBindViewHolder(PreferenceViewHolder holder) {
91         super.onBindViewHolder(holder);
92 
93         View blockView = holder.itemView.findViewById(R.id.block);
94         View alertView = holder.itemView.findViewById(R.id.alert);
95         View silenceView = holder.itemView.findViewById(R.id.silence);
96         if (!mIsBlockable) {
97             blockView.setVisibility(View.GONE);
98             if (mImportance == IMPORTANCE_NONE) {
99                 mImportance = IMPORTANCE_LOW;
100                 callChangeListener(IMPORTANCE_LOW);
101             }
102 
103         }
104         blockButton = blockView.findViewById(R.id.block_icon);
105         silenceButton = silenceView.findViewById(R.id.silence_icon);
106         alertButton = alertView.findViewById(R.id.alert_icon);
107         mImageButtons.put(blockButton, mContext.getColor(R.color.notification_block_color));
108         mImageButtons.put(silenceButton, mContext.getColor(R.color.notification_silence_color));
109         mImageButtons.put(alertButton, mContext.getColor(R.color.notification_alert_color));
110 
111         switch (mImportance) {
112             case IMPORTANCE_NONE:
113                 colorizeImageButton(blockButton.getId());
114                 if (!mIsConfigurable) {
115                     alertView.setVisibility(View.GONE);
116                     silenceView.setVisibility(View.GONE);
117                 }
118                 break;
119             case IMPORTANCE_MIN:
120             case IMPORTANCE_LOW:
121                 colorizeImageButton(silenceButton.getId());
122                 if (!mIsConfigurable) {
123                     alertView.setVisibility(View.GONE);
124                     blockView.setVisibility(View.GONE);
125                 }
126                 break;
127             case IMPORTANCE_HIGH:
128             default:
129                 colorizeImageButton(alertButton.getId());
130                 if (!mIsConfigurable) {
131                     blockView.setVisibility(View.GONE);
132                     silenceView.setVisibility(View.GONE);
133                 }
134                 break;
135         }
136 
137         blockButton.setOnClickListener(v -> {
138             callChangeListener(IMPORTANCE_NONE);
139             colorizeImageButton(blockButton.getId());
140         });
141         silenceButton.setOnClickListener(v -> {
142             callChangeListener(IMPORTANCE_LOW);
143             colorizeImageButton(silenceButton.getId());
144         });
145         alertButton.setOnClickListener(v -> {
146             callChangeListener(IMPORTANCE_DEFAULT);
147             colorizeImageButton(alertButton.getId());
148         });
149     }
150 
colorizeImageButton(int buttonId)151     private void colorizeImageButton(int buttonId) {
152         if (mImageButtons != null) {
153             for (int i = 0; i < mImageButtons.size(); i++) {
154                 final ImageButton imageButton = mImageButtons.keyAt(i);
155                 final int color = mImageButtons.valueAt(i);
156                 if (imageButton != null) {
157                     LayerDrawable drawable = (LayerDrawable) imageButton.getDrawable();
158                     Drawable foreground = drawable.findDrawableByLayerId(R.id.fore);
159                     GradientDrawable background =
160                             (GradientDrawable) drawable.findDrawableByLayerId(R.id.back);
161                     if (buttonId == imageButton.getId()) {
162                         foreground.setTint(Color.WHITE);
163                         background.setColor(color);
164                     } else {
165                         foreground.setTint(color);
166                         background.setColor(Color.TRANSPARENT);
167                     }
168                 }
169             }
170         }
171     }
172 }
173