1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.systemui.statusbar.phone;
17
18import android.animation.Animator;
19import android.animation.Animator.AnimatorListener;
20import android.animation.ObjectAnimator;
21import android.content.Context;
22import android.graphics.drawable.RippleDrawable;
23import android.os.Handler;
24import android.os.Message;
25import android.util.AttributeSet;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.ViewConfiguration;
29import android.view.animation.Animation;
30import android.view.animation.AnimationUtils;
31
32import com.android.keyguard.AlphaOptimizedImageButton;
33
34public class SettingsButton extends AlphaOptimizedImageButton {
35
36    private static final long LONG_PRESS_LENGTH = 1000;
37    private static final long ACCEL_LENGTH = 750;
38    private static final long FULL_SPEED_LENGTH = 375;
39    private static final long RUN_DURATION = 350;
40
41    private boolean mUpToSpeed;
42    private ObjectAnimator mAnimator;
43
44    private float mSlop;
45
46    public SettingsButton(Context context, AttributeSet attrs) {
47        super(context, attrs);
48        mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
49    }
50
51    public boolean isAnimating() {
52        return mAnimator != null && mAnimator.isRunning();
53    }
54
55    public boolean isTunerClick() {
56        return mUpToSpeed;
57    }
58
59    @Override
60    public boolean onTouchEvent(MotionEvent event) {
61        switch (event.getActionMasked()) {
62            case MotionEvent.ACTION_DOWN:
63                postDelayed(mLongPressCallback, LONG_PRESS_LENGTH);
64                break;
65            case MotionEvent.ACTION_UP:
66                if (mUpToSpeed) {
67                    startExitAnimation();
68                } else {
69                    cancelLongClick();
70                }
71                break;
72            case MotionEvent.ACTION_CANCEL:
73                cancelLongClick();
74                break;
75            case MotionEvent.ACTION_MOVE:
76                float x = event.getX();
77                float y = event.getY();
78                if ((x < -mSlop) || (y < -mSlop) || (x > getWidth() + mSlop)
79                        || (y > getHeight() + mSlop)) {
80                    cancelLongClick();
81                }
82                break;
83        }
84        return super.onTouchEvent(event);
85    }
86
87    private void cancelLongClick() {
88        cancelAnimation();
89        mUpToSpeed = false;
90        removeCallbacks(mLongPressCallback);
91    }
92
93    private void cancelAnimation() {
94        if (mAnimator != null) {
95            mAnimator.removeAllListeners();
96            mAnimator.cancel();
97            mAnimator = null;
98        }
99    }
100
101    private void startExitAnimation() {
102        animate()
103                .translationX(((View) getParent().getParent()).getWidth() - getX())
104                .alpha(0)
105                .setDuration(RUN_DURATION)
106                .setInterpolator(AnimationUtils.loadInterpolator(mContext,
107                        android.R.interpolator.accelerate_cubic))
108                .setListener(new AnimatorListener() {
109                    @Override
110                    public void onAnimationStart(Animator animation) {
111                    }
112
113                    @Override
114                    public void onAnimationRepeat(Animator animation) {
115                    }
116
117                    @Override
118                    public void onAnimationEnd(Animator animation) {
119                        setAlpha(1f);
120                        setTranslationX(0);
121                        cancelLongClick();
122                    }
123
124                    @Override
125                    public void onAnimationCancel(Animator animation) {
126                    }
127                })
128                .start();
129    }
130
131    protected void startAccelSpin() {
132        cancelAnimation();
133        mAnimator = ObjectAnimator.ofFloat(this, View.ROTATION, 0, 360);
134        mAnimator.setInterpolator(AnimationUtils.loadInterpolator(mContext,
135                android.R.interpolator.accelerate_quad));
136        mAnimator.setDuration(ACCEL_LENGTH);
137        mAnimator.addListener(new AnimatorListener() {
138            @Override
139            public void onAnimationStart(Animator animation) {
140            }
141
142            @Override
143            public void onAnimationRepeat(Animator animation) {
144            }
145
146            @Override
147            public void onAnimationEnd(Animator animation) {
148                startContinuousSpin();
149            }
150
151            @Override
152            public void onAnimationCancel(Animator animation) {
153            }
154        });
155        mAnimator.start();
156    }
157
158    protected void startContinuousSpin() {
159        cancelAnimation();
160        mUpToSpeed = true;
161        mAnimator = ObjectAnimator.ofFloat(this, View.ROTATION, 0, 360);
162        mAnimator.setInterpolator(AnimationUtils.loadInterpolator(mContext,
163                android.R.interpolator.linear));
164        mAnimator.setDuration(FULL_SPEED_LENGTH);
165        mAnimator.setRepeatCount(Animation.INFINITE);
166        mAnimator.start();
167    }
168
169    private final Runnable mLongPressCallback = new Runnable() {
170        @Override
171        public void run() {
172            startAccelSpin();
173        }
174    };
175}
176