blob: f478a3530512a6732991f0d29c2f6e2a2d97cd30 [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;
21import android.content.Context;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.animation.AccelerateInterpolator;
25import android.widget.FrameLayout;
26
27import 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 */
32public 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) {
104 showDropTarget();
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) {
118 hideDropTarget();
119 } else {
120 mDeferOnDragEnd = false;
121 }
122 }
123
124 public abstract void showDropTarget();
125
126 public abstract void hideDropTarget();
127
128 public abstract void enableAccessibleDrag(boolean enable);
129
130 public abstract void setup(Launcher launcher, DragController dragController);
131}