blob: dec6cb452e160c7443bdbb00da7801a200629055 [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 Goyald0030b02017-12-08 15:07:24 -080023import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070024
Sunny Goyal47328fd2016-05-25 18:56:41 -070025import android.animation.TimeInterpolator;
26import android.content.Context;
Sunny Goyal07b69292018-01-08 14:19:34 -080027import android.graphics.Rect;
Sunny Goyal47328fd2016-05-25 18:56:41 -070028import android.util.AttributeSet;
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 =
50 () -> updateVisibility(DropTargetBar.this, isAccessibilityEnabled(getContext()));
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 {
108 gap = grid.desiredWorkspaceLeftRightMarginPx - grid.defaultWidgetPadding.right;
109 }
110 lp.width = grid.availableWidthPx - 2 * gap;
111
112 lp.topMargin += grid.edgeMarginPx;
113 lp.height = grid.dropTargetBarSizePx;
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800114 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
Sunny Goyal07b69292018-01-08 14:19:34 -0800115 }
116 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800117 for (ButtonDropTarget button : mDropTargets) {
118 button.setToolTipLocation(tooltipLocation);
119 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800120 }
121
Sunny Goyal47328fd2016-05-25 18:56:41 -0700122 public void setup(DragController dragController) {
123 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700125 dragController.addDragListener(mDropTargets[i]);
126 dragController.addDropTarget(mDropTargets[i]);
127 }
128 }
129
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700130 @Override
131 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800132 int width = MeasureSpec.getSize(widthMeasureSpec);
133 int height = MeasureSpec.getSize(heightMeasureSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700134
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800135 if (mIsVertical) {
136 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
137 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700138
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800139 for (ButtonDropTarget button : mDropTargets) {
140 if (button.getVisibility() != GONE) {
141 button.setTextVisible(false);
142 button.measure(widthSpec, heightSpec);
143 }
144 }
145 } else {
146 int visibleCount = getVisibleButtonsCount();
147 int availableWidth = width / visibleCount;
148 boolean textVisible = true;
149 for (ButtonDropTarget buttons : mDropTargets) {
150 if (buttons.getVisibility() != GONE) {
151 textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
152 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700153 }
154
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800155 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
156 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
157 for (ButtonDropTarget button : mDropTargets) {
158 if (button.getVisibility() != GONE) {
159 button.setTextVisible(textVisible);
160 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700161 }
162 }
163 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800164 setMeasuredDimension(width, height);
165 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700166
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800167 @Override
168 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
169 if (mIsVertical) {
170 int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap);
171 int start = gap;
172 int end;
173
174 for (ButtonDropTarget button : mDropTargets) {
175 if (button.getVisibility() != GONE) {
176 end = start + button.getMeasuredHeight();
177 button.layout(0, start, button.getMeasuredWidth(), end);
178 start = end + gap;
179 }
180 }
181 } else {
182 int visibleCount = getVisibleButtonsCount();
183 int frameSize = (right - left) / visibleCount;
184
185 int start = frameSize / 2;
186 int halfWidth;
187 for (ButtonDropTarget button : mDropTargets) {
188 if (button.getVisibility() != GONE) {
189 halfWidth = button.getMeasuredWidth() / 2;
190 button.layout(start - halfWidth, 0,
191 start + halfWidth, button.getMeasuredHeight());
192 start = start + frameSize;
193 }
194 }
195 }
196 }
197
198 private int getVisibleButtonsCount() {
199 int visibleCount = 0;
200 for (ButtonDropTarget buttons : mDropTargets) {
201 if (buttons.getVisibility() != GONE) {
202 visibleCount++;
203 }
204 }
205 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700206 }
207
Sunny Goyal47328fd2016-05-25 18:56:41 -0700208 private void animateToVisibility(boolean isVisible) {
209 if (mVisible != isVisible) {
210 mVisible = isVisible;
211
212 // Cancel any existing animation
213 if (mCurrentAnimation != null) {
214 mCurrentAnimation.cancel();
215 mCurrentAnimation = null;
216 }
217
218 float finalAlpha = mVisible ? 1 : 0;
219 if (Float.compare(getAlpha(), finalAlpha) != 0) {
220 setVisibility(View.VISIBLE);
221 mCurrentAnimation = animate().alpha(finalAlpha)
222 .setInterpolator(DEFAULT_INTERPOLATOR)
223 .setDuration(DEFAULT_DRAG_FADE_DURATION)
224 .withEndAction(mFadeAnimationEndRunnable);
225 }
226
227 }
228 }
229
Sunny Goyal47328fd2016-05-25 18:56:41 -0700230 /*
231 * DragController.DragListener implementation
232 */
233 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700234 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700235 animateToVisibility(true);
236 }
237
238 /**
239 * This is called to defer hiding the delete drop target until the drop animation has completed,
240 * instead of hiding immediately when the drag has ended.
241 */
242 protected void deferOnDragEnd() {
243 mDeferOnDragEnd = true;
244 }
245
246 @Override
247 public void onDragEnd() {
248 if (!mDeferOnDragEnd) {
249 animateToVisibility(false);
250 } else {
251 mDeferOnDragEnd = false;
252 }
253 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700254
255 public ButtonDropTarget[] getDropTargets() {
256 return mDropTargets;
257 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700258}