• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.settings;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.support.v7.preference.CheckBoxPreference;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceGroup;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 
27 public class RadioPreference extends CheckBoxPreference {
28     private String mRadioGroup;
29 
RadioPreference(Context context)30     public RadioPreference(Context context) {
31         this(context, null);
32     }
33 
RadioPreference(Context context, AttributeSet attrs)34     public RadioPreference(Context context, AttributeSet attrs) {
35         super(context, attrs);
36         final TypedArray a =
37                 context.obtainStyledAttributes(attrs, R.styleable.RadioPreference, 0, 0);
38 
39         mRadioGroup = a.getString(R.styleable.RadioPreference_radioGroup);
40 
41         a.recycle();
42 
43         setWidgetLayoutResource(R.layout.radio_preference_widget);
44     }
45 
getRadioGroup()46     public String getRadioGroup() {
47         return mRadioGroup;
48     }
49 
setRadioGroup(String radioGroup)50     public void setRadioGroup(String radioGroup) {
51         mRadioGroup = radioGroup;
52     }
53 
clearOtherRadioPreferences(PreferenceGroup preferenceGroup)54     public void clearOtherRadioPreferences(PreferenceGroup preferenceGroup) {
55         final int count = preferenceGroup.getPreferenceCount();
56         for (int i = 0; i < count; i++) {
57             final Preference p = preferenceGroup.getPreference(i);
58             if (!(p instanceof RadioPreference)) {
59                 continue;
60             }
61             final RadioPreference radioPreference = (RadioPreference) p;
62             if (!TextUtils.equals(getRadioGroup(), radioPreference.getRadioGroup())) {
63                 continue;
64             }
65             if (TextUtils.equals(getKey(), radioPreference.getKey())) {
66                 continue;
67             }
68             radioPreference.setChecked(false);
69         }
70     }
71 }
72