blob: 44127fef7bbe5e7143238c59b91e6e8a387a83ee [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);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700367 boolean markCellsAsOccupied = !(child instanceof Folder);
368 if (!group.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700369 // TODO: This branch occurs when the workspace is adding views
370 // outside of the defined grid
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700371 // maybe we should be deleting these items from the LauncherModel?
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
373 }
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 if (!(child instanceof Folder)) {
Joe Onorato0d44e942009-11-16 18:20:51 -0800376 child.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 child.setOnLongClickListener(mLongClickListener);
378 }
Joe Onorato00acb122009-08-04 16:04:30 -0400379 if (child instanceof DropTarget) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700380 mDragController.addDropTarget((DropTarget) child);
Joe Onorato00acb122009-08-04 16:04:30 -0400381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
383
Michael Jurkadee05892010-07-27 10:01:56 -0700384 public boolean onTouch(View v, MotionEvent event) {
385 // this is an intercepted event being forwarded from a cell layout
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700386 if (mIsSmall || mIsInUnshrinkAnimation) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700387 mLauncher.onWorkspaceClick((CellLayout) v);
Michael Jurka96226222010-08-20 00:54:40 -0700388 return true;
Adam Cohen21f12b52010-10-07 17:15:37 -0700389 } else if (!mPageMoving) {
390 if (v == getChildAt(mCurrentPage - 1)) {
391 snapToPage(mCurrentPage - 1);
392 return true;
393 } else if (v == getChildAt(mCurrentPage + 1)) {
394 snapToPage(mCurrentPage + 1);
395 return true;
396 }
Michael Jurkadee05892010-07-27 10:01:56 -0700397 }
398 return false;
Michael Jurka0e260592010-06-30 17:07:39 -0700399 }
400
Michael Jurka5f1c5092010-09-03 14:15:02 -0700401 @Override
402 public boolean dispatchUnhandledMove(View focused, int direction) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700403 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700404 // when the home screens are shrunken, shouldn't allow side-scrolling
405 return false;
406 }
407 return super.dispatchUnhandledMove(focused, direction);
408 }
409
410 @Override
411 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700412 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700413 // when the home screens are shrunken, shouldn't allow side-scrolling
414 return false;
415 }
416 return super.onInterceptTouchEvent(ev);
417 }
418
Michael Jurka1adf5392010-10-18 18:10:22 -0700419 @Override
420 protected void determineScrollingStart(MotionEvent ev) {
421 if (!mIsSmall && !mIsInUnshrinkAnimation) super.determineScrollingStart(ev);
422 }
423
Patrick Dubroy1262e362010-10-06 15:49:50 -0700424 protected void onPageBeginMoving() {
Michael Jurka0142d492010-08-25 17:46:15 -0700425 if (mNextPage != INVALID_PAGE) {
426 // we're snapping to a particular screen
Michael Jurka16fed412010-10-01 16:12:03 -0700427 enableChildrenCache(mCurrentPage, mNextPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700428 } else {
429 // this is when user is actively dragging a particular screen, they might
430 // swipe it either left or right (but we won't advance by more than one screen)
Michael Jurka16fed412010-10-01 16:12:03 -0700431 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700433 showOutlines();
Adam Cohen21f12b52010-10-07 17:15:37 -0700434 mPageMoving = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
436
Patrick Dubroy1262e362010-10-06 15:49:50 -0700437 protected void onPageEndMoving() {
Michael Jurka16fed412010-10-01 16:12:03 -0700438 clearChildrenCache();
Patrick Dubroy1262e362010-10-06 15:49:50 -0700439 // Hide the outlines, as long as we're not dragging
440 if (!mDragController.dragging()) {
441 hideOutlines();
442 }
Adam Cohen21f12b52010-10-07 17:15:37 -0700443 mPageMoving = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700444 }
445
446 @Override
447 protected void notifyPageSwitchListener() {
448 super.notifyPageSwitchListener();
449
450 if (mPreviousIndicator != null) {
451 // if we know the next page, we show the indication for it right away; it looks
452 // weird if the indicators are lagging
453 int page = mNextPage;
454 if (page == INVALID_PAGE) {
455 page = mCurrentPage;
456 }
457 mPreviousIndicator.setLevel(page);
458 mNextIndicator.setLevel(page);
459 }
460 Launcher.setScreen(mCurrentPage);
461 };
462
Dianne Hackborn8f573952009-08-10 23:21:09 -0700463 private void updateWallpaperOffset() {
Romain Guy798300c2009-08-11 14:43:29 -0700464 updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
465 }
466
467 private void updateWallpaperOffset(int scrollRange) {
Winson Chung86f77532010-08-24 11:08:22 -0700468 final boolean isStaticWallpaper = (mWallpaperManager != null) &&
469 (mWallpaperManager.getWallpaperInfo() == null);
470 if (LauncherApplication.isScreenXLarge() && !isStaticWallpaper) {
471 IBinder token = getWindowToken();
472 if (token != null) {
473 mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 );
474 mWallpaperManager.setWallpaperOffsets(getWindowToken(),
475 Math.max(0.f, Math.min(mScrollX/(float)scrollRange, 1.f)), 0);
476 }
Joe Onorato956091b2010-02-19 12:47:40 -0800477 }
Dianne Hackborn8f573952009-08-10 23:21:09 -0700478 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700479
Adam Cohenf34bab52010-09-30 14:11:56 -0700480 public void showOutlines() {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700481 if (!mIsSmall && !mIsInUnshrinkAnimation) {
482 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel();
483 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel();
Chet Haase472b2812010-10-14 07:02:04 -0700484 mBackgroundFadeInAnimation = ObjectAnimator.ofFloat(this, "backgroundAlpha", 1.0f);
485 mBackgroundFadeInAnimation.setDuration(BACKGROUND_FADE_IN_DURATION);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700486 mBackgroundFadeInAnimation.start();
487 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700488 }
489
490 public void hideOutlines() {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700491 if (!mIsSmall && !mIsInUnshrinkAnimation) {
492 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel();
493 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel();
Chet Haase472b2812010-10-14 07:02:04 -0700494 mBackgroundFadeOutAnimation = ObjectAnimator.ofFloat(this, "backgroundAlpha", 0.0f);
495 mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700496 mBackgroundFadeOutAnimation.setStartDelay(BACKGROUND_FADE_OUT_DELAY);
497 mBackgroundFadeOutAnimation.start();
498 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700499 }
500
501 public void setBackgroundAlpha(float alpha) {
502 mBackgroundAlpha = alpha;
503 for (int i = 0; i < getChildCount(); i++) {
504 CellLayout cl = (CellLayout) getChildAt(i);
505 cl.setBackgroundAlpha(alpha);
506 }
507 }
508
509 public float getBackgroundAlpha() {
510 return mBackgroundAlpha;
511 }
512
513 @Override
514 protected void screenScrolled(int screenCenter) {
Adam Cohen21f12b52010-10-07 17:15:37 -0700515 final int halfScreenSize = getMeasuredWidth() / 2;
516 for (int i = 0; i < getChildCount(); i++) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700517 View v = getChildAt(i);
518 if (v != null) {
Adam Cohen21f12b52010-10-07 17:15:37 -0700519 int totalDistance = v.getMeasuredWidth() + mPageSpacing;
520 int delta = screenCenter - (getChildOffset(i) -
521 getRelativeChildOffset(i) + halfScreenSize);
Adam Cohenf34bab52010-09-30 14:11:56 -0700522
Adam Cohen21f12b52010-10-07 17:15:37 -0700523 float scrollProgress = delta/(totalDistance*1.0f);
524 scrollProgress = Math.min(scrollProgress, 1.0f);
525 scrollProgress = Math.max(scrollProgress, -1.0f);
Adam Cohenf34bab52010-09-30 14:11:56 -0700526
Adam Cohen21f12b52010-10-07 17:15:37 -0700527 float rotation = WORKSPACE_ROTATION * scrollProgress;
528 v.setRotationY(rotation);
Adam Cohenf34bab52010-09-30 14:11:56 -0700529 }
530 }
531 }
532
Joe Onorato00acb122009-08-04 16:04:30 -0400533 protected void onAttachedToWindow() {
534 super.onAttachedToWindow();
Joe Onorato956091b2010-02-19 12:47:40 -0800535 computeScroll();
Joe Onorato00acb122009-08-04 16:04:30 -0400536 mDragController.setWindowToken(getWindowToken());
537 }
538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700540 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurka0142d492010-08-25 17:46:15 -0700541 super.onLayout(changed, left, top, right, bottom);
Winson Chungaafa03c2010-06-11 17:34:16 -0700542
Michael Jurka79212d82010-07-30 16:36:20 -0700543 // if shrinkToBottom() is called on initialization, it has to be deferred
544 // until after the first call to onLayout so that it has the correct width
545 if (mWaitingToShrinkToBottom) {
546 shrinkToBottom(false);
547 mWaitingToShrinkToBottom = false;
548 }
549
Winson Chungaafa03c2010-06-11 17:34:16 -0700550 if (LauncherApplication.isInPlaceRotationEnabled()) {
551 // When the device is rotated, the scroll position of the current screen
552 // needs to be refreshed
Michael Jurka0142d492010-08-25 17:46:15 -0700553 setCurrentPage(getCurrentPage());
Winson Chungaafa03c2010-06-11 17:34:16 -0700554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556
557 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700558 protected void dispatchDraw(Canvas canvas) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700559 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -0700560 // Draw all the workspaces if we're small
561 final int pageCount = getChildCount();
562 final long drawingTime = getDrawingTime();
563 for (int i = 0; i < pageCount; i++) {
564 final View page = (View) getChildAt(i);
565
Michael Jurka0142d492010-08-25 17:46:15 -0700566 drawChild(canvas, page, drawingTime);
567 }
568 } else {
569 super.dispatchDraw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
572
573 @Override
574 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Joe Onorato67886212009-09-14 19:05:05 -0400575 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 final Folder openFolder = getOpenFolder();
577 if (openFolder != null) {
578 return openFolder.requestFocus(direction, previouslyFocusedRect);
579 } else {
Michael Jurka0142d492010-08-25 17:46:15 -0700580 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 }
582 }
583 return false;
584 }
585
586 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -0700587 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Joe Onorato67886212009-09-14 19:05:05 -0400588 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final Folder openFolder = getOpenFolder();
Michael Jurka0142d492010-08-25 17:46:15 -0700590 if (openFolder != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 openFolder.addFocusables(views, direction);
Michael Jurka0142d492010-08-25 17:46:15 -0700592 } else {
593 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595 }
596 }
597
598 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700599 public boolean dispatchTouchEvent(MotionEvent ev) {
600 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Michael Jurka54dd7542010-07-30 14:47:52 -0700601 // (In XLarge mode, the workspace is shrunken below all apps, and responds to taps
602 // ie when you click on a mini-screen, it zooms back to that screen)
Joe Onorato6b4adbc2010-09-01 12:13:25 -0700603 if (!LauncherApplication.isScreenXLarge() && mLauncher.isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700604 return false;
605 }
606 }
607 return super.dispatchTouchEvent(ev);
608 }
609
Michael Jurka0142d492010-08-25 17:46:15 -0700610 void enableChildrenCache(int fromPage, int toPage) {
611 if (fromPage > toPage) {
612 final int temp = fromPage;
613 fromPage = toPage;
614 toPage = temp;
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800615 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700616
Michael Jurkadee05892010-07-27 10:01:56 -0700617 final int screenCount = getChildCount();
Adam Powellfea5d022010-04-29 11:42:45 -0700618
Michael Jurka0142d492010-08-25 17:46:15 -0700619 fromPage = Math.max(fromPage, 0);
620 toPage = Math.min(toPage, screenCount - 1);
Adam Powellfea5d022010-04-29 11:42:45 -0700621
Michael Jurka0142d492010-08-25 17:46:15 -0700622 for (int i = fromPage; i <= toPage; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 final CellLayout layout = (CellLayout) getChildAt(i);
624 layout.setChildrenDrawnWithCacheEnabled(true);
625 layout.setChildrenDrawingCacheEnabled(true);
626 }
627 }
628
629 void clearChildrenCache() {
Michael Jurkadee05892010-07-27 10:01:56 -0700630 final int screenCount = getChildCount();
631 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 final CellLayout layout = (CellLayout) getChildAt(i);
633 layout.setChildrenDrawnWithCacheEnabled(false);
634 }
635 }
636
637 @Override
638 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato2bc6b7c2009-10-01 14:08:30 -0700639 if (mLauncher.isAllAppsVisible()) {
640 // Cancel any scrolling that is in progress.
641 if (!mScroller.isFinished()) {
642 mScroller.abortAnimation();
643 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700644 setCurrentPage(mCurrentPage);
Joe Onorato7c312c12009-08-13 21:36:53 -0700645 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 -0800646 }
647
Michael Jurka0142d492010-08-25 17:46:15 -0700648 return super.onTouchEvent(ev);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700650
Michael Jurka01f0ed42010-08-20 00:41:17 -0700651 public boolean isSmall() {
652 return mIsSmall;
653 }
654
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700655 void shrinkToTop(boolean animated) {
Michael Jurka0142d492010-08-25 17:46:15 -0700656 shrink(ShrinkPosition.SHRINK_TO_TOP, animated);
Michael Jurka01f0ed42010-08-20 00:41:17 -0700657 }
658
659 void shrinkToMiddle() {
Michael Jurka0142d492010-08-25 17:46:15 -0700660 shrink(ShrinkPosition.SHRINK_TO_MIDDLE, true);
Michael Jurka213d9632010-07-28 11:29:25 -0700661 }
662
663 void shrinkToBottom() {
Michael Jurka79212d82010-07-30 16:36:20 -0700664 shrinkToBottom(true);
665 }
666
667 void shrinkToBottom(boolean animated) {
668 if (mFirstLayout) {
669 // (mFirstLayout == "first layout has not happened yet")
670 // if we get a call to shrink() as part of our initialization (for example, if
671 // Launcher is started in All Apps mode) then we need to wait for a layout call
672 // to get our width so we can layout the mini-screen views correctly
673 mWaitingToShrinkToBottom = true;
674 } else {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700675 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN, animated);
Michael Jurka79212d82010-07-30 16:36:20 -0700676 }
Michael Jurka213d9632010-07-28 11:29:25 -0700677 }
678
Adam Cohena985e592010-09-09 11:23:48 -0700679 private float getYScaleForScreen(int screen) {
680 int x = Math.abs(screen - 2);
681
682 // TODO: This should be generalized for use with arbitrary rotation angles.
683 switch(x) {
684 case 0: return EXTRA_SCALE_FACTOR_0;
685 case 1: return EXTRA_SCALE_FACTOR_1;
686 case 2: return EXTRA_SCALE_FACTOR_2;
687 }
688 return 1.0f;
689 }
690
Michael Jurkadee05892010-07-27 10:01:56 -0700691 // we use this to shrink the workspace for the all apps view and the customize view
Michael Jurka0142d492010-08-25 17:46:15 -0700692 private void shrink(ShrinkPosition shrinkPosition, boolean animated) {
Michael Jurkadee05892010-07-27 10:01:56 -0700693 mIsSmall = true;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700694 mShrunkenState = shrinkPosition;
695
696 // Stop any scrolling, move to the current page right away
697 setCurrentPage(mCurrentPage);
698 updateWhichPagesAcceptDrops(mShrunkenState);
699
Michael Jurka5f1c5092010-09-03 14:15:02 -0700700 // we intercept and reject all touch events when we're small, so be sure to reset the state
701 mTouchState = TOUCH_STATE_REST;
702 mActivePointerId = INVALID_POINTER;
703
Patrick Dubroy7247f632010-08-04 16:02:59 -0700704 final Resources res = getResources();
Michael Jurkadee05892010-07-27 10:01:56 -0700705 final int screenWidth = getWidth();
Michael Jurka213d9632010-07-28 11:29:25 -0700706 final int screenHeight = getHeight();
Michael Jurka0142d492010-08-25 17:46:15 -0700707
708 // Making the assumption that all pages have the same width as the 0th
709 final int pageWidth = getChildAt(0).getMeasuredWidth();
710 final int pageHeight = getChildAt(0).getMeasuredHeight();
711
712 final int scaledPageWidth = (int) (SHRINK_FACTOR * pageWidth);
713 final int scaledPageHeight = (int) (SHRINK_FACTOR * pageHeight);
Adam Cohena985e592010-09-09 11:23:48 -0700714 final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing);
Michael Jurkadee05892010-07-27 10:01:56 -0700715
716 final int screenCount = getChildCount();
Adam Cohena985e592010-09-09 11:23:48 -0700717 float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing;
Michael Jurka213d9632010-07-28 11:29:25 -0700718
Winson Chungdf4b83d2010-10-20 17:49:27 -0700719 boolean isPortrait = getMeasuredHeight() > getMeasuredWidth();
720 float newY = (isPortrait ?
721 getResources().getDimension(R.dimen.smallScreenVerticalMarginPortrait) :
722 getResources().getDimension(R.dimen.smallScreenVerticalMarginLandscape));
Michael Jurka3e7c7632010-10-02 16:01:03 -0700723 float finalAlpha = 1.0f;
724 float extraShrinkFactor = 1.0f;
725 if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE) {
726 newY = screenHeight - newY - scaledPageHeight;
727 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN) {
728
729 // We shrink and disappear to nothing in the case of all apps
730 // (which is when we shrink to the bottom)
Michael Jurka0142d492010-08-25 17:46:15 -0700731 newY = screenHeight - newY - scaledPageHeight;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700732 finalAlpha = 0.0f;
733 extraShrinkFactor = 0.1f;
Michael Jurka0142d492010-08-25 17:46:15 -0700734 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_MIDDLE) {
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700735 newY = screenHeight / 2 - scaledPageHeight / 2;
736 finalAlpha = 1.0f;
737 } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_TOP) {
738 newY = screenHeight / 10;
Michael Jurka213d9632010-07-28 11:29:25 -0700739 }
Michael Jurkadee05892010-07-27 10:01:56 -0700740
741 // We animate all the screens to the centered position in workspace
742 // At the same time, the screens become greyed/dimmed
743
744 // newX is initialized to the left-most position of the centered screens
Michael Jurka0142d492010-08-25 17:46:15 -0700745 float newX = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2;
Adam Cohena985e592010-09-09 11:23:48 -0700746
747 // We are going to scale about the center of the view, so we need to adjust the positions
748 // of the views accordingly
749 newX -= (pageWidth - scaledPageWidth) / 2.0f;
750 newY -= (pageHeight - scaledPageHeight) / 2.0f;
Michael Jurkadee05892010-07-27 10:01:56 -0700751 for (int i = 0; i < screenCount; i++) {
752 CellLayout cl = (CellLayout) getChildAt(i);
Adam Cohena985e592010-09-09 11:23:48 -0700753
Adam Cohenf34bab52010-09-30 14:11:56 -0700754 float rotation = (-i + 2) * WORKSPACE_ROTATION;
Adam Cohena985e592010-09-09 11:23:48 -0700755 float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f));
756 float rotationScaleY = getYScaleForScreen(i);
757
Michael Jurka79212d82010-07-30 16:36:20 -0700758 if (animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -0700759 final int duration = res.getInteger(R.integer.config_workspaceShrinkTime);
Chet Haase472b2812010-10-14 07:02:04 -0700760 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(cl,
761 PropertyValuesHolder.ofFloat("x", newX),
762 PropertyValuesHolder.ofFloat("y", newY),
763 PropertyValuesHolder.ofFloat("scaleX",
Michael Jurka3e7c7632010-10-02 16:01:03 -0700764 SHRINK_FACTOR * rotationScaleX * extraShrinkFactor),
Chet Haase472b2812010-10-14 07:02:04 -0700765 PropertyValuesHolder.ofFloat("scaleY",
Michael Jurka3e7c7632010-10-02 16:01:03 -0700766 SHRINK_FACTOR * rotationScaleY * extraShrinkFactor),
Chet Haase472b2812010-10-14 07:02:04 -0700767 PropertyValuesHolder.ofFloat("backgroundAlpha", finalAlpha),
768 PropertyValuesHolder.ofFloat("alpha", finalAlpha),
769 PropertyValuesHolder.ofFloat("rotationY", rotation));
770 anim.setDuration(duration);
771 anim.start();
Michael Jurka79212d82010-07-30 16:36:20 -0700772 } else {
773 cl.setX((int)newX);
774 cl.setY((int)newY);
Adam Cohenf34bab52010-09-30 14:11:56 -0700775 cl.setScaleX(SHRINK_FACTOR * rotationScaleX);
776 cl.setScaleY(SHRINK_FACTOR * rotationScaleY);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700777 cl.setBackgroundAlpha(1.0f);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700778 cl.setAlpha(finalAlpha);
Adam Cohena985e592010-09-09 11:23:48 -0700779 cl.setRotationY(rotation);
Michael Jurka79212d82010-07-30 16:36:20 -0700780 }
Michael Jurkadee05892010-07-27 10:01:56 -0700781 // increment newX for the next screen
Adam Cohena985e592010-09-09 11:23:48 -0700782 newX += scaledPageWidth + extraScaledSpacing;
Michael Jurkadee05892010-07-27 10:01:56 -0700783 }
784 setChildrenDrawnWithCacheEnabled(true);
Michael Jurkadee05892010-07-27 10:01:56 -0700785 }
786
Michael Jurka3e7c7632010-10-02 16:01:03 -0700787
788 private void updateWhichPagesAcceptDrops(ShrinkPosition state) {
789 updateWhichPagesAcceptDropsHelper(state, false, 1, 1);
790 }
791
792
793 private void updateWhichPagesAcceptDropsDuringDrag(ShrinkPosition state, int spanX, int spanY) {
794 updateWhichPagesAcceptDropsHelper(state, true, spanX, spanY);
795 }
796
797 private void updateWhichPagesAcceptDropsHelper(
798 ShrinkPosition state, boolean isDragHappening, int spanX, int spanY) {
799 final int screenCount = getChildCount();
800 for (int i = 0; i < screenCount; i++) {
801 CellLayout cl = (CellLayout) getChildAt(i);
802
803 switch (state) {
804 case SHRINK_TO_TOP:
805 if (!isDragHappening) {
806 boolean showDropHighlight = i == mCurrentPage;
807 cl.setAcceptsDrops(showDropHighlight);
808 break;
809 }
810 // otherwise, fall through below and mark non-full screens as accepting drops
811 case SHRINK_TO_BOTTOM_HIDDEN:
812 case SHRINK_TO_BOTTOM_VISIBLE:
813 if (!isDragHappening) {
814 // even if a drag isn't happening, we don't want to show a screen as
815 // accepting drops if it doesn't have at least one free cell
816 spanX = 1;
817 spanY = 1;
818 }
819 // the page accepts drops if we can find at least one empty spot
820 cl.setAcceptsDrops(cl.findCellForSpan(null, spanX, spanY));
821 break;
822 default:
823 throw new RuntimeException(
824 "updateWhichPagesAcceptDropsHelper passed an unhandled ShrinkPosition");
825 }
826 }
827 }
828
829 /*
830 *
831 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithItemMinSize) whenever we
832 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
833 *
834 * These methods mark the appropriate pages as accepting drops (which alters their visual
835 * appearance) and, if the pages are hidden, makes them visible.
836 *
837 */
838 public void onDragStartedWithItemSpans(int spanX, int spanY) {
839 updateWhichPagesAcceptDropsDuringDrag(mShrunkenState, spanX, spanY);
840 if (mShrunkenState == ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN) {
841 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE, true);
842 }
843 }
844
845 public void onDragStartedWithItemMinSize(int minWidth, int minHeight) {
846 int[] spanXY = CellLayout.rectToCell(getResources(), minWidth, minHeight, null);
847 onDragStartedWithItemSpans(spanXY[0], spanXY[1]);
848 }
849
850 // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was
851 // never dragged over
852 public void onDragStopped() {
853 updateWhichPagesAcceptDrops(mShrunkenState);
854 if (mShrunkenState == ShrinkPosition.SHRINK_TO_BOTTOM_VISIBLE) {
855 shrink(ShrinkPosition.SHRINK_TO_BOTTOM_HIDDEN, true);
856 }
857 }
858
Michael Jurkadee05892010-07-27 10:01:56 -0700859 // We call this when we trigger an unshrink by clicking on the CellLayout cl
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700860 public void unshrink(CellLayout clThatWasClicked) {
Michael Jurka0142d492010-08-25 17:46:15 -0700861 int newCurrentPage = mCurrentPage;
Michael Jurka4cb37242010-08-09 21:05:32 -0700862 final int screenCount = getChildCount();
863 for (int i = 0; i < screenCount; i++) {
864 if (getChildAt(i) == clThatWasClicked) {
Michael Jurka0142d492010-08-25 17:46:15 -0700865 newCurrentPage = i;
Michael Jurkadee05892010-07-27 10:01:56 -0700866 }
Michael Jurka4cb37242010-08-09 21:05:32 -0700867 }
Michael Jurka0142d492010-08-25 17:46:15 -0700868 unshrink(newCurrentPage);
Michael Jurka4cb37242010-08-09 21:05:32 -0700869 }
870
Adam Cohen21f12b52010-10-07 17:15:37 -0700871 @Override
872 protected boolean handlePagingClicks() {
873 return true;
874 }
875
Michael Jurka0142d492010-08-25 17:46:15 -0700876 private void unshrink(int newCurrentPage) {
Michael Jurka4cb37242010-08-09 21:05:32 -0700877 if (mIsSmall) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700878 int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
879 int delta = newX - mScrollX;
Michael Jurka4cb37242010-08-09 21:05:32 -0700880
881 final int screenCount = getChildCount();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700882 for (int i = 0; i < screenCount; i++) {
883 CellLayout cl = (CellLayout) getChildAt(i);
884 cl.setX(cl.getX() + delta);
885 }
Michael Jurka0142d492010-08-25 17:46:15 -0700886 setCurrentPage(newCurrentPage);
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700887 unshrink();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700888 }
Michael Jurkadee05892010-07-27 10:01:56 -0700889 }
890
Michael Jurka4cb37242010-08-09 21:05:32 -0700891 void unshrink() {
892 unshrink(true);
893 }
894
895 void unshrink(boolean animated) {
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700896 if (mIsSmall) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700897 mIsSmall = false;
Chet Haaseb1254a62010-09-07 13:35:00 -0700898 AnimatorSet s = new AnimatorSet();
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700899 final int screenCount = getChildCount();
Michael Jurka4cb37242010-08-09 21:05:32 -0700900
901 final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700902 for (int i = 0; i < screenCount; i++) {
Patrick Dubroy7247f632010-08-04 16:02:59 -0700903 final CellLayout cl = (CellLayout)getChildAt(i);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700904 float finalAlphaValue = (i == mCurrentPage) ? 1.0f : 0.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -0700905 float rotation = 0.0f;
906
907 if (i < mCurrentPage) {
908 rotation = WORKSPACE_ROTATION;
909 } else if (i > mCurrentPage) {
910 rotation = -WORKSPACE_ROTATION;
911 }
912
Michael Jurka4cb37242010-08-09 21:05:32 -0700913 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -0700914
Michael Jurka4cb37242010-08-09 21:05:32 -0700915 s.playTogether(
Chet Haase472b2812010-10-14 07:02:04 -0700916 ObjectAnimator.ofFloat(cl, "translationX", 0.0f).setDuration(duration),
917 ObjectAnimator.ofFloat(cl, "translationY", 0.0f).setDuration(duration),
918 ObjectAnimator.ofFloat(cl, "scaleX", 1.0f).setDuration(duration),
919 ObjectAnimator.ofFloat(cl, "scaleY", 1.0f).setDuration(duration),
920 ObjectAnimator.ofFloat(cl, "backgroundAlpha", 0.0f).setDuration(duration),
921 ObjectAnimator.ofFloat(cl, "alpha", finalAlphaValue).setDuration(duration),
922 ObjectAnimator.ofFloat(cl, "rotationY", rotation).setDuration(duration));
Michael Jurka4cb37242010-08-09 21:05:32 -0700923 } else {
924 cl.setTranslationX(0.0f);
925 cl.setTranslationY(0.0f);
926 cl.setScaleX(1.0f);
927 cl.setScaleY(1.0f);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700928 cl.setBackgroundAlpha(0.0f);
Adam Cohenf34bab52010-09-30 14:11:56 -0700929 cl.setAlpha(finalAlphaValue);
930 cl.setRotationY(rotation);
Michael Jurka4cb37242010-08-09 21:05:32 -0700931 }
Michael Jurkab0f28bd2010-07-30 11:58:59 -0700932 }
Michael Jurkac86756c2010-10-10 15:15:47 -0700933 if (animated) {
934 // If we call this when we're not animated, onAnimationEnd is never called on
935 // the listener; make sure we only use the listener when we're actually animating
936 s.addListener(mUnshrinkAnimationListener);
937 s.start();
938 }
Michael Jurkadee05892010-07-27 10:01:56 -0700939 }
Michael Jurkadee05892010-07-27 10:01:56 -0700940 }
941
Joe Onorato4be866d2010-10-10 11:26:02 -0700942 /**
943 * Draw the View v into the given Canvas.
944 *
945 * @param v the view to draw
946 * @param destCanvas the canvas to draw on
947 * @param padding the horizontal and vertical padding to use when drawing
948 */
949 private void drawDragView(View v, Canvas destCanvas, int padding) {
950 final Rect clipRect = mTempRect;
951 v.getDrawingRect(clipRect);
952
953 // For a TextView, adjust the clip rect so that we don't include the text label
954 if (v instanceof TextView) {
955 final int iconHeight = ((TextView)v).getCompoundPaddingTop() - v.getPaddingTop();
956 clipRect.bottom = clipRect.top + iconHeight;
957 }
958
959 // Draw the View into the bitmap.
960 // The translate of scrollX and scrollY is necessary when drawing TextViews, because
961 // they set scrollX and scrollY to large values to achieve centered text
962
963 destCanvas.save();
964 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
965 destCanvas.clipRect(clipRect, Op.REPLACE);
966 v.draw(destCanvas);
967 destCanvas.restore();
968 }
969
970 /**
971 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
972 * Responsibility for the bitmap is transferred to the caller.
973 */
974 private Bitmap createDragOutline(View v, Canvas canvas, int padding) {
975 final int outlineColor = getResources().getColor(R.color.drag_outline_color);
976 final Bitmap b = Bitmap.createBitmap(
977 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
978
979 canvas.setBitmap(b);
980 drawDragView(v, canvas, padding);
981 mOutlineHelper.applyExpensiveOuterOutline(b, canvas, outlineColor, true);
982
983 return b;
984 }
985
986 /**
987 * Returns a new bitmap to show when the given View is being dragged around.
988 * Responsibility for the bitmap is transferred to the caller.
989 */
990 private Bitmap createDragBitmap(View v, Canvas canvas, int padding) {
991 final int outlineColor = getResources().getColor(R.color.drag_outline_color);
992 final Bitmap b = Bitmap.createBitmap(
993 mDragOutline.getWidth(), mDragOutline.getHeight(), Bitmap.Config.ARGB_8888);
994
995 canvas.setBitmap(b);
996 canvas.drawBitmap(mDragOutline, 0, 0, null);
997 drawDragView(v, canvas, padding);
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700998 mOutlineHelper.applyOuterBlur(b, canvas, outlineColor);
Joe Onorato4be866d2010-10-10 11:26:02 -0700999
1000 return b;
1001 }
1002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 void startDrag(CellLayout.CellInfo cellInfo) {
1004 View child = cellInfo.cell;
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 // Make sure the drag was started by a long press as opposed to a long click.
Bjorn Bringert7984c942009-12-09 15:38:25 +00001007 if (!child.isInTouchMode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 return;
1009 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 mDragInfo = cellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001012 mDragInfo.screen = mCurrentPage;
Winson Chungaafa03c2010-06-11 17:34:16 -07001013
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001014 CellLayout current = getCurrentDropLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015
1016 current.onDragChild(child);
Joe Onorato4be866d2010-10-10 11:26:02 -07001017
1018 child.clearFocus();
1019 child.setPressed(false);
1020
1021 final Canvas canvas = new Canvas();
1022
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001023 // We need to add extra padding to the bitmap to make room for the glow effect
1024 final int bitmapPadding = HolographicOutlineHelper.OUTER_BLUR_RADIUS;
1025
Joe Onorato4be866d2010-10-10 11:26:02 -07001026 // The outline is used to visualize where the item will land if dropped
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001027 mDragOutline = createDragOutline(child, canvas, bitmapPadding);
Joe Onorato4be866d2010-10-10 11:26:02 -07001028
1029 // The drag bitmap follows the touch point around on the screen
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001030 final Bitmap b = createDragBitmap(child, canvas, bitmapPadding);
Joe Onorato4be866d2010-10-10 11:26:02 -07001031
1032 final int bmpWidth = b.getWidth();
1033 final int bmpHeight = b.getHeight();
1034 child.getLocationOnScreen(mTempXY);
1035 final int screenX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2;
1036 final int screenY = (int) mTempXY[1] + (child.getHeight() - bmpHeight) / 2;
1037 mDragController.startDrag(b, screenX, screenY, 0, 0, bmpWidth, bmpHeight, this,
1038 child.getTag(), DragController.DRAG_ACTION_MOVE, null);
1039 b.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 void addApplicationShortcut(ShortcutInfo info, int screen, int cellX, int cellY,
1043 boolean insertAtFirst, int intersectX, int intersectY) {
1044 final CellLayout cellLayout = (CellLayout) getChildAt(screen);
1045 View view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info);
1046
1047 final int[] cellXY = new int[2];
1048 cellLayout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
1049 addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
1050 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
1051 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
1052 cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054
Patrick Dubroyce34a972010-10-19 10:34:32 -07001055 private void setPositionForDropAnimation(
1056 View dragView, int dragViewX, int dragViewY, View parent, View child) {
1057 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1058
1059 // Based on the position of the drag view, find the top left of the original view
1060 int viewX = dragViewX + (dragView.getWidth() - child.getWidth()) / 2;
1061 int viewY = dragViewY + (dragView.getHeight() - child.getHeight()) / 2;
1062 viewX -= getResources().getInteger(R.integer.config_dragViewOffsetX);
1063 viewY -= getResources().getInteger(R.integer.config_dragViewOffsetY);
1064
1065 // Set its old pos (in the new parent's coordinates); the CellLayout will
1066 // animate it from this position during the next layout pass
1067 lp.oldX = viewX - (parent.getLeft() - mScrollX);
1068 lp.oldY = viewY - (parent.getTop() - mScrollY);
1069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070
Joe Onorato00acb122009-08-04 16:04:30 -04001071 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
1072 DragView dragView, Object dragInfo) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07001073
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074 int originX = x - xOffset;
1075 int originY = y - yOffset;
Patrick Dubroyce34a972010-10-19 10:34:32 -07001076
1077 if (mDragTargetLayout == null) {
1078 // Cancel the drag if we're not over a screen at time of drop
1079 if (mDragInfo != null) {
1080 // Set its position so the parent can animate it back
1081 final View parent = getChildAt(mDragInfo.screen);
1082 setPositionForDropAnimation(dragView, originX, originY, parent, mDragInfo.cell);
1083 }
1084 return;
1085 }
1086
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001087 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 // get originX and originY in the local coordinate system of the screen
1089 mTempOriginXY[0] = originX;
1090 mTempOriginXY[1] = originY;
Michael Jurkad718d6a2010-10-14 15:35:17 -07001091 mapPointFromSelfToChild(mDragTargetLayout, mTempOriginXY);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092 originX = (int)mTempOriginXY[0];
1093 originY = (int)mTempOriginXY[1];
Michael Jurkaa63c4522010-08-19 13:52:27 -07001094 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 if (source != this) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001097 onDropExternal(originX, originY, dragInfo, mDragTargetLayout);
Patrick Dubroyce34a972010-10-19 10:34:32 -07001098 } else if (mDragInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 // Move internally
Patrick Dubroyce34a972010-10-19 10:34:32 -07001100 final View cell = mDragInfo.cell;
1101 mTargetCell = findNearestVacantArea(originX, originY,
1102 mDragInfo.spanX, mDragInfo.spanY, cell, mDragTargetLayout,
1103 mTargetCell);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001104
Patrick Dubroyce34a972010-10-19 10:34:32 -07001105 int screen = indexOfChild(mDragTargetLayout);
1106 if (screen != mDragInfo.screen) {
1107 final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1108 originalCellLayout.removeView(cell);
1109 addInScreen(cell, screen, mTargetCell[0], mTargetCell[1],
1110 mDragInfo.spanX, mDragInfo.spanY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07001112 mDragTargetLayout.onDropChild(cell);
1113
1114 // update the item's position after drop
1115 final ItemInfo info = (ItemInfo) cell.getTag();
1116 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
1117 mDragTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]);
1118 lp.cellX = mTargetCell[0];
1119 lp.cellY = mTargetCell[1];
1120 cell.setId(LauncherModel.getCellLayoutChildId(-1, mDragInfo.screen,
1121 mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY));
1122
1123 LauncherModel.moveItemInDatabase(mLauncher, info,
1124 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
1125 lp.cellX, lp.cellY);
1126
1127 // Prepare it to be animated into its new position
1128 // This must be called after the view has been re-parented
1129 setPositionForDropAnimation(dragView, originX, originY, mDragTargetLayout, cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131 }
1132
Winson Chungaafa03c2010-06-11 17:34:16 -07001133 public void onDragEnter(DragSource source, int x, int y, int xOffset,
1134 int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001135 mDragTargetLayout = null; // Reset the drag state
1136
Michael Jurkad718d6a2010-10-14 15:35:17 -07001137 if (!mIsSmall) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001138 mDragTargetLayout = getCurrentDropLayout();
1139 mDragTargetLayout.onDragEnter(dragView);
Michael Jurkad718d6a2010-10-14 15:35:17 -07001140 showOutlines();
1141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143
Michael Jurka18014792010-10-14 09:01:34 -07001144 public DropTarget getDropTargetDelegate(DragSource source, int x, int y,
1145 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy440c3602010-07-13 17:50:32 -07001146
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001147 if (mIsSmall || mIsInUnshrinkAnimation) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001148 // 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 -07001149 return null;
1150 }
Patrick Dubroy440c3602010-07-13 17:50:32 -07001151 // We may need to delegate the drag to a child view. If a 1x1 item
1152 // would land in a cell occupied by a DragTarget (e.g. a Folder),
1153 // then drag events should be handled by that child.
1154
1155 ItemInfo item = (ItemInfo)dragInfo;
1156 CellLayout currentLayout = getCurrentDropLayout();
1157
1158 int dragPointX, dragPointY;
1159 if (item.spanX == 1 && item.spanY == 1) {
1160 // For a 1x1, calculate the drop cell exactly as in onDragOver
1161 dragPointX = x - xOffset;
1162 dragPointY = y - yOffset;
1163 } else {
1164 // Otherwise, use the exact drag coordinates
1165 dragPointX = x;
1166 dragPointY = y;
1167 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 dragPointX += mScrollX - currentLayout.getLeft();
1169 dragPointY += mScrollY - currentLayout.getTop();
Patrick Dubroy440c3602010-07-13 17:50:32 -07001170
1171 // If we are dragging over a cell that contains a DropTarget that will
1172 // accept the drop, delegate to that DropTarget.
1173 final int[] cellXY = mTempCell;
1174 currentLayout.estimateDropCell(dragPointX, dragPointY, item.spanX, item.spanY, cellXY);
1175 View child = currentLayout.getChildAt(cellXY[0], cellXY[1]);
1176 if (child instanceof DropTarget) {
1177 DropTarget target = (DropTarget)child;
1178 if (target.acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
1179 return target;
1180 }
1181 }
1182 return null;
1183 }
1184
Michael Jurka4516c112010-10-07 15:13:47 -07001185 /*
1186 *
1187 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
1188 * coordinate space. The argument xy is modified with the return result.
1189 *
1190 */
1191 void mapPointFromSelfToChild(View v, float[] xy) {
1192 mapPointFromSelfToChild(v, xy, null);
1193 }
1194
1195 /*
1196 *
1197 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
1198 * coordinate space. The argument xy is modified with the return result.
1199 *
1200 * if cachedInverseMatrix is not null, this method will just use that matrix instead of
Michael Jurkad718d6a2010-10-14 15:35:17 -07001201 * computing it itself; we use this to avoid redundant matrix inversions in
Michael Jurka4516c112010-10-07 15:13:47 -07001202 * findMatchingPageForDragOver
1203 *
1204 */
1205 void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
1206 if (cachedInverseMatrix == null) {
1207 v.getMatrix().invert(mTempInverseMatrix);
1208 cachedInverseMatrix = mTempInverseMatrix;
1209 }
1210 xy[0] = xy[0] + mScrollX - v.getLeft();
1211 xy[1] = xy[1] + mScrollY - v.getTop();
1212 cachedInverseMatrix.mapPoints(xy);
1213 }
1214
1215 /*
1216 *
1217 * Convert the 2D coordinate xy from this CellLayout's coordinate space to
1218 * the parent View's coordinate space. The argument xy is modified with the return result.
1219 *
1220 */
1221 void mapPointFromChildToSelf(View v, float[] xy) {
1222 v.getMatrix().mapPoints(xy);
1223 xy[0] -= (mScrollX - v.getLeft());
1224 xy[1] -= (mScrollY - v.getTop());
1225 }
1226
1227 static private float squaredDistance(float[] point1, float[] point2) {
1228 float distanceX = point1[0] - point2[0];
1229 float distanceY = point2[1] - point2[1];
1230 return distanceX * distanceX + distanceY * distanceY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 }
1232
Michael Jurka4516c112010-10-07 15:13:47 -07001233 /*
1234 *
1235 * Returns true if the passed CellLayout cl overlaps with dragView
1236 *
1237 */
1238 boolean overlaps(CellLayout cl, DragView dragView,
1239 int dragViewX, int dragViewY, Matrix cachedInverseMatrix) {
1240 // Transform the coordinates of the item being dragged to the CellLayout's coordinates
1241 final float[] draggedItemTopLeft = mTempDragCoordinates;
1242 draggedItemTopLeft[0] = dragViewX + dragView.getScaledDragRegionXOffset();
1243 draggedItemTopLeft[1] = dragViewY + dragView.getScaledDragRegionYOffset();
1244 final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates;
1245 draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getScaledDragRegionWidth();
1246 draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getScaledDragRegionHeight();
Michael Jurkaa63c4522010-08-19 13:52:27 -07001247
Michael Jurka4516c112010-10-07 15:13:47 -07001248 // Transform the dragged item's top left coordinates
1249 // to the CellLayout's local coordinates
1250 mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix);
1251 float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]);
1252 float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]);
1253
1254 if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) {
1255 // Transform the dragged item's bottom right coordinates
1256 // to the CellLayout's local coordinates
1257 mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix);
1258 float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]);
1259 float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]);
1260
1261 if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) {
1262 float overlap = (overlapRegionRight - overlapRegionLeft) *
1263 (overlapRegionBottom - overlapRegionTop);
1264 if (overlap > 0) {
1265 return true;
1266 }
1267 }
1268 }
1269 return false;
1270 }
1271
1272 /*
1273 *
1274 * This method returns the CellLayout that is currently being dragged to. In order to drag
1275 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
1276 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
1277 *
1278 * Return null if no CellLayout is currently being dragged over
1279 *
1280 */
1281 private CellLayout findMatchingPageForDragOver(
1282 DragView dragView, int originX, int originY, int offsetX, int offsetY) {
1283 // We loop through all the screens (ie CellLayouts) and see which ones overlap
1284 // with the item being dragged and then choose the one that's closest to the touch point
Michael Jurkaa63c4522010-08-19 13:52:27 -07001285 final int screenCount = getChildCount();
1286 CellLayout bestMatchingScreen = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001287 float smallestDistSoFar = Float.MAX_VALUE;
Michael Jurka4516c112010-10-07 15:13:47 -07001288
Michael Jurkaa63c4522010-08-19 13:52:27 -07001289 for (int i = 0; i < screenCount; i++) {
1290 CellLayout cl = (CellLayout)getChildAt(i);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001291
Michael Jurka4516c112010-10-07 15:13:47 -07001292 final float[] touchXy = mTempTouchCoordinates;
1293 touchXy[0] = originX + offsetX;
1294 touchXy[1] = originY + offsetY;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001295
Michael Jurka4516c112010-10-07 15:13:47 -07001296 // Transform the touch coordinates to the CellLayout's local coordinates
1297 // If the touch point is within the bounds of the cell layout, we can return immediately
Michael Jurka0280c3b2010-09-17 15:00:07 -07001298 cl.getMatrix().invert(mTempInverseMatrix);
Michael Jurka4516c112010-10-07 15:13:47 -07001299 mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001300
Michael Jurka4516c112010-10-07 15:13:47 -07001301 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
1302 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
1303 return cl;
1304 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001305
Michael Jurka4516c112010-10-07 15:13:47 -07001306 if (overlaps(cl, dragView, originX, originY, mTempInverseMatrix)) {
1307 // Get the center of the cell layout in screen coordinates
1308 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
1309 cellLayoutCenter[0] = cl.getWidth()/2;
1310 cellLayoutCenter[1] = cl.getHeight()/2;
1311 mapPointFromChildToSelf(cl, cellLayoutCenter);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312
Michael Jurka4516c112010-10-07 15:13:47 -07001313 touchXy[0] = originX + offsetX;
1314 touchXy[1] = originY + offsetY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315
Michael Jurka4516c112010-10-07 15:13:47 -07001316 // Calculate the distance between the center of the CellLayout
1317 // and the touch point
1318 float dist = squaredDistance(touchXy, cellLayoutCenter);
1319
1320 if (dist < smallestDistSoFar) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 smallestDistSoFar = dist;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001322 bestMatchingScreen = cl;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001323 }
Michael Jurka4516c112010-10-07 15:13:47 -07001324 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001325 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001326 return bestMatchingScreen;
1327 }
1328
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001329 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
1330 DragView dragView, Object dragInfo) {
Patrick Dubroy1262e362010-10-06 15:49:50 -07001331 // When touch is inside the scroll area, skip dragOver actions for the current screen
1332 if (!mInScrollArea) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001333 CellLayout layout;
1334 int originX = x - xOffset;
1335 int originY = y - yOffset;
1336 if (mIsSmall || mIsInUnshrinkAnimation) {
1337 layout = findMatchingPageForDragOver(
1338 dragView, originX, originY, xOffset, yOffset);
Patrick Dubroy1262e362010-10-06 15:49:50 -07001339
Michael Jurkad718d6a2010-10-14 15:35:17 -07001340 if (layout != mDragTargetLayout) {
1341 if (mDragTargetLayout != null) {
1342 mDragTargetLayout.setHover(false);
1343 }
1344 mDragTargetLayout = layout;
1345 if (mDragTargetLayout != null) {
1346 mDragTargetLayout.setHover(true);
1347 }
1348 }
1349 } else {
1350 layout = getCurrentDropLayout();
1351
1352 final ItemInfo item = (ItemInfo)dragInfo;
1353 if (dragInfo instanceof LauncherAppWidgetInfo) {
1354 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo)dragInfo;
1355
1356 if (widgetInfo.spanX == -1) {
1357 // Calculate the grid spans needed to fit this widget
1358 int[] spans = layout.rectToCell(
1359 widgetInfo.minWidth, widgetInfo.minHeight, null);
1360 item.spanX = spans[0];
1361 item.spanY = spans[1];
1362 }
1363 }
1364
1365 if (source instanceof AllAppsPagedView) {
1366 // This is a hack to fix the point used to determine which cell an icon from
1367 // the all apps screen is over
1368 if (item != null && item.spanX == 1 && layout != null) {
1369 int dragRegionLeft = (dragView.getWidth() - layout.getCellWidth()) / 2;
1370
1371 originX += dragRegionLeft - dragView.getDragRegionLeft();
1372 if (dragView.getDragRegionWidth() != layout.getCellWidth()) {
1373 dragView.setDragRegion(dragView.getDragRegionLeft(),
1374 dragView.getDragRegionTop(),
1375 layout.getCellWidth(),
1376 dragView.getDragRegionHeight());
1377 }
1378 }
1379 }
1380
1381 if (layout != mDragTargetLayout) {
1382 if (mDragTargetLayout != null) {
1383 mDragTargetLayout.onDragExit();
1384 }
1385 layout.onDragEnter(dragView);
1386 mDragTargetLayout = layout;
1387 }
1388
1389 // only visualize the drop locations for moving icons within the home screen on
1390 // tablet on phone, we also visualize icons dragged in from All Apps
1391 if ((!LauncherApplication.isScreenXLarge() || source == this)
1392 && mDragTargetLayout != null) {
1393 final View child = (mDragInfo == null) ? null : mDragInfo.cell;
1394 int localOriginX = originX - (mDragTargetLayout.getLeft() - mScrollX);
1395 int localOriginY = originY - (mDragTargetLayout.getTop() - mScrollY);
1396 mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
1397 localOriginX, localOriginY, item.spanX, item.spanY);
1398 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001399 }
Patrick Dubroy976ebec2010-08-04 20:03:37 -07001400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
Winson Chungaafa03c2010-06-11 17:34:16 -07001403 public void onDragExit(DragSource source, int x, int y, int xOffset,
1404 int yOffset, DragView dragView, Object dragInfo) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001405 if (mDragTargetLayout != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001406 mDragTargetLayout.onDragExit();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001407 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001408 if (!mIsPageMoving) {
1409 hideOutlines();
1410 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001411 clearAllHovers();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413
Winson Chungaafa03c2010-06-11 17:34:16 -07001414 private void onDropExternal(int x, int y, Object dragInfo,
1415 CellLayout cellLayout) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 onDropExternal(x, y, dragInfo, cellLayout, false);
1417 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001418
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001419 /**
1420 * Add the item specified by dragInfo to the given layout.
1421 * This is basically the equivalent of onDropExternal, except it's not initiated
1422 * by drag and drop.
1423 * @return true if successful
1424 */
1425 public boolean addExternalItemToScreen(Object dragInfo, View layout) {
1426 CellLayout cl = (CellLayout) layout;
1427 ItemInfo info = (ItemInfo) dragInfo;
1428
Michael Jurka0280c3b2010-09-17 15:00:07 -07001429 if (cl.findCellForSpan(mTempEstimate, info.spanX, info.spanY)) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001430 onDropExternal(0, 0, dragInfo, cl, false);
1431 return true;
1432 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001433 mLauncher.showOutOfSpaceMessage();
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07001434 return false;
1435 }
1436
Michael Jurka0280c3b2010-09-17 15:00:07 -07001437 // Drag from somewhere else
Joe Onorato4be866d2010-10-10 11:26:02 -07001438 // NOTE: This can also be called when we are outside of a drag event, when we want
1439 // to add an item to one of the workspace screens.
Winson Chungaafa03c2010-06-11 17:34:16 -07001440 private void onDropExternal(int x, int y, Object dragInfo,
1441 CellLayout cellLayout, boolean insertAtFirst) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442 int screen = indexOfChild(cellLayout);
1443 if (dragInfo instanceof PendingAddItemInfo) {
1444 PendingAddItemInfo info = (PendingAddItemInfo) dragInfo;
1445 // When dragging and dropping from customization tray, we deal with creating
1446 // widgets/shortcuts/folders in a slightly different way
1447 int[] touchXY = new int[2];
1448 touchXY[0] = x;
1449 touchXY[1] = y;
1450 switch (info.itemType) {
1451 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1452 mLauncher.addAppWidgetFromDrop(info.componentName, screen, touchXY);
1453 break;
1454 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1455 mLauncher.addLiveFolderFromDrop(info.componentName, screen, touchXY);
1456 break;
1457 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1458 mLauncher.processShortcutFromDrop(info.componentName, screen, touchXY);
1459 break;
1460 default:
1461 throw new IllegalStateException("Unknown item type: " + info.itemType);
1462 }
1463 cellLayout.onDragExit();
Michael Jurka18014792010-10-14 09:01:34 -07001464 cellLayout.animateDrop();
Michael Jurka0280c3b2010-09-17 15:00:07 -07001465 return;
1466 }
1467
1468 // This is for other drag/drop cases, like dragging from All Apps
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 ItemInfo info = (ItemInfo) dragInfo;
1470
Michael Jurkaaf442092010-06-10 17:01:57 -07001471 View view = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 switch (info.itemType) {
1474 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1475 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onoratoe48e7c12010-02-19 13:10:40 -08001476 if (info.container == NO_ID && info instanceof ApplicationInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 // Came from all apps -- make a copy
Winson Chungaafa03c2010-06-11 17:34:16 -07001478 info = new ShortcutInfo((ApplicationInfo) info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001480 view = mLauncher.createShortcut(R.layout.application, cellLayout,
1481 (ShortcutInfo) info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 break;
1483 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1484 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 cellLayout, ((UserFolderInfo) info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001486 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 default:
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 throw new IllegalStateException("Unknown item type: " + info.itemType);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001491 // If the view is null, it has already been added.
1492 if (view == null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 cellLayout.onDragExit();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001494 } else {
Michael Jurka7ded67c2010-09-28 13:57:39 -07001495 mTargetCell = findNearestVacantArea(x, y, 1, 1, null, cellLayout, mTargetCell);
Winson Chungaafa03c2010-06-11 17:34:16 -07001496 addInScreen(view, indexOfChild(cellLayout), mTargetCell[0],
1497 mTargetCell[1], info.spanX, info.spanY, insertAtFirst);
1498 cellLayout.onDropChild(view);
Michael Jurka18014792010-10-14 09:01:34 -07001499 cellLayout.animateDrop();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001500 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -07001501
1502 LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
Winson Chungaafa03c2010-06-11 17:34:16 -07001504 lp.cellX, lp.cellY);
Joe Onorato00acb122009-08-04 16:04:30 -04001505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001507
Jeff Sharkey70864282009-04-07 21:08:40 -07001508 /**
1509 * Return the current {@link CellLayout}, correctly picking the destination
1510 * screen while a scroll is in progress.
1511 */
1512 private CellLayout getCurrentDropLayout() {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001513 // if we're currently small, use findMatchingPageForDragOver instead
1514 if (mIsSmall) return null;
Michael Jurka0142d492010-08-25 17:46:15 -07001515 int index = mScroller.isFinished() ? mCurrentPage : mNextPage;
Jeff Sharkey70864282009-04-07 21:08:40 -07001516 return (CellLayout) getChildAt(index);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
1518
Jeff Sharkey70864282009-04-07 21:08:40 -07001519 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 * Return the current CellInfo describing our current drag; this method exists
1521 * so that Launcher can sync this object with the correct info when the activity is created/
1522 * destroyed
1523 *
1524 */
1525 public CellLayout.CellInfo getDragInfo() {
1526 return mDragInfo;
1527 }
1528
1529 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001530 * {@inheritDoc}
1531 */
1532 public boolean acceptDrop(DragSource source, int x, int y,
Joe Onorato00acb122009-08-04 16:04:30 -04001533 int xOffset, int yOffset, DragView dragView, Object dragInfo) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001534 if (mDragTargetLayout == null) {
1535 // cancel the drag if we're not over a screen at time of drop
1536 return false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 }
Michael Jurkad718d6a2010-10-14 15:35:17 -07001538
Michael Jurkac28de512010-08-13 11:27:44 -07001539 final CellLayout.CellInfo dragCellInfo = mDragInfo;
1540 final int spanX = dragCellInfo == null ? 1 : dragCellInfo.spanX;
1541 final int spanY = dragCellInfo == null ? 1 : dragCellInfo.spanY;
Romain Guy4c58c482009-05-12 17:35:41 -07001542
Michael Jurkac28de512010-08-13 11:27:44 -07001543 final View ignoreView = dragCellInfo == null ? null : dragCellInfo.cell;
Romain Guy4c58c482009-05-12 17:35:41 -07001544
Michael Jurkad718d6a2010-10-14 15:35:17 -07001545 if (mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) {
Michael Jurka0e260592010-06-30 17:07:39 -07001546 return true;
1547 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 mLauncher.showOutOfSpaceMessage();
Michael Jurka0e260592010-06-30 17:07:39 -07001549 return false;
1550 }
Jeff Sharkey70864282009-04-07 21:08:40 -07001551 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001552
Jeff Sharkey70864282009-04-07 21:08:40 -07001553 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001554 * Calculate the nearest cell where the given object would be dropped.
1555 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07001556 private int[] findNearestVacantArea(int pixelX, int pixelY,
Jeff Sharkey70864282009-04-07 21:08:40 -07001557 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001558
Michael Jurka5f1c5092010-09-03 14:15:02 -07001559 int localPixelX = pixelX - (layout.getLeft() - mScrollX);
1560 int localPixelY = pixelY - (layout.getTop() - mScrollY);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001561
Jeff Sharkey70864282009-04-07 21:08:40 -07001562 // Find the best target drop location
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 return layout.findNearestVacantArea(
Michael Jurkafc9f07d2010-09-30 13:22:31 -07001564 localPixelX, localPixelY, spanX, spanY, ignoreView, recycle);
Jeff Sharkey70864282009-04-07 21:08:40 -07001565 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001566
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001567 /**
1568 * Estimate the size that a child with the given dimensions will take in the current screen.
1569 */
1570 void estimateChildSize(int minWidth, int minHeight, int[] result) {
Michael Jurka0142d492010-08-25 17:46:15 -07001571 ((CellLayout)getChildAt(mCurrentPage)).estimateChildSize(minWidth, minHeight, result);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001572 }
1573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 void setLauncher(Launcher launcher) {
1575 mLauncher = launcher;
1576 }
1577
Joe Onorato00acb122009-08-04 16:04:30 -04001578 public void setDragController(DragController dragController) {
1579 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
1582 public void onDropCompleted(View target, boolean success) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001583 if (success) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 if (target != this && mDragInfo != null) {
1585 final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1586 cellLayout.removeView(mDragInfo.cell);
Joe Onorato00acb122009-08-04 16:04:30 -04001587 if (mDragInfo.cell instanceof DropTarget) {
1588 mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1589 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001590 // final Object tag = mDragInfo.cell.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07001592 } else if (mDragInfo != null) {
1593 ((CellLayout) getChildAt(mDragInfo.screen)).onDropAborted(mDragInfo.cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
1595
Joe Onorato4be866d2010-10-10 11:26:02 -07001596 mDragOutline = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 mDragInfo = null;
1598 }
1599
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 public boolean isDropEnabled() {
1601 return true;
1602 }
1603
Michael Jurka0142d492010-08-25 17:46:15 -07001604 @Override
1605 protected void onRestoreInstanceState(Parcelable state) {
1606 super.onRestoreInstanceState(state);
1607 Launcher.setScreen(mCurrentPage);
1608 }
1609
1610 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 public void scrollLeft() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001612 if (!mIsSmall && !mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -07001613 super.scrollLeft();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615 }
1616
Michael Jurka0142d492010-08-25 17:46:15 -07001617 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 public void scrollRight() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001619 if (!mIsSmall && !mIsInUnshrinkAnimation) {
Michael Jurka0142d492010-08-25 17:46:15 -07001620 super.scrollRight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
1622 }
1623
Patrick Dubroy1262e362010-10-06 15:49:50 -07001624 @Override
1625 public void onEnterScrollArea(int direction) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001626 if (!mIsSmall && !mIsInUnshrinkAnimation) {
1627 mInScrollArea = true;
1628 final int screen = getCurrentPage() + ((direction == DragController.SCROLL_LEFT) ? -1 : 1);
1629 if (0 <= screen && screen < getChildCount()) {
1630 ((CellLayout) getChildAt(screen)).setHover(true);
1631 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001632
Michael Jurkad718d6a2010-10-14 15:35:17 -07001633 if (mDragTargetLayout != null) {
1634 mDragTargetLayout.onDragExit();
1635 mDragTargetLayout = null;
1636 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001637 }
1638 }
1639
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001640 private void clearAllHovers() {
1641 final int childCount = getChildCount();
1642 for (int i = 0; i < childCount; i++) {
1643 ((CellLayout) getChildAt(i)).setHover(false);
1644 }
1645 }
1646
Patrick Dubroy1262e362010-10-06 15:49:50 -07001647 @Override
1648 public void onExitScrollArea() {
Michael Jurkad718d6a2010-10-14 15:35:17 -07001649 if (mInScrollArea) {
1650 mInScrollArea = false;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001651 clearAllHovers();
Patrick Dubroy1262e362010-10-06 15:49:50 -07001652 }
1653 }
1654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 public Folder getFolderForTag(Object tag) {
Michael Jurkadee05892010-07-27 10:01:56 -07001656 final int screenCount = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 for (int screen = 0; screen < screenCount; screen++) {
1658 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1659 int count = currentScreen.getChildCount();
1660 for (int i = 0; i < count; i++) {
1661 View child = currentScreen.getChildAt(i);
1662 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1663 if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1664 Folder f = (Folder) child;
Winson Chungaafa03c2010-06-11 17:34:16 -07001665 if (f.getInfo() == tag && f.getInfo().opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 return f;
1667 }
1668 }
1669 }
1670 }
1671 return null;
1672 }
1673
1674 public View getViewForTag(Object tag) {
1675 int screenCount = getChildCount();
1676 for (int screen = 0; screen < screenCount; screen++) {
1677 CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1678 int count = currentScreen.getChildCount();
1679 for (int i = 0; i < count; i++) {
1680 View child = currentScreen.getChildAt(i);
1681 if (child.getTag() == tag) {
1682 return child;
1683 }
1684 }
1685 }
1686 return null;
1687 }
1688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689
Joe Onorato64e6be72010-03-05 15:05:52 -05001690 void removeItems(final ArrayList<ApplicationInfo> apps) {
Michael Jurkadee05892010-07-27 10:01:56 -07001691 final int screenCount = getChildCount();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001692 final PackageManager manager = getContext().getPackageManager();
Romain Guy629de3e2010-01-13 12:20:59 -08001693 final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
Romain Guy574d20e2009-06-01 15:34:04 -07001694
Joe Onorato64e6be72010-03-05 15:05:52 -05001695 final HashSet<String> packageNames = new HashSet<String>();
1696 final int appCount = apps.size();
1697 for (int i = 0; i < appCount; i++) {
1698 packageNames.add(apps.get(i).componentName.getPackageName());
1699 }
1700
Michael Jurkadee05892010-07-27 10:01:56 -07001701 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 final CellLayout layout = (CellLayout) getChildAt(i);
Romain Guy574d20e2009-06-01 15:34:04 -07001703
Romain Guy629de3e2010-01-13 12:20:59 -08001704 // Avoid ANRs by treating each screen separately
1705 post(new Runnable() {
1706 public void run() {
1707 final ArrayList<View> childrenToRemove = new ArrayList<View>();
1708 childrenToRemove.clear();
Winson Chungaafa03c2010-06-11 17:34:16 -07001709
Romain Guy629de3e2010-01-13 12:20:59 -08001710 int childCount = layout.getChildCount();
1711 for (int j = 0; j < childCount; j++) {
1712 final View view = layout.getChildAt(j);
1713 Object tag = view.getTag();
Winson Chungaafa03c2010-06-11 17:34:16 -07001714
Joe Onorato0589f0f2010-02-08 13:44:00 -08001715 if (tag instanceof ShortcutInfo) {
1716 final ShortcutInfo info = (ShortcutInfo) tag;
Romain Guy629de3e2010-01-13 12:20:59 -08001717 final Intent intent = info.intent;
1718 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07001719
Joe Onorato64e6be72010-03-05 15:05:52 -05001720 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1721 for (String packageName: packageNames) {
1722 if (packageName.equals(name.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001723 LauncherModel.deleteItemFromDatabase(mLauncher, info);
1724 childrenToRemove.add(view);
1725 }
1726 }
Romain Guy629de3e2010-01-13 12:20:59 -08001727 }
1728 } else if (tag instanceof UserFolderInfo) {
1729 final UserFolderInfo info = (UserFolderInfo) tag;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001730 final ArrayList<ShortcutInfo> contents = info.contents;
1731 final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
Romain Guy629de3e2010-01-13 12:20:59 -08001732 final int contentsCount = contents.size();
1733 boolean removedFromFolder = false;
Winson Chungaafa03c2010-06-11 17:34:16 -07001734
Romain Guy629de3e2010-01-13 12:20:59 -08001735 for (int k = 0; k < contentsCount; k++) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001736 final ShortcutInfo appInfo = contents.get(k);
Romain Guy629de3e2010-01-13 12:20:59 -08001737 final Intent intent = appInfo.intent;
1738 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07001739
Joe Onorato64e6be72010-03-05 15:05:52 -05001740 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1741 for (String packageName: packageNames) {
1742 if (packageName.equals(name.getPackageName())) {
1743 toRemove.add(appInfo);
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -07001744 LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
Joe Onorato64e6be72010-03-05 15:05:52 -05001745 removedFromFolder = true;
1746 }
1747 }
Romain Guy629de3e2010-01-13 12:20:59 -08001748 }
1749 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001750
Romain Guy629de3e2010-01-13 12:20:59 -08001751 contents.removeAll(toRemove);
1752 if (removedFromFolder) {
1753 final Folder folder = getOpenFolder();
Winson Chungaafa03c2010-06-11 17:34:16 -07001754 if (folder != null)
1755 folder.notifyDataSetChanged();
Romain Guy629de3e2010-01-13 12:20:59 -08001756 }
1757 } else if (tag instanceof LiveFolderInfo) {
1758 final LiveFolderInfo info = (LiveFolderInfo) tag;
1759 final Uri uri = info.uri;
1760 final ProviderInfo providerInfo = manager.resolveContentProvider(
1761 uri.getAuthority(), 0);
Romain Guy574d20e2009-06-01 15:34:04 -07001762
Joe Onoratof11079b2010-04-15 11:47:28 -07001763 if (providerInfo != null) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001764 for (String packageName: packageNames) {
1765 if (packageName.equals(providerInfo.packageName)) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001766 LauncherModel.deleteItemFromDatabase(mLauncher, info);
Winson Chungaafa03c2010-06-11 17:34:16 -07001767 childrenToRemove.add(view);
Joe Onorato64e6be72010-03-05 15:05:52 -05001768 }
1769 }
Romain Guy629de3e2010-01-13 12:20:59 -08001770 }
1771 } else if (tag instanceof LauncherAppWidgetInfo) {
1772 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
1773 final AppWidgetProviderInfo provider =
1774 widgets.getAppWidgetInfo(info.appWidgetId);
Daniel Sandlere1cc6c32010-05-07 11:49:29 -04001775 if (provider != null) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001776 for (String packageName: packageNames) {
1777 if (packageName.equals(provider.provider.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001778 LauncherModel.deleteItemFromDatabase(mLauncher, info);
Winson Chungaafa03c2010-06-11 17:34:16 -07001779 childrenToRemove.add(view);
Joe Onorato64e6be72010-03-05 15:05:52 -05001780 }
1781 }
Romain Guy629de3e2010-01-13 12:20:59 -08001782 }
Romain Guy574d20e2009-06-01 15:34:04 -07001783 }
1784 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001785
Romain Guy629de3e2010-01-13 12:20:59 -08001786 childCount = childrenToRemove.size();
1787 for (int j = 0; j < childCount; j++) {
1788 View child = childrenToRemove.get(j);
1789 layout.removeViewInLayout(child);
1790 if (child instanceof DropTarget) {
1791 mDragController.removeDropTarget((DropTarget)child);
1792 }
Romain Guy574d20e2009-06-01 15:34:04 -07001793 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001794
Romain Guy629de3e2010-01-13 12:20:59 -08001795 if (childCount > 0) {
1796 layout.requestLayout();
1797 layout.invalidate();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
Romain Guy629de3e2010-01-13 12:20:59 -08001800 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 }
1802 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001803
Joe Onorato64e6be72010-03-05 15:05:52 -05001804 void updateShortcuts(ArrayList<ApplicationInfo> apps) {
Michael Jurkadee05892010-07-27 10:01:56 -07001805 final int screenCount = getChildCount();
1806 for (int i = 0; i < screenCount; i++) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001807 final CellLayout layout = (CellLayout) getChildAt(i);
1808 int childCount = layout.getChildCount();
1809 for (int j = 0; j < childCount; j++) {
1810 final View view = layout.getChildAt(j);
1811 Object tag = view.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001812 if (tag instanceof ShortcutInfo) {
1813 ShortcutInfo info = (ShortcutInfo)tag;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001814 // We need to check for ACTION_MAIN otherwise getComponent() might
1815 // return null for some shortcuts (for instance, for shortcuts to
1816 // web pages.)
1817 final Intent intent = info.intent;
1818 final ComponentName name = intent.getComponent();
1819 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Joe Onorato64e6be72010-03-05 15:05:52 -05001820 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1821 final int appCount = apps.size();
Winson Chungaafa03c2010-06-11 17:34:16 -07001822 for (int k = 0; k < appCount; k++) {
Joe Onorato64e6be72010-03-05 15:05:52 -05001823 ApplicationInfo app = apps.get(k);
1824 if (app.componentName.equals(name)) {
1825 info.setIcon(mIconCache.getIcon(info.intent));
1826 ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
1827 new FastBitmapDrawable(info.getIcon(mIconCache)),
1828 null, null);
1829 }
1830 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001831 }
1832 }
1833 }
1834 }
1835 }
1836
Joe Onorato14f122b2009-11-19 14:06:36 -08001837 void moveToDefaultScreen(boolean animate) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001838 if (mIsSmall || mIsInUnshrinkAnimation) {
1839 mLauncher.showWorkspace(animate, (CellLayout)getChildAt(mDefaultPage));
1840 } else if (animate) {
1841 snapToPage(mDefaultPage);
Joe Onoratoc45b1682010-01-11 18:48:40 -05001842 } else {
Michael Jurka0142d492010-08-25 17:46:15 -07001843 setCurrentPage(mDefaultPage);
Joe Onoratoc45b1682010-01-11 18:48:40 -05001844 }
Michael Jurka0142d492010-08-25 17:46:15 -07001845 getChildAt(mDefaultPage).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Romain Guy8a73c512009-11-09 19:19:59 -08001848 void setIndicators(Drawable previous, Drawable next) {
1849 mPreviousIndicator = previous;
1850 mNextIndicator = next;
Michael Jurka0142d492010-08-25 17:46:15 -07001851 previous.setLevel(mCurrentPage);
1852 next.setLevel(mCurrentPage);
Romain Guy8a73c512009-11-09 19:19:59 -08001853 }
1854
Michael Jurka0142d492010-08-25 17:46:15 -07001855 @Override
1856 public void syncPages() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Michael Jurka0142d492010-08-25 17:46:15 -07001858
1859 @Override
1860 public void syncPageItems(int page) {
1861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863}