blob: 1122d77a52b6449e036f6475a4232bd5255269d1 [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() {
Winson Chung7a1d1652011-03-18 15:56:01 -070046 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
47 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
48 final Workspace workspace = (Workspace) cellLayout.getParent();
49 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -070050 }
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Michael Jurkac9a96192010-11-01 11:52:08 -070053 UserFolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
55 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
56
57 final Resources resources = launcher.getResources();
Michael Jurkac9a96192010-11-01 11:52:08 -070058 Drawable d = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 icon.mCloseIcon = d;
Michael Jurkac9a96192010-11-01 11:52:08 -070060 icon.mOpenIcon = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder_open);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 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,
Joe Onorato00acb122009-08-04 16:04:30 -040072 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 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
Adam Cohendf035382011-04-11 17:22:04 -070080 public void addItem(ShortcutInfo item) {
81 mInfo.add(item);
82 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);
83 }
84
Joe Onorato00acb122009-08-04 16:04:30 -040085 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
86 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080087 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 }
Michael Jurka66d72172011-04-12 16:29:25 -070094 item.cellX = -1;
95 item.cellY = -1;
Adam Cohendf035382011-04-11 17:22:04 -070096 addItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98
99 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400100 DragView dragView, Object dragInfo) {
Patrick Dubroy379f1602010-07-14 11:29:09 -0700101 if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
102 setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);
103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
105
106 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400107 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400111 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);
113 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700114
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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120}