1 /* 2 * Copyright (C) 2014 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 17 package com.android.systemui.qs; 18 19 import android.animation.Animator; 20 import android.animation.Animator.AnimatorListener; 21 import android.animation.AnimatorListenerAdapter; 22 import android.graphics.drawable.TransitionDrawable; 23 import android.view.View; 24 import android.view.ViewAnimationUtils; 25 26 /** Helper for quick settings detail panel clip animations. **/ 27 public class QSDetailClipper { 28 29 private final View mDetail; 30 private final TransitionDrawable mBackground; 31 32 private Animator mAnimator; 33 QSDetailClipper(View detail)34 public QSDetailClipper(View detail) { 35 mDetail = detail; 36 mBackground = (TransitionDrawable) detail.getBackground(); 37 } 38 animateCircularClip(int x, int y, boolean in, AnimatorListener listener)39 public void animateCircularClip(int x, int y, boolean in, AnimatorListener listener) { 40 updateCircularClip(true /* animate */, x, y, in, listener); 41 } 42 43 /** 44 * @param animate whether or not animation has a duration of 0. Either way, {@code listener} 45 * will be called. 46 * @param x x position where animation should originate 47 * @param y y position where animation should originate 48 * @param in whether animating in or out 49 * @param listener Animation listener. Called whether or not {@code animate} is true. 50 */ updateCircularClip(boolean animate, int x, int y, boolean in, AnimatorListener listener)51 public void updateCircularClip(boolean animate, int x, int y, boolean in, 52 AnimatorListener listener) { 53 if (mAnimator != null) { 54 mAnimator.cancel(); 55 } 56 final int w = mDetail.getWidth() - x; 57 final int h = mDetail.getHeight() - y; 58 int innerR = 0; 59 if (x < 0 || w < 0 || y < 0 || h < 0) { 60 innerR = Math.abs(x); 61 innerR = Math.min(innerR, Math.abs(y)); 62 innerR = Math.min(innerR, Math.abs(w)); 63 innerR = Math.min(innerR, Math.abs(h)); 64 } 65 int r = (int) Math.ceil(Math.sqrt(x * x + y * y)); 66 r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + y * y))); 67 r = (int) Math.max(r, Math.ceil(Math.sqrt(w * w + h * h))); 68 r = (int) Math.max(r, Math.ceil(Math.sqrt(x * x + h * h))); 69 if (in) { 70 mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, innerR, r); 71 } else { 72 mAnimator = ViewAnimationUtils.createCircularReveal(mDetail, x, y, r, innerR); 73 } 74 mAnimator.setDuration(animate ? (long) (mAnimator.getDuration() * 1.5) : 0); 75 if (listener != null) { 76 mAnimator.addListener(listener); 77 } 78 if (in) { 79 mBackground.startTransition(animate ? (int) (mAnimator.getDuration() * 0.6) : 0); 80 mAnimator.addListener(mVisibleOnStart); 81 } else { 82 mDetail.postDelayed(mReverseBackground, 83 animate ? (long) (mAnimator.getDuration() * 0.65) : 0); 84 mAnimator.addListener(mGoneOnEnd); 85 } 86 mAnimator.start(); 87 } 88 89 private final Runnable mReverseBackground = new Runnable() { 90 @Override 91 public void run() { 92 if (mAnimator != null) { 93 mBackground.reverseTransition((int)(mAnimator.getDuration() * 0.35)); 94 } 95 } 96 }; 97 98 private final AnimatorListenerAdapter mVisibleOnStart = new AnimatorListenerAdapter() { 99 @Override 100 public void onAnimationStart(Animator animation) { 101 mDetail.setVisibility(View.VISIBLE); 102 } 103 104 public void onAnimationEnd(Animator animation) { 105 mAnimator = null; 106 } 107 }; 108 109 private final AnimatorListenerAdapter mGoneOnEnd = new AnimatorListenerAdapter() { 110 @Override 111 public void onAnimationEnd(Animator animation) { 112 mDetail.setVisibility(View.GONE); 113 mBackground.resetTransition(); 114 mAnimator = null; 115 }; 116 }; 117 showBackground()118 public void showBackground() { 119 mBackground.showSecondLayer(); 120 } 121 122 /** 123 * Cancels the animator if it's running. 124 */ cancelAnimator()125 public void cancelAnimator() { 126 if (mAnimator != null) { 127 mAnimator.cancel(); 128 } 129 } 130 } 131