Workspace.java revision 7bd1bbb509f9569fa18d6b4d33242679fd98bc9b
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
190280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.Animator;
20b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haaseimport android.animation.AnimatorSet;
210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.ObjectAnimator;
22cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroyimport android.animation.TimeInterpolator;
23cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroyimport android.animation.ValueAnimator;
24cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroyimport android.animation.ValueAnimator.AnimatorUpdateListener;
2568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport android.app.AlertDialog;
268f573952b8729b4319043ae0908997ecd2d68951Dianne Hackbornimport android.app.WallpaperManager;
27038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurkaimport android.appwidget.AppWidgetHostView;
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
29629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
30a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.content.ClipData;
31a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.content.ClipDescription;
32495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.content.ComponentName;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
3431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
357247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroyimport android.content.res.Resources;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.TypedArray;
374be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onoratoimport android.graphics.Bitmap;
386b879f0a5885274a85333531e091283405d490ccAdam Lesinskiimport android.graphics.Camera;
3931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Canvas;
40a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurkaimport android.graphics.Matrix;
41a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.graphics.Paint;
42b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chungimport android.graphics.Point;
4370fc438583779ef903541bf9ec546a8235a18785Winson Chungimport android.graphics.PorterDuff;
4431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Rect;
45a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.graphics.RectF;
464be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onoratoimport android.graphics.Region.Op;
47f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.graphics.drawable.Drawable;
48956091ba7863bd72086275e61084864994dd6fa7Joe Onoratoimport android.os.IBinder;
49495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.os.Parcelable;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.util.AttributeSet;
51b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chungimport android.util.DisplayMetrics;
52291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandlerimport android.util.Log;
53580e27748137ff08599aa719d106b31215a28353Winson Chungimport android.util.Pair;
54a34abf8c781485b788fddacb352d586bffca886cWinson Chungimport android.view.Display;
55a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.view.DragEvent;
5631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.MotionEvent;
5731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.View;
586e3140865d3f0def2e55934d8b0b2c1503386e54Winson Chungimport android.view.ViewGroup;
59ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohenimport android.view.View.MeasureSpec;
60cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroyimport android.view.animation.DecelerateInterpolator;
61a6427b15c18d5b8f3078f553d78f8432de9f46e9Winson Chungimport android.widget.ImageView;
62f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.widget.TextView;
63a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chungimport android.widget.Toast;
6431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
656639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohenimport com.android.launcher.R;
6619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohenimport com.android.launcher2.FolderIcon.FolderRingAnimator;
676639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohenimport com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
68edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
69716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport java.util.ArrayList;
70716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport java.util.HashSet;
71716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport java.util.List;
72716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen
7331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
740142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * The workspace is a wide area with a wallpaper and a finite number of pages.
750142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * Each page contains a number of icons, folders or widgets the user can
76aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung * interact with. A workspace is meant to be used with a fixed width only.
7731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
780142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurkapublic class Workspace extends SmoothPagedView
79d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        implements DropTarget, DragSource, DragScroller, View.OnTouchListener,
809433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        DragController.DragListener, LauncherTransitionable {
81e47f55c30b9c24f01f2be861247c92f17fbe4a61Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
823a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato    private static final String TAG = "Launcher.Workspace";
830142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
84f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    // Y rotation to apply to the workspace screens
85f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private static final float WORKSPACE_ROTATION = 12.5f;
86b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    private static final float WORKSPACE_OVERSCROLL_ROTATION = 24f;
87cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    private static float CAMERA_DISTANCE = 6500;
88a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
8968d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0;
9068d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375;
919171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100;
92f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
93f135c6c364e92cddb84c1b4a94ed535214b712a6Winson Chung    private static final int BACKGROUND_FADE_OUT_DURATION = 350;
94ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    private static final int ADJACENT_SCREEN_DROP_DURATION = 300;
95265b9a66542148e0ff13971001cb9461065e1e0eAdam Cohen    private static final int FLING_THRESHOLD_VELOCITY = 500;
96ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
979171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    // These animators are used to fade the children's outlines
989171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    private ObjectAnimator mChildrenOutlineFadeInAnimation;
999171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    private ObjectAnimator mChildrenOutlineFadeOutAnimation;
1009171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    private float mChildrenOutlineAlpha = 0;
1019171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
1029171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    // These properties refer to the background protection gradient used for AllApps and Customize
103e0f5a61a1f105216b0067124bacac69194c0ef70Michael Jurka    private ValueAnimator mBackgroundFadeInAnimation;
104e0f5a61a1f105216b0067124bacac69194c0ef70Michael Jurka    private ValueAnimator mBackgroundFadeOutAnimation;
1059171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    private Drawable mBackground;
10625356e75ebae5d6e0be43f38d2c73b5cb59d05f2Michael Jurka    boolean mDrawBackground = true;
107f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private float mBackgroundAlpha = 0;
10868d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    private float mOverScrollMaxBackgroundAlpha = 0.0f;
109e0f66b546994a9bdee452851c17a148db02ec300Adam Cohen    private int mOverScrollPageIndex = -1;
110f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
111beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    private float mWallpaperScrollRatio = 1.0f;
112beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
1138f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn    private final WallpaperManager mWallpaperManager;
1149c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    private IBinder mWindowToken;
1151670698ff3502548aa066e32c28ecf62c5592a34Michael Jurka    private static final float WALLPAPER_SCREENS_SPAN = 2f;
116aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    private int mDefaultPage;
11831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
12031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * CellInfo for the cell that is currently being dragged
12131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
12231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private CellLayout.CellInfo mDragInfo;
123aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
12470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
12570864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Target drop area calculated during last acceptDrop call.
12670864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
127c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    private int[] mTargetCell = new int[2];
12831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1296569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    /**
1306569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     * The CellLayout that is currently being dragged over
1316569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     */
1326569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    private CellLayout mDragTargetLayout = null;
1338a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen    private boolean mDragHasEnteredWorkspace = false;
1346569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
13531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Launcher mLauncher;
1360589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
13700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private DragController mDragController;
138aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1394516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    // These are temporary variables to prevent having to allocate a new object just to
1404516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
14131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private int[] mTempCell = new int[2];
14270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    private int[] mTempEstimate = new int[2];
143e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    private float[] mDragViewVisualCenter = new float[2];
144a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    private float[] mTempDragCoordinates = new float[2];
1454516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    private float[] mTempCellLayoutCenterCoordinates = new float[2];
146a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    private float[] mTempDragBottomRightCoordinates = new float[2];
1470280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private Matrix mTempInverseMatrix = new Matrix();
14831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
149c2f7f4794867cd1b3e97fd0f379ad8ad15958a6cMichael Jurka    private SpringLoadedDragController mSpringLoadedDragController;
150b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung    private float mSpringLoadedShrinkFactor;
151d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
152d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static final int DEFAULT_CELL_COUNT_X = 4;
153d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static final int DEFAULT_CELL_COUNT_Y = 4;
15431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1551262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    // State variable that indicates whether the pages are small (ie when you're
156dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // in all apps or customize mode)
157d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
158d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    enum State { NORMAL, SPRING_LOADED, SMALL };
1597777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen    private State mState = State.NORMAL;
160d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    private boolean mIsSwitchingState = false;
161d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
162d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    boolean mAnimatingViewIntoPlace = false;
163d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    boolean mIsDragOccuring = false;
164d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    boolean mChildrenLayersEnabled = true;
165dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
16654fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy    /** Is the user is dragging an item near the edge of a page? */
1671262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    private boolean mInScrollArea = false;
1681262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy
1698e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
1704be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    private Bitmap mDragOutline = null;
1718e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    private final Rect mTempRect = new Rect();
1728e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy    private final int[] mTempXY = new int[2];
17321b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen    private float mOverscrollFade = 0;
1744be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
175a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    // Paint used to draw external drop outline
176a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    private final Paint mExternalDragOutlinePaint = new Paint();
177a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
1786b879f0a5885274a85333531e091283405d490ccAdam Lesinski    // Camera and Matrix used to determine the final position of a neighboring CellLayout
1796b879f0a5885274a85333531e091283405d490ccAdam Lesinski    private final Matrix mMatrix = new Matrix();
1806b879f0a5885274a85333531e091283405d490ccAdam Lesinski    private final Camera mCamera = new Camera();
1816b879f0a5885274a85333531e091283405d490ccAdam Lesinski    private final float mTempFloat2[] = new float[2];
1826b879f0a5885274a85333531e091283405d490ccAdam Lesinski
183c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    enum WallpaperVerticalOffset { TOP, MIDDLE, BOTTOM };
184c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    int mWallpaperWidth;
185c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    int mWallpaperHeight;
186ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    WallpaperOffsetInterpolator mWallpaperOffset;
187c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    boolean mUpdateWallpaperOffsetImmediately = false;
18826976d9b53161f80faf822d482ac771b8621cf31Adam Cohen    private Runnable mDelayedResizeRunnable;
189cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    private int mDisplayWidth;
190cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    private int mDisplayHeight;
191cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    private int mWallpaperTravelWidth;
192c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
19319072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
194c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    private static final int FOLDER_CREATION_TIMEOUT = 250;
19519072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private final Alarm mFolderCreationAlarm = new Alarm();
19619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private FolderRingAnimator mDragFolderRingAnimator = null;
19719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private View mLastDragOverView = null;
19819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private boolean mCreateUserFolderOnDrop = false;
199073a46ff635d089cbcb15d939d1de42dd2b86c97Adam Cohen
200f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget)
201f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    private float mXDown;
202f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    private float mYDown;
203f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
204f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
205f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float TOUCH_SLOP_DAMPING_FACTOR = 4;
206f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
207ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    // Relating to the animation of items being dropped externally
208ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    public static final int ANIMATE_INTO_POSITION = 0;
209ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 1;
210ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 2;
211ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
2124b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    // These variables are used for storing the initial and final values during workspace animations
213ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    private int mSavedScrollX;
214ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    private float mSavedRotationY;
215ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    private float mSavedTranslationX;
2164b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float mCurrentScaleX;
2174b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float mCurrentScaleY;
2184b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float mCurrentRotationY;
2194b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float mCurrentTranslationX;
2204b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float mCurrentTranslationY;
2214b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldTranslationXs;
2224b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldTranslationYs;
2234b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldScaleXs;
2244b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldScaleYs;
2254b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldBackgroundAlphas;
2264b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldBackgroundAlphaMultipliers;
2274b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldAlphas;
2284b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mOldRotationYs;
2294b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewTranslationXs;
2304b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewTranslationYs;
2314b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewScaleXs;
2324b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewScaleYs;
2334b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewBackgroundAlphas;
2344b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewBackgroundAlphaMultipliers;
2354b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewAlphas;
2364b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private float[] mNewRotationYs;
237e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung    private float mTransitionProgress = 1f;
2384b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
23931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
24031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
24131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
24231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
2430142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
24431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
24531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs) {
24631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        this(context, attrs, 0);
24731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
24831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
25031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
25131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
25231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
2530142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
25431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param defStyle Unused.
25531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
25631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs, int defStyle) {
25731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super(context, attrs, defStyle);
2580142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mContentIsRefreshable = false;
2595f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
260f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // With workspace, data is available straight from the get-go
261f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        setDataIsReady();
262f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
2632b8e44577096a737b97c6687fc8ad1f067ea1e89Winson Chung        mFadeInAdjacentScreens =
2642b8e44577096a737b97c6687fc8ad1f067ea1e89Winson Chung            getResources().getBoolean(R.bool.config_workspaceFadeAdjacentScreens);
2658f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn        mWallpaperManager = WallpaperManager.getInstance(context);
266aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
267f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        int cellCountX = DEFAULT_CELL_COUNT_X;
268f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        int cellCountY = DEFAULT_CELL_COUNT_Y;
269f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka
270aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        TypedArray a = context.obtainStyledAttributes(attrs,
271aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                R.styleable.Workspace, defStyle, 0);
272f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka
273b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        final Resources res = context.getResources();
274f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        if (LauncherApplication.isScreenLarge()) {
275f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            // Determine number of rows/columns dynamically
276f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            // TODO: This code currently fails on tablets with an aspect ratio < 1.3.
277f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            // Around that ratio we should make cells the same size in portrait and
278f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            // landscape
279f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka            TypedArray actionBarSizeTypedArray =
280f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka                context.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
281f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            final float actionBarHeight = actionBarSizeTypedArray.getDimension(0, 0f);
282f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            final float systemBarHeight = res.getDimension(R.dimen.status_bar_height);
283f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            final float smallestScreenDim = res.getConfiguration().smallestScreenWidthDp;
284f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka
285f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            cellCountX = 1;
286f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            while (CellLayout.widthInPortrait(res, cellCountX + 1) <= smallestScreenDim) {
287f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka                cellCountX++;
288f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka            }
289f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka
290f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            cellCountY = 1;
291f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka            while (actionBarHeight + CellLayout.heightInLandscape(res, cellCountY + 1)
292f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka                <= smallestScreenDim - systemBarHeight) {
293f61249b79948a4b37b38104cbe81c98086d1f30bMichael Jurka                cellCountY++;
294f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka            }
295f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        }
296f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka
297b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        mSpringLoadedShrinkFactor =
298b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung            res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
299b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung
300f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        // if the value is manually specified, use that instead
301f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        cellCountX = a.getInt(R.styleable.Workspace_cellCountX, cellCountX);
302f6440da9d02f3ee1553db4bd431a202eb1d1a9ddMichael Jurka        cellCountY = a.getInt(R.styleable.Workspace_cellCountY, cellCountY);
3030142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
30431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        a.recycle();
30531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
306d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY);
3070d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato        setHapticFeedbackEnabled(false);
3080142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
309cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mLauncher = (Launcher) context;
31031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        initWorkspace();
311c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung
312c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung        // Disable multitouch across the workspace/all apps/customize tray
313c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung        setMotionEventSplittingEnabled(true);
31431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
31531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
316038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each
317038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    // dimension if unsuccessful
318038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    public int[] estimateItemSize(int hSpan, int vSpan,
319038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            PendingAddItemInfo pendingItemInfo, boolean springLoaded) {
320038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        int[] size = new int[2];
321038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        if (getChildCount() > 0) {
322038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            CellLayout cl = (CellLayout) mLauncher.getWorkspace().getChildAt(0);
323038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            RectF r = estimateItemPosition(cl, pendingItemInfo, 0, 0, hSpan, vSpan);
324038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[0] = (int) r.width();
325038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[1] = (int) r.height();
326038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            if (springLoaded) {
327038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka                size[0] *= mSpringLoadedShrinkFactor;
328038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka                size[1] *= mSpringLoadedShrinkFactor;
329038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            }
330038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            return size;
331038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        } else {
332038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[0] = Integer.MAX_VALUE;
333038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[1] = Integer.MAX_VALUE;
334038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            return size;
335038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        }
336038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    }
337038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    public RectF estimateItemPosition(CellLayout cl, ItemInfo pendingInfo,
338038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            int hCell, int vCell, int hSpan, int vSpan) {
339038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        RectF r = new RectF();
340038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        cl.cellToRect(hCell, vCell, hSpan, vSpan, r);
341038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        return r;
342038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    }
343038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka
344c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    public void buildPageHardwareLayers() {
345c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        if (getWindowToken() != null) {
346c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka            final int childCount = getChildCount();
347c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka            for (int i = 0; i < childCount; i++) {
348c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka                CellLayout cl = (CellLayout) getChildAt(i);
349c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka                cl.buildChildrenLayer();
350c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka            }
351c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        }
352c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    }
353c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka
354d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    public void onDragStart(DragSource source, Object info, int dragAction) {
355d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        mIsDragOccuring = true;
356d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        updateChildrenLayersEnabled();
357446e940fc8839173de12f1299b8c24f318d53a3fAdam Cohen        mLauncher.lockScreenOrientationOnLargeUI();
358d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
359d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
360d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    public void onDragEnd() {
361d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        mIsDragOccuring = false;
362d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        updateChildrenLayersEnabled();
363446e940fc8839173de12f1299b8c24f318d53a3fAdam Cohen        mLauncher.unlockScreenOrientationOnLargeUI();
364d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
365d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
36631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
36731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Initializes various states for this workspace.
36831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
3690142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void initWorkspace() {
3700589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Context context = getContext();
3710142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mCurrentPage = mDefaultPage;
3720142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
3730589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        LauncherApplication app = (LauncherApplication)context.getApplicationContext();
3740589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = app.getIconCache();
375a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        mExternalDragOutlinePaint.setAntiAlias(true);
376cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        setWillNotDraw(false);
3777777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        setChildrenDrawnWithCacheEnabled(true);
37831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
379b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung        try {
380b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung            final Resources res = getResources();
381fd3385fe9e0f034b04f99d5d59a58d74fe040da4Winson Chung            mBackground = res.getDrawable(R.drawable.apps_customize_bg);
382b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung        } catch (Resources.NotFoundException e) {
383b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung            // In this case, we will skip drawing background protection
384b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung        }
3859171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
386ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        mWallpaperOffset = new WallpaperOffsetInterpolator();
387cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        Display display = mLauncher.getWindowManager().getDefaultDisplay();
388cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mDisplayWidth = display.getWidth();
389cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mDisplayHeight = display.getHeight();
390cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mWallpaperTravelWidth = (int) (mDisplayWidth *
391cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                wallpaperTravelToScreenWidthRatio(mDisplayWidth, mDisplayHeight));
392265b9a66542148e0ff13971001cb9461065e1e0eAdam Cohen
393265b9a66542148e0ff13971001cb9461065e1e0eAdam Cohen        mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
394265b9a66542148e0ff13971001cb9461065e1e0eAdam Cohen
39531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
39631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
39731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
398f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    protected int getScrollMode() {
399b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        return SmoothPagedView.X_LARGE_MODE;
400f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
401f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
40208ee77090f1fef565142e9341914369b1aa342d1Michael Jurka    @Override
40308ee77090f1fef565142e9341914369b1aa342d1Michael Jurka    protected void onViewAdded(View child) {
40408ee77090f1fef565142e9341914369b1aa342d1Michael Jurka        super.onViewAdded(child);
40531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
40631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
40731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
4082801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        CellLayout cl = ((CellLayout) child);
4092801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        cl.setOnInterceptTouchListener(this);
4102801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        cl.setClickable(true);
4112801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        cl.enableHardwareLayers();
412e5fb0f27bca7afb996258a7163c76ca7390d7bffMichael Jurka    }
4132801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
41431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
41531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return The open folder on the current screen, or null if there is none
41631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
41731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    Folder getOpenFolder() {
418716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        DragLayer dragLayer = mLauncher.getDragLayer();
4198e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen        int count = dragLayer.getChildCount();
42031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int i = 0; i < count; i++) {
4218e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen            View child = dragLayer.getChildAt(i);
422aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            if (child instanceof Folder) {
423aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                Folder folder = (Folder) child;
424aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                if (folder.getInfo().opened)
425aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    return folder;
42631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
42731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
42831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
42931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
43031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
431a0aa0121b91fde17e427ab2763ce312e64cf5d33Patrick Dubroy    boolean isTouchActive() {
432a0aa0121b91fde17e427ab2763ce312e64cf5d33Patrick Dubroy        return mTouchState != TOUCH_STATE_REST;
43331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
43431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
43531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
43631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Adds the specified child in the specified screen. The position and dimension of
43731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * the child are defined by x, y, spanX and spanY.
43831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
43931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param child The child to add in one of the workspace's screens.
44031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param screen The screen in which to add the child.
44131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param x The X position of the child in the screen's grid.
44231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param y The Y position of the child in the screen's grid.
44331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanX The number of cells spanned horizontally by the child.
44431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanY The number of cells spanned vertically by the child.
44531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
4463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY) {
4473d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addInScreen(child, container, screen, x, y, spanX, spanY, false);
448aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
449aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
45031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
45131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Adds the specified child in the specified screen. The position and dimension of
45231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * the child are defined by x, y, spanX and spanY.
45331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
45431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param child The child to add in one of the workspace's screens.
45531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param screen The screen in which to add the child.
45631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param x The X position of the child in the screen's grid.
45731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param y The Y position of the child in the screen's grid.
45831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanX The number of cells spanned horizontally by the child.
45931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanY The number of cells spanned vertically by the child.
46031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param insert When true, the child is inserted at the beginning of the children list.
46131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
4623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY,
4633d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            boolean insert) {
4643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (screen < 0 || screen >= getChildCount()) {
4663d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                Log.e(TAG, "The screen must be >= 0 and < " + getChildCount()
4673d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    + " (was " + screen + "); skipping child");
4683d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return;
4693d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
4703d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
4713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
4723d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final CellLayout layout;
4733d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
4743d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layout = mLauncher.getHotseat().getLayout();
4754d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            child.setOnKeyListener(null);
4763d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
477099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Hide folder title in the hotseat
478099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (child instanceof FolderIcon) {
479099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) child).setTextVisible(false);
480099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
481099f60d51f6310e1290687e83958e665b3324504Adam Cohen
4823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (screen < 0) {
4833d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                screen = mLauncher.getHotseat().getOrderInHotseat(x, y);
4843d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
4853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                // Note: We do this to ensure that the hotseat is always laid out in the orientation
4863d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                // of the hotseat in order regardless of which orientation they were added
4873d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                x = mLauncher.getHotseat().getCellXFromOrder(screen);
4883d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                y = mLauncher.getHotseat().getCellYFromOrder(screen);
4893d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
4903d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
491099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Show folder title if not in the hotseat
492099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (child instanceof FolderIcon) {
493099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) child).setTextVisible(true);
494099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
495099f60d51f6310e1290687e83958e665b3324504Adam Cohen
4963d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layout = (CellLayout) getChildAt(screen);
497ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            child.setOnKeyListener(new IconKeyEventListener());
49831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
49931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
500ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        LayoutParams genericLp = child.getLayoutParams();
501ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        CellLayout.LayoutParams lp;
502ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
50331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
50431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
505ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            lp = (CellLayout.LayoutParams) genericLp;
50631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellX = x;
50731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellY = y;
50831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellHSpan = spanX;
50931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellVSpan = spanY;
51031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
511aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
5127f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen        if (spanX < 0 && spanY < 0) {
5137f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen            lp.isLockedToGrid = false;
5147f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen        }
5157f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen
5166569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // Get the canonical child id to uniquely represent this view in this screen
5173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int childId = LauncherModel.getCellLayoutChildId(container, screen, x, y, spanX, spanY);
518f3ca3ab6958b104cbf2c2fa04add97d372a94d1cMichael Jurka        boolean markCellsAsOccupied = !(child instanceof Folder);
5193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
520aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // TODO: This branch occurs when the workspace is adding views
521aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // outside of the defined grid
5226569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            // maybe we should be deleting these items from the LauncherModel?
523aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
524aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
525aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
52631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof Folder)) {
5270d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato            child.setHapticFeedbackEnabled(false);
52831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            child.setOnLongClickListener(mLongClickListener);
52931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
53000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        if (child instanceof DropTarget) {
531aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            mDragController.addDropTarget((DropTarget) child);
53200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        }
53331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
53431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
535d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    /**
536d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy     * Check if the point (x, y) hits a given page.
537d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy     */
538d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    private boolean hitsPage(int index, float x, float y) {
539d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        final View page = getChildAt(index);
540d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        if (page != null) {
541d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy            float[] localXY = { x, y };
542d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy            mapPointFromSelfToChild(page, localXY);
543d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy            return (localXY[0] >= 0 && localXY[0] < page.getWidth()
544d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy                    && localXY[1] >= 0 && localXY[1] < page.getHeight());
545d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        }
546d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        return false;
547d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    }
548d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy
549d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    @Override
550d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    protected boolean hitsPreviousPage(float x, float y) {
551d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        // mNextPage is set to INVALID_PAGE whenever we are stationary.
552d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        // Calculating "next page" this way ensures that you scroll to whatever page you tap on
553d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage;
554b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka
555b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        // Only allow tap to next page on large devices, where there's significant margin outside
556b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        // the active workspace
557b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        return LauncherApplication.isScreenLarge() && hitsPage(current - 1, x, y);
558d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    }
559d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy
560d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    @Override
561d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    protected boolean hitsNextPage(float x, float y) {
562d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        // mNextPage is set to INVALID_PAGE whenever we are stationary.
563d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        // Calculating "next page" this way ensures that you scroll to whatever page you tap on
564d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy        final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage;
565b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka
566b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        // Only allow tap to next page on large devices, where there's significant margin outside
567b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        // the active workspace
568b72094fe1da249cc16af517d5ef598b3cbcbdf11Michael Jurka        return LauncherApplication.isScreenLarge() && hitsPage(current + 1, x, y);
569d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    }
570d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy
571e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy    /**
572e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * Called directly from a CellLayout (not by the framework), after we've been added as a
573e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout
574e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * that it should intercept touch events, which is not something that is normally supported.
575e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     */
576e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy    @Override
577dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    public boolean onTouch(View v, MotionEvent event) {
578d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        return (isSmall() || mIsSwitchingState);
579e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy    }
580e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy
581fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    public boolean isSwitchingState() {
582fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen        return mIsSwitchingState;
583fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    }
584fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen
585ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    protected void onWindowVisibilityChanged (int visibility) {
586ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mLauncher.onWindowVisibilityChanged(visibility);
587ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
588ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
5895f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
5905f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean dispatchUnhandledMove(View focused, int direction) {
591d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isSmall() || mIsSwitchingState) {
5925f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            // when the home screens are shrunken, shouldn't allow side-scrolling
5935f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            return false;
5945f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        }
5955f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.dispatchUnhandledMove(focused, direction);
5965f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
5975f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
5985f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
5995f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean onInterceptTouchEvent(MotionEvent ev) {
600d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        switch (ev.getAction() & MotionEvent.ACTION_MASK) {
601d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_DOWN:
602f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen            mXDown = ev.getX();
603f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen            mYDown = ev.getY();
604d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            break;
605d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_POINTER_UP:
606d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_UP:
607d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            if (mTouchState == TOUCH_STATE_REST) {
608d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
609d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                if (!currentPage.lastDownOnOccupiedCell()) {
610d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                    onWallpaperTap(ev);
611d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                }
612d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            }
613f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen        }
6145f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.onInterceptTouchEvent(ev);
6155f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
6165f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
6171adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    @Override
6181adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    protected void determineScrollingStart(MotionEvent ev) {
619d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
62069c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            float deltaX = Math.abs(ev.getX() - mXDown);
62169c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            float deltaY = Math.abs(ev.getY() - mYDown);
622f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
62369c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            if (Float.compare(deltaX, 0f) == 0) return;
624f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
62569c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            float slope = deltaY / deltaX;
62669c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            float theta = (float) Math.atan(slope);
627f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
62869c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            if (deltaX > mTouchSlop || deltaY > mTouchSlop) {
62969c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                cancelCurrentPageLongPress();
63069c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            }
631f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
63269c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            if (theta > MAX_SWIPE_ANGLE) {
63369c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
63469c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                return;
63569c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
63669c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
63769c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // increase the touch slop to make it harder to begin scrolling the workspace. This
63869c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // results in vertically scrolling widgets to more easily. The higher the angle, the
63969c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // more we increase touch slop.
64069c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
64169c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                float extraRatio = (float)
64269c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                        Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
64369c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
64469c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            } else {
64569c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
64669c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen                super.determineScrollingStart(ev);
64769c42da4d30f1cbba5fe1ac8960b4e305e14b807Adam Cohen            }
648f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen        }
6491adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    }
6501adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka
651007c69867d821ea2b271398577a8b3440b3a7046Winson Chung    @Override
652007c69867d821ea2b271398577a8b3440b3a7046Winson Chung    protected boolean isScrollingIndicatorEnabled() {
653d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        return mState != State.SPRING_LOADED;
654007c69867d821ea2b271398577a8b3440b3a7046Winson Chung    }
655007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
6561262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    protected void onPageBeginMoving() {
657007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        super.onPageBeginMoving();
658007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
659d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isHardwareAccelerated()) {
660d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            updateChildrenLayersEnabled();
6610142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        } else {
662d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            if (mNextPage != INVALID_PAGE) {
663d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // we're snapping to a particular screen
664d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                enableChildrenCache(mCurrentPage, mNextPage);
665d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            } else {
666d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // this is when user is actively dragging a particular screen, they might
667d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // swipe it either left or right (but we won't advance by more than one screen)
668d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
669d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            }
67031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
671007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
672007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        // Only show page outlines as we pan if we are on large screen
673007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        if (LauncherApplication.isScreenLarge()) {
674007c69867d821ea2b271398577a8b3440b3a7046Winson Chung            showOutlines();
675007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        }
6761afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung
6771afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        // Show the scroll indicator as you pan the page
6781afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        showScrollingIndicator(false);
67931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
68031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
6811262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    protected void onPageEndMoving() {
682007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        super.onPageEndMoving();
683007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
684d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isHardwareAccelerated()) {
685d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            updateChildrenLayersEnabled();
686d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        } else {
687d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            clearChildrenCache();
688d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        }
689d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
6903bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung
6913bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung        if (mDragController.isDragging()) {
6923bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung            if (isSmall()) {
6933bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung                // If we are in springloaded mode, then force an event to check if the current touch
6943bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung                // is under a new page (to scroll to)
6953bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung                mDragController.forceMoveEvent();
6963bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung            }
6973bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung        } else {
6983bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung            // If we are not mid-dragging, hide the page outlines if we are on a large screen
699007c69867d821ea2b271398577a8b3440b3a7046Winson Chung            if (LauncherApplication.isScreenLarge()) {
700007c69867d821ea2b271398577a8b3440b3a7046Winson Chung                hideOutlines();
701007c69867d821ea2b271398577a8b3440b3a7046Winson Chung            }
7021afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung
7031afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung            // Hide the scroll indicator as you pan the page
7041afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung            if (!mDragController.isDragging()) {
7051afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung                hideScrollingIndicator(false);
7061afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung            }
7071262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy        }
70868d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        mOverScrollMaxBackgroundAlpha = 0.0f;
709e0f66b546994a9bdee452851c17a148db02ec300Adam Cohen        mOverScrollPageIndex = -1;
71026976d9b53161f80faf822d482ac771b8621cf31Adam Cohen
71126976d9b53161f80faf822d482ac771b8621cf31Adam Cohen        if (mDelayedResizeRunnable != null) {
71226976d9b53161f80faf822d482ac771b8621cf31Adam Cohen            mDelayedResizeRunnable.run();
71326976d9b53161f80faf822d482ac771b8621cf31Adam Cohen            mDelayedResizeRunnable = null;
71426976d9b53161f80faf822d482ac771b8621cf31Adam Cohen        }
7150142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
7160142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
7170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
7180142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void notifyPageSwitchListener() {
7190142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.notifyPageSwitchListener();
7200142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
7210142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    };
7220142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
723c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    // As a ratio of screen height, the total distance we want the parallax effect to span
724c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    // horizontally
725c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    private float wallpaperTravelToScreenWidthRatio(int width, int height) {
726c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        float aspectRatio = width / (float) height;
727c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
728c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
729c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
730c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // We will use these two data points to extrapolate how much the wallpaper parallax effect
731c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // to span (ie travel) at any aspect ratio:
732c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
733c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float ASPECT_RATIO_LANDSCAPE = 16/10f;
734c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float ASPECT_RATIO_PORTRAIT = 10/16f;
735c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
736c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
737c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
738c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // To find out the desired width at different aspect ratios, we use the following two
739c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // formulas, where the coefficient on x is the aspect ratio (width/height):
740c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        //   (16/10)x + y = 1.5
741c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        //   (10/16)x + y = 1.2
742c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // We solve for x and y and end up with a final formula:
743c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float x =
744c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka            (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
745c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka            (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
746c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
747c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        return x * aspectRatio + y;
748c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
749c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
750c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    // The range of scroll values for Workspace
751c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    private int getScrollRange() {
752c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        return getChildOffset(getChildCount() - 1) - getChildOffset(0);
753c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
754c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
755c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    protected void setWallpaperDimension() {
756201bc828d5a0078f505e8e37152156a0cb43c10aWinson Chung        DisplayMetrics displayMetrics = new DisplayMetrics();
757cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mLauncher.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
758201bc828d5a0078f505e8e37152156a0cb43c10aWinson Chung        final int maxDim = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
759201bc828d5a0078f505e8e37152156a0cb43c10aWinson Chung        final int minDim = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
760c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
761c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // We need to ensure that there is enough extra space in the wallpaper for the intended
762c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        // parallax effects
763a64718061a46e3982e35547e675244642bb82860Michael Jurka        if (LauncherApplication.isScreenLarge()) {
764a64718061a46e3982e35547e675244642bb82860Michael Jurka            mWallpaperWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim));
765193080b4c6bfde502aa122b15315745650661af7Michael Jurka            mWallpaperHeight = maxDim;
766a64718061a46e3982e35547e675244642bb82860Michael Jurka        } else {
7671670698ff3502548aa066e32c28ecf62c5592a34Michael Jurka            mWallpaperWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim);
768a64718061a46e3982e35547e675244642bb82860Michael Jurka            mWallpaperHeight = maxDim;
769a64718061a46e3982e35547e675244642bb82860Michael Jurka        }
770f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung        new Thread("setWallpaperDimension") {
771f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            public void run() {
772f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                mWallpaperManager.suggestDesiredDimensions(mWallpaperWidth, mWallpaperHeight);
773f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            }
774f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung        }.start();
775c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
776c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
777ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    public void setVerticalWallpaperOffset(float offset) {
778ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        mWallpaperOffset.setFinalY(offset);
779ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    }
780ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    public float getVerticalWallpaperOffset() {
781ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        return mWallpaperOffset.getCurrY();
782ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    }
783ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    public void setHorizontalWallpaperOffset(float offset) {
784ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        mWallpaperOffset.setFinalX(offset);
785ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    }
786ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    public float getHorizontalWallpaperOffset() {
787ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        return mWallpaperOffset.getCurrX();
788c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
789c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
790ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    private float wallpaperOffsetForCurrentScroll() {
791ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        // The wallpaper travel width is how far, from left to right, the wallpaper will move
792193080b4c6bfde502aa122b15315745650661af7Michael Jurka        // at this orientation. On tablets in portrait mode we don't move all the way to the
793193080b4c6bfde502aa122b15315745650661af7Michael Jurka        // edges of the wallpaper, or otherwise the parallax effect would be too strong.
794193080b4c6bfde502aa122b15315745650661af7Michael Jurka        int wallpaperTravelWidth = mWallpaperWidth;
795193080b4c6bfde502aa122b15315745650661af7Michael Jurka        if (LauncherApplication.isScreenLarge()) {
796193080b4c6bfde502aa122b15315745650661af7Michael Jurka            wallpaperTravelWidth = mWallpaperTravelWidth;
797f70c78142457b0e9c691f2a20ed87407c7433f5bMichael Jurka        }
798ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
799ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        // Set wallpaper offset steps (1 / (number of screens - 1))
800193080b4c6bfde502aa122b15315745650661af7Michael Jurka        mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f);
801ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
802193080b4c6bfde502aa122b15315745650661af7Michael Jurka        // For the purposes of computing the scrollRange and overScrollOffset, we assume
803193080b4c6bfde502aa122b15315745650661af7Michael Jurka        // that mLayoutScale is 1. This means that when we're in spring-loaded mode,
804beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        // there's no discrepancy between the wallpaper offset for a given page.
805beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        float layoutScale = mLayoutScale;
806beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        mLayoutScale = 1f;
80709d6eb0f3fb1d81ea59881e6c65512d09d573cc1Michael Jurka        int scrollRange = getScrollRange();
80809d6eb0f3fb1d81ea59881e6c65512d09d573cc1Michael Jurka
809beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        // Again, we adjust the wallpaper offset to be consistent between values of mLayoutScale
810193080b4c6bfde502aa122b15315745650661af7Michael Jurka        float adjustedScrollX = Math.max(0, Math.min(mScrollX, mMaxScrollX));
811b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        adjustedScrollX *= mWallpaperScrollRatio;
812beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        mLayoutScale = layoutScale;
813beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
814ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float scrollProgress =
815193080b4c6bfde502aa122b15315745650661af7Michael Jurka            adjustedScrollX / (float) scrollRange;
816ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float offsetInDips = wallpaperTravelWidth * scrollProgress +
817f70c78142457b0e9c691f2a20ed87407c7433f5bMichael Jurka            (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it
818ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float offset = offsetInDips / (float) mWallpaperWidth;
819ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        return offset;
820ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    }
821ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    private void syncWallpaperOffsetWithScroll() {
822b46a2d130b3d04df830402d579060b63db559ae3Winson Chung        final boolean enableWallpaperEffects = isHardwareAccelerated();
823b46a2d130b3d04df830402d579060b63db559ae3Winson Chung        if (enableWallpaperEffects) {
824ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mWallpaperOffset.setFinalX(wallpaperOffsetForCurrentScroll());
825c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        }
826c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
827c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
828c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    public void updateWallpaperOffsetImmediately() {
829c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        mUpdateWallpaperOffsetImmediately = true;
830c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
831c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
832ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    private void updateWallpaperOffsets() {
833ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        boolean updateNow = false;
834ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        boolean keepUpdating = true;
835ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        if (mUpdateWallpaperOffsetImmediately) {
836ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            updateNow = true;
837ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            keepUpdating = false;
838ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mWallpaperOffset.jumpToFinal();
839ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mUpdateWallpaperOffsetImmediately = false;
840c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        } else {
841ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            updateNow = keepUpdating = mWallpaperOffset.computeScrollOffset();
842ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
843ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        if (updateNow) {
8449c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka            if (mWindowToken != null) {
8459c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka                mWallpaperManager.setWallpaperOffsets(mWindowToken,
846ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                        mWallpaperOffset.getCurrX(), mWallpaperOffset.getCurrY());
847ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            }
848ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
849ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        if (keepUpdating) {
8507372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka            invalidate();
851956091ba7863bd72086275e61084864994dd6fa7Joe Onorato        }
852ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    }
853ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
854beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    @Override
855beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    protected void updateCurrentPageScroll() {
856beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        super.updateCurrentPageScroll();
8575037a0b02a720ae14cbf673d7d5bb148f5a524c8Adam Cohen        computeWallpaperScrollRatio(mCurrentPage);
858beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    }
859beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
860beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    @Override
861beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    protected void snapToPage(int whichPage) {
862beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        super.snapToPage(whichPage);
8635037a0b02a720ae14cbf673d7d5bb148f5a524c8Adam Cohen        computeWallpaperScrollRatio(whichPage);
864beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    }
865beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
8665037a0b02a720ae14cbf673d7d5bb148f5a524c8Adam Cohen    private void computeWallpaperScrollRatio(int page) {
867beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        // Here, we determine what the desired scroll would be with and without a layout scale,
868beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        // and compute a ratio between the two. This allows us to adjust the wallpaper offset
869beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        // as though there is no layout scale.
870beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        float layoutScale = mLayoutScale;
8715037a0b02a720ae14cbf673d7d5bb148f5a524c8Adam Cohen        int scaled = getChildOffset(page) - getRelativeChildOffset(page);
872beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        mLayoutScale = 1.0f;
8735037a0b02a720ae14cbf673d7d5bb148f5a524c8Adam Cohen        float unscaled = getChildOffset(page) - getRelativeChildOffset(page);
874beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        mLayoutScale = layoutScale;
875beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        if (scaled > 0) {
876beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen            mWallpaperScrollRatio = (1.0f * unscaled) / scaled;
877beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        } else {
878beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen            mWallpaperScrollRatio = 1f;
879beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        }
880beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen    }
881beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
882ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka    class WallpaperOffsetInterpolator {
883ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float mFinalHorizontalWallpaperOffset = 0.0f;
884ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float mFinalVerticalWallpaperOffset = 0.5f;
885ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float mHorizontalWallpaperOffset = 0.0f;
886ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        float mVerticalWallpaperOffset = 0.5f;
887ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        long mLastWallpaperOffsetUpdateTime;
888ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        boolean mIsMovingFast;
889ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        boolean mOverrideHorizontalCatchupConstant;
890ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        float mHorizontalCatchupConstant = 0.35f;
891ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        float mVerticalCatchupConstant = 0.35f;
892ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
893ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public WallpaperOffsetInterpolator() {
894ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
895ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
896ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        public void setOverrideHorizontalCatchupConstant(boolean override) {
897ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            mOverrideHorizontalCatchupConstant = override;
898ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        }
899ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
900ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        public void setHorizontalCatchupConstant(float f) {
901ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            mHorizontalCatchupConstant = f;
902ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        }
903ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
904ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        public void setVerticalCatchupConstant(float f) {
905ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            mVerticalCatchupConstant = f;
906ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka        }
907ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
908ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public boolean computeScrollOffset() {
909ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            if (Float.compare(mHorizontalWallpaperOffset, mFinalHorizontalWallpaperOffset) == 0 &&
910ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                    Float.compare(mVerticalWallpaperOffset, mFinalVerticalWallpaperOffset) == 0) {
911ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                mIsMovingFast = false;
912ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                return false;
913ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            }
914cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen            boolean isLandscape = mDisplayWidth > mDisplayHeight;
915ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
916ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            long currentTime = System.currentTimeMillis();
917ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            long timeSinceLastUpdate = currentTime - mLastWallpaperOffsetUpdateTime;
918ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            timeSinceLastUpdate = Math.min((long) (1000/30f), timeSinceLastUpdate);
919ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            timeSinceLastUpdate = Math.max(1L, timeSinceLastUpdate);
920ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
921ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            float xdiff = Math.abs(mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset);
922ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            if (!mIsMovingFast && xdiff > 0.07) {
923ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                mIsMovingFast = true;
924ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            }
925ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
926ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            float fractionToCatchUpIn1MsHorizontal;
927ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            if (mOverrideHorizontalCatchupConstant) {
928ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                fractionToCatchUpIn1MsHorizontal = mHorizontalCatchupConstant;
929ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            } else if (mIsMovingFast) {
930ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.5f : 0.75f;
931ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            } else {
932ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                // slow
933ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.27f : 0.5f;
934ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            }
935ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            float fractionToCatchUpIn1MsVertical = mVerticalCatchupConstant;
936ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
937ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            fractionToCatchUpIn1MsHorizontal /= 33f;
938ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            fractionToCatchUpIn1MsVertical /= 33f;
939ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
940ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            final float UPDATE_THRESHOLD = 0.00001f;
941ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            float hOffsetDelta = mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset;
942ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            float vOffsetDelta = mFinalVerticalWallpaperOffset - mVerticalWallpaperOffset;
943ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka            boolean jumpToFinalValue = Math.abs(hOffsetDelta) < UPDATE_THRESHOLD &&
944ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                Math.abs(vOffsetDelta) < UPDATE_THRESHOLD;
945a64718061a46e3982e35547e675244642bb82860Michael Jurka
946a64718061a46e3982e35547e675244642bb82860Michael Jurka            // Don't have any lag between workspace and wallpaper on non-large devices
947a64718061a46e3982e35547e675244642bb82860Michael Jurka            if (!LauncherApplication.isScreenLarge() || jumpToFinalValue) {
948ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset;
949ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset;
950ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            } else {
951ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                float percentToCatchUpVertical =
952ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                    Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsVertical);
953ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                float percentToCatchUpHorizontal =
954ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka                    Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsHorizontal);
955ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                mHorizontalWallpaperOffset += percentToCatchUpHorizontal * hOffsetDelta;
956ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka                mVerticalWallpaperOffset += percentToCatchUpVertical * vOffsetDelta;
957ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            }
958ca5b836e16d320f17057cd1a40a93c382e72501aMichael Jurka
959ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mLastWallpaperOffsetUpdateTime = System.currentTimeMillis();
960ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            return true;
961ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
962ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
963ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public float getCurrX() {
964ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            return mHorizontalWallpaperOffset;
965ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
966ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
967ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public float getFinalX() {
968ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            return mFinalHorizontalWallpaperOffset;
969ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
970ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
971ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public float getCurrY() {
972ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            return mVerticalWallpaperOffset;
973ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
974ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
975ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public float getFinalY() {
976ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            return mFinalVerticalWallpaperOffset;
977ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
978ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
979ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public void setFinalX(float x) {
980ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mFinalHorizontalWallpaperOffset = Math.max(0f, Math.min(x, 1.0f));
981ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
982ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
983ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public void setFinalY(float y) {
984ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mFinalVerticalWallpaperOffset = Math.max(0f, Math.min(y, 1.0f));
985ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
986ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka
987ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        public void jumpToFinal() {
988ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset;
989ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka            mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset;
990ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        }
9918f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn    }
992aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
993340c5f3a708efa280a120708594247952ca65182Michael Jurka    @Override
994340c5f3a708efa280a120708594247952ca65182Michael Jurka    public void computeScroll() {
995340c5f3a708efa280a120708594247952ca65182Michael Jurka        super.computeScroll();
996beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen        syncWallpaperOffsetWithScroll();
997340c5f3a708efa280a120708594247952ca65182Michael Jurka    }
998340c5f3a708efa280a120708594247952ca65182Michael Jurka
99994f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    void showOutlines() {
1000d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
10019171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
10029171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
10039171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeInAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 1.0f);
10049171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION);
10059171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeInAnimation.start();
10063e7c7634531302271270c8cf418abc959d621cbcMichael Jurka        }
1007f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
1008f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
100994f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    void hideOutlines() {
1010d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
10119171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
10129171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
10139171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeOutAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 0.0f);
10149171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION);
10159171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY);
10169171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung            mChildrenOutlineFadeOutAnimation.start();
10173e7c7634531302271270c8cf418abc959d621cbcMichael Jurka        }
1018f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
1019f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
102094f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    public void showOutlinesTemporarily() {
102194f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        if (!mIsPageMoving && !isTouchActive()) {
102294f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            snapToPage(mCurrentPage);
102394f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        }
102494f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    }
102594f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy
10269171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    public void setChildrenOutlineAlpha(float alpha) {
10279171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung        mChildrenOutlineAlpha = alpha;
1028f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        for (int i = 0; i < getChildCount(); i++) {
1029f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            CellLayout cl = (CellLayout) getChildAt(i);
1030f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cl.setBackgroundAlpha(alpha);
1031f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
1032f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
1033f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
10349171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    public float getChildrenOutlineAlpha() {
10359171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung        return mChildrenOutlineAlpha;
10369171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    }
10379171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
10389c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    void disableBackground() {
10399c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        mDrawBackground = false;
10409c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    }
10419c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    void enableBackground() {
10429c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        mDrawBackground = true;
10439c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    }
10449c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka
10457bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka    private void animateBackgroundGradient(float finalAlpha, boolean animated) {
1046b4b7fa7c0383b6af58d73d3e024b91d586148cf8Winson Chung        if (mBackground == null) return;
1047b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka        if (mBackgroundFadeInAnimation != null) {
1048b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka            mBackgroundFadeInAnimation.cancel();
1049b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka            mBackgroundFadeInAnimation = null;
1050b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka        }
1051b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka        if (mBackgroundFadeOutAnimation != null) {
1052b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka            mBackgroundFadeOutAnimation.cancel();
1053b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka            mBackgroundFadeOutAnimation = null;
1054b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka        }
10557bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka        float startAlpha = getBackgroundAlpha();
10567bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka        if (finalAlpha != startAlpha) {
10577bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            if (animated) {
10587bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                mBackgroundFadeOutAnimation = ValueAnimator.ofFloat(startAlpha, finalAlpha);
10597bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() {
10607bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    public void onAnimationUpdate(ValueAnimator animation) {
10617bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                        setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue());
10627bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    }
10637bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                });
10647bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f));
10657bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION);
10667bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                mBackgroundFadeOutAnimation.start();
10677bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            } else {
10687bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                setBackgroundAlpha(finalAlpha);
10697bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            }
1070b9e14974ae5c03bd996bc102ed23a24126449990Michael Jurka        }
10719171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    }
10729171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
10739171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    public void setBackgroundAlpha(float alpha) {
10749c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        if (alpha != mBackgroundAlpha) {
10759c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka            mBackgroundAlpha = alpha;
10769c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka            invalidate();
10779c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        }
10789171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    }
10799171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
1080f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    public float getBackgroundAlpha() {
1081f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        return mBackgroundAlpha;
1082f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
1083f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
10846b879f0a5885274a85333531e091283405d490ccAdam Lesinski    /**
10856b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * Due to 3D transformations, if two CellLayouts are theoretically touching each other,
10866b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * on the xy plane, when one is rotated along the y-axis, the gap between them is perceived
10876b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * as being larger. This method computes what offset the rotated view should be translated
10886b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * in order to minimize this perceived gap.
10896b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * @param degrees Angle of the view
10906b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * @param width Width of the view
10916b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * @param height Height of the view
10926b879f0a5885274a85333531e091283405d490ccAdam Lesinski     * @return Offset to be used in a View.setTranslationX() call
10936b879f0a5885274a85333531e091283405d490ccAdam Lesinski     */
10946b879f0a5885274a85333531e091283405d490ccAdam Lesinski    private float getOffsetXForRotation(float degrees, int width, int height) {
10956b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mMatrix.reset();
10966b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mCamera.save();
10976b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mCamera.rotateY(Math.abs(degrees));
10986b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mCamera.getMatrix(mMatrix);
10996b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mCamera.restore();
11006b879f0a5885274a85333531e091283405d490ccAdam Lesinski
11016b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mMatrix.preTranslate(-width * 0.5f, -height * 0.5f);
11026b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mMatrix.postTranslate(width * 0.5f, height * 0.5f);
11036b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mTempFloat2[0] = width;
11046b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mTempFloat2[1] = height;
11056b879f0a5885274a85333531e091283405d490ccAdam Lesinski        mMatrix.mapPoints(mTempFloat2);
11066b879f0a5885274a85333531e091283405d490ccAdam Lesinski        return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f);
11076b879f0a5885274a85333531e091283405d490ccAdam Lesinski    }
11086b879f0a5885274a85333531e091283405d490ccAdam Lesinski
110968d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    float backgroundAlphaInterpolator(float r) {
111068d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        float pivotA = 0.1f;
111168d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        float pivotB = 0.4f;
111268d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        if (r < pivotA) {
111368d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen            return 0;
111468d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        } else if (r > pivotB) {
111568d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen            return 1.0f;
111668d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        } else {
111768d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen            return (r - pivotA)/(pivotB - pivotA);
111868d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        }
111968d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    }
112068d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen
112168d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    float overScrollBackgroundAlphaInterpolator(float r) {
1122e0f66b546994a9bdee452851c17a148db02ec300Adam Cohen        float threshold = 0.08f;
112368d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen
112468d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        if (r > mOverScrollMaxBackgroundAlpha) {
112568d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen            mOverScrollMaxBackgroundAlpha = r;
112668d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        } else if (r < mOverScrollMaxBackgroundAlpha) {
112768d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen            r = mOverScrollMaxBackgroundAlpha;
112868d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        }
112968d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen
113068d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen        return Math.min(r / threshold, 1.0f);
113168d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen    }
113268d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen
1133b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    private void screenScrolledLargeUI(int screenCenter) {
113470ed13b370e390010ee57eed5983fb86da061ed9Adam Cohen        if (isSwitchingState()) return;
1135ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        boolean isInOverscroll = false;
113621f12b52a54d2658ed14fcf7bb1ca17a198f62beAdam Cohen        for (int i = 0; i < getChildCount(); i++) {
11371b0aaac0b3abd777ed319341f95a8dfff23c79f4Adam Cohen            CellLayout cl = (CellLayout) getChildAt(i);
11381b0aaac0b3abd777ed319341f95a8dfff23c79f4Adam Cohen            if (cl != null) {
1139b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen                float scrollProgress = getScrollProgress(screenCenter, cl, i);
114021f12b52a54d2658ed14fcf7bb1ca17a198f62beAdam Cohen                float rotation = WORKSPACE_ROTATION * scrollProgress;
11416b879f0a5885274a85333531e091283405d490ccAdam Lesinski                float translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight());
114268d739365bf650fe7fecf99cd3bfe63a0d41bd12Adam Cohen
1143b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen                // If the current page (i) is being over scrolled, we use a different
1144b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen                // set of rules for setting the background alpha multiplier.
1145cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                if (!isSmall()) {
1146ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                    if ((mOverScrollX < 0 && i == 0) || (mOverScrollX > mMaxScrollX &&
1147ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                            i == getChildCount() -1)) {
1148ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        isInOverscroll = true;
1149ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        rotation *= -1;
1150cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                        cl.setBackgroundAlphaMultiplier(
1151cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                                overScrollBackgroundAlphaInterpolator(Math.abs(scrollProgress)));
1152cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                        mOverScrollPageIndex = i;
1153ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        cl.setOverScrollAmount(Math.abs(scrollProgress), i == 0);
1154ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        cl.setPivotX(cl.getMeasuredWidth() * (i == 0 ? 0.75f : 0.25f));
1155ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        cl.setPivotY(cl.getMeasuredHeight() * 0.5f);
1156ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                        cl.setOverscrollTransformsDirty(true);
1157cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                    } else if (mOverScrollPageIndex != i) {
1158cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                        cl.setBackgroundAlphaMultiplier(
1159cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                                backgroundAlphaInterpolator(Math.abs(scrollProgress)));
1160cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                    }
1161b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen                }
11627372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                cl.setTranslationX(translationX);
11637372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                cl.setRotationY(rotation);
116473894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen                if (mFadeInAdjacentScreens && !isSmall()) {
116573894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen                    float alpha = 1 - Math.abs(scrollProgress);
11667372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setAlpha(alpha);
116773894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen                }
1168f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
1169f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
1170ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        if (!isSwitchingState() && !isInOverscroll) {
1171ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen            ((CellLayout) getChildAt(0)).resetOverscrollTransforms();
1172ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen            ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms();
1173ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        }
117473894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen        invalidate();
1175f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
1176f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
1177b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    private void screenScrolledStandardUI(int screenCenter) {
1178ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        if (mOverScrollX < 0 || mOverScrollX > mMaxScrollX) {
1179ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen            int index = mOverScrollX < 0 ? 0 : getChildCount() - 1;
1180b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            CellLayout cl = (CellLayout) getChildAt(index);
1181b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            float scrollProgress = getScrollProgress(screenCenter, cl, index);
1182b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            cl.setOverScrollAmount(Math.abs(scrollProgress), index == 0);
1183b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            float rotation = - WORKSPACE_OVERSCROLL_ROTATION * scrollProgress;
1184cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen            cl.setCameraDistance(mDensity * CAMERA_DISTANCE);
1185b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            cl.setPivotX(cl.getMeasuredWidth() * (index == 0 ? 0.75f : 0.25f));
1186ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen            cl.setPivotY(cl.getMeasuredHeight() * 0.5f);
1187b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            cl.setRotationY(rotation);
1188ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen            cl.setOverscrollTransformsDirty(true);
118921b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen            setFadeForOverScroll(Math.abs(scrollProgress));
1190b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        } else {
119121b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen            if (mOverscrollFade != 0) {
119221b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen                setFadeForOverScroll(0);
119321b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen            }
11947842d7fc78eebfb2b7cbad23adf11f7b6d923f79Adam Cohen            // We don't want to mess with the translations during transitions
11957842d7fc78eebfb2b7cbad23adf11f7b6d923f79Adam Cohen            if (!isSwitchingState()) {
1196ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                ((CellLayout) getChildAt(0)).resetOverscrollTransforms();
1197ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen                ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms();
11987842d7fc78eebfb2b7cbad23adf11f7b6d923f79Adam Cohen            }
1199b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        }
1200b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    }
1201b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen
1202b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    @Override
1203b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    protected void screenScrolled(int screenCenter) {
1204b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        if (LauncherApplication.isScreenLarge()) {
120573894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen            // We don't call super.screenScrolled() here because we handle the adjacent pages alpha
120673894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen            // ourselves (for efficiency), and there are no scrolling indicators to update.
1207b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            screenScrolledLargeUI(screenCenter);
1208b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        } else {
120973894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen            super.screenScrolled(screenCenter);
1210b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            screenScrolledStandardUI(screenCenter);
1211b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        }
1212b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    }
1213b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen
1214b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    @Override
1215b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    protected void overScroll(float amount) {
1216b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        if (LauncherApplication.isScreenLarge()) {
1217b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            dampedOverScroll(amount);
1218b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        } else {
1219b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen            acceleratedOverScroll(amount);
1220b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen        }
1221b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    }
1222b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen
122300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onAttachedToWindow() {
122400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        super.onAttachedToWindow();
12259c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        mWindowToken = getWindowToken();
1226956091ba7863bd72086275e61084864994dd6fa7Joe Onorato        computeScroll();
12279c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        mDragController.setWindowToken(mWindowToken);
12289c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    }
12299c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka
12309c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    protected void onDetachedFromWindow() {
12319c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        mWindowToken = null;
123200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
123300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
123431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
123528750fba6a2d141eb9a1e566718c17236030b815Michael Jurka    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1236c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
1237c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka            mUpdateWallpaperOffsetImmediately = true;
1238c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        }
12390142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.onLayout(changed, left, top, right, bottom);
124031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
124131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
124231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
12439171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    protected void onDraw(Canvas canvas) {
1244ab1983f46ce0ee0c94ea1170d21a67eb4e3c7967Michael Jurka        updateWallpaperOffsets();
1245c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
12469171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung        // Draw the background gradient if necessary
12479c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka        if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) {
12488d006d5e144ae33e509ea42e0b9760ef6c5bc884Winson Chung            int alpha = (int) (mBackgroundAlpha * 255);
1249f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            mBackground.setAlpha(alpha);
1250f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            mBackground.setBounds(mScrollX, 0, mScrollX + getMeasuredWidth(),
1251f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                    getMeasuredHeight());
1252f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            mBackground.draw(canvas);
12539171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung        }
1254073a46ff635d089cbcb15d939d1de42dd2b86c97Adam Cohen
12559171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung        super.onDraw(canvas);
12569171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung    }
12579171e6d8a2b7b5aa136617b9779a8bbadc5259f7Winson Chung
125821b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen    boolean isDrawingBackgroundGradient() {
125921b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground);
126021b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen    }
126121b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen
1262dfab7f0285eea71abd7fa64e893541fdf831c73bMichael Jurka    @Override
1263dfab7f0285eea71abd7fa64e893541fdf831c73bMichael Jurka    protected void dispatchDraw(Canvas canvas) {
1264557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        super.dispatchDraw(canvas);
1265557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung
1266557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        if (mInScrollArea && !LauncherApplication.isScreenLarge()) {
1267557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            final int width = getWidth();
1268557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            final int height = getHeight();
1269557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            final int pageHeight = getChildAt(0).getHeight();
1270557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung
1271dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung            // Set the height of the outline to be the height of the page
1272dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung            final int offset = (height - pageHeight - mPaddingTop - mPaddingBottom) / 2;
1273dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung            final int paddingTop = mPaddingTop + offset;
1274dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung            final int paddingBottom = mPaddingBottom + offset;
1275557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung
1276aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            final int page = (mNextPage != INVALID_PAGE ? mNextPage : mCurrentPage);
1277aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            final CellLayout leftPage = (CellLayout) getChildAt(page - 1);
1278aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            final CellLayout rightPage = (CellLayout) getChildAt(page + 1);
1279557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung
1280557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            if (leftPage != null && leftPage.getIsDragOverlapping()) {
1281557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                final Drawable d = getResources().getDrawable(R.drawable.page_hover_left_holo);
1282dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung                d.setBounds(mScrollX, paddingTop, mScrollX + d.getIntrinsicWidth(),
1283dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung                        height - paddingBottom);
1284557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                d.draw(canvas);
1285557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            } else if (rightPage != null && rightPage.getIsDragOverlapping()) {
1286557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                final Drawable d = getResources().getDrawable(R.drawable.page_hover_right_holo);
1287dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung                d.setBounds(mScrollX + width - d.getIntrinsicWidth(), paddingTop, mScrollX + width,
1288dea74b7d12b0fcd50bfdb4274f9867ba76d75238Winson Chung                        height - paddingBottom);
1289557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                d.draw(canvas);
12900207c522472d7252f146f4d39efa456ca5248c6bPatrick Dubroy            }
129131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
129231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
129331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
129431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
129531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
1296678862186af4ea70d9b4012c9bc127e7fea5e607Joe Onorato        if (!mLauncher.isAllAppsVisible()) {
129731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
129831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (openFolder != null) {
129931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                return openFolder.requestFocus(direction, previouslyFocusedRect);
130031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            } else {
13010142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
130231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
130331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
130431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return false;
130531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
130631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
130731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
130897d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    public int getDescendantFocusability() {
1309d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isSmall()) {
131097d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung            return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
131197d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        }
131297d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        return super.getDescendantFocusability();
131397d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    }
131497d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
131597d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    @Override
1316c2e24c0a37ddeab930f731c062705d435e477f75Romain Guy    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
1317678862186af4ea70d9b4012c9bc127e7fea5e607Joe Onorato        if (!mLauncher.isAllAppsVisible()) {
131831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
13190142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            if (openFolder != null) {
132031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                openFolder.addFocusables(views, direction);
13210142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            } else {
13220142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                super.addFocusables(views, direction, focusableMode);
132331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
132431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
132531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
132631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1327d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    public boolean isSmall() {
13283384c7be3cf735f68a90d835627350fa48b94d48Adam Cohen        return mState == State.SMALL || mState == State.SPRING_LOADED;
1329d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
1330d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
13310142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    void enableChildrenCache(int fromPage, int toPage) {
13320142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (fromPage > toPage) {
13330142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            final int temp = fromPage;
13340142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            fromPage = toPage;
13350142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            toPage = temp;
13363a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron        }
1337aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1338dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
1339fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
13400142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        fromPage = Math.max(fromPage, 0);
13410142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        toPage = Math.min(toPage, screenCount - 1);
1342fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
13430142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        for (int i = fromPage; i <= toPage; i++) {
134431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
134531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(true);
134631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawingCacheEnabled(true);
134731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
134831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
134931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
135031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void clearChildrenCache() {
1351dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
1352dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
135331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
135431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(false);
13558182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            // In software mode, we don't want the items to continue to be drawn into bitmaps
13568182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            if (!isHardwareAccelerated()) {
13578182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen                layout.setChildrenDrawingCacheEnabled(false);
13588182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            }
135931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
136031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
136131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1362d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    private void updateChildrenLayersEnabled() {
13637777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        boolean small = isSmall() || mIsSwitchingState;
1364d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        boolean dragging = mAnimatingViewIntoPlace || mIsDragOccuring;
1365d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        boolean enableChildrenLayers = small || dragging || isPageMoving();
1366d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
1367d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (enableChildrenLayers != mChildrenLayersEnabled) {
1368d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            mChildrenLayersEnabled = enableChildrenLayers;
136980c69853d64b73e5d62ed33bea1b10680557ee3eMichael Jurka            for (int i = 0; i < getPageCount(); i++) {
137080c69853d64b73e5d62ed33bea1b10680557ee3eMichael Jurka                ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(mChildrenLayersEnabled);
1371d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            }
1372ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka        }
1373ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka    }
1374ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka
13751d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown    protected void onWallpaperTap(MotionEvent ev) {
13761d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        final int[] position = mTempCell;
13771d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        getLocationOnScreen(position);
13781d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
13791d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        int pointerIndex = ev.getActionIndex();
13801d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        position[0] += (int) ev.getX(pointerIndex);
13811d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        position[1] += (int) ev.getY(pointerIndex);
13821d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
13831d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        mWallpaperManager.sendWallpaperCommand(getWindowToken(),
13841d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                ev.getAction() == MotionEvent.ACTION_UP
13851d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                        ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP,
13861d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                position[0], position[1], 0, null);
13871d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown    }
13881d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
138961033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    /*
139061033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * This interpolator emulates the rate at which the perceived scale of an object changes
139161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * as its distance from a camera increases. When this interpolator is applied to a scale
139261033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * animation on a view, it evokes the sense that the object is shrinking due to moving away
139361033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * from the camera.
139461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     */
139561033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    static class ZInterpolator implements TimeInterpolator {
139661033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        private float focalLength;
139761033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
139861033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        public ZInterpolator(float foc) {
139961033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen            focalLength = foc;
140061033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        }
140161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
1402eed565d5126cb40b2a7ecdf5f6e8ac7a7464007bAdam Cohen        public float getInterpolation(float input) {
1403eed565d5126cb40b2a7ecdf5f6e8ac7a7464007bAdam Cohen            return (1.0f - focalLength / (focalLength + input)) /
140461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen                (1.0f - focalLength / (focalLength + 1.0f));
1405cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen        }
1406cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen    }
1407cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen
140861033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    /*
140961033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * The exact reverse of ZInterpolator.
141061033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     */
141161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    static class InverseZInterpolator implements TimeInterpolator {
141261033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        private ZInterpolator zInterpolator;
141361033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        public InverseZInterpolator(float foc) {
141461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen            zInterpolator = new ZInterpolator(foc);
141561033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        }
1416cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen        public float getInterpolation(float input) {
141761033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen            return 1 - zInterpolator.getInterpolation(1 - input);
1418cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen        }
1419cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen    }
1420cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen
142161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    /*
142261033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * ZInterpolator compounded with an ease-out.
142361033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     */
142461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    static class ZoomOutInterpolator implements TimeInterpolator {
1425b64d36e6f8006b3ea5a6c2951af4cf8dea72d08eAdam Cohen        private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f);
1426b64d36e6f8006b3ea5a6c2951af4cf8dea72d08eAdam Cohen        private final ZInterpolator zInterpolator = new ZInterpolator(0.13f);
142761033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
142861033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        public float getInterpolation(float input) {
142961033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen            return decelerate.getInterpolation(zInterpolator.getInterpolation(input));
143061033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        }
143161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    }
143261033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
143361033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    /*
143461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     * InvereZInterpolator compounded with an ease-out.
143561033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen     */
143661033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    static class ZoomInInterpolator implements TimeInterpolator {
143761033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f);
143861033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f);
143961033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
144061033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        public float getInterpolation(float input) {
144161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen            return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input));
144261033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen        }
144361033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    }
144461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
144561033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator();
14463e7c7634531302271270c8cf418abc959d621cbcMichael Jurka
14473e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    /*
14486639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
14496639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we
14506639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
14516639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
14526639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * These methods mark the appropriate pages as accepting drops (which alters their visual
14536639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * appearance).
14546639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
14556639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    */
14566639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    public void onDragStartedWithItem(View v) {
14576639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen        final Canvas canvas = new Canvas();
14586639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen
14596639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen        // We need to add extra padding to the bitmap to make room for the glow effect
14606639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen        final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
14616639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen
14626639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen        // The outline is used to visualize where the item will land if dropped
14636639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen        mDragOutline = createDragOutline(v, canvas, bitmapPadding);
14646639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    }
14656639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen
1466038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, Paint alphaClipPaint) {
1467d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        final Canvas canvas = new Canvas();
14683e7c7634531302271270c8cf418abc959d621cbcMichael Jurka
1469d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        // We need to add extra padding to the bitmap to make room for the glow effect
147038b4f7c5eab773ad36048b5ab8713750dcaaf748Michael Jurka        final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
1471d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1472038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        int[] size = estimateItemSize(info.spanX, info.spanY, info, false);
14736639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen
1474d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        // The outline is used to visualize where the item will land if dropped
14758db7000140d3c35be92a4e07cf54ea6ec4ae3030Peter Ng        mDragOutline = createDragOutline(b, canvas, bitmapPadding, size[0], size[1], alphaClipPaint);
14763e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    }
14773e7c7634531302271270c8cf418abc959d621cbcMichael Jurka
14783e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was
14793e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    // never dragged over
14807bccb42f88cbbaf8cf180a876e8c5d5e306ce62aPatrick Dubroy    public void onDragStopped(boolean success) {
14817bccb42f88cbbaf8cf180a876e8c5d5e306ce62aPatrick Dubroy        // In the success case, DragController has already called onDragExit()
14827bccb42f88cbbaf8cf180a876e8c5d5e306ce62aPatrick Dubroy        if (!success) {
1483c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            doDragExit(null);
14847bccb42f88cbbaf8cf180a876e8c5d5e306ce62aPatrick Dubroy        }
14853e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    }
14863e7c7634531302271270c8cf418abc959d621cbcMichael Jurka
1487758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy    public void exitWidgetResizeMode() {
1488716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        DragLayer dragLayer = mLauncher.getDragLayer();
14896788269814a678af976dfc7d6135993e172ce586Adam Cohen        dragLayer.clearAllResizeFrames();
1490758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy    }
1491758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy
14924b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    private void initAnimationArrays() {
14934b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        final int childCount = getChildCount();
14944b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        if (mOldTranslationXs != null) return;
14954b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldTranslationXs = new float[childCount];
14964b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldTranslationYs = new float[childCount];
14974b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldScaleXs = new float[childCount];
14984b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldScaleYs = new float[childCount];
14994b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldBackgroundAlphas = new float[childCount];
15004b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldBackgroundAlphaMultipliers = new float[childCount];
15014b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldAlphas = new float[childCount];
15024b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mOldRotationYs = new float[childCount];
15034b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewTranslationXs = new float[childCount];
15044b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewTranslationYs = new float[childCount];
15054b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewScaleXs = new float[childCount];
15064b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewScaleYs = new float[childCount];
15074b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewBackgroundAlphas = new float[childCount];
15084b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewBackgroundAlphaMultipliers = new float[childCount];
15094b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewAlphas = new float[childCount];
15104b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        mNewRotationYs = new float[childCount];
15114b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
15124b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
15132a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka    Animator getChangeStateAnimation(final State state, boolean animated) {
15142a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        return getChangeStateAnimation(state, animated, 0);
15157777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen    }
15167777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
15172a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka    Animator getChangeStateAnimation(final State state, boolean animated, int delay) {
1518bdf78559f223ac11e01e3311edd5a48a80383e1eMichael Jurka        if (mState == state) {
15192a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            return null;
152099633da90012dc732cbc12a96b33e17abcecb464Michael Jurka        }
152199633da90012dc732cbc12a96b33e17abcecb464Michael Jurka
15221b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        // Initialize animation arrays for the first time if necessary
15231b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        initAnimationArrays();
15241b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung
15252a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        AnimatorSet anim = animated ? new AnimatorSet() : null;
15267777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
15277777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        // Stop any scrolling, move to the current page right away
15287777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        setCurrentPage((mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage);
15297777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
15301b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final State oldState = mState;
15311b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final boolean oldStateIsNormal = (oldState == State.NORMAL);
15321b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final boolean oldStateIsSmall = (oldState == State.SMALL);
15337777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        mState = state;
15341b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final boolean stateIsNormal = (state == State.NORMAL);
15351b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final boolean stateIsSpringLoaded = (state == State.SPRING_LOADED);
15361b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        final boolean stateIsSmall = (state == State.SMALL);
15371b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        float finalScaleFactor = 1.0f;
15381b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        float finalBackgroundAlpha = stateIsSpringLoaded ? 1.0f : 0f;
15391b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        float translationX = 0;
15401b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        float translationY = 0;
15417777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        boolean zoomIn = true;
1542beff8c6a13d5b5ad69f5a85ee0150cecd28e6341Adam Cohen
15437777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        if (state != State.NORMAL) {
15441b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            finalScaleFactor = mSpringLoadedShrinkFactor - (stateIsSmall ? 0.1f : 0);
15451b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            if (oldStateIsNormal && stateIsSmall) {
15467777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                zoomIn = false;
15477777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                setLayoutScale(finalScaleFactor);
15487777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                updateChildrenLayersEnabled();
1549d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            } else {
1550cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                finalBackgroundAlpha = 1.0f;
15517777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                setLayoutScale(finalScaleFactor);
15523c4c20fbe682cb4b3ef94f09afe0af09171583f3Michael Jurka            }
15537777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        } else {
15547777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            setLayoutScale(1.0f);
15557777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        }
15566b879f0a5885274a85333531e091283405d490ccAdam Lesinski
15577777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        final int duration = zoomIn ?
15587777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                getResources().getInteger(R.integer.config_workspaceUnshrinkTime) :
15597777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime);
15601b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        for (int i = 0; i < getChildCount(); i++) {
15611b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            final CellLayout cl = (CellLayout) getChildAt(i);
15627777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            float rotation = 0f;
15631b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            float initialAlpha = cl.getAlpha();
15641b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            float finalAlphaMultiplierValue = 1f;
15651b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            float finalAlpha = (!mFadeInAdjacentScreens || stateIsSpringLoaded ||
15661b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                    (i == mCurrentPage)) ? 1f : 0f;
15671b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung
15681b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            // Determine the pages alpha during the state transition
15691b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            if ((oldStateIsSmall && stateIsNormal) ||
15701b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                (oldStateIsNormal && stateIsSmall)) {
15711b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                // To/from workspace - only show the current page unless the transition is not
15721b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                //                     animated and the animation end callback below doesn't run
15731b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                if (i == mCurrentPage || !animated) {
15741b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                    finalAlpha = 1f;
15751b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                    finalAlphaMultiplierValue = 0f;
15761b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                } else {
15771b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                    initialAlpha = 0f;
15781b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                    finalAlpha = 0f;
15791b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                }
15807777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            }
15814afe9b31f51022538b30646fe83780e83bf22490Winson Chung
15821b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            // Update the rotation of the screen (don't apply rotation on Phone UI)
15837777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            if (LauncherApplication.isScreenLarge()) {
15847777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                if (i < mCurrentPage) {
15857777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    rotation = WORKSPACE_ROTATION;
15867777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                } else if (i > mCurrentPage) {
15877777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    rotation = -WORKSPACE_ROTATION;
1588785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                }
15897777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            }
1590f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
15917777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            // If the screen is not xlarge, then don't rotate the CellLayouts
15927777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            // NOTE: If we don't update the side pages alpha, then we should not hide the side
15937777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            //       pages. see unshrink().
15947777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            if (LauncherApplication.isScreenLarge()) {
15957777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight());
15967777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            }
159763257c110a4ee54d5e8872c471cce254cf613c7aWinson Chung
15981b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            mOldAlphas[i] = initialAlpha;
15991b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung            mNewAlphas[i] = finalAlpha;
16007777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            if (animated) {
16017777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldTranslationXs[i] = cl.getTranslationX();
16027777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldTranslationYs[i] = cl.getTranslationY();
16037777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldScaleXs[i] = cl.getScaleX();
16047777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldScaleYs[i] = cl.getScaleY();
16057777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldBackgroundAlphas[i] = cl.getBackgroundAlpha();
16067777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldBackgroundAlphaMultipliers[i] = cl.getBackgroundAlphaMultiplier();
16077777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mOldRotationYs[i] = cl.getRotationY();
1608cbbaf9862aa160c5e397cb49b554de20981a35feAdam Cohen
16097777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewTranslationXs[i] = translationX;
16107777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewTranslationYs[i] = translationY;
16117777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewScaleXs[i] = finalScaleFactor;
16127777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewScaleYs[i] = finalScaleFactor;
16137777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewBackgroundAlphas[i] = finalBackgroundAlpha;
16147777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewBackgroundAlphaMultipliers[i] = finalAlphaMultiplierValue;
16157777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                mNewRotationYs[i] = rotation;
16167777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen            } else {
16177777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setTranslationX(translationX);
16187777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setTranslationY(translationY);
16197777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setScaleX(finalScaleFactor);
16207777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setScaleY(finalScaleFactor);
16211b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                cl.setBackgroundAlpha(finalBackgroundAlpha);
16227777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setBackgroundAlphaMultiplier(finalAlphaMultiplierValue);
16231b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung                cl.setAlpha(finalAlpha);
16247777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                cl.setRotationY(rotation);
1625b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            }
16267777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        }
16271c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung
16287777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        if (animated) {
16297407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            for (int index = 0; index < getChildCount(); index++) {
16307407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                final int i = index;
16317407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                final CellLayout cl = (CellLayout) getChildAt(i);
16322a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                if (mOldAlphas[i] == 0 && mNewAlphas[i] == 0) {
16337372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setTranslationX(mNewTranslationXs[i]);
16347372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setTranslationY(mNewTranslationYs[i]);
16357372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setScaleX(mNewScaleXs[i]);
16367372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setScaleY(mNewScaleYs[i]);
16377372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setBackgroundAlpha(mNewBackgroundAlphas[i]);
16382a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    cl.setBackgroundAlphaMultiplier(mNewBackgroundAlphaMultipliers[i]);
16397372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setAlpha(mNewAlphas[i]);
16407372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                    cl.setRotationY(mNewRotationYs[i]);
16417407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                } else {
16427407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    LauncherViewPropertyAnimator a = new LauncherViewPropertyAnimator(cl);
16437407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    a.translationX(mNewTranslationXs[i])
16447407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        .translationY(mNewTranslationYs[i])
16457407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        .scaleX(mNewScaleXs[i])
16467407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        .scaleY(mNewScaleYs[i])
16477407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        .setDuration(duration)
16487407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        .setInterpolator(mZoomInInterpolator);
16497407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    if (mOldAlphas[i] != mNewAlphas[i]) {
16507407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        a.alpha(mNewAlphas[i]);
1651742574b15b2b5298a2328443176f2890fb8ebe98Michael Jurka                    }
16527407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    anim.play(a);
16537407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    if (mOldRotationYs[i] != 0 || mNewRotationYs[i] != 0) {
16547407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        ValueAnimator rotate = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
16557407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        rotate.setInterpolator(new DecelerateInterpolator(2.0f));
16567407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        rotate.addUpdateListener(new LauncherAnimatorUpdateListener() {
16577407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                public void onAnimationUpdate(float a, float b) {
1658b33e05fa50a0a386e96acf8dc9b5ccd1ba6834aeWinson Chung                                    cl.setRotationY(a * mOldRotationYs[i] + b * mNewRotationYs[i]);
16597407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                }
16607407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                            });
16617407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        anim.play(rotate);
16627777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    }
16637407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    if (mOldBackgroundAlphas[i] != 0 ||
16647407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        mNewBackgroundAlphas[i] != 0 ||
16657407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        mOldBackgroundAlphaMultipliers[i] != 0 ||
16667407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        mNewBackgroundAlphaMultipliers[i] != 0) {
16677407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        ValueAnimator bgAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
16687407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        bgAnim.setInterpolator(mZoomInInterpolator);
16697407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
16707407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                public void onAnimationUpdate(float a, float b) {
16717372c59277a23a31eeb1513a554b29d7bf05aedbMichael Jurka                                    cl.setBackgroundAlpha(
16727407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                            a * mOldBackgroundAlphas[i] +
16737407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                            b * mNewBackgroundAlphas[i]);
16747407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                    cl.setBackgroundAlphaMultiplier(
16757407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                            a * mOldBackgroundAlphaMultipliers[i] +
16767407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                            b * mNewBackgroundAlphaMultipliers[i]);
16777407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                                }
16787407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                            });
16797407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                        anim.play(bgAnim);
16807777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    }
1681b46a2d130b3d04df830402d579060b63db559ae3Winson Chung                }
16827407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            }
16832a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            anim.setStartDelay(delay);
1684dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        }
16858d006d5e144ae33e509ea42e0b9760ef6c5bc884Winson Chung
16861b7c1d351585cc2ce1c45bce85c9a26a9394753eWinson Chung        if (stateIsSpringLoaded) {
16877bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            // Right now we're covered by Apps Customize
16887bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            // Show the background gradient immediately, so the gradient will
16897bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            // be showing once AppsCustomize disappears
16907bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            animateBackgroundGradient(getResources().getInteger(
16917bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, false);
16927bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka        } else {
16937bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            // Fade the background gradient away
16947bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka            animateBackgroundGradient(0f, true);
16957bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka        }
16962a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        return anim;
1697dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    }
1698dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
16999433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    @Override
17009433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
17019433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        mIsSwitchingState = true;
17029433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    }
17039433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka
17049433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    @Override
17059433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
17069433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        mIsSwitchingState = false;
17079433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        mWallpaperOffset.setOverrideHorizontalCatchupConstant(false);
17089433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        updateChildrenLayersEnabled();
17099433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        // The code in getChangeStateAnimation to determine initialAlpha and finalAlpha will ensure
17109433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        // ensure that only the current page is visible during (and subsequently, after) the
17119433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        // transition animation.  If fade adjacent pages is disabled, then re-enable the page
17129433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        // visibility after the transition animation.
17139433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        if (!mFadeInAdjacentScreens) {
17149433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka            for (int i = 0; i < getChildCount(); i++) {
17159433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka                final CellLayout cl = (CellLayout) getChildAt(i);
17169433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka                cl.setAlpha(1f);
17179433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka            }
17189433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        }
17199433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    }
17209433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka
17219433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    @Override
17229433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    public View getContent() {
17239433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka        return this;
17249433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka    }
17259433fa7ebad74320e39bfac6161a68bc850fe161Michael Jurka
17264be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
17274be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Draw the View v into the given Canvas.
17284be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     *
17294be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param v the view to draw
17304be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param destCanvas the canvas to draw on
17314be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param padding the horizontal and vertical padding to use when drawing
17324be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     */
1733ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
17344be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final Rect clipRect = mTempRect;
17354be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        v.getDrawingRect(clipRect);
17364be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
1737099f60d51f6310e1290687e83958e665b3324504Adam Cohen        boolean textVisible = false;
1738099f60d51f6310e1290687e83958e665b3324504Adam Cohen
1739ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        destCanvas.save();
1740ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        if (v instanceof TextView && pruneToDrawable) {
1741ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            Drawable d = ((TextView) v).getCompoundDrawables()[1];
1742ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
1743ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            destCanvas.translate(padding / 2, padding / 2);
1744ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            d.draw(destCanvas);
1745ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        } else {
1746ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            if (v instanceof FolderIcon) {
1747099f60d51f6310e1290687e83958e665b3324504Adam Cohen                // For FolderIcons the text can bleed into the icon area, and so we need to
1748099f60d51f6310e1290687e83958e665b3324504Adam Cohen                // hide the text completely (which can't be achieved by clipping).
1749099f60d51f6310e1290687e83958e665b3324504Adam Cohen                if (((FolderIcon) v).getTextVisible()) {
1750099f60d51f6310e1290687e83958e665b3324504Adam Cohen                    ((FolderIcon) v).setTextVisible(false);
1751099f60d51f6310e1290687e83958e665b3324504Adam Cohen                    textVisible = true;
1752099f60d51f6310e1290687e83958e665b3324504Adam Cohen                }
1753ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            } else if (v instanceof BubbleTextView) {
1754ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                final BubbleTextView tv = (BubbleTextView) v;
1755ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V +
1756ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                        tv.getLayout().getLineTop(0);
1757ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            } else if (v instanceof TextView) {
1758ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                final TextView tv = (TextView) v;
1759ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() +
1760ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                        tv.getLayout().getLineTop(0);
1761ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            }
1762ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
1763ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            destCanvas.clipRect(clipRect, Op.REPLACE);
1764ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            v.draw(destCanvas);
1765099f60d51f6310e1290687e83958e665b3324504Adam Cohen
1766099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Restore text visibility of FolderIcon if necessary
1767099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (textVisible) {
1768099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) v).setTextVisible(true);
1769099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
17704be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        }
1771ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        destCanvas.restore();
1772ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    }
17734be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
1774ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    /**
1775ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     * Returns a new bitmap to show when the given View is being dragged around.
1776ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     * Responsibility for the bitmap is transferred to the caller.
1777ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     */
1778ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    public Bitmap createDragBitmap(View v, Canvas canvas, int padding) {
1779ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
1780ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        Bitmap b;
17814be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
1782ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        if (v instanceof TextView) {
1783ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            Drawable d = ((TextView) v).getCompoundDrawables()[1];
1784ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            b = Bitmap.createBitmap(d.getIntrinsicWidth() + padding,
1785ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                    d.getIntrinsicHeight() + padding, Bitmap.Config.ARGB_8888);
1786ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        } else {
1787ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            b = Bitmap.createBitmap(
1788ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                    v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
1789ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        }
1790ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
1791ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        canvas.setBitmap(b);
1792ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        drawDragView(v, canvas, padding, true);
1793ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        mOutlineHelper.applyOuterBlur(b, canvas, outlineColor);
1794aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen        canvas.setBitmap(null);
1795ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
1796ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        return b;
17974be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    }
17984be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
17994be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
18004be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
18014be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Responsibility for the bitmap is transferred to the caller.
18024be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     */
18034be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    private Bitmap createDragOutline(View v, Canvas canvas, int padding) {
1804ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
18054be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final Bitmap b = Bitmap.createBitmap(
18064be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato                v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
18074be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18084be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        canvas.setBitmap(b);
1809b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung        drawDragView(v, canvas, padding, true);
18105bb50bdb13f5fc22f5febd9480e78222b26c6fe7Adam Cohen        mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
1811aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen        canvas.setBitmap(null);
18124be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        return b;
18134be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    }
18144be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18154be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
1816d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
1817d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     * Responsibility for the bitmap is transferred to the caller.
1818d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     */
18198db7000140d3c35be92a4e07cf54ea6ec4ae3030Peter Ng    private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h,
18208db7000140d3c35be92a4e07cf54ea6ec4ae3030Peter Ng            Paint alphaClipPaint) {
1821ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
1822f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
1823d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        canvas.setBitmap(b);
1824f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
1825f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight());
1826f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(),
1827f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka                (h - padding) / (float) orig.getHeight());
1828f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        int scaledWidth = (int) (scaleFactor * orig.getWidth());
1829f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        int scaledHeight = (int) (scaleFactor * orig.getHeight());
1830f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);
1831f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
1832f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        // center the image
1833f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2);
1834f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
18351120e0355798391712f1d2e2b205f0bd3724770cWinson Chung        canvas.drawBitmap(orig, src, dst, null);
18368db7000140d3c35be92a4e07cf54ea6ec4ae3030Peter Ng        mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor,
18378db7000140d3c35be92a4e07cf54ea6ec4ae3030Peter Ng                alphaClipPaint);
1838aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen        canvas.setBitmap(null);
1839d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1840d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        return b;
1841d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
1842d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1843d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    /**
1844a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * Creates a drag outline to represent a drop (that we don't have the actual information for
1845a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * yet).  May be changed in the future to alter the drop outline slightly depending on the
1846a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * clip description mime data.
1847a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     */
1848a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    private Bitmap createExternalDragOutline(Canvas canvas, int padding) {
1849a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        Resources r = getResources();
1850ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        final int outlineColor = r.getColor(android.R.color.holo_blue_light);
1851a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int iconWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
1852a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int iconHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
1853a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int rectRadius = r.getDimensionPixelSize(R.dimen.external_drop_icon_rect_radius);
1854a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int inset = (int) (Math.min(iconWidth, iconHeight) * 0.2f);
1855a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final Bitmap b = Bitmap.createBitmap(
1856a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                iconWidth + padding, iconHeight + padding, Bitmap.Config.ARGB_8888);
1857a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
1858a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        canvas.setBitmap(b);
1859a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        canvas.drawRoundRect(new RectF(inset, inset, iconWidth - inset, iconHeight - inset),
1860a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                rectRadius, rectRadius, mExternalDragOutlinePaint);
18615bb50bdb13f5fc22f5febd9480e78222b26c6fe7Adam Cohen        mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
1862aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen        canvas.setBitmap(null);
1863a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        return b;
1864a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
1865a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
186631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void startDrag(CellLayout.CellInfo cellInfo) {
186731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        View child = cellInfo.cell;
1868aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
186931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Make sure the drag was started by a long press as opposed to a long click.
18707984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert        if (!child.isInTouchMode()) {
187131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return;
187231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1873aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
187431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = cellInfo;
1875fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen        child.setVisibility(GONE);
18764be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18774be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        child.clearFocus();
18784be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        child.setPressed(false);
18794be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18804be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final Canvas canvas = new Canvas();
18814be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18828e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy        // We need to add extra padding to the bitmap to make room for the glow effect
188338b4f7c5eab773ad36048b5ab8713750dcaaf748Michael Jurka        final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
18848e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy
18854be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        // The outline is used to visualize where the item will land if dropped
18868e58e916061cbe2623697efac0924f2aa3753a92Patrick Dubroy        mDragOutline = createDragOutline(child, canvas, bitmapPadding);
1887ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        beginDragShared(child, this);
1888ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    }
1889ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
1890ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    public void beginDragShared(View child, DragSource source) {
1891b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung        Resources r = getResources();
1892b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung
1893ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        // We need to add extra padding to the bitmap to make room for the glow effect
1894ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
18954be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18964be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        // The drag bitmap follows the touch point around on the screen
1897ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        final Bitmap b = createDragBitmap(child, new Canvas(), bitmapPadding);
18984be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
18994be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final int bmpWidth = b.getWidth();
1900e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
19018dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
19028dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        final int dragLayerX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2;
1903fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen        int dragLayerY = mTempXY[1] - bitmapPadding / 2;
1904e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
1905b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung        Point dragVisualizeOffset = null;
1906e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        Rect dragRect = null;
1907b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung        if (child instanceof BubbleTextView || child instanceof PagedViewIcon) {
1908b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            int iconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
1909b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            int iconPaddingTop = r.getDimensionPixelSize(R.dimen.app_icon_padding_top);
1910e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int top = child.getPaddingTop();
1911e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int left = (bmpWidth - iconSize) / 2;
1912e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int right = left + iconSize;
1913e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int bottom = top + iconSize;
1914fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            dragLayerY += top;
1915b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            // Note: The drag region is used to calculate drag layer offsets, but the
1916b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
1917b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            dragVisualizeOffset = new Point(-bitmapPadding / 2, iconPaddingTop - bitmapPadding / 2);
1918e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            dragRect = new Rect(left, top, right, bottom);
19190e4857c302da6adbd45231d7a8bae0289fa33250Adam Cohen        } else if (child instanceof FolderIcon) {
1920b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            int previewSize = r.getDimensionPixelSize(R.dimen.folder_preview_size);
19210e4857c302da6adbd45231d7a8bae0289fa33250Adam Cohen            dragRect = new Rect(0, 0, child.getWidth(), previewSize);
1922e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        }
1923e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
19241e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        // Clear the pressed state if necessary
19251e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        if (child instanceof BubbleTextView) {
19261e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung            BubbleTextView icon = (BubbleTextView) child;
19271e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung            icon.clearPressedOrFocusedBackground();
19281e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        }
19291e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung
1930ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
1931b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung                DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect);
19324be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        b.recycle();
19331afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung
19341afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        // Show the scrolling indicator when you pick up an item
19351afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        showScrollingIndicator(false);
193631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
193731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, int screen,
19393d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) {
19403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info);
194131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19420280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final int[] cellXY = new int[2];
19433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
19443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
19453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, cellXY[0],
19463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                cellXY[1]);
194731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
194831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19494b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public boolean transitionStateShouldAllowDrop() {
19504b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        return (!isSwitchingState() || mTransitionProgress > 0.5f);
19514b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
19524b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
1953cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy    /**
1954cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy     * {@inheritDoc}
1955cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy     */
1956cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public boolean acceptDrop(DragObject d) {
1957cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        // If it's an external drop (e.g. from All Apps), check if it should be accepted
1958cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen        if (d.dragSource != this) {
1959cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            // Don't accept the drop if we're not over a screen at time of drop
1960a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            if (mDragTargetLayout == null) {
1961cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                return false;
1962cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            }
19634b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            if (!transitionStateShouldAllowDrop()) return false;
1964cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
1965a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
1966a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                    d.dragView, mDragViewVisualCenter);
1967a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
19682e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            // We want the point to be mapped to the dragTarget.
19692e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
19702e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen                mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
19712e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            } else {
19722e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen                mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
19732e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            }
19742e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen
1975557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            int spanX = 1;
1976557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            int spanY = 1;
1977557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            View ignoreView = null;
1978557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            if (mDragInfo != null) {
1979557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                final CellLayout.CellInfo dragCellInfo = mDragInfo;
1980557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanX = dragCellInfo.spanX;
1981557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanY = dragCellInfo.spanY;
1982557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                ignoreView = dragCellInfo.cell;
1983557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            } else {
1984557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                final ItemInfo dragInfo = (ItemInfo) d.dragInfo;
1985557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanX = dragInfo.spanX;
1986557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanY = dragInfo.spanY;
1987557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
1988cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
1989a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
1990a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                    (int) mDragViewVisualCenter[1], spanX, spanY, mDragTargetLayout, mTargetCell);
1991a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell, true)) {
1992a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
1993a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
1994a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            if (willAddToExistingUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout,
1995a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                    mTargetCell)) {
1996a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
1997a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
1998a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
1999cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            // Don't accept the drop if there's no room for the item
2000cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            if (!mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) {
200196ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                // Don't show the message if we are dropping on the AllApps button and the hotseat
200296ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                // is full
200396ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                if (mTargetCell != null && mLauncher.isHotseatLayout(mDragTargetLayout)) {
200496ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                    Hotseat hotseat = mLauncher.getHotseat();
200596ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                    if (Hotseat.isAllAppsButtonRank(
200696ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                            hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) {
200796ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                        return false;
200896ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                    }
200996ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                }
201096ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung
2011cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                mLauncher.showOutOfSpaceMessage();
2012cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                return false;
2013cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            }
2014cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        }
2015cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        return true;
2016cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy    }
2017cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
2018a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell,
2019a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            boolean considerTimeout) {
2020a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2021a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
20223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        boolean hasntMoved = false;
20233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mDragInfo != null) {
20243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
20253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            hasntMoved = (mDragInfo.cellX == targetCell[0] &&
20263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    mDragInfo.cellY == targetCell[1]) && (cellParent == target);
20273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
2028e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2029a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) {
2030a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            return false;
2031a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        }
2032e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2033a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo);
2034e0310965022e7a1adb7ad489505d404186608689Adam Cohen        boolean willBecomeShortcut =
2035c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
2036c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT);
2037e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2038e0310965022e7a1adb7ad489505d404186608689Adam Cohen        return (aboveShortcut && willBecomeShortcut);
2039e0310965022e7a1adb7ad489505d404186608689Adam Cohen    }
2040e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2041a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell) {
2042a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2043a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        if (dropOverView instanceof FolderIcon) {
2044a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            FolderIcon fi = (FolderIcon) dropOverView;
2045a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            if (fi.acceptDrop(dragInfo)) {
2046a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
2047a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
2048a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        }
2049a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        return false;
2050a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    }
2051a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
20523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    boolean createUserFolderIfNecessary(View newView, long container, CellLayout target,
2053557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            int[] targetCell, boolean external, DragView dragView, Runnable postAnimationRunnable) {
2054c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        View v = target.getChildAt(targetCell[0], targetCell[1]);
2055ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        boolean hasntMoved = false;
2056ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        if (mDragInfo != null) {
2057ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung            CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2058ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung            hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2059ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung                    mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2060ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        }
206110b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
206219072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false;
206319072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        mCreateUserFolderOnDrop = false;
2064c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        final int screen = (targetCell == null) ? mDragInfo.screen : indexOfChild(target);
206510b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
206610b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo);
206710b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo);
206810b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
206910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        if (aboveShortcut && willBecomeShortcut) {
207010b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag();
207110b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            ShortcutInfo destInfo = (ShortcutInfo) v.getTag();
207210b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            // if the drag started here, we need to remove it from the workspace
207310b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            if (!external) {
20743d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
207510b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            }
207610b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
2077d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen            Rect folderLocation = new Rect();
2078ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation);
207910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            target.removeView(v);
2080d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen
20813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            FolderIcon fi =
20823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mLauncher.addFolder(target, container, screen, targetCell[0], targetCell[1]);
2083a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            destInfo.cellX = -1;
2084a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            destInfo.cellY = -1;
2085a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            sourceInfo.cellX = -1;
2086a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            sourceInfo.cellY = -1;
2087d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen
2088558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            // If the dragView is null, we can't animate
2089558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            boolean animate = dragView != null;
2090558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (animate) {
2091558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale,
2092558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        postAnimationRunnable);
2093558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            } else {
2094558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.addItem(destInfo);
2095558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.addItem(sourceInfo);
2096558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
209710b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            return true;
209810b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        }
209910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        return false;
210010b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen    }
210110b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
2102a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell,
21033e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            DragObject d, boolean external) {
2104a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2105c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        if (dropOverView instanceof FolderIcon) {
2106c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            FolderIcon fi = (FolderIcon) dropOverView;
21073e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            if (fi.acceptDrop(d.dragInfo)) {
21083e8f811f31a2275e778244427271f77f4116c08bAdam Cohen                fi.onDrop(d);
2109c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
2110c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // if the drag started here, we need to remove it from the workspace
2111c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                if (!external) {
21123d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
2113c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
2114c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                return true;
2115c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            }
2116c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        }
2117c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        return false;
2118c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    }
2119c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
2120cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDrop(DragObject d) {
2121cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen        mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView,
2122e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                mDragViewVisualCenter);
2123e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2124e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // We want the point to be mapped to the dragTarget.
2125ba781611b3e2fb1c42daa7167c1079b90b100efaAdam Cohen        if (mDragTargetLayout != null) {
21263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
21273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
21283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
21293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
21303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
2131ba781611b3e2fb1c42daa7167c1079b90b100efaAdam Cohen        }
2132c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka
213376078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen        CellLayout dropTargetLayout = mDragTargetLayout;
2134b09cac5a738b679038cbeaec80041637100b65d9Michael Jurka
2135ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        int snapScreen = -1;
2136cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen        if (d.dragSource != this) {
2137e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
2138e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                    (int) mDragViewVisualCenter[1] };
21393e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d);
2140ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy        } else if (mDragInfo != null) {
2141ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            final View cell = mDragInfo.cell;
214254fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
2143ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung            if (dropTargetLayout != null) {
2144cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                // Move internally
214540e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
214640e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
214740e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                long container = hasMovedIntoHotseat ?
21483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        LauncherSettings.Favorites.CONTAINER_HOTSEAT :
21493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        LauncherSettings.Favorites.CONTAINER_DESKTOP;
21503d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                int screen = (mTargetCell[0] < 0) ?
215154fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy                        mDragInfo.screen : indexOfChild(dropTargetLayout);
2152c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
2153c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
2154c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // First we find the cell nearest to point at which the item is
2155c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // dropped, without any consideration to whether there is an item there.
2156c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int)
2157c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
2158c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // If the item being dropped is a shortcut and the nearest drop
215976078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen                // cell also contains a shortcut, then create a folder with the two shortcuts.
21601c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung                if (!mInScrollArea && createUserFolderIfNecessary(cell, container,
21613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        dropTargetLayout, mTargetCell, false, d.dragView, null)) {
2162c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
2163c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
2164c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
21653e8f811f31a2275e778244427271f77f4116c08bAdam Cohen                if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, d, false)) {
2166df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                    return;
2167df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                }
2168df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
2169df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                // Aside from the special case where we're dropping a shortcut onto a shortcut,
2170df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                // we need to find the nearest cell location that is vacant
2171e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                mTargetCell = findNearestVacantArea((int) mDragViewVisualCenter[0],
2172e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                        (int) mDragViewVisualCenter[1], mDragInfo.spanX, mDragInfo.spanY, cell,
2173e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                        dropTargetLayout, mTargetCell);
2174df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
21757cfc2825c3a1029f962d2fc387ae2eaa85b51798Michael Jurka                if (mCurrentPage != screen && !hasMovedIntoHotseat) {
2176ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen                    snapScreen = screen;
217754fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy                    snapToPage(screen);
217854fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy                }
217954fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
2180c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                if (mTargetCell[0] >= 0 && mTargetCell[1] >= 0) {
218140e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                    if (hasMovedLayouts) {
21829438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                        // Reparent the view
21833d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        getParentCellLayoutForView(cell).removeView(cell);
21843d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        addInScreen(cell, container, screen, mTargetCell[0], mTargetCell[1],
21853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                                mDragInfo.spanX, mDragInfo.spanY);
21869438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    }
2187cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
21889438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    // update the item's position after drop
21899438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    final ItemInfo info = (ItemInfo) cell.getTag();
21909438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
219154fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy                    dropTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]);
21929438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    lp.cellX = mTargetCell[0];
21939438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    lp.cellY = mTargetCell[1];
21943d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    cell.setId(LauncherModel.getCellLayoutChildId(container, mDragInfo.screen,
21959438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                            mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY));
21969438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy
21973d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
21983d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            cell instanceof LauncherAppWidgetHostView) {
2199d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        final CellLayout cellLayout = dropTargetLayout;
2200d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        // We post this call so that the widget has a chance to be placed
2201d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        // in its final location
2202d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
2203d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
2204d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo();
220527c09b05d3ed2a4ce9d53140216aba998727a569Adam Cohen                        if (pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) {
220626976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                            final Runnable resizeRunnable = new Runnable() {
2207d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                                public void run() {
2208716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen                                    DragLayer dragLayer = mLauncher.getDragLayer();
2209c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                                    dragLayer.addResizeFrame(info, hostView, cellLayout);
2210d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                                }
221126976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                            };
221226976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                            post(new Runnable() {
221326976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                public void run() {
221426976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                    if (!isPageMoving()) {
221526976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                        resizeRunnable.run();
221626976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                    } else {
221726976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                        mDelayedResizeRunnable = resizeRunnable;
221826976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                    }
221926976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                }
2220d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                            });
2221d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        }
2222d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                    }
2223d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
22243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    LauncherModel.moveItemInDatabase(mLauncher, info, container, screen, lp.cellX,
22253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            lp.cellY);
22269438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                }
222731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
2228cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
22298c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            final CellLayout parent = (CellLayout) cell.getParent().getParent();
2230ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy
2231ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            // Prepare it to be animated into its new position
2232ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            // This must be called after the view has been re-parented
2233d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            final Runnable disableHardwareLayersRunnable = new Runnable() {
2234d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                @Override
2235d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                public void run() {
2236d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                    mAnimatingViewIntoPlace = false;
2237d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                    updateChildrenLayersEnabled();
2238d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                }
2239d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            };
2240d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            mAnimatingViewIntoPlace = true;
2241fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            if (d.dragView.hasDrawn()) {
2242ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen                int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
2243ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
22446441de0ec2a71862798dd51180d0811b42edd514Adam Cohen                        disableHardwareLayersRunnable, this);
2245fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            } else {
22467bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung                d.deferDragViewCleanupPostAnimation = false;
2247fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen                cell.setVisibility(VISIBLE);
2248fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            }
2249716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen            parent.onDropChild(cell);
225031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
225131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
225231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2253ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    public void setFinalScrollForPageChange(int screen) {
2254ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        if (screen >= 0) {
2255ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            mSavedScrollX = getScrollX();
2256ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            CellLayout cl = (CellLayout) getChildAt(screen);
2257ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            mSavedTranslationX = cl.getTranslationX();
2258ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            mSavedRotationY = cl.getRotationY();
2259ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            final int newX = getChildOffset(screen) - getRelativeChildOffset(screen);
2260ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            setScrollX(newX);
2261ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            cl.setTranslationX(0f);
2262ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            cl.setRotationY(0f);
2263ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        }
2264ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    }
2265ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
2266ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    public void resetFinalScrollForPageChange(int screen) {
2267ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        if (screen >= 0) {
2268ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            CellLayout cl = (CellLayout) getChildAt(screen);
2269ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            setScrollX(mSavedScrollX);
2270ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            cl.setTranslationX(mSavedTranslationX);
2271ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen            cl.setRotationY(mSavedRotationY);
2272ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        }
2273ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    }
2274ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
227576078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen    public void getViewLocationRelativeToSelf(View v, int[] location) {
22768dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        getLocationInWindow(location);
2277e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int x = location[0];
2278e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int y = location[1];
2279e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
22808dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        v.getLocationInWindow(location);
2281e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int vX = location[0];
2282e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int vY = location[1];
2283e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2284e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        location[0] = vX - x;
2285e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        location[1] = vY - y;
2286e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    }
2287e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2288cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDragEnter(DragObject d) {
22898a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen        mDragHasEnteredWorkspace = true;
2290b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        if (mDragTargetLayout != null) {
2291c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mDragTargetLayout.setIsDragOverlapping(false);
2292b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung            mDragTargetLayout.onDragExit();
2293b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        }
2294c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mDragTargetLayout = getCurrentDropLayout();
2295c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mDragTargetLayout.setIsDragOverlapping(true);
2296c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mDragTargetLayout.onDragEnter();
2297c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2298c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we
2299c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // don't need to show the outlines
2300c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (LauncherApplication.isScreenLarge()) {
2301c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            showOutlines();
2302c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
2303c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
2304b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung
2305c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    private void doDragExit(DragObject d) {
2306c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Clean up folders
2307c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        cleanupFolderCreation(d);
2308fd3385fe9e0f034b04f99d5d59a58d74fe040da4Winson Chung
2309c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Reset the scroll area and previous drag target
2310c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        onResetScrollArea();
2311c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2312c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mDragTargetLayout != null) {
2313c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mDragTargetLayout.setIsDragOverlapping(false);
2314c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mDragTargetLayout.onDragExit();
2315d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka        }
2316c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mLastDragOverView = null;
231774c28d1a6cf13fdfa55ff9518d2b27be10d01f6eAdam Cohen        mSpringLoadedDragController.cancel();
2318c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2319c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (!mIsPageMoving) {
2320c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            hideOutlines();
2321c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
2322c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
2323c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2324c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    public void onDragExit(DragObject d) {
23258a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen        mDragHasEnteredWorkspace = false;
2326c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        doDragExit(d);
232731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
232831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2329cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public DropTarget getDropTargetDelegate(DragObject d) {
2330440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        return null;
2331440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy    }
23326569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
2333a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    /**
2334580e27748137ff08599aa719d106b31215a28353Winson Chung     * Tests to see if the drop will be accepted by Launcher, and if so, includes additional data
2335580e27748137ff08599aa719d106b31215a28353Winson Chung     * in the returned structure related to the widgets that match the drop (or a null list if it is
2336580e27748137ff08599aa719d106b31215a28353Winson Chung     * a shortcut drop).  If the drop is not accepted then a null structure is returned.
2337580e27748137ff08599aa719d106b31215a28353Winson Chung     */
2338580e27748137ff08599aa719d106b31215a28353Winson Chung    private Pair<Integer, List<WidgetMimeTypeHandlerData>> validateDrag(DragEvent event) {
2339580e27748137ff08599aa719d106b31215a28353Winson Chung        final LauncherModel model = mLauncher.getModel();
2340580e27748137ff08599aa719d106b31215a28353Winson Chung        final ClipDescription desc = event.getClipDescription();
2341580e27748137ff08599aa719d106b31215a28353Winson Chung        final int mimeTypeCount = desc.getMimeTypeCount();
2342580e27748137ff08599aa719d106b31215a28353Winson Chung        for (int i = 0; i < mimeTypeCount; ++i) {
2343580e27748137ff08599aa719d106b31215a28353Winson Chung            final String mimeType = desc.getMimeType(i);
2344580e27748137ff08599aa719d106b31215a28353Winson Chung            if (mimeType.equals(InstallShortcutReceiver.SHORTCUT_MIMETYPE)) {
2345580e27748137ff08599aa719d106b31215a28353Winson Chung                return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, null);
2346580e27748137ff08599aa719d106b31215a28353Winson Chung            } else {
2347580e27748137ff08599aa719d106b31215a28353Winson Chung                final List<WidgetMimeTypeHandlerData> widgets =
2348580e27748137ff08599aa719d106b31215a28353Winson Chung                    model.resolveWidgetsForMimeType(mContext, mimeType);
2349580e27748137ff08599aa719d106b31215a28353Winson Chung                if (widgets.size() > 0) {
2350580e27748137ff08599aa719d106b31215a28353Winson Chung                    return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, widgets);
2351580e27748137ff08599aa719d106b31215a28353Winson Chung                }
2352580e27748137ff08599aa719d106b31215a28353Winson Chung            }
2353580e27748137ff08599aa719d106b31215a28353Winson Chung        }
2354580e27748137ff08599aa719d106b31215a28353Winson Chung        return null;
2355580e27748137ff08599aa719d106b31215a28353Winson Chung    }
2356580e27748137ff08599aa719d106b31215a28353Winson Chung
2357580e27748137ff08599aa719d106b31215a28353Winson Chung    /**
2358a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     * Global drag and drop handler
2359a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung     */
2360a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    @Override
2361a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    public boolean onDragEvent(DragEvent event) {
236268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung        final ClipDescription desc = event.getClipDescription();
2363a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final CellLayout layout = (CellLayout) getChildAt(mCurrentPage);
2364a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int[] pos = new int[2];
2365a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        layout.getLocationOnScreen(pos);
2366a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        // We need to offset the drag coordinates to layout coordinate space
2367a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int x = (int) event.getX() - pos[0];
2368a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        final int y = (int) event.getY() - pos[1];
2369a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
2370a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        switch (event.getAction()) {
2371580e27748137ff08599aa719d106b31215a28353Winson Chung        case DragEvent.ACTION_DRAG_STARTED: {
2372580e27748137ff08599aa719d106b31215a28353Winson Chung            // Validate this drag
2373580e27748137ff08599aa719d106b31215a28353Winson Chung            Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event);
2374580e27748137ff08599aa719d106b31215a28353Winson Chung            if (test != null) {
2375580e27748137ff08599aa719d106b31215a28353Winson Chung                boolean isShortcut = (test.second == null);
2376580e27748137ff08599aa719d106b31215a28353Winson Chung                if (isShortcut) {
2377580e27748137ff08599aa719d106b31215a28353Winson Chung                    // Check if we have enough space on this screen to add a new shortcut
2378580e27748137ff08599aa719d106b31215a28353Winson Chung                    if (!layout.findCellForSpan(pos, 1, 1)) {
2379557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                        mLauncher.showOutOfSpaceMessage();
2380580e27748137ff08599aa719d106b31215a28353Winson Chung                        return false;
2381580e27748137ff08599aa719d106b31215a28353Winson Chung                    }
2382580e27748137ff08599aa719d106b31215a28353Winson Chung                }
2383580e27748137ff08599aa719d106b31215a28353Winson Chung            } else {
2384580e27748137ff08599aa719d106b31215a28353Winson Chung                // Show error message if we couldn't accept any of the items
2385580e27748137ff08599aa719d106b31215a28353Winson Chung                Toast.makeText(mContext, mContext.getString(R.string.external_drop_widget_error),
2386a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                        Toast.LENGTH_SHORT).show();
2387a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                return false;
2388a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
2389a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
239068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // Create the drag outline
239168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // We need to add extra padding to the bitmap to make room for the glow effect
239268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            final Canvas canvas = new Canvas();
239338b4f7c5eab773ad36048b5ab8713750dcaaf748Michael Jurka            final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
239468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            mDragOutline = createExternalDragOutline(canvas, bitmapPadding);
2395a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
239668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // Show the current page outlines to indicate that we can accept this drop
239768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            showOutlines();
239868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            layout.onDragEnter();
2399b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1, null, null);
2400a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
240168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            return true;
2402580e27748137ff08599aa719d106b31215a28353Winson Chung        }
2403a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        case DragEvent.ACTION_DRAG_LOCATION:
2404a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            // Visualize the drop location
2405b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1, null, null);
2406a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            return true;
2407580e27748137ff08599aa719d106b31215a28353Winson Chung        case DragEvent.ACTION_DROP: {
2408a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            // Try and add any shortcuts
2409a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            final LauncherModel model = mLauncher.getModel();
2410a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            final ClipData data = event.getClipData();
241155cef262f97749300c2f6e764da0b00cbcb78879Winson Chung
241268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // We assume that the mime types are ordered in descending importance of
241368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // representation. So we enumerate the list of mime types and alert the
241468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // user if any widgets can handle the drop.  Only the most preferred
241568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            // representation will be handled.
241668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            pos[0] = x;
241768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung            pos[1] = y;
2418580e27748137ff08599aa719d106b31215a28353Winson Chung            Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event);
2419580e27748137ff08599aa719d106b31215a28353Winson Chung            if (test != null) {
2420580e27748137ff08599aa719d106b31215a28353Winson Chung                final int index = test.first;
2421580e27748137ff08599aa719d106b31215a28353Winson Chung                final List<WidgetMimeTypeHandlerData> widgets = test.second;
2422580e27748137ff08599aa719d106b31215a28353Winson Chung                final boolean isShortcut = (widgets == null);
2423580e27748137ff08599aa719d106b31215a28353Winson Chung                final String mimeType = desc.getMimeType(index);
2424580e27748137ff08599aa719d106b31215a28353Winson Chung                if (isShortcut) {
24250d5aad7edbed3ed5c13904cc13ec8842ffe8837bDianne Hackborn                    final Intent intent = data.getItemAt(index).getIntent();
242668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                    Object info = model.infoFromShortcutIntent(mContext, intent, data.getIcon());
2427d919882006a457d16dea46bd546472e6534b5c19Adam Cohen                    if (info != null) {
2428d919882006a457d16dea46bd546472e6534b5c19Adam Cohen                        onDropExternal(new int[] { x, y }, info, layout, false);
2429d919882006a457d16dea46bd546472e6534b5c19Adam Cohen                    }
243068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                } else {
2431580e27748137ff08599aa719d106b31215a28353Winson Chung                    if (widgets.size() == 1) {
243268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        // If there is only one item, then go ahead and add and configure
243368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        // that widget
243468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        final AppWidgetProviderInfo widgetInfo = widgets.get(0).widgetInfo;
243568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        final PendingAddWidgetInfo createInfo =
2436c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                                new PendingAddWidgetInfo(widgetInfo, mimeType, data);
24373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        mLauncher.addAppWidgetFromDrop(createInfo,
24383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentPage, null, pos);
2439580e27748137ff08599aa719d106b31215a28353Winson Chung                    } else {
244068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        // Show the widget picker dialog if there is more than one widget
244168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        // that can handle this data type
244268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        final InstallWidgetReceiver.WidgetListAdapter adapter =
244368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                            new InstallWidgetReceiver.WidgetListAdapter(mLauncher, mimeType,
244468846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                    data, widgets, layout, mCurrentPage, pos);
244568846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        final AlertDialog.Builder builder =
244668846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                            new AlertDialog.Builder(mContext);
244768846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        builder.setAdapter(adapter, adapter);
244868846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        builder.setCancelable(true);
244968846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        builder.setTitle(mContext.getString(
245068846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                                R.string.external_drop_widget_pick_title));
245168846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        builder.setIcon(R.drawable.ic_no_applications);
245268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chung                        builder.show();
245355cef262f97749300c2f6e764da0b00cbcb78879Winson Chung                    }
2454a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung                }
2455a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            }
2456a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            return true;
2457580e27748137ff08599aa719d106b31215a28353Winson Chung        }
2458a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        case DragEvent.ACTION_DRAG_ENDED:
2459a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            // Hide the page outlines after the drop
2460a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            layout.onDragExit();
2461a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            hideOutlines();
2462a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung            return true;
2463a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        }
2464a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        return super.onDragEvent(event);
2465a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
2466a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
24674516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    /*
24684516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
24694516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
24704516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * coordinate space. The argument xy is modified with the return result.
24714516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
24724516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    */
24734516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   void mapPointFromSelfToChild(View v, float[] xy) {
24744516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       mapPointFromSelfToChild(v, xy, null);
24754516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   }
24764516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
24774516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   /*
24784516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
24794516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
24804516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * coordinate space. The argument xy is modified with the return result.
24814516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
24824516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * if cachedInverseMatrix is not null, this method will just use that matrix instead of
2483d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka    * computing it itself; we use this to avoid redundant matrix inversions in
24844516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * findMatchingPageForDragOver
24854516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
24864516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    */
24874516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
24884516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       if (cachedInverseMatrix == null) {
24894516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka           v.getMatrix().invert(mTempInverseMatrix);
24904516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka           cachedInverseMatrix = mTempInverseMatrix;
24914516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       }
24923bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       int scrollX = mScrollX;
24933bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       if (mNextPage != INVALID_PAGE) {
24943bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung           scrollX = mScroller.getFinalX();
24953bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       }
24963bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       xy[0] = xy[0] + scrollX - v.getLeft();
24974516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       xy[1] = xy[1] + mScrollY - v.getTop();
24984516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       cachedInverseMatrix.mapPoints(xy);
24994516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   }
25004516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
25014516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   /*
25023d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    * Maps a point from the Workspace's coordinate system to another sibling view's. (Workspace
25033d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    * covers the full screen)
25043d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    */
25053d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung   void mapPointFromSelfToSibling(View v, float[] xy) {
25063d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung       xy[0] = xy[0] - v.getLeft();
25073d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung       xy[1] = xy[1] - v.getTop();
25083d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung   }
25093d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
25103d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung   /*
25114516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
25124516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * Convert the 2D coordinate xy from this CellLayout's coordinate space to
25134516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * the parent View's coordinate space. The argument xy is modified with the return result.
25144516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
25154516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    */
25164516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   void mapPointFromChildToSelf(View v, float[] xy) {
25174516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       v.getMatrix().mapPoints(xy);
25183bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       int scrollX = mScrollX;
25193bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       if (mNextPage != INVALID_PAGE) {
25203bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung           scrollX = mScroller.getFinalX();
25213bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       }
25223bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung       xy[0] -= (scrollX - v.getLeft());
25234516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka       xy[1] -= (mScrollY - v.getTop());
25244516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   }
25254516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
2526e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen   static private float squaredDistance(float[] point1, float[] point2) {
25274516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float distanceX = point1[0] - point2[0];
25284516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float distanceY = point2[1] - point2[1];
25294516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        return distanceX * distanceX + distanceY * distanceY;
2530e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen   }
25310280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
25324516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    /*
25334516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
25344516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * Returns true if the passed CellLayout cl overlaps with dragView
25354516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
25364516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     */
25374516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    boolean overlaps(CellLayout cl, DragView dragView,
25384516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            int dragViewX, int dragViewY, Matrix cachedInverseMatrix) {
25394516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // Transform the coordinates of the item being dragged to the CellLayout's coordinates
25404516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        final float[] draggedItemTopLeft = mTempDragCoordinates;
2541f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        draggedItemTopLeft[0] = dragViewX;
2542f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        draggedItemTopLeft[1] = dragViewY;
25434516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates;
2544f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getDragRegionWidth();
2545f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getDragRegionHeight();
25464516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
25474516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // Transform the dragged item's top left coordinates
25484516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // to the CellLayout's local coordinates
25494516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix);
25504516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]);
25514516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]);
25524516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
25534516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) {
25544516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            // Transform the dragged item's bottom right coordinates
25554516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            // to the CellLayout's local coordinates
25564516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix);
25574516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]);
25584516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]);
25594516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
25604516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) {
25614516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                float overlap = (overlapRegionRight - overlapRegionLeft) *
25624516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                         (overlapRegionBottom - overlapRegionTop);
25634516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                if (overlap > 0) {
25644516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                    return true;
25654516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                }
25664516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka             }
25674516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        }
25684516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        return false;
25694516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    }
2570a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
25714516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    /*
25724516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
25734516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * This method returns the CellLayout that is currently being dragged to. In order to drag
25744516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
25754516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
25764516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
25774516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * Return null if no CellLayout is currently being dragged over
25784516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
25794516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     */
25804516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    private CellLayout findMatchingPageForDragOver(
258100618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen            DragView dragView, float originX, float originY, boolean exact) {
25824516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // We loop through all the screens (ie CellLayouts) and see which ones overlap
25834516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // with the item being dragged and then choose the one that's closest to the touch point
2584a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        final int screenCount = getChildCount();
2585a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        CellLayout bestMatchingScreen = null;
25860280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float smallestDistSoFar = Float.MAX_VALUE;
25874516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
2588a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        for (int i = 0; i < screenCount; i++) {
25893d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            CellLayout cl = (CellLayout) getChildAt(i);
2590a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
259100618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen            final float[] touchXy = {originX, originY};
25924516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            // Transform the touch coordinates to the CellLayout's local coordinates
25934516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            // If the touch point is within the bounds of the cell layout, we can return immediately
25940280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cl.getMatrix().invert(mTempInverseMatrix);
25954516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix);
25964516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
25974516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
25984516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                    touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
25994516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                return cl;
26004516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            }
26014516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
260296ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung            if (!exact) {
26034516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // Get the center of the cell layout in screen coordinates
26044516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
26054516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                cellLayoutCenter[0] = cl.getWidth()/2;
26064516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                cellLayoutCenter[1] = cl.getHeight()/2;
26074516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                mapPointFromChildToSelf(cl, cellLayoutCenter);
26084516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
260900618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen                touchXy[0] = originX;
261000618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen                touchXy[1] = originY;
26114516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
26124516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // Calculate the distance between the center of the CellLayout
26134516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // and the touch point
26144516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                float dist = squaredDistance(touchXy, cellLayoutCenter);
26154516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
26164516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                if (dist < smallestDistSoFar) {
26170280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    smallestDistSoFar = dist;
2618a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                    bestMatchingScreen = cl;
2619a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                }
26204516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            }
2621a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
2622a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        return bestMatchingScreen;
2623a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
2624a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
2625e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    // This is used to compute the visual center of the dragView. This point is then
2626e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    // used to visualize drop locations and determine where to drop an item. The idea is that
2627e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    // the visual center represents the user's interpretation of where the item is, and hence
2628e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    // is the appropriate point to use when determining drop location.
2629e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
2630e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            DragView dragView, float[] recycle) {
2631e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        float res[];
2632e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        if (recycle == null) {
2633e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            res = new float[2];
2634e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        } else {
2635e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            res = recycle;
2636e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        }
2637e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2638e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // First off, the drag view has been shifted in a way that is not represented in the
2639e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // x and y values or the x/yOffsets. Here we account for that shift.
2640e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX);
2641e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
2642e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2643e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // These represent the visual top and left of drag view if a dragRect was provided.
2644e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // If a dragRect was not provided, then they correspond to the actual view left and
2645e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // top, as the dragRect is in that case taken to be the entire dragView.
2646e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // R.dimen.dragViewOffsetY.
2647e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int left = x - xOffset;
2648e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        int top = y - yOffset;
2649e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2650e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // In order to find the visual center, we shift by half the dragRect
2651e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        res[0] = left + dragView.getDragRegion().width() / 2;
2652e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        res[1] = top + dragView.getDragRegion().height() / 2;
2653e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2654e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        return res;
2655e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen    }
2656e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2657ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    private boolean isDragWidget(DragObject d) {
2658ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        return (d.dragInfo instanceof LauncherAppWidgetInfo ||
2659ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung                d.dragInfo instanceof PendingAddWidgetInfo);
2660ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    }
2661ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    private boolean isExternalDragWidget(DragObject d) {
2662ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        return d.dragSource != this && isDragWidget(d);
2663ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    }
2664ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung
2665cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDragOver(DragObject d) {
2666c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Skip drag over events while we are dragging over side pages
2667c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mInScrollArea) return;
2668d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (mIsSwitchingState) return;
2669c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
26704afe9b31f51022538b30646fe83780e83bf22490Winson Chung        Rect r = new Rect();
26713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        CellLayout layout = null;
2672c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        ItemInfo item = (ItemInfo) d.dragInfo;
2673c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2674c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Ensure that we have proper spans for the item that we are dropping
2675c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found");
267600618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen        mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
26774afe9b31f51022538b30646fe83780e83bf22490Winson Chung            d.dragView, mDragViewVisualCenter);
267800618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen
2679c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Identify whether we have dragged over a side page
2680d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isSmall()) {
2681ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung            if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) {
26824afe9b31f51022538b30646fe83780e83bf22490Winson Chung                mLauncher.getHotseat().getHitRect(r);
26834afe9b31f51022538b30646fe83780e83bf22490Winson Chung                if (r.contains(d.x, d.y)) {
26844afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    layout = mLauncher.getHotseat().getLayout();
26854afe9b31f51022538b30646fe83780e83bf22490Winson Chung                }
26864afe9b31f51022538b30646fe83780e83bf22490Winson Chung            }
26874afe9b31f51022538b30646fe83780e83bf22490Winson Chung            if (layout == null) {
268896ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false);
26894afe9b31f51022538b30646fe83780e83bf22490Winson Chung            }
2690c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (layout != mDragTargetLayout) {
2691c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // Cancel all intermediate folder states
2692c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                cleanupFolderCreation(d);
2693e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2694c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (mDragTargetLayout != null) {
2695c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.setIsDragOverlapping(false);
2696c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.onDragExit();
2697d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka                }
2698c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout = layout;
2699c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (mDragTargetLayout != null) {
2700c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.setIsDragOverlapping(true);
2701c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.onDragEnter();
2702c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                } else {
2703c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mLastDragOverView = null;
2704c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                }
2705c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2706d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED);
2707c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (isInSpringLoadedMode) {
27084afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    if (mLauncher.isHotseatLayout(layout)) {
27094afe9b31f51022538b30646fe83780e83bf22490Winson Chung                        mSpringLoadedDragController.cancel();
27104afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    } else {
27114afe9b31f51022538b30646fe83780e83bf22490Winson Chung                        mSpringLoadedDragController.setAlarm(mDragTargetLayout);
27124afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    }
2713d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka                }
2714d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            }
2715c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        } else {
27163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            // Test to see if we are over the hotseat otherwise just use the current page
2717ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung            if (mLauncher.getHotseat() != null && !isDragWidget(d)) {
27183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mLauncher.getHotseat().getHitRect(r);
27193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                if (r.contains(d.x, d.y)) {
27203d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    layout = mLauncher.getHotseat().getLayout();
27213d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                }
27223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
27233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (layout == null) {
27243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                layout = getCurrentDropLayout();
27253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
2726c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (layout != mDragTargetLayout) {
2727c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (mDragTargetLayout != null) {
2728c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.setIsDragOverlapping(false);
2729c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.onDragExit();
2730d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka                }
2731c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout = layout;
2732c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout.setIsDragOverlapping(true);
2733c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout.onDragEnter();
2734c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
2735c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
2736a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
2737c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Handle the drag over
2738c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mDragTargetLayout != null) {
2739c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            final View child = (mDragInfo == null) ? null : mDragInfo.cell;
274019072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
2741c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            // We want the point to be mapped to the dragTarget.
27423d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
27433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
27443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
27453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
27463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
2747c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            ItemInfo info = (ItemInfo) d.dragInfo;
274819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
2749c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
2750c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    (int) mDragViewVisualCenter[1], 1, 1, mDragTargetLayout, mTargetCell);
2751c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0],
2752c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mTargetCell[1]);
2753c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2754c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            boolean userFolderPending = willCreateUserFolder(info, mDragTargetLayout,
2755c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mTargetCell, false);
2756c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            boolean isOverFolder = dragOverView instanceof FolderIcon;
2757c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (dragOverView != mLastDragOverView) {
2758c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                cancelFolderCreation();
2759c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) {
2760c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo);
2761d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka                }
27621262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy            }
2763c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2764c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (userFolderPending && dragOverView != mLastDragOverView) {
2765c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mFolderCreationAlarm.setOnAlarmListener(new
2766c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                        FolderCreationAlarmListener(mDragTargetLayout, mTargetCell[0], mTargetCell[1]));
2767c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT);
2768c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
2769c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2770c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (dragOverView != mLastDragOverView && isOverFolder) {
2771c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                ((FolderIcon) dragOverView).onDragEnter(d.dragInfo);
2772c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (mDragTargetLayout != null) {
2773c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.clearDragOutlines();
2774c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                }
2775c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
2776c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mLastDragOverView = dragOverView;
2777c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2778c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (!mCreateUserFolderOnDrop && !isOverFolder) {
2779c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
2780c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                        (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1],
2781b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung                        item.spanX, item.spanY, d.dragView.getDragVisualizeOffset(),
2782b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung                        d.dragView.getDragRegion());
2783c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
2784c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
2785c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
2786c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
2787c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    private void cleanupFolderCreation(DragObject d) {
2788c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) {
2789c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mDragFolderRingAnimator.animateToNaturalState();
2790c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
2791c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) {
2792c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (d != null) {
2793c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo);
2794c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
2795976ebec9e31643d3513f9f0de2b433d9aa186ce9Patrick Dubroy        }
2796c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mFolderCreationAlarm.cancelAlarm();
279731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
279831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2799c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    private void cancelFolderCreation() {
2800c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) {
2801c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            mDragFolderRingAnimator.animateToNaturalState();
2802c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        }
2803c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        mCreateUserFolderOnDrop = false;
2804c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        mFolderCreationAlarm.cancelAlarm();
2805c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    }
2806c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
280719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    class FolderCreationAlarmListener implements OnAlarmListener {
280869ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        CellLayout layout;
280969ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        int cellX;
281069ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        int cellY;
281119072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
281269ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) {
281369ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.layout = layout;
281469ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.cellX = cellX;
281569ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.cellY = cellY;
281619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        }
281719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
281819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        public void onAlarm(Alarm alarm) {
281919072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen            if (mDragFolderRingAnimator == null) {
282019072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen                mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null);
282119072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen            }
282269ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            mDragFolderRingAnimator.setCell(cellX, cellY);
282369ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            mDragFolderRingAnimator.setCellLayout(layout);
282419072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen            mDragFolderRingAnimator.animateToAcceptState();
282569ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            layout.showFolderAccept(mDragFolderRingAnimator);
282669ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            layout.clearDragOutlines();
282719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen            mCreateUserFolderOnDrop = true;
282819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        }
282919072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    }
283019072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
2831a34abf8c781485b788fddacb352d586bffca886cWinson Chung    @Override
2832a34abf8c781485b788fddacb352d586bffca886cWinson Chung    public void getHitRect(Rect outRect) {
2833a34abf8c781485b788fddacb352d586bffca886cWinson Chung        // We want the workspace to have the whole area of the display (it will find the correct
2834a34abf8c781485b788fddacb352d586bffca886cWinson Chung        // cell layout to drop to in the existing drag/drop logic.
2835cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        outRect.set(0, 0, mDisplayWidth, mDisplayHeight);
2836a34abf8c781485b788fddacb352d586bffca886cWinson Chung    }
2837a34abf8c781485b788fddacb352d586bffca886cWinson Chung
28382b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    /**
28392b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * Add the item specified by dragInfo to the given layout.
28402b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * @return true if successful
28412b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     */
2842120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen    public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) {
2843120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen        if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) {
2844bbaa75c8c432c46c00ee2eb836e438b3774d6709Patrick Dubroy            onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false);
28452b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy            return true;
28462b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        }
28470280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        mLauncher.showOutOfSpaceMessage();
28482b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        return false;
28492b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    }
28502b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
2851d5e42733799817d11d739b0a882da8dda721126dAdam Cohen    private void onDropExternal(int[] touchXY, Object dragInfo,
2852d5e42733799817d11d739b0a882da8dda721126dAdam Cohen            CellLayout cellLayout, boolean insertAtFirst) {
2853e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null);
2854d5e42733799817d11d739b0a882da8dda721126dAdam Cohen    }
2855d5e42733799817d11d739b0a882da8dda721126dAdam Cohen
2856120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen    /**
2857120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * Drop an item that didn't originate on one of the workspace screens.
2858120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * It may have come from Launcher (e.g. from all apps or customize), or it may have
2859120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * come from another app altogether.
2860120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     *
2861120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * NOTE: This can also be called when we are outside of a drag event, when we want
2862120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * to add an item to one of the workspace screens.
2863120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     */
2864557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private void onDropExternal(final int[] touchXY, final Object dragInfo,
2865557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
2866557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        final Runnable exitSpringLoadedRunnable = new Runnable() {
2867557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            @Override
2868557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            public void run() {
2869ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                mLauncher.exitSpringLoadedDragModeDelayed(true, false, null);
2870557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
2871557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        };
2872b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
2873b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        ItemInfo info = (ItemInfo) dragInfo;
2874b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        int spanX = info.spanX;
2875b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        int spanY = info.spanY;
2876b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        if (mDragInfo != null) {
2877b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            spanX = mDragInfo.spanX;
2878b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            spanY = mDragInfo.spanY;
2879b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        }
2880b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
28813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final long container = mLauncher.isHotseatLayout(cellLayout) ?
28823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                LauncherSettings.Favorites.CONTAINER_HOTSEAT :
28833d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    LauncherSettings.Favorites.CONTAINER_DESKTOP;
2884557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        final int screen = indexOfChild(cellLayout);
28853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (!mLauncher.isHotseatLayout(cellLayout) && screen != mCurrentPage
28863d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                && mState != State.SPRING_LOADED) {
288776078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen            snapToPage(screen);
288876078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen        }
2889b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
2890b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        if (info instanceof PendingAddItemInfo) {
2891b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
2892b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
2893558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            boolean findNearestVacantCell = true;
2894558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
2895558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
2896558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        cellLayout, mTargetCell);
2897558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell,
2898558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo,
2899558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                                mDragTargetLayout, mTargetCell)) {
2900558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                    findNearestVacantCell = false;
2901558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                }
2902558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
2903558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (findNearestVacantCell) {
2904558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                    mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], spanX, spanY, null,
2905558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        cellLayout, mTargetCell);
2906558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
2907558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
2908b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            Runnable onAnimationCompleteRunnable = new Runnable() {
2909557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                @Override
2910557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                public void run() {
2911557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    // When dragging and dropping from customization tray, we deal with creating
2912557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    // widgets/shortcuts/folders in a slightly different way
2913b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen                    switch (pendingInfo.itemType) {
2914557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2915b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen                        mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo,
29163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                                container, screen, mTargetCell, null);
2917557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                        break;
2918557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2919b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen                        mLauncher.processShortcutFromDrop(pendingInfo.componentName,
29203d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                                container, screen, mTargetCell, null);
2921557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                        break;
2922557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    default:
2923b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen                        throw new IllegalStateException("Unknown item type: " +
2924b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen                                pendingInfo.itemType);
2925557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    }
2926557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    cellLayout.onDragExit();
2927557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                }
2928b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            };
2929b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
2930ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animateExternalDrop((PendingAddItemInfo) info, cellLayout, d.dragView,
2931ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    onAnimationCompleteRunnable, ANIMATE_INTO_POSITION);
29329c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka        } else {
29339c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            // This is for other drag/drop cases, like dragging from All Apps
29349c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            View view = null;
293531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29369c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            switch (info.itemType) {
29379c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
29389c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
29399c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                if (info.container == NO_ID && info instanceof ApplicationInfo) {
29409c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                    // Came from all apps -- make a copy
2941c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka                    info = new ShortcutInfo((ApplicationInfo) info);
29429c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                }
29439c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                view = mLauncher.createShortcut(R.layout.application, cellLayout,
29449c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                        (ShortcutInfo) info);
29459c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                break;
2946df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2947c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout,
2948c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        (FolderInfo) info, mIconCache);
29499c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                break;
29509c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            default:
29519c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                throw new IllegalStateException("Unknown item type: " + info.itemType);
29529c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            }
295331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2954c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            // First we find the cell nearest to point at which the item is
2955c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            // dropped, without any consideration to whether there is an item there.
2956c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            if (touchXY != null) {
2957c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
2958c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        cellLayout, mTargetCell);
2959557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                d.postAnimationRunnable = exitSpringLoadedRunnable;
29603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, true,
29613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        d.dragView, d.postAnimationRunnable)) {
2962c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
2963c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
29643e8f811f31a2275e778244427271f77f4116c08bAdam Cohen                if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, d, true)) {
2965c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
2966c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
296710b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            }
296810b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
2969c4e772e4b3f21bde342e475ebc49b5b76b8f74e8Michael Jurka            if (touchXY != null) {
29709c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                // when dragging and dropping, just find the closest free spot
2971c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, null,
2972c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        cellLayout, mTargetCell);
29739c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            } else {
29749c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                cellLayout.findCellForSpan(mTargetCell, 1, 1);
297531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
29763d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            addInScreen(view, container, screen, mTargetCell[0], mTargetCell[1], info.spanX,
29773d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    info.spanY, insertAtFirst);
2978716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen            cellLayout.onDropChild(view);
29796569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
2980d5e42733799817d11d739b0a882da8dda721126dAdam Cohen            cellLayout.getChildrenLayout().measureChild(view);
2981d5e42733799817d11d739b0a882da8dda721126dAdam Cohen
29823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen,
2983aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    lp.cellX, lp.cellY);
29843e8f811f31a2275e778244427271f77f4116c08bAdam Cohen
29853e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            if (d.dragView != null) {
29864b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // We wrap the animation call in the temporary set and reset of the current
29874b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // cellLayout to its final transform -- this means we animate the drag view to
29884b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // the correct final location.
29894b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                setFinalTransitionTransform(cellLayout);
2990557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view,
29914b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                        exitSpringLoadedRunnable);
29924b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                resetTransitionTransform(cellLayout);
29934b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            }
29944b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        }
29954b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
29964b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
2997ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    // The following methods deal with animating an item from external drop
2998ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    void onPreDraw(View v) {
2999ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (v instanceof ViewGroup) {
3000ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            ViewGroup vg = (ViewGroup) v;
3001ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            for (int i = 0; i < vg.getChildCount(); i++) {
3002ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                View child = vg.getChildAt(i);
3003ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                onPreDraw(child);
3004ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            }
3005ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else if (v instanceof TextView) {
3006ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            ((TextView) v).onPreDraw();
3007ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
3008ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
3009ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3010ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    public Bitmap createWidgetBitmap(PendingAddWidgetInfo widgetInfo) {
3011ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo.spanX,
3012ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                widgetInfo.spanY, widgetInfo, false);
3013ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        View layout = widgetInfo.boundWidget;
3014ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.setVisibility(VISIBLE);
3015ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3016ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
3017ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
3018ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
3019ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                Bitmap.Config.ARGB_8888);
3020ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Canvas c = new Canvas(b);
3021ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3022ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.measure(width, height);
3023ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
3024ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        onPreDraw(layout);
3025ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.draw(c);
3026ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        c.setBitmap(null);
3027ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        return b;
3028ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
3029ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3030ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    public void animateExternalDrop(PendingAddItemInfo pendingInfo, CellLayout cellLayout,
3031ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            DragView dragView, Runnable onCompleteRunnable, int animationType) {
3032ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
3033ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // location and size on the home screen.
3034ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int spanX = pendingInfo.spanX;
3035ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int spanY = pendingInfo.spanY;
3036ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        RectF r = estimateItemPosition(cellLayout, pendingInfo,
3037ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                mTargetCell[0], mTargetCell[1], spanX, spanY);
3038ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int loc[] = new int[2];
3039ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        loc[0] = (int) r.left;
3040ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        loc[1] = (int) r.top;
3041ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        setFinalTransitionTransform(cellLayout);
3042ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float cellLayoutScale =
3043ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(cellLayout, loc);
3044ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        resetTransitionTransform(cellLayout);
3045ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3046ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float dragViewScaleX = r.width() / dragView.getMeasuredWidth();
3047ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float dragViewScaleY = r.height() / dragView.getMeasuredHeight();
3048ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // The animation will scale the dragView about its center, so we need to center about
3049ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // the final location.
3050ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2;
3051ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;
3052ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3053ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float scaleX = dragViewScaleX * cellLayoutScale;
3054ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float scaleY = dragViewScaleY * cellLayoutScale;
3055ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3056ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Resources res = mLauncher.getResources();
3057ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200;
3058ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3059ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int animationEnd = DragLayer.ANIMATION_END_REMAIN_VISIBLE;
3060ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
3061ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                (((PendingAddWidgetInfo) pendingInfo).info.configure == null ||
3062ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                animationType == COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION)) {
3063ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            Bitmap crossFadeBitmap = createWidgetBitmap((PendingAddWidgetInfo) pendingInfo);
3064ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            dragView.setCrossFadeBitmap(crossFadeBitmap);
3065ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            dragView.crossFade((int) (duration * 0.8f));
3066ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animationEnd = DragLayer.ANIMATION_END_DISAPPEAR;
3067ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else {
3068ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            scaleX = scaleY = Math.min(scaleX,  scaleY);
3069ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
3070ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
30717bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung        if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) {
30727bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung            mLauncher.getDragLayer().animateViewIntoPosition(dragView, loc, 0f, 0.1f, 0.1f,
3073ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration);
3074ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else {
30757bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung            mLauncher.getDragLayer().animateViewIntoPosition(dragView, loc, 1f, scaleX, scaleY,
3076ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                animationEnd, onCompleteRunnable, duration);
3077ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
3078ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
3079ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
30804b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public void setFinalTransitionTransform(CellLayout layout) {
30814b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        if (isSwitchingState()) {
30824b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            int index = indexOfChild(layout);
30834b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentScaleX = layout.getScaleX();
30844b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentScaleY = layout.getScaleY();
30854b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentTranslationX = layout.getTranslationX();
30864b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentTranslationY = layout.getTranslationY();
30874b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentRotationY = layout.getRotationY();
30884b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setScaleX(mNewScaleXs[index]);
30894b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setScaleY(mNewScaleYs[index]);
30904b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setTranslationX(mNewTranslationXs[index]);
30914b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setTranslationY(mNewTranslationYs[index]);
30924b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setRotationY(mNewRotationYs[index]);
30934b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        }
30944b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
30954b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public void resetTransitionTransform(CellLayout layout) {
30964b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        if (isSwitchingState()) {
30974b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentScaleX = layout.getScaleX();
30984b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentScaleY = layout.getScaleY();
30994b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentTranslationX = layout.getTranslationX();
31004b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentTranslationY = layout.getTranslationY();
31014b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            mCurrentRotationY = layout.getRotationY();
31024b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setScaleX(mCurrentScaleX);
31034b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setScaleY(mCurrentScaleY);
31044b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setTranslationX(mCurrentTranslationX);
31054b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setTranslationY(mCurrentTranslationY);
31064b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            layout.setRotationY(mCurrentRotationY);
3107af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        }
310831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3109aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
311070864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
311170864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Return the current {@link CellLayout}, correctly picking the destination
311270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * screen while a scroll is in progress.
311370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
31145f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    public CellLayout getCurrentDropLayout() {
31155f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy        return (CellLayout) getChildAt(mNextPage == INVALID_PAGE ? mCurrentPage : mNextPage);
311670864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
311731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
311870864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
31190280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * Return the current CellInfo describing our current drag; this method exists
31200280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * so that Launcher can sync this object with the correct info when the activity is created/
31210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * destroyed
31220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     *
31230280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     */
31240280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public CellLayout.CellInfo getDragInfo() {
31250280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return mDragInfo;
31260280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
31270280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
31280280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    /**
312970864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Calculate the nearest cell where the given object would be dropped.
3130e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     *
3131e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     * pixelX and pixelY should be in the coordinate system of layout
313270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
31336a1435d78d5133b1f37274c4d358bf6d22e10229Michael Jurka    private int[] findNearestVacantArea(int pixelX, int pixelY,
313470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey            int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
31350280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return layout.findNearestVacantArea(
3136e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                pixelX, pixelY, spanX, spanY, ignoreView, recycle);
313770864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
3138aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3139df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    /**
3140df0353815c629fc678824b07a234b89a1ff94208Adam Cohen     * Calculate the nearest cell where the given object would be dropped.
3141e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     *
3142e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     * pixelX and pixelY should be in the coordinate system of layout
3143df0353815c629fc678824b07a234b89a1ff94208Adam Cohen     */
3144df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    private int[] findNearestArea(int pixelX, int pixelY,
3145e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int spanX, int spanY, CellLayout layout, int[] recycle) {
3146df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        return layout.findNearestArea(
3147e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                pixelX, pixelY, spanX, spanY, recycle);
3148df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    }
3149df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
3150cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    void setup(DragController dragController) {
3151c2f7f4794867cd1b3e97fd0f379ad8ad15958a6cMichael Jurka        mSpringLoadedDragController = new SpringLoadedDragController(mLauncher);
315200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController = dragController;
3153d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
3154d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // hardware layers on children are enabled on startup, but should be disabled until
3155d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // needed
3156d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        updateChildrenLayersEnabled();
3157d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        setWallpaperDimension();
315831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
315931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3160e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy    /**
3161e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy     * Called at the end of a drag which originated on the workspace.
3162e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy     */
3163c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    public void onDropCompleted(View target, DragObject d, boolean success) {
3164aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        if (success) {
3165d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            if (target != this) {
3166d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                if (mDragInfo != null) {
31673d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
3168d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                    if (mDragInfo.cell instanceof DropTarget) {
3169d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                        mDragController.removeDropTarget((DropTarget) mDragInfo.cell);
3170d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                    }
317100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                }
317231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
3173ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy        } else if (mDragInfo != null) {
3174e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy            // NOTE: When 'success' is true, onDragExit is called by the DragController before
3175e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy            // calling onDropCompleted(). We call it ourselves here, but maybe this should be
3176e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy            // moved into DragController.cancelDrag().
3177c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            doDragExit(null);
31783d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            CellLayout cellLayout;
31793d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mLauncher.isHotseatLayout(target)) {
31803d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                cellLayout = mLauncher.getHotseat().getLayout();
31813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
31823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
31833d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
31843d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            cellLayout.onDropChild(mDragInfo.cell);
318531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
318636cc09b07b19198f4ea886583cef462ade27192cAdam Cohen        if (d.cancelled &&  mDragInfo.cell != null) {
318736cc09b07b19198f4ea886583cef462ade27192cAdam Cohen                mDragInfo.cell.setVisibility(VISIBLE);
318836cc09b07b19198f4ea886583cef462ade27192cAdam Cohen        }
31894be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        mDragOutline = null;
319031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = null;
31911afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung
31921afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        // Hide the scrolling indicator after you pick up an item
31931afedc379f8271b9bf86a8d887c3c020d5e59214Winson Chung        hideScrollingIndicator(false);
319431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
319531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
31960280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public boolean isDropEnabled() {
31970280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return true;
31980280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
31990280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
32000142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
32010142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void onRestoreInstanceState(Parcelable state) {
32020142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.onRestoreInstanceState(state);
32030142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
32040142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
32050142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
32060142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
320731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollLeft() {
3208d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
32090142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollLeft();
321031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
321195bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        Folder openFolder = getOpenFolder();
321295bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        if (openFolder != null) {
321395bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen            openFolder.completeDragExit();
321495bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        }
321531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
321631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
32170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
321831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollRight() {
3219d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
32200142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollRight();
322131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
322295bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        Folder openFolder = getOpenFolder();
322395bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        if (openFolder != null) {
322495bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen            openFolder.completeDragExit();
322595bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        }
322631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3227138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
32281262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    @Override
32293e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung    public boolean onEnterScrollArea(int x, int y, int direction) {
32301c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung        // Ignore the scroll area if we are dragging over the hot seat
32311c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung        if (mLauncher.getHotseat() != null) {
32321c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            Rect r = new Rect();
32331c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            mLauncher.getHotseat().getHitRect(r);
32341c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            if (r.contains(x, y)) {
32353e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung                return false;
32361c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            }
32371c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung        }
32381c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung
32393e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        boolean result = false;
3240d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (!isSmall() && !mIsSwitchingState) {
3241d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka            mInScrollArea = true;
324254fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
3243aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            final int page = (mNextPage != INVALID_PAGE ? mNextPage : mCurrentPage) +
3244aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung                       (direction == DragController.SCROLL_LEFT ? -1 : 1);
3245c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            cancelFolderCreation();
324654fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
3247aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            if (0 <= page && page < getChildCount()) {
3248aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung                CellLayout layout = (CellLayout) getChildAt(page);
3249c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // Exit the current layout and mark the overlapping layout
32500207c522472d7252f146f4d39efa456ca5248c6bPatrick Dubroy                if (mDragTargetLayout != null) {
3251c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                    mDragTargetLayout.setIsDragOverlapping(false);
32520207c522472d7252f146f4d39efa456ca5248c6bPatrick Dubroy                    mDragTargetLayout.onDragExit();
32530207c522472d7252f146f4d39efa456ca5248c6bPatrick Dubroy                }
3254c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout = layout;
3255c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout.setIsDragOverlapping(true);
325670b9530f3147663d38875260b48d931793b7d378Patrick Dubroy
3257c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // Workspace is responsible for drawing the edge glow on adjacent pages,
3258c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // so we need to redraw the workspace when this may have changed.
3259c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                invalidate();
32603e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung                result = true;
3261c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
326270b9530f3147663d38875260b48d931793b7d378Patrick Dubroy        }
32633e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        return result;
326408ae2ec4847a971ad1b19c163e3a0d6307a8ed72Patrick Dubroy    }
326508ae2ec4847a971ad1b19c163e3a0d6307a8ed72Patrick Dubroy
32661262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    @Override
32673e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung    public boolean onExitScrollArea() {
32683e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        boolean result = false;
3269d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka        if (mInScrollArea) {
3270c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (mDragTargetLayout != null) {
3271c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                mDragTargetLayout.setIsDragOverlapping(false);
3272c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // Workspace is responsible for drawing the edge glow on adjacent pages,
3273c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // so we need to redraw the workspace when this may have changed.
3274c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                invalidate();
3275c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
32768a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen            if (mDragTargetLayout != null && mDragHasEnteredWorkspace) {
32778a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen                // Unmark the overlapping layout and re-enter the current layout
32788a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen                mDragTargetLayout = getCurrentDropLayout();
32798a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen                mDragTargetLayout.onDragEnter();
32808a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen            }
32818a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen            result = true;
3282c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mInScrollArea = false;
3283c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
32843e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        return result;
3285c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
3286c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3287c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    private void onResetScrollArea() {
3288c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mDragTargetLayout != null) {
3289c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            // Unmark the overlapping layout
3290c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mDragTargetLayout.setIsDragOverlapping(false);
3291c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3292c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            // Workspace is responsible for drawing the edge glow on adjacent pages,
3293c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            // so we need to redraw the workspace when this may have changed.
3294c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            invalidate();
32951262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy        }
3296c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mInScrollArea = false;
32971262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    }
32981262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy
32993d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
33003d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns a specific CellLayout
33013d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
33023d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    CellLayout getParentCellLayoutForView(View v) {
33033d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts();
33043d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayout layout : layouts) {
33053d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (layout.getChildrenLayout().indexOfChild(v) > -1) {
33063d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return layout;
33073d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
33083d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
33093d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return null;
33103d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
33113d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
33123d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
33133d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns a list of all the CellLayouts in the workspace.
33143d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
33153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() {
33163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> layouts = new ArrayList<CellLayout>();
33173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int screenCount = getChildCount();
33183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (int screen = 0; screen < screenCount; screen++) {
33193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layouts.add(((CellLayout) getChildAt(screen)));
33203d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
33213d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mLauncher.getHotseat() != null) {
33223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layouts.add(mLauncher.getHotseat().getLayout());
33233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
33243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return layouts;
33253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
33263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
33273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
33283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * We should only use this to search for specific children.  Do not use this method to modify
33293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * CellLayoutChildren directly.
33303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
33313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ArrayList<CellLayoutChildren> getWorkspaceAndHotseatCellLayoutChildren() {
33323d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayoutChildren> childrenLayouts = new ArrayList<CellLayoutChildren>();
33333d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int screenCount = getChildCount();
333431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int screen = 0; screen < screenCount; screen++) {
33353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            childrenLayouts.add(((CellLayout) getChildAt(screen)).getChildrenLayout());
33363d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
33373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mLauncher.getHotseat() != null) {
33383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            childrenLayouts.add(mLauncher.getHotseat().getLayout().getChildrenLayout());
33393d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
33403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return childrenLayouts;
33413d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
33423d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
33433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    public Folder getFolderForTag(Object tag) {
33443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
33453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayoutChildren layout: childrenLayouts) {
33463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int count = layout.getChildCount();
334731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            for (int i = 0; i < count; i++) {
33483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                View child = layout.getChildAt(i);
33493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                if (child instanceof Folder) {
335031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    Folder f = (Folder) child;
3351aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    if (f.getInfo() == tag && f.getInfo().opened) {
335231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                        return f;
335331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    }
335431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
335531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
335631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
335731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
335831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
335931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
336031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public View getViewForTag(Object tag) {
33613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
33623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayoutChildren layout: childrenLayouts) {
33633d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int count = layout.getChildCount();
336431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            for (int i = 0; i < count; i++) {
33653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                View child = layout.getChildAt(i);
336631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                if (child.getTag() == tag) {
336731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    return child;
336831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
336931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
337031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
337131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
337231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
337331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3374df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    void clearDropTargets() {
33753d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
33763d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayoutChildren layout: childrenLayouts) {
3377df0353815c629fc678824b07a234b89a1ff94208Adam Cohen            int childCount = layout.getChildCount();
3378df0353815c629fc678824b07a234b89a1ff94208Adam Cohen            for (int j = 0; j < childCount; j++) {
3379df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                View v = layout.getChildAt(j);
3380df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                if (v instanceof DropTarget) {
3381df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                    mDragController.removeDropTarget((DropTarget) v);
3382df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                }
3383df0353815c629fc678824b07a234b89a1ff94208Adam Cohen            }
3384df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        }
3385df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    }
338631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
338764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    void removeItems(final ArrayList<ApplicationInfo> apps) {
3388629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy        final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
3389574d20ec84551370987dde530c27ec493bdef564Romain Guy
339064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        final HashSet<String> packageNames = new HashSet<String>();
339164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        final int appCount = apps.size();
339264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        for (int i = 0; i < appCount; i++) {
339364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato            packageNames.add(apps.get(i).componentName.getPackageName());
339464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        }
339564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
33963d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
33973d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (final CellLayout layoutParent: cellLayouts) {
33987a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung            final ViewGroup layout = layoutParent.getChildrenLayout();
3399629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy
3400629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            // Avoid ANRs by treating each screen separately
3401629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            post(new Runnable() {
3402629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                public void run() {
3403629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    final ArrayList<View> childrenToRemove = new ArrayList<View>();
3404629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    childrenToRemove.clear();
3405aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3406629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    int childCount = layout.getChildCount();
3407629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    for (int j = 0; j < childCount; j++) {
3408629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        final View view = layout.getChildAt(j);
3409629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        Object tag = view.getTag();
3410aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
34110589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        if (tag instanceof ShortcutInfo) {
34120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            final ShortcutInfo info = (ShortcutInfo) tag;
3413629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final Intent intent = info.intent;
3414629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final ComponentName name = intent.getComponent();
3415aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
341664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
341764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                for (String packageName: packageNames) {
341864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    if (packageName.equals(name.getPackageName())) {
341964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
342064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        childrenToRemove.add(view);
342164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
342264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
3423629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
3424df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                        } else if (tag instanceof FolderInfo) {
3425df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                            final FolderInfo info = (FolderInfo) tag;
34260589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            final ArrayList<ShortcutInfo> contents = info.contents;
3427629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final int contentsCount = contents.size();
3428df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                            final ArrayList<ShortcutInfo> appsToRemoveFromFolder =
3429df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                                    new ArrayList<ShortcutInfo>();
3430aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3431629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            for (int k = 0; k < contentsCount; k++) {
34320589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                                final ShortcutInfo appInfo = contents.get(k);
3433629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                final Intent intent = appInfo.intent;
3434629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                final ComponentName name = intent.getComponent();
3435aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
343664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
343764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    for (String packageName: packageNames) {
343864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        if (packageName.equals(name.getPackageName())) {
3439df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                                            appsToRemoveFromFolder.add(appInfo);
344064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        }
344164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
3442629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                }
3443629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
3444df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                            for (ShortcutInfo item: appsToRemoveFromFolder) {
3445df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                                info.remove(item);
3446df1e4e868a1970b756486dadd652651d40276d36Adam Cohen                                LauncherModel.deleteItemFromDatabase(mLauncher, item);
3447629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
3448629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        } else if (tag instanceof LauncherAppWidgetInfo) {
3449629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
3450629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final AppWidgetProviderInfo provider =
3451629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                    widgets.getAppWidgetInfo(info.appWidgetId);
3452e1cc6c3745c9ba721b6ab888d74c9d9e1d13ea6aDaniel Sandler                            if (provider != null) {
345364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                for (String packageName: packageNames) {
345464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    if (packageName.equals(provider.provider.getPackageName())) {
345564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
3456aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                                        childrenToRemove.add(view);
345764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
345864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
3459629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
3460574d20ec84551370987dde530c27ec493bdef564Romain Guy                        }
3461574d20ec84551370987dde530c27ec493bdef564Romain Guy                    }
3462aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3463629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    childCount = childrenToRemove.size();
3464629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    for (int j = 0; j < childCount; j++) {
3465629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        View child = childrenToRemove.get(j);
34667a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung                        // Note: We can not remove the view directly from CellLayoutChildren as this
34677a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung                        // does not re-mark the spaces as unoccupied.
34687a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung                        layoutParent.removeViewInLayout(child);
3469629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        if (child instanceof DropTarget) {
3470629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            mDragController.removeDropTarget((DropTarget)child);
3471629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        }
3472574d20ec84551370987dde530c27ec493bdef564Romain Guy                    }
3473aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3474629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    if (childCount > 0) {
3475629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        layout.requestLayout();
3476629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        layout.invalidate();
34775c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
347831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
3479629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            });
348031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
348131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3482f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
348364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    void updateShortcuts(ArrayList<ApplicationInfo> apps) {
34843d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
34853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayoutChildren layout: childrenLayouts) {
3486f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            int childCount = layout.getChildCount();
3487f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            for (int j = 0; j < childCount; j++) {
3488f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                final View view = layout.getChildAt(j);
3489f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                Object tag = view.getTag();
34900589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                if (tag instanceof ShortcutInfo) {
34910589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    ShortcutInfo info = (ShortcutInfo)tag;
3492f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // We need to check for ACTION_MAIN otherwise getComponent() might
3493f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // return null for some shortcuts (for instance, for shortcuts to
3494f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // web pages.)
3495f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    final Intent intent = info.intent;
3496f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    final ComponentName name = intent.getComponent();
3497f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
349864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
349964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                        final int appCount = apps.size();
3500aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        for (int k = 0; k < appCount; k++) {
350164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            ApplicationInfo app = apps.get(k);
350264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            if (app.componentName.equals(name)) {
350364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                info.setIcon(mIconCache.getIcon(info.intent));
350464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
350564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        new FastBitmapDrawable(info.getIcon(mIconCache)),
350664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        null, null);
350764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
350864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                        }
3509f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    }
3510f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                }
3511f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
3512f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
3513f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
3514f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
351514f122bf847e50a3e7730ccbe57abc25d086a01bJoe Onorato    void moveToDefaultScreen(boolean animate) {
3516de1af7661548692d370518528ff91c7422b9c8aeWinson Chung        if (!isSmall()) {
3517de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            if (animate) {
3518de1af7661548692d370518528ff91c7422b9c8aeWinson Chung                snapToPage(mDefaultPage);
3519de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            } else {
3520de1af7661548692d370518528ff91c7422b9c8aeWinson Chung                setCurrentPage(mDefaultPage);
3521de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            }
3522c45b16862918a0c6c253bba12032165b030565afJoe Onorato        }
35230142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        getChildAt(mDefaultPage).requestFocus();
352431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
352531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
35260142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
35270142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    public void syncPages() {
35280142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
352931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
35300142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
3531f314b0eab03bb354c14b6c89f694a6685bd246d9Winson Chung    public void syncPageItems(int page, boolean immediate) {
353231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
35336a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung
35346a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung    @Override
35356a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung    protected String getCurrentPageDescription() {
35366a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung        int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
35376a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung        return String.format(mContext.getString(R.string.workspace_scroll_format),
35386a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung                page + 1, getChildCount());
35396a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung    }
35408dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen
35418dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    public void getLocationInDragLayer(int[] loc) {
35428dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
35438dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    }
354432174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung
354521b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen    void setFadeForOverScroll(float fade) {
354621b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        if (!isScrollingIndicatorEnabled()) return;
354721b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen
354821b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        mOverscrollFade = fade;
354921b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        float reducedFade = 0.5f + 0.5f * (1 - fade);
355021b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        final ViewGroup parent = (ViewGroup) getParent();
3551e96e5cb768fcc6f1211c8c31fa2da4dcd4e465ccWinson Chung        final ImageView qsbDivider = (ImageView) (parent.findViewById(R.id.qsb_divider));
355221b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        final ImageView dockDivider = (ImageView) (parent.findViewById(R.id.dock_divider));
3553afaa05014e0bf3ed1192f9ddec2af4283bc50248Michael Jurka        final View scrollIndicator = getScrollingIndicator();
355421b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen
355521b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        cancelScrollingIndicatorAnimations();
3556e96e5cb768fcc6f1211c8c31fa2da4dcd4e465ccWinson Chung        if (qsbDivider != null) qsbDivider.setAlpha(reducedFade);
3557e96e5cb768fcc6f1211c8c31fa2da4dcd4e465ccWinson Chung        if (dockDivider != null) dockDivider.setAlpha(reducedFade);
355821b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen        scrollIndicator.setAlpha(1 - fade);
355921b411074e39fd10cbc8dd0220bb159da4af2c76Adam Cohen    }
356031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
3561