blob: a30c03e66e9ed02ab73915fe1febe5b16ceab3e0 [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;
Winson Chung201bc822011-06-20 15:41:53 -070021import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.res.Resources;
Winson Chung61fa4192011-06-12 15:15:29 -070023import android.graphics.Color;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.util.AttributeSet;
27import android.view.View;
28import android.view.ViewGroup;
Winson Chung61fa4192011-06-12 15:15:29 -070029import android.widget.TextView;
Winson Chung4c98d922011-05-31 16:50:48 -070030
31import com.android.launcher.R;
32
Winson Chung61fa4192011-06-12 15:15:29 -070033public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070034
Winson Chung61fa4192011-06-12 15:15:29 -070035 private TextView mText;
Winson Chung4c98d922011-05-31 16:50:48 -070036 private int mHoverColor = 0xFFFF0000;
37
38 public DeleteDropTarget(Context context, AttributeSet attrs) {
39 this(context, attrs, 0);
40 }
41
42 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
43 super(context, attrs, defStyle);
44 }
45
46 @Override
47 protected void onFinishInflate() {
48 super.onFinishInflate();
49
50 // Get the drawable
Winson Chung61fa4192011-06-12 15:15:29 -070051 mText = (TextView) findViewById(R.id.delete_target_text);
Winson Chung4c98d922011-05-31 16:50:48 -070052
53 // Get the hover color
54 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070055 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
56 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
57 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Winson Chung61fa4192011-06-12 15:15:29 -070058 setBackgroundColor(mHoverColor);
59 getBackground().setAlpha(0);
Winson Chung201bc822011-06-20 15:41:53 -070060
61 // Remove the text in the Phone UI in landscape
62 int orientation = getResources().getConfiguration().orientation;
63 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
64 if (!LauncherApplication.isScreenLarge()) {
65 mText.setText("");
66 }
67 }
Winson Chung4c98d922011-05-31 16:50:48 -070068 }
69
70 private boolean isAllAppsApplication(DragSource source, Object info) {
71 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
72 }
73 private boolean isAllAppsWidget(DragSource source, Object info) {
74 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
75 }
76 private boolean isWorkspaceApplication(DragObject d) {
77 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof ShortcutInfo);
78 }
79 private boolean isWorkspaceWidget(DragObject d) {
80 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof LauncherAppWidgetInfo);
81 }
82 private boolean isWorkspaceFolder(DragObject d) {
83 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
84 }
85
86 @Override
87 public boolean acceptDrop(DragObject d) {
88 // We can remove everything including App shortcuts, folders, widgets, etc.
89 return true;
90 }
91
92 @Override
93 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -070094 boolean isVisible = true;
95 boolean isUninstall = false;
96
Winson Chungf0ea4d32011-06-06 14:27:16 -070097 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -070098 if (isAllAppsWidget(source, info)) {
99 isVisible = false;
100 }
101
102 // If we are dragging an application from AppsCustomize, only show the control if we can
103 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
104 if (isAllAppsApplication(source, info)) {
105 ApplicationInfo appInfo = (ApplicationInfo) info;
106 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
107 isUninstall = true;
108 } else {
109 isVisible = false;
110 }
111 }
112
113 mActive = isVisible;
Winson Chung61fa4192011-06-12 15:15:29 -0700114 setVisibility(isVisible ? View.VISIBLE : View.GONE);
115 if (mText.getText().length() > 0) {
116 mText.setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700117 : R.string.delete_target_label);
118 }
119 }
120
121 @Override
122 public void onDragEnd() {
123 super.onDragEnd();
124 mActive = false;
125 }
126
127 public void onDragEnter(DragObject d) {
128 super.onDragEnter(d);
129
Winson Chung61fa4192011-06-12 15:15:29 -0700130 ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha",
131 Color.alpha(mHoverColor));
132 anim.setDuration(mTransitionDuration);
133 anim.start();
Winson Chung4c98d922011-05-31 16:50:48 -0700134 }
135
136 public void onDragExit(DragObject d) {
137 super.onDragExit(d);
138
Winson Chung61fa4192011-06-12 15:15:29 -0700139 ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", 0);
140 anim.setDuration(mTransitionDuration);
141 anim.start();
Winson Chung4c98d922011-05-31 16:50:48 -0700142 }
143
144 public void onDrop(DragObject d) {
145 ItemInfo item = (ItemInfo) d.dragInfo;
146
147 if (isAllAppsApplication(d.dragSource, item)) {
148 // Uninstall the application if it is being dragged from AppsCustomize
149 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
150 } else if (isWorkspaceApplication(d)) {
151 LauncherModel.deleteItemFromDatabase(mLauncher, item);
152 } else if (isWorkspaceFolder(d)) {
153 // Remove the folder from the workspace and delete the contents from launcher model
154 FolderInfo folderInfo = (FolderInfo) item;
155 mLauncher.removeFolder(folderInfo);
156 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
157 } else if (isWorkspaceWidget(d)) {
158 // Remove the widget from the workspace
159 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
160 LauncherModel.deleteItemFromDatabase(mLauncher, item);
161
162 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
163 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
164 if (appWidgetHost != null) {
165 // Deleting an app widget ID is a void call but writes to disk before returning
166 // to the caller...
167 new Thread("deleteAppWidgetId") {
168 public void run() {
169 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
170 }
171 }.start();
172 }
173 }
174 }
175}