blob: 108e55779748c2dd03cff6c145f1dceb06a3eedb [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
Winson Chung61fa4192011-06-12 15:15:29 -070021import android.content.Context;
Tony Wickham855b1b52016-03-28 16:56:30 -070022import android.content.res.Resources;
Jordan Silva567372f2023-03-28 00:33:08 +010023import android.graphics.Paint;
Winson Chung61967cb2012-02-28 18:11:33 -080024import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070025import android.graphics.drawable.Drawable;
Alex Chau906d8822022-05-23 10:42:25 +010026import android.text.InputType;
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
Andrew Cole44898c32023-04-05 13:49:32 -070036import androidx.annotation.VisibleForTesting;
37
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070038import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070039import com.android.launcher3.dragndrop.DragController;
40import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070041import com.android.launcher3.dragndrop.DragOptions;
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +010042import com.android.launcher3.dragndrop.DragView;
Sunny Goyale396abf2020-04-06 15:11:17 -070043import com.android.launcher3.model.data.ItemInfo;
Andrew Cole44898c32023-04-05 13:49:32 -070044import com.android.launcher3.views.ActivityContext;
Winson Chung61fa4192011-06-12 15:15:29 -070045
46/**
47 * Implements a DropTarget.
48 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070049public abstract class ButtonDropTarget extends TextView
50 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070051
Sunny Goyalac00cba2017-11-13 15:58:01 -080052 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070053 private static final int DRAG_VIEW_DROP_DURATION = 285;
Steven Ng6a2acfa2021-03-19 13:23:53 +000054 private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
Alex Chau906d8822022-05-23 10:42:25 +010055 private static final int MAX_LINES_TEXT_MULTI_LINE = 2;
56 private static final int MAX_LINES_TEXT_SINGLE_LINE = 1;
Winson Chung61fa4192011-06-12 15:15:29 -070057
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080058 public static final int TOOLTIP_DEFAULT = 0;
59 public static final int TOOLTIP_LEFT = 1;
60 public static final int TOOLTIP_RIGHT = 2;
61
Andrew Cole44898c32023-04-05 13:49:32 -070062 protected final ActivityContext mActivityContext;
63 protected final DropTargetHandler mDropTargetHandler;
Sunny Goyal47328fd2016-05-25 18:56:41 -070064 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070065
66 /** Whether this drop target is active for the current drag */
67 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070068 /** Whether an accessible drag is in progress */
69 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070070 /** An item must be dragged at least this many pixels before this drop target is enabled. */
71 private final int mDragDistanceThreshold;
Yogisha Dixited052ab2021-06-08 21:19:15 +010072 /** The size of the drawable shown in the drop target. */
73 private final int mDrawableSize;
Steven Nga1dcbce2021-07-23 17:06:10 +010074 /** The padding, in pixels, between the text and drawable. */
75 private final int mDrawablePadding;
Winson Chung61fa4192011-06-12 15:15:29 -070076
Jon Mirandabfaa4a42017-08-21 15:31:51 -070077 protected CharSequence mText;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070078 protected Drawable mDrawable;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080079 private boolean mTextVisible = true;
Alex Chau906d8822022-05-23 10:42:25 +010080 private boolean mIconVisible = true;
81 private boolean mTextMultiLine = true;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080082
83 private PopupWindow mToolTip;
84 private int mToolTipLocation;
Sunny Goyalfa401a12015-04-10 13:45:42 -070085
Andrew Cole44898c32023-04-05 13:49:32 -070086 public ButtonDropTarget(Context context) {
87 this(context, null, 0);
88 }
Winson Chung61fa4192011-06-12 15:15:29 -070089 public ButtonDropTarget(Context context, AttributeSet attrs) {
90 this(context, attrs, 0);
91 }
92
93 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
94 super(context, attrs, defStyle);
Andrew Cole44898c32023-04-05 13:49:32 -070095 mActivityContext = ActivityContext.lookupContext(context);
96 mDropTargetHandler = mActivityContext.getDropTargetHandler();
Sunny Goyal47328fd2016-05-25 18:56:41 -070097
Tony Wickham855b1b52016-03-28 16:56:30 -070098 Resources resources = getResources();
Tony Wickham855b1b52016-03-28 16:56:30 -070099 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Pat Manninge63dd252022-08-02 16:15:29 +0100100 mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_button_drawable_size);
Steven Nga1dcbce2021-07-23 17:06:10 +0100101 mDrawablePadding = resources.getDimensionPixelSize(
102 R.dimen.drop_target_button_drawable_padding);
Winson Chung61fa4192011-06-12 15:15:29 -0700103 }
104
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 @Override
106 protected void onFinishInflate() {
107 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700108 mText = getText();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800109 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -0700110 }
111
Winson Chung1054d4e2018-03-05 19:39:21 +0000112 protected void updateText(int resId) {
113 setText(resId);
114 mText = getText();
115 setContentDescription(mText);
116 }
117
Andrew Colef9b87e32023-05-11 14:45:40 -0700118 protected void updateText(CharSequence text) {
119 setText(text);
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.
Yogisha Dixit58c5e652021-06-16 14:18:22 +0100127 mDrawable = getContext().getDrawable(resId).mutate();
Yogisha Dixit3cbd7e32021-05-29 00:26:25 +0100128 mDrawable.setTintList(getTextColors());
Alex Chau906d8822022-05-23 10:42:25 +0100129 updateIconVisibility();
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) {
Sebastian Francod2d8e972022-04-04 14:51:53 -0700159 x = -getMeasuredWidth() - message.getMeasuredWidth() / 2;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800160 } 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) {
Alex Chau158caf42022-05-24 17:09:32 +0100194 if (options.isKeyboardDrag) {
195 mActive = false;
196 } else {
197 setupItemInfo(dragObject.dragInfo);
198 mActive = supportsDrop(dragObject.dragInfo);
199 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800200 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700201
202 mAccessibleDrag = options.isAccessibleDrag;
203 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700204 }
205
206 @Override
207 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700208 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700209 }
210
Alex Chau158caf42022-05-24 17:09:32 +0100211 /**
212 * Setups button for the specified ItemInfo.
213 */
214 protected abstract void setupItemInfo(ItemInfo info);
215
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700216 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217
Winson Chung1054d4e2018-03-05 19:39:21 +0000218 public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700219
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700221 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700222 return mActive && (mAccessibleDrag ||
Andrew Cole44898c32023-04-05 13:49:32 -0700223 mActivityContext.getDragController().getDistanceDragged()
224 >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700225 }
226
Sunny Goyalfa401a12015-04-10 13:45:42 -0700227 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700228 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700230 setOnClickListener(null);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100231 setSelected(false);
Winson Chung61fa4192011-06-12 15:15:29 -0700232 }
233
Sunny Goyalfa401a12015-04-10 13:45:42 -0700234 /**
235 * On drop animate the dropView to the icon.
236 */
237 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700238 public void onDrop(final DragObject d, final DragOptions options) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700239 if (options.isFlingToDelete) {
240 // FlingAnimation handles the animation and then calls completeDrop().
241 return;
242 }
Andrew Cole44898c32023-04-05 13:49:32 -0700243
244 final DragLayer dragLayer = mDropTargetHandler.getDragLayer();
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +0100245 final DragView dragView = d.dragView;
Sunny Goyal0f76b562016-12-13 19:37:10 -0800246 final Rect to = getIconRect(d);
Sunny Goyal031418c2021-07-22 10:02:50 -0700247 final float scale = (float) to.width() / dragView.getMeasuredWidth();
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +0100248 dragView.detachContentView(/* reattachToPreviousParent= */ true);
249
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700250 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700251
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800252 Runnable onAnimationEndRunnable = () -> {
253 completeDrop(d);
254 mDropTargetBar.onDragEnd();
Andrew Cole44898c32023-04-05 13:49:32 -0700255 mDropTargetHandler.onDropAnimationComplete();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700256 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800257
Andrew Cole44898c32023-04-05 13:49:32 -0700258
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700259 dragLayer.animateView(d.dragView, to, scale, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800260 DRAG_VIEW_DROP_DURATION,
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700261 Interpolators.DEACCEL_2, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700262 DragLayer.ANIMATION_END_DISAPPEAR, null);
263 }
264
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700265 public abstract int getAccessibilityAction();
266
Sunny Goyale9b651e2015-04-24 11:44:51 -0700267 @Override
268 public void prepareAccessibilityDrop() { }
269
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700270 public abstract void onAccessibilityDrop(View view, ItemInfo item);
271
Sunny Goyal0f76b562016-12-13 19:37:10 -0800272 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700273
Winson Chung61fa4192011-06-12 15:15:29 -0700274 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700275 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700276 super.getHitRect(outRect);
Andrew Cole44898c32023-04-05 13:49:32 -0700277 outRect.bottom += mActivityContext.getDeviceProfile().dropTargetDragPaddingPx;
Winson Chung156ab5b2013-07-12 14:14:16 -0700278
Sunny Goyalac00cba2017-11-13 15:58:01 -0800279 sTempCords[0] = sTempCords[1] = 0;
Andrew Cole44898c32023-04-05 13:49:32 -0700280 mActivityContext.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
Sunny Goyalac00cba2017-11-13 15:58:01 -0800281 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700282 }
283
Sunny Goyal0f76b562016-12-13 19:37:10 -0800284 public Rect getIconRect(DragObject dragObject) {
285 int viewWidth = dragObject.dragView.getMeasuredWidth();
286 int viewHeight = dragObject.dragView.getMeasuredHeight();
287 int drawableWidth = mDrawable.getIntrinsicWidth();
288 int drawableHeight = mDrawable.getIntrinsicHeight();
Andrew Cole44898c32023-04-05 13:49:32 -0700289 DragLayer dragLayer = mDropTargetHandler.getDragLayer();
Winson Chung61967cb2012-02-28 18:11:33 -0800290
291 // Find the rect to animate to (the view is center aligned)
292 Rect to = new Rect();
293 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800294
295 final int width = drawableWidth;
296 final int height = drawableHeight;
297
298 final int left;
299 final int right;
300
Sunny Goyal70660032015-05-14 00:07:08 -0700301 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800302 right = to.right - getPaddingRight();
303 left = right - width;
304 } else {
305 left = to.left + getPaddingLeft();
306 right = left + width;
307 }
308
309 final int top = to.top + (getMeasuredHeight() - height) / 2;
Sebastian Franco63d1a182021-10-18 09:52:04 -0500310 final int bottom = top + height;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800311
312 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800313
314 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800315 final int xOffset = -(viewWidth - width) / 2;
316 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800317 to.offset(xOffset, yOffset);
318
319 return to;
320 }
321
Sebastian Franco63d1a182021-10-18 09:52:04 -0500322 private void centerIcon() {
323 int x = mTextVisible ? 0
324 : (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 - mDrawableSize / 2;
325 mDrawable.setBounds(x, 0, x + mDrawableSize, mDrawableSize);
326 }
327
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700328 @Override
329 public void onClick(View v) {
Andrew Cole44898c32023-04-05 13:49:32 -0700330 mDropTargetHandler.onClick(this);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700331 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -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);
Alex Chau906d8822022-05-23 10:42:25 +0100338 updateIconVisibility();
Pat Manning27bfcaa2022-04-25 13:50:28 +0100339 }
Pat Manning27bfcaa2022-04-25 13:50:28 +0100340 }
341
Alex Chau906d8822022-05-23 10:42:25 +0100342 /**
343 * Display button text over multiple lines when isMultiLine is true, single line otherwise.
344 */
345 public void setTextMultiLine(boolean isMultiLine) {
346 if (mTextMultiLine != isMultiLine) {
347 mTextMultiLine = isMultiLine;
348 setSingleLine(!isMultiLine);
349 setMaxLines(isMultiLine ? MAX_LINES_TEXT_MULTI_LINE : MAX_LINES_TEXT_SINGLE_LINE);
350 int inputType = InputType.TYPE_CLASS_TEXT;
351 if (isMultiLine) {
352 inputType |= InputType.TYPE_TEXT_FLAG_MULTI_LINE;
353
354 }
355 setInputType(inputType);
356 }
357 }
358
359 protected boolean isTextMultiLine() {
360 return mTextMultiLine;
361 }
362
363 /**
364 * Sets the button icon visible when isVisible is true, hides it otherwise.
365 */
366 public void setIconVisible(boolean isVisible) {
367 if (mIconVisible != isVisible) {
368 mIconVisible = isVisible;
369 updateIconVisibility();
370 }
371 }
372
373 private void updateIconVisibility() {
374 if (mIconVisible) {
375 centerIcon();
376 }
377 setCompoundDrawablesRelative(mIconVisible ? mDrawable : null, null, null, null);
378 setCompoundDrawablePadding(mIconVisible && mTextVisible ? mDrawablePadding : 0);
379 }
380
Sebastian Franco63d1a182021-10-18 09:52:04 -0500381 @Override
382 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
383 super.onSizeChanged(w, h, oldw, oldh);
384 centerIcon();
385 }
386
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800387 public void setToolTipLocation(int location) {
388 mToolTipLocation = location;
389 hideTooltip();
390 }
391
Pat Manninge63dd252022-08-02 16:15:29 +0100392 /**
393 * Returns if the text will be truncated within the provided availableWidth.
394 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800395 public boolean isTextTruncated(int availableWidth) {
Pat Manninge63dd252022-08-02 16:15:29 +0100396 availableWidth -= getPaddingLeft() + getPaddingRight();
397 if (mIconVisible) {
398 availableWidth -= mDrawable.getIntrinsicWidth() + getCompoundDrawablePadding();
399 }
400 if (availableWidth <= 0) {
401 return true;
402 }
403 CharSequence firstLine = TextUtils.ellipsize(mText, getPaint(), availableWidth,
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700404 TextUtils.TruncateAt.END);
Pat Manninge63dd252022-08-02 16:15:29 +0100405 if (!mTextMultiLine) {
406 return !TextUtils.equals(mText, firstLine);
407 }
408 if (TextUtils.equals(mText, firstLine)) {
409 // When multi-line is active, if it can display as one line, then text is not truncated.
410 return false;
411 }
412 CharSequence secondLine =
413 TextUtils.ellipsize(mText.subSequence(firstLine.length(), mText.length()),
414 getPaint(), availableWidth, TextUtils.TruncateAt.END);
415 return !(TextUtils.equals(mText.subSequence(0, firstLine.length()), firstLine)
416 && TextUtils.equals(mText.subSequence(firstLine.length(), secondLine.length()),
417 secondLine));
418 }
419
420 /**
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000421 * Returns if the text will be clipped vertically within the provided availableHeight.
422 */
Andrew Cole44898c32023-04-05 13:49:32 -0700423 @VisibleForTesting
424 protected boolean isTextClippedVertically(int availableHeight) {
Jordan Silva567372f2023-03-28 00:33:08 +0100425 Paint.FontMetricsInt fontMetricsInt = getPaint().getFontMetricsInt();
426 int lineCount = (getLineCount() <= 0) ? 1 : getLineCount();
427 int textHeight = lineCount * (fontMetricsInt.bottom - fontMetricsInt.top);
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000428
Jordan Silva567372f2023-03-28 00:33:08 +0100429 return textHeight + getPaddingTop() + getPaddingBottom() >= availableHeight;
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000430 }
431
432 /**
Pat Manninge63dd252022-08-02 16:15:29 +0100433 * Reduce the size of the text until it fits the measured width or reaches a minimum.
434 *
435 * The minimum size is defined by {@code R.dimen.button_drop_target_min_text_size} and
436 * it diminishes by intervals defined by
437 * {@code R.dimen.button_drop_target_resize_text_increment}
438 * This functionality is very similar to the option
439 * {@link TextView#setAutoSizeTextTypeWithDefaults(int)} but can't be used in this view because
440 * the layout width is {@code WRAP_CONTENT}.
441 *
442 * @return The biggest text size in SP that makes the text fit or if the text can't fit returns
443 * the min available value
444 */
445 public float resizeTextToFit() {
446 float minSize = Utilities.pxToSp(getResources()
447 .getDimensionPixelSize(R.dimen.button_drop_target_min_text_size));
448 float step = Utilities.pxToSp(getResources()
449 .getDimensionPixelSize(R.dimen.button_drop_target_resize_text_increment));
450 float textSize = Utilities.pxToSp(getTextSize());
451
452 int availableWidth = getMeasuredWidth();
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000453 int availableHeight = getMeasuredHeight();
454
455 while (isTextTruncated(availableWidth) || isTextClippedVertically(availableHeight)) {
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800456 textSize -= step;
457 if (textSize < minSize) {
458 textSize = minSize;
Pat Manninge63dd252022-08-02 16:15:29 +0100459 setTextSize(textSize);
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800460 break;
Pat Manninge63dd252022-08-02 16:15:29 +0100461 }
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800462 setTextSize(textSize);
Pat Manninge63dd252022-08-02 16:15:29 +0100463 }
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800464 return textSize;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700465 }
Winson Chung61fa4192011-06-12 15:15:29 -0700466}