1 /* 2 * Copyright (C) 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 package com.android.launcher3.anim; 17 18 import android.os.Build; 19 import android.view.View; 20 import android.view.WindowInsets; 21 import android.view.WindowInsetsAnimation; 22 23 import androidx.annotation.RequiresApi; 24 25 import com.android.launcher3.Utilities; 26 27 import java.util.List; 28 29 /** 30 * Callback that animates views above the IME 31 */ 32 @RequiresApi(api = Build.VERSION_CODES.R) 33 public class KeyboardInsetAnimationCallback extends WindowInsetsAnimation.Callback { 34 private final View mView; 35 36 private float mInitialTranslation; 37 private float mTerminalTranslation; 38 KeyboardInsetAnimationCallback(View view)39 public KeyboardInsetAnimationCallback(View view) { 40 super(DISPATCH_MODE_STOP); 41 mView = view; 42 } 43 44 @Override onPrepare(WindowInsetsAnimation animation)45 public void onPrepare(WindowInsetsAnimation animation) { 46 mInitialTranslation = mView.getTranslationY(); 47 } 48 49 50 @Override onProgress(WindowInsets windowInsets, List<WindowInsetsAnimation> list)51 public WindowInsets onProgress(WindowInsets windowInsets, List<WindowInsetsAnimation> list) { 52 if (list.size() == 0) { 53 mView.setTranslationY(mInitialTranslation); 54 return windowInsets; 55 } 56 float progress = list.get(0).getInterpolatedFraction(); 57 58 mView.setTranslationY( 59 Utilities.mapRange(progress, mInitialTranslation, mTerminalTranslation)); 60 61 return windowInsets; 62 } 63 64 @Override onStart(WindowInsetsAnimation animation, WindowInsetsAnimation.Bounds bounds)65 public WindowInsetsAnimation.Bounds onStart(WindowInsetsAnimation animation, 66 WindowInsetsAnimation.Bounds bounds) { 67 mTerminalTranslation = mView.getTranslationY(); 68 mView.setTranslationY(mInitialTranslation); 69 return super.onStart(animation, bounds); 70 } 71 } 72