blob: b800b076f4f1ff28dd69889474172456277f5e63 [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
3import android.content.Context;
Michael Jurka66d72172011-04-12 16:29:25 -07004import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005import android.util.AttributeSet;
6import android.view.LayoutInflater;
7import android.view.View;
Michael Jurka66d72172011-04-12 16:29:25 -07008import android.widget.AdapterView;
9import android.widget.BaseAdapter;
10import android.widget.Button;
11import android.widget.GridView;
12import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080013
Romain Guyedcce092010-03-04 13:03:17 -080014import com.android.launcher.R;
Michael Jurka66d72172011-04-12 16:29:25 -070015import java.util.ArrayList;
Romain Guyedcce092010-03-04 13:03:17 -080016
The Android Open Source Project31dd5032009-03-03 19:32:27 -080017/**
18 * Folder which contains applications or shortcuts chosen by the user.
19 *
20 */
21public class UserFolder extends Folder implements DropTarget {
Joe Onorato2e5c4322009-10-06 12:34:42 -070022 private static final String TAG = "Launcher.UserFolder";
23
Michael Jurka66d72172011-04-12 16:29:25 -070024 protected CellLayout mContent;
25 private final LayoutInflater mInflater;
26 private final IconCache mIconCache;
27
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028 public UserFolder(Context context, AttributeSet attrs) {
29 super(context, attrs);
Michael Jurka66d72172011-04-12 16:29:25 -070030 mInflater = LayoutInflater.from(context);
31 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
32 }
33
34 @Override
35 protected void onFinishInflate() {
36 super.onFinishInflate();
37
38 mContent = (CellLayout) findViewById(R.id.folder_content);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 }
40
41 /**
42 * Creates a new UserFolder, inflated from R.layout.user_folder.
43 *
44 * @param context The application's context.
45 *
46 * @return A new UserFolder.
47 */
48 static UserFolder fromXml(Context context) {
49 return (UserFolder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
50 }
51
Michael Jurka66d72172011-04-12 16:29:25 -070052 @Override
53 void notifyDataSetChanged() {
54 // recreate all the children if the data set changes under us. We may want to do this more
55 // intelligently (ie just removing the views that should no longer exist)
56 mContent.removeAllViewsInLayout();
57 bind(mInfo);
58 }
59
60 public void onClick(View v) {
61 Object tag = v.getTag();
62 if (tag instanceof ShortcutInfo) {
63 // refactor this code from Folder
64 ShortcutInfo item = (ShortcutInfo) tag;
65 int[] pos = new int[2];
66 v.getLocationOnScreen(pos);
67 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
68 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
69 mLauncher.startActivitySafely(item.intent, item);
70 } else {
71 super.onClick(v);
72 }
73 }
74
75 public boolean onLongClick(View v) {
76 Object tag = v.getTag();
77 if (tag instanceof ShortcutInfo) {
78 // refactor this code from Folder
79 ShortcutInfo item = (ShortcutInfo) tag;
80 if (!v.isInTouchMode()) {
81 return false;
82 }
83
84 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
85 mLauncher.closeFolder(this);
86 mDragItem = item;
87
88 return true;
89 } else {
90 return super.onLongClick(v);
91 }
92 }
93
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040095 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 final ItemInfo item = (ItemInfo) dragInfo;
97 final int itemType = item.itemType;
98 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
Joe Onorato2e5c4322009-10-06 12:34:42 -070099 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
100 && item.container != mInfo.id;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Joe Onorato00acb122009-08-04 16:04:30 -0400103 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
104 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800105 ShortcutInfo item;
106 if (dragInfo instanceof ApplicationInfo) {
Joe Onorato2e5c4322009-10-06 12:34:42 -0700107 // Came from all apps -- make a copy
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108 item = ((ApplicationInfo)dragInfo).makeShortcut();
Michael Jurka66d72172011-04-12 16:29:25 -0700109 item.spanX = 1;
110 item.spanY = 1;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800111 } else {
112 item = (ShortcutInfo)dragInfo;
Joe Onorato2e5c4322009-10-06 12:34:42 -0700113 }
Michael Jurka66d72172011-04-12 16:29:25 -0700114 findAndSetEmptyCells(item);
115 ((UserFolderInfo)mInfo).add(item);
116 createAndAddShortcut(item);
117 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
118 }
119
120 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
121 int[] emptyCell = new int[2];
122 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
123 item.cellX = emptyCell[0];
124 item.cellY = emptyCell[1];
125 LauncherModel.addOrMoveItemInDatabase(
126 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
127 return true;
128 } else {
129 return false;
130 }
131 }
132
133 protected void createAndAddShortcut(ShortcutInfo item) {
134 final TextView textView =
135 (TextView) mInflater.inflate(R.layout.application_boxed, this, false);
136 textView.setCompoundDrawablesWithIntrinsicBounds(null,
137 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
138 textView.setText(item.title);
139 textView.setTag(item);
140
141 textView.setOnClickListener(this);
142 textView.setOnLongClickListener(this);
143
144 CellLayout.LayoutParams lp =
145 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
146 boolean insert = false;
147 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
149
Joe Onorato00acb122009-08-04 16:04:30 -0400150 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
151 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
Joe Onorato00acb122009-08-04 16:04:30 -0400154 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
155 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 }
157
Joe Onorato00acb122009-08-04 16:04:30 -0400158 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
159 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
162 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800163 public void onDropCompleted(View target, Object dragInfo, boolean success) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 if (success) {
Michael Jurka66d72172011-04-12 16:29:25 -0700165 ((UserFolderInfo)mInfo).remove(mDragItem);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 }
167 }
168
Michael Jurka0280c3b2010-09-17 15:00:07 -0700169 public boolean isDropEnabled() {
170 return true;
171 }
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 void bind(FolderInfo info) {
174 super.bind(info);
Michael Jurka66d72172011-04-12 16:29:25 -0700175 ArrayList<ShortcutInfo> children = ((UserFolderInfo)info).contents;
176 for (int i = 0; i < children.size(); i++) {
177 ShortcutInfo child = (ShortcutInfo) children.get(i);
178 if ((child.cellX == -1 && child.cellY == -1) ||
179 mContent.isOccupied(child.cellX, child.cellY)) {
180 findAndSetEmptyCells(child);
181 }
182 createAndAddShortcut((ShortcutInfo) children.get(i));
183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 }
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 @Override
187 void onOpen() {
188 super.onOpen();
Michael Jurka66d72172011-04-12 16:29:25 -0700189 // When the folder opens, we need to refresh the GridView's selection by
190 // forcing a layout
191 // TODO: find out if this is still necessary
192 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 requestFocus();
194 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700195
196 @Override
197 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
198 DragView dragView, Object dragInfo) {
199 return null;
200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201}