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.tv.twopanelsettings.slices;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 
24 import androidx.preference.CheckBoxPreference;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceGroup;
27 
28 import com.android.tv.twopanelsettings.R;
29 
30 /**
31  * A copy of RadioPreference from TvSettings. Find better solutions in the future(b/141955757).
32  */
33 public class RadioPreference extends CheckBoxPreference {
34     private String mRadioGroup;
35 
RadioPreference(Context context)36     public RadioPreference(Context context) {
37         this(context, null);
38     }
39 
RadioPreference(Context context, AttributeSet attrs)40     public RadioPreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         final TypedArray a =
43                 context.obtainStyledAttributes(attrs, R.styleable.RadioPreference, 0, 0);
44 
45         mRadioGroup = a.getString(R.styleable.RadioPreference_radioGroup);
46 
47         a.recycle();
48 
49         setWidgetLayoutResource(R.layout.radio_preference_widget);
50     }
51 
getRadioGroup()52     public String getRadioGroup() {
53         return mRadioGroup;
54     }
55 
setRadioGroup(String radioGroup)56     public void setRadioGroup(String radioGroup) {
57         mRadioGroup = radioGroup;
58     }
59 
clearOtherRadioPreferences(PreferenceGroup preferenceGroup)60     public void clearOtherRadioPreferences(PreferenceGroup preferenceGroup) {
61         final int count = preferenceGroup.getPreferenceCount();
62         for (int i = 0; i < count; i++) {
63             final Preference p = preferenceGroup.getPreference(i);
64             if (!(p instanceof RadioPreference)) {
65                 continue;
66             }
67             final RadioPreference radioPreference = (RadioPreference) p;
68             if (!TextUtils.equals(getRadioGroup(), radioPreference.getRadioGroup())) {
69                 continue;
70             }
71             if (TextUtils.equals(getKey(), radioPreference.getKey())) {
72                 continue;
73             }
74             radioPreference.setChecked(false);
75         }
76     }
77 }
78