blob: 00317f70105d4136a3e769780811437c6696a9e4 [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
Yogisha Dixita3a6f512021-04-28 13:45:37 +010039import androidx.appcompat.content.res.AppCompatResources;
40
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070041import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070042import com.android.launcher3.dragndrop.DragController;
43import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070044import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyale396abf2020-04-06 15:11:17 -070045import com.android.launcher3.model.data.ItemInfo;
Winson Chung61fa4192011-06-12 15:15:29 -070046
47/**
48 * Implements a DropTarget.
49 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070050public abstract class ButtonDropTarget extends TextView
51 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070052
Sunny Goyal55bdeed2018-09-18 16:17:22 -070053 private static final Property<ButtonDropTarget, Integer> TEXT_COLOR =
54 new Property<ButtonDropTarget, Integer>(Integer.TYPE, "textColor") {
55
56 @Override
57 public Integer get(ButtonDropTarget target) {
58 return target.getTextColor();
59 }
60
61 @Override
62 public void set(ButtonDropTarget target, Integer value) {
63 target.setTextColor(value);
64 }
65 };
66
Sunny Goyalac00cba2017-11-13 15:58:01 -080067 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070068 private static final int DRAG_VIEW_DROP_DURATION = 285;
Steven Ng6a2acfa2021-03-19 13:23:53 +000069 private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
Winson Chung61fa4192011-06-12 15:15:29 -070070
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080071 public static final int TOOLTIP_DEFAULT = 0;
72 public static final int TOOLTIP_LEFT = 1;
73 public static final int TOOLTIP_RIGHT = 2;
74
Sunny Goyal47328fd2016-05-25 18:56:41 -070075 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070076
Winson Chunga62e9fd2011-07-11 15:20:48 -070077 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070078 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070079
80 /** Whether this drop target is active for the current drag */
81 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070082 /** Whether an accessible drag is in progress */
83 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070084 /** An item must be dragged at least this many pixels before this drop target is enabled. */
85 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070086
Jon Mirandabfaa4a42017-08-21 15:31:51 -070087 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070089 protected Drawable mDrawable;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080090 private boolean mTextVisible = true;
91
92 private PopupWindow mToolTip;
93 private int mToolTipLocation;
Sunny Goyalfa401a12015-04-10 13:45:42 -070094
Sunny Goyal3a644ed2015-05-21 10:28:02 -070095 private AnimatorSet mCurrentColorAnim;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070096
Winson Chung61fa4192011-06-12 15:15:29 -070097 public ButtonDropTarget(Context context, AttributeSet attrs) {
98 this(context, attrs, 0);
99 }
100
101 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
102 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -0700103 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -0700104
Tony Wickham855b1b52016-03-28 16:56:30 -0700105 Resources resources = getResources();
106 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Tony Wickham855b1b52016-03-28 16:56:30 -0700107 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700108 }
109
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 @Override
111 protected void onFinishInflate() {
112 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700113 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 mOriginalTextColor = getTextColors();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800115 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -0700116 }
117
Winson Chung1054d4e2018-03-05 19:39:21 +0000118 protected void updateText(int resId) {
119 setText(resId);
120 mText = getText();
121 setContentDescription(mText);
122 }
123
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700125 // We do not set the drawable in the xml as that inflates two drawables corresponding to
126 // drawableLeft and drawableStart.
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800127 if (mTextVisible) {
128 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
129 mDrawable = getCompoundDrawablesRelative()[0];
130 } else {
131 setCompoundDrawablesRelativeWithIntrinsicBounds(0, resId, 0, 0);
132 mDrawable = getCompoundDrawablesRelative()[1];
133 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700134 }
135
Sunny Goyal47328fd2016-05-25 18:56:41 -0700136 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700137 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700138 }
139
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800140 private void hideTooltip() {
141 if (mToolTip != null) {
142 mToolTip.dismiss();
143 mToolTip = null;
144 }
145 }
146
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100147 private void setBackgroundDrawable(int resId) {
148 Drawable bd = AppCompatResources.getDrawable(getContext(), resId);
149 setBackground(bd);
150 }
151
Sunny Goyalfa401a12015-04-10 13:45:42 -0700152 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700153 public final void onDragEnter(DragObject d) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700154 if (!mAccessibleDrag && !mTextVisible) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800155 // Show tooltip
156 hideTooltip();
157
158 TextView message = (TextView) LayoutInflater.from(getContext()).inflate(
159 R.layout.drop_target_tool_tip, null);
160 message.setText(mText);
161
162 mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT);
163 int x = 0, y = 0;
164 if (mToolTipLocation != TOOLTIP_DEFAULT) {
165 y = -getMeasuredHeight();
166 message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
167 if (mToolTipLocation == TOOLTIP_LEFT) {
168 x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
169 } else {
170 x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
171 }
172 }
173 mToolTip.showAsDropDown(this, x, y);
174 }
175
Steven Ng6a2acfa2021-03-19 13:23:53 +0000176 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100177 setBackgroundDrawable(R.drawable.drop_target_frame_hover);
Sunny Goyald21301e2015-09-25 12:17:08 -0700178 if (d.stateAnnouncer != null) {
179 d.stateAnnouncer.cancel();
180 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700181 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700182 }
183
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700185 public void onDragOver(DragObject d) {
186 // Do nothing
187 }
188
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 @Override
190 public final void onDragExit(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800191 hideTooltip();
192
Sunny Goyalfa401a12015-04-10 13:45:42 -0700193 if (!d.dragComplete) {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000194 d.dragView.setAlpha(1f);
Yogisha Dixita3a6f512021-04-28 13:45:37 +0100195 setBackgroundDrawable(R.drawable.drop_target_frame);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700196 } else {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000197 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198 }
Winson Chung61fa4192011-06-12 15:15:29 -0700199 }
200
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700201 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700202 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800203 mActive = !options.isKeyboardDrag && supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700204 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700205 if (mCurrentColorAnim != null) {
206 mCurrentColorAnim.cancel();
207 mCurrentColorAnim = null;
208 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700209 setTextColor(mOriginalTextColor);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800210 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700211
212 mAccessibleDrag = options.isAccessibleDrag;
213 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 }
215
216 @Override
217 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700218 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 }
220
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700221 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222
Winson Chung1054d4e2018-03-05 19:39:21 +0000223 public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700224
Sunny Goyalfa401a12015-04-10 13:45:42 -0700225 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700226 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700227 return mActive && (mAccessibleDrag ||
228 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700229 }
230
Sunny Goyalfa401a12015-04-10 13:45:42 -0700231 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700232 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700233 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700234 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700235 }
236
Sunny Goyalfa401a12015-04-10 13:45:42 -0700237 /**
238 * On drop animate the dropView to the icon.
239 */
240 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700241 public void onDrop(final DragObject d, final DragOptions options) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700242 if (options.isFlingToDelete) {
243 // FlingAnimation handles the animation and then calls completeDrop().
244 return;
245 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700246 final DragLayer dragLayer = mLauncher.getDragLayer();
247 final Rect from = new Rect();
248 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
249
Sunny Goyal0f76b562016-12-13 19:37:10 -0800250 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700251 final float scale = (float) to.width() / from.width();
Steven Ngcf93ef62021-04-22 17:44:22 +0100252 d.dragView.detachContentView(/* reattachToPreviousParent= */ true);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700253 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700254
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800255 Runnable onAnimationEndRunnable = () -> {
256 completeDrop(d);
257 mDropTargetBar.onDragEnd();
258 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700259 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800260
Sunny Goyalfa401a12015-04-10 13:45:42 -0700261 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800262 DRAG_VIEW_DROP_DURATION,
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -0700263 Interpolators.DEACCEL_2, Interpolators.LINEAR, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700264 DragLayer.ANIMATION_END_DISAPPEAR, null);
265 }
266
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700267 public abstract int getAccessibilityAction();
268
Sunny Goyale9b651e2015-04-24 11:44:51 -0700269 @Override
270 public void prepareAccessibilityDrop() { }
271
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700272 public abstract void onAccessibilityDrop(View view, ItemInfo item);
273
Sunny Goyal0f76b562016-12-13 19:37:10 -0800274 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700275
Winson Chung61fa4192011-06-12 15:15:29 -0700276 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700277 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700278 super.getHitRect(outRect);
279 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700280
Sunny Goyalac00cba2017-11-13 15:58:01 -0800281 sTempCords[0] = sTempCords[1] = 0;
282 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
283 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700284 }
285
Sunny Goyal0f76b562016-12-13 19:37:10 -0800286 public Rect getIconRect(DragObject dragObject) {
287 int viewWidth = dragObject.dragView.getMeasuredWidth();
288 int viewHeight = dragObject.dragView.getMeasuredHeight();
289 int drawableWidth = mDrawable.getIntrinsicWidth();
290 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800291 DragLayer dragLayer = mLauncher.getDragLayer();
292
293 // Find the rect to animate to (the view is center aligned)
294 Rect to = new Rect();
295 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800296
297 final int width = drawableWidth;
298 final int height = drawableHeight;
299
300 final int left;
301 final int right;
302
Sunny Goyal70660032015-05-14 00:07:08 -0700303 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800304 right = to.right - getPaddingRight();
305 left = right - width;
306 } else {
307 left = to.left + getPaddingLeft();
308 right = left + width;
309 }
310
311 final int top = to.top + (getMeasuredHeight() - height) / 2;
312 final int bottom = top + height;
313
314 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800315
316 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800317 final int xOffset = -(viewWidth - width) / 2;
318 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800319 to.offset(xOffset, yOffset);
320
321 return to;
322 }
323
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700324 @Override
325 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700326 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700327 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700328
329 public int getTextColor() {
330 return getTextColors().getDefaultColor();
331 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700332
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800333 public void setTextVisible(boolean isVisible) {
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800334 CharSequence newText = isVisible ? mText : "";
335 if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800336 mTextVisible = isVisible;
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800337 setText(newText);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800338 if (mTextVisible) {
339 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
340 } else {
341 setCompoundDrawablesRelativeWithIntrinsicBounds(null, mDrawable, null, null);
342 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700343 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700344 }
345
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800346 public void setToolTipLocation(int location) {
347 mToolTipLocation = location;
348 hideTooltip();
349 }
350
351 public boolean isTextTruncated(int availableWidth) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700352 availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
353 + getCompoundDrawablePadding());
354 CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
355 TextUtils.TruncateAt.END);
356 return !mText.equals(displayedText);
357 }
Winson Chung61fa4192011-06-12 15:15:29 -0700358}