summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/quicklaunch/ShortcutPreference.java
blob: 92efdeb0db249e356f05f900ca55a5f84e607116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.quicklaunch;

import com.android.settings.R;

import android.content.Context;
import android.content.res.ColorStateList;
import android.preference.Preference;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

/**
 * Preference type for a shortcut in {@link QuickLaunchSettings}.
 */
public class ShortcutPreference extends Preference implements Comparable<Preference> {

    private static Object sStaticVarsLock = new Object();
    
    // These static fields are used across all instances of ShortcutPreference.
    // There will be many ShortcutPreference instances (~36 for US).
    private static String STRING_ASSIGN_APPLICATION;
    private static String STRING_NO_SHORTCUT;

    private static int sDimAlpha;
    private static ColorStateList sRegularTitleColor;
    private static ColorStateList sDimTitleColor;
    private static ColorStateList sRegularSummaryColor;
    private static ColorStateList sDimSummaryColor;
    
    private char mShortcut;
    private boolean mHasBookmark;
    
    public ShortcutPreference(Context context, char shortcut) {
        super(context);

        synchronized (sStaticVarsLock) {
            // Init statics. This should only happen for the first ShortcutPreference created,
            // the rest will already have them initialized.
            if (STRING_ASSIGN_APPLICATION == null) {
                STRING_ASSIGN_APPLICATION = context.getString(R.string.quick_launch_assign_application);
                STRING_NO_SHORTCUT = context.getString(R.string.quick_launch_no_shortcut);
    
                TypedValue outValue = new TypedValue();
                context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
                sDimAlpha = (int) (outValue.getFloat() * 255);
            }
        }
        
        mShortcut = shortcut;

        setWidgetLayoutResource(R.layout.preference_widget_shortcut);
    }

    public char getShortcut() {
        return mShortcut;
    }

    public void setShortcut(char shortcut) {
        if (shortcut != mShortcut) {
            mShortcut = shortcut;
            notifyChanged();
        }
    }

    public boolean hasBookmark() {
        return mHasBookmark;
    }

    public void setHasBookmark(boolean hasBookmark) {
        if (hasBookmark != mHasBookmark) {
            mHasBookmark = hasBookmark;
            notifyChanged();
        }
    }

    @Override
    public CharSequence getTitle() {
        return mHasBookmark ? super.getTitle() : STRING_ASSIGN_APPLICATION;
    }

    @Override
    public CharSequence getSummary() {
        return mHasBookmark ? super.getSummary() : STRING_NO_SHORTCUT;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        
        TextView shortcutView = (TextView) view.findViewById(R.id.shortcut);
        if (shortcutView != null) {
            shortcutView.setText(String.valueOf(mShortcut));
        }
    
        TextView titleView = (TextView) view.findViewById(android.R.id.title);

        synchronized (sStaticVarsLock) {
            if (sRegularTitleColor == null) {
                sRegularTitleColor = titleView.getTextColors();
                sDimTitleColor = sRegularTitleColor.withAlpha(sDimAlpha);
            }
        }
        
        ColorStateList color = mHasBookmark ? sRegularTitleColor : sDimTitleColor;
        if (color != null) {
            titleView.setTextColor(color);
        }
        
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);

        synchronized (sStaticVarsLock) {
            if (sRegularSummaryColor == null) {
                sRegularSummaryColor = summaryView.getTextColors();
                sDimSummaryColor = sRegularSummaryColor.withAlpha(sDimAlpha);
            }
        }
        
        color = mHasBookmark ? sRegularSummaryColor : sDimSummaryColor;
        if (color != null) {
            summaryView.setTextColor(color);
        }
        
    }

    public int compareTo(Preference another) {
        if (!(another instanceof ShortcutPreference)) return super.compareTo(another);

        // Letters before digits
        char other = ((ShortcutPreference) another).mShortcut;
        if (Character.isDigit(mShortcut) && Character.isLetter(other)) return 1;
        else if (Character.isDigit(other) && Character.isLetter(mShortcut)) return -1;
        else return mShortcut - other;
    }
    
}