blob: 737d19841ca66805f4a86c0bce07fb2c9fcbaf69 [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
19import android.content.Context;
20import android.graphics.Paint;
21import android.graphics.PorterDuff;
22import android.graphics.PorterDuffColorFilter;
23import android.graphics.Rect;
24import android.util.AttributeSet;
25import android.view.View;
26import android.widget.ImageView;
27
28/**
29 * Implements a DropTarget which allows applications to be dropped on it,
30 * in order to launch the application info for that app.
31 */
32public class ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener {
33 private Launcher mLauncher;
34 private DragController mDragController;
35 private boolean mActive = false;
36
37 private View mHandle;
38
39 private final Paint mPaint = new Paint();
40
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);
47 }
48
49 @Override
50 protected void onFinishInflate() {
51 super.onFinishInflate();
52 }
53
54 /**
55 * Set the color that will be used as a filter over objects dragged over this object.
56 */
57 public void setDragColor(int color) {
58 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
59 }
60
61 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
62 DragView dragView, Object dragInfo) {
63
64 // acceptDrop is called just before onDrop. We do the work here, rather than
65 // in onDrop, because it allows us to reject the drop (by returning false)
66 // so that the object being dragged isn't removed from the home screen.
67
68 String packageName = null;
69 if (dragInfo instanceof ApplicationInfo) {
70 packageName = ((ApplicationInfo)dragInfo).componentName.getPackageName();
71 } else if (dragInfo instanceof ShortcutInfo) {
72 packageName = ((ShortcutInfo)dragInfo).intent.getComponent().getPackageName();
73 }
74 mLauncher.startApplicationDetailsActivity(packageName);
75 return false;
76 }
77
78 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
79 DragView dragView, Object dragInfo, Rect recycle) {
80 return null;
81 }
82
83 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
84 DragView dragView, Object dragInfo) {
85
86 }
87
88 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
89 DragView dragView, Object dragInfo) {
90 dragView.setPaint(mPaint);
91 }
92
93 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
94 DragView dragView, Object dragInfo) {
95 }
96
97 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
98 DragView dragView, Object dragInfo) {
99 // TODO: Animate out
100 dragView.setPaint(null);
101 }
102
103 public void onDragStart(DragSource source, Object info, int dragAction) {
104 if (info != null) {
105 mActive = true;
106
107 // TODO: Animate these in and out
108
109 // Only show the info icon when an application is selected
110 if (((ItemInfo)info).itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
111 setVisibility(VISIBLE);
112 }
113 mHandle.setVisibility(INVISIBLE);
114 }
115 }
116
117 public void onDragEnd() {
118 if (mActive) {
119 mActive = false;
120 // TODO: Animate these in and out
121 setVisibility(GONE);
122 mHandle.setVisibility(VISIBLE);
123 }
124 }
125
126 void setLauncher(Launcher launcher) {
127 mLauncher = launcher;
128 }
129
130 void setDragController(DragController dragController) {
131 mDragController = dragController;
132 }
133
134 void setHandle(View view) {
135 mHandle = view;
136 }
137
138 @Override
139 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
140 DragView dragView, Object dragInfo) {
141 return null;
142 }
143}