1 /* 2 * Copyright 2018 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 androidx.browser.browseractions; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorListenerAdapter; 21 import android.app.Dialog; 22 import android.content.Context; 23 import android.graphics.Color; 24 import android.graphics.drawable.ColorDrawable; 25 import android.view.MotionEvent; 26 import android.view.View; 27 import android.view.Window; 28 29 import androidx.interpolator.view.animation.LinearOutSlowInInterpolator; 30 31 /** 32 * The dialog class showing the context menu and ensures proper animation is played upon calling 33 * {@link #show()} and {@link #dismiss()}. 34 */ 35 class BrowserActionsFallbackMenuDialog extends Dialog { 36 private static final long ENTER_ANIMATION_DURATION_MS = 250; 37 // Exit animation duration should be set to 60% of the enter animation duration. 38 private static final long EXIT_ANIMATION_DURATION_MS = 150; 39 private final View mContentView; 40 41 BrowserActionsFallbackMenuDialog(Context context, View contentView) { 42 super(context); 43 mContentView = contentView; 44 } 45 46 @Override 47 public void show() { 48 Window dialogWindow = getWindow(); 49 dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 50 startAnimation(true); 51 super.show(); 52 } 53 54 @Override 55 public boolean onTouchEvent(MotionEvent event) { 56 if (event.getAction() == MotionEvent.ACTION_DOWN) { 57 dismiss(); 58 return true; 59 } 60 return false; 61 } 62 63 @Override 64 public void dismiss() { 65 startAnimation(false); 66 } 67 68 private void startAnimation(final boolean isEnterAnimation) { 69 float from = isEnterAnimation ? 0f : 1f; 70 float to = isEnterAnimation ? 1f : 0f; 71 long duration = isEnterAnimation ? ENTER_ANIMATION_DURATION_MS : EXIT_ANIMATION_DURATION_MS; 72 mContentView.setScaleX(from); 73 mContentView.setScaleY(from); 74 75 mContentView.animate() 76 .scaleX(to) 77 .scaleY(to) 78 .setDuration(duration) 79 .setInterpolator(new LinearOutSlowInInterpolator()) 80 .setListener(new AnimatorListenerAdapter() { 81 @Override 82 public void onAnimationEnd(Animator animation) { 83 if (!isEnterAnimation) { 84 BrowserActionsFallbackMenuDialog.super.dismiss(); 85 } 86 } 87 }) 88 .start(); 89 } 90 } 91