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.ColorStateList; 21 import android.content.res.TypedArray; 22 import android.text.TextUtils; 23 import android.util.AttributeSet; 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.CarUiPrimarySwitchPreference; 30 import com.android.settingslib.Utils; 31 32 /** 33 * Extends {@link CarUiPrimarySwitchPreference} to customize colors for each state. 34 */ 35 // TODO(242221289): Remove ColoredSwitchPreference once contentDescription is 36 // added to chassis preferences 37 public class ColoredSwitchPreference extends CarUiPrimarySwitchPreference { 38 private ColorStateList mEnabledColor; 39 private ColorStateList mDisabledColor; 40 private String mContentDescription; 41 ColoredSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)42 public ColoredSwitchPreference(Context context, AttributeSet attrs, 43 int defStyleAttr, int defStyleRes) { 44 super(context, attrs, defStyleAttr, defStyleRes); 45 init(context, attrs); 46 } 47 ColoredSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)48 public ColoredSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 49 super(context, attrs, defStyleAttr); 50 init(context, attrs); 51 } 52 ColoredSwitchPreference(Context context, AttributeSet attrs)53 public ColoredSwitchPreference(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 init(context, attrs); 56 } 57 ColoredSwitchPreference(Context context)58 public ColoredSwitchPreference(Context context) { 59 super(context); 60 init(context, /* attrs= */ null); 61 } 62 init(Context context, AttributeSet attrs)63 private void init(Context context, AttributeSet attrs) { 64 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColoredSwitchPreference); 65 mDisabledColor = a.getColorStateList( 66 R.styleable.ColoredSwitchPreference_disabledTitleColor); 67 if (mDisabledColor == null) { 68 mDisabledColor = Utils.getColorAttr(context, android.R.attr.textColorPrimary); 69 } 70 mEnabledColor = a.getColorStateList(R.styleable.ColoredSwitchPreference_enabledTitleColor); 71 if (mEnabledColor == null) { 72 mEnabledColor = Utils.getColorAttr(context, android.R.attr.textColorPrimary); 73 } 74 } 75 76 @Override onBindViewHolder(PreferenceViewHolder holder)77 public void onBindViewHolder(PreferenceViewHolder holder) { 78 super.onBindViewHolder(holder); 79 if (!TextUtils.isEmpty(mContentDescription)) { 80 holder.itemView.setContentDescription(mContentDescription); 81 } 82 TextView title = holder.itemView.findViewById(android.R.id.title); 83 if (title != null) { 84 if (isChecked()) { 85 title.setTextColor(mEnabledColor); 86 } else { 87 title.setTextColor(mDisabledColor); 88 } 89 } 90 } 91 92 /** 93 * Set a content description for the preference. This is mainly for accessibility or 94 * UI Automation purposes. 95 */ setContentDescription(String contentDescription)96 public void setContentDescription(String contentDescription) { 97 mContentDescription = contentDescription; 98 notifyChanged(); 99 } 100 } 101