blob: 9b38623c2b2f824474c0b5e6585259a0230c1954 [file] [log] [blame]
Tony Wickhamb54c4a32015-09-11 08:40:20 -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
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Sunny Goyal7c50b312016-02-10 12:26:23 -080021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070024import android.content.Context;
25import android.util.AttributeSet;
26import android.view.View;
Sunny Goyal7c50b312016-02-10 12:26:23 -080027import android.view.accessibility.AccessibilityManager;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070028import android.view.animation.AccelerateInterpolator;
29import android.widget.FrameLayout;
30
31import 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 */
36public abstract class BaseDropTargetBar extends FrameLayout implements DragController.DragListener {
37 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal7c50b312016-02-10 12:26:23 -080038 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = new AccelerateInterpolator();
Tony Wickhamb54c4a32015-09-11 08:40:20 -070039
40 protected View mDropTargetBar;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070041 protected boolean mAccessibilityEnabled = false;
42
Sunny Goyal7c50b312016-02-10 12:26:23 -080043 protected AnimatorSet mCurrentAnimation;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070044 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 Wickhamb54c4a32015-09-11 08:40:20 -070062 }
63
Tony Wickhamb54c4a32015-09-11 08:40:20 -070064 /**
Sunny Goyal7c50b312016-02-10 12:26:23 -080065 * Convenience method to animate the alpha of a view.
Tony Wickhamb54c4a32015-09-11 08:40:20 -070066 */
Sunny Goyal7c50b312016-02-10 12:26:23 -080067 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 Wickhamb54c4a32015-09-11 08:40:20 -070086 }
87
Sunny Goyal7c50b312016-02-10 12:26:23 -080088 if (newAnimationDuration > 0) {
89 mCurrentAnimation = new AnimatorSet();
90 mCurrentAnimation.setDuration(newAnimationDuration);
Tony Wickhamb54c4a32015-09-11 08:40:20 -070091 }
92 }
93
94 /*
95 * DragController.DragListener implementation
96 */
97 @Override
98 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070099 showDropTargets();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700100 }
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 Wickham9aae47f2015-10-01 13:04:22 -0700113 hideDropTargets();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700114 } else {
115 mDeferOnDragEnd = false;
116 }
117 }
118
Tony Wickham9aae47f2015-10-01 13:04:22 -0700119 public abstract void showDropTargets();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700120
Tony Wickham9aae47f2015-10-01 13:04:22 -0700121 public abstract void hideDropTargets();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700122
123 public abstract void enableAccessibleDrag(boolean enable);
124
125 public abstract void setup(Launcher launcher, DragController dragController);
Sunny Goyal7c50b312016-02-10 12:26:23 -0800126
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 Wickhamb54c4a32015-09-11 08:40:20 -0700146}