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