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.util.AttributeSet; 25 import android.view.View; 26 import android.widget.RadioButton; 27 import android.widget.RadioGroup; 28 29 import androidx.annotation.NonNull; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceViewHolder; 32 33 import com.android.settings.R; 34 import com.android.settingslib.RestrictedLockUtils; 35 import com.android.settingslib.RestrictedPreferenceHelper; 36 37 /** 38 * A tri-state preference allowing a user to specify what gets to bubble. 39 */ 40 public class BubblePreference extends Preference implements RadioGroup.OnCheckedChangeListener { 41 RestrictedPreferenceHelper mHelper; 42 43 private int mSelectedPreference; 44 45 private boolean mSelectedVisible; 46 BubblePreference(Context context)47 public BubblePreference(Context context) { 48 this(context, null); 49 } 50 BubblePreference(Context context, AttributeSet attrs)51 public BubblePreference(Context context, AttributeSet attrs) { 52 this(context, attrs, 0); 53 } 54 BubblePreference(Context context, AttributeSet attrs, int defStyleAttr)55 public BubblePreference(Context context, AttributeSet attrs, int defStyleAttr) { 56 this(context, attrs, defStyleAttr, 0); 57 } 58 BubblePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)59 public BubblePreference(Context context, AttributeSet attrs, 60 int defStyleAttr, int defStyleRes) { 61 super(context, attrs, defStyleAttr, defStyleRes); 62 mHelper = new RestrictedPreferenceHelper(context, this, attrs); 63 mHelper.useAdminDisabledSummary(true); 64 setLayoutResource(R.layout.bubble_preference); 65 } 66 setSelectedPreference(int preference)67 public void setSelectedPreference(int preference) { 68 mSelectedPreference = preference; 69 notifyChanged(); 70 } 71 getSelectedPreference()72 public int getSelectedPreference() { 73 return mSelectedPreference; 74 } 75 setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin)76 public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) { 77 if (mHelper.setDisabledByAdmin(admin)) { 78 notifyChanged(); 79 } 80 } 81 setSelectedVisibility(boolean visible)82 public void setSelectedVisibility(boolean visible) { 83 if (mSelectedVisible != visible) { 84 mSelectedVisible = visible; 85 notifyChanged(); 86 } 87 } 88 89 @Override onBindViewHolder(@onNull final PreferenceViewHolder holder)90 public void onBindViewHolder(@NonNull final PreferenceViewHolder holder) { 91 super.onBindViewHolder(holder); 92 final boolean disabledByAdmin = mHelper.isDisabledByAdmin(); 93 View summary = holder.findViewById(android.R.id.summary); 94 if (disabledByAdmin) { 95 mHelper.onBindViewHolder(holder); 96 summary.setVisibility(View.VISIBLE); 97 } else { 98 summary.setVisibility(View.GONE); 99 } 100 holder.itemView.setClickable(false); 101 102 RadioButton bubbleAllButton = (RadioButton) holder.findViewById(R.id.bubble_all); 103 bubbleAllButton.setChecked(mSelectedPreference == BUBBLE_PREFERENCE_ALL); 104 bubbleAllButton.setTag(BUBBLE_PREFERENCE_ALL); 105 bubbleAllButton.setVisibility(disabledByAdmin ? View.GONE : View.VISIBLE); 106 107 RadioButton bubbleSelectedButton = (RadioButton) holder.findViewById(R.id.bubble_selected); 108 bubbleSelectedButton.setChecked(mSelectedPreference == BUBBLE_PREFERENCE_SELECTED); 109 bubbleSelectedButton.setTag(BUBBLE_PREFERENCE_SELECTED); 110 int selectedButtonVisibility = 111 (!mSelectedVisible || disabledByAdmin) ? View.GONE : View.VISIBLE; 112 bubbleSelectedButton.setVisibility(selectedButtonVisibility); 113 114 RadioButton bubbleNoneButton = (RadioButton) holder.findViewById(R.id.bubble_none); 115 bubbleNoneButton.setChecked(mSelectedPreference == BUBBLE_PREFERENCE_NONE); 116 bubbleNoneButton.setTag(BUBBLE_PREFERENCE_NONE); 117 bubbleNoneButton.setVisibility(disabledByAdmin ? View.GONE : View.VISIBLE); 118 119 RadioGroup bublesRadioGroup = (RadioGroup) holder.findViewById(R.id.radio_group); 120 bublesRadioGroup.setOnCheckedChangeListener(this); 121 } 122 123 @Override onCheckedChanged(@onNull RadioGroup group, int checkedId)124 public void onCheckedChanged(@NonNull RadioGroup group, int checkedId) { 125 View v = group.findViewById(checkedId); 126 if (v == null || v.getTag() == null) { 127 return; 128 } 129 int selectedTag = (int) v.getTag(); 130 callChangeListener(selectedTag); 131 } 132 } 133