OSDN Git Service

b6d9b35ef29b352f6c06b29aae96aab3ba33ef1b
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / widgets / theme / HoloSelector.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.andrew.apollo.widgets.theme;
13
14 import android.annotation.SuppressLint;
15 import android.content.Context;
16 import android.graphics.Color;
17 import android.graphics.drawable.ColorDrawable;
18 import android.graphics.drawable.StateListDrawable;
19
20 import com.andrew.apollo.utils.ApolloUtils;
21 import com.andrew.apollo.utils.ThemeUtils;
22
23 import java.lang.ref.WeakReference;
24
25 /**
26  * A themeable {@link StateListDrawable}.
27  * 
28  * @author Andrew Neal (andrewdneal@gmail.com)
29  */
30 public class HoloSelector extends StateListDrawable {
31
32     /**
33      * Used to theme the touched and focused colors
34      */
35     private static final String RESOURCE_NAME = "holo_selector";
36
37     /**
38      * Focused state
39      */
40     private static final int FOCUSED = android.R.attr.state_focused;
41
42     /**
43      * Pressed state
44      */
45     private static final int PRESSED = android.R.attr.state_pressed;
46
47     /**
48      * Constructor for <code>HoloSelector</code>
49      * 
50      * @param context The {@link Context} to use.
51      */
52     @SuppressLint("NewApi")
53     public HoloSelector(final Context context) {
54         final ThemeUtils resources = new ThemeUtils(context);
55         final int themeColor = resources.getColor(RESOURCE_NAME);
56         // Focused
57         addState(new int[] {
58             FOCUSED
59         }, makeColorDrawable(themeColor));
60         // Pressed
61         addState(new int[] {
62             PRESSED
63         }, makeColorDrawable(themeColor));
64         // Default
65         addState(new int[] {}, makeColorDrawable(Color.TRANSPARENT));
66         setExitFadeDuration(400);
67     }
68
69     /**
70      * @param color The color to use.
71      * @return A new {@link ColorDrawable}.
72      */
73     private static final ColorDrawable makeColorDrawable(final int color) {
74         return new WeakReference<ColorDrawable>(new ColorDrawable(color)).get();
75     }
76 }