• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app;
18 
19 import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL;
20 import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21 import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
22 
23 import android.content.Context;
24 import android.content.res.ColorStateList;
25 import android.graphics.drawable.Drawable;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceViewHolder;
33 
34 import com.android.settings.Utils;
35 import com.android.settingslib.R;
36 import com.android.settingslib.RestrictedLockUtils;
37 import com.android.settingslib.RestrictedPreferenceHelper;
38 
39 /**
40  * A tri-state preference allowing a user to specify what gets to bubble.
41  */
42 public class BubblePreference extends Preference implements View.OnClickListener {
43     RestrictedPreferenceHelper mHelper;
44 
45     private int mSelectedPreference;
46 
47     private Context mContext;
48 
49     private ButtonViewHolder mBubbleAllButton;
50     private ButtonViewHolder mBubbleSelectedButton;
51     private ButtonViewHolder mBubbleNoneButton;
52 
53     private boolean mSelectedVisible;
54 
BubblePreference(Context context)55     public BubblePreference(Context context) {
56         this(context, null);
57     }
58 
BubblePreference(Context context, AttributeSet attrs)59     public BubblePreference(Context context, AttributeSet attrs) {
60         this(context, attrs, 0);
61     }
62 
BubblePreference(Context context, AttributeSet attrs, int defStyleAttr)63     public BubblePreference(Context context, AttributeSet attrs, int defStyleAttr) {
64         this(context, attrs, defStyleAttr, 0);
65     }
66 
BubblePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)67     public BubblePreference(Context context, AttributeSet attrs,
68             int defStyleAttr, int defStyleRes) {
69         super(context, attrs, defStyleAttr, defStyleRes);
70         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
71         mHelper.useAdminDisabledSummary(true);
72         mContext = context;
73         setLayoutResource(R.layout.bubble_preference);
74     }
75 
setSelectedPreference(int preference)76     public void setSelectedPreference(int preference) {
77         mSelectedPreference = preference;
78     }
79 
getSelectedPreference()80     public int getSelectedPreference() {
81         return mSelectedPreference;
82     }
83 
setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)84     public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
85         if (mHelper.setDisabledByAdmin(admin)) {
86             notifyChanged();
87         }
88     }
89 
setSelectedVisibility(boolean visible)90     public void setSelectedVisibility(boolean visible) {
91         mSelectedVisible = visible;
92         notifyChanged();
93     }
94 
95     @Override
onBindViewHolder(final PreferenceViewHolder holder)96     public void onBindViewHolder(final PreferenceViewHolder holder) {
97         super.onBindViewHolder(holder);
98 
99         final boolean disabledByAdmin = mHelper.isDisabledByAdmin();
100         View summary = holder.findViewById(android.R.id.summary);
101         if (disabledByAdmin) {
102             mHelper.onBindViewHolder(holder);
103             summary.setVisibility(View.VISIBLE);
104         } else {
105             summary.setVisibility(View.GONE);
106         }
107         holder.itemView.setClickable(false);
108 
109         View bubbleAll = holder.findViewById(R.id.bubble_all);
110         ImageView bubbleAllImage = (ImageView) holder.findViewById(R.id.bubble_all_icon);
111         TextView bubbleAllText = (TextView) holder.findViewById(R.id.bubble_all_label);
112         mBubbleAllButton = new ButtonViewHolder(bubbleAll, bubbleAllImage, bubbleAllText,
113                 BUBBLE_PREFERENCE_ALL);
114         mBubbleAllButton.setSelected(mContext, mSelectedPreference == BUBBLE_PREFERENCE_ALL);
115         bubbleAll.setTag(BUBBLE_PREFERENCE_ALL);
116         bubbleAll.setOnClickListener(this);
117         bubbleAll.setVisibility(disabledByAdmin ? View.GONE : View.VISIBLE);
118 
119         View bubbleSelected = holder.findViewById(R.id.bubble_selected);
120         ImageView bubbleSelectedImage = (ImageView) holder.findViewById(R.id.bubble_selected_icon);
121         TextView bubbleSelectedText = (TextView) holder.findViewById(R.id.bubble_selected_label);
122         mBubbleSelectedButton = new ButtonViewHolder(bubbleSelected, bubbleSelectedImage,
123                 bubbleSelectedText, BUBBLE_PREFERENCE_SELECTED);
124         mBubbleSelectedButton.setSelected(mContext,
125                 mSelectedPreference == BUBBLE_PREFERENCE_SELECTED);
126         bubbleSelected.setTag(BUBBLE_PREFERENCE_SELECTED);
127         bubbleSelected.setOnClickListener(this);
128         bubbleSelected.setVisibility((!mSelectedVisible || disabledByAdmin)
129                 ? View.GONE : View.VISIBLE);
130 
131         View bubbleNone = holder.findViewById(R.id.bubble_none);
132         ImageView bubbleNoneImage = (ImageView) holder.findViewById(R.id.bubble_none_icon);
133         TextView bubbleNoneText = (TextView) holder.findViewById(R.id.bubble_none_label);
134         mBubbleNoneButton = new ButtonViewHolder(bubbleNone, bubbleNoneImage, bubbleNoneText,
135                 BUBBLE_PREFERENCE_NONE);
136         mBubbleNoneButton.setSelected(mContext, mSelectedPreference == BUBBLE_PREFERENCE_NONE);
137         bubbleNone.setTag(BUBBLE_PREFERENCE_NONE);
138         bubbleNone.setOnClickListener(this);
139         bubbleNone.setVisibility(disabledByAdmin ? View.GONE : View.VISIBLE);
140     }
141 
142     @Override
onClick(View v)143     public void onClick(View v) {
144         final int selected = (int) v.getTag();
145         callChangeListener(selected);
146 
147         mBubbleAllButton.setSelected(mContext, selected == BUBBLE_PREFERENCE_ALL);
148         mBubbleSelectedButton.setSelected(mContext, selected == BUBBLE_PREFERENCE_SELECTED);
149         mBubbleNoneButton.setSelected(mContext, selected == BUBBLE_PREFERENCE_NONE);
150     }
151 
152     private class ButtonViewHolder {
153         private View mView;
154         private ImageView mImageView;
155         private TextView mTextView;
156         private int mId;
157 
ButtonViewHolder(View v, ImageView iv, TextView tv, int identifier)158         ButtonViewHolder(View v, ImageView iv, TextView tv, int identifier) {
159             mView = v;
160             mImageView = iv;
161             mTextView = tv;
162             mId = identifier;
163         }
164 
setSelected(Context context, boolean selected)165         void setSelected(Context context, boolean selected) {
166             mView.setBackground(mContext.getDrawable(selected
167                 ? R.drawable.button_border_selected
168                 : R.drawable.button_border_unselected));
169             mView.setSelected(selected);
170 
171             ColorStateList stateList = selected
172                     ? Utils.getColorAccent(context)
173                     : Utils.getColorAttr(context, android.R.attr.textColorPrimary);
174             mImageView.setImageTintList(stateList);
175             mTextView.setTextColor(stateList);
176         }
177     }
178 }
179