OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / widgets / PlayPauseButton.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;
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.andrew.apollo.R;
23 import com.andrew.apollo.utils.ApolloUtils;
24 import com.andrew.apollo.utils.MusicUtils;
25 import com.andrew.apollo.utils.ThemeUtils;
26 import com.andrew.apollo.widgets.theme.HoloSelector;
27
28 /**
29  * A custom {@link ImageButton} that represents the "play and pause" button.
30  * 
31  * @author Andrew Neal (andrewdneal@gmail.com)
32  */
33 public class PlayPauseButton extends ImageButton implements OnClickListener, OnLongClickListener {
34
35     /**
36      * Play button theme resource
37      */
38     private static final String PLAY = "btn_playback_play";
39
40     /**
41      * Pause button theme resource
42      */
43     private static final String PAUSE = "btn_playback_pause";
44
45     /**
46      * The resources to use.
47      */
48     private final ThemeUtils mResources;
49
50     /**
51      * @param context The {@link Context} to use
52      * @param attrs The attributes of the XML tag that is inflating the view.
53      */
54     @SuppressWarnings("deprecation")
55     public PlayPauseButton(final Context context, final AttributeSet attrs) {
56         super(context, attrs);
57         // Initialze the theme resources
58         mResources = new ThemeUtils(context);
59         // Theme the selector
60         setBackgroundDrawable(new HoloSelector(context));
61         // Control playback (play/pause)
62         setOnClickListener(this);
63         // Show the cheat sheet
64         setOnLongClickListener(this);
65     }
66
67     /**
68      * {@inheritDoc}
69      */
70     @Override
71     public void onClick(final View v) {
72         MusicUtils.playOrPause();
73         updateState();
74     }
75
76     /**
77      * {@inheritDoc}
78      */
79     @Override
80     public boolean onLongClick(final View view) {
81         if (TextUtils.isEmpty(view.getContentDescription())) {
82             return false;
83         } else {
84             ApolloUtils.showCheatSheet(view);
85             return true;
86         }
87     }
88
89     /**
90      * Sets the correct drawable for playback.
91      */
92     public void updateState() {
93         if (MusicUtils.isPlaying()) {
94             setContentDescription(getResources().getString(R.string.accessibility_pause));
95             setImageDrawable(mResources.getDrawable(PAUSE));
96         } else {
97             setContentDescription(getResources().getString(R.string.accessibility_play));
98             setImageDrawable(mResources.getDrawable(PLAY));
99         }
100     }
101
102 }