• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.car.settings.R;
29 import com.android.car.ui.preference.CarUiPreference;
30 
31 /**
32  * Preference that provides a four button layout.
33  * -----------------------------------------
34  * | button1 | button2 | button3 | button4 |
35  * -----------------------------------------
36  *
37  * Adapted from {@link com.android.settingslib.widget.ActionButtonsPreference} for CarSettings.
38  *
39  * This widget also has custom divider elements above and below, which can be customized with the
40  * Preference_allowDividerAbove and Preference_allowDividerBelow attributes. The dividers are
41  * enabled by default.
42  */
43 public class ActionButtonsPreference extends CarUiPreference implements
44         ActionButtonInfo.ButtonInfoChangeListener {
45 
46     /**
47      * Identifier enum for the four different action buttons.
48      */
49     public enum ActionButtons {
50         BUTTON1,
51         BUTTON2,
52         BUTTON3,
53         BUTTON4
54     }
55 
56     private ActionButtonInfo mButton1Info;
57     private ActionButtonInfo mButton2Info;
58     private ActionButtonInfo mButton3Info;
59     private ActionButtonInfo mButton4Info;
60 
61     private boolean mAllowDividerAbove;
62     private boolean mAllowDividerBelow;
63 
ActionButtonsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)64     public ActionButtonsPreference(Context context, AttributeSet attrs,
65             int defStyleAttr, int defStyleRes) {
66         super(context, attrs, defStyleAttr, defStyleRes);
67         init(context, attrs);
68     }
69 
ActionButtonsPreference(Context context, AttributeSet attrs, int defStyleAttr)70     public ActionButtonsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
71         super(context, attrs, defStyleAttr);
72         init(context, attrs);
73     }
74 
ActionButtonsPreference(Context context, AttributeSet attrs)75     public ActionButtonsPreference(Context context, AttributeSet attrs) {
76         super(context, attrs);
77         init(context, attrs);
78     }
79 
ActionButtonsPreference(Context context)80     public ActionButtonsPreference(Context context) {
81         super(context);
82         init(context, /* attrs= */ null);
83     }
84 
init(Context context, AttributeSet attrs)85     private void init(Context context, AttributeSet attrs) {
86         setLayoutResource(R.layout.action_buttons_preference);
87         setSelectable(false);
88 
89         TypedArray a = context.obtainStyledAttributes(attrs,
90                 R.styleable.Preference);
91         mAllowDividerAbove = a.getBoolean(R.styleable.Preference_allowDividerAbove,
92                 /* defValue= */ true);
93         mAllowDividerBelow = a.getBoolean(R.styleable.Preference_allowDividerBelow,
94                 /* defValue= */ true);
95         mButton1Info = new ActionButtonInfo(context, /* changeListener= */ this);
96         mButton2Info = new ActionButtonInfo(context, /* changeListener= */ this);
97         mButton3Info = new ActionButtonInfo(context, /* changeListener= */ this);
98         mButton4Info = new ActionButtonInfo(context, /* changeListener= */ this);
99     }
100 
101     @Override
onBindViewHolder(PreferenceViewHolder holder)102     public void onBindViewHolder(PreferenceViewHolder holder) {
103         super.onBindViewHolder(holder);
104         holder.findViewById(R.id.topDivider).setVisibility(
105                 mAllowDividerAbove ? View.VISIBLE : View.GONE);
106         holder.findViewById(R.id.bottomDivider).setVisibility(
107                 mAllowDividerBelow ? View.VISIBLE : View.GONE);
108 
109         mButton1Info
110                 .setButtonView(holder.findViewById(R.id.button1))
111                 .setButtonTextView((TextView) holder.findViewById(R.id.button1Text))
112                 .setButtonIconView((ImageView) holder.findViewById(R.id.button1Icon))
113                 .setPreferenceRestricted(isUxRestricted());
114 
115         mButton2Info
116                 .setButtonView(holder.findViewById(R.id.button2))
117                 .setButtonTextView((TextView) holder.findViewById(R.id.button2Text))
118                 .setButtonIconView((ImageView) holder.findViewById(R.id.button2Icon))
119                 .setPreferenceRestricted(isUxRestricted());
120 
121         mButton3Info
122                 .setButtonView(holder.findViewById(R.id.button3))
123                 .setButtonTextView((TextView) holder.findViewById(R.id.button3Text))
124                 .setButtonIconView((ImageView) holder.findViewById(R.id.button3Icon))
125                 .setPreferenceRestricted(isUxRestricted());
126 
127         mButton4Info
128                 .setButtonView(holder.findViewById(R.id.button4))
129                 .setButtonTextView((TextView) holder.findViewById(R.id.button4Text))
130                 .setButtonIconView((ImageView) holder.findViewById(R.id.button4Icon))
131                 .setPreferenceRestricted(isUxRestricted());
132 
133         mButton1Info.setUpButton();
134         mButton2Info.setUpButton();
135         mButton3Info.setUpButton();
136         mButton4Info.setUpButton();
137     }
138 
139     @Override
onButtonInfoChange(ActionButtonInfo buttonInfo)140     public void onButtonInfoChange(ActionButtonInfo buttonInfo) {
141         notifyChanged();
142     }
143 
144     /**
145      * Retrieve the specified ActionButtonInfo based on the ActionButtons enum.
146      */
getButton(ActionButtons button)147     public ActionButtonInfo getButton(ActionButtons button) {
148         switch(button) {
149             case BUTTON1:
150                 return mButton1Info;
151             case BUTTON2:
152                 return mButton2Info;
153             case BUTTON3:
154                 return mButton3Info;
155             case BUTTON4:
156                 return mButton4Info;
157             default:
158                 throw new IllegalArgumentException("Invalid button requested");
159         }
160     }
161 }
162