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.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 public class RadioPreference extends CheckBoxPreference { 29 private String mRadioGroup; 30 RadioPreference(Context context)31 public RadioPreference(Context context) { 32 this(context, null); 33 } 34 RadioPreference(Context context, AttributeSet attrs)35 public RadioPreference(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 final TypedArray a = 38 context.obtainStyledAttributes(attrs, R.styleable.RadioPreference, 0, 0); 39 40 mRadioGroup = a.getString(R.styleable.RadioPreference_radioGroup); 41 42 a.recycle(); 43 44 setWidgetLayoutResource(R.layout.radio_preference_widget); 45 } 46 getRadioGroup()47 public String getRadioGroup() { 48 return mRadioGroup; 49 } 50 setRadioGroup(String radioGroup)51 public void setRadioGroup(String radioGroup) { 52 mRadioGroup = radioGroup; 53 } 54 clearOtherRadioPreferences(PreferenceGroup preferenceGroup)55 public void clearOtherRadioPreferences(PreferenceGroup preferenceGroup) { 56 final int count = preferenceGroup.getPreferenceCount(); 57 for (int i = 0; i < count; i++) { 58 final Preference p = preferenceGroup.getPreference(i); 59 if (!(p instanceof RadioPreference)) { 60 continue; 61 } 62 final RadioPreference radioPreference = (RadioPreference) p; 63 if (!TextUtils.equals(getRadioGroup(), radioPreference.getRadioGroup())) { 64 continue; 65 } 66 if (TextUtils.equals(getKey(), radioPreference.getKey())) { 67 continue; 68 } 69 radioPreference.setChecked(false); 70 } 71 } 72 } 73