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.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.TextView;
24 
25 import androidx.core.content.res.TypedArrayUtils;
26 import androidx.preference.CheckBoxPreference;
27 import androidx.preference.PreferenceViewHolder;
28 
29 import com.android.car.developeroptions.R;
30 
31 /**
32  * Check box preference with check box replaced by radio button.
33  *
34  * Functionally speaking, it's actually a CheckBoxPreference. We only modified
35  * the widget to RadioButton to make it "look like" a RadioButtonPreference.
36  *
37  * In other words, there's no "RadioButtonPreferenceGroup" in this
38  * implementation. When you check one RadioButtonPreference, if you want to
39  * uncheck all the other preferences, you should do that by code yourself.
40  */
41 public class RadioButtonPreference extends CheckBoxPreference {
42     public interface OnClickListener {
onRadioButtonClicked(RadioButtonPreference emiter)43        void onRadioButtonClicked(RadioButtonPreference emiter);
44     }
45 
46     private OnClickListener mListener = null;
47     private View appendix;
48     private int appendixVisibility = -1;
49 
RadioButtonPreference(Context context, AttributeSet attrs, int defStyle)50     public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) {
51         super(context, attrs, defStyle);
52         setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
53         setLayoutResource(R.layout.preference_radio);
54         setIconSpaceReserved(false);
55     }
56 
RadioButtonPreference(Context context, AttributeSet attrs)57     public RadioButtonPreference(Context context, AttributeSet attrs) {
58         this(context, attrs, TypedArrayUtils.getAttr(context,
59                 androidx.preference.R.attr.preferenceStyle,
60                 android.R.attr.preferenceStyle));
61     }
62 
RadioButtonPreference(Context context)63     public RadioButtonPreference(Context context) {
64         this(context, null);
65     }
66 
setOnClickListener(OnClickListener listener)67     public void setOnClickListener(OnClickListener listener) {
68         mListener = listener;
69     }
70 
71     @Override
onClick()72     public void onClick() {
73         if (mListener != null) {
74             mListener.onRadioButtonClicked(this);
75         }
76     }
77 
78     @Override
onBindViewHolder(PreferenceViewHolder view)79     public void onBindViewHolder(PreferenceViewHolder view) {
80         super.onBindViewHolder(view);
81 
82         View summaryContainer = view.findViewById(R.id.summary_container);
83         if (summaryContainer != null) {
84             summaryContainer.setVisibility(
85                 TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE);
86             appendix = view.findViewById(R.id.appendix);
87             if (appendix != null && appendixVisibility != -1) {
88                 appendix.setVisibility(appendixVisibility);
89             }
90         }
91 
92         TextView title = (TextView) view.findViewById(android.R.id.title);
93         if (title != null) {
94             title.setSingleLine(false);
95             title.setMaxLines(3);
96         }
97     }
98 
setAppendixVisibility(int visibility)99     public void setAppendixVisibility(int visibility) {
100         if (appendix != null) {
101             appendix.setVisibility(visibility);
102         }
103         appendixVisibility = visibility;
104     }
105 }
106