Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 19 | import static com.android.launcher3.AlphaUpdateListener.updateVisibility; |
| 20 | import static com.android.launcher3.Utilities.isAccessibilityEnabled; |
| 21 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 22 | import android.animation.TimeInterpolator; |
| 23 | import android.content.Context; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewDebug; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 28 | import android.view.ViewPropertyAnimator; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 29 | import android.view.animation.AccelerateInterpolator; |
| 30 | import android.widget.LinearLayout; |
| 31 | |
| 32 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 33 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 35 | import java.util.ArrayList; |
| 36 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 37 | /* |
| 38 | * The top bar containing various drop targets: Delete/App Info/Uninstall. |
| 39 | */ |
| 40 | public 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 49 | updateVisibility(DropTargetBar.this, isAccessibilityEnabled(getContext())); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | @ViewDebug.ExportedProperty(category = "launcher") |
| 54 | protected boolean mDeferOnDragEnd; |
| 55 | |
| 56 | @ViewDebug.ExportedProperty(category = "launcher") |
| 57 | protected boolean mVisible = false; |
| 58 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 59 | private ButtonDropTarget[] mDropTargets; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 60 | private ViewPropertyAnimator mCurrentAnimation; |
| 61 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 62 | 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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 74 | // Initialize with hidden state |
| 75 | setAlpha(0f); |
| 76 | } |
| 77 | |
| 78 | public void setup(DragController dragController) { |
| 79 | dragController.addDragListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 80 | 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 Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 101 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 102 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 103 | @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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 155 | 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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 177 | /* |
| 178 | * DragController.DragListener implementation |
| 179 | */ |
| 180 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 181 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 182 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame^] | 201 | |
| 202 | public ButtonDropTarget[] getDropTargets() { |
| 203 | return mDropTargets; |
| 204 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 205 | } |