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.settings.widget;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.widget.ImageView;
22 
23 import androidx.preference.PreferenceViewHolder;
24 
25 import com.android.settings.R;
26 import com.android.settingslib.widget.RadioButtonPreference;
27 
28 public class RadioButtonPreferenceWithExtraWidget extends RadioButtonPreference {
29     public static final int EXTRA_WIDGET_VISIBILITY_GONE = 0;
30     public static final int EXTRA_WIDGET_VISIBILITY_INFO = 1;
31     public static final int EXTRA_WIDGET_VISIBILITY_SETTING = 2;
32 
33     private View mExtraWidgetDivider;
34     private ImageView mExtraWidget;
35 
36     private int mExtraWidgetVisibility = EXTRA_WIDGET_VISIBILITY_GONE;
37     private View.OnClickListener mExtraWidgetOnClickListener;
38 
RadioButtonPreferenceWithExtraWidget(Context context)39     public RadioButtonPreferenceWithExtraWidget(Context context) {
40         super(context, null);
41         setLayoutResource(R.layout.preference_radio_with_extra_widget);
42     }
43 
44     @Override
onBindViewHolder(PreferenceViewHolder view)45     public void onBindViewHolder(PreferenceViewHolder view) {
46         super.onBindViewHolder(view);
47 
48         mExtraWidget = (ImageView) view.findViewById(R.id.radio_extra_widget);
49         mExtraWidgetDivider = view.findViewById(R.id.radio_extra_widget_divider);
50         setExtraWidgetVisibility(mExtraWidgetVisibility);
51 
52         if (mExtraWidgetOnClickListener != null) {
53             setExtraWidgetOnClickListener(mExtraWidgetOnClickListener);
54         }
55     }
56 
setExtraWidgetVisibility(int visibility)57     public void setExtraWidgetVisibility(int visibility) {
58         mExtraWidgetVisibility = visibility;
59         if (mExtraWidget == null || mExtraWidgetDivider == null) {
60             return;
61         }
62 
63         if (visibility == EXTRA_WIDGET_VISIBILITY_GONE) {
64             mExtraWidget.setClickable(false);
65             mExtraWidget.setVisibility(View.GONE);
66             mExtraWidgetDivider.setVisibility(View.GONE);
67         } else {
68             mExtraWidget.setClickable(true);
69             mExtraWidget.setVisibility(View.VISIBLE);
70             mExtraWidgetDivider.setVisibility(View.VISIBLE);
71             if (mExtraWidgetVisibility == EXTRA_WIDGET_VISIBILITY_INFO) {
72                 mExtraWidget.setImageResource(R.drawable.ic_settings_about);
73                 mExtraWidget.setContentDescription(
74                         getContext().getResources().getText(R.string.information_label));
75             } else if (mExtraWidgetVisibility == EXTRA_WIDGET_VISIBILITY_SETTING) {
76                 mExtraWidget.setImageResource(R.drawable.ic_settings_accent);
77                 mExtraWidget.setContentDescription(
78                         getContext().getResources().getText(R.string.settings_label));
79             }
80         }
81     }
82 
setExtraWidgetOnClickListener(View.OnClickListener listener)83     public void setExtraWidgetOnClickListener(View.OnClickListener listener) {
84         mExtraWidgetOnClickListener = listener;
85         if (mExtraWidget != null) {
86             mExtraWidget.setEnabled(true);
87             mExtraWidget.setOnClickListener(listener);
88         }
89     }
90 }