blob: 88f6c498aa00d60db74e6f1881f185e6e2d14a79 [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;
Alex Chaua02eddc2021-04-29 00:36:06 +010028import android.util.TypedValue;
Sunny Goyal07b69292018-01-08 14:19:34 -080029import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070030import android.view.View;
31import android.view.ViewDebug;
32import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080033import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070034
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070035import com.android.launcher3.anim.Interpolators;
Sunny Goyal47328fd2016-05-25 18:56:41 -070036import com.android.launcher3.dragndrop.DragController;
Sunny Goyal07b69292018-01-08 14:19:34 -080037import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal94b510c2016-08-16 15:36:48 -070038import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal47328fd2016-05-25 18:56:41 -070039
40/*
41 * The top bar containing various drop targets: Delete/App Info/Uninstall.
42 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080043public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080044 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070045
46 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070047 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070048
Sunny Goyal07b69292018-01-08 14:19:34 -080049 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070050 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070051
52 @ViewDebug.ExportedProperty(category = "launcher")
53 protected boolean mDeferOnDragEnd;
54
55 @ViewDebug.ExportedProperty(category = "launcher")
56 protected boolean mVisible = false;
57
Sunny Goyal0236d0b2017-10-24 14:54:30 -070058 private ButtonDropTarget[] mDropTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070059 private ViewPropertyAnimator mCurrentAnimation;
60
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080061 private boolean mIsVertical = true;
62
Sunny Goyal47328fd2016-05-25 18:56:41 -070063 public DropTargetBar(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 }
66
67 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080074 mDropTargets = new ButtonDropTarget[getChildCount()];
75 for (int i = 0; i < mDropTargets.length; i++) {
76 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
77 mDropTargets[i].setDropTargetBar(this);
78 }
Sunny Goyal47328fd2016-05-25 18:56:41 -070079 }
80
Sunny Goyal07b69292018-01-08 14:19:34 -080081 @Override
82 public void setInsets(Rect insets) {
83 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
84 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080085 mIsVertical = grid.isVerticalBarLayout();
Sunny Goyal07b69292018-01-08 14:19:34 -080086
87 lp.leftMargin = insets.left;
88 lp.topMargin = insets.top;
89 lp.bottomMargin = insets.bottom;
90 lp.rightMargin = insets.right;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080091 int tooltipLocation = TOOLTIP_DEFAULT;
Sunny Goyal07b69292018-01-08 14:19:34 -080092
93 if (grid.isVerticalBarLayout()) {
94 lp.width = grid.dropTargetBarSizePx;
95 lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -080096 lp.gravity = grid.isSeascape() ? Gravity.RIGHT : Gravity.LEFT;
97 tooltipLocation = grid.isSeascape() ? TOOLTIP_LEFT : TOOLTIP_RIGHT;
Sunny Goyal07b69292018-01-08 14:19:34 -080098 } else {
99 int gap;
100 if (grid.isTablet) {
101 // XXX: If the icon size changes across orientations, we will have to take
102 // that into account here too.
103 gap = ((grid.widthPx - 2 * grid.edgeMarginPx
104 - (grid.inv.numColumns * grid.cellWidthPx))
105 / (2 * (grid.inv.numColumns + 1)))
106 + grid.edgeMarginPx;
107 } else {
Alina Zaidi1b5efa32021-06-30 16:54:14 +0100108 gap = getContext().getResources()
109 .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
Sunny Goyal07b69292018-01-08 14:19:34 -0800110 }
111 lp.width = grid.availableWidthPx - 2 * gap;
112
113 lp.topMargin += grid.edgeMarginPx;
114 lp.height = grid.dropTargetBarSizePx;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800115 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
Sunny Goyal07b69292018-01-08 14:19:34 -0800116 }
117 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800118 for (ButtonDropTarget button : mDropTargets) {
Alex Chaua02eddc2021-04-29 00:36:06 +0100119 button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800120 button.setToolTipLocation(tooltipLocation);
121 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800122 }
123
Sunny Goyal47328fd2016-05-25 18:56:41 -0700124 public void setup(DragController dragController) {
125 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700126 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700127 dragController.addDragListener(mDropTargets[i]);
128 dragController.addDropTarget(mDropTargets[i]);
129 }
130 }
131
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700132 @Override
133 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800134 int width = MeasureSpec.getSize(widthMeasureSpec);
135 int height = MeasureSpec.getSize(heightMeasureSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700136
Sunny Goyala4647b62021-02-02 13:45:34 -0800137 int visibleCount = getVisibleButtonsCount();
138 if (visibleCount == 0) {
139 // do nothing
140 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800141 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
142 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700143
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800144 for (ButtonDropTarget button : mDropTargets) {
145 if (button.getVisibility() != GONE) {
146 button.setTextVisible(false);
147 button.measure(widthSpec, heightSpec);
148 }
149 }
150 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800151 int availableWidth = width / visibleCount;
152 boolean textVisible = true;
153 for (ButtonDropTarget buttons : mDropTargets) {
154 if (buttons.getVisibility() != GONE) {
155 textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
156 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700157 }
158
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800159 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
160 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
161 for (ButtonDropTarget button : mDropTargets) {
162 if (button.getVisibility() != GONE) {
163 button.setTextVisible(textVisible);
164 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700165 }
166 }
167 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800168 setMeasuredDimension(width, height);
169 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700170
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800171 @Override
172 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800173 int visibleCount = getVisibleButtonsCount();
174 if (visibleCount == 0) {
175 // do nothing
176 } else if (mIsVertical) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800177 int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap);
178 int start = gap;
179 int end;
180
181 for (ButtonDropTarget button : mDropTargets) {
182 if (button.getVisibility() != GONE) {
183 end = start + button.getMeasuredHeight();
184 button.layout(0, start, button.getMeasuredWidth(), end);
185 start = end + gap;
186 }
187 }
188 } else {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800189 int frameSize = (right - left) / visibleCount;
190
191 int start = frameSize / 2;
192 int halfWidth;
193 for (ButtonDropTarget button : mDropTargets) {
194 if (button.getVisibility() != GONE) {
195 halfWidth = button.getMeasuredWidth() / 2;
196 button.layout(start - halfWidth, 0,
197 start + halfWidth, button.getMeasuredHeight());
198 start = start + frameSize;
199 }
200 }
201 }
202 }
203
204 private int getVisibleButtonsCount() {
205 int visibleCount = 0;
206 for (ButtonDropTarget buttons : mDropTargets) {
207 if (buttons.getVisibility() != GONE) {
208 visibleCount++;
209 }
210 }
211 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700212 }
213
Hyunyoung Song497708c2019-05-01 16:16:53 -0700214 public void animateToVisibility(boolean isVisible) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700215 if (mVisible != isVisible) {
216 mVisible = isVisible;
217
218 // Cancel any existing animation
219 if (mCurrentAnimation != null) {
220 mCurrentAnimation.cancel();
221 mCurrentAnimation = null;
222 }
223
224 float finalAlpha = mVisible ? 1 : 0;
225 if (Float.compare(getAlpha(), finalAlpha) != 0) {
226 setVisibility(View.VISIBLE);
227 mCurrentAnimation = animate().alpha(finalAlpha)
228 .setInterpolator(DEFAULT_INTERPOLATOR)
229 .setDuration(DEFAULT_DRAG_FADE_DURATION)
230 .withEndAction(mFadeAnimationEndRunnable);
231 }
232
233 }
234 }
235
Sunny Goyal47328fd2016-05-25 18:56:41 -0700236 /*
237 * DragController.DragListener implementation
238 */
239 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700240 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700241 animateToVisibility(true);
242 }
243
244 /**
245 * This is called to defer hiding the delete drop target until the drop animation has completed,
246 * instead of hiding immediately when the drag has ended.
247 */
248 protected void deferOnDragEnd() {
249 mDeferOnDragEnd = true;
250 }
251
252 @Override
253 public void onDragEnd() {
254 if (!mDeferOnDragEnd) {
255 animateToVisibility(false);
256 } else {
257 mDeferOnDragEnd = false;
258 }
259 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700260
261 public ButtonDropTarget[] getDropTargets() {
262 return mDropTargets;
263 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700264}