blob: 9d421c68a6770b229cf0e346dde740f318de6669 [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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070042
43 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070049
Winson Chung760e5372010-12-15 13:14:23 -080050 // Set the hover paint colour
51 int colour = getContext().getResources().getColor(R.color.app_info_filter);
52 mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));
Winson Chungbe5212b2010-12-20 11:36:33 -080053
54 // For the application info drop target, we just ignore the left padding since we don't want
55 // to overlap with the delete zone padding
56 int tb = getResources().getDimensionPixelSize(R.dimen.delete_zone_vertical_drag_padding);
57 int lr = getResources().getDimensionPixelSize(R.dimen.delete_zone_horizontal_drag_padding);
58 setDragPadding(tb, lr, tb, 0);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070059 }
60
61 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
62 DragView dragView, Object dragInfo) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063 // acceptDrop is called just before onDrop. We do the work here, rather than
64 // in onDrop, because it allows us to reject the drop (by returning false)
65 // so that the object being dragged isn't removed from the home screen.
Adam Cohencdc30d52010-12-01 15:09:47 -080066 if (getVisibility() != VISIBLE) return false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067
Patrick Dubroybc6840b2010-09-01 11:54:27 -070068 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070069 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070070 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070071 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070072 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070074 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075 return false;
76 }
77
Patrick Dubroy4ed62782010-08-17 15:11:18 -070078 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
79 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080080 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -080081 dragView.setPaint(mHoverPaint);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070082 }
83
84 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
85 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080086 if (!mDragAndDropEnabled) return;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070087 dragView.setPaint(null);
88 }
89
90 public void onDragStart(DragSource source, Object info, int dragAction) {
Adam Cohencdc30d52010-12-01 15:09:47 -080091 if (info != null && mDragAndDropEnabled) {
Michael Jurka774bd372010-10-22 13:40:50 -070092 final int itemType = ((ItemInfo)info).itemType;
93 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Adam Cohencdc30d52010-12-01 15:09:47 -080094 if (mActive) {
Winson Chung760e5372010-12-15 13:14:23 -080095 // Fade in this icon
96 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -080097 mFadeAnimator = new AnimatorSet();
98 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
99 infoButtonAnimator.setDuration(sFadeInAnimationDuration);
100
Michael Jurkab8e14472010-12-20 16:06:10 -0800101 mFadeAnimator.play(infoButtonAnimator);
102
Adam Cohencdc30d52010-12-01 15:09:47 -0800103 setVisibility(VISIBLE);
Winson Chung760e5372010-12-15 13:14:23 -0800104
Michael Jurkab8e14472010-12-20 16:06:10 -0800105 // Fade out the overlapping views
106 if (mOverlappingViews != null) {
107 for (View view : mOverlappingViews) {
108 ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 0.0f);
109 oa.setDuration(sFadeOutAnimationDuration);
110 mFadeAnimator.play(oa);
111 }
112 mFadeAnimator.addListener(new AnimatorListener() {
Winson Chung760e5372010-12-15 13:14:23 -0800113 public void onAnimationStart(Animator animation) {}
114 public void onAnimationRepeat(Animator animation) {}
115 public void onAnimationEnd(Animator animation) {
116 onEndOrCancel();
117 }
118 public void onAnimationCancel(Animator animation) {
119 onEndOrCancel();
120 }
121 private void onEndOrCancel() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800122 for (View view : mOverlappingViews) {
123 view.setVisibility(INVISIBLE);
124 }
125 mFadeAnimator = null;
Winson Chung760e5372010-12-15 13:14:23 -0800126 }
127 });
Winson Chung760e5372010-12-15 13:14:23 -0800128 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800129 mFadeAnimator.start();
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700130 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700131 }
132 }
133
134 public void onDragEnd() {
Adam Cohencdc30d52010-12-01 15:09:47 -0800135 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -0800136 if (mActive) mActive = false;
Adam Cohencdc30d52010-12-01 15:09:47 -0800137
Winson Chung760e5372010-12-15 13:14:23 -0800138 // Fade out this icon
139 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -0800140 mFadeAnimator = new AnimatorSet();
141 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
142 infoButtonAnimator.setDuration(sFadeOutAnimationDuration);
Winson Chung760e5372010-12-15 13:14:23 -0800143 mFadeAnimator.addListener(new AnimatorListener() {
144 public void onAnimationStart(Animator animation) {}
145 public void onAnimationRepeat(Animator animation) {}
146 public void onAnimationEnd(Animator animation) {
147 onEndOrCancel();
148 }
149 public void onAnimationCancel(Animator animation) {
150 onEndOrCancel();
151 }
152 private void onEndOrCancel() {
153 setVisibility(GONE);
154 mFadeAnimator = null;
155 }
156 });
Michael Jurkab8e14472010-12-20 16:06:10 -0800157 mFadeAnimator.play(infoButtonAnimator);
Winson Chung760e5372010-12-15 13:14:23 -0800158
Michael Jurkab8e14472010-12-20 16:06:10 -0800159 // Fade in the overlapping views
160 if (mOverlappingViews != null) {
161 for (View view : mOverlappingViews) {
162 ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 1.0f);
163 oa.setDuration(sFadeInAnimationDuration);
164 mFadeAnimator.play(oa);
165 view.setVisibility(VISIBLE);
166 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700167 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800168 mFadeAnimator.start();
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700169 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700170}