OSDN Git Service

Eleven: rebrand step 2: update file contents
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / widgets / PlayPauseButton.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package org.lineageos.eleven.widgets;
15
16 import android.animation.Animator;
17 import android.content.Context;
18 import android.text.TextUtils;
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.view.ViewAnimationUtils;
22 import android.view.View.OnClickListener;
23 import android.view.View.OnLongClickListener;
24 import android.view.animation.Animation;
25 import android.view.animation.AnimationUtils;
26 import android.widget.ImageButton;
27
28 import org.lineageos.eleven.R;
29 import org.lineageos.eleven.utils.ApolloUtils;
30 import org.lineageos.eleven.utils.MusicUtils;
31
32 /**
33  * A custom {@link ImageButton} that represents the "play and pause" button.
34  *
35  * @author Andrew Neal (andrewdneal@gmail.com)
36  */
37 public class PlayPauseButton extends ImageButton implements OnClickListener, OnLongClickListener {
38
39     /**
40      * Play button theme resource
41      */
42     private static final String PLAY = "btn_playback_play";
43
44     /**
45      * Pause button theme resource
46      */
47     private static final String PAUSE = "btn_playback_pause";
48
49     /**
50      * @param context The {@link Context} to use
51      * @param attrs The attributes of the XML tag that is inflating the view.
52      */
53     @SuppressWarnings("deprecation")
54     public PlayPauseButton(final Context context, final AttributeSet attrs) {
55         super(context, attrs);
56         setBackground(getResources().getDrawable(R.drawable.selectable_background));
57         // Control playback (play/pause)
58         setOnClickListener(this);
59         // Show the cheat sheet
60         setOnLongClickListener(this);
61     }
62
63     /**
64      * {@inheritDoc}
65      */
66     @Override
67     public void onClick(final View v) {
68         MusicUtils.playOrPause();
69         int centerX = (v.getLeft() + v.getRight())  / 2;
70         int centerY = (v.getTop()  + v.getBottom()) / 2;
71         int startRadius = 0;
72         int endRadius = (int) Math.hypot(v.getWidth(), v.getHeight());
73
74         Animator anim = ViewAnimationUtils.createCircularReveal(
75                 v, centerX, centerY, startRadius, endRadius);
76
77         anim.setDuration(800);
78         anim.start();
79
80         updateState();
81     }
82
83     /**
84      * {@inheritDoc}
85      */
86     @Override
87     public boolean onLongClick(final View view) {
88         if (TextUtils.isEmpty(view.getContentDescription())) {
89             return false;
90         } else {
91             ApolloUtils.showCheatSheet(view);
92             return true;
93         }
94     }
95
96     /**
97      * Sets the correct drawable for playback.
98      */
99     public void updateState() {
100         if (MusicUtils.isPlaying()) {
101             setContentDescription(getResources().getString(R.string.accessibility_pause));
102             setImageDrawable(getResources().getDrawable(R.drawable.btn_playback_pause));
103         } else {
104             setContentDescription(getResources().getString(R.string.accessibility_play));
105             setImageDrawable(getResources().getDrawable(R.drawable.btn_playback_play));
106         }
107     }
108
109 }