OSDN Git Service

Repackaged com.andrew.apollo to com.cyngn.eleven
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / widgets / RepeatButton.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.cyngn.eleven.widgets;
13
14 import android.content.Context;
15 import android.text.TextUtils;
16 import android.util.AttributeSet;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.view.View.OnLongClickListener;
20 import android.widget.ImageButton;
21
22 import com.cyngn.eleven.MusicPlaybackService;
23 import com.cyngn.eleven.R;
24 import com.cyngn.eleven.utils.ApolloUtils;
25 import com.cyngn.eleven.utils.MusicUtils;
26 import com.cyngn.eleven.utils.ThemeUtils;
27 import com.cyngn.eleven.widgets.theme.HoloSelector;
28
29 /**
30  * A custom {@link ImageButton} that represents the "repeat" button.
31  * 
32  * @author Andrew Neal (andrewdneal@gmail.com)
33  */
34 public class RepeatButton extends ImageButton implements OnClickListener, OnLongClickListener {
35
36     /**
37      * Repeat one theme resource
38      */
39     private static final String REPEAT_ALL = "btn_playback_repeat_all";
40
41     /**
42      * Repeat one theme resource
43      */
44     private static final String REPEAT_CURRENT = "btn_playback_repeat_one";
45
46     /**
47      * Repeat one theme resource
48      */
49     private static final String REPEAT_NONE = "btn_playback_repeat";
50
51     /**
52      * The resources to use.
53      */
54     private final ThemeUtils mResources;
55
56     /**
57      * @param context The {@link Context} to use
58      * @param attrs The attributes of the XML tag that is inflating the view.
59      */
60     @SuppressWarnings("deprecation")
61     public RepeatButton(final Context context, final AttributeSet attrs) {
62         super(context, attrs);
63         // Initialze the theme resources
64         mResources = new ThemeUtils(context);
65         // Set the selector
66         setBackgroundDrawable(new HoloSelector(context));
67         // Control playback (cycle repeat modes)
68         setOnClickListener(this);
69         // Show the cheat sheet
70         setOnLongClickListener(this);
71     }
72
73     /**
74      * {@inheritDoc}
75      */
76     @Override
77     public void onClick(final View v) {
78         MusicUtils.cycleRepeat();
79         updateRepeatState();
80     }
81
82     /**
83      * {@inheritDoc}
84      */
85     @Override
86     public boolean onLongClick(final View view) {
87         if (TextUtils.isEmpty(view.getContentDescription())) {
88             return false;
89         } else {
90             ApolloUtils.showCheatSheet(view);
91             return true;
92         }
93     }
94
95     /**
96      * Sets the correct drawable for the repeat state.
97      */
98     public void updateRepeatState() {
99         switch (MusicUtils.getRepeatMode()) {
100             case MusicPlaybackService.REPEAT_ALL:
101                 setContentDescription(getResources().getString(R.string.accessibility_repeat_all));
102                 setImageDrawable(mResources.getDrawable(REPEAT_ALL));
103                 break;
104             case MusicPlaybackService.REPEAT_CURRENT:
105                 setContentDescription(getResources().getString(R.string.accessibility_repeat_one));
106                 setImageDrawable(mResources.getDrawable(REPEAT_CURRENT));
107                 break;
108             case MusicPlaybackService.REPEAT_NONE:
109                 setContentDescription(getResources().getString(R.string.accessibility_repeat));
110                 setImageDrawable(mResources.getDrawable(REPEAT_NONE));
111                 break;
112             default:
113                 break;
114         }
115     }
116 }