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