1 /* 2 * Copyright 2021 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.shared.recents.utilities; 18 19 import android.view.View; 20 21 /** 22 * Shows view ripples by toggling the provided Views "pressed" state. 23 * Ripples 4 times. 24 */ 25 public class ViewRippler { 26 private static final int RIPPLE_OFFSET_MS = 50; 27 private static final int RIPPLE_INTERVAL_MS = 2000; 28 private View mRoot; 29 start(View root)30 public void start(View root) { 31 stop(); // Stop any pending ripple animations 32 33 mRoot = root; 34 35 // Schedule pending ripples, offset the 1st to avoid problems with visibility change 36 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS); 37 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS); 38 mRoot.postOnAnimationDelayed(mRipple, 2 * RIPPLE_INTERVAL_MS); 39 mRoot.postOnAnimationDelayed(mRipple, 3 * RIPPLE_INTERVAL_MS); 40 mRoot.postOnAnimationDelayed(mRipple, 4 * RIPPLE_INTERVAL_MS); 41 } 42 stop()43 public void stop() { 44 if (mRoot != null) mRoot.removeCallbacks(mRipple); 45 } 46 47 private final Runnable mRipple = new Runnable() { 48 @Override 49 public void run() { // Cause the ripple to fire via false presses 50 if (!mRoot.isAttachedToWindow()) return; 51 mRoot.setPressed(true /* pressed */); 52 mRoot.setPressed(false /* pressed */); 53 } 54 }; 55 }