blob: 7c81c1a259c13c2a676bbd0ac4ab662fadff0291 [file] [log] [blame]
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001/*
2 * Copyright (C) 2010 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.launcher2;
18
Michael Jurka800242b2010-12-16 11:39:26 -080019import com.android.launcher.R;
20
Winson Chung760e5372010-12-15 13:14:23 -080021import android.animation.Animator;
Michael Jurka800242b2010-12-16 11:39:26 -080022import android.animation.AnimatorSet;
Winson Chung760e5372010-12-15 13:14:23 -080023import android.animation.ObjectAnimator;
Michael Jurka800242b2010-12-16 11:39:26 -080024import android.animation.Animator.AnimatorListener;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070025import android.content.ComponentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070026import android.content.Context;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027import android.graphics.PorterDuff;
28import android.graphics.PorterDuffColorFilter;
Winson Chungbe5212b2010-12-20 11:36:33 -080029import android.graphics.Rect;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070030import android.util.AttributeSet;
Michael Jurka800242b2010-12-16 11:39:26 -080031import android.view.View;
Adam Cohencdc30d52010-12-01 15:09:47 -080032
Patrick Dubroy4ed62782010-08-17 15:11:18 -070033/**
34 * Implements a DropTarget which allows applications to be dropped on it,
35 * in order to launch the application info for that app.
36 */
Winson Chung760e5372010-12-15 13:14:23 -080037public class ApplicationInfoDropTarget extends IconDropTarget {
38 private static final int sFadeInAnimationDuration = 200;
39 private static final int sFadeOutAnimationDuration = 100;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070040
Michael Jurka800242b2010-12-16 11:39:26 -080041 private AnimatorSet mFadeAnimator;
Winson Chung760e5372010-12-15 13:14:23 -080042 private ObjectAnimator mHandleFadeAnimator;
Michael Jurka800242b2010-12-16 11:39:26 -080043 private boolean mHandleWasVisibleOnDragStart;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070044
45 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
46 this(context, attrs, 0);
47 }
48
49 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070051
Winson Chung760e5372010-12-15 13:14:23 -080052 // Set the hover paint colour
53 int colour = getContext().getResources().getColor(R.color.app_info_filter);
54 mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));
Winson Chungbe5212b2010-12-20 11:36:33 -080055
56 // For the application info drop target, we just ignore the left padding since we don't want
57 // to overlap with the delete zone padding
58 int tb = getResources().getDimensionPixelSize(R.dimen.delete_zone_vertical_drag_padding);
59 int lr = getResources().getDimensionPixelSize(R.dimen.delete_zone_horizontal_drag_padding);
60 setDragPadding(tb, lr, tb, 0);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070061 }
62
63 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
64 DragView dragView, Object dragInfo) {
65
66 // acceptDrop is called just before onDrop. We do the work here, rather than
67 // in onDrop, because it allows us to reject the drop (by returning false)
68 // so that the object being dragged isn't removed from the home screen.
Adam Cohencdc30d52010-12-01 15:09:47 -080069 if (getVisibility() != VISIBLE) return false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070070
Patrick Dubroybc6840b2010-09-01 11:54:27 -070071 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070073 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070075 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070076 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070077 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070078 return false;
79 }
80
Patrick Dubroy4ed62782010-08-17 15:11:18 -070081 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
82 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080083 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -080084 dragView.setPaint(mHoverPaint);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070085 }
86
87 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
88 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080089 if (!mDragAndDropEnabled) return;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070090 dragView.setPaint(null);
91 }
92
93 public void onDragStart(DragSource source, Object info, int dragAction) {
Adam Cohencdc30d52010-12-01 15:09:47 -080094 if (info != null && mDragAndDropEnabled) {
Michael Jurka774bd372010-10-22 13:40:50 -070095 final int itemType = ((ItemInfo)info).itemType;
96 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Adam Cohencdc30d52010-12-01 15:09:47 -080097 if (mActive) {
Winson Chung760e5372010-12-15 13:14:23 -080098 // Fade in this icon
99 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -0800100 mFadeAnimator = new AnimatorSet();
101 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
102 infoButtonAnimator.setDuration(sFadeInAnimationDuration);
103
104 if (mHandle == mLauncher.findViewById(R.id.configure_button)) {
105 final View divider = mLauncher.findViewById(R.id.divider_during_drag);
106 divider.setVisibility(VISIBLE);
107 Animator dividerAnimator = ObjectAnimator.ofFloat(divider, "alpha", 1.0f);
108 dividerAnimator.setDuration(sFadeInAnimationDuration);
109 mFadeAnimator.play(infoButtonAnimator).with(dividerAnimator);
110 } else {
111 mFadeAnimator.play(infoButtonAnimator);
112 }
Winson Chung760e5372010-12-15 13:14:23 -0800113 mFadeAnimator.start();
Adam Cohencdc30d52010-12-01 15:09:47 -0800114 setVisibility(VISIBLE);
Winson Chung760e5372010-12-15 13:14:23 -0800115
116 // Fade out the handle
117 if (mHandle != null) {
Michael Jurka800242b2010-12-16 11:39:26 -0800118 mHandleWasVisibleOnDragStart = mHandle.getVisibility() == VISIBLE;
Winson Chung760e5372010-12-15 13:14:23 -0800119 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
120 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 0.0f);
121 mHandleFadeAnimator.setDuration(sFadeOutAnimationDuration);
122 mHandleFadeAnimator.addListener(new AnimatorListener() {
123 public void onAnimationStart(Animator animation) {}
124 public void onAnimationRepeat(Animator animation) {}
125 public void onAnimationEnd(Animator animation) {
126 onEndOrCancel();
127 }
128 public void onAnimationCancel(Animator animation) {
129 onEndOrCancel();
130 }
131 private void onEndOrCancel() {
132 mHandle.setVisibility(INVISIBLE);
133 mHandleFadeAnimator = null;
134 }
135 });
136 mHandleFadeAnimator.start();
137 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700138 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700139 }
140 }
141
142 public void onDragEnd() {
Adam Cohencdc30d52010-12-01 15:09:47 -0800143 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -0800144 if (mActive) mActive = false;
Adam Cohencdc30d52010-12-01 15:09:47 -0800145
Winson Chung760e5372010-12-15 13:14:23 -0800146 // Fade out this icon
147 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -0800148 mFadeAnimator = new AnimatorSet();
149 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
150 infoButtonAnimator.setDuration(sFadeOutAnimationDuration);
151 final View divider = mLauncher.findViewById(R.id.divider_during_drag);
152 divider.setVisibility(VISIBLE);
153 Animator dividerAnimator = ObjectAnimator.ofFloat(divider, "alpha", 0.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800154 mFadeAnimator.addListener(new AnimatorListener() {
155 public void onAnimationStart(Animator animation) {}
156 public void onAnimationRepeat(Animator animation) {}
157 public void onAnimationEnd(Animator animation) {
158 onEndOrCancel();
159 }
160 public void onAnimationCancel(Animator animation) {
161 onEndOrCancel();
162 }
163 private void onEndOrCancel() {
164 setVisibility(GONE);
Michael Jurka800242b2010-12-16 11:39:26 -0800165 divider.setVisibility(GONE);
Winson Chung760e5372010-12-15 13:14:23 -0800166 mFadeAnimator = null;
167 }
168 });
Michael Jurka800242b2010-12-16 11:39:26 -0800169 mFadeAnimator.play(infoButtonAnimator).with(dividerAnimator);
Winson Chung760e5372010-12-15 13:14:23 -0800170 mFadeAnimator.start();
171
172 // Fade in the handle
Michael Jurka800242b2010-12-16 11:39:26 -0800173 if (mHandle != null && mHandleWasVisibleOnDragStart) {
Winson Chung760e5372010-12-15 13:14:23 -0800174 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
175 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 1.0f);
176 mHandleFadeAnimator.setDuration(sFadeInAnimationDuration);
177 mHandleFadeAnimator.start();
Michael Jurka466810b2010-10-25 13:41:02 -0700178 mHandle.setVisibility(VISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700179 }
180 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700181}