blob: 42a6f1b70d1d0ae61dbbeae26fc1416b35893208 [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
Michael Jurkaaf442092010-06-10 17:01:57 -070019import com.android.launcher.R;
Adam Powell495f2892010-04-16 16:40:55 -070020
Michael Jurka0280c3b2010-09-17 15:00:07 -070021import android.animation.Animator;
Patrick Dubroyce34a972010-10-19 10:34:32 -070022import android.animation.Animator.AnimatorListener;
Michael Jurka18014792010-10-14 09:01:34 -070023import android.animation.AnimatorListenerAdapter;
Chet Haaseb1254a62010-09-07 13:35:00 -070024import android.animation.AnimatorSet;
Michael Jurka0280c3b2010-09-17 15:00:07 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Dianne Hackborn8f573952009-08-10 23:21:09 -070027import android.app.WallpaperManager;
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Adam Powell495f2892010-04-16 16:40:55 -070030import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.Intent;
Adam Powell495f2892010-04-16 16:40:55 -070033import android.content.pm.PackageManager;
Romain Guy5c16f3e2010-01-12 17:24:58 -080034import android.content.pm.ProviderInfo;
Patrick Dubroy7247f632010-08-04 16:02:59 -070035import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070037import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.Canvas;
Michael Jurkaa63c4522010-08-19 13:52:27 -070039import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.Rect;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.graphics.Region.Op;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070042import android.graphics.drawable.Drawable;
Romain Guy5c16f3e2010-01-12 17:24:58 -080043import android.net.Uri;
Joe Onorato956091b2010-02-19 12:47:40 -080044import android.os.IBinder;
Adam Powell495f2892010-04-16 16:40:55 -070045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.util.AttributeSet;
Daniel Sandler291ad122010-05-24 16:03:53 -040047import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.view.View;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070050import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Michael Jurkaaf442092010-06-10 17:01:57 -070052import java.util.ArrayList;
53import java.util.HashSet;
Romain Guyedcce092010-03-04 13:03:17 -080054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
Michael Jurka0142d492010-08-25 17:46:15 -070056 * The workspace is a wide area with a wallpaper and a finite number of pages.
57 * Each page contains a number of icons, folders or widgets the user can
Winson Chungaafa03c2010-06-11 17:34:16 -070058 * interact with. A workspace is meant to be used with a fixed width only.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 */
Michael Jurka0142d492010-08-25 17:46:15 -070060public class Workspace extends SmoothPagedView
Michael Jurkadee05892010-07-27 10:01:56 -070061 implements DropTarget, DragSource, DragScroller, View.OnTouchListener {
Romain Guye47f55c2009-11-11 19:21:22 -080062 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato3a8820b2009-11-10 15:06:42 -080063 private static final String TAG = "Launcher.Workspace";
Michael Jurka0142d492010-08-25 17:46:15 -070064
Michael Jurkadee05892010-07-27 10:01:56 -070065 // This is how much the workspace shrinks when we enter all apps or
66 // customization mode
67 private static final float SHRINK_FACTOR = 0.16f;
Adam Cohena985e592010-09-09 11:23:48 -070068
Adam Cohenf34bab52010-09-30 14:11:56 -070069 // Y rotation to apply to the workspace screens
70 private static final float WORKSPACE_ROTATION = 12.5f;
Adam Cohena985e592010-09-09 11:23:48 -070071
72 // These are extra scale factors to apply to the mini home screens
73 // so as to achieve the desired transform
74 private static final float EXTRA_SCALE_FACTOR_0 = 0.97f;
75 private static final float EXTRA_SCALE_FACTOR_1 = 1.0f;
Adam Cohene54cec82010-09-13 18:19:55 -070076 private static final float EXTRA_SCALE_FACTOR_2 = 1.08f;
Adam Cohena985e592010-09-09 11:23:48 -070077
Adam Cohenf34bab52010-09-30 14:11:56 -070078 private static final int BACKGROUND_FADE_OUT_DELAY = 300;
79 private static final int BACKGROUND_FADE_OUT_DURATION = 300;
80 private static final int BACKGROUND_FADE_IN_DURATION = 100;
81
Michael Jurka3e7c7632010-10-02 16:01:03 -070082 // These animators are used to fade the background
Chet Haase472b2812010-10-14 07:02:04 -070083 private ObjectAnimator mBackgroundFadeInAnimation;
84 private ObjectAnimator mBackgroundFadeOutAnimation;
Adam Cohenf34bab52010-09-30 14:11:56 -070085 private float mBackgroundAlpha = 0;
86
Dianne Hackborn8f573952009-08-10 23:21:09 -070087 private final WallpaperManager mWallpaperManager;
Winson Chungaafa03c2010-06-11 17:34:16 -070088
Michael Jurka0142d492010-08-25 17:46:15 -070089 private int mDefaultPage;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurka79212d82010-07-30 16:36:20 -070091 private boolean mWaitingToShrinkToBottom = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Adam Cohen21f12b52010-10-07 17:15:37 -070093 private boolean mPageMoving = false;
94
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 /**
96 * CellInfo for the cell that is currently being dragged
97 */
98 private CellLayout.CellInfo mDragInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -070099
Jeff Sharkey70864282009-04-07 21:08:40 -0700100 /**
101 * Target drop area calculated during last acceptDrop call.
102 */
103 private int[] mTargetCell = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700105 /**
106 * The CellLayout that is currently being dragged over
107 */
108 private CellLayout mDragTargetLayout = null;
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private Launcher mLauncher;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800111 private IconCache mIconCache;
Joe Onorato00acb122009-08-04 16:04:30 -0400112 private DragController mDragController;
Winson Chungaafa03c2010-06-11 17:34:16 -0700113
Michael Jurka4516c112010-10-07 15:13:47 -0700114 // These are temporary variables to prevent having to allocate a new object just to
115 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private int[] mTempCell = new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700117 private int[] mTempEstimate = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700118 private float[] mTempOriginXY = new float[2];
Michael Jurkaa63c4522010-08-19 13:52:27 -0700119 private float[] mTempDragCoordinates = new float[2];
Michael Jurka4516c112010-10-07 15:13:47 -0700120 private float[] mTempTouchCoordinates = new float[2];
121 private float[] mTempCellLayoutCenterCoordinates = new float[2];
Michael Jurkaa63c4522010-08-19 13:52:27 -0700122 private float[] mTempDragBottomRightCoordinates = new float[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700123 private Matrix mTempInverseMatrix = new Matrix();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Adam Cohend22015c2010-07-26 22:02:18 -0700125 private static final int DEFAULT_CELL_COUNT_X = 4;
126 private static final int DEFAULT_CELL_COUNT_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
Romain Guy8a73c512009-11-09 19:19:59 -0800128 private Drawable mPreviousIndicator;
129 private Drawable mNextIndicator;
Winson Chungaafa03c2010-06-11 17:34:16 -0700130
Patrick Dubroy1262e362010-10-06 15:49:50 -0700131 // State variable that indicates whether the pages are small (ie when you're
Michael Jurkadee05892010-07-27 10:01:56 -0700132 // in all apps or customize mode)
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700133 private boolean mIsSmall = false;
134 private boolean mIsInUnshrinkAnimation = false;
Chet Haaseb1254a62010-09-07 13:35:00 -0700135 private AnimatorListener mUnshrinkAnimationListener;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700136 private enum ShrinkPosition {
137 SHRINK_TO_TOP, SHRINK_TO_MIDDLE, SHRINK_TO_BOTTOM_HIDDEN, SHRINK_TO_BOTTOM_VISIBLE };
138 private ShrinkPosition mShrunkenState;
Michael Jurkadee05892010-07-27 10:01:56 -0700139
Patrick Dubroy1262e362010-10-06 15:49:50 -0700140 private boolean mInScrollArea = false;
141
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700142 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
Joe Onorato4be866d2010-10-10 11:26:02 -0700143 private Bitmap mDragOutline = null;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700144 private final Rect mTempRect = new Rect();
145 private final int[] mTempXY = new int[2];
Joe Onorato4be866d2010-10-10 11:26:02 -0700146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 /**
148 * Used to inflate the Workspace from XML.
149 *
150 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700151 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 */
153 public Workspace(Context context, AttributeSet attrs) {
154 this(context, attrs, 0);
155 }
156
157 /**
158 * Used to inflate the Workspace from XML.
159 *
160 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700161 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 * @param defStyle Unused.
163 */
164 public Workspace(Context context, AttributeSet attrs, int defStyle) {
165 super(context, attrs, defStyle);
Michael Jurka0142d492010-08-25 17:46:15 -0700166 mContentIsRefreshable = false;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700167
168 if (!LauncherApplication.isScreenXLarge()) {
169 mFadeInAdjacentScreens = false;
170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Dianne Hackborn8f573952009-08-10 23:21:09 -0700172 mWallpaperManager = WallpaperManager.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700173
174 TypedArray a = context.obtainStyledAttributes(attrs,
175 R.styleable.Workspace, defStyle, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700176 int cellCountX = a.getInt(R.styleable.Workspace_cellCountX, DEFAULT_CELL_COUNT_X);
177 int cellCountY = a.getInt(R.styleable.Workspace_cellCountY, DEFAULT_CELL_COUNT_Y);
Michael Jurka0142d492010-08-25 17:46:15 -0700178 mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 a.recycle();
180
Adam Cohend22015c2010-07-26 22:02:18 -0700181 LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY);
Joe Onorato0d44e942009-11-16 18:20:51 -0800182 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 initWorkspace();
185 }
186
187 /**
188 * Initializes various states for this workspace.
189 */
Michael Jurka0142d492010-08-25 17:46:15 -0700190 protected void initWorkspace() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 Context context = getContext();
Michael Jurka0142d492010-08-25 17:46:15 -0700192 mCurrentPage = mDefaultPage;
193 Launcher.setScreen(mCurrentPage);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 LauncherApplication app = (LauncherApplication)context.getApplicationContext();
195 mIconCache = app.getIconCache();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Chet Haase472b2812010-10-14 07:02:04 -0700197 mUnshrinkAnimationListener = new AnimatorListenerAdapter() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 public void onAnimationStart(Animator animation) {
199 mIsInUnshrinkAnimation = true;
200 }
Chet Haaseb1254a62010-09-07 13:35:00 -0700201 public void onAnimationEnd(Animator animation) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700202 mIsInUnshrinkAnimation = false;
Michael Jurkadee05892010-07-27 10:01:56 -0700203 }
Michael Jurkadee05892010-07-27 10:01:56 -0700204 };
Michael Jurka0142d492010-08-25 17:46:15 -0700205
206 mSnapVelocity = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 }
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 @Override
Adam Cohenf34bab52010-09-30 14:11:56 -0700210 protected int getScrollMode() {
211 if (LauncherApplication.isScreenXLarge()) {
212 return SmoothPagedView.QUINTIC_MODE;
213 } else {
214 return SmoothPagedView.OVERSHOOT_MODE;
215 }
216 }
217
218 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 public void addView(View child, int index, LayoutParams params) {
220 if (!(child instanceof CellLayout)) {
221 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
222 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700223 ((CellLayout) child).setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 super.addView(child, index, params);
225 }
226
227 @Override
228 public void addView(View child) {
229 if (!(child instanceof CellLayout)) {
230 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
231 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700232 ((CellLayout) child).setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 super.addView(child);
234 }
235
236 @Override
237 public void addView(View child, int index) {
238 if (!(child instanceof CellLayout)) {
239 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
240 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700241 ((CellLayout) child).setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 super.addView(child, index);
243 }
244
245 @Override
246 public void addView(View child, int width, int height) {
247 if (!(child instanceof CellLayout)) {
248 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
249 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700250 ((CellLayout) child).setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 super.addView(child, width, height);
252 }
253
254 @Override
255 public void addView(View child, LayoutParams params) {
256 if (!(child instanceof CellLayout)) {
257 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
258 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700259 ((CellLayout) child).setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 super.addView(child, params);
261 }
262
263 /**
264 * @return The open folder on the current screen, or null if there is none
265 */
266 Folder getOpenFolder() {
Michael Jurka0142d492010-08-25 17:46:15 -0700267 CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
268 int count = currentPage.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 for (int i = 0; i < count; i++) {
Michael Jurka0142d492010-08-25 17:46:15 -0700270 View child = currentPage.getChildAt(i);
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 if (child instanceof Folder) {
272 Folder folder = (Folder) child;
273 if (folder.getInfo().opened)
274 return folder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
276 }
277 return null;
278 }
279
280 ArrayList<Folder> getOpenFolders() {
Michael Jurkadee05892010-07-27 10:01:56 -0700281 final int screenCount = getChildCount();
282 ArrayList<Folder> folders = new ArrayList<Folder>(screenCount);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurkadee05892010-07-27 10:01:56 -0700284 for (int screen = 0; screen < screenCount; screen++) {
Michael Jurka0142d492010-08-25 17:46:15 -0700285 CellLayout currentPage = (CellLayout) getChildAt(screen);
286 int count = currentPage.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 for (int i = 0; i < count; i++) {
Michael Jurka0142d492010-08-25 17:46:15 -0700288 View child = currentPage.getChildAt(i);
Winson Chungaafa03c2010-06-11 17:34:16 -0700289 if (child instanceof Folder) {
290 Folder folder = (Folder) child;
291 if (folder.getInfo().opened)
292 folders.add(folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 break;
294 }
295 }
296 }
297
298 return folders;
299 }
300
Michael Jurka0142d492010-08-25 17:46:15 -0700301 boolean isDefaultPageShowing() {
302 return mCurrentPage == mDefaultPage;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 }
304
305 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 * Sets the current screen.
307 *
Michael Jurka0142d492010-08-25 17:46:15 -0700308 * @param currentPage
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 */
Michael Jurka0142d492010-08-25 17:46:15 -0700310 @Override
311 void setCurrentPage(int currentPage) {
312 super.setCurrentPage(currentPage);
313 updateWallpaperOffset(mScrollX);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
315
316 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 * Adds the specified child in the specified screen. The position and dimension of
318 * the child are defined by x, y, spanX and spanY.
319 *
320 * @param child The child to add in one of the workspace's screens.
321 * @param screen The screen in which to add the child.
322 * @param x The X position of the child in the screen's grid.
323 * @param y The Y position of the child in the screen's grid.
324 * @param spanX The number of cells spanned horizontally by the child.
325 * @param spanY The number of cells spanned vertically by the child.
326 */
327 void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) {
328 addInScreen(child, screen, x, y, spanX, spanY, false);
329 }
330
Winson Chungaafa03c2010-06-11 17:34:16 -0700331 void addInFullScreen(View child, int screen) {
332 addInScreen(child, screen, 0, 0, -1, -1);
333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 /**
336 * Adds the specified child in the specified screen. The position and dimension of
337 * the child are defined by x, y, spanX and spanY.
338 *
339 * @param child The child to add in one of the workspace's screens.
340 * @param screen The screen in which to add the child.
341 * @param x The X position of the child in the screen's grid.
342 * @param y The Y position of the child in the screen's grid.
343 * @param spanX The number of cells spanned horizontally by the child.
344 * @param spanY The number of cells spanned vertically by the child.
345 * @param insert When true, the child is inserted at the beginning of the children list.
346 */
347 void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) {
348 if (screen < 0 || screen >= getChildCount()) {
Daniel Sandler291ad122010-05-24 16:03:53 -0400349 Log.e(TAG, "The screen must be >= 0 and < " + getChildCount()
350 + " (was " + screen + "); skipping child");
351 return;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
354 final CellLayout group = (CellLayout) getChildAt(screen);
355 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
356 if (lp == null) {
357 lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
358 } else {
359 lp.cellX = x;
360 lp.cellY = y;
361 lp.cellHSpan = spanX;
362 lp.cellVSpan = spanY;
363 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700364
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700365 // Get the canonical child id to uniquely represent this view in this screen
Michael Jurka845ba3b2010-09-28 17:09:46 -0700366 int childId = LauncherModel.getCellLayoutChildId(-1, screen, x, y, spanX, spanY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700367 if (!group.addViewToCellLayout(child, insert ? 0 : -1, childId, lp)) {
368 // TODO: This branch occurs when the workspace is adding views
369 // outside of the defined grid
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700370 // maybe we should be deleting these items from the LauncherModel?
Winson Chungaafa03c2010-06-11 17:34:16 -0700371 Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 if (!(child instanceof Folder)) {
Joe Onorato0d44e942009-11-16 18:20:51 -0800375 child.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 child.setOnLongClickListener(mLongClickListener);
377 }
Joe Onorato00acb122009-08-04 16:04:30 -0400378 if (child instanceof DropTarget) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700379 mDragController.addDropTarget((DropTarget) child);
Joe Onorato00acb122009-08-04 16:04:30 -0400380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
382
Michael Jurkadee05892010-07-27 10:01:56 -0700383 public boolean onTouch(View v, MotionEvent event) {
384 // this is an intercepted event being forwarded from a cell layout
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700385 if (mIsSmall || mIsInUnshrinkAnimation) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700386 mLauncher.onWorkspaceClick((CellLayout) v);
Michael Jurka96226222010-08-20 00:54:40 -0700387 return true;
Adam Cohen21f12b52010-10-07 17:15:37 -0700388 } else if (!mPageMoving) {
389 if (v == getChildAt(mCurrentPage - 1)) {
390 snapToPage(mCurrentPage - 1);
391 return true;
392 } else if (v == getChildAt(mCurrentPage + 1)) {
393 snapToPage(mCurrentPage + 1);
394 return true;
395 }
Michael Jurkadee05892010-07-27 10:01:56 -0700396 }
397 return false;
Michael Jurka0e260592010-06-30 17:07:39 -0700398 }
399
Michael Jurka5f1c5092010-09-03 14:15:02 -0700400 @Override
401 public boolean dispatchUnhandledMove(View focused, int direction) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700402 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700403 // when the home screens are shrunken, shouldn't allow side-scrolling
404 return false;
405 }
406 return super.dispatchUnhandledMove(focused, direction);
407 }
408
409 @Override
410 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700411 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700412 // when the home screens are shrunken, shouldn't allow side-scrolling
413 return false;
414 }
415 return super.onInterceptTouchEvent(ev);
416 }
417
Michael Jurka1adf5392010-10-18 18:10:22 -0700418 @Override
419 protected void determineScrollingStart(MotionEvent ev) {
420 if (!mIsSmall && !mIsInUnshrinkAnimation) super.determineScrollingStart(ev);
421 }
422
Patrick Dubroy1262e362010-10-06 15:49:50 -0700423 protected void onPageBeginMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700424 if (mNextPage != INVALID_PAGE) {
425 // we're snapping to a particular screen
Michael Jurka16fed412010-10-01 16:12:03 -0700426 enableChildrenCache(mCurrentPage, mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700427 } else {
428 // this is when user is actively dragging a particular screen, they might
429 // swipe it either left or right (but we won't advance by more than one screen)
Michael Jurka16fed412010-10-01 16:12:03 -0700430 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700432 showOutlines();
Adam Cohen21f12b52010-10-07 17:15:37 -0700433 mPageMoving = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
Patrick Dubroy1262e362010-10-06 15:49:50 -0700436 protected void onPageEndMoving() {
Michael Jurka16fed412010-10-01 16:12:03 -0700437 clearChildrenCache();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700438 // Hide the outlines, as long as we're not dragging
439 if (!mDragController.dragging()) {
440 hideOutlines();
441 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700442 mPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700443 }
444
445 @Override
446 protected void notifyPageSwitchListener() {
447 super.notifyPageSwitchListener();
448
449 if (mPreviousIndicator != null) {
450 // if we know the next page, we show the indication for it right away; it looks
451 // weird if the indicators are lagging
452 int page = mNextPage;
453 if (page == INVALID_PAGE) {
454 page = mCurrentPage;
455 }
456 mPreviousIndicator.setLevel(page);
457 mNextIndicator.setLevel(page);
458 }
459 Launcher.setScreen(mCurrentPage);
460 };
461
Dianne Hackborn8f573952009-08-10 23:21:09 -0700462 private void updateWallpaperOffset() {
Romain Guy798300c2009-08-11 14:43:29 -0700463 updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
464 }
465
466 private void updateWallpaperOffset(int scrollRange) {
Winson Chung86f77532010-08-24 11:08:22 -0700467 final boolean isStaticWallpaper = (mWallpaperManager != null) &&
468 (mWallpaperManager.getWallpaperInfo() == null);
469 if (LauncherApplication.isScreenXLarge() && !isStaticWallpaper) {
470 IBinder token = getWindowToken();
471 if (token != null) {
472 mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 );
473 mWallpaperManager.setWallpaperOffsets(getWindowToken(),
474 Math.max(0.f, Math.min(mScrollX/(float)scrollRange, 1.f)), 0);
475 }
Joe Onorato956091b2010-02-19 12:47:40 -0800476 }
Dianne Hackborn8f573952009-08-10 23:21:09 -0700477 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700478
Adam Cohenf34bab52010-09-30 14:11:56 -0700479 public void showOutlines() {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700480 if (!mIsSmall && !mIsInUnshrinkAnimation) {
481 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel();
482 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel();
Chet Haase472b2812010-10-14 07:02:04 -0700483 mBackgroundFadeInAnimation = ObjectAnimator.ofFloat(this, "backgroundAlpha", 1.0f);
484 mBackgroundFadeInAnimation.setDuration(BACKGROUND_FADE_IN_DURATION);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700485 mBackgroundFadeInAnimation.start();
486 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700487 }
488
489 public void hideOutlines() {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700490 if (!mIsSmall && !mIsInUnshrinkAnimation) {
491 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel();
492 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel();
Chet Haase472b2812010-10-14 07:02:04 -0700493 mBackgroundFadeOutAnimation = ObjectAnimator.ofFloat(this, "backgroundAlpha", 0.0f);
494 mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700495 mBackgroundFadeOutAnimation.setStartDelay(BACKGROUND_FADE_OUT_DELAY);
496 mBackgroundFadeOutAnimation.start();
497 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700498 }
499
500 public void setBackgroundAlpha(float alpha) {
501 mBackgroundAlpha = alpha;
502 for (int i = 0; i < getChildCount(); i++) {
503 CellLayout cl = (CellLayout) getChildAt(i);
504 cl.setBackgroundAlpha(alpha);
505 }
506 }
507
508 public float getBackgroundAlpha() {
509 return mBackgroundAlpha;
510 }
511
512 @Override
513 protected void screenScrolled(int screenCenter) {
Adam Cohen21f12b52010-10-07 17:15:37 -0700514 final int halfScreenSize = getMeasuredWidth() / 2;
515 for (int i = 0; i < getChildCount(); i++) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700516 View v = getChildAt(i);
517 if (v != null) {
Adam Cohen21f12b52010-10-07 17:15:37 -0700518 int totalDistance = v.getMeasuredWidth() + mPageSpacing;
519 int delta = screenCenter - (getChildOffset(i) -
520 getRelativeChildOffset(i) + halfScreenSize);
Adam Cohenf34bab52010-09-30 14:11:56 -0700521
Adam Cohen21f12b52010-10-07 17:15:37 -0700522 float scrollProgress = delta/(totalDistance*1.0f);
523 scrollProgress = Math.min(scrollProgress, 1.0f);
524 scrollProgress = Math.max(scrollProgress, -1.0f);
Adam Cohenf34bab52010-09-30 14:11:56 -0700525
Adam Cohen21f12b52010-10-07 17:15:37 -0700526 float rotation = WORKSPACE_ROTATION * scrollProgress;
527 v.setRotationY(rotation);
Adam Cohenf34bab52010-09-30 14:11:56 -0700528 }
529 }
530 }
531
Joe Onorato00acb122009-08-04 16:04:30 -0400532 protected void onAttachedToWindow() {
533 super.onAttachedToWindow();
Joe Onorato956091b2010-02-19 12:47:40 -0800534 computeScroll();
Joe Onorato00acb122009-08-04 16:04:30 -0400535 mDragController.setWindowToken(getWindowToken());
536 }
537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700539 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurka0142d492010-08-25 17:46:15 -0700540 super.onLayout(changed, left, top, right, bottom);
Winson Chungaafa03c2010-06-11 17:34:16 -0700541
Michael Jurka79212d82010-07-30 16:36:20 -0700542 // if shrinkToBottom() is called on initialization, it has to be deferred
543 // until after the first call to onLayout so that it has the correct width
544 if (mWaitingToShrinkToBottom) {
545 shrinkToBottom(false);
546 mWaitingToShrinkToBottom = false;
547 }
548
Winson Chungaafa03c2010-06-11 17:34:16 -0700549 if (LauncherApplication.isInPlaceRotationEnabled()) {
550 // When the device is rotated, the scroll position of the current screen
551 // needs to be refreshed
Michael Jurka0142d492010-08-25 17:46:15 -0700552 setCurrentPage(getCurrentPage());
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
555
556 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700557 protected void dispatchDraw(Canvas canvas) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700558 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -0700559 // Draw all the workspaces if we're small
560 final int pageCount = getChildCount();
561 final long drawingTime = getDrawingTime();
562 for (int i = 0; i < pageCount; i++) {
563 final View page = (View) getChildAt(i);
564
Michael Jurka0142d492010-08-25 17:46:15 -0700565 drawChild(canvas, page, drawingTime);
566 }
567 } else {
568 super.dispatchDraw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571
572 @Override
573 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Joe Onorato67886212009-09-14 19:05:05 -0400574 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 final Folder openFolder = getOpenFolder();
576 if (openFolder != null) {
577 return openFolder.requestFocus(direction, previouslyFocusedRect);
578 } else {
Michael Jurka0142d492010-08-25 17:46:15 -0700579 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581 }
582 return false;
583 }
584
585 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -0700586 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Joe Onorato67886212009-09-14 19:05:05 -0400587 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 final Folder openFolder = getOpenFolder();
Michael Jurka0142d492010-08-25 17:46:15 -0700589 if (openFolder != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 openFolder.addFocusables(views, direction);
Michael Jurka0142d492010-08-25 17:46:15 -0700591 } else {
592 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
594 }
595 }
596
597 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700598 public boolean dispatchTouchEvent(MotionEvent ev) {
599 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Michael Jurka54dd7542010-07-30 14:47:52 -0700600 // (In XLarge mode, the workspace is shrunken below all apps, and responds to taps
601 // ie when you click on a mini-screen, it zooms back to that screen)
Joe Onorato6b4adbc2010-09-01 12:13:25 -0700602 if (!LauncherApplication.isScreenXLarge() && mLauncher.isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700603 return false;
604 }
605 }
606 return super.dispatchTouchEvent(ev);
607 }
608
Michael Jurka0142d492010-08-25 17:46:15 -0700609 void enableChildrenCache(int fromPage, int toPage) {
610 if (fromPage > toPage) {
611 final int temp = fromPage;
612 fromPage = toPage;
613 toPage = temp;
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800614 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700615
Michael Jurkadee05892010-07-27 10:01:56 -0700616 final int screenCount = getChildCount();
Adam Powellfea5d022010-04-29 11:42:45 -0700617
Michael Jurka0142d492010-08-25 17:46:15 -0700618 fromPage = Math.max(fromPage, 0);
619 toPage = Math.min(toPage, screenCount - 1);
Adam Powellfea5d022010-04-29 11:42:45 -0700620
Michael Jurka0142d492010-08-25 17:46:15 -0700621 for (int i = fromPage; i <= toPage; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final CellLayout layout = (CellLayout) getChildAt(i);
623 layout.setChildrenDrawnWithCacheEnabled(true);
624 layout.setChildrenDrawingCacheEnabled(true);
625 }
626 }
627
628 void clearChildrenCache() {
Michael Jurkadee05892010-07-27 10:01:56 -0700629 final int screenCount = getChildCount();
630 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 final CellLayout layout = (CellLayout) getChildAt(i);
632 layout.setChildrenDrawnWithCacheEnabled(false);
633 }
634 }
635
636 @Override
637 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato2bc6b7c2009-10-01 14:08:30 -0700638 if (mLauncher.isAllAppsVisible()) {
639 // Cancel any scrolling that is in progress.
640 if (!mScroller.isFinished()) {
641 mScroller.abortAnimation();
642 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700643 setCurrentPage(mCurrentPage);
Joe Onorato7c312c12009-08-13 21:36:53 -0700644 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 -0800645 }
646
Michael Jurka0142d492010-08-25 17:46:15 -0700647 return super.onTouchEvent(ev);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700649
Michael Jurka01f0ed42010-08-20 00:41:17 -0700650 public boolean isSmall() {
651 return mIsSmall;
652 }
653
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700654 void shrinkToTop(boolean animated) {
Michael Jurka0142d492010-08-25 17:46:15 -0700655 shrink(ShrinkPosition.SHRINK_TO_TOP, animated);
Michael Jurka01f0ed42010-08-20 00:41:17 -0700656 }
657
658 void shrinkToMiddle() {
Michael Jurka0142d492010-08-25 17:46:15 -0700659 shrink(ShrinkPosition.SHRINK_TO_MIDDLE, true);
Michael Jurka213d9632010-07-28 11:29:25 -0700660 }
661
662 void shrinkToBottom() {
Michael Jurka79212d82010-07-30 16:36:20 -0700663 shrinkToBottom(true);
664 }
665
666 void shrinkToBottom(boolean animated) {
667 if (mFirstLayout) {
668 // (mFirstLayout == "first layout has not happened yet")
669 // if we get a call to shrink() as part of our initialization (for example, if
670 // Launcher is started in All Apps mode) then we need to wait for a layout call
671 // to get our width so we can layout the mini-screen views correctly
672 mWaitingToShrinkToBottom = true;
673 } else {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700674 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN, animated);
Michael Jurka79212d82010-07-30 16:36:20 -0700675 }
Michael Jurka213d9632010-07-28 11:29:25 -0700676 }
677
Adam Cohena985e592010-09-09 11:23:48 -0700678 private float getYScaleForScreen(int screen) {
679 int x = Math.abs(screen - 2);
680
681 // TODO: This should be generalized for use with arbitrary rotation angles.
682 switch(x) {
683 case 0: return EXTRA_SCALE_FACTOR_0;
684 case 1: return EXTRA_SCALE_FACTOR_1;
685 case 2: return EXTRA_SCALE_FACTOR_2;
686 }
687 return 1.0f;
688 }
689
Michael Jurkadee05892010-07-27 10:01:56 -0700690 // we use this to shrink the workspace for the all apps view and the customize view
Michael Jurka0142d492010-08-25 17:46:15 -0700691 private void shrink(ShrinkPosition shrinkPosition, boolean animated) {
Michael Jurkadee05892010-07-27 10:01:56 -0700692 mIsSmall = true;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700693 mShrunkenState = shrinkPosition;
694
695 // Stop any scrolling, move to the current page right away
696 setCurrentPage(mCurrentPage);
697 updateWhichPagesAcceptDrops(mShrunkenState);
698
Michael Jurka5f1c5092010-09-03 14:15:02 -0700699 // we intercept and reject all touch events when we're small, so be sure to reset the state
700 mTouchState = TOUCH_STATE_REST;
701 mActivePointerId = INVALID_POINTER;
702
Patrick Dubroy7247f632010-08-04 16:02:59 -0700703 final Resources res = getResources();
Michael Jurkadee05892010-07-27 10:01:56 -0700704 final int screenWidth = getWidth();
Michael Jurka213d9632010-07-28 11:29:25 -0700705 final int screenHeight = getHeight();
Michael Jurka0142d492010-08-25 17:46:15 -0700706
707 // Making the assumption that all pages have the same width as the 0th
708 final int pageWidth = getChildAt(0).getMeasuredWidth();
709 final int pageHeight = getChildAt(0).getMeasuredHeight();
710
711 final int scaledPageWidth = (int) (SHRINK_FACTOR * pageWidth);
712 final int scaledPageHeight = (int) (SHRINK_FACTOR * pageHeight);
Adam Cohena985e592010-09-09 11:23:48 -0700713 final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing);
Michael Jurkadee05892010-07-27 10:01:56 -0700714
715 final int screenCount = getChildCount();
Adam Cohena985e592010-09-09 11:23:48 -0700716 float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing;
Michael Jurka213d9632010-07-28 11:29:25 -0700717
Winson Chungdf4b83d2010-10-20 17:49:27 -0700718 boolean isPortrait = getMeasuredHeight() > getMeasuredWidth();
719 float newY = (isPortrait ?
720 getResources().getDimension(R.dimen.smallScreenVerticalMarginPortrait) :
721 getResources().getDimension(R.dimen.smallScreenVerticalMarginLandscape));
Michael Jurka3e7c7632010-10-02 16:01:03 -0700722 float finalAlpha = 1.0f;
723 float extraShrinkFactor = 1.0f;
724 if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE) {
725 newY = screenHeight - newY - scaledPageHeight;
726 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN) {
727
728 // We shrink and disappear to nothing in the case of all apps
729 // (which is when we shrink to the bottom)
Michael Jurka0142d492010-08-25 17:46:15 -0700730 newY = screenHeight - newY - scaledPageHeight;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700731 finalAlpha = 0.0f;
732 extraShrinkFactor = 0.1f;
Michael Jurka0142d492010-08-25 17:46:15 -0700733 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_MIDDLE) {
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700734 newY = screenHeight / 2 - scaledPageHeight / 2;
735 finalAlpha = 1.0f;
736 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_TOP) {
737 newY = screenHeight / 10;
Michael Jurka213d9632010-07-28 11:29:25 -0700738 }
Michael Jurkadee05892010-07-27 10:01:56 -0700739
740 // We animate all the screens to the centered position in workspace
741 // At the same time, the screens become greyed/dimmed
742
743 // newX is initialized to the left-most position of the centered screens
Michael Jurka0142d492010-08-25 17:46:15 -0700744 float newX = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2;
Adam Cohena985e592010-09-09 11:23:48 -0700745
746 // We are going to scale about the center of the view, so we need to adjust the positions
747 // of the views accordingly
748 newX -= (pageWidth - scaledPageWidth) / 2.0f;
749 newY -= (pageHeight - scaledPageHeight) / 2.0f;
Michael Jurkadee05892010-07-27 10:01:56 -0700750 for (int i = 0; i < screenCount; i++) {
751 CellLayout cl = (CellLayout) getChildAt(i);
Adam Cohena985e592010-09-09 11:23:48 -0700752
Adam Cohenf34bab52010-09-30 14:11:56 -0700753 float rotation = (-i + 2) * WORKSPACE_ROTATION;
Adam Cohena985e592010-09-09 11:23:48 -0700754 float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f));
755 float rotationScaleY = getYScaleForScreen(i);
756
Michael Jurka79212d82010-07-30 16:36:20 -0700757 if (animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -0700758 final int duration = res.getInteger(R.integer.config_workspaceShrinkTime);
Chet Haase472b2812010-10-14 07:02:04 -0700759 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(cl,
760 PropertyValuesHolder.ofFloat("x", newX),
761 PropertyValuesHolder.ofFloat("y", newY),
762 PropertyValuesHolder.ofFloat("scaleX",
Michael Jurka3e7c7632010-10-02 16:01:03 -0700763 SHRINK_FACTOR * rotationScaleX * extraShrinkFactor),
Chet Haase472b2812010-10-14 07:02:04 -0700764 PropertyValuesHolder.ofFloat("scaleY",
Michael Jurka3e7c7632010-10-02 16:01:03 -0700765 SHRINK_FACTOR * rotationScaleY * extraShrinkFactor),
Chet Haase472b2812010-10-14 07:02:04 -0700766 PropertyValuesHolder.ofFloat("backgroundAlpha", finalAlpha),
767 PropertyValuesHolder.ofFloat("alpha", finalAlpha),
768 PropertyValuesHolder.ofFloat("rotationY", rotation));
769 anim.setDuration(duration);
770 anim.start();
Michael Jurka79212d82010-07-30 16:36:20 -0700771 } else {
772 cl.setX((int)newX);
773 cl.setY((int)newY);
Adam Cohenf34bab52010-09-30 14:11:56 -0700774 cl.setScaleX(SHRINK_FACTOR * rotationScaleX);
775 cl.setScaleY(SHRINK_FACTOR * rotationScaleY);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700776 cl.setBackgroundAlpha(1.0f);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700777 cl.setAlpha(finalAlpha);
Adam Cohena985e592010-09-09 11:23:48 -0700778 cl.setRotationY(rotation);
Michael Jurka79212d82010-07-30 16:36:20 -0700779 }
Michael Jurkadee05892010-07-27 10:01:56 -0700780 // increment newX for the next screen
Adam Cohena985e592010-09-09 11:23:48 -0700781 newX += scaledPageWidth + extraScaledSpacing;
Michael Jurkadee05892010-07-27 10:01:56 -0700782 }
783 setChildrenDrawnWithCacheEnabled(true);
Michael Jurkadee05892010-07-27 10:01:56 -0700784 }
785
Michael Jurka3e7c7632010-10-02 16:01:03 -0700786
787 private void updateWhichPagesAcceptDrops(ShrinkPosition state) {
788 updateWhichPagesAcceptDropsHelper(state, false, 1, 1);
789 }
790
791
792 private void updateWhichPagesAcceptDropsDuringDrag(ShrinkPosition state, int spanX, int spanY) {
793 updateWhichPagesAcceptDropsHelper(state, true, spanX, spanY);
794 }
795
796 private void updateWhichPagesAcceptDropsHelper(
797 ShrinkPosition state, boolean isDragHappening, int spanX, int spanY) {
798 final int screenCount = getChildCount();
799 for (int i = 0; i < screenCount; i++) {
800 CellLayout cl = (CellLayout) getChildAt(i);
801
802 switch (state) {
803 case SHRINK_TO_TOP:
804 if (!isDragHappening) {
805 boolean showDropHighlight = i == mCurrentPage;
806 cl.setAcceptsDrops(showDropHighlight);
807 break;
808 }
809 // otherwise, fall through below and mark non-full screens as accepting drops
810 case SHRINK_TO_BOTTOM_HIDDEN:
811 case SHRINK_TO_BOTTOM_VISIBLE:
812 if (!isDragHappening) {
813 // even if a drag isn't happening, we don't want to show a screen as
814 // accepting drops if it doesn't have at least one free cell
815 spanX = 1;
816 spanY = 1;
817 }
818 // the page accepts drops if we can find at least one empty spot
819 cl.setAcceptsDrops(cl.findCellForSpan(null, spanX, spanY));
820 break;
821 default:
822 throw new RuntimeException(
823 "updateWhichPagesAcceptDropsHelper passed an unhandled ShrinkPosition");
824 }
825 }
826 }
827
828 /*
829 *
830 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithItemMinSize) whenever we
831 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
832 *
833 * These methods mark the appropriate pages as accepting drops (which alters their visual
834 * appearance) and, if the pages are hidden, makes them visible.
835 *
836 */
837 public void onDragStartedWithItemSpans(int spanX, int spanY) {
838 updateWhichPagesAcceptDropsDuringDrag(mShrunkenState, spanX, spanY);
839 if (mShrunkenState == ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN) {
840 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE, true);
841 }
842 }
843
844 public void onDragStartedWithItemMinSize(int minWidth, int minHeight) {
845 int[] spanXY = CellLayout.rectToCell(getResources(), minWidth, minHeight, null);
846 onDragStartedWithItemSpans(spanXY[0], spanXY[1]);
847 }
848
849 // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was
850 // never dragged over
851 public void onDragStopped() {
852 updateWhichPagesAcceptDrops(mShrunkenState);
853 if (mShrunkenState == ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE) {
854 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN, true);
855 }
856 }
857
Michael Jurkadee05892010-07-27 10:01:56 -0700858 // We call this when we trigger an unshrink by clicking on the CellLayout cl
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700859 public void unshrink(CellLayout clThatWasClicked) {
Michael Jurka0142d492010-08-25 17:46:15 -0700860 int newCurrentPage = mCurrentPage;
Michael Jurka4cb37242010-08-09 21:05:32 -0700861 final int screenCount = getChildCount();
862 for (int i = 0; i < screenCount; i++) {
863 if (getChildAt(i) == clThatWasClicked) {
Michael Jurka0142d492010-08-25 17:46:15 -0700864 newCurrentPage = i;
Michael Jurkadee05892010-07-27 10:01:56 -0700865 }
Michael Jurka4cb37242010-08-09 21:05:32 -0700866 }
Michael Jurka0142d492010-08-25 17:46:15 -0700867 unshrink(newCurrentPage);
Michael Jurka4cb37242010-08-09 21:05:32 -0700868 }
869
Adam Cohen21f12b52010-10-07 17:15:37 -0700870 @Override
871 protected boolean handlePagingClicks() {
872 return true;
873 }
874
Michael Jurka0142d492010-08-25 17:46:15 -0700875 private void unshrink(int newCurrentPage) {
Michael Jurka4cb37242010-08-09 21:05:32 -0700876 if (mIsSmall) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700877 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
878 int delta = newX - mScrollX;
Michael Jurka4cb37242010-08-09 21:05:32 -0700879
880 final int screenCount = getChildCount();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700881 for (int i = 0; i < screenCount; i++) {
882 CellLayout cl = (CellLayout) getChildAt(i);
883 cl.setX(cl.getX() + delta);
884 }
Michael Jurka0142d492010-08-25 17:46:15 -0700885 setCurrentPage(newCurrentPage);
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700886 unshrink();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700887 }
Michael Jurkadee05892010-07-27 10:01:56 -0700888 }
889
Michael Jurka4cb37242010-08-09 21:05:32 -0700890 void unshrink() {
891 unshrink(true);
892 }
893
894 void unshrink(boolean animated) {
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700895 if (mIsSmall) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700896 mIsSmall = false;
Chet Haaseb1254a62010-09-07 13:35:00 -0700897 AnimatorSet s = new AnimatorSet();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700898 final int screenCount = getChildCount();
Michael Jurka4cb37242010-08-09 21:05:32 -0700899
900 final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700901 for (int i = 0; i < screenCount; i++) {
Patrick Dubroy7247f632010-08-04 16:02:59 -0700902 final CellLayout cl = (CellLayout)getChildAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700903 float finalAlphaValue = (i == mCurrentPage) ? 1.0f : 0.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700904 float rotation = 0.0f;
905
906 if (i < mCurrentPage) {
907 rotation = WORKSPACE_ROTATION;
908 } else if (i > mCurrentPage) {
909 rotation = -WORKSPACE_ROTATION;
910 }
911
Michael Jurka4cb37242010-08-09 21:05:32 -0700912 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -0700913
Michael Jurka4cb37242010-08-09 21:05:32 -0700914 s.playTogether(
Chet Haase472b2812010-10-14 07:02:04 -0700915 ObjectAnimator.ofFloat(cl, "translationX", 0.0f).setDuration(duration),
916 ObjectAnimator.ofFloat(cl, "translationY", 0.0f).setDuration(duration),
917 ObjectAnimator.ofFloat(cl, "scaleX", 1.0f).setDuration(duration),
918 ObjectAnimator.ofFloat(cl, "scaleY", 1.0f).setDuration(duration),
919 ObjectAnimator.ofFloat(cl, "backgroundAlpha", 0.0f).setDuration(duration),
920 ObjectAnimator.ofFloat(cl, "alpha", finalAlphaValue).setDuration(duration),
921 ObjectAnimator.ofFloat(cl, "rotationY", rotation).setDuration(duration));
Michael Jurka4cb37242010-08-09 21:05:32 -0700922 } else {
923 cl.setTranslationX(0.0f);
924 cl.setTranslationY(0.0f);
925 cl.setScaleX(1.0f);
926 cl.setScaleY(1.0f);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700927 cl.setBackgroundAlpha(0.0f);
Adam Cohenf34bab52010-09-30 14:11:56 -0700928 cl.setAlpha(finalAlphaValue);
929 cl.setRotationY(rotation);
Michael Jurka4cb37242010-08-09 21:05:32 -0700930 }
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700931 }
Michael Jurkac86756c2010-10-10 15:15:47 -0700932 if (animated) {
933 // If we call this when we're not animated, onAnimationEnd is never called on
934 // the listener; make sure we only use the listener when we're actually animating
935 s.addListener(mUnshrinkAnimationListener);
936 s.start();
937 }
Michael Jurkadee05892010-07-27 10:01:56 -0700938 }
Michael Jurkadee05892010-07-27 10:01:56 -0700939 }
940
Joe Onorato4be866d2010-10-10 11:26:02 -0700941 /**
942 * Draw the View v into the given Canvas.
943 *
944 * @param v the view to draw
945 * @param destCanvas the canvas to draw on
946 * @param padding the horizontal and vertical padding to use when drawing
947 */
948 private void drawDragView(View v, Canvas destCanvas, int padding) {
949 final Rect clipRect = mTempRect;
950 v.getDrawingRect(clipRect);
951
952 // For a TextView, adjust the clip rect so that we don't include the text label
953 if (v instanceof TextView) {
954 final int iconHeight = ((TextView)v).getCompoundPaddingTop() - v.getPaddingTop();
955 clipRect.bottom = clipRect.top + iconHeight;
956 }
957
958 // Draw the View into the bitmap.
959 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
960 // they set scrollX and scrollY to large values to achieve centered text
961
962 destCanvas.save();
963 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
964 destCanvas.clipRect(clipRect, Op.REPLACE);
965 v.draw(destCanvas);
966 destCanvas.restore();
967 }
968
969 /**
970 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
971 * Responsibility for the bitmap is transferred to the caller.
972 */
973 private Bitmap createDragOutline(View v, Canvas canvas, int padding) {
974 final int outlineColor = getResources().getColor(R.color.drag_outline_color);
975 final Bitmap b = Bitmap.createBitmap(
976 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
977
978 canvas.setBitmap(b);
979 drawDragView(v, canvas, padding);
980 mOutlineHelper.applyExpensiveOuterOutline(b, canvas, outlineColor, true);
981
982 return b;
983 }
984
985 /**
986 * Returns a new bitmap to show when the given View is being dragged around.
987 * Responsibility for the bitmap is transferred to the caller.
988 */
989 private Bitmap createDragBitmap(View v, Canvas canvas, int padding) {
990 final int outlineColor = getResources().getColor(R.color.drag_outline_color);
991 final Bitmap b = Bitmap.createBitmap(
992 mDragOutline.getWidth(), mDragOutline.getHeight(), Bitmap.Config.ARGB_8888);
993
994 canvas.setBitmap(b);
995 canvas.drawBitmap(mDragOutline, 0, 0, null);
996 drawDragView(v, canvas, padding);
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700997 mOutlineHelper.applyOuterBlur(b, canvas, outlineColor);
Joe Onorato4be866d2010-10-10 11:26:02 -0700998
999 return b;
1000 }
1001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 void startDrag(CellLayout.CellInfo cellInfo) {
1003 View child = cellInfo.cell;
Winson Chungaafa03c2010-06-11 17:34:16 -07001004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 // Make sure the drag was started by a long press as opposed to a long click.
Bjorn Bringert7984c942009-12-09 15:38:25 +00001006 if (!child.isInTouchMode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 return;
1008 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 mDragInfo = cellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001011 mDragInfo.screen = mCurrentPage;
Winson Chungaafa03c2010-06-11 17:34:16 -07001012
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001013 CellLayout current = getCurrentDropLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014
1015 current.onDragChild(child);
Joe Onorato4be866d2010-10-10 11:26:02 -07001016
1017 child.clearFocus();
1018 child.setPressed(false);
1019
1020 final Canvas canvas = new Canvas();
1021
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001022 // We need to add extra padding to the bitmap to make room for the glow effect
1023 final int bitmapPadding = HolographicOutlineHelper.OUTER_BLUR_RADIUS;
1024
Joe Onorato4be866d2010-10-10 11:26:02 -07001025 // The outline is used to visualize where the item will land if dropped
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001026 mDragOutline = createDragOutline(child, canvas, bitmapPadding);
Joe Onorato4be866d2010-10-10 11:26:02 -07001027
1028 // The drag bitmap follows the touch point around on the screen
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001029 final Bitmap b = createDragBitmap(child, canvas, bitmapPadding);
Joe Onorato4be866d2010-10-10 11:26:02 -07001030
1031 final int bmpWidth = b.getWidth();
1032 final int bmpHeight = b.getHeight();
1033 child.getLocationOnScreen(mTempXY);
1034 final int screenX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2;
1035 final int screenY = (int) mTempXY[1] + (child.getHeight() - bmpHeight) / 2;
1036 mDragController.startDrag(b, screenX, screenY, 0, 0, bmpWidth, bmpHeight, this,
1037 child.getTag(), DragController.DRAG_ACTION_MOVE, null);
1038 b.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040
Michael Jurka0280c3b2010-09-17 15:00:07 -07001041 void addApplicationShortcut(ShortcutInfo info, int screen, int cellX, int cellY,
1042 boolean insertAtFirst, int intersectX, int intersectY) {
1043 final CellLayout cellLayout = (CellLayout) getChildAt(screen);
1044 View view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info);
1045
1046 final int[] cellXY = new int[2];
1047 cellLayout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
1048 addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
1049 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
1050 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
1051 cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
Patrick Dubroyce34a972010-10-19 10:34:32 -07001054 private void setPositionForDropAnimation(
1055 View dragView, int dragViewX, int dragViewY, View parent, View child) {
1056 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1057
1058 // Based on the position of the drag view, find the top left of the original view
1059 int viewX = dragViewX + (dragView.getWidth() - child.getWidth()) / 2;
1060 int viewY = dragViewY + (dragView.getHeight() - child.getHeight()) / 2;
1061 viewX -= getResources().getInteger(R.integer.config_dragViewOffsetX);
1062 viewY -= getResources().getInteger(R.integer.config_dragViewOffsetY);
1063
1064 // Set its old pos (in the new parent's coordinates); the CellLayout will
1065 // animate it from this position during the next layout pass
1066 lp.oldX = viewX - (parent.getLeft() - mScrollX);
1067 lp.oldY = viewY - (parent.getTop() - mScrollY);
1068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069
Joe Onorato00acb122009-08-04 16:04:30 -04001070 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
1071 DragView dragView, Object dragInfo) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001072
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 int originX = x - xOffset;
1074 int originY = y - yOffset;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001075
1076 if (mDragTargetLayout == null) {
1077 // Cancel the drag if we're not over a screen at time of drop
1078 if (mDragInfo != null) {
1079 // Set its position so the parent can animate it back
1080 final View parent = getChildAt(mDragInfo.screen);
1081 setPositionForDropAnimation(dragView, originX, originY, parent, mDragInfo.cell);
1082 }
1083 return;
1084 }
1085
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001086 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 // get originX and originY in the local coordinate system of the screen
1088 mTempOriginXY[0] = originX;
1089 mTempOriginXY[1] = originY;
Michael Jurkad718d6a2010-10-14 15:35:17 -07001090 mapPointFromSelfToChild(mDragTargetLayout, mTempOriginXY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 originX = (int)mTempOriginXY[0];
1092 originY = (int)mTempOriginXY[1];
Michael Jurkaa63c4522010-08-19 13:52:27 -07001093 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 if (source != this) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001096 onDropExternal(originX, originY, dragInfo, mDragTargetLayout);
Patrick Dubroyce34a972010-10-19 10:34:32 -07001097 } else if (mDragInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 // Move internally
Patrick Dubroyce34a972010-10-19 10:34:32 -07001099 final View cell = mDragInfo.cell;
1100 mTargetCell = findNearestVacantArea(originX, originY,
1101 mDragInfo.spanX, mDragInfo.spanY, cell, mDragTargetLayout,
1102 mTargetCell);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001103
Patrick Dubroyce34a972010-10-19 10:34:32 -07001104 int screen = indexOfChild(mDragTargetLayout);
1105 if (screen != mDragInfo.screen) {
1106 final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1107 originalCellLayout.removeView(cell);
1108 addInScreen(cell, screen, mTargetCell[0], mTargetCell[1],
1109 mDragInfo.spanX, mDragInfo.spanY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07001111 mDragTargetLayout.onDropChild(cell);
1112
1113 // update the item's position after drop
1114 final ItemInfo info = (ItemInfo) cell.getTag();
1115 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
1116 mDragTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]);
1117 lp.cellX = mTargetCell[0];
1118 lp.cellY = mTargetCell[1];
1119 cell.setId(LauncherModel.getCellLayoutChildId(-1, mDragInfo.screen,
1120 mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY));
1121
1122 LauncherModel.moveItemInDatabase(mLauncher, info,
1123 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
1124 lp.cellX, lp.cellY);
1125
1126 // Prepare it to be animated into its new position
1127 // This must be called after the view has been re-parented
1128 setPositionForDropAnimation(dragView, originX, originY, mDragTargetLayout, cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130 }
1131
Winson Chungaafa03c2010-06-11 17:34:16 -07001132 public void onDragEnter(DragSource source, int x, int y, int xOffset,
1133 int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001134 mDragTargetLayout = null; // Reset the drag state
1135
Michael Jurkad718d6a2010-10-14 15:35:17 -07001136 if (!mIsSmall) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001137 mDragTargetLayout = getCurrentDropLayout();
1138 mDragTargetLayout.onDragEnter(dragView);
Michael Jurkad718d6a2010-10-14 15:35:17 -07001139 showOutlines();
1140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
1142
Michael Jurka18014792010-10-14 09:01:34 -07001143 public DropTarget getDropTargetDelegate(DragSource source, int x, int y,
1144 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy440c3602010-07-13 17:50:32 -07001145
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001146 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001147 // If we're shrunken, don't let anyone drag on folders/etc that are on the mini-screens
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 return null;
1149 }
Patrick Dubroy440c3602010-07-13 17:50:32 -07001150 // We may need to delegate the drag to a child view. If a 1x1 item
1151 // would land in a cell occupied by a DragTarget (e.g. a Folder),
1152 // then drag events should be handled by that child.
1153
1154 ItemInfo item = (ItemInfo)dragInfo;
1155 CellLayout currentLayout = getCurrentDropLayout();
1156
1157 int dragPointX, dragPointY;
1158 if (item.spanX == 1 && item.spanY == 1) {
1159 // For a 1x1, calculate the drop cell exactly as in onDragOver
1160 dragPointX = x - xOffset;
1161 dragPointY = y - yOffset;
1162 } else {
1163 // Otherwise, use the exact drag coordinates
1164 dragPointX = x;
1165 dragPointY = y;
1166 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 dragPointX += mScrollX - currentLayout.getLeft();
1168 dragPointY += mScrollY - currentLayout.getTop();
Patrick Dubroy440c3602010-07-13 17:50:32 -07001169
1170 // If we are dragging over a cell that contains a DropTarget that will
1171 // accept the drop, delegate to that DropTarget.
1172 final int[] cellXY = mTempCell;
1173 currentLayout.estimateDropCell(dragPointX, dragPointY, item.spanX, item.spanY, cellXY);
1174 View child = currentLayout.getChildAt(cellXY[0], cellXY[1]);
1175 if (child instanceof DropTarget) {
1176 DropTarget target = (DropTarget)child;
1177 if (target.acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
1178 return target;
1179 }
1180 }
1181 return null;
1182 }
1183
Michael Jurka4516c112010-10-07 15:13:47 -07001184 /*
1185 *
1186 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
1187 * coordinate space. The argument xy is modified with the return result.
1188 *
1189 */
1190 void mapPointFromSelfToChild(View v, float[] xy) {
1191 mapPointFromSelfToChild(v, xy, null);
1192 }
1193
1194 /*
1195 *
1196 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
1197 * coordinate space. The argument xy is modified with the return result.
1198 *
1199 * if cachedInverseMatrix is not null, this method will just use that matrix instead of
Michael Jurkad718d6a2010-10-14 15:35:17 -07001200 * computing it itself; we use this to avoid redundant matrix inversions in
Michael Jurka4516c112010-10-07 15:13:47 -07001201 * findMatchingPageForDragOver
1202 *
1203 */
1204 void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
1205 if (cachedInverseMatrix == null) {
1206 v.getMatrix().invert(mTempInverseMatrix);
1207 cachedInverseMatrix = mTempInverseMatrix;
1208 }
1209 xy[0] = xy[0] + mScrollX - v.getLeft();
1210 xy[1] = xy[1] + mScrollY - v.getTop();
1211 cachedInverseMatrix.mapPoints(xy);
1212 }
1213
1214 /*
1215 *
1216 * Convert the 2D coordinate xy from this CellLayout's coordinate space to
1217 * the parent View's coordinate space. The argument xy is modified with the return result.
1218 *
1219 */
1220 void mapPointFromChildToSelf(View v, float[] xy) {
1221 v.getMatrix().mapPoints(xy);
1222 xy[0] -= (mScrollX - v.getLeft());
1223 xy[1] -= (mScrollY - v.getTop());
1224 }
1225
1226 static private float squaredDistance(float[] point1, float[] point2) {
1227 float distanceX = point1[0] - point2[0];
1228 float distanceY = point2[1] - point2[1];
1229 return distanceX * distanceX + distanceY * distanceY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 }
1231
Michael Jurka4516c112010-10-07 15:13:47 -07001232 /*
1233 *
1234 * Returns true if the passed CellLayout cl overlaps with dragView
1235 *
1236 */
1237 boolean overlaps(CellLayout cl, DragView dragView,
1238 int dragViewX, int dragViewY, Matrix cachedInverseMatrix) {
1239 // Transform the coordinates of the item being dragged to the CellLayout's coordinates
1240 final float[] draggedItemTopLeft = mTempDragCoordinates;
1241 draggedItemTopLeft[0] = dragViewX + dragView.getScaledDragRegionXOffset();
1242 draggedItemTopLeft[1] = dragViewY + dragView.getScaledDragRegionYOffset();
1243 final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates;
1244 draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getScaledDragRegionWidth();
1245 draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getScaledDragRegionHeight();
Michael Jurkaa63c4522010-08-19 13:52:27 -07001246
Michael Jurka4516c112010-10-07 15:13:47 -07001247 // Transform the dragged item's top left coordinates
1248 // to the CellLayout's local coordinates
1249 mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix);
1250 float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]);
1251 float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]);
1252
1253 if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) {
1254 // Transform the dragged item's bottom right coordinates
1255 // to the CellLayout's local coordinates
1256 mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix);
1257 float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]);
1258 float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]);
1259
1260 if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) {
1261 float overlap = (overlapRegionRight - overlapRegionLeft) *
1262 (overlapRegionBottom - overlapRegionTop);
1263 if (overlap > 0) {
1264 return true;
1265 }
1266 }
1267 }
1268 return false;
1269 }
1270
1271 /*
1272 *
1273 * This method returns the CellLayout that is currently being dragged to. In order to drag
1274 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
1275 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
1276 *
1277 * Return null if no CellLayout is currently being dragged over
1278 *
1279 */
1280 private CellLayout findMatchingPageForDragOver(
1281 DragView dragView, int originX, int originY, int offsetX, int offsetY) {
1282 // We loop through all the screens (ie CellLayouts) and see which ones overlap
1283 // with the item being dragged and then choose the one that's closest to the touch point
Michael Jurkaa63c4522010-08-19 13:52:27 -07001284 final int screenCount = getChildCount();
1285 CellLayout bestMatchingScreen = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 float smallestDistSoFar = Float.MAX_VALUE;
Michael Jurka4516c112010-10-07 15:13:47 -07001287
Michael Jurkaa63c4522010-08-19 13:52:27 -07001288 for (int i = 0; i < screenCount; i++) {
1289 CellLayout cl = (CellLayout)getChildAt(i);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001290
Michael Jurka4516c112010-10-07 15:13:47 -07001291 final float[] touchXy = mTempTouchCoordinates;
1292 touchXy[0] = originX + offsetX;
1293 touchXy[1] = originY + offsetY;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001294
Michael Jurka4516c112010-10-07 15:13:47 -07001295 // Transform the touch coordinates to the CellLayout's local coordinates
1296 // If the touch point is within the bounds of the cell layout, we can return immediately
Michael Jurka0280c3b2010-09-17 15:00:07 -07001297 cl.getMatrix().invert(mTempInverseMatrix);
Michael Jurka4516c112010-10-07 15:13:47 -07001298 mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001299
Michael Jurka4516c112010-10-07 15:13:47 -07001300 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
1301 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
1302 return cl;
1303 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001304
Michael Jurka4516c112010-10-07 15:13:47 -07001305 if (overlaps(cl, dragView, originX, originY, mTempInverseMatrix)) {
1306 // Get the center of the cell layout in screen coordinates
1307 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
1308 cellLayoutCenter[0] = cl.getWidth()/2;
1309 cellLayoutCenter[1] = cl.getHeight()/2;
1310 mapPointFromChildToSelf(cl, cellLayoutCenter);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001311
Michael Jurka4516c112010-10-07 15:13:47 -07001312 touchXy[0] = originX + offsetX;
1313 touchXy[1] = originY + offsetY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314
Michael Jurka4516c112010-10-07 15:13:47 -07001315 // Calculate the distance between the center of the CellLayout
1316 // and the touch point
1317 float dist = squaredDistance(touchXy, cellLayoutCenter);
1318
1319 if (dist < smallestDistSoFar) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 smallestDistSoFar = dist;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001321 bestMatchingScreen = cl;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001322 }
Michael Jurka4516c112010-10-07 15:13:47 -07001323 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001324 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001325 return bestMatchingScreen;
1326 }
1327
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001328 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
1329 DragView dragView, Object dragInfo) {
Patrick Dubroy1262e362010-10-06 15:49:50 -07001330 // When touch is inside the scroll area, skip dragOver actions for the current screen
1331 if (!mInScrollArea) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001332 CellLayout layout;
1333 int originX = x - xOffset;
1334 int originY = y - yOffset;
1335 if (mIsSmall || mIsInUnshrinkAnimation) {
1336 layout = findMatchingPageForDragOver(
1337 dragView, originX, originY, xOffset, yOffset);
Patrick Dubroy1262e362010-10-06 15:49:50 -07001338
Michael Jurkad718d6a2010-10-14 15:35:17 -07001339 if (layout != mDragTargetLayout) {
1340 if (mDragTargetLayout != null) {
1341 mDragTargetLayout.setHover(false);
1342 }
1343 mDragTargetLayout = layout;
1344 if (mDragTargetLayout != null) {
1345 mDragTargetLayout.setHover(true);
1346 }
1347 }
1348 } else {
1349 layout = getCurrentDropLayout();
1350
1351 final ItemInfo item = (ItemInfo)dragInfo;
1352 if (dragInfo instanceof LauncherAppWidgetInfo) {
1353 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo)dragInfo;
1354
1355 if (widgetInfo.spanX == -1) {
1356 // Calculate the grid spans needed to fit this widget
1357 int[] spans = layout.rectToCell(
1358 widgetInfo.minWidth, widgetInfo.minHeight, null);
1359 item.spanX = spans[0];
1360 item.spanY = spans[1];
1361 }
1362 }
1363
1364 if (source instanceof AllAppsPagedView) {
1365 // This is a hack to fix the point used to determine which cell an icon from
1366 // the all apps screen is over
1367 if (item != null && item.spanX == 1 && layout != null) {
1368 int dragRegionLeft = (dragView.getWidth() - layout.getCellWidth()) / 2;
1369
1370 originX += dragRegionLeft - dragView.getDragRegionLeft();
1371 if (dragView.getDragRegionWidth() != layout.getCellWidth()) {
1372 dragView.setDragRegion(dragView.getDragRegionLeft(),
1373 dragView.getDragRegionTop(),
1374 layout.getCellWidth(),
1375 dragView.getDragRegionHeight());
1376 }
1377 }
1378 }
1379
1380 if (layout != mDragTargetLayout) {
1381 if (mDragTargetLayout != null) {
1382 mDragTargetLayout.onDragExit();
1383 }
1384 layout.onDragEnter(dragView);
1385 mDragTargetLayout = layout;
1386 }
1387
1388 // only visualize the drop locations for moving icons within the home screen on
1389 // tablet on phone, we also visualize icons dragged in from All Apps
1390 if ((!LauncherApplication.isScreenXLarge() || source == this)
1391 && mDragTargetLayout != null) {
1392 final View child = (mDragInfo == null) ? null : mDragInfo.cell;
1393 int localOriginX = originX - (mDragTargetLayout.getLeft() - mScrollX);
1394 int localOriginY = originY - (mDragTargetLayout.getTop() - mScrollY);
1395 mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
1396 localOriginX, localOriginY, item.spanX, item.spanY);
1397 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001398 }
Patrick Dubroy976ebec2010-08-04 20:03:37 -07001399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
Winson Chungaafa03c2010-06-11 17:34:16 -07001402 public void onDragExit(DragSource source, int x, int y, int xOffset,
1403 int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001404 if (mDragTargetLayout != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 mDragTargetLayout.onDragExit();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001406 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001407 if (!mIsPageMoving) {
1408 hideOutlines();
1409 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001410 clearAllHovers();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
1412
Winson Chungaafa03c2010-06-11 17:34:16 -07001413 private void onDropExternal(int x, int y, Object dragInfo,
1414 CellLayout cellLayout) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 onDropExternal(x, y, dragInfo, cellLayout, false);
1416 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001417
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001418 /**
1419 * Add the item specified by dragInfo to the given layout.
1420 * This is basically the equivalent of onDropExternal, except it's not initiated
1421 * by drag and drop.
1422 * @return true if successful
1423 */
1424 public boolean addExternalItemToScreen(Object dragInfo, View layout) {
1425 CellLayout cl = (CellLayout) layout;
1426 ItemInfo info = (ItemInfo) dragInfo;
1427
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 if (cl.findCellForSpan(mTempEstimate, info.spanX, info.spanY)) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001429 onDropExternal(0, 0, dragInfo, cl, false);
1430 return true;
1431 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 mLauncher.showOutOfSpaceMessage();
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001433 return false;
1434 }
1435
Michael Jurka0280c3b2010-09-17 15:00:07 -07001436 // Drag from somewhere else
Joe Onorato4be866d2010-10-10 11:26:02 -07001437 // NOTE: This can also be called when we are outside of a drag event, when we want
1438 // to add an item to one of the workspace screens.
Winson Chungaafa03c2010-06-11 17:34:16 -07001439 private void onDropExternal(int x, int y, Object dragInfo,
1440 CellLayout cellLayout, boolean insertAtFirst) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 int screen = indexOfChild(cellLayout);
1442 if (dragInfo instanceof PendingAddItemInfo) {
1443 PendingAddItemInfo info = (PendingAddItemInfo) dragInfo;
1444 // When dragging and dropping from customization tray, we deal with creating
1445 // widgets/shortcuts/folders in a slightly different way
1446 int[] touchXY = new int[2];
1447 touchXY[0] = x;
1448 touchXY[1] = y;
1449 switch (info.itemType) {
1450 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1451 mLauncher.addAppWidgetFromDrop(info.componentName, screen, touchXY);
1452 break;
1453 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1454 mLauncher.addLiveFolderFromDrop(info.componentName, screen, touchXY);
1455 break;
1456 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1457 mLauncher.processShortcutFromDrop(info.componentName, screen, touchXY);
1458 break;
1459 default:
1460 throw new IllegalStateException("Unknown item type: " + info.itemType);
1461 }
1462 cellLayout.onDragExit();
Michael Jurka18014792010-10-14 09:01:34 -07001463 cellLayout.animateDrop();
Michael Jurka0280c3b2010-09-17 15:00:07 -07001464 return;
1465 }
1466
1467 // This is for other drag/drop cases, like dragging from All Apps
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 ItemInfo info = (ItemInfo) dragInfo;
1469
Michael Jurkaaf442092010-06-10 17:01:57 -07001470 View view = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
1472 switch (info.itemType) {
1473 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1474 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onoratoe48e7c12010-02-19 13:10:40 -08001475 if (info.container == NO_ID && info instanceof ApplicationInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 // Came from all apps -- make a copy
Winson Chungaafa03c2010-06-11 17:34:16 -07001477 info = new ShortcutInfo((ApplicationInfo) info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001479 view = mLauncher.createShortcut(R.layout.application, cellLayout,
1480 (ShortcutInfo) info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 break;
1482 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1483 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001484 cellLayout, ((UserFolderInfo) info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001485 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 default:
Michael Jurka0280c3b2010-09-17 15:00:07 -07001487 throw new IllegalStateException("Unknown item type: " + info.itemType);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001490 // If the view is null, it has already been added.
1491 if (view == null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492 cellLayout.onDragExit();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001493 } else {
Michael Jurka7ded67c2010-09-28 13:57:39 -07001494 mTargetCell = findNearestVacantArea(x, y, 1, 1, null, cellLayout, mTargetCell);
Winson Chungaafa03c2010-06-11 17:34:16 -07001495 addInScreen(view, indexOfChild(cellLayout), mTargetCell[0],
1496 mTargetCell[1], info.spanX, info.spanY, insertAtFirst);
1497 cellLayout.onDropChild(view);
Michael Jurka18014792010-10-14 09:01:34 -07001498 cellLayout.animateDrop();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001499 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -07001500
1501 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
Winson Chungaafa03c2010-06-11 17:34:16 -07001503 lp.cellX, lp.cellY);
Joe Onorato00acb122009-08-04 16:04:30 -04001504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001506
Jeff Sharkey70864282009-04-07 21:08:40 -07001507 /**
1508 * Return the current {@link CellLayout}, correctly picking the destination
1509 * screen while a scroll is in progress.
1510 */
1511 private CellLayout getCurrentDropLayout() {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001512 // if we're currently small, use findMatchingPageForDragOver instead
1513 if (mIsSmall) return null;
Michael Jurka0142d492010-08-25 17:46:15 -07001514 int index = mScroller.isFinished() ? mCurrentPage : mNextPage;
Jeff Sharkey70864282009-04-07 21:08:40 -07001515 return (CellLayout) getChildAt(index);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
Jeff Sharkey70864282009-04-07 21:08:40 -07001518 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 * Return the current CellInfo describing our current drag; this method exists
1520 * so that Launcher can sync this object with the correct info when the activity is created/
1521 * destroyed
1522 *
1523 */
1524 public CellLayout.CellInfo getDragInfo() {
1525 return mDragInfo;
1526 }
1527
1528 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001529 * {@inheritDoc}
1530 */
1531 public boolean acceptDrop(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -04001532 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001533 if (mDragTargetLayout == null) {
1534 // cancel the drag if we're not over a screen at time of drop
1535 return false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001536 }
Michael Jurkad718d6a2010-10-14 15:35:17 -07001537
Michael Jurkac28de512010-08-13 11:27:44 -07001538 final CellLayout.CellInfo dragCellInfo = mDragInfo;
1539 final int spanX = dragCellInfo == null ? 1 : dragCellInfo.spanX;
1540 final int spanY = dragCellInfo == null ? 1 : dragCellInfo.spanY;
Romain Guy4c58c482009-05-12 17:35:41 -07001541
Michael Jurkac28de512010-08-13 11:27:44 -07001542 final View ignoreView = dragCellInfo == null ? null : dragCellInfo.cell;
Romain Guy4c58c482009-05-12 17:35:41 -07001543
Michael Jurkad718d6a2010-10-14 15:35:17 -07001544 if (mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) {
Michael Jurka0e260592010-06-30 17:07:39 -07001545 return true;
1546 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 mLauncher.showOutOfSpaceMessage();
Michael Jurka0e260592010-06-30 17:07:39 -07001548 return false;
1549 }
Jeff Sharkey70864282009-04-07 21:08:40 -07001550 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001551
Jeff Sharkey70864282009-04-07 21:08:40 -07001552 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001553 * Calculate the nearest cell where the given object would be dropped.
1554 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001555 private int[] findNearestVacantArea(int pixelX, int pixelY,
Jeff Sharkey70864282009-04-07 21:08:40 -07001556 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001557
Michael Jurka5f1c5092010-09-03 14:15:02 -07001558 int localPixelX = pixelX - (layout.getLeft() - mScrollX);
1559 int localPixelY = pixelY - (layout.getTop() - mScrollY);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001560
Jeff Sharkey70864282009-04-07 21:08:40 -07001561 // Find the best target drop location
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 return layout.findNearestVacantArea(
Michael Jurkafc9f07d2010-09-30 13:22:31 -07001563 localPixelX, localPixelY, spanX, spanY, ignoreView, recycle);
Jeff Sharkey70864282009-04-07 21:08:40 -07001564 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001565
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001566 /**
1567 * Estimate the size that a child with the given dimensions will take in the current screen.
1568 */
1569 void estimateChildSize(int minWidth, int minHeight, int[] result) {
Michael Jurka0142d492010-08-25 17:46:15 -07001570 ((CellLayout)getChildAt(mCurrentPage)).estimateChildSize(minWidth, minHeight, result);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001571 }
1572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 void setLauncher(Launcher launcher) {
1574 mLauncher = launcher;
1575 }
1576
Joe Onorato00acb122009-08-04 16:04:30 -04001577 public void setDragController(DragController dragController) {
1578 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580
1581 public void onDropCompleted(View target, boolean success) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001582 if (success) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 if (target != this && mDragInfo != null) {
1584 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1585 cellLayout.removeView(mDragInfo.cell);
Joe Onorato00acb122009-08-04 16:04:30 -04001586 if (mDragInfo.cell instanceof DropTarget) {
1587 mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1588 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001589 // final Object tag = mDragInfo.cell.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07001591 } else if (mDragInfo != null) {
1592 ((CellLayout) getChildAt(mDragInfo.screen)).onDropAborted(mDragInfo.cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594
Joe Onorato4be866d2010-10-10 11:26:02 -07001595 mDragOutline = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 mDragInfo = null;
1597 }
1598
Michael Jurka0280c3b2010-09-17 15:00:07 -07001599 public boolean isDropEnabled() {
1600 return true;
1601 }
1602
Michael Jurka0142d492010-08-25 17:46:15 -07001603 @Override
1604 protected void onRestoreInstanceState(Parcelable state) {
1605 super.onRestoreInstanceState(state);
1606 Launcher.setScreen(mCurrentPage);
1607 }
1608
1609 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 public void scrollLeft() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001611 if (!mIsSmall && !mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -07001612 super.scrollLeft();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
1614 }
1615
Michael Jurka0142d492010-08-25 17:46:15 -07001616 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 public void scrollRight() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001618 if (!mIsSmall && !mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -07001619 super.scrollRight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
1621 }
1622
Patrick Dubroy1262e362010-10-06 15:49:50 -07001623 @Override
1624 public void onEnterScrollArea(int direction) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001625 if (!mIsSmall && !mIsInUnshrinkAnimation) {
1626 mInScrollArea = true;
1627 final int screen = getCurrentPage() + ((direction == DragController.SCROLL_LEFT) ? -1 : 1);
1628 if (0 <= screen && screen < getChildCount()) {
1629 ((CellLayout) getChildAt(screen)).setHover(true);
1630 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001631
Michael Jurkad718d6a2010-10-14 15:35:17 -07001632 if (mDragTargetLayout != null) {
1633 mDragTargetLayout.onDragExit();
1634 mDragTargetLayout = null;
1635 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001636 }
1637 }
1638
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001639 private void clearAllHovers() {
1640 final int childCount = getChildCount();
1641 for (int i = 0; i < childCount; i++) {
1642 ((CellLayout) getChildAt(i)).setHover(false);
1643 }
1644 }
1645
Patrick Dubroy1262e362010-10-06 15:49:50 -07001646 @Override
1647 public void onExitScrollArea() {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001648 if (mInScrollArea) {
1649 mInScrollArea = false;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001650 clearAllHovers();
Patrick Dubroy1262e362010-10-06 15:49:50 -07001651 }
1652 }
1653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 public Folder getFolderForTag(Object tag) {
Michael Jurkadee05892010-07-27 10:01:56 -07001655 final int screenCount = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 for (int screen = 0; screen < screenCount; screen++) {
1657 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1658 int count = currentScreen.getChildCount();
1659 for (int i = 0; i < count; i++) {
1660 View child = currentScreen.getChildAt(i);
1661 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1662 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1663 Folder f = (Folder) child;
Winson Chungaafa03c2010-06-11 17:34:16 -07001664 if (f.getInfo() == tag && f.getInfo().opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return f;
1666 }
1667 }
1668 }
1669 }
1670 return null;
1671 }
1672
1673 public View getViewForTag(Object tag) {
1674 int screenCount = getChildCount();
1675 for (int screen = 0; screen < screenCount; screen++) {
1676 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1677 int count = currentScreen.getChildCount();
1678 for (int i = 0; i < count; i++) {
1679 View child = currentScreen.getChildAt(i);
1680 if (child.getTag() == tag) {
1681 return child;
1682 }
1683 }
1684 }
1685 return null;
1686 }
1687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
Joe Onorato64e6be72010-03-05 15:05:52 -05001689 void removeItems(final ArrayList<ApplicationInfo> apps) {
Michael Jurkadee05892010-07-27 10:01:56 -07001690 final int screenCount = getChildCount();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001691 final PackageManager manager = getContext().getPackageManager();
Romain Guy629de3e2010-01-13 12:20:59 -08001692 final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
Romain Guy574d20e2009-06-01 15:34:04 -07001693
Joe Onorato64e6be72010-03-05 15:05:52 -05001694 final HashSet<String> packageNames = new HashSet<String>();
1695 final int appCount = apps.size();
1696 for (int i = 0; i < appCount; i++) {
1697 packageNames.add(apps.get(i).componentName.getPackageName());
1698 }
1699
Michael Jurkadee05892010-07-27 10:01:56 -07001700 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 final CellLayout layout = (CellLayout) getChildAt(i);
Romain Guy574d20e2009-06-01 15:34:04 -07001702
Romain Guy629de3e2010-01-13 12:20:59 -08001703 // Avoid ANRs by treating each screen separately
1704 post(new Runnable() {
1705 public void run() {
1706 final ArrayList<View> childrenToRemove = new ArrayList<View>();
1707 childrenToRemove.clear();
Winson Chungaafa03c2010-06-11 17:34:16 -07001708
Romain Guy629de3e2010-01-13 12:20:59 -08001709 int childCount = layout.getChildCount();
1710 for (int j = 0; j < childCount; j++) {
1711 final View view = layout.getChildAt(j);
1712 Object tag = view.getTag();
Winson Chungaafa03c2010-06-11 17:34:16 -07001713
Joe Onorato0589f0f2010-02-08 13:44:00 -08001714 if (tag instanceof ShortcutInfo) {
1715 final ShortcutInfo info = (ShortcutInfo) tag;
Romain Guy629de3e2010-01-13 12:20:59 -08001716 final Intent intent = info.intent;
1717 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07001718
Joe Onorato64e6be72010-03-05 15:05:52 -05001719 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1720 for (String packageName: packageNames) {
1721 if (packageName.equals(name.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001722 LauncherModel.deleteItemFromDatabase(mLauncher, info);
1723 childrenToRemove.add(view);
1724 }
1725 }
Romain Guy629de3e2010-01-13 12:20:59 -08001726 }
1727 } else if (tag instanceof UserFolderInfo) {
1728 final UserFolderInfo info = (UserFolderInfo) tag;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001729 final ArrayList<ShortcutInfo> contents = info.contents;
1730 final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
Romain Guy629de3e2010-01-13 12:20:59 -08001731 final int contentsCount = contents.size();
1732 boolean removedFromFolder = false;
Winson Chungaafa03c2010-06-11 17:34:16 -07001733
Romain Guy629de3e2010-01-13 12:20:59 -08001734 for (int k = 0; k < contentsCount; k++) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001735 final ShortcutInfo appInfo = contents.get(k);
Romain Guy629de3e2010-01-13 12:20:59 -08001736 final Intent intent = appInfo.intent;
1737 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07001738
Joe Onorato64e6be72010-03-05 15:05:52 -05001739 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1740 for (String packageName: packageNames) {
1741 if (packageName.equals(name.getPackageName())) {
1742 toRemove.add(appInfo);
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -07001743 LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
Joe Onorato64e6be72010-03-05 15:05:52 -05001744 removedFromFolder = true;
1745 }
1746 }
Romain Guy629de3e2010-01-13 12:20:59 -08001747 }
1748 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001749
Romain Guy629de3e2010-01-13 12:20:59 -08001750 contents.removeAll(toRemove);
1751 if (removedFromFolder) {
1752 final Folder folder = getOpenFolder();
Winson Chungaafa03c2010-06-11 17:34:16 -07001753 if (folder != null)
1754 folder.notifyDataSetChanged();
Romain Guy629de3e2010-01-13 12:20:59 -08001755 }
1756 } else if (tag instanceof LiveFolderInfo) {
1757 final LiveFolderInfo info = (LiveFolderInfo) tag;
1758 final Uri uri = info.uri;
1759 final ProviderInfo providerInfo = manager.resolveContentProvider(
1760 uri.getAuthority(), 0);
Romain Guy574d20e2009-06-01 15:34:04 -07001761
Joe Onoratof11079b2010-04-15 11:47:28 -07001762 if (providerInfo != null) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001763 for (String packageName: packageNames) {
1764 if (packageName.equals(providerInfo.packageName)) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001765 LauncherModel.deleteItemFromDatabase(mLauncher, info);
Winson Chungaafa03c2010-06-11 17:34:16 -07001766 childrenToRemove.add(view);
Joe Onorato64e6be72010-03-05 15:05:52 -05001767 }
1768 }
Romain Guy629de3e2010-01-13 12:20:59 -08001769 }
1770 } else if (tag instanceof LauncherAppWidgetInfo) {
1771 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
1772 final AppWidgetProviderInfo provider =
1773 widgets.getAppWidgetInfo(info.appWidgetId);
Daniel Sandlere1cc6c32010-05-07 11:49:29 -04001774 if (provider != null) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001775 for (String packageName: packageNames) {
1776 if (packageName.equals(provider.provider.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001777 LauncherModel.deleteItemFromDatabase(mLauncher, info);
Winson Chungaafa03c2010-06-11 17:34:16 -07001778 childrenToRemove.add(view);
Joe Onorato64e6be72010-03-05 15:05:52 -05001779 }
1780 }
Romain Guy629de3e2010-01-13 12:20:59 -08001781 }
Romain Guy574d20e2009-06-01 15:34:04 -07001782 }
1783 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001784
Romain Guy629de3e2010-01-13 12:20:59 -08001785 childCount = childrenToRemove.size();
1786 for (int j = 0; j < childCount; j++) {
1787 View child = childrenToRemove.get(j);
1788 layout.removeViewInLayout(child);
1789 if (child instanceof DropTarget) {
1790 mDragController.removeDropTarget((DropTarget)child);
1791 }
Romain Guy574d20e2009-06-01 15:34:04 -07001792 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001793
Romain Guy629de3e2010-01-13 12:20:59 -08001794 if (childCount > 0) {
1795 layout.requestLayout();
1796 layout.invalidate();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
Romain Guy629de3e2010-01-13 12:20:59 -08001799 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
1801 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001802
Joe Onorato64e6be72010-03-05 15:05:52 -05001803 void updateShortcuts(ArrayList<ApplicationInfo> apps) {
Michael Jurkadee05892010-07-27 10:01:56 -07001804 final int screenCount = getChildCount();
1805 for (int i = 0; i < screenCount; i++) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001806 final CellLayout layout = (CellLayout) getChildAt(i);
1807 int childCount = layout.getChildCount();
1808 for (int j = 0; j < childCount; j++) {
1809 final View view = layout.getChildAt(j);
1810 Object tag = view.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001811 if (tag instanceof ShortcutInfo) {
1812 ShortcutInfo info = (ShortcutInfo)tag;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001813 // We need to check for ACTION_MAIN otherwise getComponent() might
1814 // return null for some shortcuts (for instance, for shortcuts to
1815 // web pages.)
1816 final Intent intent = info.intent;
1817 final ComponentName name = intent.getComponent();
1818 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Joe Onorato64e6be72010-03-05 15:05:52 -05001819 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1820 final int appCount = apps.size();
Winson Chungaafa03c2010-06-11 17:34:16 -07001821 for (int k = 0; k < appCount; k++) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001822 ApplicationInfo app = apps.get(k);
1823 if (app.componentName.equals(name)) {
1824 info.setIcon(mIconCache.getIcon(info.intent));
1825 ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
1826 new FastBitmapDrawable(info.getIcon(mIconCache)),
1827 null, null);
1828 }
1829 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001830 }
1831 }
1832 }
1833 }
1834 }
1835
Joe Onorato14f122b2009-11-19 14:06:36 -08001836 void moveToDefaultScreen(boolean animate) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001837 if (mIsSmall || mIsInUnshrinkAnimation) {
1838 mLauncher.showWorkspace(animate, (CellLayout)getChildAt(mDefaultPage));
1839 } else if (animate) {
1840 snapToPage(mDefaultPage);
Joe Onoratoc45b1682010-01-11 18:48:40 -05001841 } else {
Michael Jurka0142d492010-08-25 17:46:15 -07001842 setCurrentPage(mDefaultPage);
Joe Onoratoc45b1682010-01-11 18:48:40 -05001843 }
Michael Jurka0142d492010-08-25 17:46:15 -07001844 getChildAt(mDefaultPage).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
1846
Romain Guy8a73c512009-11-09 19:19:59 -08001847 void setIndicators(Drawable previous, Drawable next) {
1848 mPreviousIndicator = previous;
1849 mNextIndicator = next;
Michael Jurka0142d492010-08-25 17:46:15 -07001850 previous.setLevel(mCurrentPage);
1851 next.setLevel(mCurrentPage);
Romain Guy8a73c512009-11-09 19:19:59 -08001852 }
1853
Michael Jurka0142d492010-08-25 17:46:15 -07001854 @Override
1855 public void syncPages() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
Michael Jurka0142d492010-08-25 17:46:15 -07001857
1858 @Override
1859 public void syncPageItems(int page) {
1860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862}