blob: 86c099edab86ae026e6388482cebb80d7a5070e1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.content.Context;
20import android.content.res.Resources;
Jeff Sharkey70864282009-04-07 21:08:40 -070021import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.view.LayoutInflater;
25import android.view.ViewGroup;
26
Romain Guyedcce092010-03-04 13:03:17 -080027import com.android.launcher.R;
28
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029/**
30 * An icon that can appear on in the workspace representing an {@link UserFolder}.
31 */
Michael Jurkac66a7c22010-12-04 11:22:18 -080032public class FolderIcon extends BubbleTextView implements DropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033 private UserFolderInfo mInfo;
34 private Launcher mLauncher;
35 private Drawable mCloseIcon;
36 private Drawable mOpenIcon;
37
38 public FolderIcon(Context context, AttributeSet attrs) {
39 super(context, attrs);
40 }
41
42 public FolderIcon(Context context) {
43 super(context);
44 }
45
Michael Jurka0280c3b2010-09-17 15:00:07 -070046 public boolean isDropEnabled() {
47 return !((Workspace)getParent().getParent()).isSmall();
48 }
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Michael Jurkac9a96192010-11-01 11:52:08 -070051 UserFolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
53 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
54
55 final Resources resources = launcher.getResources();
Michael Jurkac9a96192010-11-01 11:52:08 -070056 Drawable d = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 icon.mCloseIcon = d;
Michael Jurkac9a96192010-11-01 11:52:08 -070058 icon.mOpenIcon = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder_open);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
60 icon.setText(folderInfo.title);
61 icon.setTag(folderInfo);
62 icon.setOnClickListener(launcher);
63 icon.mInfo = folderInfo;
64 icon.mLauncher = launcher;
65
66 return icon;
67 }
68
69 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040070 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 final ItemInfo item = (ItemInfo) dragInfo;
72 final int itemType = item.itemType;
73 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
74 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
75 && item.container != mInfo.id;
76 }
77
Joe Onorato00acb122009-08-04 16:04:30 -040078 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
79 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080080 ShortcutInfo item;
81 if (dragInfo instanceof ApplicationInfo) {
82 // Came from all apps -- make a copy
83 item = ((ApplicationInfo)dragInfo).makeShortcut();
84 } else {
85 item = (ShortcutInfo)dragInfo;
86 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 mInfo.add(item);
88 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);
89 }
90
91 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040092 DragView dragView, Object dragInfo) {
Patrick Dubroy379f1602010-07-14 11:29:09 -070093 if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
94 setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);
95 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 }
97
98 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040099 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 }
101
102 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400103 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);
105 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700106
107 @Override
108 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
109 DragView dragView, Object dragInfo) {
110 return null;
111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112}