blob: 2ee350195486dda6cbe19eeda5f5ceee221481bd [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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070029import android.util.AttributeSet;
Michael Jurka800242b2010-12-16 11:39:26 -080030import android.view.View;
Adam Cohencdc30d52010-12-01 15:09:47 -080031
Patrick Dubroy4ed62782010-08-17 15:11:18 -070032/**
33 * Implements a DropTarget which allows applications to be dropped on it,
34 * in order to launch the application info for that app.
35 */
Winson Chung760e5372010-12-15 13:14:23 -080036public class ApplicationInfoDropTarget extends IconDropTarget {
37 private static final int sFadeInAnimationDuration = 200;
38 private static final int sFadeOutAnimationDuration = 100;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070039
Michael Jurka800242b2010-12-16 11:39:26 -080040 private AnimatorSet mFadeAnimator;
Winson Chung760e5372010-12-15 13:14:23 -080041 private ObjectAnimator mHandleFadeAnimator;
Michael Jurka800242b2010-12-16 11:39:26 -080042 private boolean mHandleWasVisibleOnDragStart;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070043
44 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
45 this(context, attrs, 0);
46 }
47
48 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070050
Winson Chung760e5372010-12-15 13:14:23 -080051 // Set the hover paint colour
52 int colour = getContext().getResources().getColor(R.color.app_info_filter);
53 mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));
Patrick Dubroy4ed62782010-08-17 15:11:18 -070054 }
55
56 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
57 DragView dragView, Object dragInfo) {
58
59 // acceptDrop is called just before onDrop. We do the work here, rather than
60 // in onDrop, because it allows us to reject the drop (by returning false)
61 // so that the object being dragged isn't removed from the home screen.
Adam Cohencdc30d52010-12-01 15:09:47 -080062 if (getVisibility() != VISIBLE) return false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063
Patrick Dubroybc6840b2010-09-01 11:54:27 -070064 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070066 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070068 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070069 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070070 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070071 return false;
72 }
73
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
75 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080076 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -080077 dragView.setPaint(mHoverPaint);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070078 }
79
80 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
81 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080082 if (!mDragAndDropEnabled) return;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070083 dragView.setPaint(null);
84 }
85
86 public void onDragStart(DragSource source, Object info, int dragAction) {
Adam Cohencdc30d52010-12-01 15:09:47 -080087 if (info != null && mDragAndDropEnabled) {
Michael Jurka774bd372010-10-22 13:40:50 -070088 final int itemType = ((ItemInfo)info).itemType;
89 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Adam Cohencdc30d52010-12-01 15:09:47 -080090 if (mActive) {
Winson Chung760e5372010-12-15 13:14:23 -080091 // Fade in this icon
92 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -080093 mFadeAnimator = new AnimatorSet();
94 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
95 infoButtonAnimator.setDuration(sFadeInAnimationDuration);
96
97 if (mHandle == mLauncher.findViewById(R.id.configure_button)) {
98 final View divider = mLauncher.findViewById(R.id.divider_during_drag);
99 divider.setVisibility(VISIBLE);
100 Animator dividerAnimator = ObjectAnimator.ofFloat(divider, "alpha", 1.0f);
101 dividerAnimator.setDuration(sFadeInAnimationDuration);
102 mFadeAnimator.play(infoButtonAnimator).with(dividerAnimator);
103 } else {
104 mFadeAnimator.play(infoButtonAnimator);
105 }
Winson Chung760e5372010-12-15 13:14:23 -0800106 mFadeAnimator.start();
Adam Cohencdc30d52010-12-01 15:09:47 -0800107 setVisibility(VISIBLE);
Winson Chung760e5372010-12-15 13:14:23 -0800108
109 // Fade out the handle
110 if (mHandle != null) {
Michael Jurka800242b2010-12-16 11:39:26 -0800111 mHandleWasVisibleOnDragStart = mHandle.getVisibility() == VISIBLE;
Winson Chung760e5372010-12-15 13:14:23 -0800112 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
113 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 0.0f);
114 mHandleFadeAnimator.setDuration(sFadeOutAnimationDuration);
115 mHandleFadeAnimator.addListener(new AnimatorListener() {
116 public void onAnimationStart(Animator animation) {}
117 public void onAnimationRepeat(Animator animation) {}
118 public void onAnimationEnd(Animator animation) {
119 onEndOrCancel();
120 }
121 public void onAnimationCancel(Animator animation) {
122 onEndOrCancel();
123 }
124 private void onEndOrCancel() {
125 mHandle.setVisibility(INVISIBLE);
126 mHandleFadeAnimator = null;
127 }
128 });
129 mHandleFadeAnimator.start();
130 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700131 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700132 }
133 }
134
135 public void onDragEnd() {
Adam Cohencdc30d52010-12-01 15:09:47 -0800136 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -0800137 if (mActive) mActive = false;
Adam Cohencdc30d52010-12-01 15:09:47 -0800138
Winson Chung760e5372010-12-15 13:14:23 -0800139 // Fade out this icon
140 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -0800141 mFadeAnimator = new AnimatorSet();
142 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
143 infoButtonAnimator.setDuration(sFadeOutAnimationDuration);
144 final View divider = mLauncher.findViewById(R.id.divider_during_drag);
145 divider.setVisibility(VISIBLE);
146 Animator dividerAnimator = ObjectAnimator.ofFloat(divider, "alpha", 0.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800147 mFadeAnimator.addListener(new AnimatorListener() {
148 public void onAnimationStart(Animator animation) {}
149 public void onAnimationRepeat(Animator animation) {}
150 public void onAnimationEnd(Animator animation) {
151 onEndOrCancel();
152 }
153 public void onAnimationCancel(Animator animation) {
154 onEndOrCancel();
155 }
156 private void onEndOrCancel() {
157 setVisibility(GONE);
Michael Jurka800242b2010-12-16 11:39:26 -0800158 divider.setVisibility(GONE);
Winson Chung760e5372010-12-15 13:14:23 -0800159 mFadeAnimator = null;
160 }
161 });
Michael Jurka800242b2010-12-16 11:39:26 -0800162 mFadeAnimator.play(infoButtonAnimator).with(dividerAnimator);
Winson Chung760e5372010-12-15 13:14:23 -0800163 mFadeAnimator.start();
164
165 // Fade in the handle
Michael Jurka800242b2010-12-16 11:39:26 -0800166 if (mHandle != null && mHandleWasVisibleOnDragStart) {
Winson Chung760e5372010-12-15 13:14:23 -0800167 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
168 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 1.0f);
169 mHandleFadeAnimator.setDuration(sFadeInAnimationDuration);
170 mHandleFadeAnimator.start();
Michael Jurka466810b2010-10-25 13:41:02 -0700171 mHandle.setVisibility(VISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700172 }
173 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700174}