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