blob: 2f8374a5e5df5689592ed899d9c9d491c499e2f6 [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;
20import static com.android.launcher3.Utilities.isAccessibilityEnabled;
21
Sunny Goyal47328fd2016-05-25 18:56:41 -070022import android.animation.TimeInterpolator;
23import android.content.Context;
24import android.util.AttributeSet;
25import android.view.View;
26import android.view.ViewDebug;
Sunny Goyal0f76b562016-12-13 19:37:10 -080027import android.view.ViewGroup;
Sunny Goyal47328fd2016-05-25 18:56:41 -070028import android.view.ViewPropertyAnimator;
Sunny Goyal47328fd2016-05-25 18:56:41 -070029import android.view.animation.AccelerateInterpolator;
30import android.widget.LinearLayout;
31
32import com.android.launcher3.dragndrop.DragController;
Sunny Goyal94b510c2016-08-16 15:36:48 -070033import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal47328fd2016-05-25 18:56:41 -070034
Sunny Goyal0236d0b2017-10-24 14:54:30 -070035import java.util.ArrayList;
36
Sunny Goyal47328fd2016-05-25 18:56:41 -070037/*
38 * The top bar containing various drop targets: Delete/App Info/Uninstall.
39 */
40public class DropTargetBar extends LinearLayout implements DragController.DragListener {
41
42 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
43 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = new AccelerateInterpolator();
44
45 private final Runnable mFadeAnimationEndRunnable = new Runnable() {
46
47 @Override
48 public void run() {
Sunny Goyal0236d0b2017-10-24 14:54:30 -070049 updateVisibility(DropTargetBar.this, isAccessibilityEnabled(getContext()));
Sunny Goyal47328fd2016-05-25 18:56:41 -070050 }
51 };
52
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
78 public void setup(DragController dragController) {
79 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -070080 ArrayList<ButtonDropTarget> outList = new ArrayList<>();
81 findDropTargets(this, outList);
82
83 mDropTargets = new ButtonDropTarget[outList.size()];
84 for (int i = 0; i < mDropTargets.length; i++) {
85 mDropTargets[i] = outList.get(i);
86 mDropTargets[i].setDropTargetBar(this);
87 dragController.addDragListener(mDropTargets[i]);
88 dragController.addDropTarget(mDropTargets[i]);
89 }
90 }
91
92 private static void findDropTargets(View view, ArrayList<ButtonDropTarget> outTargets) {
93 if (view instanceof ButtonDropTarget) {
94 outTargets.add((ButtonDropTarget) view);
95 } else if (view instanceof ViewGroup) {
96 ViewGroup vg = (ViewGroup) view;
97 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
98 findDropTargets(vg.getChildAt(i), outTargets);
99 }
100 }
Sunny Goyal0f76b562016-12-13 19:37:10 -0800101 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700102
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700103 @Override
104 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
105 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
106
107 boolean hideText = hideTextHelper(false /* shouldUpdateText */, false /* no-op */);
108 if (hideTextHelper(true /* shouldUpdateText */, hideText)) {
109 // Text has changed, so we need to re-measure.
110 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
111 }
112 }
113
114 /**
115 * Helper method that iterates through the children and returns whether any of the visible
116 * {@link ButtonDropTarget} has truncated text.
117 *
118 * @param shouldUpdateText If True, updates the text of all children.
119 * @param hideText If True and {@param shouldUpdateText} is True, clears the text of all
120 * children; otherwise it sets the original text value.
121 *
122 *
123 * @return If shouldUpdateText is True, returns whether any of the children updated their text.
124 * Else, returns whether any of the children have truncated their text.
125 */
126 private boolean hideTextHelper(boolean shouldUpdateText, boolean hideText) {
127 boolean result = false;
128 View visibleView;
129 ButtonDropTarget dropTarget;
130 for (int i = getChildCount() - 1; i >= 0; --i) {
131 if (getChildAt(i) instanceof ButtonDropTarget) {
132 visibleView = dropTarget = (ButtonDropTarget) getChildAt(i);
133 } else if (getChildAt(i) instanceof ViewGroup) {
134 // The Drop Target is wrapped in a FrameLayout.
135 visibleView = getChildAt(i);
136 dropTarget = (ButtonDropTarget) ((ViewGroup) visibleView).getChildAt(0);
137 } else {
138 // Ignore other views.
139 continue;
140 }
141
142 if (visibleView.getVisibility() == View.VISIBLE) {
143 if (shouldUpdateText) {
144 result |= dropTarget.updateText(hideText);
145 } else if (dropTarget.isTextTruncated()) {
146 result = true;
147 break;
148 }
149 }
150 }
151
152 return result;
153 }
154
Sunny Goyal47328fd2016-05-25 18:56:41 -0700155 private void animateToVisibility(boolean isVisible) {
156 if (mVisible != isVisible) {
157 mVisible = isVisible;
158
159 // Cancel any existing animation
160 if (mCurrentAnimation != null) {
161 mCurrentAnimation.cancel();
162 mCurrentAnimation = null;
163 }
164
165 float finalAlpha = mVisible ? 1 : 0;
166 if (Float.compare(getAlpha(), finalAlpha) != 0) {
167 setVisibility(View.VISIBLE);
168 mCurrentAnimation = animate().alpha(finalAlpha)
169 .setInterpolator(DEFAULT_INTERPOLATOR)
170 .setDuration(DEFAULT_DRAG_FADE_DURATION)
171 .withEndAction(mFadeAnimationEndRunnable);
172 }
173
174 }
175 }
176
Sunny Goyal47328fd2016-05-25 18:56:41 -0700177 /*
178 * DragController.DragListener implementation
179 */
180 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700181 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700182 animateToVisibility(true);
183 }
184
185 /**
186 * This is called to defer hiding the delete drop target until the drop animation has completed,
187 * instead of hiding immediately when the drag has ended.
188 */
189 protected void deferOnDragEnd() {
190 mDeferOnDragEnd = true;
191 }
192
193 @Override
194 public void onDragEnd() {
195 if (!mDeferOnDragEnd) {
196 animateToVisibility(false);
197 } else {
198 mDeferOnDragEnd = false;
199 }
200 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700201
202 public ButtonDropTarget[] getDropTargets() {
203 return mDropTargets;
204 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700205}