1 /* 2 * Copyright 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.car.settings.common; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 23 import androidx.preference.Preference; 24 25 /** 26 * {@link Preference} with a secondary clickable button on the side. 27 * {@link #setLayoutResource(int)} or the {@code widgetLayout} resource may be used to specify 28 * the icon to display in the button. 29 * 30 * <p>Note: the button is enabled even when {@link #isEnabled()} is {@code false}. 31 */ 32 public class ButtonPreference extends TwoActionPreference { 33 34 /** 35 * Interface definition for a callback to be invoked when the button is clicked. 36 */ 37 public interface OnButtonClickListener { 38 /** 39 * Called when a button has been clicked. 40 * 41 * @param preference the preference whose button was clicked. 42 */ onButtonClick(ButtonPreference preference)43 void onButtonClick(ButtonPreference preference); 44 } 45 46 private OnButtonClickListener mOnButtonClickListener; 47 ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48 public ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr, 49 int defStyleRes) { 50 super(context, attrs, defStyleAttr, defStyleRes); 51 } 52 ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr)53 public ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) { 54 super(context, attrs, defStyleAttr); 55 } 56 ButtonPreference(Context context, AttributeSet attrs)57 public ButtonPreference(Context context, AttributeSet attrs) { 58 super(context, attrs); 59 } 60 ButtonPreference(Context context)61 public ButtonPreference(Context context) { 62 super(context); 63 } 64 65 /** 66 * Sets an {@link OnButtonClickListener} to be invoked when the button is clicked. 67 */ setOnButtonClickListener(OnButtonClickListener listener)68 public void setOnButtonClickListener(OnButtonClickListener listener) { 69 mOnButtonClickListener = listener; 70 } 71 72 /** Virtually clicks the button contained inside this preference. */ performButtonClick()73 public void performButtonClick() { 74 if (isActionShown()) { 75 if (mOnButtonClickListener != null) { 76 mOnButtonClickListener.onButtonClick(this); 77 } 78 } 79 } 80 81 @Override onBindWidgetFrame(View widgetFrame)82 protected void onBindWidgetFrame(View widgetFrame) { 83 widgetFrame.setOnClickListener(v -> performButtonClick()); 84 } 85 } 86