blob: f3442e6e330ec5519f5711e404455835d3cc2a4a [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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 */
16
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070017package com.android.dialer.dialpadview;
Eric Erfanianccca3152017-02-22 16:32:36 -080018
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.ArgbEvaluator;
22import android.animation.ValueAnimator;
Eric Erfanianccca3152017-02-22 16:32:36 -080023import android.content.Context;
24import android.graphics.Color;
25import android.graphics.ColorFilter;
26import android.graphics.LightingColorFilter;
27import android.os.Handler;
28import android.os.Vibrator;
29import android.view.View;
Eric Erfanianccca3152017-02-22 16:32:36 -080030
31/** Animates the dial button on "emergency" phone numbers. */
32public class PseudoEmergencyAnimator {
33
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070034 static final String PSEUDO_EMERGENCY_NUMBER = "01189998819991197253";
Eric Erfanianccca3152017-02-22 16:32:36 -080035 private static final int VIBRATE_LENGTH_MILLIS = 200;
36 private static final int ITERATION_LENGTH_MILLIS = 1000;
37 private static final int ANIMATION_ITERATION_COUNT = 6;
linyuh183cb712017-12-27 17:02:37 -080038 private ViewProvider viewProvider;
39 private ValueAnimator pseudoEmergencyColorAnimator;
Eric Erfanianccca3152017-02-22 16:32:36 -080040
41 PseudoEmergencyAnimator(ViewProvider viewProvider) {
linyuh183cb712017-12-27 17:02:37 -080042 this.viewProvider = viewProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080043 }
44
45 public void destroy() {
46 end();
linyuh183cb712017-12-27 17:02:37 -080047 viewProvider = null;
Eric Erfanianccca3152017-02-22 16:32:36 -080048 }
49
50 public void start() {
linyuh183cb712017-12-27 17:02:37 -080051 if (pseudoEmergencyColorAnimator == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -080052 Integer colorFrom = Color.BLUE;
53 Integer colorTo = Color.RED;
linyuh183cb712017-12-27 17:02:37 -080054 pseudoEmergencyColorAnimator =
Eric Erfanianccca3152017-02-22 16:32:36 -080055 ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
56
linyuh183cb712017-12-27 17:02:37 -080057 pseudoEmergencyColorAnimator.addUpdateListener(
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070058 animator -> {
59 try {
60 int color = (int) animator.getAnimatedValue();
61 ColorFilter colorFilter = new LightingColorFilter(Color.BLACK, color);
Eric Erfanianccca3152017-02-22 16:32:36 -080062
linyuh183cb712017-12-27 17:02:37 -080063 if (viewProvider.getFab() != null) {
64 viewProvider.getFab().getBackground().setColorFilter(colorFilter);
Eric Erfanianccca3152017-02-22 16:32:36 -080065 }
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070066 } catch (Exception e) {
67 animator.cancel();
Eric Erfanianccca3152017-02-22 16:32:36 -080068 }
69 });
70
linyuh183cb712017-12-27 17:02:37 -080071 pseudoEmergencyColorAnimator.addListener(
Eric Erfanianccca3152017-02-22 16:32:36 -080072 new AnimatorListener() {
73 @Override
74 public void onAnimationCancel(Animator animation) {}
75
76 @Override
77 public void onAnimationRepeat(Animator animation) {
78 try {
79 vibrate(VIBRATE_LENGTH_MILLIS);
80 } catch (Exception e) {
81 animation.cancel();
82 }
83 }
84
85 @Override
86 public void onAnimationStart(Animator animation) {}
87
88 @Override
89 public void onAnimationEnd(Animator animation) {
90 try {
linyuh183cb712017-12-27 17:02:37 -080091 if (viewProvider.getFab() != null) {
92 viewProvider.getFab().getBackground().clearColorFilter();
Eric Erfanianccca3152017-02-22 16:32:36 -080093 }
94
95 new Handler()
96 .postDelayed(
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070097 () -> {
98 try {
99 vibrate(VIBRATE_LENGTH_MILLIS);
100 } catch (Exception e) {
101 // ignored
Eric Erfanianccca3152017-02-22 16:32:36 -0800102 }
103 },
104 ITERATION_LENGTH_MILLIS);
105 } catch (Exception e) {
106 animation.cancel();
107 }
108 }
109 });
110
linyuh183cb712017-12-27 17:02:37 -0800111 pseudoEmergencyColorAnimator.setDuration(VIBRATE_LENGTH_MILLIS);
112 pseudoEmergencyColorAnimator.setRepeatMode(ValueAnimator.REVERSE);
113 pseudoEmergencyColorAnimator.setRepeatCount(ANIMATION_ITERATION_COUNT);
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 }
linyuh183cb712017-12-27 17:02:37 -0800115 if (!pseudoEmergencyColorAnimator.isStarted()) {
116 pseudoEmergencyColorAnimator.start();
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 }
118 }
119
120 public void end() {
linyuh183cb712017-12-27 17:02:37 -0800121 if (pseudoEmergencyColorAnimator != null && pseudoEmergencyColorAnimator.isStarted()) {
122 pseudoEmergencyColorAnimator.end();
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 }
124 }
125
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 private void vibrate(long milliseconds) {
linyuh183cb712017-12-27 17:02:37 -0800127 Context context = viewProvider.getContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 if (context != null) {
129 Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
130 if (vibrator != null) {
131 vibrator.vibrate(milliseconds);
132 }
133 }
134 }
135
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700136 interface ViewProvider {
Eric Erfanianccca3152017-02-22 16:32:36 -0800137
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700138 View getFab();
139
140 Context getContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 }
142}