2021-05-13 14:13:18 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.widget;
|
|
|
|
|
|
2021-06-09 16:41:22 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2021-05-13 14:13:18 +01:00
|
|
|
import android.content.Context;
|
2021-06-09 16:41:22 +01:00
|
|
|
import android.graphics.Insets;
|
2021-05-13 14:13:18 +01:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.util.AttributeSet;
|
2021-07-08 17:13:29 +01:00
|
|
|
import android.view.MotionEvent;
|
2021-06-09 16:41:22 +01:00
|
|
|
import android.view.View;
|
2021-06-01 15:17:16 +01:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.view.ViewParent;
|
2021-06-09 16:41:22 +01:00
|
|
|
import android.view.WindowInsets;
|
2021-07-08 17:13:29 +01:00
|
|
|
import android.widget.ScrollView;
|
2021-05-13 14:13:18 +01:00
|
|
|
|
2021-06-09 16:41:22 +01:00
|
|
|
import com.android.launcher3.DeviceProfile;
|
2021-06-01 15:17:16 +01:00
|
|
|
import com.android.launcher3.R;
|
2021-05-13 14:13:18 +01:00
|
|
|
import com.android.launcher3.dragndrop.AddItemActivity;
|
|
|
|
|
import com.android.launcher3.views.AbstractSlideInView;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bottom sheet for the pin widget.
|
|
|
|
|
*/
|
2021-06-09 16:41:22 +01:00
|
|
|
public class AddItemWidgetsBottomSheet extends AbstractSlideInView<AddItemActivity> implements
|
|
|
|
|
View.OnApplyWindowInsetsListener {
|
2021-05-13 14:13:18 +01:00
|
|
|
|
|
|
|
|
private static final int DEFAULT_CLOSE_DURATION = 200;
|
|
|
|
|
|
2021-06-09 16:41:22 +01:00
|
|
|
private final Rect mInsets;
|
2021-07-08 17:13:29 +01:00
|
|
|
private ScrollView mWidgetPreviewScrollView;
|
2021-05-13 14:13:18 +01:00
|
|
|
|
2021-07-26 21:31:50 +01:00
|
|
|
private int mContentHorizontalMarginInPx;
|
|
|
|
|
|
2021-05-13 14:13:18 +01:00
|
|
|
public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
mInsets = new Rect();
|
2021-07-26 21:31:50 +01:00
|
|
|
mContentHorizontalMarginInPx = getResources().getDimensionPixelSize(
|
|
|
|
|
R.dimen.widget_list_horizontal_margin);
|
2021-06-01 15:17:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attaches to activity container and animates open the bottom sheet.
|
|
|
|
|
*/
|
|
|
|
|
public void show() {
|
|
|
|
|
ViewParent parent = getParent();
|
|
|
|
|
if (parent instanceof ViewGroup) {
|
|
|
|
|
((ViewGroup) parent).removeView(this);
|
|
|
|
|
}
|
|
|
|
|
attachToContainer();
|
2021-06-09 16:41:22 +01:00
|
|
|
setOnApplyWindowInsetsListener(this);
|
2021-05-13 14:13:18 +01:00
|
|
|
animateOpen();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 17:13:29 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
|
|
|
|
|
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
|
|
|
|
|
mNoIntercept = false;
|
|
|
|
|
// Suppress drag to dismiss gesture if the scroll view is being scrolled.
|
|
|
|
|
if (getPopupContainer().isEventOverView(mWidgetPreviewScrollView, ev)
|
|
|
|
|
&& mWidgetPreviewScrollView.getScrollY() > 0) {
|
|
|
|
|
mNoIntercept = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return super.onControllerInterceptTouchEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:13:18 +01:00
|
|
|
@Override
|
|
|
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
2021-06-09 16:41:22 +01:00
|
|
|
int width = r - l;
|
|
|
|
|
int height = b - t;
|
|
|
|
|
|
|
|
|
|
// Lay out content as center bottom aligned.
|
|
|
|
|
int contentWidth = mContent.getMeasuredWidth();
|
|
|
|
|
int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
|
|
|
|
|
mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
|
|
|
|
|
contentLeft + contentWidth, height);
|
|
|
|
|
|
2021-05-13 14:13:18 +01:00
|
|
|
setTranslationShift(mTranslationShift);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 16:41:22 +01:00
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
|
DeviceProfile deviceProfile = mActivityContext.getDeviceProfile();
|
|
|
|
|
int widthUsed;
|
2022-05-06 14:40:07 +01:00
|
|
|
if (deviceProfile.isTablet) {
|
|
|
|
|
int margin = deviceProfile.allAppsLeftRightMargin;
|
|
|
|
|
widthUsed = Math.max(2 * margin, 2 * (mInsets.left + mInsets.right));
|
|
|
|
|
} else if (mInsets.bottom > 0) {
|
2021-06-09 16:41:22 +01:00
|
|
|
widthUsed = mInsets.left + mInsets.right;
|
|
|
|
|
} else {
|
|
|
|
|
Rect padding = deviceProfile.workspacePadding;
|
|
|
|
|
widthUsed = Math.max(padding.left + padding.right,
|
|
|
|
|
2 * (mInsets.left + mInsets.right));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
measureChildWithMargins(mContent, widthMeasureSpec,
|
2022-05-06 14:40:07 +01:00
|
|
|
widthUsed, heightMeasureSpec, deviceProfile.bottomSheetTopPadding);
|
2021-06-09 16:41:22 +01:00
|
|
|
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
|
|
|
|
|
MeasureSpec.getSize(heightMeasureSpec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
mContent = findViewById(R.id.add_item_bottom_sheet_content);
|
2021-07-08 17:13:29 +01:00
|
|
|
mWidgetPreviewScrollView = findViewById(R.id.widget_preview_scroll_view);
|
2021-06-09 16:41:22 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:13:18 +01:00
|
|
|
private void animateOpen() {
|
2023-08-07 21:25:21 +00:00
|
|
|
if (mIsOpen || mOpenCloseAnimation.getAnimationPlayer().isRunning()) {
|
2021-05-13 14:13:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mIsOpen = true;
|
2023-08-07 21:25:21 +00:00
|
|
|
setUpDefaultOpenAnimation().start();
|
2021-05-13 14:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void handleClose(boolean animate) {
|
|
|
|
|
handleClose(animate, DEFAULT_CLOSE_DURATION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean isOfType(@FloatingViewType int type) {
|
|
|
|
|
return (type & TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-06-09 16:41:22 +01:00
|
|
|
protected int getScrimColor(Context context) {
|
|
|
|
|
return context.getResources().getColor(R.color.widgets_picker_scrim);
|
2021-05-13 14:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 16:41:22 +01:00
|
|
|
@SuppressLint("NewApi") // Already added API check.
|
2021-05-13 14:13:18 +01:00
|
|
|
@Override
|
2021-06-09 16:41:22 +01:00
|
|
|
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
|
2024-01-24 14:38:48 -08:00
|
|
|
Insets insets = windowInsets.getInsets(WindowInsets.Type.systemBars());
|
|
|
|
|
mInsets.set(insets.left, insets.top, insets.right, insets.bottom);
|
|
|
|
|
mContent.setPadding(mContent.getPaddingStart(), mContent.getPaddingTop(),
|
|
|
|
|
mContent.getPaddingEnd(), mInsets.bottom);
|
2021-07-26 21:31:50 +01:00
|
|
|
|
|
|
|
|
int contentHorizontalMarginInPx = getResources().getDimensionPixelSize(
|
|
|
|
|
R.dimen.widget_list_horizontal_margin);
|
|
|
|
|
if (contentHorizontalMarginInPx != mContentHorizontalMarginInPx) {
|
|
|
|
|
setContentHorizontalMargin(findViewById(R.id.widget_appName),
|
|
|
|
|
contentHorizontalMarginInPx);
|
|
|
|
|
setContentHorizontalMargin(findViewById(R.id.widget_drag_instruction),
|
|
|
|
|
contentHorizontalMarginInPx);
|
|
|
|
|
setContentHorizontalMargin(findViewById(R.id.widget_cell), contentHorizontalMarginInPx);
|
|
|
|
|
setContentHorizontalMargin(findViewById(R.id.actions_container),
|
|
|
|
|
contentHorizontalMarginInPx);
|
|
|
|
|
mContentHorizontalMarginInPx = contentHorizontalMarginInPx;
|
|
|
|
|
}
|
2021-06-09 16:41:22 +01:00
|
|
|
return windowInsets;
|
2021-06-01 15:17:16 +01:00
|
|
|
}
|
2021-07-26 21:31:50 +01:00
|
|
|
|
|
|
|
|
private static void setContentHorizontalMargin(View view, int contentHorizontalMargin) {
|
|
|
|
|
ViewGroup.MarginLayoutParams layoutParams =
|
|
|
|
|
((ViewGroup.MarginLayoutParams) view.getLayoutParams());
|
|
|
|
|
layoutParams.setMarginStart(contentHorizontalMargin);
|
|
|
|
|
layoutParams.setMarginEnd(contentHorizontalMargin);
|
|
|
|
|
}
|
2021-05-13 14:13:18 +01:00
|
|
|
}
|