blob: c7684939db36547634620aa25c426bb888685387 [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;
Sunny Goyal07b69292018-01-08 14:19:34 -080028import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070029import android.view.View;
30import android.view.ViewDebug;
31import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080032import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070033
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070034import com.android.launcher3.anim.Interpolators;
Sunny Goyal47328fd2016-05-25 18:56:41 -070035import com.android.launcher3.dragndrop.DragController;
Sunny Goyal07b69292018-01-08 14:19:34 -080036import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal94b510c2016-08-16 15:36:48 -070037import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal47328fd2016-05-25 18:56:41 -070038
39/*
40 * The top bar containing various drop targets: Delete/App Info/Uninstall.
41 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080042public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080043 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070044
45 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070046 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070047
Sunny Goyal07b69292018-01-08 14:19:34 -080048 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070049 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070050
51 @ViewDebug.ExportedProperty(category = "launcher")
52 protected boolean mDeferOnDragEnd;
53
54 @ViewDebug.ExportedProperty(category = "launcher")
55 protected boolean mVisible = false;
56
Sunny Goyal0236d0b2017-10-24 14:54:30 -070057 private ButtonDropTarget[] mDropTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070058 private ViewPropertyAnimator mCurrentAnimation;
59
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080060 private boolean mIsVertical = true;
61
Sunny Goyal47328fd2016-05-25 18:56:41 -070062 public DropTargetBar(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 }
65
66 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68 }
69
70 @Override
71 protected void onFinishInflate() {
72 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080073 mDropTargets = new ButtonDropTarget[getChildCount()];
74 for (int i = 0; i < mDropTargets.length; i++) {
75 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
76 mDropTargets[i].setDropTargetBar(this);
77 }
Sunny Goyal47328fd2016-05-25 18:56:41 -070078 }
79
Sunny Goyal07b69292018-01-08 14:19:34 -080080 @Override
81 public void setInsets(Rect insets) {
82 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
83 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080084 mIsVertical = grid.isVerticalBarLayout();
Sunny Goyal07b69292018-01-08 14:19:34 -080085
86 lp.leftMargin = insets.left;
87 lp.topMargin = insets.top;
88 lp.bottomMargin = insets.bottom;
89 lp.rightMargin = insets.right;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080090 int tooltipLocation = TOOLTIP_DEFAULT;
Sunny Goyal07b69292018-01-08 14:19:34 -080091
92 if (grid.isVerticalBarLayout()) {
93 lp.width = grid.dropTargetBarSizePx;
94 lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -080095 lp.gravity = grid.isSeascape() ? Gravity.RIGHT : Gravity.LEFT;
96 tooltipLocation = grid.isSeascape() ? TOOLTIP_LEFT : TOOLTIP_RIGHT;
Sunny Goyal07b69292018-01-08 14:19:34 -080097 } else {
98 int gap;
99 if (grid.isTablet) {
100 // XXX: If the icon size changes across orientations, we will have to take
101 // that into account here too.
102 gap = ((grid.widthPx - 2 * grid.edgeMarginPx
103 - (grid.inv.numColumns * grid.cellWidthPx))
104 / (2 * (grid.inv.numColumns + 1)))
105 + grid.edgeMarginPx;
106 } else {
Sunny Goyal58fa4b62019-03-22 16:23:25 -0700107 gap = grid.desiredWorkspaceLeftRightMarginPx - grid.inv.defaultWidgetPadding.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800108 }
109 lp.width = grid.availableWidthPx - 2 * gap;
110
111 lp.topMargin += grid.edgeMarginPx;
112 lp.height = grid.dropTargetBarSizePx;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800113 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
Sunny Goyal07b69292018-01-08 14:19:34 -0800114 }
115 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800116 for (ButtonDropTarget button : mDropTargets) {
117 button.setToolTipLocation(tooltipLocation);
118 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800119 }
120
Sunny Goyal47328fd2016-05-25 18:56:41 -0700121 public void setup(DragController dragController) {
122 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700123 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 dragController.addDragListener(mDropTargets[i]);
125 dragController.addDropTarget(mDropTargets[i]);
126 }
127 }
128
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700129 @Override
130 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800131 int width = MeasureSpec.getSize(widthMeasureSpec);
132 int height = MeasureSpec.getSize(heightMeasureSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700133
Sunny Goyala4647b62021-02-02 13:45:34 -0800134 int visibleCount = getVisibleButtonsCount();
135 if (visibleCount == 0) {
136 // do nothing
137 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800138 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
139 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700140
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800141 for (ButtonDropTarget button : mDropTargets) {
142 if (button.getVisibility() != GONE) {
143 button.setTextVisible(false);
144 button.measure(widthSpec, heightSpec);
145 }
146 }
147 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800148 int availableWidth = width / visibleCount;
149 boolean textVisible = true;
150 for (ButtonDropTarget buttons : mDropTargets) {
151 if (buttons.getVisibility() != GONE) {
152 textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
153 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700154 }
155
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800156 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
157 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
158 for (ButtonDropTarget button : mDropTargets) {
159 if (button.getVisibility() != GONE) {
160 button.setTextVisible(textVisible);
161 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700162 }
163 }
164 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800165 setMeasuredDimension(width, height);
166 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700167
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800168 @Override
169 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800170 int visibleCount = getVisibleButtonsCount();
171 if (visibleCount == 0) {
172 // do nothing
173 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800174 int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap);
175 int start = gap;
176 int end;
177
178 for (ButtonDropTarget button : mDropTargets) {
179 if (button.getVisibility() != GONE) {
180 end = start + button.getMeasuredHeight();
181 button.layout(0, start, button.getMeasuredWidth(), end);
182 start = end + gap;
183 }
184 }
185 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800186 int frameSize = (right - left) / visibleCount;
187
188 int start = frameSize / 2;
189 int halfWidth;
190 for (ButtonDropTarget button : mDropTargets) {
191 if (button.getVisibility() != GONE) {
192 halfWidth = button.getMeasuredWidth() / 2;
193 button.layout(start - halfWidth, 0,
194 start + halfWidth, button.getMeasuredHeight());
195 start = start + frameSize;
196 }
197 }
198 }
199 }
200
201 private int getVisibleButtonsCount() {
202 int visibleCount = 0;
203 for (ButtonDropTarget buttons : mDropTargets) {
204 if (buttons.getVisibility() != GONE) {
205 visibleCount++;
206 }
207 }
208 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700209 }
210
Hyunyoung Song497708c2019-05-01 16:16:53 -0700211 public void animateToVisibility(boolean isVisible) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700212 if (mVisible != isVisible) {
213 mVisible = isVisible;
214
215 // Cancel any existing animation
216 if (mCurrentAnimation != null) {
217 mCurrentAnimation.cancel();
218 mCurrentAnimation = null;
219 }
220
221 float finalAlpha = mVisible ? 1 : 0;
222 if (Float.compare(getAlpha(), finalAlpha) != 0) {
223 setVisibility(View.VISIBLE);
224 mCurrentAnimation = animate().alpha(finalAlpha)
225 .setInterpolator(DEFAULT_INTERPOLATOR)
226 .setDuration(DEFAULT_DRAG_FADE_DURATION)
227 .withEndAction(mFadeAnimationEndRunnable);
228 }
229
230 }
231 }
232
Sunny Goyal47328fd2016-05-25 18:56:41 -0700233 /*
234 * DragController.DragListener implementation
235 */
236 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700237 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700238 animateToVisibility(true);
239 }
240
241 /**
242 * This is called to defer hiding the delete drop target until the drop animation has completed,
243 * instead of hiding immediately when the drag has ended.
244 */
245 protected void deferOnDragEnd() {
246 mDeferOnDragEnd = true;
247 }
248
249 @Override
250 public void onDragEnd() {
251 if (!mDeferOnDragEnd) {
252 animateToVisibility(false);
253 } else {
254 mDeferOnDragEnd = false;
255 }
256 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700257
258 public ButtonDropTarget[] getDropTargets() {
259 return mDropTargets;
260 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700261}