blob: fdcbbd4782856523cf0444270c51243b2f36f337 [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
Winson Chung760e5372010-12-15 13:14:23 -080019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.ObjectAnimator;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070022import android.content.ComponentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023import android.content.Context;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070026import android.util.AttributeSet;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Adam Cohencdc30d52010-12-01 15:09:47 -080028import com.android.launcher.R;
29
Patrick Dubroy4ed62782010-08-17 15:11:18 -070030/**
31 * Implements a DropTarget which allows applications to be dropped on it,
32 * in order to launch the application info for that app.
33 */
Winson Chung760e5372010-12-15 13:14:23 -080034public class ApplicationInfoDropTarget extends IconDropTarget {
35 private static final int sFadeInAnimationDuration = 200;
36 private static final int sFadeOutAnimationDuration = 100;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070037
Winson Chung760e5372010-12-15 13:14:23 -080038 private ObjectAnimator mFadeAnimator;
39 private ObjectAnimator mHandleFadeAnimator;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070040
41 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
42 this(context, attrs, 0);
43 }
44
45 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
46 super(context, attrs, defStyle);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070047
Winson Chung760e5372010-12-15 13:14:23 -080048 // Set the hover paint colour
49 int colour = getContext().getResources().getColor(R.color.app_info_filter);
50 mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));
Patrick Dubroy4ed62782010-08-17 15:11:18 -070051 }
52
53 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
54 DragView dragView, Object dragInfo) {
55
56 // acceptDrop is called just before onDrop. We do the work here, rather than
57 // in onDrop, because it allows us to reject the drop (by returning false)
58 // so that the object being dragged isn't removed from the home screen.
Adam Cohencdc30d52010-12-01 15:09:47 -080059 if (getVisibility() != VISIBLE) return false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070060
Patrick Dubroybc6840b2010-09-01 11:54:27 -070061 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070063 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070065 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070067 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068 return false;
69 }
70
Patrick Dubroy4ed62782010-08-17 15:11:18 -070071 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
72 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080073 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -080074 dragView.setPaint(mHoverPaint);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075 }
76
77 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
78 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080079 if (!mDragAndDropEnabled) return;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070080 dragView.setPaint(null);
81 }
82
83 public void onDragStart(DragSource source, Object info, int dragAction) {
Adam Cohencdc30d52010-12-01 15:09:47 -080084 if (info != null && mDragAndDropEnabled) {
Michael Jurka774bd372010-10-22 13:40:50 -070085 final int itemType = ((ItemInfo)info).itemType;
86 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Adam Cohencdc30d52010-12-01 15:09:47 -080087 if (mActive) {
Winson Chung760e5372010-12-15 13:14:23 -080088 // Fade in this icon
89 if (mFadeAnimator != null) mFadeAnimator.cancel();
90 mFadeAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
91 mFadeAnimator.setDuration(sFadeInAnimationDuration);
92 mFadeAnimator.start();
Adam Cohencdc30d52010-12-01 15:09:47 -080093 setVisibility(VISIBLE);
Winson Chung760e5372010-12-15 13:14:23 -080094
95 // Fade out the handle
96 if (mHandle != null) {
97 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
98 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 0.0f);
99 mHandleFadeAnimator.setDuration(sFadeOutAnimationDuration);
100 mHandleFadeAnimator.addListener(new AnimatorListener() {
101 public void onAnimationStart(Animator animation) {}
102 public void onAnimationRepeat(Animator animation) {}
103 public void onAnimationEnd(Animator animation) {
104 onEndOrCancel();
105 }
106 public void onAnimationCancel(Animator animation) {
107 onEndOrCancel();
108 }
109 private void onEndOrCancel() {
110 mHandle.setVisibility(INVISIBLE);
111 mHandleFadeAnimator = null;
112 }
113 });
114 mHandleFadeAnimator.start();
115 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700116 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700117 }
118 }
119
120 public void onDragEnd() {
Adam Cohencdc30d52010-12-01 15:09:47 -0800121 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -0800122 if (mActive) mActive = false;
Adam Cohencdc30d52010-12-01 15:09:47 -0800123
Winson Chung760e5372010-12-15 13:14:23 -0800124 // Fade out this icon
125 if (mFadeAnimator != null) mFadeAnimator.cancel();
126 mFadeAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
127 mFadeAnimator.setDuration(sFadeOutAnimationDuration);
128 mFadeAnimator.addListener(new AnimatorListener() {
129 public void onAnimationStart(Animator animation) {}
130 public void onAnimationRepeat(Animator animation) {}
131 public void onAnimationEnd(Animator animation) {
132 onEndOrCancel();
133 }
134 public void onAnimationCancel(Animator animation) {
135 onEndOrCancel();
136 }
137 private void onEndOrCancel() {
138 setVisibility(GONE);
139 mFadeAnimator = null;
140 }
141 });
142 mFadeAnimator.start();
143
144 // Fade in the handle
Adam Cohencdc30d52010-12-01 15:09:47 -0800145 if (mHandle != null) {
Winson Chung760e5372010-12-15 13:14:23 -0800146 if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel();
147 mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 1.0f);
148 mHandleFadeAnimator.setDuration(sFadeInAnimationDuration);
149 mHandleFadeAnimator.start();
Michael Jurka466810b2010-10-25 13:41:02 -0700150 mHandle.setVisibility(VISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700151 }
152 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700153}