blob: bbc3409afd49781d8620c41cf211b59364c5e474 [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
Joe Onorato00acb122009-08-04 16:04:30 -040080 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
81 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080082 ShortcutInfo item;
83 if (dragInfo instanceof ApplicationInfo) {
84 // Came from all apps -- make a copy
85 item = ((ApplicationInfo)dragInfo).makeShortcut();
86 } else {
87 item = (ShortcutInfo)dragInfo;
88 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 mInfo.add(item);
90 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);
91 }
92
93 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040094 DragView dragView, Object dragInfo) {
Patrick Dubroy379f1602010-07-14 11:29:09 -070095 if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
96 setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);
97 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 }
99
100 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400101 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
103
104 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400105 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);
107 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700108
109 @Override
110 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
111 DragView dragView, Object dragInfo) {
112 return null;
113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114}