blob: dd83c781acd89b8e29f4f59b103fa2d144adcd52 [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;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.ViewGroup;
25
Romain Guyedcce092010-03-04 13:03:17 -080026import com.android.launcher.R;
27
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028/**
29 * An icon that can appear on in the workspace representing an {@link UserFolder}.
30 */
Michael Jurkac66a7c22010-12-04 11:22:18 -080031public class FolderIcon extends BubbleTextView implements DropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032 private UserFolderInfo 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
Michael Jurka0280c3b2010-09-17 15:00:07 -070045 public boolean isDropEnabled() {
46 return !((Workspace)getParent().getParent()).isSmall();
47 }
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Michael Jurkac9a96192010-11-01 11:52:08 -070050 UserFolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
52 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
53
54 final Resources resources = launcher.getResources();
Michael Jurkac9a96192010-11-01 11:52:08 -070055 Drawable d = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 icon.mCloseIcon = d;
Michael Jurkac9a96192010-11-01 11:52:08 -070057 icon.mOpenIcon = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder_open);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
59 icon.setText(folderInfo.title);
60 icon.setTag(folderInfo);
61 icon.setOnClickListener(launcher);
62 icon.mInfo = folderInfo;
63 icon.mLauncher = launcher;
64
65 return icon;
66 }
67
68 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040069 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 final ItemInfo item = (ItemInfo) dragInfo;
71 final int itemType = item.itemType;
72 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
73 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
74 && item.container != mInfo.id;
75 }
76
Joe Onorato00acb122009-08-04 16:04:30 -040077 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
78 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 ShortcutInfo item;
80 if (dragInfo instanceof ApplicationInfo) {
81 // Came from all apps -- make a copy
82 item = ((ApplicationInfo)dragInfo).makeShortcut();
83 } else {
84 item = (ShortcutInfo)dragInfo;
85 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 mInfo.add(item);
87 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);
88 }
89
90 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040091 DragView dragView, Object dragInfo) {
Patrick Dubroy379f1602010-07-14 11:29:09 -070092 if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
93 setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);
94 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 }
96
97 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040098 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 }
100
101 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400102 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);
104 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700105
106 @Override
107 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
108 DragView dragView, Object dragInfo) {
109 return null;
110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111}