FolderIcon.java revision df2cc41acbfacd576f99483a4af1cda32ebd3d09
1/*
2 * Copyright (C) 2008 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.Resources;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.ViewGroup;
25
26import com.android.launcher.R;
27
28/**
29 * An icon that can appear on in the workspace representing an {@link UserFolder}.
30 */
31public class FolderIcon extends BubbleTextView implements DropTarget {
32    private FolderInfo mInfo;
33    private Launcher mLauncher;
34    private Drawable mCloseIcon;
35    private Drawable mOpenIcon;
36
37    public FolderIcon(Context context, AttributeSet attrs) {
38        super(context, attrs);
39    }
40
41    public FolderIcon(Context context) {
42        super(context);
43    }
44
45    public boolean isDropEnabled() {
46        final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
47        final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
48        final Workspace workspace = (Workspace) cellLayout.getParent();
49        return !workspace.isSmall();
50    }
51
52    static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
53            FolderInfo folderInfo, IconCache iconCache) {
54
55        FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
56
57        final Resources resources = launcher.getResources();
58        Drawable d = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder);
59        icon.mCloseIcon = d;
60        icon.mOpenIcon = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder_open);
61        icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
62        icon.setText(folderInfo.title);
63        icon.setTag(folderInfo);
64        icon.setOnClickListener(launcher);
65        icon.mInfo = folderInfo;
66        icon.mLauncher = launcher;
67
68        return icon;
69    }
70
71    public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
72            DragView dragView, Object dragInfo) {
73        final ItemInfo item = (ItemInfo) dragInfo;
74        final int itemType = item.itemType;
75        return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
76                itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
77                && item.container != mInfo.id;
78    }
79
80    public void addItem(ShortcutInfo item) {
81        mInfo.add(item);
82        LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);
83    }
84
85    public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
86            DragView dragView, Object dragInfo) {
87        ShortcutInfo item;
88        if (dragInfo instanceof ApplicationInfo) {
89            // Came from all apps -- make a copy
90            item = ((ApplicationInfo)dragInfo).makeShortcut();
91        } else {
92            item = (ShortcutInfo)dragInfo;
93        }
94        item.cellX = -1;
95        item.cellY = -1;
96        addItem(item);
97    }
98
99    public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
100            DragView dragView, Object dragInfo) {
101        if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
102            setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);
103        }
104    }
105
106    public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
107            DragView dragView, Object dragInfo) {
108    }
109
110    public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
111            DragView dragView, Object dragInfo) {
112        setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);
113    }
114
115    @Override
116    public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
117            DragView dragView, Object dragInfo) {
118        return null;
119    }
120}
121