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.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.widget.Toast; 25 26 import androidx.annotation.NonNull; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.car.ui.R; 31 import com.android.car.ui.utils.CarUiUtils; 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 public class CarUiPreference extends Preference implements DisabledPreferenceCallback { 38 39 private Context mContext; 40 private boolean mShowChevron; 41 private String mMessageToShowWhenDisabledPreferenceClicked; 42 43 private boolean mShouldShowRippleOnDisabledPreference; 44 private Drawable mBackground; 45 private View mPreference; 46 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)47 public CarUiPreference(Context context, AttributeSet attrs, 48 int defStyleAttr, int defStyleRes) { 49 super(context, attrs, defStyleAttr, defStyleRes); 50 init(context, attrs, defStyleAttr, defStyleRes); 51 } 52 CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr)53 public CarUiPreference(Context context, AttributeSet attrs, int defStyleAttr) { 54 this(context, attrs, defStyleAttr, R.style.Preference_CarUi_Preference); 55 } 56 CarUiPreference(Context context, AttributeSet attrs)57 public CarUiPreference(Context context, AttributeSet attrs) { 58 this(context, attrs, R.attr.carUiPreferenceStyle); 59 } 60 CarUiPreference(Context context)61 public CarUiPreference(Context context) { 62 this(context, null); 63 } 64 init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)65 public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 66 mContext = context; 67 68 TypedArray a = getContext().obtainStyledAttributes( 69 attrs, 70 R.styleable.CarUiPreference, 71 defStyleAttr, 72 defStyleRes); 73 74 mShowChevron = a.getBoolean(R.styleable.CarUiPreference_showChevron, true); 75 mShouldShowRippleOnDisabledPreference = a.getBoolean( 76 R.styleable.CarUiPreference_showRippleOnDisabledPreference, false); 77 78 a.recycle(); 79 } 80 81 82 @Override onBindViewHolder(PreferenceViewHolder holder)83 public void onBindViewHolder(PreferenceViewHolder holder) { 84 super.onBindViewHolder(holder); 85 boolean viewEnabled = isEnabled(); 86 mPreference = holder.itemView; 87 mBackground = CarUiUtils.setPreferenceViewEnabled(viewEnabled, holder.itemView, mBackground, 88 mShouldShowRippleOnDisabledPreference); 89 } 90 91 @Override onAttached()92 public void onAttached() { 93 super.onAttached(); 94 95 boolean allowChevron = mContext.getResources().getBoolean( 96 R.bool.car_ui_preference_show_chevron); 97 98 if (!allowChevron || !mShowChevron) { 99 return; 100 } 101 102 if (getOnPreferenceClickListener() != null || getIntent() != null 103 || getFragment() != null) { 104 setWidgetLayoutResource(R.layout.car_ui_preference_chevron); 105 } 106 } 107 108 /** 109 * This is similar to {@link Preference#performClick()} with the only difference that we do not 110 * return when view is not enabled. 111 */ 112 @Override 113 @SuppressWarnings("RestrictTo") performClick()114 public void performClick() { 115 if (isEnabled()) { 116 super.performClick(); 117 } else if (mMessageToShowWhenDisabledPreferenceClicked != null 118 && !mMessageToShowWhenDisabledPreferenceClicked.isEmpty()) { 119 Toast.makeText(mContext, mMessageToShowWhenDisabledPreferenceClicked, 120 Toast.LENGTH_LONG).show(); 121 } 122 } 123 setShowChevron(boolean showChevron)124 public void setShowChevron(boolean showChevron) { 125 mShowChevron = showChevron; 126 } 127 128 /** 129 * Sets the ripple on the disabled preference. 130 */ 131 @Override setShouldShowRippleOnDisabledPreference(boolean showRipple)132 public void setShouldShowRippleOnDisabledPreference(boolean showRipple) { 133 mShouldShowRippleOnDisabledPreference = showRipple; 134 CarUiUtils.updateRippleStateOnDisabledPreference(isEnabled(), 135 mShouldShowRippleOnDisabledPreference, mBackground, mPreference); 136 } 137 138 @Override setMessageToShowWhenDisabledPreferenceClicked(@onNull String message)139 public void setMessageToShowWhenDisabledPreferenceClicked(@NonNull String message) { 140 mMessageToShowWhenDisabledPreferenceClicked = message; 141 } 142 } 143