blob: ce359d44a0bf7aac8b372933f618eeee6f751670 [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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070041
42 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
43 this(context, attrs, 0);
44 }
45
46 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
47 super(context, attrs, defStyle);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070048
Winson Chung760e5372010-12-15 13:14:23 -080049 // Set the hover paint colour
50 int colour = getContext().getResources().getColor(R.color.app_info_filter);
51 mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));
Winson Chungbe5212b2010-12-20 11:36:33 -080052
Winson Chung59e1f9a2010-12-21 11:31:54 -080053 if (LauncherApplication.isScreenXLarge()) {
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(
57 R.dimen.delete_zone_vertical_drag_padding);
58 int lr = getResources().getDimensionPixelSize(
59 R.dimen.delete_zone_horizontal_drag_padding);
60 setDragPadding(tb, lr, tb, 0);
61 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062 }
63
64 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
65 DragView dragView, Object dragInfo) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066 // 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
Michael Jurkab8e14472010-12-20 16:06:10 -0800104 mFadeAnimator.play(infoButtonAnimator);
105
Adam Cohencdc30d52010-12-01 15:09:47 -0800106 setVisibility(VISIBLE);
Winson Chung760e5372010-12-15 13:14:23 -0800107
Michael Jurkab8e14472010-12-20 16:06:10 -0800108 // Fade out the overlapping views
109 if (mOverlappingViews != null) {
110 for (View view : mOverlappingViews) {
111 ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 0.0f);
112 oa.setDuration(sFadeOutAnimationDuration);
113 mFadeAnimator.play(oa);
114 }
115 mFadeAnimator.addListener(new AnimatorListener() {
Winson Chung760e5372010-12-15 13:14:23 -0800116 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() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800125 for (View view : mOverlappingViews) {
126 view.setVisibility(INVISIBLE);
127 }
128 mFadeAnimator = null;
Winson Chung760e5372010-12-15 13:14:23 -0800129 }
130 });
Winson Chung760e5372010-12-15 13:14:23 -0800131 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800132 mFadeAnimator.start();
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700133 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700134 }
135 }
136
137 public void onDragEnd() {
Adam Cohencdc30d52010-12-01 15:09:47 -0800138 if (!mDragAndDropEnabled) return;
Winson Chung760e5372010-12-15 13:14:23 -0800139 if (mActive) mActive = false;
Adam Cohencdc30d52010-12-01 15:09:47 -0800140
Winson Chung760e5372010-12-15 13:14:23 -0800141 // Fade out this icon
142 if (mFadeAnimator != null) mFadeAnimator.cancel();
Michael Jurka800242b2010-12-16 11:39:26 -0800143 mFadeAnimator = new AnimatorSet();
144 Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
145 infoButtonAnimator.setDuration(sFadeOutAnimationDuration);
Winson Chung760e5372010-12-15 13:14:23 -0800146 mFadeAnimator.addListener(new AnimatorListener() {
147 public void onAnimationStart(Animator animation) {}
148 public void onAnimationRepeat(Animator animation) {}
149 public void onAnimationEnd(Animator animation) {
150 onEndOrCancel();
151 }
152 public void onAnimationCancel(Animator animation) {
153 onEndOrCancel();
154 }
155 private void onEndOrCancel() {
156 setVisibility(GONE);
157 mFadeAnimator = null;
158 }
159 });
Michael Jurkab8e14472010-12-20 16:06:10 -0800160 mFadeAnimator.play(infoButtonAnimator);
Winson Chung760e5372010-12-15 13:14:23 -0800161
Michael Jurkab8e14472010-12-20 16:06:10 -0800162 // Fade in the overlapping views
163 if (mOverlappingViews != null) {
164 for (View view : mOverlappingViews) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700165 // Check whether the views are enabled first, before trying to fade them in
166 if (view.isEnabled()) {
167 ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 1.0f);
168 oa.setDuration(sFadeInAnimationDuration);
169 mFadeAnimator.play(oa);
170 view.setVisibility(VISIBLE);
171 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800172 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700173 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800174 mFadeAnimator.start();
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700175 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700176}