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.
12 package com.andrew.apollo.widgets;
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;
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;
29 * A custom {@link ImageButton} that represents the "play and pause" button.
31 * @author Andrew Neal (andrewdneal@gmail.com)
33 public class PlayPauseButton extends ImageButton implements OnClickListener, OnLongClickListener {
36 * Play button theme resource
38 private static final String PLAY = "btn_playback_play";
41 * Pause button theme resource
43 private static final String PAUSE = "btn_playback_pause";
46 * The resources to use.
48 private final ThemeUtils mResources;
51 * @param context The {@link Context} to use
52 * @param attrs The attributes of the XML tag that is inflating the view.
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);
60 setBackgroundDrawable(new HoloSelector(context));
61 // Control playback (play/pause)
62 setOnClickListener(this);
63 // Show the cheat sheet
64 setOnLongClickListener(this);
71 public void onClick(final View v) {
72 MusicUtils.playOrPause();
80 public boolean onLongClick(final View view) {
81 if (TextUtils.isEmpty(view.getContentDescription())) {
84 ApolloUtils.showCheatSheet(view);
90 * Sets the correct drawable for playback.
92 public void updateState() {
93 if (MusicUtils.isPlaying()) {
94 setContentDescription(getResources().getString(R.string.accessibility_pause));
95 setImageDrawable(mResources.getDrawable(PAUSE));
97 setContentDescription(getResources().getString(R.string.accessibility_play));
98 setImageDrawable(mResources.getDrawable(PLAY));