blob: 5b3fd1e16ba3ecb72dd6096abc7465594a284e2a [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 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 Chung61fa4192011-06-12 15:15:29 -070019import android.animation.ObjectAnimator;
Winson Chung4c98d922011-05-31 16:50:48 -070020import android.content.Context;
21import android.content.res.Resources;
Winson Chung61fa4192011-06-12 15:15:29 -070022import android.graphics.Color;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.graphics.PorterDuff;
24import android.graphics.PorterDuffColorFilter;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
27import android.view.ViewGroup;
Winson Chung61fa4192011-06-12 15:15:29 -070028import android.widget.TextView;
Winson Chung4c98d922011-05-31 16:50:48 -070029
30import com.android.launcher.R;
31
Winson Chung61fa4192011-06-12 15:15:29 -070032public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070033
Winson Chung61fa4192011-06-12 15:15:29 -070034 private TextView mText;
Winson Chung4c98d922011-05-31 16:50:48 -070035 private int mHoverColor = 0xFFFF0000;
36
37 public DeleteDropTarget(Context context, AttributeSet attrs) {
38 this(context, attrs, 0);
39 }
40
41 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
42 super(context, attrs, defStyle);
43 }
44
45 @Override
46 protected void onFinishInflate() {
47 super.onFinishInflate();
48
49 // Get the drawable
Winson Chung61fa4192011-06-12 15:15:29 -070050 mText = (TextView) findViewById(R.id.delete_target_text);
Winson Chung4c98d922011-05-31 16:50:48 -070051
52 // Get the hover color
53 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070054 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
55 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
56 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Winson Chung61fa4192011-06-12 15:15:29 -070057 setBackgroundColor(mHoverColor);
58 getBackground().setAlpha(0);
Winson Chung4c98d922011-05-31 16:50:48 -070059 }
60
61 private boolean isAllAppsApplication(DragSource source, Object info) {
62 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
63 }
64 private boolean isAllAppsWidget(DragSource source, Object info) {
65 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
66 }
67 private boolean isWorkspaceApplication(DragObject d) {
68 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof ShortcutInfo);
69 }
70 private boolean isWorkspaceWidget(DragObject d) {
71 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof LauncherAppWidgetInfo);
72 }
73 private boolean isWorkspaceFolder(DragObject d) {
74 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
75 }
76
77 @Override
78 public boolean acceptDrop(DragObject d) {
79 // We can remove everything including App shortcuts, folders, widgets, etc.
80 return true;
81 }
82
83 @Override
84 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -070085 boolean isVisible = true;
86 boolean isUninstall = false;
87
Winson Chungf0ea4d32011-06-06 14:27:16 -070088 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -070089 if (isAllAppsWidget(source, info)) {
90 isVisible = false;
91 }
92
93 // If we are dragging an application from AppsCustomize, only show the control if we can
94 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
95 if (isAllAppsApplication(source, info)) {
96 ApplicationInfo appInfo = (ApplicationInfo) info;
97 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
98 isUninstall = true;
99 } else {
100 isVisible = false;
101 }
102 }
103
104 mActive = isVisible;
Winson Chung61fa4192011-06-12 15:15:29 -0700105 setVisibility(isVisible ? View.VISIBLE : View.GONE);
106 if (mText.getText().length() > 0) {
107 mText.setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700108 : R.string.delete_target_label);
109 }
110 }
111
112 @Override
113 public void onDragEnd() {
114 super.onDragEnd();
115 mActive = false;
116 }
117
118 public void onDragEnter(DragObject d) {
119 super.onDragEnter(d);
120
Winson Chung61fa4192011-06-12 15:15:29 -0700121 ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha",
122 Color.alpha(mHoverColor));
123 anim.setDuration(mTransitionDuration);
124 anim.start();
Winson Chung4c98d922011-05-31 16:50:48 -0700125 }
126
127 public void onDragExit(DragObject d) {
128 super.onDragExit(d);
129
Winson Chung61fa4192011-06-12 15:15:29 -0700130 ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", 0);
131 anim.setDuration(mTransitionDuration);
132 anim.start();
Winson Chung4c98d922011-05-31 16:50:48 -0700133 }
134
135 public void onDrop(DragObject d) {
136 ItemInfo item = (ItemInfo) d.dragInfo;
137
138 if (isAllAppsApplication(d.dragSource, item)) {
139 // Uninstall the application if it is being dragged from AppsCustomize
140 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
141 } else if (isWorkspaceApplication(d)) {
142 LauncherModel.deleteItemFromDatabase(mLauncher, item);
143 } else if (isWorkspaceFolder(d)) {
144 // Remove the folder from the workspace and delete the contents from launcher model
145 FolderInfo folderInfo = (FolderInfo) item;
146 mLauncher.removeFolder(folderInfo);
147 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
148 } else if (isWorkspaceWidget(d)) {
149 // Remove the widget from the workspace
150 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
151 LauncherModel.deleteItemFromDatabase(mLauncher, item);
152
153 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
154 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
155 if (appWidgetHost != null) {
156 // Deleting an app widget ID is a void call but writes to disk before returning
157 // to the caller...
158 new Thread("deleteAppWidgetId") {
159 public void run() {
160 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
161 }
162 }.start();
163 }
164 }
165 }
166}