summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/fingerprint/FingerprintLocationAnimationView.java
blob: 65f38bd7ac3ff6da9dd266d67f5d1ec473c9eaea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.settings.fingerprint;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;

import com.android.settings.R;

/**
 * View which plays an animation to indicate where the sensor is on the device.
 */
public class FingerprintLocationAnimationView extends View {

    private static final float MAX_PULSE_ALPHA = 0.15f;
    private static final long DELAY_BETWEEN_PHASE = 1000;

    private final Interpolator mLinearOutSlowInInterpolator;
    private final Interpolator mFastOutSlowInInterpolator;

    private final int mDotRadius;
    private final int mMaxPulseRadius;
    private final float mFractionCenterX;
    private final float mFractionCenterY;
    private final Paint mDotPaint = new Paint();
    private final Paint mPulsePaint = new Paint();
    private float mPulseRadius;
    private ValueAnimator mRadiusAnimator;
    private ValueAnimator mAlphaAnimator;

    public FingerprintLocationAnimationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mDotRadius = getResources().getDimensionPixelSize(R.dimen.fingerprint_dot_radius);
        mMaxPulseRadius = getResources().getDimensionPixelSize(R.dimen.fingerprint_pulse_radius);
        mFractionCenterX = getResources().getFraction(
                R.fraction.fingerprint_sensor_location_fraction_x, 1, 1);
        mFractionCenterY = getResources().getFraction(
                R.fraction.fingerprint_sensor_location_fraction_y, 1, 1);
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
        int color = getResources().getColor(typedValue.resourceId, null);
        mDotPaint.setAntiAlias(true);
        mPulsePaint.setAntiAlias(true);
        mDotPaint.setColor(color);
        mPulsePaint.setColor(color);
        mLinearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
                android.R.interpolator.linear_out_slow_in);
        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
                android.R.interpolator.linear_out_slow_in);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawPulse(canvas);
        drawDot(canvas);
    }

    private void drawDot(Canvas canvas) {
        canvas.drawCircle(getCenterX(), getCenterY(), mDotRadius, mDotPaint);
    }

    private void drawPulse(Canvas canvas) {
        canvas.drawCircle(getCenterX(), getCenterY(), mPulseRadius, mPulsePaint);
    }

    private float getCenterX() {
        return getWidth() * mFractionCenterX;
    }

    private float getCenterY() {
        return getHeight() * mFractionCenterY;
    }

    public void startAnimation() {
        startPhase();
    }

    public void stopAnimation() {
        removeCallbacks(mStartPhaseRunnable);
        if (mRadiusAnimator != null) {
            mRadiusAnimator.cancel();
        }
        if (mAlphaAnimator != null) {
            mAlphaAnimator.cancel();
        }
    }

    private void startPhase() {
        startRadiusAnimation();
        startAlphaAnimation();
    }

    private void startRadiusAnimation() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, mMaxPulseRadius);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPulseRadius = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {

            boolean mCancelled;

            @Override
            public void onAnimationCancel(Animator animation) {
                mCancelled = true;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mRadiusAnimator = null;
                if (!mCancelled) {
                    postDelayed(mStartPhaseRunnable, DELAY_BETWEEN_PHASE);
                }
            }
        });
        animator.setDuration(1000);
        animator.setInterpolator(mLinearOutSlowInInterpolator);
        animator.start();
        mRadiusAnimator = animator;
    }

    private void startAlphaAnimation() {
        mPulsePaint.setAlpha((int) (255f * MAX_PULSE_ALPHA));
        ValueAnimator animator = ValueAnimator.ofFloat(MAX_PULSE_ALPHA, 0f);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPulsePaint.setAlpha((int) (255f * (float) animation.getAnimatedValue()));
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mAlphaAnimator = null;
            }
        });
        animator.setDuration(750);
        animator.setInterpolator(mFastOutSlowInInterpolator);
        animator.setStartDelay(250);
        animator.start();
        mAlphaAnimator = animator;
    }

    private final Runnable mStartPhaseRunnable = new Runnable() {
        @Override
        public void run() {
            startPhase();
        }
    };
}