2011-06-12 15:15:29 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
2015-05-21 10:28:02 -07:00
|
|
|
import android.animation.AnimatorSet;
|
|
|
|
|
import android.animation.FloatArrayEvaluator;
|
2015-04-28 22:01:31 -07:00
|
|
|
import android.animation.ObjectAnimator;
|
2015-05-21 10:28:02 -07:00
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
2011-06-12 15:15:29 -07:00
|
|
|
import android.content.Context;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.content.res.ColorStateList;
|
2016-03-28 16:56:30 -07:00
|
|
|
import android.content.res.Resources;
|
2016-03-24 17:28:25 -07:00
|
|
|
import android.content.res.TypedArray;
|
2015-05-21 10:28:02 -07:00
|
|
|
import android.graphics.ColorMatrix;
|
|
|
|
|
import android.graphics.ColorMatrixColorFilter;
|
2012-02-28 18:11:33 -08:00
|
|
|
import android.graphics.Rect;
|
2015-05-21 10:28:02 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-08-21 15:31:51 -07:00
|
|
|
import android.text.TextUtils;
|
2011-06-12 15:15:29 -07:00
|
|
|
import android.util.AttributeSet;
|
2013-02-06 15:40:46 -08:00
|
|
|
import android.view.View;
|
2015-04-22 11:29:51 -07:00
|
|
|
import android.view.View.OnClickListener;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.view.ViewGroup;
|
2015-09-24 11:23:31 -07:00
|
|
|
import android.view.accessibility.AccessibilityEvent;
|
2015-04-10 13:45:42 -07:00
|
|
|
import android.view.animation.DecelerateInterpolator;
|
|
|
|
|
import android.view.animation.LinearInterpolator;
|
2011-07-19 21:47:37 -07:00
|
|
|
import android.widget.TextView;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
2015-08-19 17:55:02 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragController;
|
|
|
|
|
import com.android.launcher3.dragndrop.DragLayer;
|
2016-08-16 15:36:48 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragOptions;
|
2015-08-19 17:55:02 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragView;
|
2017-02-27 13:30:20 -08:00
|
|
|
import com.android.launcher3.util.Themes;
|
2015-04-10 13:45:42 -07:00
|
|
|
import com.android.launcher3.util.Thunk;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a DropTarget.
|
|
|
|
|
*/
|
2015-04-22 11:29:51 -07:00
|
|
|
public abstract class ButtonDropTarget extends TextView
|
|
|
|
|
implements DropTarget, DragController.DragListener, OnClickListener {
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2015-09-11 08:40:20 -07:00
|
|
|
private static final int DRAG_VIEW_DROP_DURATION = 285;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
2016-03-24 17:28:25 -07:00
|
|
|
private final boolean mHideParentOnDisable;
|
2016-05-25 18:56:41 -07:00
|
|
|
protected final Launcher mLauncher;
|
2016-03-24 17:28:25 -07:00
|
|
|
|
2011-07-11 15:20:48 -07:00
|
|
|
private int mBottomDragPadding;
|
2016-05-25 18:56:41 -07:00
|
|
|
protected DropTargetBar mDropTargetBar;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
/** Whether this drop target is active for the current drag */
|
|
|
|
|
protected boolean mActive;
|
2016-08-17 11:11:48 -07:00
|
|
|
/** Whether an accessible drag is in progress */
|
|
|
|
|
private boolean mAccessibleDrag;
|
2016-03-28 16:56:30 -07:00
|
|
|
/** An item must be dragged at least this many pixels before this drop target is enabled. */
|
|
|
|
|
private final int mDragDistanceThreshold;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
/** The paint applied to the drag view on hover */
|
2012-02-28 18:11:33 -08:00
|
|
|
protected int mHoverColor = 0;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
2017-08-21 15:31:51 -07:00
|
|
|
protected CharSequence mText;
|
2015-04-10 13:45:42 -07:00
|
|
|
protected ColorStateList mOriginalTextColor;
|
2015-05-21 10:28:02 -07:00
|
|
|
protected Drawable mDrawable;
|
|
|
|
|
|
|
|
|
|
private AnimatorSet mCurrentColorAnim;
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2011-06-12 15:15:29 -07:00
|
|
|
public ButtonDropTarget(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
2016-10-07 12:50:01 -07:00
|
|
|
mLauncher = Launcher.getLauncher(context);
|
2016-05-25 18:56:41 -07:00
|
|
|
|
2016-03-28 16:56:30 -07:00
|
|
|
Resources resources = getResources();
|
|
|
|
|
mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
|
2016-03-24 17:28:25 -07:00
|
|
|
|
|
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs,
|
|
|
|
|
R.styleable.ButtonDropTarget, defStyle, 0);
|
|
|
|
|
mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
|
|
|
|
|
a.recycle();
|
2016-03-28 16:56:30 -07:00
|
|
|
mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
2017-08-21 15:31:51 -07:00
|
|
|
mText = getText();
|
2015-04-10 13:45:42 -07:00
|
|
|
mOriginalTextColor = getTextColors();
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
protected void setDrawable(int resId) {
|
2015-05-20 16:40:30 -07:00
|
|
|
// We do not set the drawable in the xml as that inflates two drawables corresponding to
|
|
|
|
|
// drawableLeft and drawableStart.
|
2017-06-19 17:29:48 -07:00
|
|
|
setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
|
|
|
|
|
mDrawable = getCompoundDrawablesRelative()[0];
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-25 18:56:41 -07:00
|
|
|
public void setDropTargetBar(DropTargetBar dropTargetBar) {
|
2015-09-11 08:40:20 -07:00
|
|
|
mDropTargetBar = dropTargetBar;
|
2011-07-19 21:47:37 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
|
|
|
|
public final void onDragEnter(DragObject d) {
|
|
|
|
|
d.dragView.setColor(mHoverColor);
|
2016-12-16 15:04:51 -08:00
|
|
|
animateTextColor(mHoverColor);
|
2015-09-25 12:17:08 -07:00
|
|
|
if (d.stateAnnouncer != null) {
|
|
|
|
|
d.stateAnnouncer.cancel();
|
|
|
|
|
}
|
2015-09-24 11:23:31 -07:00
|
|
|
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
|
|
|
|
public void onDragOver(DragObject d) {
|
2012-03-01 16:09:54 -08:00
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-17 12:24:25 -07:00
|
|
|
protected void resetHoverColor() {
|
2016-12-16 15:04:51 -08:00
|
|
|
animateTextColor(mOriginalTextColor.getDefaultColor());
|
2015-04-28 22:01:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void animateTextColor(int targetColor) {
|
|
|
|
|
if (mCurrentColorAnim != null) {
|
|
|
|
|
mCurrentColorAnim.cancel();
|
|
|
|
|
}
|
2015-05-21 10:28:02 -07:00
|
|
|
|
|
|
|
|
mCurrentColorAnim = new AnimatorSet();
|
2015-04-28 22:01:31 -07:00
|
|
|
mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
|
2015-05-21 10:28:02 -07:00
|
|
|
|
|
|
|
|
if (mSrcFilter == null) {
|
|
|
|
|
mSrcFilter = new ColorMatrix();
|
|
|
|
|
mDstFilter = new ColorMatrix();
|
|
|
|
|
mCurrentFilter = new ColorMatrix();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 13:30:20 -08:00
|
|
|
Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
|
|
|
|
|
Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
|
2015-05-21 10:28:02 -07:00
|
|
|
ValueAnimator anim1 = ValueAnimator.ofObject(
|
|
|
|
|
new FloatArrayEvaluator(mCurrentFilter.getArray()),
|
|
|
|
|
mSrcFilter.getArray(), mDstFilter.getArray());
|
|
|
|
|
anim1.addUpdateListener(new AnimatorUpdateListener() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mCurrentColorAnim.play(anim1);
|
|
|
|
|
mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
|
2015-04-28 22:01:31 -07:00
|
|
|
mCurrentColorAnim.start();
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
|
|
|
|
public final void onDragExit(DragObject d) {
|
|
|
|
|
if (!d.dragComplete) {
|
|
|
|
|
d.dragView.setColor(0);
|
|
|
|
|
resetHoverColor();
|
|
|
|
|
} else {
|
|
|
|
|
// Restore the hover color
|
|
|
|
|
d.dragView.setColor(mHoverColor);
|
|
|
|
|
}
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-12 20:04:41 -07:00
|
|
|
@Override
|
2016-08-16 15:36:48 -07:00
|
|
|
public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
|
2017-10-03 16:17:32 -07:00
|
|
|
mActive = supportsDrop(dragObject.dragInfo);
|
2015-05-21 10:28:02 -07:00
|
|
|
mDrawable.setColorFilter(null);
|
2015-04-28 22:01:31 -07:00
|
|
|
if (mCurrentColorAnim != null) {
|
|
|
|
|
mCurrentColorAnim.cancel();
|
|
|
|
|
mCurrentColorAnim = null;
|
|
|
|
|
}
|
2015-04-10 13:45:42 -07:00
|
|
|
setTextColor(mOriginalTextColor);
|
2016-03-24 17:28:25 -07:00
|
|
|
(mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
|
|
|
|
|
.setVisibility(mActive ? View.VISIBLE : View.GONE);
|
2016-08-16 15:36:48 -07:00
|
|
|
|
|
|
|
|
mAccessibleDrag = options.isAccessibleDrag;
|
|
|
|
|
setOnClickListener(mAccessibleDrag ? this : null);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
|
|
|
|
public final boolean acceptDrop(DragObject dragObject) {
|
2017-10-03 16:17:32 -07:00
|
|
|
return supportsDrop(dragObject.dragInfo);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 16:17:32 -07:00
|
|
|
protected abstract boolean supportsDrop(ItemInfo info);
|
2015-04-10 13:45:42 -07:00
|
|
|
|
|
|
|
|
@Override
|
2011-06-12 15:15:29 -07:00
|
|
|
public boolean isDropEnabled() {
|
2016-08-17 11:11:48 -07:00
|
|
|
return mActive && (mAccessibleDrag ||
|
|
|
|
|
mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
@Override
|
2011-06-12 15:15:29 -07:00
|
|
|
public void onDragEnd() {
|
2015-04-10 13:45:42 -07:00
|
|
|
mActive = false;
|
2016-08-16 15:36:48 -07:00
|
|
|
setOnClickListener(null);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 13:45:42 -07:00
|
|
|
/**
|
|
|
|
|
* On drop animate the dropView to the icon.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2017-10-05 11:40:05 -07:00
|
|
|
public void onDrop(final DragObject d, final DragOptions options) {
|
2015-04-10 13:45:42 -07:00
|
|
|
final DragLayer dragLayer = mLauncher.getDragLayer();
|
|
|
|
|
final Rect from = new Rect();
|
|
|
|
|
dragLayer.getViewRectRelativeToSelf(d.dragView, from);
|
|
|
|
|
|
2016-12-13 19:37:10 -08:00
|
|
|
final Rect to = getIconRect(d);
|
2015-04-10 13:45:42 -07:00
|
|
|
final float scale = (float) to.width() / from.width();
|
2015-09-11 08:40:20 -07:00
|
|
|
mDropTargetBar.deferOnDragEnd();
|
2015-04-10 13:45:42 -07:00
|
|
|
|
|
|
|
|
Runnable onAnimationEndRunnable = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
completeDrop(d);
|
2015-09-11 08:40:20 -07:00
|
|
|
mDropTargetBar.onDragEnd();
|
2017-10-06 13:29:57 -07:00
|
|
|
mLauncher.exitSpringLoadedDragMode(true, 0);
|
2015-04-10 13:45:42 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
|
2017-01-25 18:23:36 -08:00
|
|
|
DRAG_VIEW_DROP_DURATION,
|
2016-09-12 14:36:30 -07:00
|
|
|
new DecelerateInterpolator(2),
|
2015-04-10 13:45:42 -07:00
|
|
|
new LinearInterpolator(), onAnimationEndRunnable,
|
|
|
|
|
DragLayer.ANIMATION_END_DISAPPEAR, null);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 11:44:51 -07:00
|
|
|
@Override
|
|
|
|
|
public void prepareAccessibilityDrop() { }
|
|
|
|
|
|
2016-12-13 19:37:10 -08:00
|
|
|
public abstract void completeDrop(DragObject d);
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2011-07-11 15:20:48 -07:00
|
|
|
@Override
|
2013-07-01 17:03:59 -07:00
|
|
|
public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
|
2011-07-11 15:20:48 -07:00
|
|
|
super.getHitRect(outRect);
|
|
|
|
|
outRect.bottom += mBottomDragPadding;
|
2013-07-12 14:14:16 -07:00
|
|
|
|
|
|
|
|
int[] coords = new int[2];
|
|
|
|
|
mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
|
|
|
|
|
outRect.offsetTo(coords[0], coords[1]);
|
2011-07-11 15:20:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-13 19:37:10 -08:00
|
|
|
public Rect getIconRect(DragObject dragObject) {
|
|
|
|
|
int viewWidth = dragObject.dragView.getMeasuredWidth();
|
|
|
|
|
int viewHeight = dragObject.dragView.getMeasuredHeight();
|
|
|
|
|
int drawableWidth = mDrawable.getIntrinsicWidth();
|
|
|
|
|
int drawableHeight = mDrawable.getIntrinsicHeight();
|
2012-02-28 18:11:33 -08:00
|
|
|
DragLayer dragLayer = mLauncher.getDragLayer();
|
|
|
|
|
|
|
|
|
|
// Find the rect to animate to (the view is center aligned)
|
|
|
|
|
Rect to = new Rect();
|
|
|
|
|
dragLayer.getViewRectRelativeToSelf(this, to);
|
2013-02-06 15:40:46 -08:00
|
|
|
|
|
|
|
|
final int width = drawableWidth;
|
|
|
|
|
final int height = drawableHeight;
|
|
|
|
|
|
|
|
|
|
final int left;
|
|
|
|
|
final int right;
|
|
|
|
|
|
2015-05-14 00:07:08 -07:00
|
|
|
if (Utilities.isRtl(getResources())) {
|
2013-02-06 15:40:46 -08:00
|
|
|
right = to.right - getPaddingRight();
|
|
|
|
|
left = right - width;
|
|
|
|
|
} else {
|
|
|
|
|
left = to.left + getPaddingLeft();
|
|
|
|
|
right = left + width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int top = to.top + (getMeasuredHeight() - height) / 2;
|
|
|
|
|
final int bottom = top + height;
|
|
|
|
|
|
|
|
|
|
to.set(left, top, right, bottom);
|
2012-02-28 18:11:33 -08:00
|
|
|
|
|
|
|
|
// Center the destination rect about the trash icon
|
2013-02-06 15:40:46 -08:00
|
|
|
final int xOffset = (int) -(viewWidth - width) / 2;
|
|
|
|
|
final int yOffset = (int) -(viewHeight - height) / 2;
|
2012-02-28 18:11:33 -08:00
|
|
|
to.offset(xOffset, yOffset);
|
|
|
|
|
|
|
|
|
|
return to;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-22 11:29:51 -07:00
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
2016-06-17 08:43:56 -07:00
|
|
|
mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
|
2015-04-22 11:29:51 -07:00
|
|
|
}
|
2015-04-28 22:01:31 -07:00
|
|
|
|
|
|
|
|
public int getTextColor() {
|
|
|
|
|
return getTextColors().getDefaultColor();
|
|
|
|
|
}
|
2017-08-21 15:31:51 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns True if any update was made.
|
|
|
|
|
*/
|
|
|
|
|
public boolean updateText(boolean hide) {
|
|
|
|
|
if ((hide && getText().toString().isEmpty()) || (!hide && mText.equals(getText()))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setText(hide ? "" : mText);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isTextTruncated() {
|
|
|
|
|
int availableWidth = getMeasuredWidth();
|
|
|
|
|
if (mHideParentOnDisable) {
|
|
|
|
|
ViewGroup parent = (ViewGroup) getParent();
|
|
|
|
|
availableWidth = parent.getMeasuredWidth() - parent.getPaddingLeft()
|
|
|
|
|
- parent.getPaddingRight();
|
|
|
|
|
}
|
|
|
|
|
availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
|
|
|
|
|
+ getCompoundDrawablePadding());
|
|
|
|
|
CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
|
|
|
|
|
TextUtils.TruncateAt.END);
|
|
|
|
|
return !mText.equals(displayedText);
|
|
|
|
|
}
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|