blob: 4c8d03a4a2f1c5f927dc400ca38e19c4ae03411c [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
Adam Cohendf2cc412011-04-27 16:56:57 -070019import java.util.ArrayList;
20
21import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
23import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
25import android.animation.PropertyValuesHolder;
26import android.animation.ValueAnimator;
27import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.Context;
Romain Guyfb5411e2010-02-24 10:04:17 -080029import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.util.AttributeSet;
Adam Cohendf2cc412011-04-27 16:56:57 -070031import android.view.LayoutInflater;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.AdapterView;
35import android.widget.Button;
36import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070037import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.widget.AdapterView.OnItemClickListener;
39import android.widget.AdapterView.OnItemLongClickListener;
40
Romain Guyedcce092010-03-04 13:03:17 -080041import com.android.launcher.R;
42
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043/**
44 * Represents a set of icons chosen by the user or generated by the system.
45 */
46public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
Adam Cohendf2cc412011-04-27 16:56:57 -070047 OnItemClickListener, OnClickListener, View.OnLongClickListener, DropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Joe Onorato00acb122009-08-04 16:04:30 -040049 protected DragController mDragController;
Adam Cohendf2cc412011-04-27 16:56:57 -070050
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 protected Launcher mLauncher;
52
53 protected Button mCloseButton;
Adam Cohendf2cc412011-04-27 16:56:57 -070054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 protected FolderInfo mInfo;
56
57 /**
58 * Which item is being dragged
59 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080060 protected ShortcutInfo mDragItem;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
64 static final int STATE_NONE = -1;
65 static final int STATE_SMALL = 0;
66 static final int STATE_ANIMATING = 1;
67 static final int STATE_OPEN = 2;
68
69 private int mExpandDuration;
70 protected CellLayout mContent;
71 private final LayoutInflater mInflater;
72 private final IconCache mIconCache;
73 private int mState = STATE_NONE;
74
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 /**
76 * Used to inflate the Workspace from XML.
77 *
78 * @param context The application's context.
79 * @param attrs The attribtues set containing the Workspace's customization values.
80 */
81 public Folder(Context context, AttributeSet attrs) {
82 super(context, attrs);
83 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -070084 mInflater = LayoutInflater.from(context);
85 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
86 mExpandDuration = getResources().getInteger(R.integer.config_folderAnimDuration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 }
88
89 @Override
90 protected void onFinishInflate() {
91 super.onFinishInflate();
92
Joe Onorato92442302009-09-21 15:37:59 -040093 mCloseButton = (Button) findViewById(R.id.folder_close);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 mCloseButton.setOnClickListener(this);
95 mCloseButton.setOnLongClickListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -070096 mContent = (CellLayout) findViewById(R.id.folder_content);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 }
98
99 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
Romain Guyfb5411e2010-02-24 10:04:17 -0800101 int[] pos = new int[2];
102 v.getLocationOnScreen(pos);
103 app.intent.setSourceBounds(new Rect(pos[0], pos[1],
104 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -0700105 mLauncher.startActivitySafely(app.intent, app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
108 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700109 Object tag = v.getTag();
110 if (tag instanceof ShortcutInfo) {
111 // refactor this code from Folder
112 ShortcutInfo item = (ShortcutInfo) tag;
113 int[] pos = new int[2];
114 v.getLocationOnScreen(pos);
115 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
116 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
117 mLauncher.startActivitySafely(item.intent, item);
118 } else {
119 mLauncher.closeFolder(this);
120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 }
122
123 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700124 Object tag = v.getTag();
125 if (tag instanceof ShortcutInfo) {
126 // refactor this code from Folder
127 ShortcutInfo item = (ShortcutInfo) tag;
128 if (!v.isInTouchMode()) {
129 return false;
130 }
131
132 mLauncher.getWorkspace().onDragStartedWithItem(v);
133 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
134
135 mLauncher.closeFolder(this);
136 mDragItem = item;
137 } else {
138 mLauncher.closeFolder(this);
139 mLauncher.showRenameDialog(mInfo);
140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 return true;
142 }
143
144 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
145 if (!view.isInTouchMode()) {
146 return false;
147 }
148
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Joe Onorato00acb122009-08-04 16:04:30 -0400151 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 mLauncher.closeFolder(this);
153 mDragItem = app;
154
155 return true;
156 }
157
Joe Onorato00acb122009-08-04 16:04:30 -0400158 public void setDragController(DragController dragController) {
159 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
Patrick Dubroya669d792010-11-23 14:40:33 -0800162 public void onDragViewVisible() {
163 }
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 void setLauncher(Launcher launcher) {
166 mLauncher = launcher;
167 }
168
169 /**
170 * @return the FolderInfo object associated with this folder
171 */
172 FolderInfo getInfo() {
173 return mInfo;
174 }
175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700177 // When the folder opens, we need to refresh the GridView's selection by
178 // forcing a layout
179 // TODO: find out if this is still necessary
180 mContent.requestLayout();
181 requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
183
184 void onClose() {
185 final Workspace workspace = mLauncher.getWorkspace();
Michael Jurka0142d492010-08-25 17:46:15 -0700186 workspace.getChildAt(workspace.getCurrentPage()).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
189 void bind(FolderInfo info) {
190 mInfo = info;
191 mCloseButton.setText(info.title);
Adam Cohendf2cc412011-04-27 16:56:57 -0700192 ArrayList<ShortcutInfo> children = info.contents;
193 for (int i = 0; i < children.size(); i++) {
194 ShortcutInfo child = (ShortcutInfo) children.get(i);
195 if ((child.cellX == -1 && child.cellY == -1) ||
196 mContent.isOccupied(child.cellX, child.cellY)) {
197 findAndSetEmptyCells(child);
198 }
199 createAndAddShortcut((ShortcutInfo) children.get(i));
200 }
201 }
202
203 /**
204 * Creates a new UserFolder, inflated from R.layout.user_folder.
205 *
206 * @param context The application's context.
207 *
208 * @return A new UserFolder.
209 */
210 static Folder fromXml(Context context) {
211 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
212 }
213
214 /**
215 * This method is intended to make the UserFolder to be visually identical in size and position
216 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
217 */
218 private void positionAndSizeAsIcon() {
219 if (!(getParent() instanceof CellLayoutChildren)) return;
220
221 CellLayoutChildren clc = (CellLayoutChildren) getParent();
222 CellLayout cellLayout = (CellLayout) clc.getParent();
223
224 FolderIcon fi = (FolderIcon) cellLayout.getChildAt(mInfo.cellX, mInfo.cellY);
225 CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) fi.getLayoutParams();
226 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
227
228 lp.width = iconLp.width;
229 lp.height = iconLp.height;
230 lp.x = iconLp.x;
231 lp.y = iconLp.y;
232
233 mContent.setAlpha(0f);
234 mState = STATE_SMALL;
235 }
236
237 public void animateOpen() {
238 if (mState != STATE_SMALL) {
239 positionAndSizeAsIcon();
240 }
241 if (!(getParent() instanceof CellLayoutChildren)) return;
242
243 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
244
245 CellLayoutChildren clc = (CellLayoutChildren) getParent();
246 CellLayout cellLayout = (CellLayout) clc.getParent();
247 Rect r = cellLayout.getContentRect(null);
248
249 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", r.width());
250 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", r.height());
251 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", 0);
252 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", 0);
253
254 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
255 oa.addUpdateListener(new AnimatorUpdateListener() {
256 public void onAnimationUpdate(ValueAnimator animation) {
257 requestLayout();
258 }
259 });
260
261 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
262 ObjectAnimator oaContentAlpha = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
263
264 AnimatorSet set = new AnimatorSet();
265 set.playTogether(oa, oaContentAlpha);
266 set.setDuration(mExpandDuration);
267 set.addListener(new AnimatorListenerAdapter() {
268 @Override
269 public void onAnimationStart(Animator animation) {
270 mState = STATE_ANIMATING;
271 }
272 @Override
273 public void onAnimationEnd(Animator animation) {
274 mState = STATE_SMALL;
275 }
276 });
277 set.start();
278 }
279
280 public void animateClosed() {
281 if (!(getParent() instanceof CellLayoutChildren)) return;
282
283 CellLayoutChildren clc = (CellLayoutChildren) getParent();
284 final CellLayout cellLayout = (CellLayout) clc.getParent();
285
286 FolderIcon fi = (FolderIcon) cellLayout.getChildAt(mInfo.cellX, mInfo.cellY);
287 CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) fi.getLayoutParams();
288 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
289
290 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width);
291 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height);
292 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x);
293 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y);
294
295 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
296 oa.addUpdateListener(new AnimatorUpdateListener() {
297 public void onAnimationUpdate(ValueAnimator animation) {
298 requestLayout();
299 }
300 });
301
302 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
303 ObjectAnimator oaContentAlpha = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
304
305 AnimatorSet set = new AnimatorSet();
306 set.playTogether(oa, oaContentAlpha);
307 set.setDuration(mExpandDuration);
308
309 set.addListener(new AnimatorListenerAdapter() {
310 @Override
311 public void onAnimationEnd(Animator animation) {
312 cellLayout.removeViewWithoutMarkingCells(Folder.this);
313 mState = STATE_OPEN;
314 }
315 @Override
316 public void onAnimationStart(Animator animation) {
317 mState = STATE_ANIMATING;
318 }
319 });
320 set.start();
321 }
322
323 void notifyDataSetChanged() {
324 // recreate all the children if the data set changes under us. We may want to do this more
325 // intelligently (ie just removing the views that should no longer exist)
326 mContent.removeAllViewsInLayout();
327 bind(mInfo);
328 }
329
330 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
331 DragView dragView, Object dragInfo) {
332 final ItemInfo item = (ItemInfo) dragInfo;
333 final int itemType = item.itemType;
334 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
335 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
336 && item.container != mInfo.id;
337 }
338
339 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
340 DragView dragView, Object dragInfo) {
341 ShortcutInfo item;
342 if (dragInfo instanceof ApplicationInfo) {
343 // Came from all apps -- make a copy
344 item = ((ApplicationInfo)dragInfo).makeShortcut();
345 item.spanX = 1;
346 item.spanY = 1;
347 } else {
348 item = (ShortcutInfo)dragInfo;
349 }
350 findAndSetEmptyCells(item);
351 mInfo.add(item);
352 createAndAddShortcut(item);
353 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
354 }
355
356 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
357 int[] emptyCell = new int[2];
358 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
359 item.cellX = emptyCell[0];
360 item.cellY = emptyCell[1];
361 LauncherModel.addOrMoveItemInDatabase(
362 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
363 return true;
364 } else {
365 return false;
366 }
367 }
368
369 protected void createAndAddShortcut(ShortcutInfo item) {
370 final TextView textView =
371 (TextView) mInflater.inflate(R.layout.application_boxed, this, false);
372 textView.setCompoundDrawablesWithIntrinsicBounds(null,
373 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
374 textView.setText(item.title);
375 textView.setTag(item);
376
377 textView.setOnClickListener(this);
378 textView.setOnLongClickListener(this);
379
380 CellLayout.LayoutParams lp =
381 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
382 boolean insert = false;
383 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
384 }
385
386 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
387 DragView dragView, Object dragInfo) {
388 }
389
390 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
391 DragView dragView, Object dragInfo) {
392 }
393
394 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
395 DragView dragView, Object dragInfo) {
396 }
397
398 public void onDropCompleted(View target, Object dragInfo, boolean success) {
399 if (success) {
400 mInfo.remove(mDragItem);
401 }
402 }
403
404 public boolean isDropEnabled() {
405 return true;
406 }
407
408 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
409 DragView dragView, Object dragInfo) {
410 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412}