blob: 6e01bc0c4609129010c19dcdc385ea0c9d723def [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
19import android.content.Context;
Winson Chung201bc822011-06-20 15:41:53 -070020import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070021import android.content.res.Resources;
22import android.graphics.PorterDuff;
23import android.graphics.PorterDuffColorFilter;
Winson Chung967289b2011-06-30 18:09:30 -070024import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
Winson Chung61fa4192011-06-12 15:15:29 -070027import android.widget.TextView;
Winson Chung4c98d922011-05-31 16:50:48 -070028
29import com.android.launcher.R;
30
Winson Chung61fa4192011-06-12 15:15:29 -070031public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070032
Winson Chung61fa4192011-06-12 15:15:29 -070033 private TextView mText;
Winson Chung967289b2011-06-30 18:09:30 -070034 private TransitionDrawable mDrawable;
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 Chung967289b2011-06-30 18:09:30 -070057 mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0];
58 mDrawable.setCrossFadeEnabled(true);
Winson Chung201bc822011-06-20 15:41:53 -070059
60 // Remove the text in the Phone UI in landscape
61 int orientation = getResources().getConfiguration().orientation;
62 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
63 if (!LauncherApplication.isScreenLarge()) {
64 mText.setText("");
65 }
66 }
Winson Chung4c98d922011-05-31 16:50:48 -070067 }
68
69 private boolean isAllAppsApplication(DragSource source, Object info) {
70 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
71 }
72 private boolean isAllAppsWidget(DragSource source, Object info) {
73 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
74 }
75 private boolean isWorkspaceApplication(DragObject d) {
76 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof ShortcutInfo);
77 }
78 private boolean isWorkspaceWidget(DragObject d) {
79 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof LauncherAppWidgetInfo);
80 }
81 private boolean isWorkspaceFolder(DragObject d) {
82 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
83 }
84
85 @Override
86 public boolean acceptDrop(DragObject d) {
87 // We can remove everything including App shortcuts, folders, widgets, etc.
88 return true;
89 }
90
91 @Override
92 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -070093 boolean isVisible = true;
94 boolean isUninstall = false;
95
Winson Chungf0ea4d32011-06-06 14:27:16 -070096 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -070097 if (isAllAppsWidget(source, info)) {
98 isVisible = false;
99 }
100
101 // If we are dragging an application from AppsCustomize, only show the control if we can
102 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
103 if (isAllAppsApplication(source, info)) {
104 ApplicationInfo appInfo = (ApplicationInfo) info;
105 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
106 isUninstall = true;
107 } else {
108 isVisible = false;
109 }
110 }
111
112 mActive = isVisible;
Winson Chung61fa4192011-06-12 15:15:29 -0700113 setVisibility(isVisible ? View.VISIBLE : View.GONE);
114 if (mText.getText().length() > 0) {
115 mText.setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700116 : R.string.delete_target_label);
117 }
118 }
119
120 @Override
121 public void onDragEnd() {
122 super.onDragEnd();
123 mActive = false;
124 }
125
126 public void onDragEnter(DragObject d) {
127 super.onDragEnter(d);
128
Winson Chung967289b2011-06-30 18:09:30 -0700129 mDrawable.startTransition(mTransitionDuration);
Winson Chung4c98d922011-05-31 16:50:48 -0700130 }
131
132 public void onDragExit(DragObject d) {
133 super.onDragExit(d);
134
Winson Chung967289b2011-06-30 18:09:30 -0700135 mDrawable.resetTransition();
Winson Chung4c98d922011-05-31 16:50:48 -0700136 }
137
138 public void onDrop(DragObject d) {
139 ItemInfo item = (ItemInfo) d.dragInfo;
140
141 if (isAllAppsApplication(d.dragSource, item)) {
142 // Uninstall the application if it is being dragged from AppsCustomize
143 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
144 } else if (isWorkspaceApplication(d)) {
145 LauncherModel.deleteItemFromDatabase(mLauncher, item);
146 } else if (isWorkspaceFolder(d)) {
147 // Remove the folder from the workspace and delete the contents from launcher model
148 FolderInfo folderInfo = (FolderInfo) item;
149 mLauncher.removeFolder(folderInfo);
150 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
151 } else if (isWorkspaceWidget(d)) {
152 // Remove the widget from the workspace
153 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
154 LauncherModel.deleteItemFromDatabase(mLauncher, item);
155
156 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
157 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
158 if (appWidgetHost != null) {
159 // Deleting an app widget ID is a void call but writes to disk before returning
160 // to the caller...
161 new Thread("deleteAppWidgetId") {
162 public void run() {
163 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
164 }
165 }.start();
166 }
167 }
168 }
169}