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