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