Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2018 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.notification;
     18 
     19 import android.content.Context;
     20 import android.support.v7.preference.PreferenceViewHolder;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.widget.RadioButton;
     24 
     25 import com.android.settings.R;
     26 import com.android.settingslib.TwoTargetPreference;
     27 
     28 /**
     29  * A radio button preference with a divider and a settings icon that links to another screen.
     30  */
     31 public class ZenCustomRadioButtonPreference extends TwoTargetPreference
     32         implements View.OnClickListener {
     33 
     34     private RadioButton mButton;
     35     private boolean mChecked;
     36 
     37     private OnGearClickListener mOnGearClickListener;
     38     private OnRadioButtonClickListener mOnRadioButtonClickListener;
     39 
     40     public ZenCustomRadioButtonPreference(Context context, AttributeSet attrs,
     41             int defStyleAttr, int defStyleRes) {
     42         super(context, attrs, defStyleAttr, defStyleRes);
     43         setLayoutResource(R.layout.preference_two_target_radio);
     44     }
     45 
     46     public ZenCustomRadioButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     47         super(context, attrs, defStyleAttr);
     48         setLayoutResource(R.layout.preference_two_target_radio);
     49     }
     50 
     51     public ZenCustomRadioButtonPreference(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53         setLayoutResource(R.layout.preference_two_target_radio);
     54     }
     55 
     56     public ZenCustomRadioButtonPreference(Context context) {
     57         super(context);
     58         setLayoutResource(R.layout.preference_two_target_radio);
     59     }
     60 
     61     @Override
     62     protected int getSecondTargetResId() {
     63         return R.layout.preference_widget_gear;
     64     }
     65 
     66     public void setOnGearClickListener(OnGearClickListener l) {
     67         mOnGearClickListener = l;
     68         notifyChanged();
     69     }
     70 
     71     public void setOnRadioButtonClickListener(OnRadioButtonClickListener l) {
     72         mOnRadioButtonClickListener = l;
     73         notifyChanged();
     74     }
     75 
     76     @Override
     77     public void onBindViewHolder(PreferenceViewHolder holder) {
     78         super.onBindViewHolder(holder);
     79         View buttonFrame = holder.findViewById(R.id.checkbox_frame);
     80         if (buttonFrame != null) {
     81             buttonFrame.setOnClickListener(this);
     82         }
     83         mButton = (RadioButton) holder.findViewById(android.R.id.checkbox);
     84         if (mButton != null) {
     85             mButton.setChecked(mChecked);
     86         }
     87 
     88         final View gear = holder.findViewById(android.R.id.widget_frame);
     89         final View divider = holder.findViewById(R.id.two_target_divider);
     90         if (mOnGearClickListener != null) {
     91             divider.setVisibility(View.VISIBLE);
     92             gear.setVisibility(View.VISIBLE);
     93             gear.setOnClickListener(this);
     94         } else {
     95             divider.setVisibility(View.GONE);
     96             gear.setVisibility(View.GONE);
     97             gear.setOnClickListener(null);
     98         }
     99     }
    100 
    101     public boolean isChecked() {
    102         return mButton != null && mChecked;
    103     }
    104 
    105     public void setChecked(boolean checked) {
    106         mChecked = checked;
    107         if (mButton != null) {
    108             mButton.setChecked(checked);
    109         }
    110     }
    111 
    112     public RadioButton getRadioButton() {
    113         return mButton;
    114     }
    115 
    116     @Override
    117     public void onClick() {
    118         if (mOnRadioButtonClickListener != null) {
    119             mOnRadioButtonClickListener.onRadioButtonClick(this);
    120         }
    121     }
    122 
    123     @Override
    124     public void onClick(View v) {
    125         if (v.getId() == android.R.id.widget_frame) {
    126             if (mOnGearClickListener != null) {
    127                 mOnGearClickListener.onGearClick(this);
    128             }
    129         } else if (v.getId() == R.id.checkbox_frame) {
    130             if (mOnRadioButtonClickListener != null) {
    131                 mOnRadioButtonClickListener.onRadioButtonClick(this);
    132             }
    133         }
    134     }
    135 
    136     public interface OnGearClickListener {
    137         void onGearClick(ZenCustomRadioButtonPreference p);
    138     }
    139 
    140     public interface OnRadioButtonClickListener {
    141         void onRadioButtonClick(ZenCustomRadioButtonPreference p);
    142     }
    143 }
    144