Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -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 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.content.Context; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | import android.view.animation.AccelerateInterpolator; |
| 25 | import android.widget.FrameLayout; |
| 26 | |
| 27 | import com.android.launcher3.dragndrop.DragController; |
| 28 | |
| 29 | /** |
| 30 | * Base class for drop target bars (where you can drop apps to do actions such as uninstall). |
| 31 | */ |
| 32 | public abstract class BaseDropTargetBar extends FrameLayout implements DragController.DragListener { |
| 33 | protected static final int DEFAULT_DRAG_FADE_DURATION = 175; |
| 34 | |
| 35 | protected View mDropTargetBar; |
| 36 | |
| 37 | protected LauncherViewPropertyAnimator mDropTargetBarAnimator; |
| 38 | protected static final AccelerateInterpolator sAccelerateInterpolator = |
| 39 | new AccelerateInterpolator(); |
| 40 | protected boolean mAccessibilityEnabled = false; |
| 41 | |
| 42 | protected boolean mDeferOnDragEnd; |
| 43 | |
| 44 | public BaseDropTargetBar(Context context, AttributeSet attrs) { |
| 45 | this(context, attrs, 0); |
| 46 | } |
| 47 | |
| 48 | public BaseDropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 49 | super(context, attrs, defStyle); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void onFinishInflate() { |
| 54 | super.onFinishInflate(); |
| 55 | |
| 56 | mDropTargetBar = findViewById(R.id.drag_target_bar); |
| 57 | |
| 58 | // Create the various fade animations |
| 59 | mDropTargetBar.setAlpha(0f); |
| 60 | mDropTargetBarAnimator = new LauncherViewPropertyAnimator(mDropTargetBar); |
| 61 | mDropTargetBarAnimator.setInterpolator(sAccelerateInterpolator); |
| 62 | mDropTargetBarAnimator.addListener(new AnimatorListenerAdapter() { |
| 63 | @Override |
| 64 | public void onAnimationStart(Animator animation) { |
| 65 | // Ensure that the view is visible for the animation |
| 66 | mDropTargetBar.setVisibility(View.VISIBLE); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onAnimationEnd(Animator animation) { |
| 71 | if (mDropTargetBar != null) { |
| 72 | AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled); |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Convenience method to animate the alpha of a view using hardware layers. |
| 81 | */ |
| 82 | protected void animateViewAlpha(LauncherViewPropertyAnimator animator, View v, float alpha, |
| 83 | int duration) { |
| 84 | if (v == null) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | animator.cancel(); |
| 89 | if (Float.compare(v.getAlpha(), alpha) != 0) { |
| 90 | if (duration > 0) { |
| 91 | animator.alpha(alpha).withLayer().setDuration(duration).start(); |
| 92 | } else { |
| 93 | v.setAlpha(alpha); |
| 94 | AlphaUpdateListener.updateVisibility(v, mAccessibilityEnabled); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * DragController.DragListener implementation |
| 101 | */ |
| 102 | @Override |
| 103 | public void onDragStart(DragSource source, ItemInfo info, int dragAction) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 104 | showDropTargets(); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * This is called to defer hiding the delete drop target until the drop animation has completed, |
| 109 | * instead of hiding immediately when the drag has ended. |
| 110 | */ |
| 111 | protected void deferOnDragEnd() { |
| 112 | mDeferOnDragEnd = true; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void onDragEnd() { |
| 117 | if (!mDeferOnDragEnd) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 118 | hideDropTargets(); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 119 | } else { |
| 120 | mDeferOnDragEnd = false; |
| 121 | } |
| 122 | } |
| 123 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 124 | public abstract void showDropTargets(); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 125 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame^] | 126 | public abstract void hideDropTargets(); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 127 | |
| 128 | public abstract void enableAccessibleDrag(boolean enable); |
| 129 | |
| 130 | public abstract void setup(Launcher launcher, DragController dragController); |
| 131 | } |