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; |
Sunny Goyal | d0030b0 | 2017-12-08 15:07:24 -0800 | [diff] [blame] | 20 | import static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 21 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 22 | import android.animation.TimeInterpolator; |
| 23 | import android.content.Context; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 26 | import android.view.Gravity; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewDebug; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 29 | import android.view.ViewGroup; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 30 | import android.view.ViewPropertyAnimator; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 32 | import android.widget.LinearLayout; |
| 33 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 34 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 35 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 36 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 37 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 41 | /* |
| 42 | * The top bar containing various drop targets: Delete/App Info/Uninstall. |
| 43 | */ |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 44 | public class DropTargetBar extends LinearLayout |
| 45 | implements DragListener, Insettable { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 46 | |
| 47 | protected static final int DEFAULT_DRAG_FADE_DURATION = 175; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 48 | protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 50 | private final Runnable mFadeAnimationEndRunnable = |
| 51 | () -> updateVisibility(DropTargetBar.this, isAccessibilityEnabled(getContext())); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 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 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 78 | @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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 112 | public void setup(DragController dragController) { |
| 113 | dragController.addDragListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 114 | 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 Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 135 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 136 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 137 | @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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 189 | 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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 211 | /* |
| 212 | * DragController.DragListener implementation |
| 213 | */ |
| 214 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 215 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 216 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 235 | |
| 236 | public ButtonDropTarget[] getDropTargets() { |
| 237 | return mDropTargets; |
| 238 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 239 | } |