blob: 9fb14f68cb22810d4afba0db0607a29f256c7984 [file] [log] [blame]
Sunny Goyal47328fd2016-05-25 18:56:41 -07001/*
2 * Copyright (C) 2011 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
17package com.android.launcher3;
18
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080019import static com.android.launcher3.ButtonDropTarget.TOOLTIP_DEFAULT;
20import static com.android.launcher3.ButtonDropTarget.TOOLTIP_LEFT;
21import static com.android.launcher3.ButtonDropTarget.TOOLTIP_RIGHT;
Sunny Goyal7185dd62018-03-14 17:51:49 -070022import static com.android.launcher3.anim.AlphaUpdateListener.updateVisibility;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070023
Sunny Goyal47328fd2016-05-25 18:56:41 -070024import android.animation.TimeInterpolator;
25import android.content.Context;
Sunny Goyal07b69292018-01-08 14:19:34 -080026import android.graphics.Rect;
Sunny Goyal47328fd2016-05-25 18:56:41 -070027import android.util.AttributeSet;
vadimt89d94232021-09-01 12:33:00 -070028import android.util.Log;
Alex Chaua02eddc2021-04-29 00:36:06 +010029import android.util.TypedValue;
Sunny Goyal07b69292018-01-08 14:19:34 -080030import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070031import android.view.View;
32import android.view.ViewDebug;
33import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080034import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070035
vadimt89d94232021-09-01 12:33:00 -070036import androidx.annotation.NonNull;
37
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070038import com.android.launcher3.anim.Interpolators;
Sunny Goyal47328fd2016-05-25 18:56:41 -070039import com.android.launcher3.dragndrop.DragController;
Sunny Goyal07b69292018-01-08 14:19:34 -080040import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal94b510c2016-08-16 15:36:48 -070041import com.android.launcher3.dragndrop.DragOptions;
vadimt89d94232021-09-01 12:33:00 -070042import com.android.launcher3.testing.TestProtocol;
Sunny Goyal47328fd2016-05-25 18:56:41 -070043
44/*
45 * The top bar containing various drop targets: Delete/App Info/Uninstall.
46 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080047public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080048 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070049
50 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070051 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070052
Sunny Goyal07b69292018-01-08 14:19:34 -080053 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070054 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070055
56 @ViewDebug.ExportedProperty(category = "launcher")
57 protected boolean mDeferOnDragEnd;
58
59 @ViewDebug.ExportedProperty(category = "launcher")
60 protected boolean mVisible = false;
61
Sunny Goyal0236d0b2017-10-24 14:54:30 -070062 private ButtonDropTarget[] mDropTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070063 private ViewPropertyAnimator mCurrentAnimation;
64
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080065 private boolean mIsVertical = true;
66
Sunny Goyal47328fd2016-05-25 18:56:41 -070067 public DropTargetBar(Context context, AttributeSet attrs) {
68 super(context, attrs);
69 }
70
71 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
72 super(context, attrs, defStyle);
73 }
74
75 @Override
76 protected void onFinishInflate() {
77 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080078 mDropTargets = new ButtonDropTarget[getChildCount()];
79 for (int i = 0; i < mDropTargets.length; i++) {
80 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
81 mDropTargets[i].setDropTargetBar(this);
82 }
Sunny Goyal47328fd2016-05-25 18:56:41 -070083 }
84
Sunny Goyal07b69292018-01-08 14:19:34 -080085 @Override
86 public void setInsets(Rect insets) {
87 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
88 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080089 mIsVertical = grid.isVerticalBarLayout();
Sunny Goyal07b69292018-01-08 14:19:34 -080090
91 lp.leftMargin = insets.left;
92 lp.topMargin = insets.top;
93 lp.bottomMargin = insets.bottom;
94 lp.rightMargin = insets.right;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080095 int tooltipLocation = TOOLTIP_DEFAULT;
Sunny Goyal07b69292018-01-08 14:19:34 -080096
97 if (grid.isVerticalBarLayout()) {
98 lp.width = grid.dropTargetBarSizePx;
99 lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800100 lp.gravity = grid.isSeascape() ? Gravity.RIGHT : Gravity.LEFT;
101 tooltipLocation = grid.isSeascape() ? TOOLTIP_LEFT : TOOLTIP_RIGHT;
Sunny Goyal07b69292018-01-08 14:19:34 -0800102 } else {
103 int gap;
104 if (grid.isTablet) {
105 // XXX: If the icon size changes across orientations, we will have to take
106 // that into account here too.
107 gap = ((grid.widthPx - 2 * grid.edgeMarginPx
108 - (grid.inv.numColumns * grid.cellWidthPx))
109 / (2 * (grid.inv.numColumns + 1)))
110 + grid.edgeMarginPx;
111 } else {
Alina Zaidi1b5efa32021-06-30 16:54:14 +0100112 gap = getContext().getResources()
113 .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
Sunny Goyal07b69292018-01-08 14:19:34 -0800114 }
115 lp.width = grid.availableWidthPx - 2 * gap;
116
117 lp.topMargin += grid.edgeMarginPx;
118 lp.height = grid.dropTargetBarSizePx;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800119 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
Sunny Goyal07b69292018-01-08 14:19:34 -0800120 }
121 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800122 for (ButtonDropTarget button : mDropTargets) {
Alex Chaua02eddc2021-04-29 00:36:06 +0100123 button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800124 button.setToolTipLocation(tooltipLocation);
125 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800126 }
127
Sunny Goyal47328fd2016-05-25 18:56:41 -0700128 public void setup(DragController dragController) {
129 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700130 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 dragController.addDragListener(mDropTargets[i]);
132 dragController.addDropTarget(mDropTargets[i]);
133 }
134 }
135
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700136 @Override
137 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800138 int width = MeasureSpec.getSize(widthMeasureSpec);
139 int height = MeasureSpec.getSize(heightMeasureSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700140
Sunny Goyala4647b62021-02-02 13:45:34 -0800141 int visibleCount = getVisibleButtonsCount();
142 if (visibleCount == 0) {
143 // do nothing
144 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800145 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
146 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700147
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800148 for (ButtonDropTarget button : mDropTargets) {
149 if (button.getVisibility() != GONE) {
150 button.setTextVisible(false);
151 button.measure(widthSpec, heightSpec);
152 }
153 }
154 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800155 int availableWidth = width / visibleCount;
156 boolean textVisible = true;
157 for (ButtonDropTarget buttons : mDropTargets) {
158 if (buttons.getVisibility() != GONE) {
159 textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
160 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700161 }
162
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800163 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
164 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
165 for (ButtonDropTarget button : mDropTargets) {
166 if (button.getVisibility() != GONE) {
167 button.setTextVisible(textVisible);
168 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700169 }
170 }
171 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800172 setMeasuredDimension(width, height);
173 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700174
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800175 @Override
176 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800177 int visibleCount = getVisibleButtonsCount();
178 if (visibleCount == 0) {
179 // do nothing
180 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800181 int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap);
182 int start = gap;
183 int end;
184
185 for (ButtonDropTarget button : mDropTargets) {
186 if (button.getVisibility() != GONE) {
187 end = start + button.getMeasuredHeight();
188 button.layout(0, start, button.getMeasuredWidth(), end);
189 start = end + gap;
190 }
191 }
192 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800193 int frameSize = (right - left) / visibleCount;
194
195 int start = frameSize / 2;
196 int halfWidth;
197 for (ButtonDropTarget button : mDropTargets) {
198 if (button.getVisibility() != GONE) {
199 halfWidth = button.getMeasuredWidth() / 2;
200 button.layout(start - halfWidth, 0,
201 start + halfWidth, button.getMeasuredHeight());
202 start = start + frameSize;
203 }
204 }
205 }
206 }
207
208 private int getVisibleButtonsCount() {
209 int visibleCount = 0;
210 for (ButtonDropTarget buttons : mDropTargets) {
211 if (buttons.getVisibility() != GONE) {
212 visibleCount++;
213 }
214 }
215 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700216 }
217
Hyunyoung Song497708c2019-05-01 16:16:53 -0700218 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700219 if (TestProtocol.sDebugTracing) {
220 Log.d(TestProtocol.NO_DROP_TARGET, "8");
221 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700222 if (mVisible != isVisible) {
223 mVisible = isVisible;
224
225 // Cancel any existing animation
226 if (mCurrentAnimation != null) {
227 mCurrentAnimation.cancel();
228 mCurrentAnimation = null;
229 }
230
231 float finalAlpha = mVisible ? 1 : 0;
232 if (Float.compare(getAlpha(), finalAlpha) != 0) {
233 setVisibility(View.VISIBLE);
234 mCurrentAnimation = animate().alpha(finalAlpha)
235 .setInterpolator(DEFAULT_INTERPOLATOR)
236 .setDuration(DEFAULT_DRAG_FADE_DURATION)
237 .withEndAction(mFadeAnimationEndRunnable);
238 }
239
240 }
241 }
242
Sunny Goyal47328fd2016-05-25 18:56:41 -0700243 /*
244 * DragController.DragListener implementation
245 */
246 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700247 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700248 if (TestProtocol.sDebugTracing) {
249 Log.d(TestProtocol.NO_DROP_TARGET, "7");
250 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700251 animateToVisibility(true);
252 }
253
254 /**
255 * This is called to defer hiding the delete drop target until the drop animation has completed,
256 * instead of hiding immediately when the drag has ended.
257 */
258 protected void deferOnDragEnd() {
259 mDeferOnDragEnd = true;
260 }
261
262 @Override
263 public void onDragEnd() {
264 if (!mDeferOnDragEnd) {
265 animateToVisibility(false);
266 } else {
267 mDeferOnDragEnd = false;
268 }
269 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700270
271 public ButtonDropTarget[] getDropTargets() {
272 return mDropTargets;
273 }
vadimt89d94232021-09-01 12:33:00 -0700274
275 @Override
276 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
277 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000278 if (TestProtocol.sDebugTracing) {
279 if (visibility == VISIBLE) {
280 Log.d(TestProtocol.NO_DROP_TARGET, "9");
281 } else {
282 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
283 }
vadimt89d94232021-09-01 12:33:00 -0700284 }
285 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700286}