1 /*
2  * Copyright 2019 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.ui.preference;
18 
19 import static com.android.car.ui.core.CarUi.MIN_TARGET_API;
20 
21 import android.annotation.TargetApi;
22 import android.content.Context;
23 import android.content.res.TypedArray;
24 import android.util.AttributeSet;
25 
26 import androidx.annotation.Nullable;
27 import androidx.preference.ListPreference;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.car.ui.R;
32 import com.android.car.ui.utils.CarUiUtils;
33 
34 import java.util.function.Consumer;
35 
36 /**
37  * This class extends the base {@link ListPreference} class. Adds the drawable icon to
38  * the preference.
39  */
40 @SuppressWarnings("AndroidJdkLibsChecker")
41 @TargetApi(MIN_TARGET_API)
42 public class CarUiListPreference extends ListPreference implements UxRestrictablePreference {
43 
44     private Consumer<Preference> mRestrictedClickListener;
45     private boolean mUxRestricted = false;
46 
CarUiListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)47     public CarUiListPreference(Context context, AttributeSet attrs,
48             int defStyleAttr, int defStyleRes) {
49         super(context, attrs, defStyleAttr, defStyleRes);
50         init(attrs, defStyleAttr, defStyleRes);
51     }
52 
CarUiListPreference(Context context, AttributeSet attrs, int defStyleAttr)53     public CarUiListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
54         this(context, attrs, defStyleAttr, R.style.Preference_CarUi_Preference);
55     }
56 
CarUiListPreference(Context context, AttributeSet attrs)57     public CarUiListPreference(Context context, AttributeSet attrs) {
58         this(context, attrs, R.attr.carUiPreferenceStyle);
59     }
60 
CarUiListPreference(Context context)61     public CarUiListPreference(Context context) {
62         this(context, null);
63     }
64 
init(AttributeSet attrs, int defStyleAttr, int defStyleRes)65     private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
66         TypedArray a = getContext().obtainStyledAttributes(
67                 attrs,
68                 R.styleable.CarUiPreference,
69                 defStyleAttr,
70                 defStyleRes);
71 
72         mUxRestricted = a.getBoolean(R.styleable.CarUiPreference_car_ui_ux_restricted, false);
73         a.recycle();
74     }
75 
76     @Override
onAttached()77     public void onAttached() {
78         super.onAttached();
79 
80         boolean showChevron = getContext().getResources().getBoolean(
81                 R.bool.car_ui_preference_show_chevron);
82 
83         if (!showChevron) {
84             return;
85         }
86 
87         setWidgetLayoutResource(R.layout.car_ui_preference_chevron);
88     }
89 
90     @Override
onBindViewHolder(PreferenceViewHolder holder)91     public void onBindViewHolder(PreferenceViewHolder holder) {
92         super.onBindViewHolder(holder);
93 
94         CarUiUtils.makeAllViewsUxRestricted(holder.itemView, isUxRestricted());
95     }
96 
97     @Override
98     @SuppressWarnings("RestrictTo")
performClick()99     public void performClick() {
100         if ((isEnabled() || isSelectable()) && isUxRestricted()) {
101             if (mRestrictedClickListener != null) {
102                 mRestrictedClickListener.accept(this);
103             }
104         } else {
105             super.performClick();
106         }
107     }
108 
109     @Override
setUxRestricted(boolean restricted)110     public void setUxRestricted(boolean restricted) {
111         if (restricted != mUxRestricted) {
112             mUxRestricted = restricted;
113             notifyChanged();
114         }
115     }
116 
117     @Override
isUxRestricted()118     public boolean isUxRestricted() {
119         return mUxRestricted;
120     }
121 
122     @Override
setOnClickWhileRestrictedListener(@ullable Consumer<Preference> listener)123     public void setOnClickWhileRestrictedListener(@Nullable Consumer<Preference> listener) {
124         mRestrictedClickListener = listener;
125     }
126 
127     @Nullable
128     @Override
getOnClickWhileRestrictedListener()129     public Consumer<Preference> getOnClickWhileRestrictedListener() {
130         return mRestrictedClickListener;
131     }
132 }
133