blob: 927bd4c0fc26b4d3c55c61a83fbba8eb2945dca9 [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.Rect;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070027import android.graphics.drawable.Drawable;
Joe Onorato080d9b62009-11-02 12:01:11 -050028import android.os.Parcelable;
29import android.os.Parcel;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.util.AttributeSet;
Joe Onorato68ba5ca2009-11-12 14:23:43 -080031import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.view.MotionEvent;
33import android.view.VelocityTracker;
34import android.view.View;
35import android.view.ViewConfiguration;
36import android.view.ViewGroup;
37import android.view.ViewParent;
38import android.widget.Scroller;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070039import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41import java.util.ArrayList;
42
43/**
44 * The workspace is a wide area with a wallpaper and a finite number of screens. Each
45 * screen contains a number of icons, folders or widgets the user can interact with.
46 * A workspace is meant to be used with a fixed width only.
47 */
Joe Onorato85a02a82009-09-08 12:34:22 -070048public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller {
Romain Guye47f55c2009-11-11 19:21:22 -080049 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato3a8820b2009-11-10 15:06:42 -080050 private static final String TAG = "Launcher.Workspace";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private static final int INVALID_SCREEN = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -070052
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 /**
54 * The velocity at which a fling gesture will cause us to snap to the next screen
55 */
56 private static final int SNAP_VELOCITY = 1000;
57
Dianne Hackborn8f573952009-08-10 23:21:09 -070058 private final WallpaperManager mWallpaperManager;
59
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 private int mDefaultScreen;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 private boolean mFirstLayout = true;
63
64 private int mCurrentScreen;
65 private int mNextScreen = INVALID_SCREEN;
66 private Scroller mScroller;
67 private VelocityTracker mVelocityTracker;
68
69 /**
70 * CellInfo for the cell that is currently being dragged
71 */
72 private CellLayout.CellInfo mDragInfo;
Jeff Sharkey70864282009-04-07 21:08:40 -070073
74 /**
75 * Target drop area calculated during last acceptDrop call.
76 */
77 private int[] mTargetCell = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
79 private float mLastMotionX;
80 private float mLastMotionY;
Mike Cleronf8bbd342009-10-23 16:15:16 -070081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 private final static int TOUCH_STATE_REST = 0;
83 private final static int TOUCH_STATE_SCROLLING = 1;
84
85 private int mTouchState = TOUCH_STATE_REST;
86
87 private OnLongClickListener mLongClickListener;
88
89 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040090 private DragController mDragController;
Jeff Sharkey70864282009-04-07 21:08:40 -070091
92 /**
93 * Cache of vacant cells, used during drag events and invalidated as needed.
94 */
95 private CellLayout.CellInfo mVacantCache = null;
96
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 private int[] mTempCell = new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -070098 private int[] mTempEstimate = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099
Joe Onoratoa9c28f62009-09-14 18:38:49 -0400100 private boolean mAllowLongPress = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
102 private int mTouchSlop;
Romain Guya206daa2009-07-06 11:51:18 -0700103 private int mMaximumVelocity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
Romain Guy8a73c512009-11-09 19:19:59 -0800105 private Drawable mPreviousIndicator;
106 private Drawable mNextIndicator;
107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 /**
109 * Used to inflate the Workspace from XML.
110 *
111 * @param context The application's context.
112 * @param attrs The attribtues set containing the Workspace's customization values.
113 */
114 public Workspace(Context context, AttributeSet attrs) {
115 this(context, attrs, 0);
116 }
117
118 /**
119 * Used to inflate the Workspace from XML.
120 *
121 * @param context The application's context.
122 * @param attrs The attribtues set containing the Workspace's customization values.
123 * @param defStyle Unused.
124 */
125 public Workspace(Context context, AttributeSet attrs, int defStyle) {
126 super(context, attrs, defStyle);
127
Dianne Hackborn8f573952009-08-10 23:21:09 -0700128 mWallpaperManager = WallpaperManager.getInstance(context);
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);
131 mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);
132 a.recycle();
133
Joe Onorato0d44e942009-11-16 18:20:51 -0800134 setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 initWorkspace();
136 }
137
138 /**
139 * Initializes various states for this workspace.
140 */
141 private void initWorkspace() {
142 mScroller = new Scroller(getContext());
143 mCurrentScreen = mDefaultScreen;
144 Launcher.setScreen(mCurrentScreen);
145
Romain Guya206daa2009-07-06 11:51:18 -0700146 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
147 mTouchSlop = configuration.getScaledTouchSlop();
148 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 @Override
152 public void addView(View child, int index, LayoutParams params) {
153 if (!(child instanceof CellLayout)) {
154 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
155 }
156 super.addView(child, index, params);
157 }
158
159 @Override
160 public void addView(View child) {
161 if (!(child instanceof CellLayout)) {
162 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
163 }
164 super.addView(child);
165 }
166
167 @Override
168 public void addView(View child, int index) {
169 if (!(child instanceof CellLayout)) {
170 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
171 }
172 super.addView(child, index);
173 }
174
175 @Override
176 public void addView(View child, int width, int height) {
177 if (!(child instanceof CellLayout)) {
178 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
179 }
180 super.addView(child, width, height);
181 }
182
183 @Override
184 public void addView(View child, LayoutParams params) {
185 if (!(child instanceof CellLayout)) {
186 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
187 }
188 super.addView(child, params);
189 }
190
191 /**
192 * @return The open folder on the current screen, or null if there is none
193 */
194 Folder getOpenFolder() {
195 CellLayout currentScreen = (CellLayout) getChildAt(mCurrentScreen);
196 int count = currentScreen.getChildCount();
197 for (int i = 0; i < count; i++) {
198 View child = currentScreen.getChildAt(i);
199 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
200 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
201 return (Folder) child;
202 }
203 }
204 return null;
205 }
206
207 ArrayList<Folder> getOpenFolders() {
208 final int screens = getChildCount();
209 ArrayList<Folder> folders = new ArrayList<Folder>(screens);
210
211 for (int screen = 0; screen < screens; screen++) {
212 CellLayout currentScreen = (CellLayout) getChildAt(screen);
213 int count = currentScreen.getChildCount();
214 for (int i = 0; i < count; i++) {
215 View child = currentScreen.getChildAt(i);
216 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
217 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
218 folders.add((Folder) child);
219 break;
220 }
221 }
222 }
223
224 return folders;
225 }
226
227 boolean isDefaultScreenShowing() {
228 return mCurrentScreen == mDefaultScreen;
229 }
230
231 /**
232 * Returns the index of the currently displayed screen.
233 *
234 * @return The index of the currently displayed screen.
235 */
236 int getCurrentScreen() {
237 return mCurrentScreen;
238 }
239
240 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 * Sets the current screen.
242 *
243 * @param currentScreen
244 */
245 void setCurrentScreen(int currentScreen) {
Romain Guy4c58c482009-05-12 17:35:41 -0700246 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
248 scrollTo(mCurrentScreen * getWidth(), 0);
249 invalidate();
250 }
251
252 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 * Adds the specified child in the current screen. The position and dimension of
254 * the child are defined by x, y, spanX and spanY.
255 *
256 * @param child The child to add in one of the workspace's screens.
257 * @param x The X position of the child in the screen's grid.
258 * @param y The Y position of the child in the screen's grid.
259 * @param spanX The number of cells spanned horizontally by the child.
260 * @param spanY The number of cells spanned vertically by the child.
261 */
262 void addInCurrentScreen(View child, int x, int y, int spanX, int spanY) {
263 addInScreen(child, mCurrentScreen, x, y, spanX, spanY, false);
264 }
265
266 /**
267 * Adds the specified child in the current screen. The position and dimension of
268 * the child are defined by x, y, spanX and spanY.
269 *
270 * @param child The child to add in one of the workspace's screens.
271 * @param x The X position of the child in the screen's grid.
272 * @param y The Y position of the child in the screen's grid.
273 * @param spanX The number of cells spanned horizontally by the child.
274 * @param spanY The number of cells spanned vertically by the child.
275 * @param insert When true, the child is inserted at the beginning of the children list.
276 */
277 void addInCurrentScreen(View child, int x, int y, int spanX, int spanY, boolean insert) {
278 addInScreen(child, mCurrentScreen, x, y, spanX, spanY, insert);
279 }
280
281 /**
282 * Adds the specified child in the specified screen. The position and dimension of
283 * the child are defined by x, y, spanX and spanY.
284 *
285 * @param child The child to add in one of the workspace's screens.
286 * @param screen The screen in which to add the child.
287 * @param x The X position of the child in the screen's grid.
288 * @param y The Y position of the child in the screen's grid.
289 * @param spanX The number of cells spanned horizontally by the child.
290 * @param spanY The number of cells spanned vertically by the child.
291 */
292 void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) {
293 addInScreen(child, screen, x, y, spanX, spanY, false);
294 }
295
296 /**
297 * Adds the specified child in the specified screen. The position and dimension of
298 * the child are defined by x, y, spanX and spanY.
299 *
300 * @param child The child to add in one of the workspace's screens.
301 * @param screen The screen in which to add the child.
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 addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) {
309 if (screen < 0 || screen >= getChildCount()) {
310 throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount());
311 }
312
Romain Guy4c58c482009-05-12 17:35:41 -0700313 clearVacantCache();
314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 final CellLayout group = (CellLayout) getChildAt(screen);
316 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
317 if (lp == null) {
318 lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
319 } else {
320 lp.cellX = x;
321 lp.cellY = y;
322 lp.cellHSpan = spanX;
323 lp.cellVSpan = spanY;
324 }
325 group.addView(child, insert ? 0 : -1, lp);
326 if (!(child instanceof Folder)) {
Joe Onorato0d44e942009-11-16 18:20:51 -0800327 child.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 child.setOnLongClickListener(mLongClickListener);
329 }
Joe Onorato00acb122009-08-04 16:04:30 -0400330 if (child instanceof DropTarget) {
331 mDragController.addDropTarget((DropTarget)child);
332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 CellLayout.CellInfo findAllVacantCells(boolean[] occupied) {
336 CellLayout group = (CellLayout) getChildAt(mCurrentScreen);
337 if (group != null) {
Jeff Sharkey70864282009-04-07 21:08:40 -0700338 return group.findAllVacantCells(occupied, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 }
340 return null;
341 }
342
Romain Guy4c58c482009-05-12 17:35:41 -0700343 private void clearVacantCache() {
344 if (mVacantCache != null) {
345 mVacantCache.clearVacantCells();
346 mVacantCache = null;
347 }
348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349
350 /**
351 * Registers the specified listener on each screen contained in this workspace.
352 *
353 * @param l The listener used to respond to long clicks.
354 */
355 @Override
356 public void setOnLongClickListener(OnLongClickListener l) {
357 mLongClickListener = l;
358 final int count = getChildCount();
359 for (int i = 0; i < count; i++) {
360 getChildAt(i).setOnLongClickListener(l);
361 }
362 }
363
Dianne Hackborn8f573952009-08-10 23:21:09 -0700364 private void updateWallpaperOffset() {
Romain Guy798300c2009-08-11 14:43:29 -0700365 updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
366 }
367
368 private void updateWallpaperOffset(int scrollRange) {
Marco Nelissenc07c79b2009-11-09 16:08:03 -0800369 mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 );
Dianne Hackborn49cdb1b2009-08-13 16:50:18 -0700370 mWallpaperManager.setWallpaperOffsets(getWindowToken(), mScrollX / (float) scrollRange, 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 public void computeScroll() {
375 if (mScroller.computeScrollOffset()) {
376 mScrollX = mScroller.getCurrX();
377 mScrollY = mScroller.getCurrY();
Dianne Hackborn8f573952009-08-10 23:21:09 -0700378 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 postInvalidate();
380 } else if (mNextScreen != INVALID_SCREEN) {
381 mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
Romain Guy8a73c512009-11-09 19:19:59 -0800382 mPreviousIndicator.setLevel(mCurrentScreen);
383 mNextIndicator.setLevel(mCurrentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 Launcher.setScreen(mCurrentScreen);
385 mNextScreen = INVALID_SCREEN;
386 clearChildrenCache();
387 }
388 }
389
390 @Override
391 protected void dispatchDraw(Canvas canvas) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 boolean restore = false;
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700393 int restoreCount = 0;
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 // ViewGroup.dispatchDraw() supports many features we don't need:
396 // clip to padding, layout animation, animation listener, disappearing
397 // children, etc. The following implementation attempts to fast-track
398 // the drawing dispatch by drawing only what we know needs to be drawn.
399
Romain Guyf8e6a802009-12-07 17:48:02 -0800400 boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 // If we are not scrolling or flinging, draw only the current screen
402 if (fastDraw) {
403 drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime());
404 } else {
405 final long drawingTime = getDrawingTime();
406 // If we are flinging, draw only the current screen and the target screen
407 if (mNextScreen >= 0 && mNextScreen < getChildCount() &&
408 Math.abs(mCurrentScreen - mNextScreen) == 1) {
409 drawChild(canvas, getChildAt(mCurrentScreen), drawingTime);
410 drawChild(canvas, getChildAt(mNextScreen), drawingTime);
411 } else {
412 // If we are scrolling, draw all of our children
413 final int count = getChildCount();
414 for (int i = 0; i < count; i++) {
415 drawChild(canvas, getChildAt(i), drawingTime);
416 }
417 }
418 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419
420 if (restore) {
Joe Onoratoe77c08d2009-08-01 00:01:20 -0700421 canvas.restoreToCount(restoreCount);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
Joe Onorato00acb122009-08-04 16:04:30 -0400425 protected void onAttachedToWindow() {
426 super.onAttachedToWindow();
427 mDragController.setWindowToken(getWindowToken());
428 }
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 @Override
431 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
432 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
433
434 final int width = MeasureSpec.getSize(widthMeasureSpec);
435 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
436 if (widthMode != MeasureSpec.EXACTLY) {
437 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
438 }
439
440 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
441 if (heightMode != MeasureSpec.EXACTLY) {
442 throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
443 }
444
445 // The children are given the same width and height as the workspace
446 final int count = getChildCount();
447 for (int i = 0; i < count; i++) {
448 getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
449 }
450
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 if (mFirstLayout) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800453 setHorizontalScrollBarEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 scrollTo(mCurrentScreen * width, 0);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800455 setHorizontalScrollBarEnabled(true);
Romain Guy798300c2009-08-11 14:43:29 -0700456 updateWallpaperOffset(width * (getChildCount() - 1));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mFirstLayout = false;
458 }
459 }
460
461 @Override
462 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
463 int childLeft = 0;
464
465 final int count = getChildCount();
466 for (int i = 0; i < count; i++) {
467 final View child = getChildAt(i);
468 if (child.getVisibility() != View.GONE) {
469 final int childWidth = child.getMeasuredWidth();
470 child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
471 childLeft += childWidth;
472 }
473 }
474 }
475
476 @Override
477 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
478 int screen = indexOfChild(child);
479 if (screen != mCurrentScreen || !mScroller.isFinished()) {
480 if (!mLauncher.isWorkspaceLocked()) {
481 snapToScreen(screen);
482 }
483 return true;
484 }
485 return false;
486 }
487
488 @Override
489 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Joe Onorato67886212009-09-14 19:05:05 -0400490 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final Folder openFolder = getOpenFolder();
492 if (openFolder != null) {
493 return openFolder.requestFocus(direction, previouslyFocusedRect);
494 } else {
495 int focusableScreen;
496 if (mNextScreen != INVALID_SCREEN) {
497 focusableScreen = mNextScreen;
498 } else {
499 focusableScreen = mCurrentScreen;
500 }
501 getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect);
502 }
503 }
504 return false;
505 }
506
507 @Override
508 public boolean dispatchUnhandledMove(View focused, int direction) {
509 if (direction == View.FOCUS_LEFT) {
510 if (getCurrentScreen() > 0) {
511 snapToScreen(getCurrentScreen() - 1);
512 return true;
513 }
514 } else if (direction == View.FOCUS_RIGHT) {
515 if (getCurrentScreen() < getChildCount() - 1) {
516 snapToScreen(getCurrentScreen() + 1);
517 return true;
518 }
519 }
520 return super.dispatchUnhandledMove(focused, direction);
521 }
522
523 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -0700524 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Joe Onorato67886212009-09-14 19:05:05 -0400525 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 final Folder openFolder = getOpenFolder();
527 if (openFolder == null) {
528 getChildAt(mCurrentScreen).addFocusables(views, direction);
529 if (direction == View.FOCUS_LEFT) {
530 if (mCurrentScreen > 0) {
531 getChildAt(mCurrentScreen - 1).addFocusables(views, direction);
532 }
533 } else if (direction == View.FOCUS_RIGHT){
534 if (mCurrentScreen < getChildCount() - 1) {
535 getChildAt(mCurrentScreen + 1).addFocusables(views, direction);
536 }
537 }
538 } else {
539 openFolder.addFocusables(views, direction);
540 }
541 }
542 }
543
544 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700545 public boolean dispatchTouchEvent(MotionEvent ev) {
546 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
547 if (mLauncher.isWorkspaceLocked() || mLauncher.isAllAppsVisible()) {
548 return false;
549 }
550 }
551 return super.dispatchTouchEvent(ev);
552 }
553
554 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800556 final boolean workspaceLocked = mLauncher.isWorkspaceLocked();
557 final boolean allAppsVisible = mLauncher.isAllAppsVisible();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800558 if (workspaceLocked || allAppsVisible) {
Joe Onorato7c312c12009-08-13 21:36:53 -0700559 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 -0800560 }
561
562 /*
563 * This method JUST determines whether we want to intercept the motion.
564 * If we return true, onTouchEvent will be called and we do the actual
565 * scrolling there.
566 */
567
568 /*
569 * Shortcut the most recurring case: the user is in the dragging
570 * state and he is moving his finger. We want to intercept this
571 * motion.
572 */
573 final int action = ev.getAction();
574 if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
575 return true;
576 }
577
578 final float x = ev.getX();
579 final float y = ev.getY();
580
581 switch (action) {
582 case MotionEvent.ACTION_MOVE:
583 /*
584 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
585 * whether the user has moved far enough from his original down touch.
586 */
587
588 /*
589 * Locally do absolute value. mLastMotionX is set to the y value
590 * of the down event.
591 */
592 final int xDiff = (int) Math.abs(x - mLastMotionX);
593 final int yDiff = (int) Math.abs(y - mLastMotionY);
594
595 final int touchSlop = mTouchSlop;
596 boolean xMoved = xDiff > touchSlop;
597 boolean yMoved = yDiff > touchSlop;
598
599 if (xMoved || yMoved) {
600
601 if (xMoved) {
602 // Scroll if the user moved far enough along the X axis
603 mTouchState = TOUCH_STATE_SCROLLING;
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800604 enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606 // Either way, cancel any pending longpress
607 if (mAllowLongPress) {
608 mAllowLongPress = false;
609 // Try canceling the long press. It could also have been scheduled
610 // by a distant descendant, so use the mAllowLongPress flag to block
611 // everything
612 final View currentScreen = getChildAt(mCurrentScreen);
613 currentScreen.cancelLongPress();
614 }
615 }
616 break;
617
618 case MotionEvent.ACTION_DOWN:
619 // Remember location of down touch
620 mLastMotionX = x;
621 mLastMotionY = y;
622 mAllowLongPress = true;
623
624 /*
625 * If being flinged and user touches the screen, initiate drag;
626 * otherwise don't. mScroller.isFinished should be false when
627 * being flinged.
628 */
629 mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
630 break;
631
632 case MotionEvent.ACTION_CANCEL:
633 case MotionEvent.ACTION_UP:
Mike Cleronf8bbd342009-10-23 16:15:16 -0700634
635 if (mTouchState != TOUCH_STATE_SCROLLING) {
636
637 final CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen);
638 if (!currentScreen.lastDownOnOccupiedCell()) {
639 // Send a tap to the wallpaper if the last down was on empty space
640 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
641 "android.wallpaper.tap", (int) ev.getX(), (int) ev.getY(), 0, null);
642 }
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 // Release the drag
646 clearChildrenCache();
647 mTouchState = TOUCH_STATE_REST;
648 mAllowLongPress = false;
Mike Cleronf8bbd342009-10-23 16:15:16 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 break;
651 }
652
653 /*
654 * The only time we want to intercept motion events is if we are in the
655 * drag mode.
656 */
657 return mTouchState != TOUCH_STATE_REST;
658 }
659
Joe Onorato3a8820b2009-11-10 15:06:42 -0800660 /**
661 * If one of our descendant views decides that it could be focused now, only
662 * pass that along if it's on the current screen.
663 *
664 * This happens when live folders requery, and if they're off screen, they
665 * end up calling requestFocus, which pulls it on screen.
666 */
667 @Override
668 public void focusableViewAvailable(View focused) {
669 View current = getChildAt(mCurrentScreen);
670 View v = focused;
671 while (true) {
672 if (v == current) {
673 super.focusableViewAvailable(focused);
674 return;
675 }
676 if (v == this) {
677 return;
678 }
679 ViewParent parent = v.getParent();
680 if (parent instanceof View) {
681 v = (View)v.getParent();
682 } else {
683 return;
684 }
685 }
686 }
687
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800688 void enableChildrenCache(int fromScreen, int toScreen) {
689 if (fromScreen > toScreen) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800690 fromScreen = toScreen;
691 toScreen = fromScreen;
692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 final int count = getChildCount();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800695
696 fromScreen = Math.max(fromScreen, 0);
697 toScreen = Math.min(toScreen, count - 1);
698
699 for (int i = fromScreen; i <= toScreen; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 final CellLayout layout = (CellLayout) getChildAt(i);
701 layout.setChildrenDrawnWithCacheEnabled(true);
702 layout.setChildrenDrawingCacheEnabled(true);
703 }
704 }
705
706 void clearChildrenCache() {
707 final int count = getChildCount();
708 for (int i = 0; i < count; i++) {
709 final CellLayout layout = (CellLayout) getChildAt(i);
710 layout.setChildrenDrawnWithCacheEnabled(false);
711 }
712 }
713
714 @Override
715 public boolean onTouchEvent(MotionEvent ev) {
Mike Cleronf8bbd342009-10-23 16:15:16 -0700716
Joe Onorato2bc6b7c2009-10-01 14:08:30 -0700717 if (mLauncher.isWorkspaceLocked()) {
718 return false; // We don't want the events. Let them fall through to the all apps view.
719 }
720 if (mLauncher.isAllAppsVisible()) {
721 // Cancel any scrolling that is in progress.
722 if (!mScroller.isFinished()) {
723 mScroller.abortAnimation();
724 }
725 snapToScreen(mCurrentScreen);
Joe Onorato7c312c12009-08-13 21:36:53 -0700726 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 -0800727 }
728
729 if (mVelocityTracker == null) {
730 mVelocityTracker = VelocityTracker.obtain();
731 }
732 mVelocityTracker.addMovement(ev);
733
734 final int action = ev.getAction();
735 final float x = ev.getX();
736
737 switch (action) {
738 case MotionEvent.ACTION_DOWN:
739 /*
740 * If being flinged and user touches, stop the fling. isFinished
741 * will be false if being flinged.
742 */
743 if (!mScroller.isFinished()) {
744 mScroller.abortAnimation();
745 }
746
747 // Remember where the motion event started
748 mLastMotionX = x;
749 break;
750 case MotionEvent.ACTION_MOVE:
751 if (mTouchState == TOUCH_STATE_SCROLLING) {
752 // Scroll to follow the motion event
753 final int deltaX = (int) (mLastMotionX - x);
754 mLastMotionX = x;
755
756 if (deltaX < 0) {
757 if (mScrollX > 0) {
758 scrollBy(Math.max(-mScrollX, deltaX), 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700759 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761 } else if (deltaX > 0) {
762 final int availableToScroll = getChildAt(getChildCount() - 1).getRight() -
763 mScrollX - getWidth();
764 if (availableToScroll > 0) {
765 scrollBy(Math.min(availableToScroll, deltaX), 0);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700766 updateWallpaperOffset();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800768 } else {
769 awakenScrollBars();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
771 }
772 break;
773 case MotionEvent.ACTION_UP:
774 if (mTouchState == TOUCH_STATE_SCROLLING) {
775 final VelocityTracker velocityTracker = mVelocityTracker;
Romain Guya206daa2009-07-06 11:51:18 -0700776 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 int velocityX = (int) velocityTracker.getXVelocity();
778
779 if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
780 // Fling hard enough to move left
781 snapToScreen(mCurrentScreen - 1);
782 } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
783 // Fling hard enough to move right
784 snapToScreen(mCurrentScreen + 1);
785 } else {
786 snapToDestination();
787 }
788
789 if (mVelocityTracker != null) {
790 mVelocityTracker.recycle();
791 mVelocityTracker = null;
792 }
793 }
794 mTouchState = TOUCH_STATE_REST;
795 break;
796 case MotionEvent.ACTION_CANCEL:
797 mTouchState = TOUCH_STATE_REST;
798 }
799
800 return true;
801 }
802
803 private void snapToDestination() {
804 final int screenWidth = getWidth();
805 final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
806
807 snapToScreen(whichScreen);
808 }
809
810 void snapToScreen(int whichScreen) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800811 snapToScreen(whichScreen, true);
812 }
813
814 void snapToScreen(int whichScreen, boolean animate) {
Romain Guye47f55c2009-11-11 19:21:22 -0800815 //if (!mScroller.isFinished()) return;
Romain Guy82d94d92009-05-06 17:43:13 -0700816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800818
819 clearVacantCache();
820 enableChildrenCache(mCurrentScreen, whichScreen);
821
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800822 final int screenDelta = Math.abs(whichScreen - mCurrentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
824 mNextScreen = whichScreen;
Romain Guye47f55c2009-11-11 19:21:22 -0800825
826 mPreviousIndicator.setLevel(mNextScreen);
827 mNextIndicator.setLevel(mNextScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 View focusedChild = getFocusedChild();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800830 if (focusedChild != null && screenDelta != 0 && focusedChild == getChildAt(mCurrentScreen)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 focusedChild.clearFocus();
832 }
833
834 final int newX = whichScreen * getWidth();
835 final int delta = newX - mScrollX;
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800836 final int duration = screenDelta * 300;
837 awakenScrollBars(duration);
Romain Guyf8e6a802009-12-07 17:48:02 -0800838
Joe Onorato14f122b2009-11-19 14:06:36 -0800839 // 1ms is close to don't animate
Romain Guyf8e6a802009-12-07 17:48:02 -0800840 if (!mScroller.isFinished()) mScroller.abortAnimation();
Joe Onorato14f122b2009-11-19 14:06:36 -0800841 mScroller.startScroll(mScrollX, 0, delta, 0, animate ? duration : 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 invalidate();
843 }
844
845 void startDrag(CellLayout.CellInfo cellInfo) {
846 View child = cellInfo.cell;
847
848 // Make sure the drag was started by a long press as opposed to a long click.
Bjorn Bringert7984c942009-12-09 15:38:25 +0000849 if (!child.isInTouchMode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 return;
851 }
852
853 mDragInfo = cellInfo;
854 mDragInfo.screen = mCurrentScreen;
855
856 CellLayout current = ((CellLayout) getChildAt(mCurrentScreen));
857
858 current.onDragChild(child);
Joe Onorato00acb122009-08-04 16:04:30 -0400859 mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 invalidate();
861 }
862
863 @Override
864 protected Parcelable onSaveInstanceState() {
865 final SavedState state = new SavedState(super.onSaveInstanceState());
866 state.currentScreen = mCurrentScreen;
867 return state;
868 }
869
870 @Override
871 protected void onRestoreInstanceState(Parcelable state) {
872 SavedState savedState = (SavedState) state;
873 super.onRestoreInstanceState(savedState.getSuperState());
874 if (savedState.currentScreen != -1) {
875 mCurrentScreen = savedState.currentScreen;
876 Launcher.setScreen(mCurrentScreen);
877 }
878 }
879
880 void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo) {
881 addApplicationShortcut(info, cellInfo, false);
882 }
883
884 void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo,
885 boolean insertAtFirst) {
886 final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen);
887 final int[] result = new int[2];
888
889 layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result);
890 onDropExternal(result[0], result[1], info, layout, insertAtFirst);
891 }
892
Joe Onorato00acb122009-08-04 16:04:30 -0400893 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
894 DragView dragView, Object dragInfo) {
Jeff Sharkey70864282009-04-07 21:08:40 -0700895 final CellLayout cellLayout = getCurrentDropLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 if (source != this) {
897 onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout);
898 } else {
899 // Move internally
900 if (mDragInfo != null) {
901 final View cell = mDragInfo.cell;
Romain Guy52d9cb32009-09-11 17:29:51 -0700902 int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
903 if (index != mDragInfo.screen) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
905 originalCellLayout.removeView(cell);
906 cellLayout.addView(cell);
907 }
Romain Guy263e0192009-05-11 11:50:46 -0700908 mTargetCell = estimateDropCell(x - xOffset, y - yOffset,
Jeff Sharkey70864282009-04-07 21:08:40 -0700909 mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell);
910 cellLayout.onDropChild(cell, mTargetCell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911
Romain Guy84f296c2009-11-04 15:00:44 -0800912 final ItemInfo info = (ItemInfo) cell.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
914 LauncherModel.moveItemInDatabase(mLauncher, info,
Romain Guy52d9cb32009-09-11 17:29:51 -0700915 LauncherSettings.Favorites.CONTAINER_DESKTOP, index, lp.cellX, lp.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 }
917 }
918 }
919
920 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400921 DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -0700922 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400926 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400930 DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -0700931 clearVacantCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) {
935 onDropExternal(x, y, dragInfo, cellLayout, false);
936 }
937
938 private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout,
939 boolean insertAtFirst) {
940 // Drag from somewhere else
941 ItemInfo info = (ItemInfo) dragInfo;
942
943 View view;
944
945 switch (info.itemType) {
946 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
947 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
948 if (info.container == NO_ID) {
949 // Came from all apps -- make a copy
950 info = new ApplicationInfo((ApplicationInfo) info);
951 }
952 view = mLauncher.createShortcut(R.layout.application, cellLayout,
953 (ApplicationInfo) info);
954 break;
955 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
956 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
957 (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info));
958 break;
959 default:
960 throw new IllegalStateException("Unknown item type: " + info.itemType);
961 }
962
963 cellLayout.addView(view, insertAtFirst ? 0 : -1);
Joe Onorato0d44e942009-11-16 18:20:51 -0800964 view.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 view.setOnLongClickListener(mLongClickListener);
Joe Onorato00acb122009-08-04 16:04:30 -0400966 if (view instanceof DropTarget) {
Romain Guy207e40e2009-09-29 16:19:19 -0700967 mDragController.addDropTarget((DropTarget) view);
Joe Onorato00acb122009-08-04 16:04:30 -0400968 }
969
Romain Guy263e0192009-05-11 11:50:46 -0700970 mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell);
Jeff Sharkey70864282009-04-07 21:08:40 -0700971 cellLayout.onDropChild(view, mTargetCell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
975 LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY);
976 }
Jeff Sharkey70864282009-04-07 21:08:40 -0700977
978 /**
979 * Return the current {@link CellLayout}, correctly picking the destination
980 * screen while a scroll is in progress.
981 */
982 private CellLayout getCurrentDropLayout() {
983 int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
984 return (CellLayout) getChildAt(index);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986
Jeff Sharkey70864282009-04-07 21:08:40 -0700987 /**
988 * {@inheritDoc}
989 */
990 public boolean acceptDrop(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -0400991 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Romain Guy4c58c482009-05-12 17:35:41 -0700992 final CellLayout layout = getCurrentDropLayout();
993 final CellLayout.CellInfo cellInfo = mDragInfo;
994 final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
995 final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
996
997 if (mVacantCache == null) {
998 final View ignoreView = cellInfo == null ? null : cellInfo.cell;
999 mVacantCache = layout.findAllVacantCells(null, ignoreView);
1000 }
1001
1002 return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false);
Jeff Sharkey70864282009-04-07 21:08:40 -07001003 }
1004
1005 /**
1006 * {@inheritDoc}
1007 */
1008 public Rect estimateDropLocation(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -04001009 int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) {
Jeff Sharkey70864282009-04-07 21:08:40 -07001010 final CellLayout layout = getCurrentDropLayout();
1011
1012 final CellLayout.CellInfo cellInfo = mDragInfo;
1013 final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1014 final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1015 final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1016
1017 final Rect location = recycle != null ? recycle : new Rect();
1018
1019 // Find drop cell and convert into rectangle
Romain Guy263e0192009-05-11 11:50:46 -07001020 int[] dropCell = estimateDropCell(x - xOffset, y - yOffset,
Jeff Sharkey70864282009-04-07 21:08:40 -07001021 spanX, spanY, ignoreView, layout, mTempCell);
1022
1023 if (dropCell == null) {
1024 return null;
1025 }
1026
1027 layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate);
1028 location.left = mTempEstimate[0];
1029 location.top = mTempEstimate[1];
1030
1031 layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate);
1032 location.right = mTempEstimate[0];
1033 location.bottom = mTempEstimate[1];
1034
1035 return location;
1036 }
1037
1038 /**
1039 * Calculate the nearest cell where the given object would be dropped.
1040 */
Romain Guy263e0192009-05-11 11:50:46 -07001041 private int[] estimateDropCell(int pixelX, int pixelY,
Jeff Sharkey70864282009-04-07 21:08:40 -07001042 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
1043 // Create vacant cell cache if none exists
1044 if (mVacantCache == null) {
1045 mVacantCache = layout.findAllVacantCells(null, ignoreView);
1046 }
1047
1048 // Find the best target drop location
1049 return layout.findNearestVacantArea(pixelX, pixelY,
1050 spanX, spanY, mVacantCache, recycle);
1051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 void setLauncher(Launcher launcher) {
1054 mLauncher = launcher;
1055 }
1056
Joe Onorato00acb122009-08-04 16:04:30 -04001057 public void setDragController(DragController dragController) {
1058 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 public void onDropCompleted(View target, boolean success) {
Romain Guy207e40e2009-09-29 16:19:19 -07001062 clearVacantCache();
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 if (success){
1065 if (target != this && mDragInfo != null) {
1066 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1067 cellLayout.removeView(mDragInfo.cell);
Joe Onorato00acb122009-08-04 16:04:30 -04001068 if (mDragInfo.cell instanceof DropTarget) {
1069 mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1070 }
Romain Guy207e40e2009-09-29 16:19:19 -07001071 //final Object tag = mDragInfo.cell.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
1073 } else {
1074 if (mDragInfo != null) {
1075 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1076 cellLayout.onDropAborted(mDragInfo.cell);
1077 }
1078 }
1079
1080 mDragInfo = null;
1081 }
1082
1083 public void scrollLeft() {
Romain Guy4c58c482009-05-12 17:35:41 -07001084 clearVacantCache();
Romain Guyf8e6a802009-12-07 17:48:02 -08001085 if (mScroller.isFinished()) {
1086 if (mCurrentScreen > 0) snapToScreen(mCurrentScreen - 1);
1087 } else {
1088 if (mNextScreen > 0) snapToScreen(mNextScreen - 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
1090 }
1091
1092 public void scrollRight() {
Romain Guy4c58c482009-05-12 17:35:41 -07001093 clearVacantCache();
Romain Guyf8e6a802009-12-07 17:48:02 -08001094 if (mScroller.isFinished()) {
1095 if (mCurrentScreen < getChildCount() -1) snapToScreen(mCurrentScreen + 1);
1096 } else {
1097 if (mNextScreen < getChildCount() -1) snapToScreen(mNextScreen + 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099 }
1100
1101 public int getScreenForView(View v) {
1102 int result = -1;
1103 if (v != null) {
1104 ViewParent vp = v.getParent();
1105 int count = getChildCount();
1106 for (int i = 0; i < count; i++) {
1107 if (vp == getChildAt(i)) {
1108 return i;
1109 }
1110 }
1111 }
1112 return result;
1113 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 public Folder getFolderForTag(Object tag) {
1116 int screenCount = getChildCount();
1117 for (int screen = 0; screen < screenCount; screen++) {
1118 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1119 int count = currentScreen.getChildCount();
1120 for (int i = 0; i < count; i++) {
1121 View child = currentScreen.getChildAt(i);
1122 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1123 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1124 Folder f = (Folder) child;
1125 if (f.getInfo() == tag) {
1126 return f;
1127 }
1128 }
1129 }
1130 }
1131 return null;
1132 }
1133
1134 public View getViewForTag(Object tag) {
1135 int screenCount = getChildCount();
1136 for (int screen = 0; screen < screenCount; screen++) {
1137 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1138 int count = currentScreen.getChildCount();
1139 for (int i = 0; i < count; i++) {
1140 View child = currentScreen.getChildAt(i);
1141 if (child.getTag() == tag) {
1142 return child;
1143 }
1144 }
1145 }
1146 return null;
1147 }
1148
1149 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 * @return True is long presses are still allowed for the current touch
1151 */
1152 public boolean allowLongPress() {
1153 return mAllowLongPress;
1154 }
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001155
1156 /**
1157 * Set true to allow long-press events to be triggered, usually checked by
1158 * {@link Launcher} to accept or block dpad-initiated long-presses.
1159 */
1160 public void setAllowLongPress(boolean allowLongPress) {
1161 mAllowLongPress = allowLongPress;
1162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163
1164 void removeShortcutsForPackage(String packageName) {
1165 final ArrayList<View> childrenToRemove = new ArrayList<View>();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 final int count = getChildCount();
Romain Guy574d20e2009-06-01 15:34:04 -07001167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 for (int i = 0; i < count; i++) {
1169 final CellLayout layout = (CellLayout) getChildAt(i);
1170 int childCount = layout.getChildCount();
Romain Guy574d20e2009-06-01 15:34:04 -07001171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 childrenToRemove.clear();
Romain Guy574d20e2009-06-01 15:34:04 -07001173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 for (int j = 0; j < childCount; j++) {
1175 final View view = layout.getChildAt(j);
1176 Object tag = view.getTag();
Romain Guy574d20e2009-06-01 15:34:04 -07001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 if (tag instanceof ApplicationInfo) {
Romain Guy574d20e2009-06-01 15:34:04 -07001179 final ApplicationInfo info = (ApplicationInfo) tag;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 // We need to check for ACTION_MAIN otherwise getComponent() might
1181 // return null for some shortcuts (for instance, for shortcuts to
1182 // web pages.)
1183 final Intent intent = info.intent;
1184 final ComponentName name = intent.getComponent();
Romain Guy574d20e2009-06-01 15:34:04 -07001185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
1187 name != null && packageName.equals(name.getPackageName())) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 LauncherModel.deleteItemFromDatabase(mLauncher, info);
1189 childrenToRemove.add(view);
1190 }
Romain Guy574d20e2009-06-01 15:34:04 -07001191 } else if (tag instanceof UserFolderInfo) {
1192 final UserFolderInfo info = (UserFolderInfo) tag;
1193 final ArrayList<ApplicationInfo> contents = info.contents;
1194 final ArrayList<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(1);
1195 final int contentsCount = contents.size();
1196 boolean removedFromFolder = false;
1197
1198 for (int k = 0; k < contentsCount; k++) {
1199 final ApplicationInfo appInfo = contents.get(k);
1200 final Intent intent = appInfo.intent;
1201 final ComponentName name = intent.getComponent();
1202
1203 if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
1204 name != null && packageName.equals(name.getPackageName())) {
1205 toRemove.add(appInfo);
1206 LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
1207 removedFromFolder = true;
1208 }
1209 }
1210
1211 contents.removeAll(toRemove);
1212 if (removedFromFolder) {
1213 final Folder folder = getOpenFolder();
1214 if (folder != null) folder.notifyDataSetChanged();
1215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217 }
Romain Guy574d20e2009-06-01 15:34:04 -07001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 childCount = childrenToRemove.size();
1220 for (int j = 0; j < childCount; j++) {
Joe Onorato00acb122009-08-04 16:04:30 -04001221 View child = childrenToRemove.get(j);
1222 layout.removeViewInLayout(child);
1223 if (child instanceof DropTarget) {
1224 mDragController.removeDropTarget((DropTarget)child);
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
Romain Guy574d20e2009-06-01 15:34:04 -07001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 if (childCount > 0) {
1229 layout.requestLayout();
1230 layout.invalidate();
1231 }
1232 }
1233 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001234
1235 void updateShortcutsForPackage(String packageName) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 final PackageManager pm = mLauncher.getPackageManager();
1237
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001238 final int count = getChildCount();
1239 for (int i = 0; i < count; i++) {
1240 final CellLayout layout = (CellLayout) getChildAt(i);
1241 int childCount = layout.getChildCount();
1242 for (int j = 0; j < childCount; j++) {
1243 final View view = layout.getChildAt(j);
1244 Object tag = view.getTag();
1245 if (tag instanceof ApplicationInfo) {
1246 ApplicationInfo info = (ApplicationInfo) tag;
1247 // We need to check for ACTION_MAIN otherwise getComponent() might
1248 // return null for some shortcuts (for instance, for shortcuts to
1249 // web pages.)
1250 final Intent intent = info.intent;
1251 final ComponentName name = intent.getComponent();
1252 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
1253 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null &&
1254 packageName.equals(name.getPackageName())) {
1255
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 final Drawable icon = AppInfoCache.getIconDrawable(pm, info);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001257 if (icon != null && icon != info.icon) {
1258 info.icon.setCallback(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001259 info.icon = Utilities.createIconThumbnail(icon, mContext);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001260 info.filtered = true;
1261 ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null,
1262 info.icon, null, null);
1263 }
1264 }
1265 }
1266 }
1267 }
1268 }
1269
Joe Onorato14f122b2009-11-19 14:06:36 -08001270 void moveToDefaultScreen(boolean animate) {
1271 snapToScreen(mDefaultScreen, animate);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 getChildAt(mDefaultScreen).requestFocus();
1273 }
1274
Romain Guy8a73c512009-11-09 19:19:59 -08001275 void setIndicators(Drawable previous, Drawable next) {
1276 mPreviousIndicator = previous;
1277 mNextIndicator = next;
1278 previous.setLevel(mCurrentScreen);
1279 next.setLevel(mCurrentScreen);
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 public static class SavedState extends BaseSavedState {
1283 int currentScreen = -1;
1284
1285 SavedState(Parcelable superState) {
1286 super(superState);
1287 }
1288
1289 private SavedState(Parcel in) {
1290 super(in);
1291 currentScreen = in.readInt();
1292 }
1293
1294 @Override
1295 public void writeToParcel(Parcel out, int flags) {
1296 super.writeToParcel(out, flags);
1297 out.writeInt(currentScreen);
1298 }
1299
1300 public static final Parcelable.Creator<SavedState> CREATOR =
1301 new Parcelable.Creator<SavedState>() {
1302 public SavedState createFromParcel(Parcel in) {
1303 return new SavedState(in);
1304 }
1305
1306 public SavedState[] newArray(int size) {
1307 return new SavedState[size];
1308 }
1309 };
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311}