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.settingslib.widget; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.widget.ImageView; 24 25 import androidx.preference.CheckBoxPreference; 26 import androidx.preference.PreferenceViewHolder; 27 28 /** 29 * DEPRECATED. Please use SelectorWithWidgetPreference instead. 30 * 31 * This file has been moved there and will be removed once all callers are updated. 32 * 33 * Check box preference with check box replaced by radio button. 34 * 35 * Functionally speaking, it's actually a CheckBoxPreference. We only modified 36 * the widget to RadioButton to make it "look like" a RadioButtonPreference. 37 * 38 * In other words, there's no "RadioButtonPreferenceGroup" in this 39 * implementation. When you check one RadioButtonPreference, if you want to 40 * uncheck all the other preferences, you should do that by code yourself. 41 * 42 * RadioButtonPreference can assign a extraWidgetListener to show a gear icon 43 * on the right side that can open another page. 44 * 45 * @Deprecated 46 */ 47 public class RadioButtonPreference extends CheckBoxPreference { 48 49 /** 50 * Interface definition for a callback to be invoked when the preference is clicked. 51 */ 52 public interface OnClickListener { 53 /** 54 * Called when a preference has been clicked. 55 * 56 * @param emiter The clicked preference 57 */ onRadioButtonClicked(RadioButtonPreference emiter)58 void onRadioButtonClicked(RadioButtonPreference emiter); 59 } 60 61 private OnClickListener mListener = null; 62 private View mAppendix; 63 private int mAppendixVisibility = -1; 64 65 private View mExtraWidgetContainer; 66 private ImageView mExtraWidget; 67 68 private View.OnClickListener mExtraWidgetOnClickListener; 69 70 /** 71 * Perform inflation from XML and apply a class-specific base style. 72 * 73 * @param context The {@link Context} this is associated with, through which it can 74 * access the current theme, resources, {@link SharedPreferences}, etc. 75 * @param attrs The attributes of the XML tag that is inflating the preference 76 * @param defStyle An attribute in the current theme that contains a reference to a style 77 * resource that supplies default values for the view. Can be 0 to not 78 * look for defaults. 79 */ RadioButtonPreference(Context context, AttributeSet attrs, int defStyle)80 public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) { 81 super(context, attrs, defStyle); 82 init(); 83 } 84 85 /** 86 * Perform inflation from XML and apply a class-specific base style. 87 * 88 * @param context The {@link Context} this is associated with, through which it can 89 * access the current theme, resources, {@link SharedPreferences}, etc. 90 * @param attrs The attributes of the XML tag that is inflating the preference 91 */ RadioButtonPreference(Context context, AttributeSet attrs)92 public RadioButtonPreference(Context context, AttributeSet attrs) { 93 super(context, attrs); 94 init(); 95 } 96 97 /** 98 * Constructor to create a preference. 99 * 100 * @param context The Context this is associated with. 101 */ RadioButtonPreference(Context context)102 public RadioButtonPreference(Context context) { 103 this(context, null); 104 } 105 106 /** 107 * Sets the callback to be invoked when this preference is clicked by the user. 108 * 109 * @param listener The callback to be invoked 110 */ setOnClickListener(OnClickListener listener)111 public void setOnClickListener(OnClickListener listener) { 112 mListener = listener; 113 } 114 115 /** 116 * Processes a click on the preference. 117 */ 118 @Override onClick()119 public void onClick() { 120 if (mListener != null) { 121 mListener.onRadioButtonClicked(this); 122 } 123 } 124 125 /** 126 * Binds the created View to the data for this preference. 127 * 128 * <p>This is a good place to grab references to custom Views in the layout and set 129 * properties on them. 130 * 131 * <p>Make sure to call through to the superclass's implementation. 132 * 133 * @param holder The ViewHolder that provides references to the views to fill in. These views 134 * will be recycled, so you should not hold a reference to them after this method 135 * returns. 136 */ 137 @Override onBindViewHolder(PreferenceViewHolder holder)138 public void onBindViewHolder(PreferenceViewHolder holder) { 139 super.onBindViewHolder(holder); 140 141 View summaryContainer = holder.findViewById(R.id.summary_container); 142 if (summaryContainer != null) { 143 summaryContainer.setVisibility( 144 TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE); 145 mAppendix = holder.findViewById(R.id.appendix); 146 if (mAppendix != null && mAppendixVisibility != -1) { 147 mAppendix.setVisibility(mAppendixVisibility); 148 } 149 } 150 151 mExtraWidget = (ImageView) holder.findViewById(R.id.radio_extra_widget); 152 mExtraWidgetContainer = holder.findViewById(R.id.radio_extra_widget_container); 153 154 setExtraWidgetOnClickListener(mExtraWidgetOnClickListener); 155 } 156 157 /** 158 * Set the visibility state of appendix view. 159 * 160 * @param visibility One of {@link View#VISIBLE}, {@link View#INVISIBLE}, or {@link View#GONE}. 161 */ setAppendixVisibility(int visibility)162 public void setAppendixVisibility(int visibility) { 163 if (mAppendix != null) { 164 mAppendix.setVisibility(visibility); 165 } 166 mAppendixVisibility = visibility; 167 } 168 169 /** 170 * Sets the callback to be invoked when extra widget is clicked by the user. 171 * 172 * @param listener The callback to be invoked 173 */ setExtraWidgetOnClickListener(View.OnClickListener listener)174 public void setExtraWidgetOnClickListener(View.OnClickListener listener) { 175 mExtraWidgetOnClickListener = listener; 176 177 if (mExtraWidget == null || mExtraWidgetContainer == null) { 178 return; 179 } 180 181 mExtraWidget.setOnClickListener(mExtraWidgetOnClickListener); 182 183 mExtraWidgetContainer.setVisibility((mExtraWidgetOnClickListener != null) 184 ? View.VISIBLE : View.GONE); 185 } 186 init()187 private void init() { 188 setWidgetLayoutResource(R.layout.preference_widget_radiobutton); 189 setLayoutResource(R.layout.preference_radio); 190 setIconSpaceReserved(false); 191 } 192 } 193