blob: e9f50c7e9e18b7fb63f04977da8a56ba2caa6fbb [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;
Winson Chung61fa4192011-06-12 15:15:29 -070024import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070025import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070026import android.content.res.Resources;
Winson Chung61967cb2012-02-28 18:11:33 -080027import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070028import android.graphics.drawable.Drawable;
Jon Mirandabfaa4a42017-08-21 15:31:51 -070029import android.text.TextUtils;
Winson Chung61fa4192011-06-12 15:15:29 -070030import android.util.AttributeSet;
Sunny Goyal55bdeed2018-09-18 16:17:22 -070031import android.util.Property;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080032import android.view.LayoutInflater;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080033import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070034import android.view.View.OnClickListener;
Sunny Goyale78e3d72015-09-24 11:23:31 -070035import android.view.accessibility.AccessibilityEvent;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080036import android.widget.PopupWindow;
Adam Cohend4d7aa52011-07-19 21:47:37 -070037import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070038
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070039import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070040import com.android.launcher3.dragndrop.DragController;
41import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070042import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyale396abf2020-04-06 15:11:17 -070043import com.android.launcher3.model.data.ItemInfo;
Winson Chung61fa4192011-06-12 15:15:29 -070044
45/**
46 * Implements a DropTarget.
47 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070048public abstract class ButtonDropTarget extends TextView
49 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070050
Sunny Goyal55bdeed2018-09-18 16:17:22 -070051 private static final Property<ButtonDropTarget, Integer> TEXT_COLOR =
52 new Property<ButtonDropTarget, Integer>(Integer.TYPE, "textColor") {
53
54 @Override
55 public Integer get(ButtonDropTarget target) {
56 return target.getTextColor();
57 }
58
59 @Override
60 public void set(ButtonDropTarget target, Integer value) {
61 target.setTextColor(value);
62 }
63 };
64
Sunny Goyalac00cba2017-11-13 15:58:01 -080065 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070066 private static final int DRAG_VIEW_DROP_DURATION = 285;
Steven Ng6a2acfa2021-03-19 13:23:53 +000067 private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
Winson Chung61fa4192011-06-12 15:15:29 -070068
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080069 public static final int TOOLTIP_DEFAULT = 0;
70 public static final int TOOLTIP_LEFT = 1;
71 public static final int TOOLTIP_RIGHT = 2;
72
Sunny Goyal47328fd2016-05-25 18:56:41 -070073 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070074
Sunny Goyal47328fd2016-05-25 18:56:41 -070075 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070076
77 /** Whether this drop target is active for the current drag */
78 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070079 /** Whether an accessible drag is in progress */
80 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070081 /** An item must be dragged at least this many pixels before this drop target is enabled. */
82 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070083
Jon Mirandabfaa4a42017-08-21 15:31:51 -070084 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070085 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070086 protected Drawable mDrawable;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080087 private boolean mTextVisible = true;
88
89 private PopupWindow mToolTip;
90 private int mToolTipLocation;
Sunny Goyalfa401a12015-04-10 13:45:42 -070091
Sunny Goyal3a644ed2015-05-21 10:28:02 -070092 private AnimatorSet mCurrentColorAnim;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070093
Winson Chung61fa4192011-06-12 15:15:29 -070094 public ButtonDropTarget(Context context, AttributeSet attrs) {
95 this(context, attrs, 0);
96 }
97
98 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
99 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -0700100 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700101
Tony Wickham855b1b52016-03-28 16:56:30 -0700102 Resources resources = getResources();
Tony Wickham855b1b52016-03-28 16:56:30 -0700103 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700104 }
105
Sunny Goyalfa401a12015-04-10 13:45:42 -0700106 @Override
107 protected void onFinishInflate() {
108 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700109 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 mOriginalTextColor = getTextColors();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800111 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -0700112 }
113
Winson Chung1054d4e2018-03-05 19:39:21 +0000114 protected void updateText(int resId) {
115 setText(resId);
116 mText = getText();
117 setContentDescription(mText);
118 }
119
Sunny Goyalfa401a12015-04-10 13:45:42 -0700120 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700121 // We do not set the drawable in the xml as that inflates two drawables corresponding to
122 // drawableLeft and drawableStart.
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800123 if (mTextVisible) {
124 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
125 mDrawable = getCompoundDrawablesRelative()[0];
126 } else {
127 setCompoundDrawablesRelativeWithIntrinsicBounds(0, resId, 0, 0);
128 mDrawable = getCompoundDrawablesRelative()[1];
129 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700130 }
131
Sunny Goyal47328fd2016-05-25 18:56:41 -0700132 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700133 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700134 }
135
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800136 private void hideTooltip() {
137 if (mToolTip != null) {
138 mToolTip.dismiss();
139 mToolTip = null;
140 }
141 }
142
Sunny Goyalfa401a12015-04-10 13:45:42 -0700143 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700144 public final void onDragEnter(DragObject d) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700145 if (!mAccessibleDrag && !mTextVisible) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800146 // Show tooltip
147 hideTooltip();
148
149 TextView message = (TextView) LayoutInflater.from(getContext()).inflate(
150 R.layout.drop_target_tool_tip, null);
151 message.setText(mText);
152
153 mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT);
154 int x = 0, y = 0;
155 if (mToolTipLocation != TOOLTIP_DEFAULT) {
156 y = -getMeasuredHeight();
157 message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
158 if (mToolTipLocation == TOOLTIP_LEFT) {
159 x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
160 } else {
161 x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
162 }
163 }
164 mToolTip.showAsDropDown(this, x, y);
165 }
166
Steven Ng6a2acfa2021-03-19 13:23:53 +0000167 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100168 setSelected(true);
Sunny Goyald21301e2015-09-25 12:17:08 -0700169 if (d.stateAnnouncer != null) {
170 d.stateAnnouncer.cancel();
171 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700172 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700173 }
174
Sunny Goyalfa401a12015-04-10 13:45:42 -0700175 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700176 public void onDragOver(DragObject d) {
177 // Do nothing
178 }
179
Sunny Goyalfa401a12015-04-10 13:45:42 -0700180 @Override
181 public final void onDragExit(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800182 hideTooltip();
183
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 if (!d.dragComplete) {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000185 d.dragView.setAlpha(1f);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100186 setSelected(false);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700187 } else {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000188 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 }
Winson Chung61fa4192011-06-12 15:15:29 -0700190 }
191
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700192 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700193 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800194 mActive = !options.isKeyboardDrag && supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700195 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700196 if (mCurrentColorAnim != null) {
197 mCurrentColorAnim.cancel();
198 mCurrentColorAnim = null;
199 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700200 setTextColor(mOriginalTextColor);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800201 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700202
203 mAccessibleDrag = options.isAccessibleDrag;
204 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700205 }
206
207 @Override
208 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700209 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 }
211
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700212 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700213
Winson Chung1054d4e2018-03-05 19:39:21 +0000214 public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700215
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700217 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700218 return mActive && (mAccessibleDrag ||
219 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700220 }
221
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700223 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700225 setOnClickListener(null);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100226 setSelected(false);
Winson Chung61fa4192011-06-12 15:15:29 -0700227 }
228
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 /**
230 * On drop animate the dropView to the icon.
231 */
232 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700233 public void onDrop(final DragObject d, final DragOptions options) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700234 if (options.isFlingToDelete) {
235 // FlingAnimation handles the animation and then calls completeDrop().
236 return;
237 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700238 final DragLayer dragLayer = mLauncher.getDragLayer();
Sunny Goyal0f76b562016-12-13 19:37:10 -0800239 final Rect to = getIconRect(d);
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700240 final float scale = (float) to.width() / d.dragView.getMeasuredWidth();
Steven Ngcf93ef62021-04-22 17:44:22 +0100241 d.dragView.detachContentView(/* reattachToPreviousParent= */ true);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700242 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700243
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800244 Runnable onAnimationEndRunnable = () -> {
245 completeDrop(d);
246 mDropTargetBar.onDragEnd();
247 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700248 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800249
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700250 dragLayer.animateView(d.dragView, to, scale, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800251 DRAG_VIEW_DROP_DURATION,
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700252 Interpolators.DEACCEL_2, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700253 DragLayer.ANIMATION_END_DISAPPEAR, null);
254 }
255
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700256 public abstract int getAccessibilityAction();
257
Sunny Goyale9b651e2015-04-24 11:44:51 -0700258 @Override
259 public void prepareAccessibilityDrop() { }
260
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700261 public abstract void onAccessibilityDrop(View view, ItemInfo item);
262
Sunny Goyal0f76b562016-12-13 19:37:10 -0800263 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700264
Winson Chung61fa4192011-06-12 15:15:29 -0700265 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700266 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700267 super.getHitRect(outRect);
Alex Chaua02eddc2021-04-29 00:36:06 +0100268 outRect.bottom += mLauncher.getDeviceProfile().dropTargetDragPaddingPx;
Winson Chung156ab5b2013-07-12 14:14:16 -0700269
Sunny Goyalac00cba2017-11-13 15:58:01 -0800270 sTempCords[0] = sTempCords[1] = 0;
271 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
272 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700273 }
274
Sunny Goyal0f76b562016-12-13 19:37:10 -0800275 public Rect getIconRect(DragObject dragObject) {
276 int viewWidth = dragObject.dragView.getMeasuredWidth();
277 int viewHeight = dragObject.dragView.getMeasuredHeight();
278 int drawableWidth = mDrawable.getIntrinsicWidth();
279 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800280 DragLayer dragLayer = mLauncher.getDragLayer();
281
282 // Find the rect to animate to (the view is center aligned)
283 Rect to = new Rect();
284 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800285
286 final int width = drawableWidth;
287 final int height = drawableHeight;
288
289 final int left;
290 final int right;
291
Sunny Goyal70660032015-05-14 00:07:08 -0700292 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800293 right = to.right - getPaddingRight();
294 left = right - width;
295 } else {
296 left = to.left + getPaddingLeft();
297 right = left + width;
298 }
299
300 final int top = to.top + (getMeasuredHeight() - height) / 2;
301 final int bottom = top + height;
302
303 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800304
305 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800306 final int xOffset = -(viewWidth - width) / 2;
307 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800308 to.offset(xOffset, yOffset);
309
310 return to;
311 }
312
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700313 @Override
314 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700315 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700316 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700317
318 public int getTextColor() {
319 return getTextColors().getDefaultColor();
320 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700321
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800322 public void setTextVisible(boolean isVisible) {
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800323 CharSequence newText = isVisible ? mText : "";
324 if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800325 mTextVisible = isVisible;
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800326 setText(newText);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800327 if (mTextVisible) {
328 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
329 } else {
330 setCompoundDrawablesRelativeWithIntrinsicBounds(null, mDrawable, null, null);
331 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700332 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700333 }
334
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800335 public void setToolTipLocation(int location) {
336 mToolTipLocation = location;
337 hideTooltip();
338 }
339
340 public boolean isTextTruncated(int availableWidth) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700341 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
342 + getCompoundDrawablePadding());
343 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
344 TextUtils.TruncateAt.END);
345 return !mText.equals(displayedText);
346 }
Winson Chung61fa4192011-06-12 15:15:29 -0700347}