blob: e43e96c76e614964531fe7a2e043af0e694b25fa [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080019import android.animation.AnimatorSet;
20import android.animation.ObjectAnimator;
21import android.animation.TimeInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.Context;
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +010023import android.graphics.Rect;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.util.AttributeSet;
25import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080026import android.view.ViewDebug;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080027import android.view.animation.DecelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070028
Vadim Tryshevfedca432015-08-19 17:55:02 -070029import com.android.launcher3.dragndrop.DragController;
Sunny Goyald1ea63f2015-08-05 12:32:47 -070030import com.android.launcher3.util.Thunk;
31
Winson Chung4c98d922011-05-31 16:50:48 -070032/*
Tony Wickhamb54c4a32015-09-11 08:40:20 -070033 * This bar will manage the transition between the QSB search bar and the delete/uninstall drop
34 * targets so that each of the individual ButtonDropTargets don't have to.
Winson Chung4c98d922011-05-31 16:50:48 -070035 */
Tony Wickhamb54c4a32015-09-11 08:40:20 -070036public class SearchDropTargetBar extends BaseDropTargetBar {
Winson Chung4c98d922011-05-31 16:50:48 -070037
Winson Chung006ee262015-08-03 14:40:11 -070038 /** The different states that the search bar space can be in. */
39 public enum State {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -070040 INVISIBLE (0f),
41 DROP_TARGET (1f);
Winson Chung4c98d922011-05-31 16:50:48 -070042
Winson Chung006ee262015-08-03 14:40:11 -070043 private final float mDropTargetBarAlpha;
44
Sunny Goyalda4fe1a2016-05-26 16:05:17 -070045 State(float dtbAlpha) {
Winson Chung006ee262015-08-03 14:40:11 -070046 mDropTargetBarAlpha = dtbAlpha;
47 }
Winson Chung006ee262015-08-03 14:40:11 -070048 }
49
Winson Chung006ee262015-08-03 14:40:11 -070050
Sunny Goyal4ffec482016-02-09 11:28:52 -080051 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalda4fe1a2016-05-26 16:05:17 -070052 private State mState = State.INVISIBLE;
Winson Chung4c98d922011-05-31 16:50:48 -070053
Sunny Goyalfa401a12015-04-10 13:45:42 -070054 // Drop targets
Sunny Goyalfa401a12015-04-10 13:45:42 -070055 private ButtonDropTarget mDeleteDropTarget;
56 private ButtonDropTarget mUninstallDropTarget;
57
Winson Chung4c98d922011-05-31 16:50:48 -070058 public SearchDropTargetBar(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64 }
65
Winson Chung4c98d922011-05-31 16:50:48 -070066 @Override
67 protected void onFinishInflate() {
68 super.onFinishInflate();
69
70 // Get the individual components
Winson Chunga6427b12011-07-27 10:53:39 -070071 mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
Tony Wickhamb54c4a32015-09-11 08:40:20 -070072 mUninstallDropTarget = (ButtonDropTarget) mDropTargetBar
73 .findViewById(R.id.uninstall_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070074
Tony Wickhamb54c4a32015-09-11 08:40:20 -070075 mDeleteDropTarget.setDropTargetBar(this);
76 mUninstallDropTarget.setDropTargetBar(this);
77 }
Adam Cohend4d7aa52011-07-19 21:47:37 -070078
Tony Wickhamb54c4a32015-09-11 08:40:20 -070079 @Override
80 public void setup(Launcher launcher, DragController dragController) {
81 dragController.addDragListener(this);
82 dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
Winson Chung006ee262015-08-03 14:40:11 -070083
Tony Wickhamb54c4a32015-09-11 08:40:20 -070084 dragController.addDragListener(mDeleteDropTarget);
85 dragController.addDragListener(mUninstallDropTarget);
86
87 dragController.addDropTarget(mDeleteDropTarget);
88 dragController.addDropTarget(mUninstallDropTarget);
89
90 mDeleteDropTarget.setLauncher(launcher);
91 mUninstallDropTarget.setLauncher(launcher);
92 }
93
94 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -070095 public void showDropTargets() {
Tony Wickhamb54c4a32015-09-11 08:40:20 -070096 animateToState(State.DROP_TARGET, DEFAULT_DRAG_FADE_DURATION);
97 }
98
99 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700100 public void hideDropTargets() {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700101 animateToState(State.INVISIBLE, DEFAULT_DRAG_FADE_DURATION);
Winson Chung006ee262015-08-03 14:40:11 -0700102 }
103
104 /**
105 * Animates the current search bar state to a new state. If the {@param duration} is 0, then
106 * the state is applied immediately.
107 */
108 public void animateToState(State newState, int duration) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800109 animateToState(newState, duration, null);
110 }
111
112 public void animateToState(State newState, int duration, AnimatorSet animation) {
Winson Chung006ee262015-08-03 14:40:11 -0700113 if (mState != newState) {
114 mState = newState;
115
Sunny Goyal7c50b312016-02-10 12:26:23 -0800116 resetAnimation(duration);
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800117 if (duration > 0) {
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800118 animateAlpha(mDropTargetBar, mState.mDropTargetBarAlpha, DEFAULT_INTERPOLATOR);
119 } else {
120 mDropTargetBar.setAlpha(mState.mDropTargetBarAlpha);
121 AlphaUpdateListener.updateVisibility(mDropTargetBar, mAccessibilityEnabled);
122 }
Winson Chung006ee262015-08-03 14:40:11 -0700123
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800124 // Start the final animation
125 if (duration > 0) {
126 if (animation != null) {
127 animation.play(mCurrentAnimation);
128 } else {
129 mCurrentAnimation.start();
130 }
131 }
Winson Chung006ee262015-08-03 14:40:11 -0700132 }
133 }
134
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700135 @Override
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700136 public void enableAccessibleDrag(boolean enable) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700137 mDeleteDropTarget.enableAccessibleDrag(enable);
138 mUninstallDropTarget.enableAccessibleDrag(enable);
139 }
Winson Chung4c98d922011-05-31 16:50:48 -0700140}