1 /* 2 * Copyright (C) 2017 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.launcher3.widget; 18 19 import static com.android.launcher3.Utilities.ATLEAST_R; 20 import static com.android.launcher3.anim.Interpolators.FAST_OUT_SLOW_IN; 21 22 import android.animation.PropertyValuesHolder; 23 import android.annotation.SuppressLint; 24 import android.content.Context; 25 import android.graphics.Insets; 26 import android.graphics.Rect; 27 import android.util.AttributeSet; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.view.ViewParent; 31 import android.view.WindowInsets; 32 33 import com.android.launcher3.DeviceProfile; 34 import com.android.launcher3.R; 35 import com.android.launcher3.dragndrop.AddItemActivity; 36 import com.android.launcher3.views.AbstractSlideInView; 37 38 /** 39 * Bottom sheet for the pin widget. 40 */ 41 public class AddItemWidgetsBottomSheet extends AbstractSlideInView<AddItemActivity> implements 42 View.OnApplyWindowInsetsListener { 43 44 private static final int DEFAULT_CLOSE_DURATION = 200; 45 46 private final Rect mInsets; 47 AddItemWidgetsBottomSheet(Context context, AttributeSet attrs)48 public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 AddItemWidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr)52 public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) { 53 super(context, attrs, defStyleAttr); 54 mInsets = new Rect(); 55 } 56 57 /** 58 * Attaches to activity container and animates open the bottom sheet. 59 */ show()60 public void show() { 61 ViewParent parent = getParent(); 62 if (parent instanceof ViewGroup) { 63 ((ViewGroup) parent).removeView(this); 64 } 65 attachToContainer(); 66 setOnApplyWindowInsetsListener(this); 67 animateOpen(); 68 } 69 70 @Override onLayout(boolean changed, int l, int t, int r, int b)71 protected void onLayout(boolean changed, int l, int t, int r, int b) { 72 int width = r - l; 73 int height = b - t; 74 75 // Lay out content as center bottom aligned. 76 int contentWidth = mContent.getMeasuredWidth(); 77 int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left; 78 mContent.layout(contentLeft, height - mContent.getMeasuredHeight(), 79 contentLeft + contentWidth, height); 80 81 setTranslationShift(mTranslationShift); 82 } 83 84 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)85 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 86 DeviceProfile deviceProfile = mActivityContext.getDeviceProfile(); 87 int widthUsed; 88 if (mInsets.bottom > 0) { 89 widthUsed = mInsets.left + mInsets.right; 90 } else { 91 Rect padding = deviceProfile.workspacePadding; 92 widthUsed = Math.max(padding.left + padding.right, 93 2 * (mInsets.left + mInsets.right)); 94 } 95 96 int heightUsed = mInsets.top + deviceProfile.edgeMarginPx; 97 measureChildWithMargins(mContent, widthMeasureSpec, 98 widthUsed, heightMeasureSpec, heightUsed); 99 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 100 MeasureSpec.getSize(heightMeasureSpec)); 101 } 102 103 @Override onFinishInflate()104 protected void onFinishInflate() { 105 super.onFinishInflate(); 106 mContent = findViewById(R.id.add_item_bottom_sheet_content); 107 } 108 animateOpen()109 private void animateOpen() { 110 if (mIsOpen || mOpenCloseAnimator.isRunning()) { 111 return; 112 } 113 mIsOpen = true; 114 mOpenCloseAnimator.setValues( 115 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED)); 116 mOpenCloseAnimator.setInterpolator(FAST_OUT_SLOW_IN); 117 mOpenCloseAnimator.start(); 118 } 119 120 @Override handleClose(boolean animate)121 protected void handleClose(boolean animate) { 122 handleClose(animate, DEFAULT_CLOSE_DURATION); 123 } 124 125 @Override isOfType(@loatingViewType int type)126 protected boolean isOfType(@FloatingViewType int type) { 127 return (type & TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP) != 0; 128 } 129 130 @Override getScrimColor(Context context)131 protected int getScrimColor(Context context) { 132 return context.getResources().getColor(R.color.widgets_picker_scrim); 133 } 134 135 @SuppressLint("NewApi") // Already added API check. 136 @Override onApplyWindowInsets(View view, WindowInsets windowInsets)137 public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) { 138 if (ATLEAST_R) { 139 Insets insets = windowInsets.getInsets(WindowInsets.Type.systemBars()); 140 mInsets.set(insets.left, insets.top, insets.right, insets.bottom); 141 } else { 142 mInsets.set(windowInsets.getSystemWindowInsetLeft(), 143 windowInsets.getSystemWindowInsetTop(), 144 windowInsets.getSystemWindowInsetRight(), 145 windowInsets.getSystemWindowInsetBottom()); 146 } 147 mContent.setPadding(mContent.getPaddingStart(), 148 mContent.getPaddingTop(), mContent.getPaddingEnd(), mInsets.bottom); 149 return windowInsets; 150 } 151 } 152