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