1 /* 2 * Copyright (C) 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.developeroptions.accessibility; 18 19 import android.content.Context; 20 import android.graphics.Color; 21 import android.graphics.drawable.ColorDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.ImageView; 27 import android.widget.TextView; 28 29 import androidx.preference.PreferenceViewHolder; 30 31 import com.android.car.developeroptions.R; 32 33 /** 34 * Grid preference that allows the user to pick a color from a predefined set of 35 * colors. Optionally shows a preview in the preference item. 36 */ 37 public class ColorPreference extends ListDialogPreference { 38 private ColorDrawable mPreviewColor; 39 private boolean mPreviewEnabled; 40 ColorPreference(Context context, AttributeSet attrs)41 public ColorPreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 44 setDialogLayoutResource(R.layout.grid_picker_dialog); 45 setListItemLayoutResource(R.layout.color_picker_item); 46 } 47 48 /** 49 * @param enabled whether to show a preview in the preference item 50 */ setPreviewEnabled(boolean enabled)51 public void setPreviewEnabled(boolean enabled) { 52 if (mPreviewEnabled != enabled) { 53 mPreviewEnabled = enabled; 54 55 if (enabled) { 56 setWidgetLayoutResource(R.layout.preference_color); 57 } else { 58 setWidgetLayoutResource(0); 59 } 60 } 61 } 62 63 @Override shouldDisableDependents()64 public boolean shouldDisableDependents() { 65 return Color.alpha(getValue()) == 0 || super.shouldDisableDependents(); 66 } 67 68 @Override getTitleAt(int index)69 protected CharSequence getTitleAt(int index) { 70 final CharSequence title = super.getTitleAt(index); 71 if (title != null) { 72 return title; 73 } 74 75 // If no title was supplied, format title using RGB values. 76 final int value = getValueAt(index); 77 final int r = Color.red(value); 78 final int g = Color.green(value); 79 final int b = Color.blue(value); 80 return getContext().getString(R.string.color_custom, r, g, b); 81 } 82 83 @Override onBindViewHolder(PreferenceViewHolder view)84 public void onBindViewHolder(PreferenceViewHolder view) { 85 super.onBindViewHolder(view); 86 87 if (mPreviewEnabled) { 88 final ImageView previewImage = (ImageView) view.findViewById(R.id.color_preview); 89 final int argb = getValue(); 90 if (Color.alpha(argb) < 255) { 91 previewImage.setBackgroundResource(R.drawable.transparency_tileable); 92 } else { 93 previewImage.setBackground(null); 94 } 95 96 if (mPreviewColor == null) { 97 mPreviewColor = new ColorDrawable(argb); 98 previewImage.setImageDrawable(mPreviewColor); 99 } else { 100 mPreviewColor.setColor(argb); 101 } 102 103 final CharSequence summary = getSummary(); 104 if (!TextUtils.isEmpty(summary)) { 105 previewImage.setContentDescription(summary); 106 } else { 107 previewImage.setContentDescription(null); 108 } 109 110 previewImage.setAlpha(isEnabled() ? 1f : 0.2f); 111 } 112 } 113 114 @Override onBindListItem(View view, int index)115 protected void onBindListItem(View view, int index) { 116 final int argb = getValueAt(index); 117 final int alpha = Color.alpha(argb); 118 119 final ImageView swatch = (ImageView) view.findViewById(R.id.color_swatch); 120 if (alpha < 255) { 121 swatch.setBackgroundResource(R.drawable.transparency_tileable); 122 } else { 123 swatch.setBackground(null); 124 } 125 126 final Drawable foreground = swatch.getDrawable(); 127 if (foreground instanceof ColorDrawable) { 128 ((ColorDrawable) foreground).setColor(argb); 129 } else { 130 swatch.setImageDrawable(new ColorDrawable(argb)); 131 } 132 133 final CharSequence title = getTitleAt(index); 134 if (title != null) { 135 final TextView summary = (TextView) view.findViewById(R.id.summary); 136 summary.setText(title); 137 } 138 } 139 } 140