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.car.settings.common;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.View;
23 
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.car.settings.R;
27 import com.android.car.ui.preference.CarUiPreference;
28 
29 /**
30  * A preference which can perform two actions. The secondary action is shown by default.
31  * {@link #showAction(boolean)} may be used to manually set the visibility of the action.
32  */
33 public abstract class TwoActionPreference extends CarUiPreference {
34 
35     private boolean mIsActionShown;
36 
TwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)37     public TwoActionPreference(Context context, AttributeSet attrs,
38             int defStyleAttr, int defStyleRes) {
39         super(context, attrs, defStyleAttr, defStyleRes);
40         init(attrs);
41     }
42 
TwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr)43     public TwoActionPreference(Context context, AttributeSet attrs, int defStyleAttr) {
44         super(context, attrs, defStyleAttr);
45         init(attrs);
46     }
47 
TwoActionPreference(Context context, AttributeSet attrs)48     public TwoActionPreference(Context context, AttributeSet attrs) {
49         super(context, attrs);
50         init(attrs);
51     }
52 
TwoActionPreference(Context context)53     public TwoActionPreference(Context context) {
54         super(context);
55         init(/* attrs= */ null);
56     }
57 
init(AttributeSet attrs)58     private void init(AttributeSet attrs) {
59         setLayoutResource(R.layout.two_action_preference);
60         TypedArray preferenceAttributes = getContext().obtainStyledAttributes(attrs,
61                 R.styleable.TwoActionPreference);
62         mIsActionShown = preferenceAttributes.getBoolean(
63                 R.styleable.TwoActionPreference_actionShown, true);
64         setShowChevron(false);
65     }
66 
67     /**
68      * Sets whether the secondary action is visible in the preference.
69      *
70      * @param isShown {@code true} if the secondary action should be shown.
71      */
showAction(boolean isShown)72     public void showAction(boolean isShown) {
73         mIsActionShown = isShown;
74         notifyChanged();
75     }
76 
77     /** Returns {@code true} if action is shown. */
isActionShown()78     public boolean isActionShown() {
79         return mIsActionShown;
80     }
81 
82     @Override
onBindViewHolder(PreferenceViewHolder holder)83     public void onBindViewHolder(PreferenceViewHolder holder) {
84         super.onBindViewHolder(holder);
85         View actionContainer = holder.findViewById(R.id.action_widget_container);
86         View widgetFrame = holder.findViewById(android.R.id.widget_frame);
87         if (mIsActionShown) {
88             actionContainer.setVisibility(View.VISIBLE);
89             onBindWidgetFrame(widgetFrame);
90         } else {
91             actionContainer.setVisibility(View.GONE);
92         }
93     }
94 
95     /**
96      * Binds the created View for the second action.
97      *
98      * <p>This is a good place to set properties on any custom view.
99      *
100      * @param widgetFrame The widget frame which controls the 2nd action.
101      */
onBindWidgetFrame(View widgetFrame)102     protected abstract void onBindWidgetFrame(View widgetFrame);
103 }
104