Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2017 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.support.v4.content.res.TypedArrayUtils;
     21 import android.support.v7.preference.CheckBoxPreference;
     22 import android.support.v7.preference.PreferenceViewHolder;
     23 import android.text.TextUtils;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.widget.TextView;
     27 
     28 import com.android.settings.R;
     29 
     30 /**
     31  * Check box preference with check box replaced by radio button.
     32  *
     33  * Functionally speaking, it's actually a CheckBoxPreference. We only modified
     34  * the widget to RadioButton to make it "look like" a RadioButtonPreference.
     35  *
     36  * In other words, there's no "RadioButtonPreferenceGroup" in this
     37  * implementation. When you check one RadioButtonPreference, if you want to
     38  * uncheck all the other preferences, you should do that by code yourself.
     39  */
     40 public class RadioButtonPreference extends CheckBoxPreference {
     41     public interface OnClickListener {
     42        void onRadioButtonClicked(RadioButtonPreference emiter);
     43     }
     44 
     45     private OnClickListener mListener = null;
     46 
     47     public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) {
     48         super(context, attrs, defStyle);
     49         setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
     50         setLayoutResource(R.layout.preference_radio);
     51         setIconSpaceReserved(false);
     52     }
     53 
     54     public RadioButtonPreference(Context context, AttributeSet attrs) {
     55         this(context, attrs, TypedArrayUtils.getAttr(context,
     56                 android.support.v7.preference.R.attr.preferenceStyle,
     57                 android.R.attr.preferenceStyle));
     58     }
     59 
     60     public RadioButtonPreference(Context context) {
     61         this(context, null);
     62     }
     63 
     64     public void setOnClickListener(OnClickListener listener) {
     65         mListener = listener;
     66     }
     67 
     68     @Override
     69     public void onClick() {
     70         if (mListener != null) {
     71             mListener.onRadioButtonClicked(this);
     72         }
     73     }
     74 
     75     @Override
     76     public void onBindViewHolder(PreferenceViewHolder view) {
     77         super.onBindViewHolder(view);
     78 
     79         View summaryContainer = view.findViewById(R.id.summary_container);
     80         if (summaryContainer != null) {
     81             summaryContainer.setVisibility(
     82                 TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE);
     83         }
     84 
     85         TextView title = (TextView) view.findViewById(android.R.id.title);
     86         if (title != null) {
     87             title.setSingleLine(false);
     88             title.setMaxLines(3);
     89         }
     90     }
     91 }
     92