blob: 6f404086b6be9b94c3bd5b773faae76075e45f88 [file] [log] [blame]
Winson Chung61fa4192011-06-12 15:15:29 -07001/*
2 * Copyright (C) 2010 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung61fa4192011-06-12 15:15:29 -070018
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070019import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalaeb16432017-10-16 11:46:41 -070020
Sunny Goyal3a644ed2015-05-21 10:28:02 -070021import android.animation.AnimatorSet;
22import android.animation.FloatArrayEvaluator;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070023import android.animation.ObjectAnimator;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung61fa4192011-06-12 15:15:29 -070026import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070027import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070028import android.content.res.Resources;
Sunny Goyalf37a2142016-03-24 17:28:25 -070029import android.content.res.TypedArray;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070030import android.graphics.ColorMatrix;
31import android.graphics.ColorMatrixColorFilter;
Winson Chung61967cb2012-02-28 18:11:33 -080032import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070033import android.graphics.drawable.Drawable;
Jon Mirandabfaa4a42017-08-21 15:31:51 -070034import android.text.TextUtils;
Winson Chung61fa4192011-06-12 15:15:29 -070035import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080036import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070037import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070039import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070040import android.view.animation.DecelerateInterpolator;
41import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070042import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070043
Vadim Tryshevfedca432015-08-19 17:55:02 -070044import com.android.launcher3.dragndrop.DragController;
45import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070046import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070047import com.android.launcher3.dragndrop.DragView;
Tony Wickham6c828672017-02-27 13:30:20 -080048import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070049import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070050
51/**
52 * Implements a DropTarget.
53 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070054public abstract class ButtonDropTarget extends TextView
55 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070056
Tony Wickhamb54c4a32015-09-11 08:40:20 -070057 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070058
Sunny Goyalf37a2142016-03-24 17:28:25 -070059 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070060 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070061
Winson Chunga62e9fd2011-07-11 15:20:48 -070062 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070063 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070064
65 /** Whether this drop target is active for the current drag */
66 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070067 /** Whether an accessible drag is in progress */
68 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070069 /** An item must be dragged at least this many pixels before this drop target is enabled. */
70 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070071
72 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080073 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070074
Jon Mirandabfaa4a42017-08-21 15:31:51 -070075 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070076 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070077 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070078
Sunny Goyal3a644ed2015-05-21 10:28:02 -070079 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070080 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070081
Winson Chung61fa4192011-06-12 15:15:29 -070082 public ButtonDropTarget(Context context, AttributeSet attrs) {
83 this(context, attrs, 0);
84 }
85
86 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
87 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070088 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070089
Tony Wickham855b1b52016-03-28 16:56:30 -070090 Resources resources = getResources();
91 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070092
93 TypedArray a = context.obtainStyledAttributes(attrs,
94 R.styleable.ButtonDropTarget, defStyle, 0);
95 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
96 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070097 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070098 }
99
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 @Override
101 protected void onFinishInflate() {
102 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700103 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700104 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700105 }
106
Sunny Goyalfa401a12015-04-10 13:45:42 -0700107 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700108 // We do not set the drawable in the xml as that inflates two drawables corresponding to
109 // drawableLeft and drawableStart.
Sunny Goyal5931ca02017-06-19 17:29:48 -0700110 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
111 mDrawable = getCompoundDrawablesRelative()[0];
Sunny Goyalfa401a12015-04-10 13:45:42 -0700112 }
113
Sunny Goyal47328fd2016-05-25 18:56:41 -0700114 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700115 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700116 }
117
Sunny Goyalfa401a12015-04-10 13:45:42 -0700118 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700119 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800120 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800121 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700122 if (d.stateAnnouncer != null) {
123 d.stateAnnouncer.cancel();
124 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700125 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700126 }
127
Sunny Goyalfa401a12015-04-10 13:45:42 -0700128 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700129 public void onDragOver(DragObject d) {
130 // Do nothing
131 }
132
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700133 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800134 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700135 }
136
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700137 private void animateTextColor(int targetColor) {
138 if (mCurrentColorAnim != null) {
139 mCurrentColorAnim.cancel();
140 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700141
142 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700143 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700144
145 if (mSrcFilter == null) {
146 mSrcFilter = new ColorMatrix();
147 mDstFilter = new ColorMatrix();
148 mCurrentFilter = new ColorMatrix();
149 }
150
Tony Wickham6c828672017-02-27 13:30:20 -0800151 Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
152 Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700153 ValueAnimator anim1 = ValueAnimator.ofObject(
154 new FloatArrayEvaluator(mCurrentFilter.getArray()),
155 mSrcFilter.getArray(), mDstFilter.getArray());
156 anim1.addUpdateListener(new AnimatorUpdateListener() {
157
158 @Override
159 public void onAnimationUpdate(ValueAnimator animation) {
160 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
161 invalidate();
162 }
163 });
164
165 mCurrentColorAnim.play(anim1);
166 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700167 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700168 }
169
Sunny Goyalfa401a12015-04-10 13:45:42 -0700170 @Override
171 public final void onDragExit(DragObject d) {
172 if (!d.dragComplete) {
173 d.dragView.setColor(0);
174 resetHoverColor();
175 } else {
176 // Restore the hover color
177 d.dragView.setColor(mHoverColor);
178 }
Winson Chung61fa4192011-06-12 15:15:29 -0700179 }
180
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700181 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700182 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700183 mActive = supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700184 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700185 if (mCurrentColorAnim != null) {
186 mCurrentColorAnim.cancel();
187 mCurrentColorAnim = null;
188 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700190 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
191 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700192
193 mAccessibleDrag = options.isAccessibleDrag;
194 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700195 }
196
197 @Override
198 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700199 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700200 }
201
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700202 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700203
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700204 public boolean supportsAccessibilityDrop(ItemInfo info) {
205 return supportsDrop(info);
206 }
207
Sunny Goyalfa401a12015-04-10 13:45:42 -0700208 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700209 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700210 return mActive && (mAccessibleDrag ||
211 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700212 }
213
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700215 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700217 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700218 }
219
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220 /**
221 * On drop animate the dropView to the icon.
222 */
223 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700224 public void onDrop(final DragObject d, final DragOptions options) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700225 final DragLayer dragLayer = mLauncher.getDragLayer();
226 final Rect from = new Rect();
227 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
228
Sunny Goyal0f76b562016-12-13 19:37:10 -0800229 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700230 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700231 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232
233 Runnable onAnimationEndRunnable = new Runnable() {
234 @Override
235 public void run() {
236 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700237 mDropTargetBar.onDragEnd();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700238 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700239 }
240 };
241 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800242 DRAG_VIEW_DROP_DURATION,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700243 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700244 new LinearInterpolator(), onAnimationEndRunnable,
245 DragLayer.ANIMATION_END_DISAPPEAR, null);
246 }
247
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700248 public abstract int getAccessibilityAction();
249
Sunny Goyale9b651e2015-04-24 11:44:51 -0700250 @Override
251 public void prepareAccessibilityDrop() { }
252
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700253 public abstract void onAccessibilityDrop(View view, ItemInfo item);
254
Sunny Goyal0f76b562016-12-13 19:37:10 -0800255 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700256
Winson Chung61fa4192011-06-12 15:15:29 -0700257 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700258 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700259 super.getHitRect(outRect);
260 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700261
262 int[] coords = new int[2];
263 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
264 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700265 }
266
Sunny Goyal0f76b562016-12-13 19:37:10 -0800267 public Rect getIconRect(DragObject dragObject) {
268 int viewWidth = dragObject.dragView.getMeasuredWidth();
269 int viewHeight = dragObject.dragView.getMeasuredHeight();
270 int drawableWidth = mDrawable.getIntrinsicWidth();
271 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800272 DragLayer dragLayer = mLauncher.getDragLayer();
273
274 // Find the rect to animate to (the view is center aligned)
275 Rect to = new Rect();
276 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800277
278 final int width = drawableWidth;
279 final int height = drawableHeight;
280
281 final int left;
282 final int right;
283
Sunny Goyal70660032015-05-14 00:07:08 -0700284 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800285 right = to.right - getPaddingRight();
286 left = right - width;
287 } else {
288 left = to.left + getPaddingLeft();
289 right = left + width;
290 }
291
292 final int top = to.top + (getMeasuredHeight() - height) / 2;
293 final int bottom = top + height;
294
295 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800296
297 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800298 final int xOffset = (int) -(viewWidth - width) / 2;
299 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800300 to.offset(xOffset, yOffset);
301
302 return to;
303 }
304
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700305 @Override
306 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700307 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700308 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700309
310 public int getTextColor() {
311 return getTextColors().getDefaultColor();
312 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700313
314 /**
315 * Returns True if any update was made.
316 */
317 public boolean updateText(boolean hide) {
318 if ((hide && getText().toString().isEmpty()) || (!hide && mText.equals(getText()))) {
319 return false;
320 }
321
322 setText(hide ? "" : mText);
323 return true;
324 }
325
326 public boolean isTextTruncated() {
327 int availableWidth = getMeasuredWidth();
328 if (mHideParentOnDisable) {
329 ViewGroup parent = (ViewGroup) getParent();
330 availableWidth = parent.getMeasuredWidth() - parent.getPaddingLeft()
331 - parent.getPaddingRight();
332 }
333 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
334 + getCompoundDrawablePadding());
335 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
336 TextUtils.TruncateAt.END);
337 return !mText.equals(displayedText);
338 }
Winson Chung61fa4192011-06-12 15:15:29 -0700339}