blob: 494aae45a81d56972a073a77ec7dac38340ebc84 [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 Goyal0236d0b2017-10-24 14:54:30 -070019import static com.android.launcher3.AlphaUpdateListener.updateVisibility;
Sunny Goyald0030b02017-12-08 15:07:24 -080020import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070021
Sunny Goyal47328fd2016-05-25 18:56:41 -070022import android.animation.TimeInterpolator;
23import android.content.Context;
Sunny Goyal07b69292018-01-08 14:19:34 -080024import android.graphics.Rect;
Sunny Goyal47328fd2016-05-25 18:56:41 -070025import android.util.AttributeSet;
Sunny Goyal07b69292018-01-08 14:19:34 -080026import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070027import android.view.View;
28import android.view.ViewDebug;
Sunny Goyal0f76b562016-12-13 19:37:10 -080029import android.view.ViewGroup;
Sunny Goyal47328fd2016-05-25 18:56:41 -070030import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080031import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070032import android.widget.LinearLayout;
33
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
Sunny Goyal0236d0b2017-10-24 14:54:30 -070039import java.util.ArrayList;
40
Sunny Goyal47328fd2016-05-25 18:56:41 -070041/*
42 * The top bar containing various drop targets: Delete/App Info/Uninstall.
43 */
Sunny Goyal07b69292018-01-08 14:19:34 -080044public class DropTargetBar extends LinearLayout
45 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070046
47 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070048 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070049
Sunny Goyal07b69292018-01-08 14:19:34 -080050 private final Runnable mFadeAnimationEndRunnable =
51 () -> updateVisibility(DropTargetBar.this, isAccessibilityEnabled(getContext()));
Sunny Goyal47328fd2016-05-25 18:56:41 -070052
53 @ViewDebug.ExportedProperty(category = "launcher")
54 protected boolean mDeferOnDragEnd;
55
56 @ViewDebug.ExportedProperty(category = "launcher")
57 protected boolean mVisible = false;
58
Sunny Goyal0236d0b2017-10-24 14:54:30 -070059 private ButtonDropTarget[] mDropTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070060 private ViewPropertyAnimator mCurrentAnimation;
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();
73
Sunny Goyal47328fd2016-05-25 18:56:41 -070074 // Initialize with hidden state
75 setAlpha(0f);
76 }
77
Sunny Goyal07b69292018-01-08 14:19:34 -080078 @Override
79 public void setInsets(Rect insets) {
80 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
81 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
82
83 lp.leftMargin = insets.left;
84 lp.topMargin = insets.top;
85 lp.bottomMargin = insets.bottom;
86 lp.rightMargin = insets.right;
87
88 if (grid.isVerticalBarLayout()) {
89 lp.width = grid.dropTargetBarSizePx;
90 lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx;
91 lp.gravity = insets.left > insets.right ? Gravity.RIGHT : Gravity.LEFT;
92 } else {
93 int gap;
94 if (grid.isTablet) {
95 // XXX: If the icon size changes across orientations, we will have to take
96 // that into account here too.
97 gap = ((grid.widthPx - 2 * grid.edgeMarginPx
98 - (grid.inv.numColumns * grid.cellWidthPx))
99 / (2 * (grid.inv.numColumns + 1)))
100 + grid.edgeMarginPx;
101 } else {
102 gap = grid.desiredWorkspaceLeftRightMarginPx - grid.defaultWidgetPadding.right;
103 }
104 lp.width = grid.availableWidthPx - 2 * gap;
105
106 lp.topMargin += grid.edgeMarginPx;
107 lp.height = grid.dropTargetBarSizePx;
108 }
109 setLayoutParams(lp);
110 }
111
Sunny Goyal47328fd2016-05-25 18:56:41 -0700112 public void setup(DragController dragController) {
113 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700114 ArrayList<ButtonDropTarget> outList = new ArrayList<>();
115 findDropTargets(this, outList);
116
117 mDropTargets = new ButtonDropTarget[outList.size()];
118 for (int i = 0; i < mDropTargets.length; i++) {
119 mDropTargets[i] = outList.get(i);
120 mDropTargets[i].setDropTargetBar(this);
121 dragController.addDragListener(mDropTargets[i]);
122 dragController.addDropTarget(mDropTargets[i]);
123 }
124 }
125
126 private static void findDropTargets(View view, ArrayList<ButtonDropTarget> outTargets) {
127 if (view instanceof ButtonDropTarget) {
128 outTargets.add((ButtonDropTarget) view);
129 } else if (view instanceof ViewGroup) {
130 ViewGroup vg = (ViewGroup) view;
131 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
132 findDropTargets(vg.getChildAt(i), outTargets);
133 }
134 }
Sunny Goyal0f76b562016-12-13 19:37:10 -0800135 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700136
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700137 @Override
138 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
139 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
140
141 boolean hideText = hideTextHelper(false /* shouldUpdateText */, false /* no-op */);
142 if (hideTextHelper(true /* shouldUpdateText */, hideText)) {
143 // Text has changed, so we need to re-measure.
144 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
145 }
146 }
147
148 /**
149 * Helper method that iterates through the children and returns whether any of the visible
150 * {@link ButtonDropTarget} has truncated text.
151 *
152 * @param shouldUpdateText If True, updates the text of all children.
153 * @param hideText If True and {@param shouldUpdateText} is True, clears the text of all
154 * children; otherwise it sets the original text value.
155 *
156 *
157 * @return If shouldUpdateText is True, returns whether any of the children updated their text.
158 * Else, returns whether any of the children have truncated their text.
159 */
160 private boolean hideTextHelper(boolean shouldUpdateText, boolean hideText) {
161 boolean result = false;
162 View visibleView;
163 ButtonDropTarget dropTarget;
164 for (int i = getChildCount() - 1; i >= 0; --i) {
165 if (getChildAt(i) instanceof ButtonDropTarget) {
166 visibleView = dropTarget = (ButtonDropTarget) getChildAt(i);
167 } else if (getChildAt(i) instanceof ViewGroup) {
168 // The Drop Target is wrapped in a FrameLayout.
169 visibleView = getChildAt(i);
170 dropTarget = (ButtonDropTarget) ((ViewGroup) visibleView).getChildAt(0);
171 } else {
172 // Ignore other views.
173 continue;
174 }
175
176 if (visibleView.getVisibility() == View.VISIBLE) {
177 if (shouldUpdateText) {
178 result |= dropTarget.updateText(hideText);
179 } else if (dropTarget.isTextTruncated()) {
180 result = true;
181 break;
182 }
183 }
184 }
185
186 return result;
187 }
188
Sunny Goyal47328fd2016-05-25 18:56:41 -0700189 private void animateToVisibility(boolean isVisible) {
190 if (mVisible != isVisible) {
191 mVisible = isVisible;
192
193 // Cancel any existing animation
194 if (mCurrentAnimation != null) {
195 mCurrentAnimation.cancel();
196 mCurrentAnimation = null;
197 }
198
199 float finalAlpha = mVisible ? 1 : 0;
200 if (Float.compare(getAlpha(), finalAlpha) != 0) {
201 setVisibility(View.VISIBLE);
202 mCurrentAnimation = animate().alpha(finalAlpha)
203 .setInterpolator(DEFAULT_INTERPOLATOR)
204 .setDuration(DEFAULT_DRAG_FADE_DURATION)
205 .withEndAction(mFadeAnimationEndRunnable);
206 }
207
208 }
209 }
210
Sunny Goyal47328fd2016-05-25 18:56:41 -0700211 /*
212 * DragController.DragListener implementation
213 */
214 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700215 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700216 animateToVisibility(true);
217 }
218
219 /**
220 * This is called to defer hiding the delete drop target until the drop animation has completed,
221 * instead of hiding immediately when the drag has ended.
222 */
223 protected void deferOnDragEnd() {
224 mDeferOnDragEnd = true;
225 }
226
227 @Override
228 public void onDragEnd() {
229 if (!mDeferOnDragEnd) {
230 animateToVisibility(false);
231 } else {
232 mDeferOnDragEnd = false;
233 }
234 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700235
236 public ButtonDropTarget[] getDropTargets() {
237 return mDropTargets;
238 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700239}