Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 17 | package com.android.dialer.dialpadview; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.ArgbEvaluator; |
| 22 | import android.animation.ValueAnimator; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.ColorFilter; |
| 26 | import android.graphics.LightingColorFilter; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Vibrator; |
| 29 | import android.view.View; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 30 | |
| 31 | /** Animates the dial button on "emergency" phone numbers. */ |
| 32 | public class PseudoEmergencyAnimator { |
| 33 | |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 34 | static final String PSEUDO_EMERGENCY_NUMBER = "01189998819991197253"; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 35 | 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; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 38 | private ViewProvider viewProvider; |
| 39 | private ValueAnimator pseudoEmergencyColorAnimator; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 40 | |
| 41 | PseudoEmergencyAnimator(ViewProvider viewProvider) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 42 | this.viewProvider = viewProvider; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public void destroy() { |
| 46 | end(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 47 | viewProvider = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public void start() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 51 | if (pseudoEmergencyColorAnimator == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 52 | Integer colorFrom = Color.BLUE; |
| 53 | Integer colorTo = Color.RED; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 54 | pseudoEmergencyColorAnimator = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 55 | ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); |
| 56 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 57 | pseudoEmergencyColorAnimator.addUpdateListener( |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 58 | animator -> { |
| 59 | try { |
| 60 | int color = (int) animator.getAnimatedValue(); |
| 61 | ColorFilter colorFilter = new LightingColorFilter(Color.BLACK, color); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 62 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 63 | if (viewProvider.getFab() != null) { |
| 64 | viewProvider.getFab().getBackground().setColorFilter(colorFilter); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | } |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 66 | } catch (Exception e) { |
| 67 | animator.cancel(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 68 | } |
| 69 | }); |
| 70 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 71 | pseudoEmergencyColorAnimator.addListener( |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 72 | 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 { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 91 | if (viewProvider.getFab() != null) { |
| 92 | viewProvider.getFab().getBackground().clearColorFilter(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | new Handler() |
| 96 | .postDelayed( |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 97 | () -> { |
| 98 | try { |
| 99 | vibrate(VIBRATE_LENGTH_MILLIS); |
| 100 | } catch (Exception e) { |
| 101 | // ignored |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 102 | } |
| 103 | }, |
| 104 | ITERATION_LENGTH_MILLIS); |
| 105 | } catch (Exception e) { |
| 106 | animation.cancel(); |
| 107 | } |
| 108 | } |
| 109 | }); |
| 110 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 111 | pseudoEmergencyColorAnimator.setDuration(VIBRATE_LENGTH_MILLIS); |
| 112 | pseudoEmergencyColorAnimator.setRepeatMode(ValueAnimator.REVERSE); |
| 113 | pseudoEmergencyColorAnimator.setRepeatCount(ANIMATION_ITERATION_COUNT); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 114 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 115 | if (!pseudoEmergencyColorAnimator.isStarted()) { |
| 116 | pseudoEmergencyColorAnimator.start(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | public void end() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 121 | if (pseudoEmergencyColorAnimator != null && pseudoEmergencyColorAnimator.isStarted()) { |
| 122 | pseudoEmergencyColorAnimator.end(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 126 | private void vibrate(long milliseconds) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 127 | Context context = viewProvider.getContext(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 128 | 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 Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 136 | interface ViewProvider { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 137 | |
Eric Erfanian | fc0eb8c | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 138 | View getFab(); |
| 139 | |
| 140 | Context getContext(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 141 | } |
| 142 | } |