blob: 61b5564941711ede0156d892aea04b0239ff253d [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
Winson Chung61fa4192011-06-12 15:15:29 -070023import android.content.Context;
Tony Wickham855b1b52016-03-28 16:56:30 -070024import android.content.res.Resources;
Winson Chung61967cb2012-02-28 18:11:33 -080025import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070026import android.graphics.drawable.Drawable;
Jon Mirandabfaa4a42017-08-21 15:31:51 -070027import android.text.TextUtils;
Winson Chung61fa4192011-06-12 15:15:29 -070028import android.util.AttributeSet;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080029import android.view.LayoutInflater;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080030import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070031import android.view.View.OnClickListener;
Sunny Goyale78e3d72015-09-24 11:23:31 -070032import android.view.accessibility.AccessibilityEvent;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080033import android.widget.PopupWindow;
Adam Cohend4d7aa52011-07-19 21:47:37 -070034import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070035
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070036import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070037import com.android.launcher3.dragndrop.DragController;
38import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070039import com.android.launcher3.dragndrop.DragOptions;
Pierre Barbier de Reuille43e17302021-06-30 18:50:41 +010040import com.android.launcher3.dragndrop.DragView;
Sunny Goyale396abf2020-04-06 15:11:17 -070041import com.android.launcher3.model.data.ItemInfo;
Winson Chung61fa4192011-06-12 15:15:29 -070042
43/**
44 * Implements a DropTarget.
45 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070046public abstract class ButtonDropTarget extends TextView
47 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070048
Sunny Goyalac00cba2017-11-13 15:58:01 -080049 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070050 private static final int DRAG_VIEW_DROP_DURATION = 285;
Steven Ng6a2acfa2021-03-19 13:23:53 +000051 private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
Winson Chung61fa4192011-06-12 15:15:29 -070052
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080053 public static final int TOOLTIP_DEFAULT = 0;
54 public static final int TOOLTIP_LEFT = 1;
55 public static final int TOOLTIP_RIGHT = 2;
56
Sunny Goyal47328fd2016-05-25 18:56:41 -070057 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070058
Sunny Goyal47328fd2016-05-25 18:56:41 -070059 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070060
61 /** Whether this drop target is active for the current drag */
62 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070063 /** Whether an accessible drag is in progress */
64 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070065 /** An item must be dragged at least this many pixels before this drop target is enabled. */
66 private final int mDragDistanceThreshold;
Yogisha Dixited052ab2021-06-08 21:19:15 +010067 /** The size of the drawable shown in the drop target. */
68 private final int mDrawableSize;
Winson Chung61fa4192011-06-12 15:15:29 -070069
Jon Mirandabfaa4a42017-08-21 15:31:51 -070070 protected CharSequence mText;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070071 protected Drawable mDrawable;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080072 private boolean mTextVisible = true;
73
74 private PopupWindow mToolTip;
75 private int mToolTipLocation;
Sunny Goyalfa401a12015-04-10 13:45:42 -070076
Winson Chung61fa4192011-06-12 15:15:29 -070077 public ButtonDropTarget(Context context, AttributeSet attrs) {
78 this(context, attrs, 0);
79 }
80
81 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070083 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070084
Tony Wickham855b1b52016-03-28 16:56:30 -070085 Resources resources = getResources();
Tony Wickham855b1b52016-03-28 16:56:30 -070086 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Yogisha Dixited052ab2021-06-08 21:19:15 +010087 mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_text_size);
Winson Chung61fa4192011-06-12 15:15:29 -070088 }
89
Sunny Goyalfa401a12015-04-10 13:45:42 -070090 @Override
91 protected void onFinishInflate() {
92 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -070093 mText = getText();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080094 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -070095 }
96
Winson Chung1054d4e2018-03-05 19:39:21 +000097 protected void updateText(int resId) {
98 setText(resId);
99 mText = getText();
100 setContentDescription(mText);
101 }
102
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700104 // We do not set the drawable in the xml as that inflates two drawables corresponding to
105 // drawableLeft and drawableStart.
Yogisha Dixit58c5e652021-06-16 14:18:22 +0100106 mDrawable = getContext().getDrawable(resId).mutate();
107 mDrawable.setBounds(0, 0, mDrawableSize, mDrawableSize);
Yogisha Dixitc0ac1dd2021-05-29 00:26:25 +0100108 mDrawable.setTintList(getTextColors());
Yogisha Dixit58c5e652021-06-16 14:18:22 +0100109 setCompoundDrawablesRelative(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 }
111
Sunny Goyal47328fd2016-05-25 18:56:41 -0700112 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700113 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114 }
115
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800116 private void hideTooltip() {
117 if (mToolTip != null) {
118 mToolTip.dismiss();
119 mToolTip = null;
120 }
121 }
122
Sunny Goyalfa401a12015-04-10 13:45:42 -0700123 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 public final void onDragEnter(DragObject d) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700125 if (!mAccessibleDrag && !mTextVisible) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800126 // Show tooltip
127 hideTooltip();
128
129 TextView message = (TextView) LayoutInflater.from(getContext()).inflate(
130 R.layout.drop_target_tool_tip, null);
131 message.setText(mText);
132
133 mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT);
134 int x = 0, y = 0;
135 if (mToolTipLocation != TOOLTIP_DEFAULT) {
136 y = -getMeasuredHeight();
137 message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
138 if (mToolTipLocation == TOOLTIP_LEFT) {
139 x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
140 } else {
141 x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
142 }
143 }
144 mToolTip.showAsDropDown(this, x, y);
145 }
146
Steven Ng6a2acfa2021-03-19 13:23:53 +0000147 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100148 setSelected(true);
Sunny Goyald21301e2015-09-25 12:17:08 -0700149 if (d.stateAnnouncer != null) {
150 d.stateAnnouncer.cancel();
151 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700152 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700153 }
154
Sunny Goyalfa401a12015-04-10 13:45:42 -0700155 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700156 public void onDragOver(DragObject d) {
157 // Do nothing
158 }
159
Sunny Goyalfa401a12015-04-10 13:45:42 -0700160 @Override
161 public final void onDragExit(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800162 hideTooltip();
163
Sunny Goyalfa401a12015-04-10 13:45:42 -0700164 if (!d.dragComplete) {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000165 d.dragView.setAlpha(1f);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100166 setSelected(false);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700167 } else {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000168 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700169 }
Winson Chung61fa4192011-06-12 15:15:29 -0700170 }
171
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700172 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700173 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800174 mActive = !options.isKeyboardDrag && supportsDrop(dragObject.dragInfo);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800175 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700176
177 mAccessibleDrag = options.isAccessibleDrag;
178 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700179 }
180
181 @Override
182 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700183 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 }
185
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700186 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700187
Winson Chung1054d4e2018-03-05 19:39:21 +0000188 public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700189
Sunny Goyalfa401a12015-04-10 13:45:42 -0700190 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700191 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700192 return mActive && (mAccessibleDrag ||
193 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700194 }
195
Sunny Goyalfa401a12015-04-10 13:45:42 -0700196 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700197 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700199 setOnClickListener(null);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100200 setSelected(false);
Winson Chung61fa4192011-06-12 15:15:29 -0700201 }
202
Sunny Goyalfa401a12015-04-10 13:45:42 -0700203 /**
204 * On drop animate the dropView to the icon.
205 */
206 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700207 public void onDrop(final DragObject d, final DragOptions options) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700208 if (options.isFlingToDelete) {
209 // FlingAnimation handles the animation and then calls completeDrop().
210 return;
211 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700212 final DragLayer dragLayer = mLauncher.getDragLayer();
Pierre Barbier de Reuille43e17302021-06-30 18:50:41 +0100213 final DragView dragView = d.dragView;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 final Rect from = new Rect();
215 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
216
Sunny Goyal0f76b562016-12-13 19:37:10 -0800217 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700218 final float scale = (float) to.width() / from.width();
Pierre Barbier de Reuille43e17302021-06-30 18:50:41 +0100219 dragView.disableColorExtraction();
220 dragView.detachContentView(/* reattachToPreviousParent= */ true);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700221 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800223 Runnable onAnimationEndRunnable = () -> {
224 completeDrop(d);
225 mDropTargetBar.onDragEnd();
226 mLauncher.getStateManager().goToState(NORMAL);
Pierre Barbier de Reuille43e17302021-06-30 18:50:41 +0100227 // Only re-enable updates once the workspace is back to normal, which will be after the
228 // current frame.
229 post(dragView::resumeColorExtraction);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700230 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800231
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800233 DRAG_VIEW_DROP_DURATION,
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700234 Interpolators.DEACCEL_2, Interpolators.LINEAR, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700235 DragLayer.ANIMATION_END_DISAPPEAR, null);
236 }
237
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700238 public abstract int getAccessibilityAction();
239
Sunny Goyale9b651e2015-04-24 11:44:51 -0700240 @Override
241 public void prepareAccessibilityDrop() { }
242
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700243 public abstract void onAccessibilityDrop(View view, ItemInfo item);
244
Sunny Goyal0f76b562016-12-13 19:37:10 -0800245 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700246
Winson Chung61fa4192011-06-12 15:15:29 -0700247 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700248 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700249 super.getHitRect(outRect);
Alex Chaua02eddc2021-04-29 00:36:06 +0100250 outRect.bottom += mLauncher.getDeviceProfile().dropTargetDragPaddingPx;
Winson Chung156ab5b2013-07-12 14:14:16 -0700251
Sunny Goyalac00cba2017-11-13 15:58:01 -0800252 sTempCords[0] = sTempCords[1] = 0;
253 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
254 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700255 }
256
Sunny Goyal0f76b562016-12-13 19:37:10 -0800257 public Rect getIconRect(DragObject dragObject) {
258 int viewWidth = dragObject.dragView.getMeasuredWidth();
259 int viewHeight = dragObject.dragView.getMeasuredHeight();
260 int drawableWidth = mDrawable.getIntrinsicWidth();
261 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800262 DragLayer dragLayer = mLauncher.getDragLayer();
263
264 // Find the rect to animate to (the view is center aligned)
265 Rect to = new Rect();
266 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800267
268 final int width = drawableWidth;
269 final int height = drawableHeight;
270
271 final int left;
272 final int right;
273
Sunny Goyal70660032015-05-14 00:07:08 -0700274 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275 right = to.right - getPaddingRight();
276 left = right - width;
277 } else {
278 left = to.left + getPaddingLeft();
279 right = left + width;
280 }
281
282 final int top = to.top + (getMeasuredHeight() - height) / 2;
283 final int bottom = top + height;
284
285 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800286
287 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800288 final int xOffset = -(viewWidth - width) / 2;
289 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800290 to.offset(xOffset, yOffset);
291
292 return to;
293 }
294
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700295 @Override
296 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700297 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700298 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700299
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800300 public void setTextVisible(boolean isVisible) {
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800301 CharSequence newText = isVisible ? mText : "";
302 if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800303 mTextVisible = isVisible;
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800304 setText(newText);
Yogisha Dixit58c5e652021-06-16 14:18:22 +0100305 setCompoundDrawablesRelative(mDrawable, null, null, null);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700306 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700307 }
308
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800309 public void setToolTipLocation(int location) {
310 mToolTipLocation = location;
311 hideTooltip();
312 }
313
314 public boolean isTextTruncated(int availableWidth) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700315 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
316 + getCompoundDrawablePadding());
317 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
318 TextUtils.TruncateAt.END);
319 return !mText.equals(displayedText);
320 }
Winson Chung61fa4192011-06-12 15:15:29 -0700321}