blob: d8c4efaa4eef108bce3b13a9f4e2c8ba7be0c107 [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;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070041
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070042import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070043import com.android.launcher3.dragndrop.DragController;
44import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070045import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070046import com.android.launcher3.dragndrop.DragView;
Tony Wickham6c828672017-02-27 13:30:20 -080047import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070048import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070049
50/**
51 * Implements a DropTarget.
52 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070053public abstract class ButtonDropTarget extends TextView
54 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070055
Tony Wickhamb54c4a32015-09-11 08:40:20 -070056 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070057
Sunny Goyalf37a2142016-03-24 17:28:25 -070058 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070059 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070060
Winson Chunga62e9fd2011-07-11 15:20:48 -070061 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070062 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070063
64 /** Whether this drop target is active for the current drag */
65 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070066 /** Whether an accessible drag is in progress */
67 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070068 /** An item must be dragged at least this many pixels before this drop target is enabled. */
69 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070070
71 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080072 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070073
Jon Mirandabfaa4a42017-08-21 15:31:51 -070074 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070075 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070076 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070077
Sunny Goyal3a644ed2015-05-21 10:28:02 -070078 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070079 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070080
Winson Chung61fa4192011-06-12 15:15:29 -070081 public ButtonDropTarget(Context context, AttributeSet attrs) {
82 this(context, attrs, 0);
83 }
84
85 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070087 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070088
Tony Wickham855b1b52016-03-28 16:56:30 -070089 Resources resources = getResources();
90 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070091
92 TypedArray a = context.obtainStyledAttributes(attrs,
93 R.styleable.ButtonDropTarget, defStyle, 0);
94 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
95 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070096 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070097 }
98
Sunny Goyalfa401a12015-04-10 13:45:42 -070099 @Override
100 protected void onFinishInflate() {
101 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700102 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700104 }
105
Sunny Goyalfa401a12015-04-10 13:45:42 -0700106 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700107 // We do not set the drawable in the xml as that inflates two drawables corresponding to
108 // drawableLeft and drawableStart.
Sunny Goyal5931ca02017-06-19 17:29:48 -0700109 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
110 mDrawable = getCompoundDrawablesRelative()[0];
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 }
112
Sunny Goyal47328fd2016-05-25 18:56:41 -0700113 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700114 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700115 }
116
Sunny Goyalfa401a12015-04-10 13:45:42 -0700117 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700118 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800119 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800120 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700121 if (d.stateAnnouncer != null) {
122 d.stateAnnouncer.cancel();
123 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700124 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700125 }
126
Sunny Goyalfa401a12015-04-10 13:45:42 -0700127 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700128 public void onDragOver(DragObject d) {
129 // Do nothing
130 }
131
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700132 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800133 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700134 }
135
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700136 private void animateTextColor(int targetColor) {
137 if (mCurrentColorAnim != null) {
138 mCurrentColorAnim.cancel();
139 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700140
141 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700142 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700143
144 if (mSrcFilter == null) {
145 mSrcFilter = new ColorMatrix();
146 mDstFilter = new ColorMatrix();
147 mCurrentFilter = new ColorMatrix();
148 }
149
Tony Wickham6c828672017-02-27 13:30:20 -0800150 Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
151 Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700152 ValueAnimator anim1 = ValueAnimator.ofObject(
153 new FloatArrayEvaluator(mCurrentFilter.getArray()),
154 mSrcFilter.getArray(), mDstFilter.getArray());
155 anim1.addUpdateListener(new AnimatorUpdateListener() {
156
157 @Override
158 public void onAnimationUpdate(ValueAnimator animation) {
159 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
160 invalidate();
161 }
162 });
163
164 mCurrentColorAnim.play(anim1);
165 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700166 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700167 }
168
Sunny Goyalfa401a12015-04-10 13:45:42 -0700169 @Override
170 public final void onDragExit(DragObject d) {
171 if (!d.dragComplete) {
172 d.dragView.setColor(0);
173 resetHoverColor();
174 } else {
175 // Restore the hover color
176 d.dragView.setColor(mHoverColor);
177 }
Winson Chung61fa4192011-06-12 15:15:29 -0700178 }
179
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700180 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700181 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700182 mActive = supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700183 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700184 if (mCurrentColorAnim != null) {
185 mCurrentColorAnim.cancel();
186 mCurrentColorAnim = null;
187 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700188 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700189 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
190 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700191
192 mAccessibleDrag = options.isAccessibleDrag;
193 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700194 }
195
196 @Override
197 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700198 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 }
200
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700201 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700202
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700203 public boolean supportsAccessibilityDrop(ItemInfo info) {
204 return supportsDrop(info);
205 }
206
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700208 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700209 return mActive && (mAccessibleDrag ||
210 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700211 }
212
Sunny Goyalfa401a12015-04-10 13:45:42 -0700213 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700214 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700215 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700216 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700217 }
218
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 /**
220 * On drop animate the dropView to the icon.
221 */
222 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700223 public void onDrop(final DragObject d, final DragOptions options) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224 final DragLayer dragLayer = mLauncher.getDragLayer();
225 final Rect from = new Rect();
226 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
227
Sunny Goyal0f76b562016-12-13 19:37:10 -0800228 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700230 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700231
232 Runnable onAnimationEndRunnable = new Runnable() {
233 @Override
234 public void run() {
235 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700236 mDropTargetBar.onDragEnd();
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700237 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700238 }
239 };
240 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800241 DRAG_VIEW_DROP_DURATION,
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700242 Interpolators.DEACCEL_2, Interpolators.LINEAR, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700243 DragLayer.ANIMATION_END_DISAPPEAR, null);
244 }
245
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700246 public abstract int getAccessibilityAction();
247
Sunny Goyale9b651e2015-04-24 11:44:51 -0700248 @Override
249 public void prepareAccessibilityDrop() { }
250
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700251 public abstract void onAccessibilityDrop(View view, ItemInfo item);
252
Sunny Goyal0f76b562016-12-13 19:37:10 -0800253 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700254
Winson Chung61fa4192011-06-12 15:15:29 -0700255 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700256 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700257 super.getHitRect(outRect);
258 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700259
260 int[] coords = new int[2];
261 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
262 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700263 }
264
Sunny Goyal0f76b562016-12-13 19:37:10 -0800265 public Rect getIconRect(DragObject dragObject) {
266 int viewWidth = dragObject.dragView.getMeasuredWidth();
267 int viewHeight = dragObject.dragView.getMeasuredHeight();
268 int drawableWidth = mDrawable.getIntrinsicWidth();
269 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800270 DragLayer dragLayer = mLauncher.getDragLayer();
271
272 // Find the rect to animate to (the view is center aligned)
273 Rect to = new Rect();
274 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275
276 final int width = drawableWidth;
277 final int height = drawableHeight;
278
279 final int left;
280 final int right;
281
Sunny Goyal70660032015-05-14 00:07:08 -0700282 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800283 right = to.right - getPaddingRight();
284 left = right - width;
285 } else {
286 left = to.left + getPaddingLeft();
287 right = left + width;
288 }
289
290 final int top = to.top + (getMeasuredHeight() - height) / 2;
291 final int bottom = top + height;
292
293 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800294
295 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800296 final int xOffset = (int) -(viewWidth - width) / 2;
297 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800298 to.offset(xOffset, yOffset);
299
300 return to;
301 }
302
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700303 @Override
304 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700305 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700306 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700307
308 public int getTextColor() {
309 return getTextColors().getDefaultColor();
310 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700311
312 /**
313 * Returns True if any update was made.
314 */
315 public boolean updateText(boolean hide) {
316 if ((hide && getText().toString().isEmpty()) || (!hide && mText.equals(getText()))) {
317 return false;
318 }
319
320 setText(hide ? "" : mText);
321 return true;
322 }
323
324 public boolean isTextTruncated() {
325 int availableWidth = getMeasuredWidth();
326 if (mHideParentOnDisable) {
327 ViewGroup parent = (ViewGroup) getParent();
328 availableWidth = parent.getMeasuredWidth() - parent.getPaddingLeft()
329 - parent.getPaddingRight();
330 }
331 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
332 + getCompoundDrawablePadding());
333 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
334 TextUtils.TruncateAt.END);
335 return !mText.equals(displayedText);
336 }
Winson Chung61fa4192011-06-12 15:15:29 -0700337}