Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 1 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 5 | import android.content.Context; |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 6 | import android.graphics.Rect; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 7 | import android.util.AttributeSet; |
| 8 | import android.view.LayoutInflater; |
| 9 | import android.view.View; |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 10 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 11 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 12 | import com.android.launcher.R; |
| 13 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 14 | /** |
| 15 | * Folder which contains applications or shortcuts chosen by the user. |
| 16 | * |
| 17 | */ |
| 18 | public class UserFolder extends Folder implements DropTarget { |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 19 | private static final String TAG = "Launcher.UserFolder"; |
| 20 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 21 | protected CellLayout mContent; |
| 22 | private final LayoutInflater mInflater; |
| 23 | private final IconCache mIconCache; |
| 24 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | public UserFolder(Context context, AttributeSet attrs) { |
| 26 | super(context, attrs); |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 27 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | } |
| 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 Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 49 | @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 Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 81 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 82 | mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY); |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 83 | |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 84 | mLauncher.closeFolder(this); |
| 85 | mDragItem = item; |
| 86 | |
| 87 | return true; |
| 88 | } else { |
| 89 | return super.onLongClick(v); |
| 90 | } |
| 91 | } |
| 92 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 94 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 95 | final ItemInfo item = (ItemInfo) dragInfo; |
| 96 | final int itemType = item.itemType; |
| 97 | return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 98 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) |
| 99 | && item.container != mInfo.id; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 102 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 103 | DragView dragView, Object dragInfo) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 104 | ShortcutInfo item; |
| 105 | if (dragInfo instanceof ApplicationInfo) { |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 106 | // Came from all apps -- make a copy |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 107 | item = ((ApplicationInfo)dragInfo).makeShortcut(); |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 108 | item.spanX = 1; |
| 109 | item.spanY = 1; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 110 | } else { |
| 111 | item = (ShortcutInfo)dragInfo; |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 112 | } |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 113 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 149 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 150 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 153 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 154 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 157 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 158 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 162 | public void onDropCompleted(View target, Object dragInfo, boolean success) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 163 | if (success) { |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 164 | ((UserFolderInfo)mInfo).remove(mDragItem); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 168 | public boolean isDropEnabled() { |
| 169 | return true; |
| 170 | } |
| 171 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | void bind(FolderInfo info) { |
| 173 | super.bind(info); |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 174 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 183 | } |
| 184 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 185 | @Override |
| 186 | void onOpen() { |
| 187 | super.onOpen(); |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 188 | // 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 192 | requestFocus(); |
| 193 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 194 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 200 | } |