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