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 android.content.Context; 20 import android.content.res.TypedArray; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import androidx.annotation.Nullable; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceViewHolder; 27 28 import com.android.car.ui.R; 29 import com.android.car.ui.utils.CarUiUtils; 30 31 import java.util.function.Consumer; 32 33 /** 34 * This class extends the base {@link Preference} class. Adds the support to add a drawable icon to 35 * the preference if there is one of fragment, intent or onPreferenceClickListener set. 36 */ 37 @SuppressWarnings("AndroidJdkLibsChecker") 38 public class CarUiPreference extends Preference implements DisabledPreferenceCallback { 39 private boolean mShowChevron; 40 41 private Consumer<Preference> mRestrictedClickListener; 42 private boolean mUxRestricted = false; 43 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)44 public CarUiPreference(Context context, AttributeSet attrs, 45 int defStyleAttr, int defStyleRes) { 46 super(context, attrs, defStyleAttr, defStyleRes); 47 init(attrs, defStyleAttr, defStyleRes); 48 } 49 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr)50 public CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr) { 51 this(context, attrs, defStyleAttr, R.style.Preference_CarUi_Preference); 52 } 53 CarUiPreference(Context context, AttributeSet attrs)54 public CarUiPreference(Context context, AttributeSet attrs) { 55 this(context, attrs, R.attr.carUiPreferenceStyle); 56 } 57 CarUiPreference(Context context)58 public CarUiPreference(Context context) { 59 this(context, null); 60 } 61 init(AttributeSet attrs, int defStyleAttr, int defStyleRes)62 private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) { 63 TypedArray a = getContext().obtainStyledAttributes( 64 attrs, 65 R.styleable.CarUiPreference, 66 defStyleAttr, 67 defStyleRes); 68 69 mShowChevron = a.getBoolean(R.styleable.CarUiPreference_showChevron, true); 70 mUxRestricted = a.getBoolean(R.styleable.CarUiPreference_car_ui_ux_restricted, false); 71 72 a.recycle(); 73 } 74 75 @Override onBindViewHolder(PreferenceViewHolder holder)76 public void onBindViewHolder(PreferenceViewHolder holder) { 77 super.onBindViewHolder(holder); 78 79 CarUiUtils.makeAllViewsUxRestricted(holder.itemView, isUxRestricted()); 80 } 81 82 @Override onAttached()83 public void onAttached() { 84 super.onAttached(); 85 86 boolean allowChevron = getContext().getResources().getBoolean( 87 R.bool.car_ui_preference_show_chevron); 88 89 if (!allowChevron || !mShowChevron) { 90 return; 91 } 92 93 if (getOnPreferenceClickListener() != null || getIntent() != null 94 || getFragment() != null) { 95 setWidgetLayoutResource(R.layout.car_ui_preference_chevron); 96 } 97 } 98 99 /** 100 * An exact copy of {@link androidx.preference.Preference#performClick(View)} 101 * This method was added here because super.performClick(View) is not open 102 * for app usage. 103 */ 104 @SuppressWarnings("RestrictTo") performClickUnrestricted(View v)105 void performClickUnrestricted(View v) { 106 performClick(); 107 } 108 109 @Override 110 @SuppressWarnings("RestrictTo") performClick()111 public void performClick() { 112 if ((isEnabled() || isSelectable()) && isUxRestricted()) { 113 if (mRestrictedClickListener != null) { 114 mRestrictedClickListener.accept(this); 115 } 116 } else { 117 super.performClick(); 118 } 119 } 120 setShowChevron(boolean showChevron)121 public void setShowChevron(boolean showChevron) { 122 mShowChevron = showChevron; 123 } 124 125 @Override isUxRestricted()126 public boolean isUxRestricted() { 127 return mUxRestricted; 128 } 129 130 @Override setOnClickWhileRestrictedListener(@ullable Consumer<Preference> listener)131 public void setOnClickWhileRestrictedListener(@Nullable Consumer<Preference> listener) { 132 mRestrictedClickListener = listener; 133 } 134 135 @Nullable 136 @Override getOnClickWhileRestrictedListener()137 public Consumer<Preference> getOnClickWhileRestrictedListener() { 138 return mRestrictedClickListener; 139 } 140 141 @Override setUxRestricted(boolean restricted)142 public void setUxRestricted(boolean restricted) { 143 mUxRestricted = restricted; 144 notifyChanged(); 145 } 146 } 147