blob: 24b9ae241a3bba525a9e10762b6bbbf6af4da24d [file] [log] [blame]
Joe Onoratoa5902522009-07-30 13:37:37 -07001package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002
Adam Cohen66396872011-04-15 17:50:36 -07003import java.util.ArrayList;
4
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005import android.content.Context;
Michael Jurka66d72172011-04-12 16:29:25 -07006import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08007import android.util.AttributeSet;
8import android.view.LayoutInflater;
9import android.view.View;
Michael Jurka66d72172011-04-12 16:29:25 -070010import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080011
Romain Guyedcce092010-03-04 13:03:17 -080012import com.android.launcher.R;
13
The Android Open Source Project31dd5032009-03-03 19:32:27 -080014/**
15 * Folder which contains applications or shortcuts chosen by the user.
16 *
17 */
18public class UserFolder extends Folder implements DropTarget {
Joe Onorato2e5c4322009-10-06 12:34:42 -070019 private static final String TAG = "Launcher.UserFolder";
20
Michael Jurka66d72172011-04-12 16:29:25 -070021 protected CellLayout mContent;
22 private final LayoutInflater mInflater;
23 private final IconCache mIconCache;
24
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025 public UserFolder(Context context, AttributeSet attrs) {
26 super(context, attrs);
Michael Jurka66d72172011-04-12 16:29:25 -070027 mInflater = LayoutInflater.from(context);
28 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
29 }
30
31 @Override
32 protected void onFinishInflate() {
33 super.onFinishInflate();
34
35 mContent = (CellLayout) findViewById(R.id.folder_content);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 }
37
38 /**
39 * Creates a new UserFolder, inflated from R.layout.user_folder.
40 *
41 * @param context The application's context.
42 *
43 * @return A new UserFolder.
44 */
45 static UserFolder fromXml(Context context) {
46 return (UserFolder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
47 }
48
Michael Jurka66d72172011-04-12 16:29:25 -070049 @Override
50 void notifyDataSetChanged() {
51 // recreate all the children if the data set changes under us. We may want to do this more
52 // intelligently (ie just removing the views that should no longer exist)
53 mContent.removeAllViewsInLayout();
54 bind(mInfo);
55 }
56
57 public void onClick(View v) {
58 Object tag = v.getTag();
59 if (tag instanceof ShortcutInfo) {
60 // refactor this code from Folder
61 ShortcutInfo item = (ShortcutInfo) tag;
62 int[] pos = new int[2];
63 v.getLocationOnScreen(pos);
64 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
65 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
66 mLauncher.startActivitySafely(item.intent, item);
67 } else {
68 super.onClick(v);
69 }
70 }
71
72 public boolean onLongClick(View v) {
73 Object tag = v.getTag();
74 if (tag instanceof ShortcutInfo) {
75 // refactor this code from Folder
76 ShortcutInfo item = (ShortcutInfo) tag;
77 if (!v.isInTouchMode()) {
78 return false;
79 }
80
Adam Cohen66396872011-04-15 17:50:36 -070081 mLauncher.getWorkspace().onDragStartedWithItem(v);
Michael Jurka66d72172011-04-12 16:29:25 -070082 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
Adam Cohen66396872011-04-15 17:50:36 -070083
Michael Jurka66d72172011-04-12 16:29:25 -070084 mLauncher.closeFolder(this);
85 mDragItem = item;
86
87 return true;
88 } else {
89 return super.onLongClick(v);
90 }
91 }
92
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040094 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 final ItemInfo item = (ItemInfo) dragInfo;
96 final int itemType = item.itemType;
97 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
Joe Onorato2e5c4322009-10-06 12:34:42 -070098 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
99 && item.container != mInfo.id;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Joe Onorato00acb122009-08-04 16:04:30 -0400102 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
103 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104 ShortcutInfo item;
105 if (dragInfo instanceof ApplicationInfo) {
Joe Onorato2e5c4322009-10-06 12:34:42 -0700106 // Came from all apps -- make a copy
Joe Onorato0589f0f2010-02-08 13:44:00 -0800107 item = ((ApplicationInfo)dragInfo).makeShortcut();
Michael Jurka66d72172011-04-12 16:29:25 -0700108 item.spanX = 1;
109 item.spanY = 1;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800110 } else {
111 item = (ShortcutInfo)dragInfo;
Joe Onorato2e5c4322009-10-06 12:34:42 -0700112 }
Michael Jurka66d72172011-04-12 16:29:25 -0700113 findAndSetEmptyCells(item);
114 ((UserFolderInfo)mInfo).add(item);
115 createAndAddShortcut(item);
116 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
117 }
118
119 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
120 int[] emptyCell = new int[2];
121 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
122 item.cellX = emptyCell[0];
123 item.cellY = emptyCell[1];
124 LauncherModel.addOrMoveItemInDatabase(
125 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
126 return true;
127 } else {
128 return false;
129 }
130 }
131
132 protected void createAndAddShortcut(ShortcutInfo item) {
133 final TextView textView =
134 (TextView) mInflater.inflate(R.layout.application_boxed, this, false);
135 textView.setCompoundDrawablesWithIntrinsicBounds(null,
136 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
137 textView.setText(item.title);
138 textView.setTag(item);
139
140 textView.setOnClickListener(this);
141 textView.setOnLongClickListener(this);
142
143 CellLayout.LayoutParams lp =
144 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
145 boolean insert = false;
146 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
Joe Onorato00acb122009-08-04 16:04:30 -0400149 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
150 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
Joe Onorato00acb122009-08-04 16:04:30 -0400153 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
154 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 }
156
Joe Onorato00acb122009-08-04 16:04:30 -0400157 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
158 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
160
161 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800162 public void onDropCompleted(View target, Object dragInfo, boolean success) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 if (success) {
Michael Jurka66d72172011-04-12 16:29:25 -0700164 ((UserFolderInfo)mInfo).remove(mDragItem);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 }
166 }
167
Michael Jurka0280c3b2010-09-17 15:00:07 -0700168 public boolean isDropEnabled() {
169 return true;
170 }
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 void bind(FolderInfo info) {
173 super.bind(info);
Michael Jurka66d72172011-04-12 16:29:25 -0700174 ArrayList<ShortcutInfo> children = ((UserFolderInfo)info).contents;
175 for (int i = 0; i < children.size(); i++) {
176 ShortcutInfo child = (ShortcutInfo) children.get(i);
177 if ((child.cellX == -1 && child.cellY == -1) ||
178 mContent.isOccupied(child.cellX, child.cellY)) {
179 findAndSetEmptyCells(child);
180 }
181 createAndAddShortcut((ShortcutInfo) children.get(i));
182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 @Override
186 void onOpen() {
187 super.onOpen();
Michael Jurka66d72172011-04-12 16:29:25 -0700188 // When the folder opens, we need to refresh the GridView's selection by
189 // forcing a layout
190 // TODO: find out if this is still necessary
191 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 requestFocus();
193 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700194
195 @Override
196 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
197 DragView dragView, Object dragInfo) {
198 return null;
199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200}