blob: 14b77f0375835792c21a652d52302d2731e5e897 [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
Dianne Hackborn8f573952009-08-10 23:21:09 -070019import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
21import android.content.Intent;
22import android.content.ComponentName;
23import android.content.res.TypedArray;
Joe Onorato9c1289c2009-08-17 11:03:03 -040024import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.RectF;
27import android.graphics.Rect;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070028import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.util.AttributeSet;
Joe Onorato7c312c12009-08-13 21:36:53 -070030import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
35import android.view.ViewGroup;
36import android.view.ViewParent;
37import android.widget.Scroller;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070038import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Parcelable;
40import android.os.Parcel;
41
42import java.util.ArrayList;
43
44/**
45 * The workspace is a wide area with a wallpaper and a finite number of screens. Each
46 * screen contains a number of icons, folders or widgets the user can interact with.
47 * A workspace is meant to be used with a fixed width only.
48 */
Joe Onorato85a02a82009-09-08 12:34:22 -070049public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller {
50 private static final String TAG = "Launcher.Workspace";
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 private static final int INVALID_SCREEN = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -070053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 /**
55 * The velocity at which a fling gesture will cause us to snap to the next screen
56 */
57 private static final int SNAP_VELOCITY = 1000;
58
Dianne Hackborn8f573952009-08-10 23:21:09 -070059 private final WallpaperManager mWallpaperManager;
60
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mDefaultScreen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 private boolean mFirstLayout = true;
64
65 private int mCurrentScreen;
66 private int mNextScreen = INVALID_SCREEN;
67 private Scroller mScroller;
68 private VelocityTracker mVelocityTracker;
69
70 /**
71 * CellInfo for the cell that is currently being dragged
72 */
73 private CellLayout.CellInfo mDragInfo;
Jeff Sharkey70864282009-04-07 21:08:40 -070074
75 /**
76 * Target drop area calculated during last acceptDrop call.
77 */
78 private int[] mTargetCell = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 private float mLastMotionX;
81 private float mLastMotionY;
82
83 private final static int TOUCH_STATE_REST = 0;
84 private final static int TOUCH_STATE_SCROLLING = 1;
85
86 private int mTouchState = TOUCH_STATE_REST;
87
88 private OnLongClickListener mLongClickListener;
89
90 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040091 private DragController mDragController;
Jeff Sharkey70864282009-04-07 21:08:40 -070092
93 /**
94 * Cache of vacant cells, used during drag events and invalidated as needed.
95 */
96 private CellLayout.CellInfo mVacantCache = null;
97
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 private int[] mTempCell = new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -070099 private int[] mTempEstimate = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
101 private boolean mAllowLongPress;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private int mTouchSlop;
Romain Guya206daa2009-07-06 11:51:18 -0700104 private int mMaximumVelocity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
106 final Rect mDrawerBounds = new Rect();
107 final Rect mClipBounds = new Rect();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 int mDrawerContentHeight;
109 int mDrawerContentWidth;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 /**
112 * Used to inflate the Workspace from XML.
113 *
114 * @param context The application's context.
115 * @param attrs The attribtues set containing the Workspace's customization values.
116 */
117 public Workspace(Context context, AttributeSet attrs) {
118 this(context, attrs, 0);
119 }
120
121 /**
122 * Used to inflate the Workspace from XML.
123 *
124 * @param context The application's context.
125 * @param attrs The attribtues set containing the Workspace's customization values.
126 * @param defStyle Unused.
127 */
128 public Workspace(Context context, AttributeSet attrs, int defStyle) {
129 super(context, attrs, defStyle);
130
Dianne Hackborn8f573952009-08-10 23:21:09 -0700131 mWallpaperManager = WallpaperManager.getInstance(context);
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);
134 mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);
135 a.recycle();
136
137 initWorkspace();
138 }
139
140 /**
141 * Initializes various states for this workspace.
142 */
143 private void initWorkspace() {
144 mScroller = new Scroller(getContext());
145 mCurrentScreen = mDefaultScreen;
146 Launcher.setScreen(mCurrentScreen);
147
Romain Guya206daa2009-07-06 11:51:18 -0700148 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
149 mTouchSlop = configuration.getScaledTouchSlop();
150 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 @Override
154 public void addView(View child, int index, LayoutParams params) {
155 if (!(child instanceof CellLayout)) {
156 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
157 }
158 super.addView(child, index, params);
159 }
160
161 @Override
162 public void addView(View child) {
163 if (!(child instanceof CellLayout)) {
164 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
165 }
166 super.addView(child);
167 }
168
169 @Override
170 public void addView(View child, int index) {
171 if (!(child instanceof CellLayout)) {
172 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
173 }
174 super.addView(child, index);
175 }
176
177 @Override
178 public void addView(View child, int width, int height) {
179 if (!(child instanceof CellLayout)) {
180 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
181 }
182 super.addView(child, width, height);
183 }
184
185 @Override
186 public void addView(View child, LayoutParams params) {
187 if (!(child instanceof CellLayout)) {
188 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
189 }
190 super.addView(child, params);
191 }
192
193 /**
194 * @return The open folder on the current screen, or null if there is none
195 */
196 Folder getOpenFolder() {
197 CellLayout currentScreen = (CellLayout) getChildAt(mCurrentScreen);
198 int count = currentScreen.getChildCount();
199 for (int i = 0; i < count; i++) {
200 View child = currentScreen.getChildAt(i);
201 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
202 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
203 return (Folder) child;
204 }
205 }
206 return null;
207 }
208
209 ArrayList<Folder> getOpenFolders() {
210 final int screens = getChildCount();
211 ArrayList<Folder> folders = new ArrayList<Folder>(screens);
212
213 for (int screen = 0; screen < screens; screen++) {
214 CellLayout currentScreen = (CellLayout) getChildAt(screen);
215 int count = currentScreen.getChildCount();
216 for (int i = 0; i < count; i++) {
217 View child = currentScreen.getChildAt(i);
218 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
219 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
220 folders.add((Folder) child);
221 break;
222 }
223 }
224 }
225
226 return folders;
227 }
228
229 boolean isDefaultScreenShowing() {
230 return mCurrentScreen == mDefaultScreen;
231 }
232
233 /**
234 * Returns the index of the currently displayed screen.
235 *
236 * @return The index of the currently displayed screen.
237 */
238 int getCurrentScreen() {
239 return mCurrentScreen;
240 }
241
242 /**
Joe Onorato59791172009-07-31 16:21:40 -0700243 * Returns how many screens there are.
244 */
245 int getScreenCount() {
246 return getChildCount();
247 }
248
249 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 * Computes a bounding rectangle for a range of cells
251 *
252 * @param cellX X coordinate of upper left corner expressed as a cell position
253 * @param cellY Y coordinate of upper left corner expressed as a cell position
254 * @param cellHSpan Width in cells
255 * @param cellVSpan Height in cells
256 * @param rect Rectnagle into which to put the results
257 */
258 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF rect) {
259 ((CellLayout)getChildAt(mCurrentScreen)).cellToRect(cellX, cellY,
260 cellHSpan, cellVSpan, rect);
261 }
262
263 /**
264 * Sets the current screen.
265 *
266 * @param currentScreen
267 */
268 void setCurrentScreen(int currentScreen) {
Romain Guy4c58c482009-05-12 17:35:41 -0700269 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
271 scrollTo(mCurrentScreen * getWidth(), 0);
Romain Guy798300c2009-08-11 14:43:29 -0700272 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 invalidate();
274 }
275
276 /**
277 * Shows the default screen (defined by the firstScreen attribute in XML.)
278 */
279 void showDefaultScreen() {
280 setCurrentScreen(mDefaultScreen);
281 }
282
283 /**
284 * Adds the specified child in the current screen. The position and dimension of
285 * the child are defined by x, y, spanX and spanY.
286 *
287 * @param child The child to add in one of the workspace's screens.
288 * @param x The X position of the child in the screen's grid.
289 * @param y The Y position of the child in the screen's grid.
290 * @param spanX The number of cells spanned horizontally by the child.
291 * @param spanY The number of cells spanned vertically by the child.
292 */
293 void addInCurrentScreen(View child, int x, int y, int spanX, int spanY) {
294 addInScreen(child, mCurrentScreen, x, y, spanX, spanY, false);
295 }
296
297 /**
298 * Adds the specified child in the current screen. The position and dimension of
299 * the child are defined by x, y, spanX and spanY.
300 *
301 * @param child The child to add in one of the workspace's screens.
302 * @param x The X position of the child in the screen's grid.
303 * @param y The Y position of the child in the screen's grid.
304 * @param spanX The number of cells spanned horizontally by the child.
305 * @param spanY The number of cells spanned vertically by the child.
306 * @param insert When true, the child is inserted at the beginning of the children list.
307 */
308 void addInCurrentScreen(View child, int x, int y, int spanX, int spanY, boolean insert) {
309 addInScreen(child, mCurrentScreen, x, y, spanX, spanY, insert);
310 }
311
312 /**
313 * Adds the specified child in the specified screen. The position and dimension of
314 * the child are defined by x, y, spanX and spanY.
315 *
316 * @param child The child to add in one of the workspace's screens.
317 * @param screen The screen in which to add the child.
318 * @param x The X position of the child in the screen's grid.
319 * @param y The Y position of the child in the screen's grid.
320 * @param spanX The number of cells spanned horizontally by the child.
321 * @param spanY The number of cells spanned vertically by the child.
322 */
323 void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) {
324 addInScreen(child, screen, x, y, spanX, spanY, false);
325 }
326
327 /**
328 * Adds the specified child in the specified screen. The position and dimension of
329 * the child are defined by x, y, spanX and spanY.
330 *
331 * @param child The child to add in one of the workspace's screens.
332 * @param screen The screen in which to add the child.
333 * @param x The X position of the child in the screen's grid.
334 * @param y The Y position of the child in the screen's grid.
335 * @param spanX The number of cells spanned horizontally by the child.
336 * @param spanY The number of cells spanned vertically by the child.
337 * @param insert When true, the child is inserted at the beginning of the children list.
338 */
339 void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) {
340 if (screen < 0 || screen >= getChildCount()) {
341 throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount());
342 }
343
Romain Guy4c58c482009-05-12 17:35:41 -0700344 clearVacantCache();
345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 final CellLayout group = (CellLayout) getChildAt(screen);
347 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
348 if (lp == null) {
349 lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
350 } else {
351 lp.cellX = x;
352 lp.cellY = y;
353 lp.cellHSpan = spanX;
354 lp.cellVSpan = spanY;
355 }
356 group.addView(child, insert ? 0 : -1, lp);
357 if (!(child instanceof Folder)) {
358 child.setOnLongClickListener(mLongClickListener);
359 }
Joe Onorato00acb122009-08-04 16:04:30 -0400360 if (child instanceof DropTarget) {
361 mDragController.addDropTarget((DropTarget)child);
362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
364
365 void addWidget(View view, Widget widget) {
366 addInScreen(view, widget.screen, widget.cellX, widget.cellY, widget.spanX,
367 widget.spanY, false);
368 }
369
370 void addWidget(View view, Widget widget, boolean insert) {
371 addInScreen(view, widget.screen, widget.cellX, widget.cellY, widget.spanX,
372 widget.spanY, insert);
373 }
374
375 CellLayout.CellInfo findAllVacantCells(boolean[] occupied) {
376 CellLayout group = (CellLayout) getChildAt(mCurrentScreen);
377 if (group != null) {
Jeff Sharkey70864282009-04-07 21:08:40 -0700378 return group.findAllVacantCells(occupied, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
380 return null;
381 }
382
Romain Guy4c58c482009-05-12 17:35:41 -0700383 private void clearVacantCache() {
384 if (mVacantCache != null) {
385 mVacantCache.clearVacantCells();
386 mVacantCache = null;
387 }
388 }
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 /**
391 * Returns the coordinate of a vacant cell for the current screen.
392 */
393 boolean getVacantCell(int[] vacant, int spanX, int spanY) {
394 CellLayout group = (CellLayout) getChildAt(mCurrentScreen);
395 if (group != null) {
396 return group.getVacantCell(vacant, spanX, spanY);
397 }
398 return false;
399 }
400
401 /**
402 * Adds the specified child in the current screen. The position and dimension of
403 * the child are defined by x, y, spanX and spanY.
404 *
405 * @param child The child to add in one of the workspace's screens.
406 * @param spanX The number of cells spanned horizontally by the child.
407 * @param spanY The number of cells spanned vertically by the child.
408 */
409 void fitInCurrentScreen(View child, int spanX, int spanY) {
410 fitInScreen(child, mCurrentScreen, spanX, spanY);
411 }
412
413 /**
414 * Adds the specified child in the specified screen. The position and dimension of
415 * the child are defined by x, y, spanX and spanY.
416 *
417 * @param child The child to add in one of the workspace's screens.
418 * @param screen The screen in which to add the child.
419 * @param spanX The number of cells spanned horizontally by the child.
420 * @param spanY The number of cells spanned vertically by the child.
421 */
422 void fitInScreen(View child, int screen, int spanX, int spanY) {
423 if (screen < 0 || screen >= getChildCount()) {
424 throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount());
425 }
426
427 final CellLayout group = (CellLayout) getChildAt(screen);
428 boolean vacant = group.getVacantCell(mTempCell, spanX, spanY);
429 if (vacant) {
430 group.addView(child,
431 new CellLayout.LayoutParams(mTempCell[0], mTempCell[1], spanX, spanY));
432 child.setOnLongClickListener(mLongClickListener);
433 if (!(child instanceof Folder)) {
434 child.setOnLongClickListener(mLongClickListener);
435 }
Joe Onorato00acb122009-08-04 16:04:30 -0400436 if (child instanceof DropTarget) {
437 mDragController.addDropTarget((DropTarget)child);
438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440 }
441
442 /**
443 * Registers the specified listener on each screen contained in this workspace.
444 *
445 * @param l The listener used to respond to long clicks.
446 */
447 @Override
448 public void setOnLongClickListener(OnLongClickListener l) {
449 mLongClickListener = l;
450 final int count = getChildCount();
451 for (int i = 0; i < count; i++) {
452 getChildAt(i).setOnLongClickListener(l);
453 }
454 }
455
Dianne Hackborn8f573952009-08-10 23:21:09 -0700456 private void updateWallpaperOffset() {
Romain Guy798300c2009-08-11 14:43:29 -0700457 updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
458 }
459
460 private void updateWallpaperOffset(int scrollRange) {
Dianne Hackborn49cdb1b2009-08-13 16:50:18 -0700461 mWallpaperManager.setWallpaperOffsets(getWindowToken(), mScrollX / (float) scrollRange, 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700462 }
463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 @Override
465 public void computeScroll() {
466 if (mScroller.computeScrollOffset()) {
467 mScrollX = mScroller.getCurrX();
468 mScrollY = mScroller.getCurrY();
Dianne Hackborn8f573952009-08-10 23:21:09 -0700469 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 postInvalidate();
471 } else if (mNextScreen != INVALID_SCREEN) {
472 mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
473 Launcher.setScreen(mCurrentScreen);
474 mNextScreen = INVALID_SCREEN;
475 clearChildrenCache();
476 }
477 }
478
479 @Override
480 protected void dispatchDraw(Canvas canvas) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700481 boolean restore = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700482 int restoreCount = 0;
483
484 // For the fade. If view gets setAlpha(), use that instead.
Joe Onorato85a02a82009-09-08 12:34:22 -0700485 float scale = mScale;
486 if (scale < 0.999f) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700487 int sx = mScrollX;
Joe Onorato85a02a82009-09-08 12:34:22 -0700488
489 int alpha = (scale < 0.5f) ? (int)(255 * 2 * scale) : 255;
490
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700491 restoreCount = canvas.saveLayerAlpha(sx, 0, sx+getWidth(), getHeight(), alpha,
492 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
493 restore = true;
Joe Onorato85a02a82009-09-08 12:34:22 -0700494
495 if (scale < 0.999f) {
496 int w = getWidth();
497 w += 2 * mCurrentScreen * w;
498 int h = getHeight();
499 canvas.translate(w/2, h/2);
500 canvas.scale(scale, scale);
501 canvas.translate(-w/2, -h/2);
502 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700503 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 // ViewGroup.dispatchDraw() supports many features we don't need:
506 // clip to padding, layout animation, animation listener, disappearing
507 // children, etc. The following implementation attempts to fast-track
508 // the drawing dispatch by drawing only what we know needs to be drawn.
509
Joe Onorato85a02a82009-09-08 12:34:22 -0700510 boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN
511 && scale > 0.999f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 // If we are not scrolling or flinging, draw only the current screen
513 if (fastDraw) {
514 drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime());
515 } else {
516 final long drawingTime = getDrawingTime();
517 // If we are flinging, draw only the current screen and the target screen
518 if (mNextScreen >= 0 && mNextScreen < getChildCount() &&
519 Math.abs(mCurrentScreen - mNextScreen) == 1) {
520 drawChild(canvas, getChildAt(mCurrentScreen), drawingTime);
521 drawChild(canvas, getChildAt(mNextScreen), drawingTime);
522 } else {
523 // If we are scrolling, draw all of our children
524 final int count = getChildCount();
525 for (int i = 0; i < count; i++) {
526 drawChild(canvas, getChildAt(i), drawingTime);
527 }
528 }
529 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700530
531 if (restore) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700532 canvas.restoreToCount(restoreCount);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 }
535
Joe Onorato85a02a82009-09-08 12:34:22 -0700536 private float mScale = 1.0f;
537 public void setScale(float scale) {
538 mScale = scale;
539 invalidate();
540 }
541
Joe Onorato00acb122009-08-04 16:04:30 -0400542 protected void onAttachedToWindow() {
543 super.onAttachedToWindow();
544 mDragController.setWindowToken(getWindowToken());
545 }
546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 @Override
548 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
549 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
550
551 final int width = MeasureSpec.getSize(widthMeasureSpec);
552 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
553 if (widthMode != MeasureSpec.EXACTLY) {
554 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
555 }
556
557 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
558 if (heightMode != MeasureSpec.EXACTLY) {
559 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
560 }
561
562 // The children are given the same width and height as the workspace
563 final int count = getChildCount();
564 for (int i = 0; i < count; i++) {
565 getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
566 }
567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 if (mFirstLayout) {
569 scrollTo(mCurrentScreen * width, 0);
Romain Guy798300c2009-08-11 14:43:29 -0700570 updateWallpaperOffset(width * (getChildCount() - 1));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 mFirstLayout = false;
572 }
573 }
574
575 @Override
576 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
577 int childLeft = 0;
578
579 final int count = getChildCount();
580 for (int i = 0; i < count; i++) {
581 final View child = getChildAt(i);
582 if (child.getVisibility() != View.GONE) {
583 final int childWidth = child.getMeasuredWidth();
584 child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
585 childLeft += childWidth;
586 }
587 }
588 }
589
590 @Override
591 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
592 int screen = indexOfChild(child);
593 if (screen != mCurrentScreen || !mScroller.isFinished()) {
594 if (!mLauncher.isWorkspaceLocked()) {
595 snapToScreen(screen);
596 }
597 return true;
598 }
599 return false;
600 }
601
602 @Override
603 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
604 if (mLauncher.isDrawerDown()) {
605 final Folder openFolder = getOpenFolder();
606 if (openFolder != null) {
607 return openFolder.requestFocus(direction, previouslyFocusedRect);
608 } else {
609 int focusableScreen;
610 if (mNextScreen != INVALID_SCREEN) {
611 focusableScreen = mNextScreen;
612 } else {
613 focusableScreen = mCurrentScreen;
614 }
615 getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect);
616 }
617 }
618 return false;
619 }
620
621 @Override
622 public boolean dispatchUnhandledMove(View focused, int direction) {
623 if (direction == View.FOCUS_LEFT) {
624 if (getCurrentScreen() > 0) {
625 snapToScreen(getCurrentScreen() - 1);
626 return true;
627 }
628 } else if (direction == View.FOCUS_RIGHT) {
629 if (getCurrentScreen() < getChildCount() - 1) {
630 snapToScreen(getCurrentScreen() + 1);
631 return true;
632 }
633 }
634 return super.dispatchUnhandledMove(focused, direction);
635 }
636
637 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -0700638 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 if (mLauncher.isDrawerDown()) {
640 final Folder openFolder = getOpenFolder();
641 if (openFolder == null) {
642 getChildAt(mCurrentScreen).addFocusables(views, direction);
643 if (direction == View.FOCUS_LEFT) {
644 if (mCurrentScreen > 0) {
645 getChildAt(mCurrentScreen - 1).addFocusables(views, direction);
646 }
647 } else if (direction == View.FOCUS_RIGHT){
648 if (mCurrentScreen < getChildCount() - 1) {
649 getChildAt(mCurrentScreen + 1).addFocusables(views, direction);
650 }
651 }
652 } else {
653 openFolder.addFocusables(views, direction);
654 }
655 }
656 }
657
658 @Override
659 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 if (mLauncher.isWorkspaceLocked() || !mLauncher.isDrawerDown()) {
Joe Onorato7c312c12009-08-13 21:36:53 -0700661 return false; // We don't want the events. Let them fall through to the all apps view.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 /*
665 * This method JUST determines whether we want to intercept the motion.
666 * If we return true, onTouchEvent will be called and we do the actual
667 * scrolling there.
668 */
669
670 /*
671 * Shortcut the most recurring case: the user is in the dragging
672 * state and he is moving his finger. We want to intercept this
673 * motion.
674 */
675 final int action = ev.getAction();
676 if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
677 return true;
678 }
679
680 final float x = ev.getX();
681 final float y = ev.getY();
682
683 switch (action) {
684 case MotionEvent.ACTION_MOVE:
685 /*
686 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
687 * whether the user has moved far enough from his original down touch.
688 */
689
690 /*
691 * Locally do absolute value. mLastMotionX is set to the y value
692 * of the down event.
693 */
694 final int xDiff = (int) Math.abs(x - mLastMotionX);
695 final int yDiff = (int) Math.abs(y - mLastMotionY);
696
697 final int touchSlop = mTouchSlop;
698 boolean xMoved = xDiff > touchSlop;
699 boolean yMoved = yDiff > touchSlop;
700
701 if (xMoved || yMoved) {
702
703 if (xMoved) {
704 // Scroll if the user moved far enough along the X axis
705 mTouchState = TOUCH_STATE_SCROLLING;
706 enableChildrenCache();
707 }
708 // Either way, cancel any pending longpress
709 if (mAllowLongPress) {
710 mAllowLongPress = false;
711 // Try canceling the long press. It could also have been scheduled
712 // by a distant descendant, so use the mAllowLongPress flag to block
713 // everything
714 final View currentScreen = getChildAt(mCurrentScreen);
715 currentScreen.cancelLongPress();
716 }
717 }
718 break;
719
720 case MotionEvent.ACTION_DOWN:
721 // Remember location of down touch
722 mLastMotionX = x;
723 mLastMotionY = y;
724 mAllowLongPress = true;
725
726 /*
727 * If being flinged and user touches the screen, initiate drag;
728 * otherwise don't. mScroller.isFinished should be false when
729 * being flinged.
730 */
731 mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
732 break;
733
734 case MotionEvent.ACTION_CANCEL:
735 case MotionEvent.ACTION_UP:
736 // Release the drag
737 clearChildrenCache();
738 mTouchState = TOUCH_STATE_REST;
739 mAllowLongPress = false;
740 break;
741 }
742
743 /*
744 * The only time we want to intercept motion events is if we are in the
745 * drag mode.
746 */
747 return mTouchState != TOUCH_STATE_REST;
748 }
749
750 void enableChildrenCache() {
751 final int count = getChildCount();
752 for (int i = 0; i < count; i++) {
753 final CellLayout layout = (CellLayout) getChildAt(i);
754 layout.setChildrenDrawnWithCacheEnabled(true);
755 layout.setChildrenDrawingCacheEnabled(true);
756 }
757 }
758
759 void clearChildrenCache() {
760 final int count = getChildCount();
761 for (int i = 0; i < count; i++) {
762 final CellLayout layout = (CellLayout) getChildAt(i);
763 layout.setChildrenDrawnWithCacheEnabled(false);
764 }
765 }
766
767 @Override
768 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato59791172009-07-31 16:21:40 -0700769
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 if (mLauncher.isWorkspaceLocked() || !mLauncher.isDrawerDown()) {
Joe Onorato7c312c12009-08-13 21:36:53 -0700771 return false; // We don't want the events. Let them fall through to the all apps view.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 if (mVelocityTracker == null) {
775 mVelocityTracker = VelocityTracker.obtain();
776 }
777 mVelocityTracker.addMovement(ev);
778
779 final int action = ev.getAction();
780 final float x = ev.getX();
781
782 switch (action) {
783 case MotionEvent.ACTION_DOWN:
784 /*
785 * If being flinged and user touches, stop the fling. isFinished
786 * will be false if being flinged.
787 */
788 if (!mScroller.isFinished()) {
789 mScroller.abortAnimation();
790 }
791
792 // Remember where the motion event started
793 mLastMotionX = x;
794 break;
795 case MotionEvent.ACTION_MOVE:
796 if (mTouchState == TOUCH_STATE_SCROLLING) {
797 // Scroll to follow the motion event
798 final int deltaX = (int) (mLastMotionX - x);
799 mLastMotionX = x;
800
801 if (deltaX < 0) {
802 if (mScrollX > 0) {
803 scrollBy(Math.max(-mScrollX, deltaX), 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700804 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 } else if (deltaX > 0) {
807 final int availableToScroll = getChildAt(getChildCount() - 1).getRight() -
808 mScrollX - getWidth();
809 if (availableToScroll > 0) {
810 scrollBy(Math.min(availableToScroll, deltaX), 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700811 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813 }
814 }
815 break;
816 case MotionEvent.ACTION_UP:
817 if (mTouchState == TOUCH_STATE_SCROLLING) {
818 final VelocityTracker velocityTracker = mVelocityTracker;
Romain Guya206daa2009-07-06 11:51:18 -0700819 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 int velocityX = (int) velocityTracker.getXVelocity();
821
822 if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
823 // Fling hard enough to move left
824 snapToScreen(mCurrentScreen - 1);
825 } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
826 // Fling hard enough to move right
827 snapToScreen(mCurrentScreen + 1);
828 } else {
829 snapToDestination();
830 }
831
832 if (mVelocityTracker != null) {
833 mVelocityTracker.recycle();
834 mVelocityTracker = null;
835 }
836 }
837 mTouchState = TOUCH_STATE_REST;
838 break;
839 case MotionEvent.ACTION_CANCEL:
840 mTouchState = TOUCH_STATE_REST;
841 }
842
843 return true;
844 }
845
846 private void snapToDestination() {
847 final int screenWidth = getWidth();
848 final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
849
850 snapToScreen(whichScreen);
851 }
852
853 void snapToScreen(int whichScreen) {
Romain Guy82d94d92009-05-06 17:43:13 -0700854 if (!mScroller.isFinished()) return;
855
Romain Guy4c58c482009-05-12 17:35:41 -0700856 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 enableChildrenCache();
858
859 whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
860 boolean changingScreens = whichScreen != mCurrentScreen;
861
862 mNextScreen = whichScreen;
863
864 View focusedChild = getFocusedChild();
865 if (focusedChild != null && changingScreens && focusedChild == getChildAt(mCurrentScreen)) {
866 focusedChild.clearFocus();
867 }
868
869 final int newX = whichScreen * getWidth();
870 final int delta = newX - mScrollX;
871 mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2);
872 invalidate();
873 }
874
875 void startDrag(CellLayout.CellInfo cellInfo) {
876 View child = cellInfo.cell;
877
878 // Make sure the drag was started by a long press as opposed to a long click.
879 // Note that Search takes focus when clicked rather than entering touch mode
880 if (!child.isInTouchMode() && !(child instanceof Search)) {
881 return;
882 }
883
884 mDragInfo = cellInfo;
885 mDragInfo.screen = mCurrentScreen;
886
887 CellLayout current = ((CellLayout) getChildAt(mCurrentScreen));
888
889 current.onDragChild(child);
Joe Onorato00acb122009-08-04 16:04:30 -0400890 mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 invalidate();
892 }
893
894 @Override
895 protected Parcelable onSaveInstanceState() {
896 final SavedState state = new SavedState(super.onSaveInstanceState());
897 state.currentScreen = mCurrentScreen;
898 return state;
899 }
900
901 @Override
902 protected void onRestoreInstanceState(Parcelable state) {
903 SavedState savedState = (SavedState) state;
904 super.onRestoreInstanceState(savedState.getSuperState());
905 if (savedState.currentScreen != -1) {
906 mCurrentScreen = savedState.currentScreen;
907 Launcher.setScreen(mCurrentScreen);
908 }
909 }
910
911 void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo) {
912 addApplicationShortcut(info, cellInfo, false);
913 }
914
915 void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo,
916 boolean insertAtFirst) {
917 final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen);
918 final int[] result = new int[2];
919
920 layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result);
921 onDropExternal(result[0], result[1], info, layout, insertAtFirst);
922 }
923
Joe Onorato00acb122009-08-04 16:04:30 -0400924 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
925 DragView dragView, Object dragInfo) {
Jeff Sharkey70864282009-04-07 21:08:40 -0700926 final CellLayout cellLayout = getCurrentDropLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 if (source != this) {
928 onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout);
929 } else {
930 // Move internally
931 if (mDragInfo != null) {
932 final View cell = mDragInfo.cell;
933 if (mCurrentScreen != mDragInfo.screen) {
934 final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
935 originalCellLayout.removeView(cell);
936 cellLayout.addView(cell);
937 }
Romain Guy263e0192009-05-11 11:50:46 -0700938 mTargetCell = estimateDropCell(x - xOffset, y - yOffset,
Jeff Sharkey70864282009-04-07 21:08:40 -0700939 mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell);
940 cellLayout.onDropChild(cell, mTargetCell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941
942 final ItemInfo info = (ItemInfo)cell.getTag();
943 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
944 LauncherModel.moveItemInDatabase(mLauncher, info,
945 LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY);
946 }
947 }
948 }
949
950 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400951 DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -0700952 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400956 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
959 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400960 DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -0700961 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
963
964 private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) {
965 onDropExternal(x, y, dragInfo, cellLayout, false);
966 }
967
968 private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout,
969 boolean insertAtFirst) {
970 // Drag from somewhere else
971 ItemInfo info = (ItemInfo) dragInfo;
972
973 View view;
974
975 switch (info.itemType) {
976 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
977 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
978 if (info.container == NO_ID) {
979 // Came from all apps -- make a copy
980 info = new ApplicationInfo((ApplicationInfo) info);
981 }
982 view = mLauncher.createShortcut(R.layout.application, cellLayout,
983 (ApplicationInfo) info);
984 break;
985 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
986 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
987 (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info));
988 break;
989 default:
990 throw new IllegalStateException("Unknown item type: " + info.itemType);
991 }
992
993 cellLayout.addView(view, insertAtFirst ? 0 : -1);
994 view.setOnLongClickListener(mLongClickListener);
Joe Onorato00acb122009-08-04 16:04:30 -0400995 if (view instanceof DropTarget) {
996 mDragController.addDropTarget((DropTarget)view);
997 }
998
Romain Guy263e0192009-05-11 11:50:46 -0700999 mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell);
Jeff Sharkey70864282009-04-07 21:08:40 -07001000 cellLayout.onDropChild(view, mTargetCell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
1002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
1004 LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY);
1005 }
Jeff Sharkey70864282009-04-07 21:08:40 -07001006
1007 /**
1008 * Return the current {@link CellLayout}, correctly picking the destination
1009 * screen while a scroll is in progress.
1010 */
1011 private CellLayout getCurrentDropLayout() {
1012 int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
1013 return (CellLayout) getChildAt(index);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015
Jeff Sharkey70864282009-04-07 21:08:40 -07001016 /**
1017 * {@inheritDoc}
1018 */
1019 public boolean acceptDrop(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -04001020 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -07001021 final CellLayout layout = getCurrentDropLayout();
1022 final CellLayout.CellInfo cellInfo = mDragInfo;
1023 final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1024 final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1025
1026 if (mVacantCache == null) {
1027 final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1028 mVacantCache = layout.findAllVacantCells(null, ignoreView);
1029 }
1030
1031 return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false);
Jeff Sharkey70864282009-04-07 21:08:40 -07001032 }
1033
1034 /**
1035 * {@inheritDoc}
1036 */
1037 public Rect estimateDropLocation(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -04001038 int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) {
Jeff Sharkey70864282009-04-07 21:08:40 -07001039 final CellLayout layout = getCurrentDropLayout();
1040
1041 final CellLayout.CellInfo cellInfo = mDragInfo;
1042 final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1043 final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1044 final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1045
1046 final Rect location = recycle != null ? recycle : new Rect();
1047
1048 // Find drop cell and convert into rectangle
Romain Guy263e0192009-05-11 11:50:46 -07001049 int[] dropCell = estimateDropCell(x - xOffset, y - yOffset,
Jeff Sharkey70864282009-04-07 21:08:40 -07001050 spanX, spanY, ignoreView, layout, mTempCell);
1051
1052 if (dropCell == null) {
1053 return null;
1054 }
1055
1056 layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate);
1057 location.left = mTempEstimate[0];
1058 location.top = mTempEstimate[1];
1059
1060 layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate);
1061 location.right = mTempEstimate[0];
1062 location.bottom = mTempEstimate[1];
1063
1064 return location;
1065 }
1066
1067 /**
1068 * Calculate the nearest cell where the given object would be dropped.
1069 */
Romain Guy263e0192009-05-11 11:50:46 -07001070 private int[] estimateDropCell(int pixelX, int pixelY,
Jeff Sharkey70864282009-04-07 21:08:40 -07001071 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
1072 // Create vacant cell cache if none exists
1073 if (mVacantCache == null) {
1074 mVacantCache = layout.findAllVacantCells(null, ignoreView);
1075 }
1076
1077 // Find the best target drop location
1078 return layout.findNearestVacantArea(pixelX, pixelY,
1079 spanX, spanY, mVacantCache, recycle);
1080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 void setLauncher(Launcher launcher) {
1083 mLauncher = launcher;
1084 }
1085
Joe Onorato00acb122009-08-04 16:04:30 -04001086 public void setDragController(DragController dragController) {
1087 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089
1090 public void onDropCompleted(View target, boolean success) {
1091 if (success){
1092 if (target != this && mDragInfo != null) {
1093 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1094 cellLayout.removeView(mDragInfo.cell);
Joe Onorato00acb122009-08-04 16:04:30 -04001095 if (mDragInfo.cell instanceof DropTarget) {
1096 mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 final Object tag = mDragInfo.cell.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100 } else {
1101 if (mDragInfo != null) {
1102 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1103 cellLayout.onDropAborted(mDragInfo.cell);
1104 }
1105 }
1106
1107 mDragInfo = null;
1108 }
1109
1110 public void scrollLeft() {
Romain Guy4c58c482009-05-12 17:35:41 -07001111 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 if (mNextScreen == INVALID_SCREEN && mCurrentScreen > 0 && mScroller.isFinished()) {
1113 snapToScreen(mCurrentScreen - 1);
1114 }
1115 }
1116
1117 public void scrollRight() {
Romain Guy4c58c482009-05-12 17:35:41 -07001118 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 if (mNextScreen == INVALID_SCREEN && mCurrentScreen < getChildCount() -1 &&
1120 mScroller.isFinished()) {
1121 snapToScreen(mCurrentScreen + 1);
1122 }
1123 }
1124
1125 public int getScreenForView(View v) {
1126 int result = -1;
1127 if (v != null) {
1128 ViewParent vp = v.getParent();
1129 int count = getChildCount();
1130 for (int i = 0; i < count; i++) {
1131 if (vp == getChildAt(i)) {
1132 return i;
1133 }
1134 }
1135 }
1136 return result;
1137 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 /**
1140 * Find a search widget on the given screen
1141 */
1142 private Search findSearchWidget(CellLayout screen) {
1143 final int count = screen.getChildCount();
1144 for (int i = 0; i < count; i++) {
1145 View v = screen.getChildAt(i);
1146 if (v instanceof Search) {
1147 return (Search) v;
1148 }
1149 }
1150 return null;
1151 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 /**
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 * Gets the first search widget on the current screen, if there is one.
1155 * Returns <code>null</code> otherwise.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 */
Karl Rosaen138a0412009-04-23 19:00:21 -07001157 public Search findSearchWidgetOnCurrentScreen() {
1158 CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen);
1159 return findSearchWidget(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
1161
1162 public Folder getFolderForTag(Object tag) {
1163 int screenCount = getChildCount();
1164 for (int screen = 0; screen < screenCount; screen++) {
1165 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1166 int count = currentScreen.getChildCount();
1167 for (int i = 0; i < count; i++) {
1168 View child = currentScreen.getChildAt(i);
1169 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1170 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1171 Folder f = (Folder) child;
1172 if (f.getInfo() == tag) {
1173 return f;
1174 }
1175 }
1176 }
1177 }
1178 return null;
1179 }
1180
1181 public View getViewForTag(Object tag) {
1182 int screenCount = getChildCount();
1183 for (int screen = 0; screen < screenCount; screen++) {
1184 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1185 int count = currentScreen.getChildCount();
1186 for (int i = 0; i < count; i++) {
1187 View child = currentScreen.getChildAt(i);
1188 if (child.getTag() == tag) {
1189 return child;
1190 }
1191 }
1192 }
1193 return null;
1194 }
1195
1196 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 * @return True is long presses are still allowed for the current touch
1198 */
1199 public boolean allowLongPress() {
1200 return mAllowLongPress;
1201 }
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001202
1203 /**
1204 * Set true to allow long-press events to be triggered, usually checked by
1205 * {@link Launcher} to accept or block dpad-initiated long-presses.
1206 */
1207 public void setAllowLongPress(boolean allowLongPress) {
1208 mAllowLongPress = allowLongPress;
1209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210
1211 void removeShortcutsForPackage(String packageName) {
1212 final ArrayList<View> childrenToRemove = new ArrayList<View>();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 final int count = getChildCount();
Romain Guy574d20e2009-06-01 15:34:04 -07001214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 for (int i = 0; i < count; i++) {
1216 final CellLayout layout = (CellLayout) getChildAt(i);
1217 int childCount = layout.getChildCount();
Romain Guy574d20e2009-06-01 15:34:04 -07001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 childrenToRemove.clear();
Romain Guy574d20e2009-06-01 15:34:04 -07001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 for (int j = 0; j < childCount; j++) {
1222 final View view = layout.getChildAt(j);
1223 Object tag = view.getTag();
Romain Guy574d20e2009-06-01 15:34:04 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 if (tag instanceof ApplicationInfo) {
Romain Guy574d20e2009-06-01 15:34:04 -07001226 final ApplicationInfo info = (ApplicationInfo) tag;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 // We need to check for ACTION_MAIN otherwise getComponent() might
1228 // return null for some shortcuts (for instance, for shortcuts to
1229 // web pages.)
1230 final Intent intent = info.intent;
1231 final ComponentName name = intent.getComponent();
Romain Guy574d20e2009-06-01 15:34:04 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
1234 name != null && packageName.equals(name.getPackageName())) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 LauncherModel.deleteItemFromDatabase(mLauncher, info);
1236 childrenToRemove.add(view);
1237 }
Romain Guy574d20e2009-06-01 15:34:04 -07001238 } else if (tag instanceof UserFolderInfo) {
1239 final UserFolderInfo info = (UserFolderInfo) tag;
1240 final ArrayList<ApplicationInfo> contents = info.contents;
1241 final ArrayList<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(1);
1242 final int contentsCount = contents.size();
1243 boolean removedFromFolder = false;
1244
1245 for (int k = 0; k < contentsCount; k++) {
1246 final ApplicationInfo appInfo = contents.get(k);
1247 final Intent intent = appInfo.intent;
1248 final ComponentName name = intent.getComponent();
1249
1250 if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
1251 name != null && packageName.equals(name.getPackageName())) {
1252 toRemove.add(appInfo);
1253 LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
1254 removedFromFolder = true;
1255 }
1256 }
1257
1258 contents.removeAll(toRemove);
1259 if (removedFromFolder) {
1260 final Folder folder = getOpenFolder();
1261 if (folder != null) folder.notifyDataSetChanged();
1262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
1264 }
Romain Guy574d20e2009-06-01 15:34:04 -07001265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 childCount = childrenToRemove.size();
1267 for (int j = 0; j < childCount; j++) {
Joe Onorato00acb122009-08-04 16:04:30 -04001268 View child = childrenToRemove.get(j);
1269 layout.removeViewInLayout(child);
1270 if (child instanceof DropTarget) {
1271 mDragController.removeDropTarget((DropTarget)child);
1272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guy574d20e2009-06-01 15:34:04 -07001274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 if (childCount > 0) {
1276 layout.requestLayout();
1277 layout.invalidate();
1278 }
1279 }
1280 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001281
1282 void updateShortcutsForPackage(String packageName) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283 final PackageManager pm = mLauncher.getPackageManager();
1284
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001285 final int count = getChildCount();
1286 for (int i = 0; i < count; i++) {
1287 final CellLayout layout = (CellLayout) getChildAt(i);
1288 int childCount = layout.getChildCount();
1289 for (int j = 0; j < childCount; j++) {
1290 final View view = layout.getChildAt(j);
1291 Object tag = view.getTag();
1292 if (tag instanceof ApplicationInfo) {
1293 ApplicationInfo info = (ApplicationInfo) tag;
1294 // We need to check for ACTION_MAIN otherwise getComponent() might
1295 // return null for some shortcuts (for instance, for shortcuts to
1296 // web pages.)
1297 final Intent intent = info.intent;
1298 final ComponentName name = intent.getComponent();
1299 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
1300 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null &&
1301 packageName.equals(name.getPackageName())) {
1302
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 final Drawable icon = AppInfoCache.getIconDrawable(pm, info);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001304 if (icon != null && icon != info.icon) {
1305 info.icon.setCallback(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001306 info.icon = Utilities.createIconThumbnail(icon, mContext);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001307 info.filtered = true;
1308 ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null,
1309 info.icon, null, null);
1310 }
1311 }
1312 }
1313 }
1314 }
1315 }
1316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 void moveToDefaultScreen() {
1318 snapToScreen(mDefaultScreen);
1319 getChildAt(mDefaultScreen).requestFocus();
1320 }
1321
1322 public static class SavedState extends BaseSavedState {
1323 int currentScreen = -1;
1324
1325 SavedState(Parcelable superState) {
1326 super(superState);
1327 }
1328
1329 private SavedState(Parcel in) {
1330 super(in);
1331 currentScreen = in.readInt();
1332 }
1333
1334 @Override
1335 public void writeToParcel(Parcel out, int flags) {
1336 super.writeToParcel(out, flags);
1337 out.writeInt(currentScreen);
1338 }
1339
1340 public static final Parcelable.Creator<SavedState> CREATOR =
1341 new Parcelable.Creator<SavedState>() {
1342 public SavedState createFromParcel(Parcel in) {
1343 return new SavedState(in);
1344 }
1345
1346 public SavedState[] newArray(int size) {
1347 return new SavedState[size];
1348 }
1349 };
1350 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001351
1352 void show() {
Joe Onorato7c312c12009-08-13 21:36:53 -07001353 setVisibility(VISIBLE);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001354 }
1355
1356 void hide() {
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358}