blob: b770cd6d15e93d32ef72e3d04445720e001e8f05 [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;
Adam Cohena9cf38f2011-05-02 15:36:58 -070021import android.graphics.Canvas;
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;
Adam Cohena9cf38f2011-05-02 15:36:58 -070026import android.widget.FrameLayout;
27import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028
Romain Guyedcce092010-03-04 13:03:17 -080029import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070030import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080031
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
33 * An icon that can appear on in the workspace representing an {@link UserFolder}.
34 */
Adam Cohena9cf38f2011-05-02 15:36:58 -070035public class FolderIcon extends FrameLayout implements DropTarget, FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070037 Folder mFolder;
38 FolderInfo mInfo;
39
40 private static final int NUM_ITEMS_IN_PREVIEW = 4;
41 private static final float ICON_ANGLE = 15f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
43 public FolderIcon(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 }
46
47 public FolderIcon(Context context) {
48 super(context);
49 }
50
Michael Jurka0280c3b2010-09-17 15:00:07 -070051 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -070052 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
53 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
54 final Workspace workspace = (Workspace) cellLayout.getParent();
55 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -070056 }
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -070059 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
61 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
62
63 final Resources resources = launcher.getResources();
Adam Cohena9cf38f2011-05-02 15:36:58 -070064 Drawable d = iconCache.getFullResIcon(resources, R.drawable.folder_bg);
65 icon.setBackgroundDrawable(d);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 icon.setTag(folderInfo);
67 icon.setOnClickListener(launcher);
68 icon.mInfo = folderInfo;
69 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070070
71 Folder folder = Folder.fromXml(launcher);
72 folder.setDragController(launcher.getDragController());
73 folder.setLauncher(launcher);
74 folder.bind(folderInfo);
75 icon.mFolder = folder;
76
77 folderInfo.addListener(icon);
78
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 return icon;
80 }
81
82 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040083 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 final ItemInfo item = (ItemInfo) dragInfo;
85 final int itemType = item.itemType;
86 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
87 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
88 && item.container != mInfo.id;
89 }
90
Adam Cohendf035382011-04-11 17:22:04 -070091 public void addItem(ShortcutInfo item) {
92 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -070093 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -070094 }
95
Joe Onorato00acb122009-08-04 16:04:30 -040096 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
97 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080098 ShortcutInfo item;
99 if (dragInfo instanceof ApplicationInfo) {
100 // Came from all apps -- make a copy
101 item = ((ApplicationInfo)dragInfo).makeShortcut();
102 } else {
103 item = (ShortcutInfo)dragInfo;
104 }
Michael Jurka66d72172011-04-12 16:29:25 -0700105 item.cellX = -1;
106 item.cellY = -1;
Adam Cohendf035382011-04-11 17:22:04 -0700107 addItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
109
110 public void onDragEnter(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 }
113
114 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400115 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 }
117
118 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400119 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700121
122 @Override
123 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
124 DragView dragView, Object dragInfo) {
125 return null;
126 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700127
128 @Override
129 protected void onDraw(Canvas canvas) {
130 if (mFolder == null) return;
131 if (mFolder.getItemCount() == 0) return;
132
133 canvas.save();
134 TextView v = (TextView) mFolder.getItemAt(0);
135 Drawable d = v.getCompoundDrawables()[1];
136
137 canvas.translate( (getMeasuredWidth() - d.getIntrinsicWidth()) / 2,
138 (getMeasuredHeight() - d.getIntrinsicHeight()) / 2);
139
140 canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2);
141 canvas.rotate(ICON_ANGLE);
142
143 canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2);
144
145 for (int i = Math.max(0, mFolder.getItemCount() - NUM_ITEMS_IN_PREVIEW);
146 i < mFolder.getItemCount(); i++) {
147 v = (TextView) mFolder.getItemAt(i);
148 d = v.getCompoundDrawables()[1];
149
150 if (d != null) {
151 d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
152 d.draw(canvas);
153 }
154
155 canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2);
156 canvas.rotate(-ICON_ANGLE);
157 canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2);
158 }
159
160 canvas.restore();
161 }
162
163 public void onAdd(ShortcutInfo item) {
164 invalidate();
165 requestLayout();
166 }
167
168 public void onRemove(ShortcutInfo item) {
169 invalidate();
170 requestLayout();
171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172}