1 /*
2  * Copyright (C) 2023 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.applications.appcompat;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.view.View;
23 
24 import androidx.preference.CheckBoxPreference;
25 import androidx.preference.PreferenceViewHolder;
26 
27 import com.android.settings.R;
28 
29 /**
30  * Radio button preference with image at the bottom.
31  *
32  * <p>Layout should stay the same as
33  * {@link com.android.settingslib.widget.SelectorWithWidgetPreference} for consistency.
34  */
35 public class RadioWithImagePreference extends CheckBoxPreference {
36 
37     /**
38      * Interface definition for a callback to be invoked when the preference is clicked.
39      */
40     public interface OnClickListener {
41         /**
42          * Called when a preference has been clicked.
43          *
44          * @param emiter The clicked preference
45          */
onRadioButtonClicked(RadioWithImagePreference emiter)46         void onRadioButtonClicked(RadioWithImagePreference emiter);
47     }
48 
49     private OnClickListener mListener = null;
50 
51     /**
52      * Performs inflation from XML and apply a class-specific base style.
53      *
54      * @param context  The {@link Context} this is associated with, through which it can
55      *                 access the current theme, resources, {@link SharedPreferences}, etc.
56      * @param attrs    The attributes of the XML tag that is inflating the preference
57      * @param defStyle An attribute in the current theme that contains a reference to a style
58      *                 resource that supplies default values for the view. Can be 0 to not
59      *                 look for defaults.
60      */
RadioWithImagePreference(Context context, AttributeSet attrs, int defStyle)61     public RadioWithImagePreference(Context context, AttributeSet attrs, int defStyle) {
62         super(context, attrs, defStyle);
63         init();
64     }
65 
66     /**
67      * Performs inflation from XML and apply a class-specific base style.
68      *
69      * @param context The {@link Context} this is associated with, through which it can
70      *                access the current theme, resources, {@link SharedPreferences}, etc.
71      * @param attrs   The attributes of the XML tag that is inflating the preference
72      */
RadioWithImagePreference(Context context, AttributeSet attrs)73     public RadioWithImagePreference(Context context, AttributeSet attrs) {
74         super(context, attrs);
75         init();
76     }
77 
78     /**
79      * Constructor to create a preference.
80      *
81      * @param context The Context this is associated with.
82      */
RadioWithImagePreference(Context context)83     public RadioWithImagePreference(Context context) {
84         this(context, null);
85     }
86 
87     /**
88      * Sets the callback to be invoked when this preference is clicked by the user.
89      *
90      * @param listener The callback to be invoked
91      */
setOnClickListener(OnClickListener listener)92     public void setOnClickListener(OnClickListener listener) {
93         mListener = listener;
94     }
95 
96     /**
97      * Processes a click on the preference.
98      */
99     @Override
onClick()100     public void onClick() {
101         if (mListener != null) {
102             mListener.onRadioButtonClicked(this);
103         }
104     }
105 
106     /**
107      * Binds the created View to the data for this preference.
108      *
109      * <p>This is a good place to grab references to custom Views in the layout and set
110      * properties on them.
111      *
112      * <p>Make sure to call through to the superclass's implementation.
113      *
114      * @param holder The ViewHolder that provides references to the views to fill in. These views
115      *               will be recycled, so you should not hold a reference to them after this method
116      *               returns.
117      */
118     @Override
onBindViewHolder(PreferenceViewHolder holder)119     public void onBindViewHolder(PreferenceViewHolder holder) {
120         super.onBindViewHolder(holder);
121 
122         View summaryContainer = holder.findViewById(R.id.summary_container);
123         if (summaryContainer != null) {
124             summaryContainer.setVisibility(
125                     TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE);
126         }
127     }
128 
init()129     private void init() {
130         setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
131         setLayoutResource(R.layout.radio_with_image_preference);
132         setIconSpaceReserved(false);
133     }
134 }
135