1 /* 2 * Copyright (C) 2016 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.deskclock.widget.toast; 18 19 import android.content.Context; 20 import androidx.annotation.Keep; 21 import androidx.coordinatorlayout.widget.CoordinatorLayout; 22 import com.google.android.material.snackbar.Snackbar; 23 import android.util.AttributeSet; 24 import android.view.View; 25 26 /** 27 * Custom {@link CoordinatorLayout.Behavior} that slides with the {@link Snackbar}. 28 */ 29 @Keep 30 public final class SnackbarSlidingBehavior extends CoordinatorLayout.Behavior<View> { 31 SnackbarSlidingBehavior(Context context, AttributeSet attrs)32 public SnackbarSlidingBehavior(Context context, AttributeSet attrs) { 33 } 34 35 @Override layoutDependsOn(CoordinatorLayout parent, View child, View dependency)36 public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { 37 return dependency instanceof Snackbar.SnackbarLayout; 38 } 39 40 @Override onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)41 public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { 42 updateTranslationY(parent, child); 43 return false; 44 } 45 46 @Override onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency)47 public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) { 48 updateTranslationY(parent, child); 49 } 50 51 @Override onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection)52 public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) { 53 updateTranslationY(parent, child); 54 return false; 55 } 56 updateTranslationY(CoordinatorLayout parent, View child)57 private void updateTranslationY(CoordinatorLayout parent, View child) { 58 float translationY = 0f; 59 for (View dependency : parent.getDependencies(child)) { 60 translationY = Math.min(translationY, dependency.getY() - child.getBottom()); 61 } 62 child.setTranslationY(translationY); 63 } 64 } 65