blob: 5966af51cef6816625d627165fb0437bcf25f837 [file] [log] [blame]
Sunny Goyal47328fd2016-05-25 18:56:41 -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.TimeInterpolator;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewDebug;
24import android.view.ViewPropertyAnimator;
25import android.view.accessibility.AccessibilityManager;
26import android.view.animation.AccelerateInterpolator;
27import android.widget.LinearLayout;
28
29import com.android.launcher3.dragndrop.DragController;
30
31/*
32 * The top bar containing various drop targets: Delete/App Info/Uninstall.
33 */
34public class DropTargetBar extends LinearLayout implements DragController.DragListener {
35
36 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
37 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = new AccelerateInterpolator();
38
39 private final Runnable mFadeAnimationEndRunnable = new Runnable() {
40
41 @Override
42 public void run() {
43 AccessibilityManager am = (AccessibilityManager)
44 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
45 boolean accessibilityEnabled = am.isEnabled();
46 AlphaUpdateListener.updateVisibility(DropTargetBar.this, accessibilityEnabled);
47 }
48 };
49
50 @ViewDebug.ExportedProperty(category = "launcher")
51 protected boolean mDeferOnDragEnd;
52
53 @ViewDebug.ExportedProperty(category = "launcher")
54 protected boolean mVisible = false;
55
56 private ViewPropertyAnimator mCurrentAnimation;
57
58 // Drop targets
59 private ButtonDropTarget mDeleteDropTarget;
60 private ButtonDropTarget mAppInfoDropTarget;
61 private ButtonDropTarget mUninstallDropTarget;
62
63 public DropTargetBar(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 }
66
67 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74
75 // Get the individual components
76 mDeleteDropTarget = (ButtonDropTarget) findViewById(R.id.delete_target_text);
77 mAppInfoDropTarget = (ButtonDropTarget) findViewById(R.id.info_target_text);
78 mUninstallDropTarget = (ButtonDropTarget) findViewById(R.id.uninstall_target_text);
79
80 mDeleteDropTarget.setDropTargetBar(this);
81 mAppInfoDropTarget.setDropTargetBar(this);
82 mUninstallDropTarget.setDropTargetBar(this);
83
84 // Initialize with hidden state
85 setAlpha(0f);
86 }
87
88 public void setup(DragController dragController) {
89 dragController.addDragListener(this);
90 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
91
92 dragController.addDragListener(mDeleteDropTarget);
93 dragController.addDragListener(mAppInfoDropTarget);
94 dragController.addDragListener(mUninstallDropTarget);
95
96 dragController.addDropTarget(mDeleteDropTarget);
97 dragController.addDropTarget(mAppInfoDropTarget);
98 dragController.addDropTarget(mUninstallDropTarget);
99 }
100
101 private void animateToVisibility(boolean isVisible) {
102 if (mVisible != isVisible) {
103 mVisible = isVisible;
104
105 // Cancel any existing animation
106 if (mCurrentAnimation != null) {
107 mCurrentAnimation.cancel();
108 mCurrentAnimation = null;
109 }
110
111 float finalAlpha = mVisible ? 1 : 0;
112 if (Float.compare(getAlpha(), finalAlpha) != 0) {
113 setVisibility(View.VISIBLE);
114 mCurrentAnimation = animate().alpha(finalAlpha)
115 .setInterpolator(DEFAULT_INTERPOLATOR)
116 .setDuration(DEFAULT_DRAG_FADE_DURATION)
117 .withEndAction(mFadeAnimationEndRunnable);
118 }
119
120 }
121 }
122
123 public void enableAccessibleDrag(boolean enable) {
124 mDeleteDropTarget.enableAccessibleDrag(enable);
125 mAppInfoDropTarget.enableAccessibleDrag(enable);
126 mUninstallDropTarget.enableAccessibleDrag(enable);
127 }
128
129 /*
130 * DragController.DragListener implementation
131 */
132 @Override
133 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
134 animateToVisibility(true);
135 }
136
137 /**
138 * This is called to defer hiding the delete drop target until the drop animation has completed,
139 * instead of hiding immediately when the drag has ended.
140 */
141 protected void deferOnDragEnd() {
142 mDeferOnDragEnd = true;
143 }
144
145 @Override
146 public void onDragEnd() {
147 if (!mDeferOnDragEnd) {
148 animateToVisibility(false);
149 } else {
150 mDeferOnDragEnd = false;
151 }
152 }
153}