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