blob: 376cab76d93b360740852df932c0ac8e2546e04a [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;
Alex Chau906d8822022-05-23 10:42:25 +010027import android.text.InputType;
Jon Mirandabfaa4a42017-08-21 15:31:51 -070028import android.text.TextUtils;
Winson Chung61fa4192011-06-12 15:15:29 -070029import android.util.AttributeSet;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080030import android.view.LayoutInflater;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080031import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070032import android.view.View.OnClickListener;
Sunny Goyale78e3d72015-09-24 11:23:31 -070033import android.view.accessibility.AccessibilityEvent;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080034import android.widget.PopupWindow;
Adam Cohend4d7aa52011-07-19 21:47:37 -070035import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070036
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070037import com.android.launcher3.anim.Interpolators;
Vadim Tryshevfedca432015-08-19 17:55:02 -070038import com.android.launcher3.dragndrop.DragController;
39import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070040import com.android.launcher3.dragndrop.DragOptions;
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +010041import com.android.launcher3.dragndrop.DragView;
Sunny Goyale396abf2020-04-06 15:11:17 -070042import com.android.launcher3.model.data.ItemInfo;
Winson Chung61fa4192011-06-12 15:15:29 -070043
44/**
45 * Implements a DropTarget.
46 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070047public abstract class ButtonDropTarget extends TextView
48 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070049
Sunny Goyalac00cba2017-11-13 15:58:01 -080050 private static final int[] sTempCords = new int[2];
Tony Wickhamb54c4a32015-09-11 08:40:20 -070051 private static final int DRAG_VIEW_DROP_DURATION = 285;
Steven Ng6a2acfa2021-03-19 13:23:53 +000052 private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
Alex Chau906d8822022-05-23 10:42:25 +010053 private static final int MAX_LINES_TEXT_MULTI_LINE = 2;
54 private static final int MAX_LINES_TEXT_SINGLE_LINE = 1;
Winson Chung61fa4192011-06-12 15:15:29 -070055
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080056 public static final int TOOLTIP_DEFAULT = 0;
57 public static final int TOOLTIP_LEFT = 1;
58 public static final int TOOLTIP_RIGHT = 2;
59
Helen Cheuk1a55cde2023-03-02 16:45:41 +000060 private final Rect mTempRect = new Rect();
61
Sunny Goyal47328fd2016-05-25 18:56:41 -070062 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070063
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
Winson Chung61fa4192011-06-12 15:15:29 -070086 public ButtonDropTarget(Context context, AttributeSet attrs) {
87 this(context, attrs, 0);
88 }
89
90 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
91 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070092 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070093
Tony Wickham855b1b52016-03-28 16:56:30 -070094 Resources resources = getResources();
Tony Wickham855b1b52016-03-28 16:56:30 -070095 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Pat Manninge63dd252022-08-02 16:15:29 +010096 mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_button_drawable_size);
Steven Nga1dcbce2021-07-23 17:06:10 +010097 mDrawablePadding = resources.getDimensionPixelSize(
98 R.dimen.drop_target_button_drawable_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070099 }
100
Sunny Goyalfa401a12015-04-10 13:45:42 -0700101 @Override
102 protected void onFinishInflate() {
103 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700104 mText = getText();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800105 setContentDescription(mText);
Winson Chung61fa4192011-06-12 15:15:29 -0700106 }
107
Winson Chung1054d4e2018-03-05 19:39:21 +0000108 protected void updateText(int resId) {
109 setText(resId);
110 mText = getText();
111 setContentDescription(mText);
112 }
113
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700115 // We do not set the drawable in the xml as that inflates two drawables corresponding to
116 // drawableLeft and drawableStart.
Yogisha Dixit58c5e652021-06-16 14:18:22 +0100117 mDrawable = getContext().getDrawable(resId).mutate();
Yogisha Dixit3cbd7e32021-05-29 00:26:25 +0100118 mDrawable.setTintList(getTextColors());
Alex Chau906d8822022-05-23 10:42:25 +0100119 updateIconVisibility();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700120 }
121
Sunny Goyal47328fd2016-05-25 18:56:41 -0700122 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700123 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700124 }
125
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800126 private void hideTooltip() {
127 if (mToolTip != null) {
128 mToolTip.dismiss();
129 mToolTip = null;
130 }
131 }
132
Sunny Goyalfa401a12015-04-10 13:45:42 -0700133 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700134 public final void onDragEnter(DragObject d) {
Sunny Goyal9b180102020-03-11 10:02:29 -0700135 if (!mAccessibleDrag && !mTextVisible) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800136 // Show tooltip
137 hideTooltip();
138
139 TextView message = (TextView) LayoutInflater.from(getContext()).inflate(
140 R.layout.drop_target_tool_tip, null);
141 message.setText(mText);
142
143 mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT);
144 int x = 0, y = 0;
145 if (mToolTipLocation != TOOLTIP_DEFAULT) {
146 y = -getMeasuredHeight();
147 message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
148 if (mToolTipLocation == TOOLTIP_LEFT) {
Sebastian Francod2d8e972022-04-04 14:51:53 -0700149 x = -getMeasuredWidth() - message.getMeasuredWidth() / 2;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800150 } else {
151 x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
152 }
153 }
154 mToolTip.showAsDropDown(this, x, y);
155 }
156
Steven Ng6a2acfa2021-03-19 13:23:53 +0000157 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100158 setSelected(true);
Sunny Goyald21301e2015-09-25 12:17:08 -0700159 if (d.stateAnnouncer != null) {
160 d.stateAnnouncer.cancel();
161 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700162 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700163 }
164
Sunny Goyalfa401a12015-04-10 13:45:42 -0700165 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700166 public void onDragOver(DragObject d) {
167 // Do nothing
168 }
169
Sunny Goyalfa401a12015-04-10 13:45:42 -0700170 @Override
171 public final void onDragExit(DragObject d) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800172 hideTooltip();
173
Sunny Goyalfa401a12015-04-10 13:45:42 -0700174 if (!d.dragComplete) {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000175 d.dragView.setAlpha(1f);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100176 setSelected(false);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700177 } else {
Steven Ng6a2acfa2021-03-19 13:23:53 +0000178 d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700179 }
Winson Chung61fa4192011-06-12 15:15:29 -0700180 }
181
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700182 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700183 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Alex Chau158caf42022-05-24 17:09:32 +0100184 if (options.isKeyboardDrag) {
185 mActive = false;
186 } else {
187 setupItemInfo(dragObject.dragInfo);
188 mActive = supportsDrop(dragObject.dragInfo);
189 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800190 setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700191
192 mAccessibleDrag = options.isAccessibleDrag;
193 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700194 }
195
196 @Override
197 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700198 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 }
200
Alex Chau158caf42022-05-24 17:09:32 +0100201 /**
202 * Setups button for the specified ItemInfo.
203 */
204 protected abstract void setupItemInfo(ItemInfo info);
205
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700206 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207
Winson Chung1054d4e2018-03-05 19:39:21 +0000208 public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700209
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700211 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700212 return mActive && (mAccessibleDrag ||
213 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700214 }
215
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700217 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700218 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700219 setOnClickListener(null);
Yogisha Dixit2ff963b2021-06-03 10:41:44 +0100220 setSelected(false);
Winson Chung61fa4192011-06-12 15:15:29 -0700221 }
222
Sunny Goyalfa401a12015-04-10 13:45:42 -0700223 /**
224 * On drop animate the dropView to the icon.
225 */
226 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700227 public void onDrop(final DragObject d, final DragOptions options) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700228 if (options.isFlingToDelete) {
229 // FlingAnimation handles the animation and then calls completeDrop().
230 return;
231 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232 final DragLayer dragLayer = mLauncher.getDragLayer();
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +0100233 final DragView dragView = d.dragView;
Sunny Goyal0f76b562016-12-13 19:37:10 -0800234 final Rect to = getIconRect(d);
Sunny Goyal031418c2021-07-22 10:02:50 -0700235 final float scale = (float) to.width() / dragView.getMeasuredWidth();
Pierre Barbier de Reuille28818012021-06-29 16:47:41 +0100236 dragView.detachContentView(/* reattachToPreviousParent= */ true);
237
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700238 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700239
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800240 Runnable onAnimationEndRunnable = () -> {
241 completeDrop(d);
242 mDropTargetBar.onDragEnd();
243 mLauncher.getStateManager().goToState(NORMAL);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700244 };
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800245
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700246 dragLayer.animateView(d.dragView, to, scale, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800247 DRAG_VIEW_DROP_DURATION,
Sunny Goyal1721ccf2021-06-08 12:57:40 -0700248 Interpolators.DEACCEL_2, onAnimationEndRunnable,
Sunny Goyalfa401a12015-04-10 13:45:42 -0700249 DragLayer.ANIMATION_END_DISAPPEAR, null);
250 }
251
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700252 public abstract int getAccessibilityAction();
253
Sunny Goyale9b651e2015-04-24 11:44:51 -0700254 @Override
255 public void prepareAccessibilityDrop() { }
256
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700257 public abstract void onAccessibilityDrop(View view, ItemInfo item);
258
Sunny Goyal0f76b562016-12-13 19:37:10 -0800259 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700260
Winson Chung61fa4192011-06-12 15:15:29 -0700261 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700262 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700263 super.getHitRect(outRect);
Alex Chaua02eddc2021-04-29 00:36:06 +0100264 outRect.bottom += mLauncher.getDeviceProfile().dropTargetDragPaddingPx;
Winson Chung156ab5b2013-07-12 14:14:16 -0700265
Sunny Goyalac00cba2017-11-13 15:58:01 -0800266 sTempCords[0] = sTempCords[1] = 0;
267 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords);
268 outRect.offsetTo(sTempCords[0], sTempCords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700269 }
270
Sunny Goyal0f76b562016-12-13 19:37:10 -0800271 public Rect getIconRect(DragObject dragObject) {
272 int viewWidth = dragObject.dragView.getMeasuredWidth();
273 int viewHeight = dragObject.dragView.getMeasuredHeight();
274 int drawableWidth = mDrawable.getIntrinsicWidth();
275 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800276 DragLayer dragLayer = mLauncher.getDragLayer();
277
278 // Find the rect to animate to (the view is center aligned)
279 Rect to = new Rect();
280 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800281
282 final int width = drawableWidth;
283 final int height = drawableHeight;
284
285 final int left;
286 final int right;
287
Sunny Goyal70660032015-05-14 00:07:08 -0700288 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800289 right = to.right - getPaddingRight();
290 left = right - width;
291 } else {
292 left = to.left + getPaddingLeft();
293 right = left + width;
294 }
295
296 final int top = to.top + (getMeasuredHeight() - height) / 2;
Sebastian Franco63d1a182021-10-18 09:52:04 -0500297 final int bottom = top + height;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800298
299 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800300
301 // Center the destination rect about the trash icon
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800302 final int xOffset = -(viewWidth - width) / 2;
303 final int yOffset = -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800304 to.offset(xOffset, yOffset);
305
306 return to;
307 }
308
Sebastian Franco63d1a182021-10-18 09:52:04 -0500309 private void centerIcon() {
310 int x = mTextVisible ? 0
311 : (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 - mDrawableSize / 2;
312 mDrawable.setBounds(x, 0, x + mDrawableSize, mDrawableSize);
313 }
314
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700315 @Override
316 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700317 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700318 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700319
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800320 public void setTextVisible(boolean isVisible) {
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800321 CharSequence newText = isVisible ? mText : "";
322 if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800323 mTextVisible = isVisible;
Jon Mirandadf1ffb92018-02-06 10:31:38 -0800324 setText(newText);
Alex Chau906d8822022-05-23 10:42:25 +0100325 updateIconVisibility();
Pat Manning27bfcaa2022-04-25 13:50:28 +0100326 }
Pat Manning27bfcaa2022-04-25 13:50:28 +0100327 }
328
Alex Chau906d8822022-05-23 10:42:25 +0100329 /**
330 * Display button text over multiple lines when isMultiLine is true, single line otherwise.
331 */
332 public void setTextMultiLine(boolean isMultiLine) {
333 if (mTextMultiLine != isMultiLine) {
334 mTextMultiLine = isMultiLine;
335 setSingleLine(!isMultiLine);
336 setMaxLines(isMultiLine ? MAX_LINES_TEXT_MULTI_LINE : MAX_LINES_TEXT_SINGLE_LINE);
337 int inputType = InputType.TYPE_CLASS_TEXT;
338 if (isMultiLine) {
339 inputType |= InputType.TYPE_TEXT_FLAG_MULTI_LINE;
340
341 }
342 setInputType(inputType);
343 }
344 }
345
346 protected boolean isTextMultiLine() {
347 return mTextMultiLine;
348 }
349
350 /**
351 * Sets the button icon visible when isVisible is true, hides it otherwise.
352 */
353 public void setIconVisible(boolean isVisible) {
354 if (mIconVisible != isVisible) {
355 mIconVisible = isVisible;
356 updateIconVisibility();
357 }
358 }
359
360 private void updateIconVisibility() {
361 if (mIconVisible) {
362 centerIcon();
363 }
364 setCompoundDrawablesRelative(mIconVisible ? mDrawable : null, null, null, null);
365 setCompoundDrawablePadding(mIconVisible && mTextVisible ? mDrawablePadding : 0);
366 }
367
Sebastian Franco63d1a182021-10-18 09:52:04 -0500368 @Override
369 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
370 super.onSizeChanged(w, h, oldw, oldh);
371 centerIcon();
372 }
373
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800374 public void setToolTipLocation(int location) {
375 mToolTipLocation = location;
376 hideTooltip();
377 }
378
Pat Manninge63dd252022-08-02 16:15:29 +0100379 /**
380 * Returns if the text will be truncated within the provided availableWidth.
381 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800382 public boolean isTextTruncated(int availableWidth) {
Pat Manninge63dd252022-08-02 16:15:29 +0100383 availableWidth -= getPaddingLeft() + getPaddingRight();
384 if (mIconVisible) {
385 availableWidth -= mDrawable.getIntrinsicWidth() + getCompoundDrawablePadding();
386 }
387 if (availableWidth <= 0) {
388 return true;
389 }
390 CharSequence firstLine = TextUtils.ellipsize(mText, getPaint(), availableWidth,
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700391 TextUtils.TruncateAt.END);
Pat Manninge63dd252022-08-02 16:15:29 +0100392 if (!mTextMultiLine) {
393 return !TextUtils.equals(mText, firstLine);
394 }
395 if (TextUtils.equals(mText, firstLine)) {
396 // When multi-line is active, if it can display as one line, then text is not truncated.
397 return false;
398 }
399 CharSequence secondLine =
400 TextUtils.ellipsize(mText.subSequence(firstLine.length(), mText.length()),
401 getPaint(), availableWidth, TextUtils.TruncateAt.END);
402 return !(TextUtils.equals(mText.subSequence(0, firstLine.length()), firstLine)
403 && TextUtils.equals(mText.subSequence(firstLine.length(), secondLine.length()),
404 secondLine));
405 }
406
407 /**
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000408 * Returns if the text will be clipped vertically within the provided availableHeight.
409 */
410 private boolean isTextClippedVertically(int availableHeight) {
411 availableHeight -= getPaddingTop() + getPaddingBottom();
412 if (availableHeight <= 0) {
413 return true;
414 }
415
416 getPaint().getTextBounds(mText.toString(), 0, mText.length(), mTempRect);
417 // Add bounds bottom to height, as text bounds height measures from the text baseline and
418 // above, which characters can descend below
419 return mTempRect.bottom + mTempRect.height() <= availableHeight;
420 }
421
422 /**
Pat Manninge63dd252022-08-02 16:15:29 +0100423 * Reduce the size of the text until it fits the measured width or reaches a minimum.
424 *
425 * The minimum size is defined by {@code R.dimen.button_drop_target_min_text_size} and
426 * it diminishes by intervals defined by
427 * {@code R.dimen.button_drop_target_resize_text_increment}
428 * This functionality is very similar to the option
429 * {@link TextView#setAutoSizeTextTypeWithDefaults(int)} but can't be used in this view because
430 * the layout width is {@code WRAP_CONTENT}.
431 *
432 * @return The biggest text size in SP that makes the text fit or if the text can't fit returns
433 * the min available value
434 */
435 public float resizeTextToFit() {
436 float minSize = Utilities.pxToSp(getResources()
437 .getDimensionPixelSize(R.dimen.button_drop_target_min_text_size));
438 float step = Utilities.pxToSp(getResources()
439 .getDimensionPixelSize(R.dimen.button_drop_target_resize_text_increment));
440 float textSize = Utilities.pxToSp(getTextSize());
441
442 int availableWidth = getMeasuredWidth();
Helen Cheuk1a55cde2023-03-02 16:45:41 +0000443 int availableHeight = getMeasuredHeight();
444
445 while (isTextTruncated(availableWidth) || isTextClippedVertically(availableHeight)) {
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800446 textSize -= step;
447 if (textSize < minSize) {
448 textSize = minSize;
Pat Manninge63dd252022-08-02 16:15:29 +0100449 setTextSize(textSize);
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800450 break;
Pat Manninge63dd252022-08-02 16:15:29 +0100451 }
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800452 setTextSize(textSize);
Pat Manninge63dd252022-08-02 16:15:29 +0100453 }
Sebastian Franco5bf936a2022-12-01 11:51:12 -0800454 return textSize;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700455 }
Winson Chung61fa4192011-06-12 15:15:29 -0700456}