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