blob: a9cf8cca7f7479d33fcfecf20eefe95bc9425a86 [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 Goyald1b3f5c2018-01-18 17:14:05 -080019import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
20
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070021import static com.android.launcher3.LauncherState.NORMAL;
Sunny Goyalaeb16432017-10-16 11:46:41 -070022
Sunny Goyal3a644ed2015-05-21 10:28:02 -070023import android.animation.AnimatorSet;
24import android.animation.FloatArrayEvaluator;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070025import android.animation.ObjectAnimator;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070026import android.animation.ValueAnimator;
Winson Chung61fa4192011-06-12 15:15:29 -070027import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070028import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070029import android.content.res.Resources;
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;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080036import android.view.LayoutInflater;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080037import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070038import android.view.View.OnClickListener;
Sunny Goyale78e3d72015-09-24 11:23:31 -070039import android.view.accessibility.AccessibilityEvent;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080040import android.widget.PopupWindow;
Adam Cohend4d7aa52011-07-19 21:47:37 -070041import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070042
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070043import com.android.launcher3.anim.Interpolators;
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
Sunny Goyalac00cba2017-11-13 15:58:01 -080057 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070058 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070059
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080060 public static final int TOOLTIP_DEFAULT = 0;
61 public static final int TOOLTIP_LEFT = 1;
62 public static final int TOOLTIP_RIGHT = 2;
63
Sunny Goyal47328fd2016-05-25 18:56:41 -070064 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070065
Winson Chunga62e9fd2011-07-11 15:20:48 -070066 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070067 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070068
69 /** Whether this drop target is active for the current drag */
70 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070071 /** Whether an accessible drag is in progress */
72 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070073 /** An item must be dragged at least this many pixels before this drop target is enabled. */
74 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070075
76 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080077 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070078
Jon Mirandabfaa4a42017-08-21 15:31:51 -070079 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070080 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070081 protected Drawable mDrawable;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080082 private boolean mTextVisible = true;
83
84 private PopupWindow mToolTip;
85 private int mToolTipLocation;
Sunny Goyalfa401a12015-04-10 13:45:42 -070086
Sunny Goyal3a644ed2015-05-21 10:28:02 -070087 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070088 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070089
Winson Chung61fa4192011-06-12 15:15:29 -070090 public ButtonDropTarget(Context context, AttributeSet attrs) {
91 this(context, attrs, 0);
92 }
93
94 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
95 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070096 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070097
Tony Wickham855b1b52016-03-28 16:56:30 -070098 Resources resources = getResources();
99 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Tony Wickham855b1b52016-03-28 16:56:30 -0700100 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700101 }
102
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 @Override
104 protected void onFinishInflate() {
105 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700106 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700107 mOriginalTextColor = getTextColors();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800108 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -0700109 }
110
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700112 // We do not set the drawable in the xml as that inflates two drawables corresponding to
113 // drawableLeft and drawableStart.
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800114 if (mTextVisible) {
115 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
116 mDrawable = getCompoundDrawablesRelative()[0];
117 } else {
118 setCompoundDrawablesRelativeWithIntrinsicBounds(0, resId, 0, 0);
119 mDrawable = getCompoundDrawablesRelative()[1];
120 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700121 }
122
Sunny Goyal47328fd2016-05-25 18:56:41 -0700123 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700124 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700125 }
126
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800127 private void hideTooltip() {
128 if (mToolTip != null) {
129 mToolTip.dismiss();
130 mToolTip = null;
131 }
132 }
133
Sunny Goyalfa401a12015-04-10 13:45:42 -0700134 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700135 public final void onDragEnter(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800136 if (!d.accessibleDrag && !mTextVisible) {
137 // Show tooltip
138 hideTooltip();
139
140 TextView message = (TextView) LayoutInflater.from(getContext()).inflate(
141 R.layout.drop_target_tool_tip, null);
142 message.setText(mText);
143
144 mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT);
145 int x = 0, y = 0;
146 if (mToolTipLocation != TOOLTIP_DEFAULT) {
147 y = -getMeasuredHeight();
148 message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
149 if (mToolTipLocation == TOOLTIP_LEFT) {
150 x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
151 } else {
152 x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
153 }
154 }
155 mToolTip.showAsDropDown(this, x, y);
156 }
157
Winson Chung61967cb2012-02-28 18:11:33 -0800158 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800159 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700160 if (d.stateAnnouncer != null) {
161 d.stateAnnouncer.cancel();
162 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700163 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700164 }
165
Sunny Goyalfa401a12015-04-10 13:45:42 -0700166 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700167 public void onDragOver(DragObject d) {
168 // Do nothing
169 }
170
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700171 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800172 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700173 }
174
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700175 private void animateTextColor(int targetColor) {
176 if (mCurrentColorAnim != null) {
177 mCurrentColorAnim.cancel();
178 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700179
180 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700181 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700182
183 if (mSrcFilter == null) {
184 mSrcFilter = new ColorMatrix();
185 mDstFilter = new ColorMatrix();
186 mCurrentFilter = new ColorMatrix();
187 }
188
Tony Wickham6c828672017-02-27 13:30:20 -0800189 Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
190 Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700191 ValueAnimator anim1 = ValueAnimator.ofObject(
192 new FloatArrayEvaluator(mCurrentFilter.getArray()),
193 mSrcFilter.getArray(), mDstFilter.getArray());
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800194 anim1.addUpdateListener((anim) -> {
195 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
196 invalidate();
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700197 });
198
199 mCurrentColorAnim.play(anim1);
200 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700201 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700202 }
203
Sunny Goyalfa401a12015-04-10 13:45:42 -0700204 @Override
205 public final void onDragExit(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800206 hideTooltip();
207
Sunny Goyalfa401a12015-04-10 13:45:42 -0700208 if (!d.dragComplete) {
209 d.dragView.setColor(0);
210 resetHoverColor();
211 } else {
212 // Restore the hover color
213 d.dragView.setColor(mHoverColor);
214 }
Winson Chung61fa4192011-06-12 15:15:29 -0700215 }
216
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700217 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700218 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700219 mActive = supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700220 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700221 if (mCurrentColorAnim != null) {
222 mCurrentColorAnim.cancel();
223 mCurrentColorAnim = null;
224 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700225 setTextColor(mOriginalTextColor);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800226 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700227
228 mAccessibleDrag = options.isAccessibleDrag;
229 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700230 }
231
232 @Override
233 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700234 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700235 }
236
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700237 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700238
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700239 public boolean supportsAccessibilityDrop(ItemInfo info) {
240 return supportsDrop(info);
241 }
242
Sunny Goyalfa401a12015-04-10 13:45:42 -0700243 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700244 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700245 return mActive && (mAccessibleDrag ||
246 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700247 }
248
Sunny Goyalfa401a12015-04-10 13:45:42 -0700249 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700250 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700251 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700252 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700253 }
254
Sunny Goyalfa401a12015-04-10 13:45:42 -0700255 /**
256 * On drop animate the dropView to the icon.
257 */
258 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700259 public void onDrop(final DragObject d, final DragOptions options) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700260 final DragLayer dragLayer = mLauncher.getDragLayer();
261 final Rect from = new Rect();
262 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
263
Sunny Goyal0f76b562016-12-13 19:37:10 -0800264 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700265 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700266 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700267
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800268 Runnable onAnimationEndRunnable = () -> {
269 completeDrop(d);
270 mDropTargetBar.onDragEnd();
271 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700272 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800273
Sunny Goyalfa401a12015-04-10 13:45:42 -0700274 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800275 DRAG_VIEW_DROP_DURATION,
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700276 Interpolators.DEACCEL_2, Interpolators.LINEAR, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700277 DragLayer.ANIMATION_END_DISAPPEAR, null);
278 }
279
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700280 public abstract int getAccessibilityAction();
281
Sunny Goyale9b651e2015-04-24 11:44:51 -0700282 @Override
283 public void prepareAccessibilityDrop() { }
284
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700285 public abstract void onAccessibilityDrop(View view, ItemInfo item);
286
Sunny Goyal0f76b562016-12-13 19:37:10 -0800287 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700288
Winson Chung61fa4192011-06-12 15:15:29 -0700289 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700290 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700291 super.getHitRect(outRect);
292 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700293
Sunny Goyalac00cba2017-11-13 15:58:01 -0800294 sTempCords[0] = sTempCords[1] = 0;
295 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
296 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700297 }
298
Sunny Goyal0f76b562016-12-13 19:37:10 -0800299 public Rect getIconRect(DragObject dragObject) {
300 int viewWidth = dragObject.dragView.getMeasuredWidth();
301 int viewHeight = dragObject.dragView.getMeasuredHeight();
302 int drawableWidth = mDrawable.getIntrinsicWidth();
303 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800304 DragLayer dragLayer = mLauncher.getDragLayer();
305
306 // Find the rect to animate to (the view is center aligned)
307 Rect to = new Rect();
308 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800309
310 final int width = drawableWidth;
311 final int height = drawableHeight;
312
313 final int left;
314 final int right;
315
Sunny Goyal70660032015-05-14 00:07:08 -0700316 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800317 right = to.right - getPaddingRight();
318 left = right - width;
319 } else {
320 left = to.left + getPaddingLeft();
321 right = left + width;
322 }
323
324 final int top = to.top + (getMeasuredHeight() - height) / 2;
325 final int bottom = top + height;
326
327 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800328
329 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800330 final int xOffset = -(viewWidth - width) / 2;
331 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800332 to.offset(xOffset, yOffset);
333
334 return to;
335 }
336
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700337 @Override
338 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700339 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700340 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700341
342 public int getTextColor() {
343 return getTextColors().getDefaultColor();
344 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700345
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800346 public void setTextVisible(boolean isVisible) {
347 if (mTextVisible != isVisible) {
348 mTextVisible = isVisible;
349 setText(isVisible ? mText : "");
350 if (mTextVisible) {
351 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
352 } else {
353 setCompoundDrawablesRelativeWithIntrinsicBounds(null, mDrawable, null, null);
354 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700355 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700356 }
357
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800358 public void setToolTipLocation(int location) {
359 mToolTipLocation = location;
360 hideTooltip();
361 }
362
363 public boolean isTextTruncated(int availableWidth) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700364 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
365 + getCompoundDrawablePadding());
366 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
367 TextUtils.TruncateAt.END);
368 return !mText.equals(displayedText);
369 }
Winson Chung61fa4192011-06-12 15:15:29 -0700370}