2017-03-20 17:12:24 -07: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;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.util.AttributeSet;
|
2017-04-11 17:21:12 -07:00
|
|
|
import android.view.Gravity;
|
2017-03-20 17:12:24 -07:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
2017-06-09 08:42:39 -07:00
|
|
|
import android.view.animation.AnimationUtils;
|
2017-03-20 17:12:24 -07:00
|
|
|
import android.view.animation.Interpolator;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.AbstractFloatingView;
|
|
|
|
|
import com.android.launcher3.DropTarget;
|
|
|
|
|
import com.android.launcher3.Insettable;
|
|
|
|
|
import com.android.launcher3.ItemInfo;
|
|
|
|
|
import com.android.launcher3.Launcher;
|
|
|
|
|
import com.android.launcher3.LauncherAnimUtils;
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
|
|
|
|
import com.android.launcher3.R;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
|
|
|
|
import com.android.launcher3.anim.PropertyListBuilder;
|
|
|
|
|
import com.android.launcher3.dragndrop.DragController;
|
|
|
|
|
import com.android.launcher3.dragndrop.DragOptions;
|
2017-06-28 16:48:55 -07:00
|
|
|
import com.android.launcher3.graphics.GradientView;
|
2017-03-20 17:12:24 -07:00
|
|
|
import com.android.launcher3.model.WidgetItem;
|
2017-09-28 13:43:24 -07:00
|
|
|
import com.android.launcher3.touch.SwipeDetector;
|
|
|
|
|
import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
|
2017-03-20 17:12:24 -07:00
|
|
|
import com.android.launcher3.util.PackageUserKey;
|
2017-06-20 10:03:56 -07:00
|
|
|
import com.android.launcher3.util.SystemUiController;
|
2017-06-27 15:27:41 -07:00
|
|
|
import com.android.launcher3.util.Themes;
|
2017-03-20 17:12:24 -07:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-12 18:31:09 -07:00
|
|
|
* Bottom sheet for the "Widgets" system shortcut in the long-press popup.
|
2017-03-20 17:12:24 -07:00
|
|
|
*/
|
2017-09-28 13:43:24 -07:00
|
|
|
public class WidgetsBottomSheet extends AbstractFloatingView implements Insettable,
|
2017-07-06 12:35:55 -07:00
|
|
|
SwipeDetector.Listener, View.OnClickListener, View.OnLongClickListener,
|
2017-03-20 17:12:24 -07:00
|
|
|
DragController.DragListener {
|
|
|
|
|
|
|
|
|
|
private int mTranslationYOpen;
|
|
|
|
|
private int mTranslationYClosed;
|
|
|
|
|
private float mTranslationYRange;
|
|
|
|
|
|
|
|
|
|
private Launcher mLauncher;
|
2017-03-20 17:12:24 -07:00
|
|
|
private ItemInfo mOriginalItemInfo;
|
2017-03-20 17:12:24 -07:00
|
|
|
private ObjectAnimator mOpenCloseAnimator;
|
|
|
|
|
private Interpolator mFastOutSlowInInterpolator;
|
2017-07-06 12:35:55 -07:00
|
|
|
private SwipeDetector.ScrollInterpolator mScrollInterpolator;
|
2017-03-20 17:12:24 -07:00
|
|
|
private Rect mInsets;
|
2017-07-06 12:35:55 -07:00
|
|
|
private SwipeDetector mSwipeDetector;
|
2017-06-28 16:48:55 -07:00
|
|
|
private GradientView mGradientBackground;
|
2017-03-20 17:12:24 -07:00
|
|
|
|
2017-04-12 18:31:09 -07:00
|
|
|
public WidgetsBottomSheet(Context context, AttributeSet attrs) {
|
2017-03-20 17:12:24 -07:00
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-12 18:31:09 -07:00
|
|
|
public WidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) {
|
2017-06-26 10:48:41 -07:00
|
|
|
super(context, attrs, defStyleAttr);
|
2017-03-20 17:12:24 -07:00
|
|
|
setWillNotDraw(false);
|
|
|
|
|
mLauncher = Launcher.getLauncher(context);
|
|
|
|
|
mOpenCloseAnimator = LauncherAnimUtils.ofPropertyValuesHolder(this);
|
2017-06-09 08:42:39 -07:00
|
|
|
mFastOutSlowInInterpolator =
|
|
|
|
|
AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
|
2017-07-06 12:35:55 -07:00
|
|
|
mScrollInterpolator = new SwipeDetector.ScrollInterpolator();
|
2017-03-20 17:12:24 -07:00
|
|
|
mInsets = new Rect();
|
2017-07-14 00:02:27 -07:00
|
|
|
mSwipeDetector = new SwipeDetector(context, this, SwipeDetector.VERTICAL);
|
2017-08-03 13:59:12 -07:00
|
|
|
mGradientBackground = (GradientView) mLauncher.getLayoutInflater().inflate(
|
|
|
|
|
R.layout.gradient_bg, mLauncher.getDragLayer(), false);
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
|
mTranslationYOpen = 0;
|
|
|
|
|
mTranslationYClosed = getMeasuredHeight();
|
|
|
|
|
mTranslationYRange = mTranslationYClosed - mTranslationYOpen;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
public void populateAndShow(ItemInfo itemInfo) {
|
|
|
|
|
mOriginalItemInfo = itemInfo;
|
2017-04-11 17:21:12 -07:00
|
|
|
((TextView) findViewById(R.id.title)).setText(getContext().getString(
|
|
|
|
|
R.string.widgets_bottom_sheet_title, mOriginalItemInfo.title));
|
2017-03-20 17:12:24 -07:00
|
|
|
|
|
|
|
|
onWidgetsBound();
|
|
|
|
|
|
2017-08-03 13:59:12 -07:00
|
|
|
mLauncher.getDragLayer().addView(mGradientBackground);
|
2017-03-20 17:12:24 -07:00
|
|
|
mLauncher.getDragLayer().addView(this);
|
|
|
|
|
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
|
|
|
|
|
setTranslationY(mTranslationYClosed);
|
|
|
|
|
mIsOpen = false;
|
|
|
|
|
open(true);
|
|
|
|
|
}
|
2017-03-20 17:12:24 -07:00
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onWidgetsBound() {
|
|
|
|
|
List<WidgetItem> widgets = mLauncher.getWidgetsForPackageUser(new PackageUserKey(
|
|
|
|
|
mOriginalItemInfo.getTargetComponent().getPackageName(), mOriginalItemInfo.user));
|
2017-03-20 17:12:24 -07:00
|
|
|
|
|
|
|
|
ViewGroup widgetRow = (ViewGroup) findViewById(R.id.widgets);
|
|
|
|
|
ViewGroup widgetCells = (ViewGroup) widgetRow.findViewById(R.id.widgets_cell_list);
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
widgetCells.removeAllViews();
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
for (int i = 0; i < widgets.size(); i++) {
|
2017-04-11 17:21:12 -07:00
|
|
|
WidgetCell widget = addItemCell(widgetCells);
|
|
|
|
|
widget.applyFromCellItem(widgets.get(i), LauncherAppState.getInstance(mLauncher)
|
|
|
|
|
.getWidgetCache());
|
|
|
|
|
widget.ensurePreview();
|
|
|
|
|
widget.setVisibility(View.VISIBLE);
|
2017-03-20 17:12:24 -07:00
|
|
|
if (i < widgets.size() - 1) {
|
|
|
|
|
addDivider(widgetCells);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 21:56:27 -07:00
|
|
|
if (widgets.size() == 1) {
|
|
|
|
|
// If there is only one widget, we want to center it instead of left-align.
|
|
|
|
|
WidgetsBottomSheet.LayoutParams params = (WidgetsBottomSheet.LayoutParams)
|
|
|
|
|
widgetRow.getLayoutParams();
|
|
|
|
|
params.gravity = Gravity.CENTER_HORIZONTAL;
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise, add an empty view to the start as padding (but still scroll edge to edge).
|
|
|
|
|
View leftPaddingView = LayoutInflater.from(getContext()).inflate(
|
|
|
|
|
R.layout.widget_list_divider, widgetRow, false);
|
|
|
|
|
leftPaddingView.getLayoutParams().width = Utilities.pxFromDp(
|
|
|
|
|
16, getResources().getDisplayMetrics());
|
|
|
|
|
widgetCells.addView(leftPaddingView, 0);
|
|
|
|
|
}
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addDivider(ViewGroup parent) {
|
|
|
|
|
LayoutInflater.from(getContext()).inflate(R.layout.widget_list_divider, parent, true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 17:21:12 -07:00
|
|
|
private WidgetCell addItemCell(ViewGroup parent) {
|
2017-03-20 17:12:24 -07:00
|
|
|
WidgetCell widget = (WidgetCell) LayoutInflater.from(getContext()).inflate(
|
|
|
|
|
R.layout.widget_cell, parent, false);
|
|
|
|
|
|
|
|
|
|
widget.setOnClickListener(this);
|
|
|
|
|
widget.setOnLongClickListener(this);
|
|
|
|
|
widget.setAnimatePreview(false);
|
|
|
|
|
|
|
|
|
|
parent.addView(widget);
|
2017-04-11 17:21:12 -07:00
|
|
|
return widget;
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
mLauncher.getWidgetsView().handleClick();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View view) {
|
|
|
|
|
mLauncher.getDragController().addDragListener(this);
|
|
|
|
|
return mLauncher.getWidgetsView().handleLongClick(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void open(boolean animate) {
|
|
|
|
|
if (mIsOpen || mOpenCloseAnimator.isRunning()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mIsOpen = true;
|
2017-06-27 15:27:41 -07:00
|
|
|
boolean isSheetDark = Themes.getAttrBoolean(mLauncher, R.attr.isMainColorDark);
|
2017-06-20 10:03:56 -07:00
|
|
|
mLauncher.getSystemUiController().updateUiState(
|
2017-06-27 15:27:41 -07:00
|
|
|
SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET,
|
|
|
|
|
isSheetDark ? SystemUiController.FLAG_DARK_NAV : SystemUiController.FLAG_LIGHT_NAV);
|
2017-03-20 17:12:24 -07:00
|
|
|
if (animate) {
|
|
|
|
|
mOpenCloseAnimator.setValues(new PropertyListBuilder()
|
|
|
|
|
.translationY(mTranslationYOpen).build());
|
|
|
|
|
mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
2017-07-06 12:35:55 -07:00
|
|
|
mSwipeDetector.finishedScrolling();
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mOpenCloseAnimator.setInterpolator(mFastOutSlowInInterpolator);
|
|
|
|
|
mOpenCloseAnimator.start();
|
|
|
|
|
} else {
|
|
|
|
|
setTranslationY(mTranslationYOpen);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void handleClose(boolean animate) {
|
|
|
|
|
if (!mIsOpen || mOpenCloseAnimator.isRunning()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (animate) {
|
|
|
|
|
mOpenCloseAnimator.setValues(new PropertyListBuilder()
|
|
|
|
|
.translationY(mTranslationYClosed).build());
|
|
|
|
|
mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
2017-07-06 12:35:55 -07:00
|
|
|
mSwipeDetector.finishedScrolling();
|
2017-08-03 13:59:12 -07:00
|
|
|
onCloseComplete();
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
});
|
2017-07-06 12:35:55 -07:00
|
|
|
mOpenCloseAnimator.setInterpolator(mSwipeDetector.isIdleState()
|
2017-03-20 17:12:24 -07:00
|
|
|
? mFastOutSlowInInterpolator : mScrollInterpolator);
|
|
|
|
|
mOpenCloseAnimator.start();
|
|
|
|
|
} else {
|
|
|
|
|
setTranslationY(mTranslationYClosed);
|
2017-08-03 13:59:12 -07:00
|
|
|
onCloseComplete();
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 13:59:12 -07:00
|
|
|
private void onCloseComplete() {
|
|
|
|
|
mIsOpen = false;
|
|
|
|
|
mLauncher.getDragLayer().removeView(mGradientBackground);
|
|
|
|
|
mLauncher.getDragLayer().removeView(WidgetsBottomSheet.this);
|
|
|
|
|
mLauncher.getSystemUiController().updateUiState(
|
|
|
|
|
SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
@Override
|
|
|
|
|
protected boolean isOfType(@FloatingViewType int type) {
|
2017-04-12 18:31:09 -07:00
|
|
|
return (type & TYPE_WIDGETS_BOTTOM_SHEET) != 0;
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setInsets(Rect insets) {
|
|
|
|
|
// Extend behind left, right, and bottom insets.
|
|
|
|
|
int leftInset = insets.left - mInsets.left;
|
|
|
|
|
int rightInset = insets.right - mInsets.right;
|
|
|
|
|
int bottomInset = insets.bottom - mInsets.bottom;
|
|
|
|
|
mInsets.set(insets);
|
|
|
|
|
setPadding(getPaddingLeft() + leftInset, getPaddingTop(),
|
|
|
|
|
getPaddingRight() + rightInset, getPaddingBottom() + bottomInset);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 12:35:55 -07:00
|
|
|
/* SwipeDetector.Listener */
|
2017-03-20 17:12:24 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDragStart(boolean start) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onDrag(float displacement, float velocity) {
|
|
|
|
|
setTranslationY(Utilities.boundToRange(displacement, mTranslationYOpen,
|
|
|
|
|
mTranslationYClosed));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 16:48:55 -07:00
|
|
|
@Override
|
|
|
|
|
public void setTranslationY(float translationY) {
|
|
|
|
|
super.setTranslationY(translationY);
|
|
|
|
|
if (mGradientBackground == null) return;
|
2017-07-24 14:41:23 -07:00
|
|
|
float p = (mTranslationYClosed - translationY) / mTranslationYRange;
|
|
|
|
|
boolean showScrim = p <= 0;
|
|
|
|
|
mGradientBackground.setProgress(p, showScrim);
|
2017-06-28 16:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
@Override
|
|
|
|
|
public void onDragEnd(float velocity, boolean fling) {
|
|
|
|
|
if ((fling && velocity > 0) || getTranslationY() > (mTranslationYRange) / 2) {
|
|
|
|
|
mScrollInterpolator.setVelocityAtZero(velocity);
|
2017-07-14 00:02:27 -07:00
|
|
|
mOpenCloseAnimator.setDuration(SwipeDetector.calculateDuration(velocity,
|
2017-03-20 17:12:24 -07:00
|
|
|
(mTranslationYClosed - getTranslationY()) / mTranslationYRange));
|
|
|
|
|
close(true);
|
|
|
|
|
} else {
|
|
|
|
|
mIsOpen = false;
|
2017-07-14 00:02:27 -07:00
|
|
|
mOpenCloseAnimator.setDuration(SwipeDetector.calculateDuration(velocity,
|
2017-03-20 17:12:24 -07:00
|
|
|
(getTranslationY() - mTranslationYOpen) / mTranslationYRange));
|
|
|
|
|
open(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-28 13:43:24 -07:00
|
|
|
@Override
|
|
|
|
|
public void logActionCommand(int command) {
|
|
|
|
|
// TODO: be more specific
|
|
|
|
|
mLauncher.getUserEventDispatcher().logActionCommand(command, ContainerType.WIDGETS);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 17:12:24 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean onControllerTouchEvent(MotionEvent ev) {
|
2017-07-06 12:35:55 -07:00
|
|
|
return mSwipeDetector.onTouchEvent(ev);
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
|
2017-09-28 13:43:24 -07:00
|
|
|
if (ev.getAction() == MotionEvent.ACTION_UP) {
|
|
|
|
|
// If we got ACTION_UP without ever returning true on intercept,
|
|
|
|
|
// the user never started dragging the bottom sheet.
|
|
|
|
|
if (!mLauncher.getDragLayer().isEventOverView(this, ev)) {
|
|
|
|
|
close(true);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 12:35:55 -07:00
|
|
|
int directionsToDetectScroll = mSwipeDetector.isIdleState() ?
|
2017-07-14 00:02:27 -07:00
|
|
|
SwipeDetector.DIRECTION_NEGATIVE : 0;
|
2017-07-06 12:35:55 -07:00
|
|
|
mSwipeDetector.setDetectableScrollConditions(
|
2017-03-20 17:12:24 -07:00
|
|
|
directionsToDetectScroll, false);
|
2017-07-06 12:35:55 -07:00
|
|
|
mSwipeDetector.onTouchEvent(ev);
|
|
|
|
|
return mSwipeDetector.isDraggingOrSettling();
|
2017-03-20 17:12:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* DragListener */
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
|
|
|
|
|
// A widget or custom shortcut was dragged.
|
|
|
|
|
close(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDragEnd() {
|
|
|
|
|
}
|
|
|
|
|
}
|