1 /* 2 * Copyright (C) 2007 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.quicklaunch; 18 19 import com.android.settings.R; 20 21 import android.content.Context; 22 import android.content.res.ColorStateList; 23 import android.preference.Preference; 24 import android.util.TypedValue; 25 import android.view.View; 26 import android.widget.TextView; 27 28 /** 29 * Preference type for a shortcut in {@link QuickLaunchSettings}. 30 */ 31 public class ShortcutPreference extends Preference implements Comparable<Preference> { 32 33 private static Object sStaticVarsLock = new Object(); 34 35 // These static fields are used across all instances of ShortcutPreference. 36 // There will be many ShortcutPreference instances (~36 for US). 37 private static String STRING_ASSIGN_APPLICATION; 38 private static String STRING_NO_SHORTCUT; 39 40 private static int sDimAlpha; 41 private static ColorStateList sRegularTitleColor; 42 private static ColorStateList sDimTitleColor; 43 private static ColorStateList sRegularSummaryColor; 44 private static ColorStateList sDimSummaryColor; 45 46 private char mShortcut; 47 private boolean mHasBookmark; 48 49 public ShortcutPreference(Context context, char shortcut) { 50 super(context); 51 52 synchronized (sStaticVarsLock) { 53 // Init statics. This should only happen for the first ShortcutPreference created, 54 // the rest will already have them initialized. 55 if (STRING_ASSIGN_APPLICATION == null) { 56 STRING_ASSIGN_APPLICATION = context.getString(R.string.quick_launch_assign_application); 57 STRING_NO_SHORTCUT = context.getString(R.string.quick_launch_no_shortcut); 58 59 TypedValue outValue = new TypedValue(); 60 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true); 61 sDimAlpha = (int) (outValue.getFloat() * 255); 62 } 63 } 64 65 mShortcut = shortcut; 66 67 setWidgetLayoutResource(R.layout.preference_widget_shortcut); 68 } 69 70 public char getShortcut() { 71 return mShortcut; 72 } 73 74 public void setShortcut(char shortcut) { 75 if (shortcut != mShortcut) { 76 mShortcut = shortcut; 77 notifyChanged(); 78 } 79 } 80 81 public boolean hasBookmark() { 82 return mHasBookmark; 83 } 84 85 public void setHasBookmark(boolean hasBookmark) { 86 if (hasBookmark != mHasBookmark) { 87 mHasBookmark = hasBookmark; 88 notifyChanged(); 89 } 90 } 91 92 @Override 93 public CharSequence getTitle() { 94 return mHasBookmark ? super.getTitle() : STRING_ASSIGN_APPLICATION; 95 } 96 97 @Override 98 public CharSequence getSummary() { 99 return mHasBookmark ? super.getSummary() : STRING_NO_SHORTCUT; 100 } 101 102 @Override 103 protected void onBindView(View view) { 104 super.onBindView(view); 105 106 TextView shortcutView = (TextView) view.findViewById(R.id.shortcut); 107 if (shortcutView != null) { 108 shortcutView.setText(String.valueOf(mShortcut)); 109 } 110 111 TextView titleView = (TextView) view.findViewById(android.R.id.title); 112 113 synchronized (sStaticVarsLock) { 114 if (sRegularTitleColor == null) { 115 sRegularTitleColor = titleView.getTextColors(); 116 sDimTitleColor = sRegularTitleColor.withAlpha(sDimAlpha); 117 } 118 } 119 120 ColorStateList color = mHasBookmark ? sRegularTitleColor : sDimTitleColor; 121 if (color != null) { 122 titleView.setTextColor(color); 123 } 124 125 TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 126 127 synchronized (sStaticVarsLock) { 128 if (sRegularSummaryColor == null) { 129 sRegularSummaryColor = summaryView.getTextColors(); 130 sDimSummaryColor = sRegularSummaryColor.withAlpha(sDimAlpha); 131 } 132 } 133 134 color = mHasBookmark ? sRegularSummaryColor : sDimSummaryColor; 135 if (color != null) { 136 summaryView.setTextColor(color); 137 } 138 139 } 140 141 public int compareTo(Preference another) { 142 if (!(another instanceof ShortcutPreference)) return super.compareTo(another); 143 144 // Letters before digits 145 char other = ((ShortcutPreference) another).mShortcut; 146 if (Character.isDigit(mShortcut) && Character.isLetter(other)) return 1; 147 else if (Character.isDigit(other) && Character.isLetter(mShortcut)) return -1; 148 else return mShortcut - other; 149 } 150 151 } 152