DeleteDropTarget.java revision aaa530adc9defb8c2b1599cae27bb268d58b2ddb
1/*
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;
20import android.content.res.Configuration;
21import android.content.res.Resources;
22import android.graphics.PorterDuff;
23import android.graphics.PorterDuffColorFilter;
24import android.graphics.drawable.TransitionDrawable;
25import android.util.AttributeSet;
26import android.view.View;
27import android.widget.TextView;
28
29import com.android.launcher.R;
30
31public class DeleteDropTarget extends ButtonDropTarget {
32
33    private TextView mText;
34    private TransitionDrawable mDrawable;
35    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
50        mText = (TextView) findViewById(R.id.delete_target_text);
51
52        // Get the hover color
53        Resources r = getResources();
54        mHoverColor = r.getColor(R.color.delete_target_hover_tint);
55        mHoverPaint.setColorFilter(new PorterDuffColorFilter(
56                mHoverColor, PorterDuff.Mode.SRC_ATOP));
57        mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0];
58        mDrawable.setCrossFadeEnabled(true);
59
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        }
67    }
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 isDragSourceWorkspaceOrFolder(DragObject d) {
76        return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
77    }
78    private boolean isWorkspaceOrFolderApplication(DragObject d) {
79        return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
80    }
81    private boolean isWorkspaceOrFolderWidget(DragObject d) {
82        return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
83    }
84    private boolean isWorkspaceFolder(DragObject d) {
85        return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
86    }
87
88    @Override
89    public boolean acceptDrop(DragObject d) {
90        // We can remove everything including App shortcuts, folders, widgets, etc.
91        return true;
92    }
93
94    @Override
95    public void onDragStart(DragSource source, Object info, int dragAction) {
96        boolean isVisible = true;
97        boolean isUninstall = false;
98
99        // If we are dragging a widget from AppsCustomize, hide the delete target
100        if (isAllAppsWidget(source, info)) {
101            isVisible = false;
102        }
103
104        // If we are dragging an application from AppsCustomize, only show the control if we can
105        // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
106        if (isAllAppsApplication(source, info)) {
107            ApplicationInfo appInfo = (ApplicationInfo) info;
108            if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
109                isUninstall = true;
110            } else {
111                isVisible = false;
112            }
113        }
114
115        mActive = isVisible;
116        mDrawable.resetTransition();
117        setVisibility(isVisible ? View.VISIBLE : View.GONE);
118        if (mText.getText().length() > 0) {
119            mText.setText(isUninstall ? R.string.delete_target_uninstall_label
120                : R.string.delete_target_label);
121        }
122    }
123
124    @Override
125    public void onDragEnd() {
126        super.onDragEnd();
127        mActive = false;
128    }
129
130    public void onDragEnter(DragObject d) {
131        super.onDragEnter(d);
132
133        mDrawable.startTransition(mTransitionDuration);
134    }
135
136    public void onDragExit(DragObject d) {
137        super.onDragExit(d);
138
139        if (!d.dragComplete) {
140            mDrawable.resetTransition();
141        }
142    }
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 (isWorkspaceOrFolderApplication(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 (isWorkspaceOrFolderWidget(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}
176