blob: 567abfa47d42b2ed98c4c8f5aa52697dee2a9d47 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka0280c3b2010-09-17 15:00:07 -070019import android.animation.Animator;
Adam Cohena5f4e482013-10-11 12:10:28 -070020import android.animation.Animator.AnimatorListener;
Adam Cohenf358a4b2013-07-23 16:47:31 -070021import android.animation.AnimatorListenerAdapter;
Chet Haaseb1254a62010-09-07 13:35:00 -070022import android.animation.AnimatorSet;
Adam Cohen22cba7f2013-07-19 16:14:00 -070023import android.animation.LayoutTransition;
Michael Jurka0280c3b2010-09-17 15:00:07 -070024import android.animation.ObjectAnimator;
Adam Cohenad4e15c2013-10-17 16:21:35 -070025import android.animation.PropertyValuesHolder;
Patrick Dubroycd68ff52010-10-28 17:57:05 -070026import android.animation.TimeInterpolator;
27import android.animation.ValueAnimator;
28import android.animation.ValueAnimator.AnimatorUpdateListener;
Dianne Hackborn8f573952009-08-10 23:21:09 -070029import android.app.WallpaperManager;
Michael Jurkabed61d22012-02-14 22:51:29 -080030import android.appwidget.AppWidgetHostView;
Romain Guy629de3e2010-01-13 12:20:59 -080031import android.appwidget.AppWidgetProviderInfo;
Adam Powell495f2892010-04-16 16:40:55 -070032import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.Context;
Michael Jurkaadc574c2013-09-12 00:05:02 +020034import android.content.SharedPreferences;
Patrick Dubroy7247f632010-08-04 16:02:59 -070035import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070037import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.graphics.Canvas;
Michael Jurkaa63c4522010-08-19 13:52:27 -070039import android.graphics.Matrix;
Winson Chungb8c69f32011-10-19 21:36:08 -070040import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080041import android.graphics.PointF;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Rect;
Joe Onorato4be866d2010-10-10 11:26:02 -070043import android.graphics.Region.Op;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070044import android.graphics.drawable.Drawable;
Adam Cohen4caf2982013-08-20 18:54:31 -070045import android.net.Uri;
Michael Jurkaa6a05472013-11-13 17:59:46 +010046import android.os.AsyncTask;
Joe Onorato956091b2010-02-19 12:47:40 -080047import android.os.IBinder;
Adam Powell495f2892010-04-16 16:40:55 -070048import android.os.Parcelable;
Adam Cohen53805212013-10-01 10:39:23 -070049import android.support.v4.view.ViewCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.util.AttributeSet;
Daniel Sandler291ad122010-05-24 16:03:53 -040051import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070052import android.util.SparseArray;
Michael Jurkacc07e7a2013-08-26 20:56:35 +020053import android.view.Choreographer;
Winson Chunga34abf82010-11-12 12:10:35 -080054import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.View;
Winson Chung6e314082011-01-27 16:46:51 -080057import android.view.ViewGroup;
Adam Cohen53805212013-10-01 10:39:23 -070058import android.view.accessibility.AccessibilityManager;
Patrick Dubroycd68ff52010-10-28 17:57:05 -070059import android.view.animation.DecelerateInterpolator;
Michael Jurkacc07e7a2013-08-26 20:56:35 +020060import android.view.animation.Interpolator;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070061import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Daniel Sandler325dc232013-06-05 22:57:57 -040063import com.android.launcher3.FolderIcon.FolderRingAnimator;
Adam Cohenbffe7452013-07-22 18:21:45 -070064import com.android.launcher3.Launcher.CustomContentCallbacks;
Daniel Sandler325dc232013-06-05 22:57:57 -040065import com.android.launcher3.LauncherSettings.Favorites;
Romain Guyedcce092010-03-04 13:03:17 -080066
Adam Cohen716b51e2011-06-30 12:09:54 -070067import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070068import java.util.HashMap;
Adam Cohen716b51e2011-06-30 12:09:54 -070069import java.util.HashSet;
Michael Jurka8bd65f52012-06-25 14:56:22 -070070import java.util.Iterator;
Adam Cohen716b51e2011-06-30 12:09:54 -070071
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072/**
Michael Jurka0142d492010-08-25 17:46:15 -070073 * The workspace is a wide area with a wallpaper and a finite number of pages.
74 * Each page contains a number of icons, folders or widgets the user can
Winson Chungaafa03c2010-06-11 17:34:16 -070075 * interact with. A workspace is meant to be used with a fixed width only.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 */
Michael Jurka0142d492010-08-25 17:46:15 -070077public class Workspace extends SmoothPagedView
Michael Jurkad74c9842011-07-10 12:44:21 -070078 implements DropTarget, DragSource, DragScroller, View.OnTouchListener,
John Spurlock77e1f472013-09-11 10:09:51 -040079 DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener,
80 Insettable {
Joe Onorato3a8820b2009-11-10 15:06:42 -080081 private static final String TAG = "Launcher.Workspace";
Michael Jurka0142d492010-08-25 17:46:15 -070082
Adam Cohenf34bab52010-09-30 14:11:56 -070083 // Y rotation to apply to the workspace screens
Adam Cohenb5ba0972011-09-07 18:02:31 -070084 private static final float WORKSPACE_OVERSCROLL_ROTATION = 24f;
Adam Cohena985e592010-09-09 11:23:48 -070085
Adam Cohen68d73932010-11-15 10:50:58 -080086 private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0;
87 private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375;
Winson Chung9171e6d2010-11-17 17:39:27 -080088 private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100;
Adam Cohenf34bab52010-09-30 14:11:56 -070089
Adam Cohenad4e15c2013-10-17 16:21:35 -070090 protected static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400;
91 protected static final int FADE_EMPTY_SCREEN_DURATION = 150;
92
Winson Chungf135c6c2010-11-18 16:32:08 -080093 private static final int BACKGROUND_FADE_OUT_DURATION = 350;
Adam Cohened51cc92011-08-01 20:28:08 -070094 private static final int ADJACENT_SCREEN_DROP_DURATION = 300;
Adam Cohen265b9a62011-12-07 14:37:18 -080095 private static final int FLING_THRESHOLD_VELOCITY = 500;
Adam Cohened51cc92011-08-01 20:28:08 -070096
Adam Cohenf358a4b2013-07-23 16:47:31 -070097 private static final float ALPHA_CUTOFF_THRESHOLD = 0.01f;
98
Winson Chung9171e6d2010-11-17 17:39:27 -080099 // These animators are used to fade the children's outlines
100 private ObjectAnimator mChildrenOutlineFadeInAnimation;
101 private ObjectAnimator mChildrenOutlineFadeOutAnimation;
102 private float mChildrenOutlineAlpha = 0;
103
104 // These properties refer to the background protection gradient used for AllApps and Customize
Michael Jurkae0f5a612011-02-07 16:45:41 -0800105 private ValueAnimator mBackgroundFadeInAnimation;
106 private ValueAnimator mBackgroundFadeOutAnimation;
Winson Chung9171e6d2010-11-17 17:39:27 -0800107 private Drawable mBackground;
Michael Jurka25356e72011-03-03 14:53:11 -0800108 boolean mDrawBackground = true;
Adam Cohenf34bab52010-09-30 14:11:56 -0700109 private float mBackgroundAlpha = 0;
110
Adam Cohen3d1b2b42013-08-14 15:57:58 -0700111 private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200;
112 private long mTouchDownTime = -1;
113 private long mCustomContentShowTime = -1;
114
Adam Cohen22cba7f2013-07-19 16:14:00 -0700115 private LayoutTransition mLayoutTransition;
Dianne Hackborn8f573952009-08-10 23:21:09 -0700116 private final WallpaperManager mWallpaperManager;
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800117 private IBinder mWindowToken;
Winson Chungaafa03c2010-06-11 17:34:16 -0700118
Winson Chung9e6a0a22013-08-27 11:58:12 -0700119 private int mOriginalDefaultPage;
Michael Jurka0142d492010-08-25 17:46:15 -0700120 private int mDefaultPage;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohen5084cba2013-09-03 12:01:16 -0700122 private ShortcutAndWidgetContainer mDragSourceInternal;
Adam Cohen53805212013-10-01 10:39:23 -0700123 private static boolean sAccessibilityEnabled;
Adam Cohen5084cba2013-09-03 12:01:16 -0700124
Adam Cohendcd297f2013-06-18 13:13:40 -0700125 // The screen id used for the empty screen always present to the right.
126 private final static long EXTRA_EMPTY_SCREEN_ID = -201;
127 private final static long CUSTOM_CONTENT_SCREEN_ID = -301;
128
129 private HashMap<Long, CellLayout> mWorkspaceScreens = new HashMap<Long, CellLayout>();
130 private ArrayList<Long> mScreenOrder = new ArrayList<Long>();
131
Adam Cohenad4e15c2013-10-17 16:21:35 -0700132 private Runnable mRemoveEmptyScreenRunnable;
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 /**
135 * CellInfo for the cell that is currently being dragged
136 */
137 private CellLayout.CellInfo mDragInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -0700138
Jeff Sharkey70864282009-04-07 21:08:40 -0700139 /**
140 * Target drop area calculated during last acceptDrop call.
141 */
Adam Cohenc0dcf592011-06-01 15:30:43 -0700142 private int[] mTargetCell = new int[2];
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700143 private int mDragOverX = -1;
144 private int mDragOverY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Adam Cohena897f392012-04-27 18:12:05 -0700146 static Rect mLandscapeCellLayoutMetrics = null;
147 static Rect mPortraitCellLayoutMetrics = null;
148
Adam Cohenbffe7452013-07-22 18:21:45 -0700149 CustomContentCallbacks mCustomContentCallbacks;
150 boolean mCustomContentShowing;
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700151 private float mLastCustomContentScrollProgress = -1f;
Adam Cohen53805212013-10-01 10:39:23 -0700152 private String mCustomContentDescription = "";
Adam Cohenbffe7452013-07-22 18:21:45 -0700153
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700154 /**
155 * The CellLayout that is currently being dragged over
156 */
157 private CellLayout mDragTargetLayout = null;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700158 /**
159 * The CellLayout that we will show as glowing
160 */
161 private CellLayout mDragOverlappingLayout = null;
162
163 /**
164 * The CellLayout which will be dropped to
165 */
166 private CellLayout mDropToLayout = null;
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Launcher mLauncher;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800169 private IconCache mIconCache;
Joe Onorato00acb122009-08-04 16:04:30 -0400170 private DragController mDragController;
Winson Chungaafa03c2010-06-11 17:34:16 -0700171
Michael Jurka4516c112010-10-07 15:13:47 -0700172 // These are temporary variables to prevent having to allocate a new object just to
173 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private int[] mTempCell = new int[2];
Adam Cohen7d30a372013-07-01 17:03:59 -0700175 private int[] mTempPt = new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700176 private int[] mTempEstimate = new int[2];
Adam Cohene3e27a82011-04-15 12:07:39 -0700177 private float[] mDragViewVisualCenter = new float[2];
Michael Jurka4516c112010-10-07 15:13:47 -0700178 private float[] mTempCellLayoutCenterCoordinates = new float[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700179 private Matrix mTempInverseMatrix = new Matrix();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Michael Jurkac2f7f472010-12-14 15:34:42 -0800181 private SpringLoadedDragController mSpringLoadedDragController;
Winson Chungb26f3d62011-06-02 10:49:29 -0700182 private float mSpringLoadedShrinkFactor;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700183 private float mOverviewModeShrinkFactor;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800184
Patrick Dubroy1262e362010-10-06 15:49:50 -0700185 // State variable that indicates whether the pages are small (ie when you're
Michael Jurkadee05892010-07-27 10:01:56 -0700186 // in all apps or customize mode)
Michael Jurkad74c9842011-07-10 12:44:21 -0700187
Adam Cohenf358a4b2013-07-23 16:47:31 -0700188 enum State { NORMAL, SPRING_LOADED, SMALL, OVERVIEW};
Adam Cohen7777d962011-08-18 18:58:38 -0700189 private State mState = State.NORMAL;
Michael Jurkad74c9842011-07-10 12:44:21 -0700190 private boolean mIsSwitchingState = false;
Michael Jurkad74c9842011-07-10 12:44:21 -0700191
Michael Jurkad74c9842011-07-10 12:44:21 -0700192 boolean mAnimatingViewIntoPlace = false;
193 boolean mIsDragOccuring = false;
194 boolean mChildrenLayersEnabled = true;
Michael Jurkadee05892010-07-27 10:01:56 -0700195
Adam Cohenaccfd562013-07-12 14:40:40 -0700196 private boolean mStripScreensOnPageStopMoving = false;
197
Patrick Dubroy54fa3b92010-11-17 12:18:45 -0800198 /** Is the user is dragging an item near the edge of a page? */
Patrick Dubroy1262e362010-10-06 15:49:50 -0700199 private boolean mInScrollArea = false;
200
Daniel Sandlere4f98912013-06-25 15:13:26 -0400201 private HolographicOutlineHelper mOutlineHelper;
Joe Onorato4be866d2010-10-10 11:26:02 -0700202 private Bitmap mDragOutline = null;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700203 private final Rect mTempRect = new Rect();
204 private final int[] mTempXY = new int[2];
Michael Jurkad51f33a2012-06-28 15:35:26 -0700205 private int[] mTempVisiblePagesRange = new int[2];
Michael Jurkab06d95f2012-04-02 06:26:53 -0700206 private boolean mOverscrollTransformsSet;
Adam Cohena29f5042013-09-26 14:29:35 -0700207 private float mLastOverscrollPivotX;
Michael Jurkaf8304f02012-04-26 13:33:26 -0700208 public static final int DRAG_BITMAP_PADDING = 2;
Michael Jurka869390b2012-05-06 15:55:19 -0700209 private boolean mWorkspaceFadeInAdjacentScreens;
Joe Onorato4be866d2010-10-10 11:26:02 -0700210
Michael Jurkaab1983f2011-01-18 15:50:17 -0800211 WallpaperOffsetInterpolator mWallpaperOffset;
Michael Jurka2a4f4922014-01-29 16:32:39 +0100212 private boolean mWallpaperIsLiveWallpaper;
213 private int mNumPagesForWallpaperParallax;
214 private float mLastSetWallpaperOffsetSteps = 0;
215
Adam Cohen26976d92011-03-22 15:33:33 -0700216 private Runnable mDelayedResizeRunnable;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700217 private Runnable mDelayedSnapToPageRunnable;
Michael Jurka84f2ce72012-04-13 15:08:01 -0700218 private Point mDisplaySize = new Point();
Adam Cohen94309882012-06-08 16:24:56 -0700219 private int mCameraDistance;
Michael Jurkac5b262c2011-01-12 20:24:50 -0800220
Adam Cohen19072da2011-05-31 14:30:45 -0700221 // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
Adam Cohen482ed822012-03-02 14:15:13 -0800222 private static final int FOLDER_CREATION_TIMEOUT = 0;
Adam Cohenfa3c58f2013-12-06 16:10:55 -0800223 public static final int REORDER_TIMEOUT = 350;
Adam Cohen19072da2011-05-31 14:30:45 -0700224 private final Alarm mFolderCreationAlarm = new Alarm();
Adam Cohen482ed822012-03-02 14:15:13 -0800225 private final Alarm mReorderAlarm = new Alarm();
Adam Cohen19072da2011-05-31 14:30:45 -0700226 private FolderRingAnimator mDragFolderRingAnimator = null;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700227 private FolderIcon mDragOverFolderIcon = null;
Adam Cohen19072da2011-05-31 14:30:45 -0700228 private boolean mCreateUserFolderOnDrop = false;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700229 private boolean mAddToExistingFolderOnDrop = false;
230 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen3aff81c2012-05-16 21:01:01 -0700231 private float mMaxDistanceForFolderCreation;
Adam Cohen073a46f2011-05-17 16:28:09 -0700232
Adam Cohenf8d28232011-02-01 21:47:00 -0800233 // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget)
234 private float mXDown;
235 private float mYDown;
236 final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
237 final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
238 final static float TOUCH_SLOP_DAMPING_FACTOR = 4;
239
Adam Cohened66b2b2012-01-23 17:28:51 -0800240 // Relating to the animation of items being dropped externally
Adam Cohend41fbf52012-02-16 23:53:59 -0800241 public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0;
242 public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1;
243 public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2;
244 public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3;
245 public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4;
Adam Cohened66b2b2012-01-23 17:28:51 -0800246
Adam Cohen482ed822012-03-02 14:15:13 -0800247 // Related to dragging, folder creation and reordering
248 private static final int DRAG_MODE_NONE = 0;
249 private static final int DRAG_MODE_CREATE_FOLDER = 1;
250 private static final int DRAG_MODE_ADD_TO_FOLDER = 2;
251 private static final int DRAG_MODE_REORDER = 3;
252 private int mDragMode = DRAG_MODE_NONE;
253 private int mLastReorderX = -1;
254 private int mLastReorderY = -1;
255
Adam Cohen1462de32012-07-24 22:34:36 -0700256 private SparseArray<Parcelable> mSavedStates;
257 private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>();
258
Adam Cohen4b285c52011-07-21 14:24:06 -0700259 // These variables are used for storing the initial and final values during workspace animations
Adam Cohened51cc92011-08-01 20:28:08 -0700260 private int mSavedScrollX;
261 private float mSavedRotationY;
262 private float mSavedTranslationX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700263
264 private float mCurrentScale;
265 private float mNewScale;
Adam Cohen4b285c52011-07-21 14:24:06 -0700266 private float[] mOldBackgroundAlphas;
Adam Cohen4b285c52011-07-21 14:24:06 -0700267 private float[] mOldAlphas;
Adam Cohen4b285c52011-07-21 14:24:06 -0700268 private float[] mNewBackgroundAlphas;
Adam Cohen4b285c52011-07-21 14:24:06 -0700269 private float[] mNewAlphas;
Adam Cohendcd297f2013-06-18 13:13:40 -0700270 private int mLastChildCount = -1;
Winson Chung70442722012-02-10 15:43:22 -0800271 private float mTransitionProgress;
Adam Cohen4b285c52011-07-21 14:24:06 -0700272
Michael Jurka1e2f4652013-07-08 18:03:46 -0700273 private Runnable mDeferredAction;
274 private boolean mDeferDropAfterUninstall;
275 private boolean mUninstallSuccessful;
276
Romain Guyeeacd562012-10-10 18:47:33 -0700277 private final Runnable mBindPages = new Runnable() {
278 @Override
279 public void run() {
280 mLauncher.getModel().bindRemainingSynchronousPages();
281 }
282 };
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 /**
285 * Used to inflate the Workspace from XML.
286 *
287 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700288 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 */
290 public Workspace(Context context, AttributeSet attrs) {
291 this(context, attrs, 0);
292 }
293
294 /**
295 * Used to inflate the Workspace from XML.
296 *
297 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700298 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 * @param defStyle Unused.
300 */
301 public Workspace(Context context, AttributeSet attrs, int defStyle) {
302 super(context, attrs, defStyle);
Michael Jurka0142d492010-08-25 17:46:15 -0700303 mContentIsRefreshable = false;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700304
Daniel Sandlere4f98912013-06-25 15:13:26 -0400305 mOutlineHelper = HolographicOutlineHelper.obtain(context);
306
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700307 mDragEnforcer = new DropTarget.DragEnforcer(context);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700308 // With workspace, data is available straight from the get-go
309 setDataIsReady();
310
Michael Jurka8bc66c72012-06-21 08:36:45 -0700311 mLauncher = (Launcher) context;
Winson Chung867ca622012-02-21 15:48:35 -0800312 final Resources res = getResources();
Adam Cohen3b185e22013-10-29 14:45:58 -0700313 mWorkspaceFadeInAdjacentScreens = LauncherAppState.getInstance().getDynamicGrid().
314 getDeviceProfile().shouldFadeAdjacentWorkspaceScreens();
Michael Jurka869390b2012-05-06 15:55:19 -0700315 mFadeInAdjacentScreens = false;
Dianne Hackborn8f573952009-08-10 23:21:09 -0700316 mWallpaperManager = WallpaperManager.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700317
Winson Chungc82d2622013-11-06 13:23:29 -0800318 LauncherAppState app = LauncherAppState.getInstance();
319 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungaafa03c2010-06-11 17:34:16 -0700320 TypedArray a = context.obtainStyledAttributes(attrs,
321 R.styleable.Workspace, defStyle, 0);
Winson Chungb26f3d62011-06-02 10:49:29 -0700322 mSpringLoadedShrinkFactor =
323 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
Winson Chungc82d2622013-11-06 13:23:29 -0800324 mOverviewModeShrinkFactor = grid.getOverviewModeScale();
Adam Cohen94309882012-06-08 16:24:56 -0700325 mCameraDistance = res.getInteger(R.integer.config_cameraDistance);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700326 mOriginalDefaultPage = mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 a.recycle();
328
Michael Jurka8b805b12012-04-18 14:23:14 -0700329 setOnHierarchyChangeListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800330 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 initWorkspace();
Winson Chungc35afb22011-02-23 13:01:49 -0800333
334 // Disable multitouch across the workspace/all apps/customize tray
335 setMotionEventSplittingEnabled(true);
Adam Cohen53805212013-10-01 10:39:23 -0700336 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
338
John Spurlock77e1f472013-09-11 10:09:51 -0400339 @Override
340 public void setInsets(Rect insets) {
341 mInsets.set(insets);
Adam Cohen6400b842013-11-26 15:45:38 -0800342
343 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
344 if (customScreen != null) {
345 View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0);
346 if (customContent instanceof Insettable) {
347 ((Insettable) customContent).setInsets(mInsets);
348 }
349 }
John Spurlock77e1f472013-09-11 10:09:51 -0400350 }
351
Michael Jurka038f9d82011-11-03 13:50:45 -0700352 // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each
353 // dimension if unsuccessful
354 public int[] estimateItemSize(int hSpan, int vSpan,
Adam Cohend41fbf52012-02-16 23:53:59 -0800355 ItemInfo itemInfo, boolean springLoaded) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700356 int[] size = new int[2];
357 if (getChildCount() > 0) {
Winson Chungad7db6e2013-10-08 14:01:06 -0700358 // Use the first non-custom page to estimate the child position
359 CellLayout cl = (CellLayout) getChildAt(numCustomPages());
Adam Cohend41fbf52012-02-16 23:53:59 -0800360 Rect r = estimateItemPosition(cl, itemInfo, 0, 0, hSpan, vSpan);
361 size[0] = r.width();
362 size[1] = r.height();
Michael Jurka038f9d82011-11-03 13:50:45 -0700363 if (springLoaded) {
364 size[0] *= mSpringLoadedShrinkFactor;
365 size[1] *= mSpringLoadedShrinkFactor;
366 }
367 return size;
368 } else {
369 size[0] = Integer.MAX_VALUE;
370 size[1] = Integer.MAX_VALUE;
371 return size;
372 }
373 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700374
Adam Cohend41fbf52012-02-16 23:53:59 -0800375 public Rect estimateItemPosition(CellLayout cl, ItemInfo pendingInfo,
Michael Jurka038f9d82011-11-03 13:50:45 -0700376 int hCell, int vCell, int hSpan, int vSpan) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800377 Rect r = new Rect();
Michael Jurka038f9d82011-11-03 13:50:45 -0700378 cl.cellToRect(hCell, vCell, hSpan, vSpan, r);
Michael Jurka038f9d82011-11-03 13:50:45 -0700379 return r;
380 }
381
Adam Cohen5084cba2013-09-03 12:01:16 -0700382 public void onDragStart(final DragSource source, Object info, int dragAction) {
Michael Jurkad74c9842011-07-10 12:44:21 -0700383 mIsDragOccuring = true;
Michael Jurka3a0469d2012-06-21 09:38:41 -0700384 updateChildrenLayersEnabled(false);
Winson Chung641d71d2012-04-26 15:58:01 -0700385 mLauncher.lockScreenOrientation();
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -0700386 mLauncher.onInteractionBegin();
Michael Jurkaa3d30ad2012-05-08 13:43:43 -0700387 setChildrenBackgroundAlphaMultipliers(1f);
Winson Chungf561bdf2012-05-03 11:20:19 -0700388 // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging
389 InstallShortcutReceiver.enableInstallQueue();
390 UninstallShortcutReceiver.enableUninstallQueue();
Adam Cohen5084cba2013-09-03 12:01:16 -0700391 post(new Runnable() {
392 @Override
393 public void run() {
394 if (mIsDragOccuring) {
395 addExtraEmptyScreenOnDrag();
396 }
397 }
398 });
Michael Jurkad74c9842011-07-10 12:44:21 -0700399 }
400
401 public void onDragEnd() {
402 mIsDragOccuring = false;
Michael Jurka3a0469d2012-06-21 09:38:41 -0700403 updateChildrenLayersEnabled(false);
Winson Chung4b919f82012-05-01 10:44:08 -0700404 mLauncher.unlockScreenOrientation(false);
Winson Chungf561bdf2012-05-03 11:20:19 -0700405
406 // Re-enable any Un/InstallShortcutReceiver and now process any queued items
407 InstallShortcutReceiver.disableAndFlushInstallQueue(getContext());
408 UninstallShortcutReceiver.disableAndFlushUninstallQueue(getContext());
Adam Cohen5084cba2013-09-03 12:01:16 -0700409
Adam Cohen5084cba2013-09-03 12:01:16 -0700410 mDragSourceInternal = null;
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -0700411 mLauncher.onInteractionEnd();
Michael Jurkad74c9842011-07-10 12:44:21 -0700412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 /**
415 * Initializes various states for this workspace.
416 */
Michael Jurka0142d492010-08-25 17:46:15 -0700417 protected void initWorkspace() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 Context context = getContext();
Michael Jurka0142d492010-08-25 17:46:15 -0700419 mCurrentPage = mDefaultPage;
420 Launcher.setScreen(mCurrentPage);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700422 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mIconCache = app.getIconCache();
Patrick Dubroycd68ff52010-10-28 17:57:05 -0700424 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800425 setClipChildren(false);
426 setClipToPadding(false);
Adam Cohen7777d962011-08-18 18:58:38 -0700427 setChildrenDrawnWithCacheEnabled(true);
Derek Prothrodadd9842014-01-17 13:43:50 -0500428
Winson Chungc82d2622013-11-06 13:23:29 -0800429 setMinScale(mOverviewModeShrinkFactor);
Adam Cohen22cba7f2013-07-19 16:14:00 -0700430 setupLayoutTransition();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Adam Cohen482ed822012-03-02 14:15:13 -0800432 final Resources res = getResources();
Winson Chungb4b7fa72010-11-18 14:38:53 -0800433 try {
Winson Chungfd3385f2011-06-15 19:51:24 -0700434 mBackground = res.getDrawable(R.drawable.apps_customize_bg);
Winson Chungb4b7fa72010-11-18 14:38:53 -0800435 } catch (Resources.NotFoundException e) {
436 // In this case, we will skip drawing background protection
437 }
Winson Chung9171e6d2010-11-17 17:39:27 -0800438
Michael Jurkaab1983f2011-01-18 15:50:17 -0800439 mWallpaperOffset = new WallpaperOffsetInterpolator();
Adam Cohencff6af82011-09-13 14:51:53 -0700440 Display display = mLauncher.getWindowManager().getDefaultDisplay();
Michael Jurka84f2ce72012-04-13 15:08:01 -0700441 display.getSize(mDisplaySize);
Adam Cohen265b9a62011-12-07 14:37:18 -0800442
Winson Chung5f8afe62013-08-12 16:19:28 -0700443 mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx);
Adam Cohen265b9a62011-12-07 14:37:18 -0800444 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
Michael Jurkaa6a05472013-11-13 17:59:46 +0100445
446 // Set the wallpaper dimensions when Launcher starts up
447 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 }
449
Adam Cohen22cba7f2013-07-19 16:14:00 -0700450 private void setupLayoutTransition() {
451 // We want to show layout transitions when pages are deleted, to close the gap.
452 mLayoutTransition = new LayoutTransition();
453 mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING);
454 mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
455 mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING);
456 mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
457 setLayoutTransition(mLayoutTransition);
458 }
459
Winson Chung964df6b2013-10-11 15:55:37 -0700460 void enableLayoutTransitions() {
461 setLayoutTransition(mLayoutTransition);
462 }
463 void disableLayoutTransitions() {
464 setLayoutTransition(null);
465 }
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 @Override
Adam Cohenf34bab52010-09-30 14:11:56 -0700468 protected int getScrollMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700469 return SmoothPagedView.X_LARGE_MODE;
Adam Cohenf34bab52010-09-30 14:11:56 -0700470 }
471
Michael Jurka08ee7702011-08-11 16:53:35 -0700472 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700473 public void onChildViewAdded(View parent, View child) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 if (!(child instanceof CellLayout)) {
475 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
476 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 CellLayout cl = ((CellLayout) child);
478 cl.setOnInterceptTouchListener(this);
Adam Cohen2801caf2011-05-13 20:57:39 -0700479 cl.setClickable(true);
Adam Cohen53805212013-10-01 10:39:23 -0700480 cl.setImportantForAccessibility(ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
Winson Chungf70696d2013-06-25 16:19:53 -0700481 super.onChildViewAdded(parent, child);
Michael Jurka8b805b12012-04-18 14:23:14 -0700482 }
483
Michael Jurka920d7f42012-05-14 16:29:55 -0700484 protected boolean shouldDrawChild(View child) {
485 final CellLayout cl = (CellLayout) child;
486 return super.shouldDrawChild(child) &&
Winson Chungf4bd2362013-10-07 17:11:27 -0700487 (mIsSwitchingState ||
488 cl.getShortcutsAndWidgets().getAlpha() > 0 ||
Michael Jurka920d7f42012-05-14 16:29:55 -0700489 cl.getBackgroundAlpha() > 0);
490 }
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 /**
493 * @return The open folder on the current screen, or null if there is none
494 */
495 Folder getOpenFolder() {
Adam Cohen716b51e2011-06-30 12:09:54 -0700496 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohen8e776a62011-06-28 18:10:06 -0700497 int count = dragLayer.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 for (int i = 0; i < count; i++) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700499 View child = dragLayer.getChildAt(i);
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 if (child instanceof Folder) {
501 Folder folder = (Folder) child;
502 if (folder.getInfo().opened)
503 return folder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
505 }
506 return null;
507 }
508
Patrick Dubroya0aa0122011-02-24 11:42:23 -0800509 boolean isTouchActive() {
510 return mTouchState != TOUCH_STATE_REST;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 }
512
Winson Chung9e6a0a22013-08-27 11:58:12 -0700513 public void removeAllWorkspaceScreens() {
Winson Chung964df6b2013-10-11 15:55:37 -0700514 // Disable all layout transitions before removing all pages to ensure that we don't get the
515 // transition animations competing with us changing the scroll when we add pages or the
516 // custom content screen
517 disableLayoutTransitions();
518
519 // Since we increment the current page when we call addCustomContentPage via bindScreens
520 // (and other places), we need to adjust the current page back when we clear the pages
521 if (hasCustomContent()) {
522 removeCustomContentPage();
523 }
524
Winson Chung9e6a0a22013-08-27 11:58:12 -0700525 // Remove the pages and clear the screen models
526 removeAllViews();
527 mScreenOrder.clear();
528 mWorkspaceScreens.clear();
Winson Chung964df6b2013-10-11 15:55:37 -0700529
530 // Re-enable the layout transitions
531 enableLayoutTransitions();
Winson Chung9e6a0a22013-08-27 11:58:12 -0700532 }
533
Adam Cohen89bddfa2013-08-20 11:57:13 -0700534 public long insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) {
Winson Chung64359a52013-07-08 17:17:08 -0700535 // Find the index to insert this view into. If the empty screen exists, then
536 // insert it before that.
537 int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
538 if (insertIndex < 0) {
539 insertIndex = mScreenOrder.size();
540 }
Adam Cohen89bddfa2013-08-20 11:57:13 -0700541 return insertNewWorkspaceScreen(screenId, insertIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700542 }
543
Adam Cohen89bddfa2013-08-20 11:57:13 -0700544 public long insertNewWorkspaceScreen(long screenId) {
545 return insertNewWorkspaceScreen(screenId, getChildCount());
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
547
Adam Cohen89bddfa2013-08-20 11:57:13 -0700548 public long insertNewWorkspaceScreen(long screenId, int insertIndex) {
Winson Chunga90303b2013-11-15 13:05:06 -0800549 // Log to disk
550 Launcher.addDumpLog(TAG, "11683562 - insertNewWorkspaceScreen(): " + screenId +
551 " at index: " + insertIndex, true);
552
Adam Cohen5084cba2013-09-03 12:01:16 -0700553 if (mWorkspaceScreens.containsKey(screenId)) {
554 throw new RuntimeException("Screen id " + screenId + " already exists!");
555 }
556
Adam Cohendcd297f2013-06-18 13:13:40 -0700557 CellLayout newScreen = (CellLayout)
558 mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null);
559
Adam Cohen7d30a372013-07-01 17:03:59 -0700560 newScreen.setOnLongClickListener(mLongClickListener);
Adam Cohenf358a4b2013-07-23 16:47:31 -0700561 newScreen.setOnClickListener(mLauncher);
Winson Chung837b2412013-09-09 14:43:51 -0700562 newScreen.setSoundEffectsEnabled(false);
Adam Cohendcd297f2013-06-18 13:13:40 -0700563 mWorkspaceScreens.put(screenId, newScreen);
Winson Chung64359a52013-07-08 17:17:08 -0700564 mScreenOrder.add(insertIndex, screenId);
565 addView(newScreen, insertIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700566 return screenId;
567 }
568
Derek Prothrodadd9842014-01-17 13:43:50 -0500569 public void createCustomContentContainer() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700570 CellLayout customScreen = (CellLayout)
Adam Cohen41eb4702013-06-27 15:08:59 -0700571 mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null);
Winson Chung59a488a2013-12-10 12:32:14 -0800572 customScreen.disableBackground();
Adam Cohendcd297f2013-06-18 13:13:40 -0700573
Adam Cohendcd297f2013-06-18 13:13:40 -0700574 mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
575 mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);
Adam Cohenedb40762013-07-18 16:45:45 -0700576
Adam Cohen2702ea02013-08-15 16:17:42 -0700577 // We want no padding on the custom content
578 customScreen.setPadding(0, 0, 0, 0);
579
Adam Cohenedb40762013-07-18 16:45:45 -0700580 addFullScreenPage(customScreen);
Michael Jurkaee0ce2b2013-07-02 17:24:35 -0700581
Adam Cohendcd297f2013-06-18 13:13:40 -0700582 // Ensure that the current page and default page are maintained.
Winson Chung9e6a0a22013-08-27 11:58:12 -0700583 mDefaultPage = mOriginalDefaultPage + 1;
Winson Chung82e5c982013-10-09 12:04:50 -0700584
585 // Update the custom content hint
Winson Chunga6945242014-01-08 14:04:34 -0800586 mLauncher.getLauncherClings().updateCustomContentHintVisibility();
Adam Cohen21cd0022013-10-09 18:57:02 -0700587 if (mRestorePage != INVALID_RESTORE_PAGE) {
588 mRestorePage = mRestorePage + 1;
589 } else {
590 setCurrentPage(getCurrentPage() + 1);
591 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700592 }
593
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 public void removeCustomContentPage() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
596 if (customScreen == null) {
597 throw new RuntimeException("Expected custom content screen to exist");
598 }
599
600 mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID);
601 mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID);
602 removeView(customScreen);
Adam Cohenbb6fda62013-10-10 12:48:52 -0700603
604 if (mCustomContentCallbacks != null) {
605 mCustomContentCallbacks.onScrollProgressChanged(0);
606 mCustomContentCallbacks.onHide();
607 }
608
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 mCustomContentCallbacks = null;
610
611 // Ensure that the current page and default page are maintained.
612 mDefaultPage = mOriginalDefaultPage - 1;
Winson Chung82e5c982013-10-09 12:04:50 -0700613
614 // Update the custom content hint
Winson Chunga6945242014-01-08 14:04:34 -0800615 mLauncher.getLauncherClings().updateCustomContentHintVisibility();
Adam Cohen21cd0022013-10-09 18:57:02 -0700616 if (mRestorePage != INVALID_RESTORE_PAGE) {
617 mRestorePage = mRestorePage - 1;
618 } else {
619 setCurrentPage(getCurrentPage() - 1);
620 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 }
622
Adam Cohen53805212013-10-01 10:39:23 -0700623 public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks,
624 String description) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700625 if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) {
626 throw new RuntimeException("Expected custom content screen to exist");
627 }
628
629 // Add the custom content to the full screen custom page
630 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
631 int spanX = customScreen.getCountX();
632 int spanY = customScreen.getCountY();
633 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
634 lp.canReorder = false;
635 lp.isFullscreen = true;
John Spurlock77e1f472013-09-11 10:09:51 -0400636 if (customContent instanceof Insettable) {
637 ((Insettable)customContent).setInsets(mInsets);
638 }
Adam Cohen166ebd42013-11-26 14:55:45 -0800639
640 // Verify that the child is removed from any existing parent.
641 if (customContent.getParent() instanceof ViewGroup) {
642 ViewGroup parent = (ViewGroup) customContent.getParent();
643 parent.removeView(customContent);
644 }
Adam Cohen225ad9b2013-10-07 13:03:00 -0700645 customScreen.removeAllViews();
Winson Chung98ca0f72013-07-29 12:58:51 -0700646 customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
Adam Cohen53805212013-10-01 10:39:23 -0700647 mCustomContentDescription = description;
Winson Chung98ca0f72013-07-29 12:58:51 -0700648
649 mCustomContentCallbacks = callbacks;
650 }
651
Adam Cohen5084cba2013-09-03 12:01:16 -0700652 public void addExtraEmptyScreenOnDrag() {
Winson Chunga90303b2013-11-15 13:05:06 -0800653 // Log to disk
654 Launcher.addDumpLog(TAG, "11683562 - addExtraEmptyScreenOnDrag()", true);
655
Adam Cohen5084cba2013-09-03 12:01:16 -0700656 boolean lastChildOnScreen = false;
657 boolean childOnFinalScreen = false;
658
Adam Cohenad4e15c2013-10-17 16:21:35 -0700659 // Cancel any pending removal of empty screen
660 mRemoveEmptyScreenRunnable = null;
661
Adam Cohen5084cba2013-09-03 12:01:16 -0700662 if (mDragSourceInternal != null) {
663 if (mDragSourceInternal.getChildCount() == 1) {
664 lastChildOnScreen = true;
665 }
666 CellLayout cl = (CellLayout) mDragSourceInternal.getParent();
667 if (indexOfChild(cl) == getChildCount() - 1) {
668 childOnFinalScreen = true;
669 }
670 }
671
672 // If this is the last item on the final screen
673 if (lastChildOnScreen && childOnFinalScreen) {
674 return;
675 }
676 if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
677 insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
678 }
679 }
680
681 public boolean addExtraEmptyScreen() {
Winson Chunga90303b2013-11-15 13:05:06 -0800682 // Log to disk
683 Launcher.addDumpLog(TAG, "11683562 - addExtraEmptyScreen()", true);
684
Adam Cohen5084cba2013-09-03 12:01:16 -0700685 if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
686 insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
687 return true;
688 }
689 return false;
690 }
691
Adam Cohenad4e15c2013-10-17 16:21:35 -0700692 private void convertFinalScreenToEmptyScreenIfNecessary() {
Winson Chunga90303b2013-11-15 13:05:06 -0800693 // Log to disk
694 Launcher.addDumpLog(TAG, "11683562 - convertFinalScreenToEmptyScreenIfNecessary()", true);
695
Adam Cohenad4e15c2013-10-17 16:21:35 -0700696 if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return;
697 long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1);
698
699 if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return;
700 CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId);
701
702 // If the final screen is empty, convert it to the extra empty screen
Adam Cohen917e3882013-10-31 15:03:35 -0700703 if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 &&
704 !finalScreen.isDropPending()) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700705 mWorkspaceScreens.remove(finalScreenId);
706 mScreenOrder.remove(finalScreenId);
707
708 // if this is the last non-custom content screen, convert it to the empty screen
709 mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen);
710 mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
Winson Chunga90303b2013-11-15 13:05:06 -0800711
Winson Chungf0716b72013-11-18 16:09:54 -0800712 // Update the model if we have changed any screens
713 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
Winson Chunga90303b2013-11-15 13:05:06 -0800714 Launcher.addDumpLog(TAG, "11683562 - extra empty screen: " + finalScreenId, true);
Adam Cohen5084cba2013-09-03 12:01:16 -0700715 }
716 }
717
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 public void removeExtraEmptyScreen(final boolean animate, final Runnable onComplete) {
719 removeExtraEmptyScreen(animate, onComplete, 0, false);
720 }
721
722 public void removeExtraEmptyScreen(final boolean animate, final Runnable onComplete,
723 final int delay, final boolean stripEmptyScreens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800724 // Log to disk
725 Launcher.addDumpLog(TAG, "11683562 - removeExtraEmptyScreen()", true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 if (delay > 0) {
727 postDelayed(new Runnable() {
728 @Override
729 public void run() {
730 removeExtraEmptyScreen(animate, onComplete, 0, stripEmptyScreens);
731 }
732
733 }, delay);
734 return;
735 }
736
737 convertFinalScreenToEmptyScreenIfNecessary();
738 if (hasExtraEmptyScreen()) {
739 int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
740 if (getNextPage() == emptyIndex) {
741 snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION);
742 fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION,
743 onComplete, stripEmptyScreens);
744 } else {
745 fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION,
746 onComplete, stripEmptyScreens);
747 }
748 return;
Adam Cohen3a14eeb2013-11-15 16:51:22 +0000749 } else if (stripEmptyScreens) {
750 // If we're not going to strip the empty screens after removing
751 // the extra empty screen, do it right away.
752 stripEmptyScreens();
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 }
Adam Cohen3a14eeb2013-11-15 16:51:22 +0000754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 if (onComplete != null) {
756 onComplete.run();
757 }
758 }
759
760 private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete,
761 final boolean stripEmptyScreens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800762 // Log to disk
763 // XXX: Do we need to update LM workspace screens below?
764 Launcher.addDumpLog(TAG, "11683562 - fadeAndRemoveEmptyScreen()", true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
766 PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f);
767
768 final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
769
770 mRemoveEmptyScreenRunnable = new Runnable() {
771 @Override
772 public void run() {
773 if (hasExtraEmptyScreen()) {
774 mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
775 mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
776 removeView(cl);
777 if (stripEmptyScreens) {
778 stripEmptyScreens();
779 }
780 }
781 }
782 };
783
784 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha);
785 oa.setDuration(duration);
786 oa.setStartDelay(delay);
787 oa.addListener(new AnimatorListenerAdapter() {
788 @Override
789 public void onAnimationEnd(Animator animation) {
790 if (mRemoveEmptyScreenRunnable != null) {
791 mRemoveEmptyScreenRunnable.run();
792 }
793 if (onComplete != null) {
794 onComplete.run();
795 }
796 }
797 });
798 oa.start();
799 }
800
Michael Jurka2f817312013-09-20 03:03:42 +0200801 public boolean hasExtraEmptyScreen() {
802 int nScreens = getChildCount();
Michael Jurkafe0ace32013-10-03 01:05:14 -0700803 nScreens = nScreens - numCustomPages();
Michael Jurka2f817312013-09-20 03:03:42 +0200804 return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1;
805 }
806
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 public long commitExtraEmptyScreen() {
Winson Chunga90303b2013-11-15 13:05:06 -0800808 // Log to disk
809 Launcher.addDumpLog(TAG, "11683562 - commitExtraEmptyScreen()", true);
810
Winson Chung89f97052013-09-20 11:32:26 -0700811 int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID);
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
Winson Chungc9168342013-06-26 14:54:55 -0700813 mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
Adam Cohendcd297f2013-06-18 13:13:40 -0700814 mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
815
Michael Jurka104c4562013-07-08 18:03:46 -0700816 long newId = LauncherAppState.getLauncherProvider().generateNewScreenId();
Adam Cohendcd297f2013-06-18 13:13:40 -0700817 mWorkspaceScreens.put(newId, cl);
818 mScreenOrder.add(newId);
819
Winson Chung89f97052013-09-20 11:32:26 -0700820 // Update the page indicator marker
821 if (getPageIndicator() != null) {
822 getPageIndicator().updateMarker(index, getPageIndicatorMarker(index));
823 }
824
Winson Chung64359a52013-07-08 17:17:08 -0700825 // Update the model for the new screen
826 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
827
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 return newId;
829 }
830
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 public CellLayout getScreenWithId(long screenId) {
832 CellLayout layout = mWorkspaceScreens.get(screenId);
833 return layout;
834 }
835
836 public long getIdForScreen(CellLayout layout) {
837 Iterator<Long> iter = mWorkspaceScreens.keySet().iterator();
838 while (iter.hasNext()) {
839 long id = iter.next();
840 if (mWorkspaceScreens.get(id) == layout) {
841 return id;
842 }
843 }
844 return -1;
845 }
846
847 public int getPageIndexForScreenId(long screenId) {
848 return indexOfChild(mWorkspaceScreens.get(screenId));
849 }
850
851 public long getScreenIdForPageIndex(int index) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700852 if (0 <= index && index < mScreenOrder.size()) {
853 return mScreenOrder.get(index);
854 }
855 return -1;
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 }
857
Winson Chungc9168342013-06-26 14:54:55 -0700858 ArrayList<Long> getScreenOrder() {
859 return mScreenOrder;
860 }
861
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 public void stripEmptyScreens() {
Winson Chunga90303b2013-11-15 13:05:06 -0800863 // Log to disk
864 Launcher.addDumpLog(TAG, "11683562 - stripEmptyScreens()", true);
865
Adam Cohen65e43032014-03-03 11:37:21 -0800866 if (mLauncher.isWorkspaceLoading()) {
Adam Cohen517a7f52014-03-01 12:12:59 -0800867 // Don't strip empty screens if the workspace is still loading
868 Launcher.addDumpLog(TAG, " - workspace loading, skip", true);
869 return;
870 }
871
Adam Cohenaccfd562013-07-12 14:40:40 -0700872 if (isPageMoving()) {
873 mStripScreensOnPageStopMoving = true;
874 return;
875 }
876
877 int currentPage = getNextPage();
Adam Cohendcd297f2013-06-18 13:13:40 -0700878 ArrayList<Long> removeScreens = new ArrayList<Long>();
879 for (Long id: mWorkspaceScreens.keySet()) {
880 CellLayout cl = mWorkspaceScreens.get(id);
Winson Chungc9168342013-06-26 14:54:55 -0700881 if (id >= 0 && cl.getShortcutsAndWidgets().getChildCount() == 0) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700882 removeScreens.add(id);
883 }
884 }
885
Adam Cohen5084cba2013-09-03 12:01:16 -0700886 // We enforce at least one page to add new items to. In the case that we remove the last
887 // such screen, we convert the last screen to the empty screen
Michael Jurkafe0ace32013-10-03 01:05:14 -0700888 int minScreens = 1 + numCustomPages();
Adam Cohen5084cba2013-09-03 12:01:16 -0700889
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 int pageShift = 0;
891 for (Long id: removeScreens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800892 Launcher.addDumpLog(TAG, "11683562 - removing id: " + id, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 CellLayout cl = mWorkspaceScreens.get(id);
894 mWorkspaceScreens.remove(id);
895 mScreenOrder.remove(id);
Adam Cohen5084cba2013-09-03 12:01:16 -0700896
897 if (getChildCount() > minScreens) {
898 if (indexOfChild(cl) < currentPage) {
899 pageShift++;
900 }
901 removeView(cl);
902 } else {
903 // if this is the last non-custom content screen, convert it to the empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 mRemoveEmptyScreenRunnable = null;
Adam Cohen5084cba2013-09-03 12:01:16 -0700905 mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl);
906 mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
Adam Cohendcd297f2013-06-18 13:13:40 -0700907 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700908 }
Winson Chung64359a52013-07-08 17:17:08 -0700909
910 if (!removeScreens.isEmpty()) {
911 // Update the model if we have changed any screens
912 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
913 }
Adam Cohenaccfd562013-07-12 14:40:40 -0700914
915 if (pageShift >= 0) {
916 setCurrentPage(currentPage - pageShift);
917 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 }
919
920 // See implementation for parameter definition.
921 void addInScreen(View child, long container, long screenId,
922 int x, int y, int spanX, int spanY) {
923 addInScreen(child, container, screenId, x, y, spanX, spanY, false, false);
924 }
925
926 // At bind time, we use the rank (screenId) to compute x and y for hotseat items.
927 // See implementation for parameter definition.
928 void addInScreenFromBind(View child, long container, long screenId, int x, int y,
929 int spanX, int spanY) {
930 addInScreen(child, container, screenId, x, y, spanX, spanY, false, true);
931 }
932
933 // See implementation for parameter definition.
934 void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
935 boolean insert) {
936 addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false);
Winson Chungaafa03c2010-06-11 17:34:16 -0700937 }
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 /**
940 * Adds the specified child in the specified screen. The position and dimension of
941 * the child are defined by x, y, spanX and spanY.
942 *
943 * @param child The child to add in one of the workspace's screens.
Adam Cohendcd297f2013-06-18 13:13:40 -0700944 * @param screenId The screen in which to add the child.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 * @param x The X position of the child in the screen's grid.
946 * @param y The Y position of the child in the screen's grid.
947 * @param spanX The number of cells spanned horizontally by the child.
948 * @param spanY The number of cells spanned vertically by the child.
949 * @param insert When true, the child is inserted at the beginning of the children list.
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute
951 * the x and y position in which to place hotseat items. Otherwise
952 * we use the x and y position to compute the rank.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 */
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
955 boolean insert, boolean computeXYFromRank) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 if (getScreenWithId(screenId) == null) {
958 Log.e(TAG, "Skipping child, screenId " + screenId + " not found");
Winson Chung8481e322013-08-09 16:06:38 -0700959 // DEBUGGING - Print out the stack trace to see where we are adding from
960 new Throwable().printStackTrace();
Winson Chung3d503fb2011-07-13 17:25:49 -0700961 return;
962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 if (screenId == EXTRA_EMPTY_SCREEN_ID) {
Adam Cohendedbd962013-07-11 14:21:49 -0700965 // This should never happen
966 throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 }
968
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 final CellLayout layout;
970 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
971 layout = mLauncher.getHotseat().getLayout();
Winson Chung4d279d92011-07-21 11:46:32 -0700972 child.setOnKeyListener(null);
Winson Chung3d503fb2011-07-13 17:25:49 -0700973
Adam Cohen099f60d2011-08-23 21:07:26 -0700974 // Hide folder title in the hotseat
975 if (child instanceof FolderIcon) {
976 ((FolderIcon) child).setTextVisible(false);
977 }
978
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 if (computeXYFromRank) {
980 x = mLauncher.getHotseat().getCellXFromOrder((int) screenId);
981 y = mLauncher.getHotseat().getCellYFromOrder((int) screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -0700982 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 screenId = mLauncher.getHotseat().getOrderInHotseat(x, y);
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 }
985 } else {
Adam Cohen099f60d2011-08-23 21:07:26 -0700986 // Show folder title if not in the hotseat
987 if (child instanceof FolderIcon) {
988 ((FolderIcon) child).setTextVisible(true);
989 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 layout = getScreenWithId(screenId);
Adam Cohenac56cff2011-09-28 20:45:37 -0700991 child.setOnKeyListener(new IconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 }
993
Adam Cohen96d30a12013-07-16 18:13:21 -0700994 ViewGroup.LayoutParams genericLp = child.getLayoutParams();
Adam Cohened66b2b2012-01-23 17:28:51 -0800995 CellLayout.LayoutParams lp;
996 if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
998 } else {
Adam Cohened66b2b2012-01-23 17:28:51 -0800999 lp = (CellLayout.LayoutParams) genericLp;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 lp.cellX = x;
1001 lp.cellY = y;
1002 lp.cellHSpan = spanX;
1003 lp.cellVSpan = spanY;
1004 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001005
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001006 if (spanX < 0 && spanY < 0) {
1007 lp.isLockedToGrid = false;
1008 }
1009
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001010 // Get the canonical child id to uniquely represent this view in this screen
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001011 ItemInfo info = (ItemInfo) child.getTag();
1012 int childId = mLauncher.getViewIdForItem(info);
1013
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001014 boolean markCellsAsOccupied = !(child instanceof Folder);
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001016 // TODO: This branch occurs when the workspace is adding views
1017 // outside of the defined grid
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001018 // maybe we should be deleting these items from the LauncherModel?
Adam Cohen4caf2982013-08-20 18:54:31 -07001019 Launcher.addDumpLog(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout", true);
Winson Chungaafa03c2010-06-11 17:34:16 -07001020 }
1021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 if (!(child instanceof Folder)) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001023 child.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 child.setOnLongClickListener(mLongClickListener);
1025 }
Joe Onorato00acb122009-08-04 16:04:30 -04001026 if (child instanceof DropTarget) {
Winson Chungaafa03c2010-06-11 17:34:16 -07001027 mDragController.addDropTarget((DropTarget) child);
Joe Onorato00acb122009-08-04 16:04:30 -04001028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001031 /**
Patrick Dubroye708c522011-03-01 16:03:43 -08001032 * Called directly from a CellLayout (not by the framework), after we've been added as a
1033 * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout
1034 * that it should intercept touch events, which is not something that is normally supported.
1035 */
1036 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001037 public boolean onTouch(View v, MotionEvent event) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001038 return (isSmall() || !isFinishedSwitchingState())
1039 || (!isSmall() && indexOfChild(v) != mCurrentPage);
Patrick Dubroye708c522011-03-01 16:03:43 -08001040 }
1041
Adam Cohenfc53cd22011-07-20 15:45:11 -07001042 public boolean isSwitchingState() {
1043 return mIsSwitchingState;
1044 }
1045
Winson Chung70442722012-02-10 15:43:22 -08001046 /** This differs from isSwitchingState in that we take into account how far the transition
1047 * has completed. */
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001048 public boolean isFinishedSwitchingState() {
Winson Chung70442722012-02-10 15:43:22 -08001049 return !mIsSwitchingState || (mTransitionProgress > 0.5f);
1050 }
1051
Adam Cohended9f8d2010-11-03 13:25:16 -07001052 protected void onWindowVisibilityChanged (int visibility) {
1053 mLauncher.onWindowVisibilityChanged(visibility);
1054 }
1055
Michael Jurka5f1c5092010-09-03 14:15:02 -07001056 @Override
1057 public boolean dispatchUnhandledMove(View focused, int direction) {
Winson Chung70442722012-02-10 15:43:22 -08001058 if (isSmall() || !isFinishedSwitchingState()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -07001059 // when the home screens are shrunken, shouldn't allow side-scrolling
1060 return false;
1061 }
1062 return super.dispatchUnhandledMove(focused, direction);
1063 }
1064
1065 @Override
1066 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkad771c962011-08-09 15:00:48 -07001067 switch (ev.getAction() & MotionEvent.ACTION_MASK) {
1068 case MotionEvent.ACTION_DOWN:
Adam Cohenf8d28232011-02-01 21:47:00 -08001069 mXDown = ev.getX();
1070 mYDown = ev.getY();
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001071 mTouchDownTime = System.currentTimeMillis();
Michael Jurkad771c962011-08-09 15:00:48 -07001072 break;
1073 case MotionEvent.ACTION_POINTER_UP:
1074 case MotionEvent.ACTION_UP:
1075 if (mTouchState == TOUCH_STATE_REST) {
1076 final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
1077 if (!currentPage.lastDownOnOccupiedCell()) {
1078 onWallpaperTap(ev);
1079 }
1080 }
Adam Cohenf8d28232011-02-01 21:47:00 -08001081 }
Michael Jurka5f1c5092010-09-03 14:15:02 -07001082 return super.onInterceptTouchEvent(ev);
1083 }
1084
Adam Cohen3d509322012-06-06 14:10:04 -07001085 protected void reinflateWidgetsIfNecessary() {
1086 final int clCount = getChildCount();
1087 for (int i = 0; i < clCount; i++) {
1088 CellLayout cl = (CellLayout) getChildAt(i);
1089 ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets();
1090 final int itemCount = swc.getChildCount();
1091 for (int j = 0; j < itemCount; j++) {
1092 View v = swc.getChildAt(j);
1093
1094 if (v.getTag() instanceof LauncherAppWidgetInfo) {
1095 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
1096 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) info.hostView;
1097 if (lahv != null && lahv.orientationChangedSincedInflation()) {
1098 mLauncher.removeAppWidget(info);
1099 // Remove the current widget which is inflated with the wrong orientation
1100 cl.removeView(lahv);
1101 mLauncher.bindAppWidget(info);
1102 }
1103 }
1104 }
1105 }
1106 }
1107
Michael Jurka1adf5392010-10-18 18:10:22 -07001108 @Override
1109 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung70442722012-02-10 15:43:22 -08001110 if (!isFinishedSwitchingState()) return;
Adam Cohenf8d28232011-02-01 21:47:00 -08001111
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001112 float deltaX = ev.getX() - mXDown;
1113 float absDeltaX = Math.abs(deltaX);
1114 float absDeltaY = Math.abs(ev.getY() - mYDown);
Adam Cohenf8d28232011-02-01 21:47:00 -08001115
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001116 if (Float.compare(absDeltaX, 0f) == 0) return;
Adam Cohenf8d28232011-02-01 21:47:00 -08001117
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001118 float slope = absDeltaY / absDeltaX;
Winson Chung70442722012-02-10 15:43:22 -08001119 float theta = (float) Math.atan(slope);
Adam Cohenf8d28232011-02-01 21:47:00 -08001120
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001121 if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) {
Winson Chung70442722012-02-10 15:43:22 -08001122 cancelCurrentPageLongPress();
1123 }
1124
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001125 boolean passRightSwipesToCustomContent =
1126 (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY;
1127
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001128 boolean swipeInIgnoreDirection = isLayoutRtl() ? deltaX < 0 : deltaX > 0;
1129 if (swipeInIgnoreDirection && getScreenIdForPageIndex(getCurrentPage()) ==
1130 CUSTOM_CONTENT_SCREEN_ID && passRightSwipesToCustomContent) {
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001131 // Pass swipes to the right to the custom content page.
1132 return;
1133 }
1134
Winson Chung70442722012-02-10 15:43:22 -08001135 if (theta > MAX_SWIPE_ANGLE) {
1136 // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
1137 return;
1138 } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
1139 // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
1140 // increase the touch slop to make it harder to begin scrolling the workspace. This
1141 // results in vertically scrolling widgets to more easily. The higher the angle, the
1142 // more we increase touch slop.
1143 theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
1144 float extraRatio = (float)
1145 Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
1146 super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
1147 } else {
1148 // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
1149 super.determineScrollingStart(ev);
Adam Cohenf8d28232011-02-01 21:47:00 -08001150 }
Michael Jurka1adf5392010-10-18 18:10:22 -07001151 }
1152
Patrick Dubroy1262e362010-10-06 15:49:50 -07001153 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -07001154 super.onPageBeginMoving();
1155
Michael Jurkad74c9842011-07-10 12:44:21 -07001156 if (isHardwareAccelerated()) {
Michael Jurka3a0469d2012-06-21 09:38:41 -07001157 updateChildrenLayersEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -07001158 } else {
Michael Jurkad74c9842011-07-10 12:44:21 -07001159 if (mNextPage != INVALID_PAGE) {
1160 // we're snapping to a particular screen
1161 enableChildrenCache(mCurrentPage, mNextPage);
1162 } else {
1163 // this is when user is actively dragging a particular screen, they might
1164 // swipe it either left or right (but we won't advance by more than one screen)
1165 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
Winson Chung007c6982011-06-14 13:27:53 -07001168
Winson Chung70442722012-02-10 15:43:22 -08001169 // If we are not fading in adjacent screens, we still need to restore the alpha in case the
1170 // user scrolls while we are transitioning (should not affect dispatchDraw optimizations)
Michael Jurka869390b2012-05-06 15:55:19 -07001171 if (!mWorkspaceFadeInAdjacentScreens) {
Winson Chung70442722012-02-10 15:43:22 -08001172 for (int i = 0; i < getChildCount(); ++i) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001173 ((CellLayout) getPageAt(i)).setShortcutAndWidgetAlpha(1f);
Winson Chung70442722012-02-10 15:43:22 -08001174 }
1175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177
Patrick Dubroy1262e362010-10-06 15:49:50 -07001178 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -07001179 super.onPageEndMoving();
1180
Michael Jurkad74c9842011-07-10 12:44:21 -07001181 if (isHardwareAccelerated()) {
Michael Jurka3a0469d2012-06-21 09:38:41 -07001182 updateChildrenLayersEnabled(false);
Michael Jurkad74c9842011-07-10 12:44:21 -07001183 } else {
1184 clearChildrenCache();
1185 }
1186
Winson Chung3bc21c32012-01-20 13:59:18 -08001187 if (mDragController.isDragging()) {
1188 if (isSmall()) {
1189 // If we are in springloaded mode, then force an event to check if the current touch
1190 // is under a new page (to scroll to)
Winson Chung25460a12013-04-01 18:21:28 -07001191 mDragController.forceTouchMove();
Winson Chung3bc21c32012-01-20 13:59:18 -08001192 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07001193 }
Adam Cohen26976d92011-03-22 15:33:33 -07001194
1195 if (mDelayedResizeRunnable != null) {
1196 mDelayedResizeRunnable.run();
1197 mDelayedResizeRunnable = null;
1198 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07001199
1200 if (mDelayedSnapToPageRunnable != null) {
1201 mDelayedSnapToPageRunnable.run();
1202 mDelayedSnapToPageRunnable = null;
1203 }
Adam Cohenaccfd562013-07-12 14:40:40 -07001204 if (mStripScreensOnPageStopMoving) {
1205 stripEmptyScreens();
1206 mStripScreensOnPageStopMoving = false;
1207 }
Adam Cohen89cbbb92013-09-25 16:52:48 -07001208 }
1209
1210 @Override
1211 protected void notifyPageSwitchListener() {
1212 super.notifyPageSwitchListener();
Adam Cohen674531f2013-12-13 15:07:14 -08001213 Launcher.setScreen(getNextPage());
Michael Jurka0142d492010-08-25 17:46:15 -07001214
Adam Cohen6ad0e7d2013-07-24 13:55:41 -07001215 if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) {
Adam Cohenbffe7452013-07-22 18:21:45 -07001216 mCustomContentShowing = true;
1217 if (mCustomContentCallbacks != null) {
1218 mCustomContentCallbacks.onShow();
Adam Cohen3d1b2b42013-08-14 15:57:58 -07001219 mCustomContentShowTime = System.currentTimeMillis();
Winson Chung5841efa2013-09-30 18:06:44 -07001220 mLauncher.updateVoiceButtonProxyVisible(false);
Adam Cohenbffe7452013-07-22 18:21:45 -07001221 }
Adam Cohen6ad0e7d2013-07-24 13:55:41 -07001222 } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) {
Adam Cohenbffe7452013-07-22 18:21:45 -07001223 mCustomContentShowing = false;
1224 if (mCustomContentCallbacks != null) {
1225 mCustomContentCallbacks.onHide();
1226 mLauncher.resetQSBScroll();
Winson Chung5841efa2013-09-30 18:06:44 -07001227 mLauncher.updateVoiceButtonProxyVisible(false);
Adam Cohenbffe7452013-07-22 18:21:45 -07001228 }
1229 }
Adam Cohen53805212013-10-01 10:39:23 -07001230 if (getPageIndicator() != null) {
1231 getPageIndicator().setContentDescription(getPageIndicatorDescription());
1232 }
Chris Craik01f2d7f2013-10-01 14:41:56 -07001233 }
Michael Jurka0142d492010-08-25 17:46:15 -07001234
Adam Cohen6fecd412013-10-02 17:41:50 -07001235 protected CustomContentCallbacks getCustomContentCallbacks() {
1236 return mCustomContentCallbacks;
1237 }
1238
Michael Jurkac5b262c2011-01-12 20:24:50 -08001239 protected void setWallpaperDimension() {
Michael Jurkaa6a05472013-11-13 17:59:46 +01001240 new AsyncTask<Void, Void, Void>() {
1241 public Void doInBackground(Void ... args) {
1242 String spKey = WallpaperCropActivity.getSharedPreferencesKey();
1243 SharedPreferences sp =
1244 mLauncher.getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
Michael Jurka7ad868b2013-12-12 15:04:25 +01001245 LauncherWallpaperPickerActivity.suggestWallpaperDimension(mLauncher.getResources(),
Michael Jurkaa6a05472013-11-13 17:59:46 +01001246 sp, mLauncher.getWindowManager(), mWallpaperManager);
1247 return null;
1248 }
1249 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001250 }
1251
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 protected void snapToPage(int whichPage, Runnable r) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07001253 snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r);
1254 }
1255
1256 protected void snapToPage(int whichPage, int duration, Runnable r) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001257 if (mDelayedSnapToPageRunnable != null) {
1258 mDelayedSnapToPageRunnable.run();
1259 }
1260 mDelayedSnapToPageRunnable = r;
Adam Cohenad4e15c2013-10-17 16:21:35 -07001261 snapToPage(whichPage, duration);
Winson Chungf0c6ae02012-03-21 16:10:31 -07001262 }
1263
Adam Cohendcd297f2013-06-18 13:13:40 -07001264 protected void snapToScreenId(long screenId, Runnable r) {
1265 snapToPage(getPageIndexForScreenId(screenId), r);
1266 }
1267
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001268 class WallpaperOffsetInterpolator implements Choreographer.FrameCallback {
1269 float mFinalOffset = 0.0f;
Michael Jurka046cf342013-09-14 16:40:34 +02001270 float mCurrentOffset = 0.5f; // to force an initial update
Michael Jurkafe09cb72013-08-27 15:48:58 +02001271 boolean mWaitingForUpdate;
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001272 Choreographer mChoreographer;
1273 Interpolator mInterpolator;
1274 boolean mAnimating;
1275 long mAnimationStartTime;
1276 float mAnimationStartOffset;
Michael Jurka2f817312013-09-20 03:03:42 +02001277 private final int ANIMATION_DURATION = 250;
1278 // Don't use all the wallpaper for parallax until you have at least this many pages
Michael Jurkafe0ace32013-10-03 01:05:14 -07001279 private final int MIN_PARALLAX_PAGE_SPAN = 3;
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001280 int mNumScreens;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001281
1282 public WallpaperOffsetInterpolator() {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001283 mChoreographer = Choreographer.getInstance();
1284 mInterpolator = new DecelerateInterpolator(1.5f);
Michael Jurkaab1983f2011-01-18 15:50:17 -08001285 }
1286
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001287 @Override
1288 public void doFrame(long frameTimeNanos) {
Michael Jurkafe09cb72013-08-27 15:48:58 +02001289 updateOffset(false);
1290 }
1291
Michael Jurkafe09cb72013-08-27 15:48:58 +02001292 private void updateOffset(boolean force) {
1293 if (mWaitingForUpdate || force) {
1294 mWaitingForUpdate = false;
Michael Jurka1bd90b02013-09-05 23:10:14 +02001295 if (computeScrollOffset() && mWindowToken != null) {
1296 try {
1297 mWallpaperManager.setWallpaperOffsets(mWindowToken,
1298 mWallpaperOffset.getCurrX(), 0.5f);
1299 setWallpaperOffsetSteps();
1300 } catch (IllegalArgumentException e) {
1301 Log.e(TAG, "Error updating wallpaper offset: " + e);
1302 }
Michael Jurkafe09cb72013-08-27 15:48:58 +02001303 }
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001304 }
Michael Jurkaca5b8362011-01-27 13:23:26 -08001305 }
1306
Michael Jurkaab1983f2011-01-18 15:50:17 -08001307 public boolean computeScrollOffset() {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001308 final float oldOffset = mCurrentOffset;
1309 if (mAnimating) {
1310 long durationSinceAnimation = System.currentTimeMillis() - mAnimationStartTime;
1311 float t0 = durationSinceAnimation / (float) ANIMATION_DURATION;
1312 float t1 = mInterpolator.getInterpolation(t0);
1313 mCurrentOffset = mAnimationStartOffset +
1314 (mFinalOffset - mAnimationStartOffset) * t1;
1315 mAnimating = durationSinceAnimation < ANIMATION_DURATION;
Michael Jurkaca5b8362011-01-27 13:23:26 -08001316 } else {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001317 mCurrentOffset = mFinalOffset;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001318 }
Michael Jurkaca5b8362011-01-27 13:23:26 -08001319
Michael Jurkafe09cb72013-08-27 15:48:58 +02001320 if (Math.abs(mCurrentOffset - mFinalOffset) > 0.0000001f) {
1321 scheduleUpdate();
1322 }
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001323 if (Math.abs(oldOffset - mCurrentOffset) > 0.0000001f) {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001324 return true;
1325 }
1326 return false;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001327 }
1328
Michael Jurkafe09cb72013-08-27 15:48:58 +02001329 private float wallpaperOffsetForCurrentScroll() {
1330 if (getChildCount() <= 1) {
1331 return 0;
1332 }
Michael Jurka2f817312013-09-20 03:03:42 +02001333
1334 // Exclude the leftmost page
Michael Jurkafe0ace32013-10-03 01:05:14 -07001335 int emptyExtraPages = numEmptyScreensToIgnore();
1336 int firstIndex = numCustomPages();
Michael Jurka2f817312013-09-20 03:03:42 +02001337 // Exclude the last extra empty screen (if we have > MIN_PARALLAX_PAGE_SPAN pages)
Michael Jurkafe0ace32013-10-03 01:05:14 -07001338 int lastIndex = getChildCount() - 1 - emptyExtraPages;
1339 if (isLayoutRtl()) {
1340 int temp = firstIndex;
1341 firstIndex = lastIndex;
1342 lastIndex = temp;
1343 }
Michael Jurka2f817312013-09-20 03:03:42 +02001344
Michael Jurkafe09cb72013-08-27 15:48:58 +02001345 int firstPageScrollX = getScrollForPage(firstIndex);
1346 int scrollRange = getScrollForPage(lastIndex) - firstPageScrollX;
1347 if (scrollRange == 0) {
1348 return 0;
1349 } else {
Michael Jurka2f817312013-09-20 03:03:42 +02001350 // TODO: do different behavior if it's a live wallpaper?
Michael Jurkafe0ace32013-10-03 01:05:14 -07001351 // Sometimes the left parameter of the pages is animated during a layout transition;
1352 // this parameter offsets it to keep the wallpaper from animating as well
Michael Jurkafe0ace32013-10-03 01:05:14 -07001353 int adjustedScroll =
Michael Jurka8fd3adc2013-10-16 13:50:24 -07001354 getScrollX() - firstPageScrollX - getLayoutTransitionOffsetForPage(0);
Michael Jurkafe0ace32013-10-03 01:05:14 -07001355 float offset = Math.min(1, adjustedScroll / (float) scrollRange);
Michael Jurka2f817312013-09-20 03:03:42 +02001356 offset = Math.max(0, offset);
1357 // Don't use up all the wallpaper parallax until you have at least
1358 // MIN_PARALLAX_PAGE_SPAN pages
Michael Jurkafe0ace32013-10-03 01:05:14 -07001359 int numScrollingPages = getNumScreensExcludingEmptyAndCustom();
Michael Jurka2a4f4922014-01-29 16:32:39 +01001360 int parallaxPageSpan;
1361 if (mWallpaperIsLiveWallpaper) {
1362 parallaxPageSpan = numScrollingPages - 1;
1363 } else {
1364 parallaxPageSpan = Math.max(MIN_PARALLAX_PAGE_SPAN, numScrollingPages - 1);
1365 }
1366 mNumPagesForWallpaperParallax = parallaxPageSpan;
1367
Michael Jurkafe0ace32013-10-03 01:05:14 -07001368 // On RTL devices, push the wallpaper offset to the right if we don't have enough
1369 // pages (ie if numScrollingPages < MIN_PARALLAX_PAGE_SPAN)
1370 int padding = isLayoutRtl() ? parallaxPageSpan - numScrollingPages + 1 : 0;
1371 return offset * (padding + numScrollingPages - 1) / parallaxPageSpan;
Michael Jurkafe09cb72013-08-27 15:48:58 +02001372 }
1373 }
1374
Michael Jurkafe0ace32013-10-03 01:05:14 -07001375 private int numEmptyScreensToIgnore() {
1376 int numScrollingPages = getChildCount() - numCustomPages();
1377 if (numScrollingPages >= MIN_PARALLAX_PAGE_SPAN && hasExtraEmptyScreen()) {
Michael Jurka2f817312013-09-20 03:03:42 +02001378 return 1;
1379 } else {
1380 return 0;
1381 }
1382 }
1383
Michael Jurkafe0ace32013-10-03 01:05:14 -07001384 private int getNumScreensExcludingEmptyAndCustom() {
1385 int numScrollingPages = getChildCount() - numEmptyScreensToIgnore() - numCustomPages();
Michael Jurka2f817312013-09-20 03:03:42 +02001386 return numScrollingPages;
1387 }
1388
Michael Jurkafe09cb72013-08-27 15:48:58 +02001389 public void syncWithScroll() {
1390 float offset = wallpaperOffsetForCurrentScroll();
1391 mWallpaperOffset.setFinalX(offset);
1392 updateOffset(true);
1393 }
1394
Michael Jurkaab1983f2011-01-18 15:50:17 -08001395 public float getCurrX() {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001396 return mCurrentOffset;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001397 }
1398
1399 public float getFinalX() {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001400 return mFinalOffset;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001401 }
1402
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001403 private void animateToFinal() {
1404 mAnimating = true;
1405 mAnimationStartOffset = mCurrentOffset;
1406 mAnimationStartTime = System.currentTimeMillis();
Michael Jurkaab1983f2011-01-18 15:50:17 -08001407 }
1408
Michael Jurkafe09cb72013-08-27 15:48:58 +02001409 private void setWallpaperOffsetSteps() {
1410 // Set wallpaper offset steps (1 / (number of screens - 1))
Michael Jurka2a4f4922014-01-29 16:32:39 +01001411 float xOffset = 1.0f / mNumPagesForWallpaperParallax;
1412 if (xOffset != mLastSetWallpaperOffsetSteps) {
1413 mWallpaperManager.setWallpaperOffsetSteps(xOffset, 1.0f);
1414 mLastSetWallpaperOffsetSteps = xOffset;
1415 }
Michael Jurkafe09cb72013-08-27 15:48:58 +02001416 }
1417
Michael Jurkaab1983f2011-01-18 15:50:17 -08001418 public void setFinalX(float x) {
Michael Jurkafe09cb72013-08-27 15:48:58 +02001419 scheduleUpdate();
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001420 mFinalOffset = Math.max(0f, Math.min(x, 1.0f));
Michael Jurkafe0ace32013-10-03 01:05:14 -07001421 if (getNumScreensExcludingEmptyAndCustom() != mNumScreens) {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001422 if (mNumScreens > 0) {
1423 // Don't animate if we're going from 0 screens
1424 animateToFinal();
1425 }
Michael Jurkafe0ace32013-10-03 01:05:14 -07001426 mNumScreens = getNumScreensExcludingEmptyAndCustom();
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001427 }
Michael Jurkaab1983f2011-01-18 15:50:17 -08001428 }
1429
Michael Jurkafe09cb72013-08-27 15:48:58 +02001430 private void scheduleUpdate() {
1431 if (!mWaitingForUpdate) {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001432 mChoreographer.postFrameCallback(this);
Michael Jurkafe09cb72013-08-27 15:48:58 +02001433 mWaitingForUpdate = true;
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001434 }
Michael Jurkaab1983f2011-01-18 15:50:17 -08001435 }
1436
1437 public void jumpToFinal() {
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001438 mCurrentOffset = mFinalOffset;
Michael Jurkaab1983f2011-01-18 15:50:17 -08001439 }
Dianne Hackborn8f573952009-08-10 23:21:09 -07001440 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001441
Michael Jurka340c5f32010-10-21 16:49:19 -07001442 @Override
1443 public void computeScroll() {
1444 super.computeScroll();
Michael Jurkafe09cb72013-08-27 15:48:58 +02001445 mWallpaperOffset.syncWithScroll();
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001446 }
1447
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001448 void showOutlines() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001449 if (!isSmall() && !mIsSwitchingState) {
Winson Chung9171e6d2010-11-17 17:39:27 -08001450 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
1451 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -07001452 mChildrenOutlineFadeInAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 1.0f);
Winson Chung9171e6d2010-11-17 17:39:27 -08001453 mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION);
1454 mChildrenOutlineFadeInAnimation.start();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001455 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001456 }
1457
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001458 void hideOutlines() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001459 if (!isSmall() && !mIsSwitchingState) {
Winson Chung9171e6d2010-11-17 17:39:27 -08001460 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
1461 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -07001462 mChildrenOutlineFadeOutAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 0.0f);
Winson Chung9171e6d2010-11-17 17:39:27 -08001463 mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION);
1464 mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY);
1465 mChildrenOutlineFadeOutAnimation.start();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001466 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001467 }
1468
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001469 public void showOutlinesTemporarily() {
1470 if (!mIsPageMoving && !isTouchActive()) {
1471 snapToPage(mCurrentPage);
1472 }
1473 }
1474
Winson Chung9171e6d2010-11-17 17:39:27 -08001475 public void setChildrenOutlineAlpha(float alpha) {
1476 mChildrenOutlineAlpha = alpha;
Adam Cohenf34bab52010-09-30 14:11:56 -07001477 for (int i = 0; i < getChildCount(); i++) {
1478 CellLayout cl = (CellLayout) getChildAt(i);
1479 cl.setBackgroundAlpha(alpha);
1480 }
1481 }
1482
Winson Chung9171e6d2010-11-17 17:39:27 -08001483 public float getChildrenOutlineAlpha() {
1484 return mChildrenOutlineAlpha;
1485 }
1486
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001487 void disableBackground() {
1488 mDrawBackground = false;
1489 }
1490 void enableBackground() {
1491 mDrawBackground = true;
1492 }
1493
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001494 private void animateBackgroundGradient(float finalAlpha, boolean animated) {
Winson Chungb4b7fa72010-11-18 14:38:53 -08001495 if (mBackground == null) return;
Michael Jurkab9e14972011-07-25 17:57:40 -07001496 if (mBackgroundFadeInAnimation != null) {
1497 mBackgroundFadeInAnimation.cancel();
1498 mBackgroundFadeInAnimation = null;
1499 }
1500 if (mBackgroundFadeOutAnimation != null) {
1501 mBackgroundFadeOutAnimation.cancel();
1502 mBackgroundFadeOutAnimation = null;
1503 }
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001504 float startAlpha = getBackgroundAlpha();
1505 if (finalAlpha != startAlpha) {
1506 if (animated) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001507 mBackgroundFadeOutAnimation =
1508 LauncherAnimUtils.ofFloat(this, startAlpha, finalAlpha);
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001509 mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() {
1510 public void onAnimationUpdate(ValueAnimator animation) {
1511 setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue());
1512 }
1513 });
1514 mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f));
1515 mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION);
1516 mBackgroundFadeOutAnimation.start();
1517 } else {
1518 setBackgroundAlpha(finalAlpha);
1519 }
Michael Jurkab9e14972011-07-25 17:57:40 -07001520 }
Winson Chung9171e6d2010-11-17 17:39:27 -08001521 }
1522
1523 public void setBackgroundAlpha(float alpha) {
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001524 if (alpha != mBackgroundAlpha) {
1525 mBackgroundAlpha = alpha;
1526 invalidate();
1527 }
Winson Chung9171e6d2010-11-17 17:39:27 -08001528 }
1529
Adam Cohenf34bab52010-09-30 14:11:56 -07001530 public float getBackgroundAlpha() {
1531 return mBackgroundAlpha;
1532 }
1533
Adam Cohen68d73932010-11-15 10:50:58 -08001534 float backgroundAlphaInterpolator(float r) {
1535 float pivotA = 0.1f;
1536 float pivotB = 0.4f;
1537 if (r < pivotA) {
1538 return 0;
1539 } else if (r > pivotB) {
1540 return 1.0f;
1541 } else {
1542 return (r - pivotA)/(pivotB - pivotA);
1543 }
1544 }
1545
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001546 private void updatePageAlphaValues(int screenCenter) {
Michael Jurka869390b2012-05-06 15:55:19 -07001547 boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX;
1548 if (mWorkspaceFadeInAdjacentScreens &&
1549 mState == State.NORMAL &&
1550 !mIsSwitchingState &&
1551 !isInOverscroll) {
Adam Cohen84a465a2013-11-11 18:49:56 +00001552 for (int i = numCustomPages(); i < getChildCount(); i++) {
Michael Jurka869390b2012-05-06 15:55:19 -07001553 CellLayout child = (CellLayout) getChildAt(i);
1554 if (child != null) {
1555 float scrollProgress = getScrollProgress(screenCenter, child, i);
Adam Cohen73894962011-10-31 13:17:17 -07001556 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka869390b2012-05-06 15:55:19 -07001557 child.getShortcutsAndWidgets().setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001558 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001559 }
1560 }
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001561 }
1562
1563 private void setChildrenBackgroundAlphaMultipliers(float a) {
1564 for (int i = 0; i < getChildCount(); i++) {
1565 CellLayout child = (CellLayout) getChildAt(i);
1566 child.setBackgroundAlphaMultiplier(a);
1567 }
1568 }
1569
Winson Chung98ca0f72013-07-29 12:58:51 -07001570 public boolean hasCustomContent() {
Adam Cohenedb40762013-07-18 16:45:45 -07001571 return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID);
1572 }
1573
Michael Jurkafe0ace32013-10-03 01:05:14 -07001574 public int numCustomPages() {
1575 return hasCustomContent() ? 1 : 0;
1576 }
1577
Adam Cohenbffe7452013-07-22 18:21:45 -07001578 public boolean isOnOrMovingToCustomContent() {
1579 return hasCustomContent() && getNextPage() == 0;
1580 }
1581
Adam Cohenedb40762013-07-18 16:45:45 -07001582 private void updateStateForCustomContent(int screenCenter) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001583 float translationX = 0;
1584 float progress = 0;
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001585 if (hasCustomContent()) {
Adam Cohenedb40762013-07-18 16:45:45 -07001586 int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID);
Adam Cohen564a2e72013-10-09 14:47:32 -07001587
Adam Cohena45de072013-10-11 16:07:47 -07001588 int scrollDelta = getScrollX() - getScrollForPage(index) -
1589 getLayoutTransitionOffsetForPage(index);
1590 float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index);
1591 translationX = scrollRange - scrollDelta;
1592 progress = (scrollRange - scrollDelta) / scrollRange;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001593
1594 if (isLayoutRtl()) {
1595 translationX = Math.min(0, translationX);
1596 } else {
1597 translationX = Math.max(0, translationX);
1598 }
Adam Cohen955806d2013-07-19 16:36:43 -07001599 progress = Math.max(0, progress);
Dave Hawkeya8881582013-09-17 15:55:33 -06001600 }
Adam Cohenedb40762013-07-18 16:45:45 -07001601
Dave Hawkeya8881582013-09-17 15:55:33 -06001602 if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return;
Adam Cohen84add1d2013-10-14 16:29:02 -07001603
1604 CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
1605 if (progress > 0 && cc.getVisibility() != VISIBLE && !isSmall()) {
1606 cc.setVisibility(VISIBLE);
1607 }
1608
Dave Hawkeya8881582013-09-17 15:55:33 -06001609 mLastCustomContentScrollProgress = progress;
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001610
Dave Hawkeya8881582013-09-17 15:55:33 -06001611 setBackgroundAlpha(progress * 0.8f);
Adam Cohen22cba7f2013-07-19 16:14:00 -07001612
Dave Hawkeya8881582013-09-17 15:55:33 -06001613 if (mLauncher.getHotseat() != null) {
1614 mLauncher.getHotseat().setTranslationX(translationX);
1615 }
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001616
Dave Hawkeya8881582013-09-17 15:55:33 -06001617 if (getPageIndicator() != null) {
1618 getPageIndicator().setTranslationX(translationX);
1619 }
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001620
Dave Hawkeya8881582013-09-17 15:55:33 -06001621 if (mCustomContentCallbacks != null) {
1622 mCustomContentCallbacks.onScrollProgressChanged(progress);
Adam Cohenedb40762013-07-18 16:45:45 -07001623 }
1624 }
1625
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001626 @Override
Adam Cohen53805212013-10-01 10:39:23 -07001627 protected OnClickListener getPageIndicatorClickListener() {
1628 AccessibilityManager am = (AccessibilityManager)
1629 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
1630 if (!am.isTouchExplorationEnabled()) {
1631 return null;
1632 }
1633 OnClickListener listener = new OnClickListener() {
1634 @Override
1635 public void onClick(View arg0) {
1636 enterOverviewMode();
1637 }
1638 };
1639 return listener;
1640 }
1641
1642 @Override
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001643 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001644 final boolean isRtl = isLayoutRtl();
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001645 super.screenScrolled(screenCenter);
1646
1647 updatePageAlphaValues(screenCenter);
Adam Cohenedb40762013-07-18 16:45:45 -07001648 updateStateForCustomContent(screenCenter);
Michael Jurkad51f33a2012-06-28 15:35:26 -07001649 enableHwLayersOnVisiblePages();
Adam Cohenf34bab52010-09-30 14:11:56 -07001650
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001651 boolean shouldOverScroll = (mOverScrollX < 0 && (!hasCustomContent() || isLayoutRtl())) ||
1652 (mOverScrollX > mMaxScrollX && (!hasCustomContent() || !isLayoutRtl()));
1653
1654 if (shouldOverScroll) {
Winson Chung52aee602013-01-30 12:01:02 -08001655 int index = 0;
1656 float pivotX = 0f;
1657 final float leftBiasedPivot = 0.25f;
1658 final float rightBiasedPivot = 0.75f;
1659 final int lowerIndex = 0;
1660 final int upperIndex = getChildCount() - 1;
Adam Cohena29f5042013-09-26 14:29:35 -07001661
1662 final boolean isLeftPage = mOverScrollX < 0;
1663 index = (!isRtl && isLeftPage) || (isRtl && !isLeftPage) ? lowerIndex : upperIndex;
1664 pivotX = isLeftPage ? rightBiasedPivot : leftBiasedPivot;
Winson Chung52aee602013-01-30 12:01:02 -08001665
Adam Cohenb5ba0972011-09-07 18:02:31 -07001666 CellLayout cl = (CellLayout) getChildAt(index);
1667 float scrollProgress = getScrollProgress(screenCenter, cl, index);
Winson Chung52aee602013-01-30 12:01:02 -08001668 cl.setOverScrollAmount(Math.abs(scrollProgress), isLeftPage);
1669 float rotation = -WORKSPACE_OVERSCROLL_ROTATION * scrollProgress;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001670 cl.setRotationY(rotation);
Adam Cohena29f5042013-09-26 14:29:35 -07001671
1672 if (!mOverscrollTransformsSet || Float.compare(mLastOverscrollPivotX, pivotX) != 0) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001673 mOverscrollTransformsSet = true;
Adam Cohena29f5042013-09-26 14:29:35 -07001674 mLastOverscrollPivotX = pivotX;
Adam Cohen94309882012-06-08 16:24:56 -07001675 cl.setCameraDistance(mDensity * mCameraDistance);
Winson Chung52aee602013-01-30 12:01:02 -08001676 cl.setPivotX(cl.getMeasuredWidth() * pivotX);
Michael Jurkab06d95f2012-04-02 06:26:53 -07001677 cl.setPivotY(cl.getMeasuredHeight() * 0.5f);
1678 cl.setOverscrollTransformsDirty(true);
1679 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001680 } else {
Adam Cohen4de09742013-12-12 16:16:39 -08001681 if (mOverscrollTransformsSet && getChildCount() > 0) {
Michael Jurkab06d95f2012-04-02 06:26:53 -07001682 mOverscrollTransformsSet = false;
Adam Cohenebea84d2011-11-09 17:20:41 -08001683 ((CellLayout) getChildAt(0)).resetOverscrollTransforms();
1684 ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms();
Adam Cohen7842d7f2011-09-12 15:28:15 -07001685 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001686 }
1687 }
1688
1689 @Override
Adam Cohenb5ba0972011-09-07 18:02:31 -07001690 protected void overScroll(float amount) {
Michael Jurka869390b2012-05-06 15:55:19 -07001691 acceleratedOverScroll(amount);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001692 }
1693
Joe Onorato00acb122009-08-04 16:04:30 -04001694 protected void onAttachedToWindow() {
1695 super.onAttachedToWindow();
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001696 mWindowToken = getWindowToken();
Joe Onorato956091b2010-02-19 12:47:40 -08001697 computeScroll();
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001698 mDragController.setWindowToken(mWindowToken);
1699 }
1700
1701 protected void onDetachedFromWindow() {
Romain Guye6661952013-08-08 19:13:22 -07001702 super.onDetachedFromWindow();
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001703 mWindowToken = null;
Joe Onorato00acb122009-08-04 16:04:30 -04001704 }
1705
Adam Cohen53805212013-10-01 10:39:23 -07001706 protected void onResume() {
1707 if (getPageIndicator() != null) {
1708 // In case accessibility state has changed, we need to perform this on every
1709 // attach to window
Adam Cohend36d9472013-10-10 15:32:41 -07001710 OnClickListener listener = getPageIndicatorClickListener();
1711 if (listener != null) {
1712 getPageIndicator().setOnClickListener(listener);
1713 }
Adam Cohen53805212013-10-01 10:39:23 -07001714 }
1715 AccessibilityManager am = (AccessibilityManager)
1716 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
1717 sAccessibilityEnabled = am.isEnabled();
Michael Jurkaa6a05472013-11-13 17:59:46 +01001718
1719 // Update wallpaper dimensions if they were changed since last onResume
1720 // (we also always set the wallpaper dimensions in the constructor)
1721 if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) {
1722 setWallpaperDimension();
1723 }
Michael Jurka2a4f4922014-01-29 16:32:39 +01001724 mWallpaperIsLiveWallpaper = mWallpaperManager.getWallpaperInfo() != null;
1725 // Force the wallpaper offset steps to be set again, because another app might have changed
1726 // them
1727 mLastSetWallpaperOffsetSteps = 0f;
Adam Cohen53805212013-10-01 10:39:23 -07001728 }
1729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001731 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkac5b262c2011-01-12 20:24:50 -08001732 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
Michael Jurkafe09cb72013-08-27 15:48:58 +02001733 mWallpaperOffset.syncWithScroll();
Michael Jurkacc07e7a2013-08-26 20:56:35 +02001734 mWallpaperOffset.jumpToFinal();
Michael Jurkac5b262c2011-01-12 20:24:50 -08001735 }
Michael Jurka0142d492010-08-25 17:46:15 -07001736 super.onLayout(changed, left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738
1739 @Override
Winson Chung9171e6d2010-11-17 17:39:27 -08001740 protected void onDraw(Canvas canvas) {
1741 // Draw the background gradient if necessary
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001742 if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) {
Winson Chung8d006d52010-11-29 15:55:29 -08001743 int alpha = (int) (mBackgroundAlpha * 255);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001744 mBackground.setAlpha(alpha);
Michael Jurka8b805b12012-04-18 14:23:14 -07001745 mBackground.setBounds(getScrollX(), 0, getScrollX() + getMeasuredWidth(),
Winson Chungf0ea4d32011-06-06 14:27:16 -07001746 getMeasuredHeight());
1747 mBackground.draw(canvas);
Winson Chung9171e6d2010-11-17 17:39:27 -08001748 }
Adam Cohen073a46f2011-05-17 16:28:09 -07001749
Winson Chung9171e6d2010-11-17 17:39:27 -08001750 super.onDraw(canvas);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751
1752 // Call back to LauncherModel to finish binding after the first draw
Romain Guyeeacd562012-10-10 18:47:33 -07001753 post(mBindPages);
Winson Chung9171e6d2010-11-17 17:39:27 -08001754 }
1755
Adam Cohen21b41102011-11-01 17:29:52 -07001756 boolean isDrawingBackgroundGradient() {
1757 return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground);
1758 }
1759
Michael Jurkadfab7f02011-11-18 13:01:04 -08001760 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Joe Onorato67886212009-09-14 19:05:05 -04001762 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 final Folder openFolder = getOpenFolder();
1764 if (openFolder != null) {
1765 return openFolder.requestFocus(direction, previouslyFocusedRect);
1766 } else {
Michael Jurka0142d492010-08-25 17:46:15 -07001767 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 }
1769 }
1770 return false;
1771 }
1772
1773 @Override
Winson Chung97d85d22011-04-13 11:27:36 -07001774 public int getDescendantFocusability() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001775 if (isSmall()) {
Winson Chung97d85d22011-04-13 11:27:36 -07001776 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
1777 }
1778 return super.getDescendantFocusability();
1779 }
1780
1781 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -07001782 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Joe Onorato67886212009-09-14 19:05:05 -04001783 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 final Folder openFolder = getOpenFolder();
Michael Jurka0142d492010-08-25 17:46:15 -07001785 if (openFolder != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 openFolder.addFocusables(views, direction);
Michael Jurka0142d492010-08-25 17:46:15 -07001787 } else {
1788 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790 }
1791 }
1792
Michael Jurkad74c9842011-07-10 12:44:21 -07001793 public boolean isSmall() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001794 return mState == State.SMALL || mState == State.SPRING_LOADED || mState == State.OVERVIEW;
Michael Jurkad74c9842011-07-10 12:44:21 -07001795 }
1796
Michael Jurka0142d492010-08-25 17:46:15 -07001797 void enableChildrenCache(int fromPage, int toPage) {
1798 if (fromPage > toPage) {
1799 final int temp = fromPage;
1800 fromPage = toPage;
1801 toPage = temp;
Mike Cleron3a2b3f22009-11-05 17:17:42 -08001802 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001803
Michael Jurkadee05892010-07-27 10:01:56 -07001804 final int screenCount = getChildCount();
Adam Powellfea5d022010-04-29 11:42:45 -07001805
Michael Jurka0142d492010-08-25 17:46:15 -07001806 fromPage = Math.max(fromPage, 0);
1807 toPage = Math.min(toPage, screenCount - 1);
Adam Powellfea5d022010-04-29 11:42:45 -07001808
Michael Jurka0142d492010-08-25 17:46:15 -07001809 for (int i = fromPage; i <= toPage; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 final CellLayout layout = (CellLayout) getChildAt(i);
1811 layout.setChildrenDrawnWithCacheEnabled(true);
1812 layout.setChildrenDrawingCacheEnabled(true);
1813 }
1814 }
1815
1816 void clearChildrenCache() {
Michael Jurkadee05892010-07-27 10:01:56 -07001817 final int screenCount = getChildCount();
1818 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 final CellLayout layout = (CellLayout) getChildAt(i);
1820 layout.setChildrenDrawnWithCacheEnabled(false);
Adam Cohen8182e5b2011-08-01 15:43:31 -07001821 // In software mode, we don't want the items to continue to be drawn into bitmaps
1822 if (!isHardwareAccelerated()) {
1823 layout.setChildrenDrawingCacheEnabled(false);
1824 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826 }
1827
Michael Jurka3a0469d2012-06-21 09:38:41 -07001828 private void updateChildrenLayersEnabled(boolean force) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001829 boolean small = mState == State.SMALL || mState == State.OVERVIEW || mIsSwitchingState;
Michael Jurka3a0469d2012-06-21 09:38:41 -07001830 boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving();
Michael Jurkad74c9842011-07-10 12:44:21 -07001831
1832 if (enableChildrenLayers != mChildrenLayersEnabled) {
1833 mChildrenLayersEnabled = enableChildrenLayers;
Michael Jurkad51f33a2012-06-28 15:35:26 -07001834 if (mChildrenLayersEnabled) {
1835 enableHwLayersOnVisiblePages();
1836 } else {
1837 for (int i = 0; i < getPageCount(); i++) {
1838 final CellLayout cl = (CellLayout) getChildAt(i);
Chris Craik01f2d7f2013-10-01 14:41:56 -07001839 cl.enableHardwareLayer(false);
Michael Jurkad51f33a2012-06-28 15:35:26 -07001840 }
1841 }
1842 }
1843 }
1844
1845 private void enableHwLayersOnVisiblePages() {
1846 if (mChildrenLayersEnabled) {
1847 final int screenCount = getChildCount();
1848 getVisiblePages(mTempVisiblePagesRange);
1849 int leftScreen = mTempVisiblePagesRange[0];
1850 int rightScreen = mTempVisiblePagesRange[1];
1851 if (leftScreen == rightScreen) {
1852 // make sure we're caching at least two pages always
1853 if (rightScreen < screenCount - 1) {
1854 rightScreen++;
1855 } else if (leftScreen > 0) {
1856 leftScreen--;
1857 }
1858 }
Chris Craik01f2d7f2013-10-01 14:41:56 -07001859
1860 final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
Michael Jurkad51f33a2012-06-28 15:35:26 -07001861 for (int i = 0; i < screenCount; i++) {
Michael Jurka47639b92013-01-14 12:42:27 +01001862 final CellLayout layout = (CellLayout) getPageAt(i);
Chris Craik01f2d7f2013-10-01 14:41:56 -07001863
1864 // enable layers between left and right screen inclusive, except for the
1865 // customScreen, which may animate its content during transitions.
1866 boolean enableLayer = layout != customScreen &&
1867 leftScreen <= i && i <= rightScreen && shouldDrawChild(layout);
1868 layout.enableHardwareLayer(enableLayer);
Michael Jurkad74c9842011-07-10 12:44:21 -07001869 }
Michael Jurkace7e05f2011-02-01 22:02:35 -08001870 }
Michael Jurkace7e05f2011-02-01 22:02:35 -08001871 }
1872
Michael Jurka3a0469d2012-06-21 09:38:41 -07001873 public void buildPageHardwareLayers() {
1874 // force layers to be enabled just for the call to buildLayer
1875 updateChildrenLayersEnabled(true);
1876 if (getWindowToken() != null) {
1877 final int childCount = getChildCount();
1878 for (int i = 0; i < childCount; i++) {
1879 CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurkad51f33a2012-06-28 15:35:26 -07001880 cl.buildHardwareLayer();
Michael Jurka3a0469d2012-06-21 09:38:41 -07001881 }
1882 }
1883 updateChildrenLayersEnabled(false);
1884 }
1885
Jeff Brown1d0867c2010-12-02 18:27:39 -08001886 protected void onWallpaperTap(MotionEvent ev) {
1887 final int[] position = mTempCell;
1888 getLocationOnScreen(position);
1889
1890 int pointerIndex = ev.getActionIndex();
1891 position[0] += (int) ev.getX(pointerIndex);
1892 position[1] += (int) ev.getY(pointerIndex);
1893
1894 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
1895 ev.getAction() == MotionEvent.ACTION_UP
1896 ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP,
1897 position[0], position[1], 0, null);
1898 }
1899
Adam Cohen61033d32010-11-15 18:29:44 -08001900 /*
1901 * This interpolator emulates the rate at which the perceived scale of an object changes
1902 * as its distance from a camera increases. When this interpolator is applied to a scale
1903 * animation on a view, it evokes the sense that the object is shrinking due to moving away
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08001904 * from the camera.
Adam Cohen61033d32010-11-15 18:29:44 -08001905 */
1906 static class ZInterpolator implements TimeInterpolator {
1907 private float focalLength;
1908
1909 public ZInterpolator(float foc) {
1910 focalLength = foc;
1911 }
1912
Adam Coheneed565d2010-11-15 11:30:05 -08001913 public float getInterpolation(float input) {
1914 return (1.0f - focalLength / (focalLength + input)) /
Adam Cohen61033d32010-11-15 18:29:44 -08001915 (1.0f - focalLength / (focalLength + 1.0f));
Adam Cohencbbaf982010-11-12 14:50:33 -08001916 }
1917 }
1918
Adam Cohen61033d32010-11-15 18:29:44 -08001919 /*
1920 * The exact reverse of ZInterpolator.
1921 */
1922 static class InverseZInterpolator implements TimeInterpolator {
1923 private ZInterpolator zInterpolator;
1924 public InverseZInterpolator(float foc) {
1925 zInterpolator = new ZInterpolator(foc);
1926 }
Adam Cohencbbaf982010-11-12 14:50:33 -08001927 public float getInterpolation(float input) {
Adam Cohen61033d32010-11-15 18:29:44 -08001928 return 1 - zInterpolator.getInterpolation(1 - input);
Adam Cohencbbaf982010-11-12 14:50:33 -08001929 }
1930 }
1931
Adam Cohen61033d32010-11-15 18:29:44 -08001932 /*
1933 * ZInterpolator compounded with an ease-out.
1934 */
1935 static class ZoomOutInterpolator implements TimeInterpolator {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001936 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f);
1937 private final ZInterpolator zInterpolator = new ZInterpolator(0.13f);
Adam Cohen61033d32010-11-15 18:29:44 -08001938
1939 public float getInterpolation(float input) {
1940 return decelerate.getInterpolation(zInterpolator.getInterpolation(input));
1941 }
1942 }
1943
1944 /*
1945 * InvereZInterpolator compounded with an ease-out.
1946 */
1947 static class ZoomInInterpolator implements TimeInterpolator {
1948 private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f);
1949 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f);
1950
1951 public float getInterpolation(float input) {
1952 return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input));
1953 }
1954 }
1955
Adam Cohen61033d32010-11-15 18:29:44 -08001956 private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001957
Michael Jurka3e7c7632010-10-02 16:01:03 -07001958 /*
Adam Cohen66396872011-04-15 17:50:36 -07001959 *
1960 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we
1961 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
1962 *
1963 * These methods mark the appropriate pages as accepting drops (which alters their visual
1964 * appearance).
1965 *
1966 */
1967 public void onDragStartedWithItem(View v) {
Adam Cohen66396872011-04-15 17:50:36 -07001968 final Canvas canvas = new Canvas();
1969
Adam Cohen66396872011-04-15 17:50:36 -07001970 // The outline is used to visualize where the item will land if dropped
Adam Cohend41fbf52012-02-16 23:53:59 -08001971 mDragOutline = createDragOutline(v, canvas, DRAG_BITMAP_PADDING);
Adam Cohen66396872011-04-15 17:50:36 -07001972 }
1973
Michael Jurka8c3339b2012-06-14 16:18:21 -07001974 public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001975 final Canvas canvas = new Canvas();
1976
Michael Jurka038f9d82011-11-03 13:50:45 -07001977 int[] size = estimateItemSize(info.spanX, info.spanY, info, false);
Adam Cohen66396872011-04-15 17:50:36 -07001978
Michael Jurkad3ef3062010-11-23 16:23:58 -08001979 // The outline is used to visualize where the item will land if dropped
Adam Cohend41fbf52012-02-16 23:53:59 -08001980 mDragOutline = createDragOutline(b, canvas, DRAG_BITMAP_PADDING, size[0],
Michael Jurka8c3339b2012-06-14 16:18:21 -07001981 size[1], clipAlpha);
Michael Jurka3e7c7632010-10-02 16:01:03 -07001982 }
1983
Patrick Dubroy758a9232011-03-03 19:54:56 -08001984 public void exitWidgetResizeMode() {
Adam Cohen716b51e2011-06-30 12:09:54 -07001985 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohen67882692011-03-11 15:29:03 -08001986 dragLayer.clearAllResizeFrames();
Patrick Dubroy758a9232011-03-03 19:54:56 -08001987 }
1988
Adam Cohen4b285c52011-07-21 14:24:06 -07001989 private void initAnimationArrays() {
1990 final int childCount = getChildCount();
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 if (mLastChildCount == childCount) return;
Adam Cohen7d30a372013-07-01 17:03:59 -07001992
Adam Cohen4b285c52011-07-21 14:24:06 -07001993 mOldBackgroundAlphas = new float[childCount];
Adam Cohen4b285c52011-07-21 14:24:06 -07001994 mOldAlphas = new float[childCount];
Adam Cohen4b285c52011-07-21 14:24:06 -07001995 mNewBackgroundAlphas = new float[childCount];
Adam Cohen4b285c52011-07-21 14:24:06 -07001996 mNewAlphas = new float[childCount];
Adam Cohen4b285c52011-07-21 14:24:06 -07001997 }
1998
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001999 Animator getChangeStateAnimation(final State state, boolean animated) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002000 return getChangeStateAnimation(state, animated, 0, -1);
Adam Cohen7777d962011-08-18 18:58:38 -07002001 }
2002
Adam Cohenf358a4b2013-07-23 16:47:31 -07002003 @Override
2004 protected void getOverviewModePages(int[] range) {
Michael Jurkafe0ace32013-10-03 01:05:14 -07002005 int start = numCustomPages();
Adam Cohen1003be92013-09-16 14:09:28 -07002006 int end = getChildCount() - 1;
2007
2008 range[0] = Math.max(0, Math.min(start, getChildCount() - 1));
2009 range[1] = Math.max(0, end);
Adam Cohendedbd962013-07-11 14:21:49 -07002010 }
2011
2012 protected void onStartReordering() {
2013 super.onStartReordering();
Adam Cohendedbd962013-07-11 14:21:49 -07002014 showOutlines();
Adam Cohen22cba7f2013-07-19 16:14:00 -07002015 // Reordering handles its own animations, disable the automatic ones.
Winson Chung964df6b2013-10-11 15:55:37 -07002016 disableLayoutTransitions();
Adam Cohendedbd962013-07-11 14:21:49 -07002017 }
2018
2019 protected void onEndReordering() {
2020 super.onEndReordering();
Adam Cohendedbd962013-07-11 14:21:49 -07002021
Adam Cohen2bf63d52013-09-29 17:46:49 -07002022 hideOutlines();
Adam Cohendedbd962013-07-11 14:21:49 -07002023 mScreenOrder.clear();
Adam Cohen2bf63d52013-09-29 17:46:49 -07002024 int count = getChildCount();
Adam Cohendedbd962013-07-11 14:21:49 -07002025 for (int i = 0; i < count; i++) {
2026 CellLayout cl = ((CellLayout) getChildAt(i));
2027 mScreenOrder.add(getIdForScreen(cl));
2028 }
Winson Chungd64d1762013-08-20 14:37:16 -07002029
Adam Cohendedbd962013-07-11 14:21:49 -07002030 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
Adam Cohen22cba7f2013-07-19 16:14:00 -07002031
2032 // Re-enable auto layout transitions for page deletion.
Winson Chung964df6b2013-10-11 15:55:37 -07002033 enableLayoutTransitions();
Adam Cohendedbd962013-07-11 14:21:49 -07002034 }
2035
Adam Cohenf358a4b2013-07-23 16:47:31 -07002036 public boolean isInOverviewMode() {
2037 return mState == State.OVERVIEW;
2038 }
2039
Adam Cohen93c97562013-09-26 13:48:01 -07002040 public boolean enterOverviewMode() {
2041 if (mTouchState != TOUCH_STATE_REST) {
2042 return false;
2043 }
Michael Jurka336fd4f2013-09-12 00:05:02 +02002044 enableOverviewMode(true, -1, true);
Adam Cohen93c97562013-09-26 13:48:01 -07002045 return true;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002046 }
2047
Michael Jurka336fd4f2013-09-12 00:05:02 +02002048 public void exitOverviewMode(boolean animated) {
2049 exitOverviewMode(-1, animated);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002050 }
2051
Michael Jurka336fd4f2013-09-12 00:05:02 +02002052 public void exitOverviewMode(int snapPage, boolean animated) {
2053 enableOverviewMode(false, snapPage, animated);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002054 }
2055
Michael Jurka336fd4f2013-09-12 00:05:02 +02002056 private void enableOverviewMode(boolean enable, int snapPage, boolean animated) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002057 State finalState = Workspace.State.OVERVIEW;
2058 if (!enable) {
2059 finalState = Workspace.State.NORMAL;
2060 }
2061
Michael Jurka336fd4f2013-09-12 00:05:02 +02002062 Animator workspaceAnim = getChangeStateAnimation(finalState, animated, 0, snapPage);
2063 if (workspaceAnim != null) {
Adam Cohenf3434992013-09-16 16:52:59 -07002064 onTransitionPrepare();
Michael Jurka336fd4f2013-09-12 00:05:02 +02002065 workspaceAnim.addListener(new AnimatorListenerAdapter() {
2066 @Override
2067 public void onAnimationEnd(Animator arg0) {
Adam Cohenf3434992013-09-16 16:52:59 -07002068 onTransitionEnd();
Michael Jurka336fd4f2013-09-12 00:05:02 +02002069 }
2070 });
2071 workspaceAnim.start();
2072 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002073 }
2074
Adam Cohen410f3cd2013-09-22 12:09:32 -07002075 int getOverviewModeTranslationY() {
Winson Chungc82d2622013-11-06 13:23:29 -08002076 LauncherAppState app = LauncherAppState.getInstance();
2077 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
2078 Rect overviewBar = grid.getOverviewModeButtonBarRect();
Adam Cohen410f3cd2013-09-22 12:09:32 -07002079
Winson Chungc82d2622013-11-06 13:23:29 -08002080 int availableHeight = getViewportHeight();
2081 int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight());
2082 int offsetFromTopEdge = (availableHeight - scaledHeight) / 2;
2083 int offsetToCenterInOverview = (availableHeight - mInsets.top - overviewBar.height()
2084 - scaledHeight) / 2;
Adam Cohen410f3cd2013-09-22 12:09:32 -07002085
Winson Chungc82d2622013-11-06 13:23:29 -08002086 return -offsetFromTopEdge + mInsets.top + offsetToCenterInOverview;
Adam Cohen410f3cd2013-09-22 12:09:32 -07002087 }
2088
Winson Chung5841efa2013-09-30 18:06:44 -07002089 boolean shouldVoiceButtonProxyBeVisible() {
2090 if (isOnOrMovingToCustomContent()) {
2091 return false;
2092 }
2093 if (mState != State.NORMAL) {
2094 return false;
2095 }
2096 return true;
2097 }
2098
Adam Cohenedaaa302013-10-01 17:33:27 -07002099 public void updateInteractionForState() {
2100 if (mState != State.NORMAL) {
2101 mLauncher.onInteractionBegin();
2102 } else {
2103 mLauncher.onInteractionEnd();
2104 }
2105 }
2106
2107 private void setState(State state) {
2108 mState = state;
2109 updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07002110 updateAccessibilityFlags();
2111 }
2112
2113 private void updateAccessibilityFlags() {
2114 int accessible = mState == State.NORMAL ?
2115 ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES :
2116 ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
2117 setImportantForAccessibility(accessible);
Adam Cohenedaaa302013-10-01 17:33:27 -07002118 }
2119
Adam Cohenf358a4b2013-07-23 16:47:31 -07002120 Animator getChangeStateAnimation(final State state, boolean animated, int delay, int snapPage) {
Michael Jurkabdf78552011-10-31 14:34:25 -07002121 if (mState == state) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002122 return null;
Michael Jurka99633da2011-07-27 22:40:17 -07002123 }
2124
Winson Chung1b7c1d32011-10-25 12:09:38 -07002125 // Initialize animation arrays for the first time if necessary
2126 initAnimationArrays();
2127
Michael Jurka2ecf9952012-06-18 12:52:28 -07002128 AnimatorSet anim = animated ? LauncherAnimUtils.createAnimatorSet() : null;
Adam Lesinski6b879f02010-11-04 16:15:23 -07002129
Winson Chung1b7c1d32011-10-25 12:09:38 -07002130 final State oldState = mState;
2131 final boolean oldStateIsNormal = (oldState == State.NORMAL);
Michael Jurka4ff7d792012-04-02 03:46:50 -07002132 final boolean oldStateIsSpringLoaded = (oldState == State.SPRING_LOADED);
Winson Chung1b7c1d32011-10-25 12:09:38 -07002133 final boolean oldStateIsSmall = (oldState == State.SMALL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002134 final boolean oldStateIsOverview = (oldState == State.OVERVIEW);
Adam Cohenedaaa302013-10-01 17:33:27 -07002135 setState(state);
Winson Chung1b7c1d32011-10-25 12:09:38 -07002136 final boolean stateIsNormal = (state == State.NORMAL);
2137 final boolean stateIsSpringLoaded = (state == State.SPRING_LOADED);
2138 final boolean stateIsSmall = (state == State.SMALL);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002139 final boolean stateIsOverview = (state == State.OVERVIEW);
Winson Chung2d75f122013-09-23 16:53:31 -07002140 float finalBackgroundAlpha = (stateIsSpringLoaded || stateIsOverview) ? 1.0f : 0f;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002141 float finalHotseatAndPageIndicatorAlpha = (stateIsOverview || stateIsSmall) ? 0f : 1f;
2142 float finalOverviewPanelAlpha = stateIsOverview ? 1f : 0f;
Winson Chung2d75f122013-09-23 16:53:31 -07002143 float finalSearchBarAlpha = !stateIsNormal ? 0f : 1f;
Adam Cohen410f3cd2013-09-22 12:09:32 -07002144 float finalWorkspaceTranslationY = stateIsOverview ? getOverviewModeTranslationY() : 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002145
Winson Chungf4bd2362013-10-07 17:11:27 -07002146 boolean workspaceToAllApps = (oldStateIsNormal && stateIsSmall);
2147 boolean allAppsToWorkspace = (oldStateIsSmall && stateIsNormal);
Adam Cohen58993ad2013-10-11 17:57:38 -07002148 boolean workspaceToOverview = (oldStateIsNormal && stateIsOverview);
2149 boolean overviewToWorkspace = (oldStateIsOverview && stateIsNormal);
2150
Adam Cohen7d30a372013-07-01 17:03:59 -07002151 mNewScale = 1.0f;
Adam Cohenbeff8c62011-08-31 17:46:01 -07002152
Adam Cohenf358a4b2013-07-23 16:47:31 -07002153 if (oldStateIsOverview) {
Adam Cohenf9618852013-11-08 06:45:03 -08002154 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002155 } else if (stateIsOverview) {
2156 enableFreeScroll();
2157 }
2158
Adam Cohen7777d962011-08-18 18:58:38 -07002159 if (state != State.NORMAL) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002160 if (stateIsSpringLoaded) {
2161 mNewScale = mSpringLoadedShrinkFactor;
2162 } else if (stateIsOverview) {
2163 mNewScale = mOverviewModeShrinkFactor;
2164 } else if (stateIsSmall){
Adam Cohenaf9b0e52013-09-23 19:27:38 -07002165 mNewScale = mOverviewModeShrinkFactor - 0.3f;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002166 }
Winson Chungf4bd2362013-10-07 17:11:27 -07002167 if (workspaceToAllApps) {
Michael Jurka3a0469d2012-06-21 09:38:41 -07002168 updateChildrenLayersEnabled(false);
Adam Cohen7777d962011-08-18 18:58:38 -07002169 }
Adam Cohen7777d962011-08-18 18:58:38 -07002170 }
Winson Chungf4bd2362013-10-07 17:11:27 -07002171
Adam Cohenfa2450a2013-10-11 18:20:27 -07002172 final int duration;
2173 if (workspaceToAllApps) {
2174 duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime);
2175 } else if (workspaceToOverview || overviewToWorkspace) {
2176 duration = getResources().getInteger(R.integer.config_overviewTransitionTime);
2177 } else {
2178 duration = getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime);
2179 }
2180
Adam Cohenf9618852013-11-08 06:45:03 -08002181 if (snapPage == -1) {
2182 snapPage = getPageNearestToCenterOfScreen();
2183 }
2184 snapToPage(snapPage, duration, mZoomInInterpolator);
2185
Winson Chung1b7c1d32011-10-25 12:09:38 -07002186 for (int i = 0; i < getChildCount(); i++) {
2187 final CellLayout cl = (CellLayout) getChildAt(i);
Adam Cohenf9618852013-11-08 06:45:03 -08002188 boolean isCurrentPage = (i == snapPage);
Winson Chung2d75f122013-09-23 16:53:31 -07002189 float initialAlpha = cl.getShortcutsAndWidgets().getAlpha();
Adam Cohen3b185e22013-10-29 14:45:58 -07002190 float finalAlpha;
2191 if (stateIsSmall) {
2192 finalAlpha = 0f;
2193 } else if (stateIsNormal && mWorkspaceFadeInAdjacentScreens) {
Adam Cohenf9618852013-11-08 06:45:03 -08002194 finalAlpha = (i == snapPage || i < numCustomPages()) ? 1f : 0f;
Adam Cohen3b185e22013-10-29 14:45:58 -07002195 } else {
2196 finalAlpha = 1f;
2197 }
Winson Chungf4bd2362013-10-07 17:11:27 -07002198
2199 // If we are animating to/from the small state, then hide the side pages and fade the
2200 // current page in
2201 if (!mIsSwitchingState) {
2202 if (workspaceToAllApps || allAppsToWorkspace) {
2203 if (allAppsToWorkspace && isCurrentPage) {
2204 initialAlpha = 0f;
2205 } else if (!isCurrentPage) {
2206 initialAlpha = finalAlpha = 0f;
2207 }
2208 cl.setShortcutAndWidgetAlpha(initialAlpha);
2209 }
2210 }
Adam Cohen7777d962011-08-18 18:58:38 -07002211
Winson Chung1b7c1d32011-10-25 12:09:38 -07002212 mOldAlphas[i] = initialAlpha;
2213 mNewAlphas[i] = finalAlpha;
Adam Cohen7777d962011-08-18 18:58:38 -07002214 if (animated) {
Adam Cohen7777d962011-08-18 18:58:38 -07002215 mOldBackgroundAlphas[i] = cl.getBackgroundAlpha();
Adam Cohen7777d962011-08-18 18:58:38 -07002216 mNewBackgroundAlphas[i] = finalBackgroundAlpha;
Adam Cohen7777d962011-08-18 18:58:38 -07002217 } else {
Winson Chung1b7c1d32011-10-25 12:09:38 -07002218 cl.setBackgroundAlpha(finalBackgroundAlpha);
Michael Jurkaa52570f2012-03-20 03:18:20 -07002219 cl.setShortcutAndWidgetAlpha(finalAlpha);
Adam Cohen7777d962011-08-18 18:58:38 -07002220 }
Michael Jurkadee05892010-07-27 10:01:56 -07002221 }
Winson Chung8d006d52010-11-29 15:55:29 -08002222
Adam Cohen3d411982013-09-24 17:02:06 -07002223 final View searchBar = mLauncher.getQsbBar();
2224 final View overviewPanel = mLauncher.getOverviewPanel();
2225 final View hotseat = mLauncher.getHotseat();
Michael Jurka853f5a42013-11-25 14:32:21 +01002226 final View pageIndicator = getPageIndicator();
Adam Cohen7777d962011-08-18 18:58:38 -07002227 if (animated) {
Adam Cohenfa2450a2013-10-11 18:20:27 -07002228 anim.setDuration(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002229 LauncherViewPropertyAnimator scale = new LauncherViewPropertyAnimator(this);
2230 scale.scaleX(mNewScale)
2231 .scaleY(mNewScale)
Adam Cohen410f3cd2013-09-22 12:09:32 -07002232 .translationY(finalWorkspaceTranslationY)
Adam Cohen7d30a372013-07-01 17:03:59 -07002233 .setInterpolator(mZoomInInterpolator);
2234 anim.play(scale);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002235 for (int index = 0; index < getChildCount(); index++) {
2236 final int i = index;
2237 final CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurka869390b2012-05-06 15:55:19 -07002238 float currentAlpha = cl.getShortcutsAndWidgets().getAlpha();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002239 if (mOldAlphas[i] == 0 && mNewAlphas[i] == 0) {
Michael Jurka7372c592012-01-16 04:21:35 -08002240 cl.setBackgroundAlpha(mNewBackgroundAlphas[i]);
Michael Jurkaa52570f2012-03-20 03:18:20 -07002241 cl.setShortcutAndWidgetAlpha(mNewAlphas[i]);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002242 } else {
Michael Jurka869390b2012-05-06 15:55:19 -07002243 if (mOldAlphas[i] != mNewAlphas[i] || currentAlpha != mNewAlphas[i]) {
Michael Jurka6c8a5792012-03-22 05:24:37 -07002244 LauncherViewPropertyAnimator alphaAnim =
2245 new LauncherViewPropertyAnimator(cl.getShortcutsAndWidgets());
Michael Jurkaa52570f2012-03-20 03:18:20 -07002246 alphaAnim.alpha(mNewAlphas[i])
Michael Jurkaa52570f2012-03-20 03:18:20 -07002247 .setInterpolator(mZoomInInterpolator);
2248 anim.play(alphaAnim);
2249 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002250 if (mOldBackgroundAlphas[i] != 0 ||
Michael Jurka869390b2012-05-06 15:55:19 -07002251 mNewBackgroundAlphas[i] != 0) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002252 ValueAnimator bgAnim =
Adam Cohenfa2450a2013-10-11 18:20:27 -07002253 LauncherAnimUtils.ofFloat(cl, 0f, 1f);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002254 bgAnim.setInterpolator(mZoomInInterpolator);
2255 bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2256 public void onAnimationUpdate(float a, float b) {
Michael Jurka7372c592012-01-16 04:21:35 -08002257 cl.setBackgroundAlpha(
Michael Jurka7407d2a2011-12-12 21:48:38 -08002258 a * mOldBackgroundAlphas[i] +
2259 b * mNewBackgroundAlphas[i]);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002260 }
2261 });
2262 anim.play(bgAnim);
2263 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002264 }
2265 }
Michael Jurka853f5a42013-11-25 14:32:21 +01002266 Animator pageIndicatorAlpha = null;
2267 if (pageIndicator != null) {
2268 pageIndicatorAlpha = new LauncherViewPropertyAnimator(pageIndicator)
2269 .alpha(finalHotseatAndPageIndicatorAlpha).withLayer();
Michael Jurka1c00fd02013-12-02 16:11:22 -08002270 pageIndicatorAlpha.addListener(new AlphaUpdateListener(pageIndicator));
Michael Jurka853f5a42013-11-25 14:32:21 +01002271 } else {
2272 // create a dummy animation so we don't need to do null checks later
2273 pageIndicatorAlpha = ValueAnimator.ofFloat(0, 0);
Winson Chung66700732013-08-20 16:56:15 -07002274 }
Michael Jurka1c00fd02013-12-02 16:11:22 -08002275
Michael Jurka853f5a42013-11-25 14:32:21 +01002276 Animator hotseatAlpha = new LauncherViewPropertyAnimator(hotseat)
2277 .alpha(finalHotseatAndPageIndicatorAlpha).withLayer();
Michael Jurka1c00fd02013-12-02 16:11:22 -08002278 hotseatAlpha.addListener(new AlphaUpdateListener(hotseat));
2279
Michael Jurka853f5a42013-11-25 14:32:21 +01002280 Animator searchBarAlpha = new LauncherViewPropertyAnimator(searchBar)
2281 .alpha(finalSearchBarAlpha).withLayer();
Michael Jurka1c00fd02013-12-02 16:11:22 -08002282 searchBarAlpha.addListener(new AlphaUpdateListener(searchBar));
2283
Michael Jurka853f5a42013-11-25 14:32:21 +01002284 Animator overviewPanelAlpha = new LauncherViewPropertyAnimator(overviewPanel)
2285 .alpha(finalOverviewPanelAlpha).withLayer();
Michael Jurka853f5a42013-11-25 14:32:21 +01002286 overviewPanelAlpha.addListener(new AlphaUpdateListener(overviewPanel));
Adam Cohen3d411982013-09-24 17:02:06 -07002287
Adam Cohen58993ad2013-10-11 17:57:38 -07002288 if (workspaceToOverview) {
Michael Jurka853f5a42013-11-25 14:32:21 +01002289 pageIndicatorAlpha.setInterpolator(new DecelerateInterpolator(2));
Adam Cohen58993ad2013-10-11 17:57:38 -07002290 hotseatAlpha.setInterpolator(new DecelerateInterpolator(2));
Michael Jurka853f5a42013-11-25 14:32:21 +01002291 overviewPanelAlpha.setInterpolator(null);
Adam Cohen58993ad2013-10-11 17:57:38 -07002292 } else if (overviewToWorkspace) {
Michael Jurka853f5a42013-11-25 14:32:21 +01002293 pageIndicatorAlpha.setInterpolator(null);
2294 hotseatAlpha.setInterpolator(null);
Adam Cohen58993ad2013-10-11 17:57:38 -07002295 overviewPanelAlpha.setInterpolator(new DecelerateInterpolator(2));
2296 }
Michael Jurka853f5a42013-11-25 14:32:21 +01002297 searchBarAlpha.setInterpolator(null);
Adam Cohen3d411982013-09-24 17:02:06 -07002298
Adam Cohenf358a4b2013-07-23 16:47:31 -07002299 anim.play(overviewPanelAlpha);
2300 anim.play(hotseatAlpha);
Adam Cohendbdff6b2013-09-18 19:09:15 -07002301 anim.play(searchBarAlpha);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002302 anim.play(pageIndicatorAlpha);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002303 anim.setStartDelay(delay);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002304 } else {
Adam Cohen3d411982013-09-24 17:02:06 -07002305 overviewPanel.setAlpha(finalOverviewPanelAlpha);
2306 AlphaUpdateListener.updateVisibility(overviewPanel);
2307 hotseat.setAlpha(finalHotseatAndPageIndicatorAlpha);
2308 AlphaUpdateListener.updateVisibility(hotseat);
Michael Jurka853f5a42013-11-25 14:32:21 +01002309 if (pageIndicator != null) {
2310 pageIndicator.setAlpha(finalHotseatAndPageIndicatorAlpha);
2311 AlphaUpdateListener.updateVisibility(pageIndicator);
Winson Chung66700732013-08-20 16:56:15 -07002312 }
Adam Cohendbdff6b2013-09-18 19:09:15 -07002313 searchBar.setAlpha(finalSearchBarAlpha);
Adam Cohen3f452c82013-09-19 11:57:17 -07002314 AlphaUpdateListener.updateVisibility(searchBar);
2315 updateCustomContentVisibility();
Adam Cohen410f3cd2013-09-22 12:09:32 -07002316 setScaleX(mNewScale);
2317 setScaleY(mNewScale);
2318 setTranslationY(finalWorkspaceTranslationY);
Adam Cohendbdff6b2013-09-18 19:09:15 -07002319 }
Winson Chung5841efa2013-09-30 18:06:44 -07002320 mLauncher.updateVoiceButtonProxyVisible(false);
Adam Cohen7777d962011-08-18 18:58:38 -07002321
Winson Chung1b7c1d32011-10-25 12:09:38 -07002322 if (stateIsSpringLoaded) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002323 // Right now we're covered by Apps Customize
2324 // Show the background gradient immediately, so the gradient will
2325 // be showing once AppsCustomize disappears
2326 animateBackgroundGradient(getResources().getInteger(
2327 R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, false);
Adam Cohendbdff6b2013-09-18 19:09:15 -07002328 } else if (stateIsOverview) {
2329 animateBackgroundGradient(getResources().getInteger(
2330 R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, true);
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002331 } else {
2332 // Fade the background gradient away
Adam Cohened307df2013-10-02 09:37:31 -07002333 animateBackgroundGradient(0f, animated);
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002334 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002335 return anim;
Michael Jurkadee05892010-07-27 10:01:56 -07002336 }
2337
Adam Cohena5f4e482013-10-11 12:10:28 -07002338 static class AlphaUpdateListener implements AnimatorUpdateListener, AnimatorListener {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002339 View view;
2340 public AlphaUpdateListener(View v) {
2341 view = v;
2342 }
2343
2344 @Override
2345 public void onAnimationUpdate(ValueAnimator arg0) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002346 updateVisibility(view);
2347 }
2348
2349 public static void updateVisibility(View view) {
Adam Cohen53805212013-10-01 10:39:23 -07002350 // We want to avoid the extra layout pass by setting the views to GONE unless
2351 // accessibility is on, in which case not setting them to GONE causes a glitch.
2352 int invisibleState = sAccessibilityEnabled ? GONE : INVISIBLE;
2353 if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != invisibleState) {
2354 view.setVisibility(invisibleState);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002355 } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD
2356 && view.getVisibility() != VISIBLE) {
2357 view.setVisibility(VISIBLE);
2358 }
2359 }
Adam Cohena5f4e482013-10-11 12:10:28 -07002360
2361 @Override
2362 public void onAnimationCancel(Animator arg0) {
2363 }
2364
2365 @Override
2366 public void onAnimationEnd(Animator arg0) {
2367 updateVisibility(view);
2368 }
2369
2370 @Override
2371 public void onAnimationRepeat(Animator arg0) {
2372 }
2373
2374 @Override
2375 public void onAnimationStart(Animator arg0) {
2376 // We want the views to be visible for animation, so fade-in/out is visible
2377 view.setVisibility(VISIBLE);
2378 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002379 }
2380
Michael Jurkabed61d22012-02-14 22:51:29 -08002381 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002382 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohenf3434992013-09-16 16:52:59 -07002383 onTransitionPrepare();
Michael Jurkabed61d22012-02-14 22:51:29 -08002384 }
2385
2386 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002387 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
2388 }
2389
2390 @Override
Winson Chung70442722012-02-10 15:43:22 -08002391 public void onLauncherTransitionStep(Launcher l, float t) {
2392 mTransitionProgress = t;
2393 }
2394
2395 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -08002396 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohenf3434992013-09-16 16:52:59 -07002397 onTransitionEnd();
2398 }
2399
2400 private void onTransitionPrepare() {
2401 mIsSwitchingState = true;
Winson Chungf4bd2362013-10-07 17:11:27 -07002402
2403 // Invalidate here to ensure that the pages are rendered during the state change transition.
2404 invalidate();
2405
Adam Cohenf3434992013-09-16 16:52:59 -07002406 updateChildrenLayersEnabled(false);
Adam Cohen3f452c82013-09-19 11:57:17 -07002407 hideCustomContentIfNecessary();
2408 }
2409
2410 void updateCustomContentVisibility() {
2411 int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE;
2412 if (hasCustomContent()) {
2413 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility);
2414 }
2415 }
2416
2417 void showCustomContentIfNecessary() {
2418 boolean show = mState == Workspace.State.NORMAL;
2419 if (show && hasCustomContent()) {
2420 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE);
2421 }
2422 }
2423
2424 void hideCustomContentIfNecessary() {
2425 boolean hide = mState != Workspace.State.NORMAL;
2426 if (hide && hasCustomContent()) {
Adam Cohen1774a8a2013-11-15 10:56:16 +00002427 disableLayoutTransitions();
Adam Cohen3f452c82013-09-19 11:57:17 -07002428 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE);
Adam Cohen1774a8a2013-11-15 10:56:16 +00002429 enableLayoutTransitions();
Adam Cohenf3434992013-09-16 16:52:59 -07002430 }
2431 }
2432
2433 private void onTransitionEnd() {
Michael Jurkabed61d22012-02-14 22:51:29 -08002434 mIsSwitchingState = false;
Michael Jurka3a0469d2012-06-21 09:38:41 -07002435 updateChildrenLayersEnabled(false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002436 // The code in getChangeStateAnimation to determine initialAlpha and finalAlpha will ensure
2437 // ensure that only the current page is visible during (and subsequently, after) the
2438 // transition animation. If fade adjacent pages is disabled, then re-enable the page
2439 // visibility after the transition animation.
Michael Jurka869390b2012-05-06 15:55:19 -07002440 if (!mWorkspaceFadeInAdjacentScreens) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002441 for (int i = 0; i < getChildCount(); i++) {
2442 final CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurkaa52570f2012-03-20 03:18:20 -07002443 cl.setShortcutAndWidgetAlpha(1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002444 }
Adam Cohen76128b62013-11-14 16:43:59 +00002445 } else {
2446 for (int i = 0; i < numCustomPages(); i++) {
2447 final CellLayout cl = (CellLayout) getChildAt(i);
2448 cl.setShortcutAndWidgetAlpha(1f);
2449 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002450 }
Adam Cohen3f452c82013-09-19 11:57:17 -07002451 showCustomContentIfNecessary();
Michael Jurkabed61d22012-02-14 22:51:29 -08002452 }
2453
2454 @Override
2455 public View getContent() {
2456 return this;
2457 }
2458
Joe Onorato4be866d2010-10-10 11:26:02 -07002459 /**
2460 * Draw the View v into the given Canvas.
2461 *
2462 * @param v the view to draw
2463 * @param destCanvas the canvas to draw on
2464 * @param padding the horizontal and vertical padding to use when drawing
2465 */
Adam Cohenac8c8762011-07-13 11:15:27 -07002466 private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
Joe Onorato4be866d2010-10-10 11:26:02 -07002467 final Rect clipRect = mTempRect;
2468 v.getDrawingRect(clipRect);
2469
Adam Cohen099f60d2011-08-23 21:07:26 -07002470 boolean textVisible = false;
2471
Adam Cohenac8c8762011-07-13 11:15:27 -07002472 destCanvas.save();
2473 if (v instanceof TextView && pruneToDrawable) {
2474 Drawable d = ((TextView) v).getCompoundDrawables()[1];
2475 clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
2476 destCanvas.translate(padding / 2, padding / 2);
2477 d.draw(destCanvas);
2478 } else {
2479 if (v instanceof FolderIcon) {
Adam Cohen099f60d2011-08-23 21:07:26 -07002480 // For FolderIcons the text can bleed into the icon area, and so we need to
2481 // hide the text completely (which can't be achieved by clipping).
2482 if (((FolderIcon) v).getTextVisible()) {
2483 ((FolderIcon) v).setTextVisible(false);
2484 textVisible = true;
2485 }
Adam Cohenac8c8762011-07-13 11:15:27 -07002486 } else if (v instanceof BubbleTextView) {
2487 final BubbleTextView tv = (BubbleTextView) v;
2488 clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V +
2489 tv.getLayout().getLineTop(0);
2490 } else if (v instanceof TextView) {
2491 final TextView tv = (TextView) v;
2492 clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() +
2493 tv.getLayout().getLineTop(0);
2494 }
2495 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
2496 destCanvas.clipRect(clipRect, Op.REPLACE);
2497 v.draw(destCanvas);
Adam Cohen099f60d2011-08-23 21:07:26 -07002498
2499 // Restore text visibility of FolderIcon if necessary
2500 if (textVisible) {
2501 ((FolderIcon) v).setTextVisible(true);
2502 }
Adam Cohenac8c8762011-07-13 11:15:27 -07002503 }
2504 destCanvas.restore();
2505 }
2506
2507 /**
2508 * Returns a new bitmap to show when the given View is being dragged around.
2509 * Responsibility for the bitmap is transferred to the caller.
2510 */
2511 public Bitmap createDragBitmap(View v, Canvas canvas, int padding) {
Adam Cohenac8c8762011-07-13 11:15:27 -07002512 Bitmap b;
2513
2514 if (v instanceof TextView) {
2515 Drawable d = ((TextView) v).getCompoundDrawables()[1];
2516 b = Bitmap.createBitmap(d.getIntrinsicWidth() + padding,
2517 d.getIntrinsicHeight() + padding, Bitmap.Config.ARGB_8888);
2518 } else {
2519 b = Bitmap.createBitmap(
2520 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
Joe Onorato4be866d2010-10-10 11:26:02 -07002521 }
2522
Adam Cohenac8c8762011-07-13 11:15:27 -07002523 canvas.setBitmap(b);
2524 drawDragView(v, canvas, padding, true);
Adam Cohenaaf473c2011-08-03 12:02:47 -07002525 canvas.setBitmap(null);
Joe Onorato4be866d2010-10-10 11:26:02 -07002526
Adam Cohenac8c8762011-07-13 11:15:27 -07002527 return b;
Joe Onorato4be866d2010-10-10 11:26:02 -07002528 }
2529
2530 /**
2531 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
2532 * Responsibility for the bitmap is transferred to the caller.
2533 */
2534 private Bitmap createDragOutline(View v, Canvas canvas, int padding) {
Adam Cohen410f3cd2013-09-22 12:09:32 -07002535 final int outlineColor = getResources().getColor(R.color.outline_color);
Joe Onorato4be866d2010-10-10 11:26:02 -07002536 final Bitmap b = Bitmap.createBitmap(
2537 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
2538
2539 canvas.setBitmap(b);
Winson Chungb8c69f32011-10-19 21:36:08 -07002540 drawDragView(v, canvas, padding, true);
Adam Cohen5bb50bd2010-12-03 11:39:55 -08002541 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -07002542 canvas.setBitmap(null);
Joe Onorato4be866d2010-10-10 11:26:02 -07002543 return b;
2544 }
2545
2546 /**
Michael Jurkad3ef3062010-11-23 16:23:58 -08002547 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
2548 * Responsibility for the bitmap is transferred to the caller.
2549 */
Peter Ng8db70002011-10-25 15:40:08 -07002550 private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h,
Michael Jurka8c3339b2012-06-14 16:18:21 -07002551 boolean clipAlpha) {
Adam Cohen410f3cd2013-09-22 12:09:32 -07002552 final int outlineColor = getResources().getColor(R.color.outline_color);
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002553 final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002554 canvas.setBitmap(b);
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002555
2556 Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight());
2557 float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(),
2558 (h - padding) / (float) orig.getHeight());
2559 int scaledWidth = (int) (scaleFactor * orig.getWidth());
2560 int scaledHeight = (int) (scaleFactor * orig.getHeight());
2561 Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);
2562
2563 // center the image
2564 dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2);
2565
Winson Chung1120e032011-11-22 16:11:31 -08002566 canvas.drawBitmap(orig, src, dst, null);
Peter Ng8db70002011-10-25 15:40:08 -07002567 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor,
Michael Jurka8c3339b2012-06-14 16:18:21 -07002568 clipAlpha);
Adam Cohenaaf473c2011-08-03 12:02:47 -07002569 canvas.setBitmap(null);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002570
2571 return b;
2572 }
2573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 void startDrag(CellLayout.CellInfo cellInfo) {
2575 View child = cellInfo.cell;
Winson Chungaafa03c2010-06-11 17:34:16 -07002576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 // Make sure the drag was started by a long press as opposed to a long click.
Bjorn Bringert7984c942009-12-09 15:38:25 +00002578 if (!child.isInTouchMode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 return;
2580 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 mDragInfo = cellInfo;
Adam Cohend41fbf52012-02-16 23:53:59 -08002583 child.setVisibility(INVISIBLE);
Adam Cohen482ed822012-03-02 14:15:13 -08002584 CellLayout layout = (CellLayout) child.getParent().getParent();
2585 layout.prepareChildForDrag(child);
Joe Onorato4be866d2010-10-10 11:26:02 -07002586
2587 child.clearFocus();
2588 child.setPressed(false);
2589
2590 final Canvas canvas = new Canvas();
2591
2592 // The outline is used to visualize where the item will land if dropped
Adam Cohend41fbf52012-02-16 23:53:59 -08002593 mDragOutline = createDragOutline(child, canvas, DRAG_BITMAP_PADDING);
Adam Cohenac8c8762011-07-13 11:15:27 -07002594 beginDragShared(child, this);
2595 }
2596
2597 public void beginDragShared(View child, DragSource source) {
Joe Onorato4be866d2010-10-10 11:26:02 -07002598 // The drag bitmap follows the touch point around on the screen
Adam Cohend41fbf52012-02-16 23:53:59 -08002599 final Bitmap b = createDragBitmap(child, new Canvas(), DRAG_BITMAP_PADDING);
Joe Onorato4be866d2010-10-10 11:26:02 -07002600
2601 final int bmpWidth = b.getWidth();
Winson Chungeecf02d2012-03-02 17:14:58 -08002602 final int bmpHeight = b.getHeight();
Adam Cohene3e27a82011-04-15 12:07:39 -07002603
Adam Cohen307fe232012-08-16 17:55:58 -07002604 float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
Winson Chungeecf02d2012-03-02 17:14:58 -08002605 int dragLayerX =
Adam Cohen307fe232012-08-16 17:55:58 -07002606 Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2);
Winson Chungeecf02d2012-03-02 17:14:58 -08002607 int dragLayerY =
Adam Cohen307fe232012-08-16 17:55:58 -07002608 Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2
Winson Chungeecf02d2012-03-02 17:14:58 -08002609 - DRAG_BITMAP_PADDING / 2);
Adam Cohene3e27a82011-04-15 12:07:39 -07002610
Winson Chung5f8afe62013-08-12 16:19:28 -07002611 LauncherAppState app = LauncherAppState.getInstance();
2612 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8c69f32011-10-19 21:36:08 -07002613 Point dragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -07002614 Rect dragRect = null;
Winson Chungb8c69f32011-10-19 21:36:08 -07002615 if (child instanceof BubbleTextView || child instanceof PagedViewIcon) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002616 int iconSize = grid.iconSizePx;
Adam Cohene3e27a82011-04-15 12:07:39 -07002617 int top = child.getPaddingTop();
2618 int left = (bmpWidth - iconSize) / 2;
2619 int right = left + iconSize;
2620 int bottom = top + iconSize;
Adam Cohenfc53cd22011-07-20 15:45:11 -07002621 dragLayerY += top;
Winson Chungb8c69f32011-10-19 21:36:08 -07002622 // Note: The drag region is used to calculate drag layer offsets, but the
2623 // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
Winson Chung5f8afe62013-08-12 16:19:28 -07002624 dragVisualizeOffset = new Point(-DRAG_BITMAP_PADDING / 2, DRAG_BITMAP_PADDING / 2);
Adam Cohene3e27a82011-04-15 12:07:39 -07002625 dragRect = new Rect(left, top, right, bottom);
Adam Cohen0e4857c2011-06-30 17:05:14 -07002626 } else if (child instanceof FolderIcon) {
Winson Chung5f8afe62013-08-12 16:19:28 -07002627 int previewSize = grid.folderIconSizePx;
2628 dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize);
Adam Cohene3e27a82011-04-15 12:07:39 -07002629 }
2630
Winson Chung1e9cbfe2011-09-30 16:52:26 -07002631 // Clear the pressed state if necessary
2632 if (child instanceof BubbleTextView) {
2633 BubbleTextView icon = (BubbleTextView) child;
2634 icon.clearPressedOrFocusedBackground();
Winson Chungc6c03672014-03-06 10:12:02 -08002635 } else if (child instanceof FolderIcon) {
2636 // Dismiss the folder cling if we haven't already
2637 mLauncher.getLauncherClings().markFolderClingDismissed();
Winson Chung1e9cbfe2011-09-30 16:52:26 -07002638 }
2639
Adam Cohen2f32ad22013-11-13 11:29:49 +00002640 if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) {
2641 String msg = "Drag started with a view that has no tag set. This "
2642 + "will cause a crash (issue 11627249) down the line. "
2643 + "View: " + child + " tag: " + child.getTag();
2644 throw new IllegalStateException(msg);
2645 }
2646
Winson Chungeeb5bbc2013-11-13 15:47:05 -08002647 DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
Adam Cohen307fe232012-08-16 17:55:58 -07002648 DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale);
Winson Chungeeb5bbc2013-11-13 15:47:05 -08002649 dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor());
Adam Cohen5084cba2013-09-03 12:01:16 -07002650
2651 if (child.getParent() instanceof ShortcutAndWidgetContainer) {
2652 mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();
2653 }
2654
Joe Onorato4be866d2010-10-10 11:26:02 -07002655 b.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 }
2657
Adam Cohendcd297f2013-06-18 13:13:40 -07002658 void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002659 int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) {
2660 View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661
2662 final int[] cellXY = new int[2];
Winson Chung3d503fb2011-07-13 17:25:49 -07002663 target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
Adam Cohendcd297f2013-06-18 13:13:40 -07002664 addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
Adam Cohen7c4c5102013-06-14 17:42:35 -07002665
Adam Cohendcd297f2013-06-18 13:13:40 -07002666 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, cellXY[0],
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 }
2669
Adam Cohen4b285c52011-07-21 14:24:06 -07002670 public boolean transitionStateShouldAllowDrop() {
Adam Cohen1b36dc32012-02-13 19:27:37 -08002671 return ((!isSwitchingState() || mTransitionProgress > 0.5f) && mState != State.SMALL);
Adam Cohen4b285c52011-07-21 14:24:06 -07002672 }
2673
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002674 /**
2675 * {@inheritDoc}
2676 */
Adam Cohencb3382b2011-05-24 14:07:08 -07002677 public boolean acceptDrop(DragObject d) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002678 // If it's an external drop (e.g. from All Apps), check if it should be accepted
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002679 CellLayout dropTargetLayout = mDropToLayout;
Adam Cohencb3382b2011-05-24 14:07:08 -07002680 if (d.dragSource != this) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002681 // Don't accept the drop if we're not over a screen at time of drop
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002682 if (dropTargetLayout == null) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002683 return false;
2684 }
Adam Cohen4b285c52011-07-21 14:24:06 -07002685 if (!transitionStateShouldAllowDrop()) return false;
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002686
Adam Cohena65beee2011-06-27 21:32:23 -07002687 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
2688 d.dragView, mDragViewVisualCenter);
2689
Adam Cohen2e9f4fb2011-08-30 22:46:55 -07002690 // We want the point to be mapped to the dragTarget.
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002691 if (mLauncher.isHotseatLayout(dropTargetLayout)) {
Adam Cohen307fe232012-08-16 17:55:58 -07002692 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
Adam Cohen2e9f4fb2011-08-30 22:46:55 -07002693 } else {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002694 mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null);
Adam Cohen2e9f4fb2011-08-30 22:46:55 -07002695 }
2696
Winson Chung557d6ed2011-07-08 15:34:52 -07002697 int spanX = 1;
2698 int spanY = 1;
Winson Chung557d6ed2011-07-08 15:34:52 -07002699 if (mDragInfo != null) {
2700 final CellLayout.CellInfo dragCellInfo = mDragInfo;
2701 spanX = dragCellInfo.spanX;
2702 spanY = dragCellInfo.spanY;
Winson Chung557d6ed2011-07-08 15:34:52 -07002703 } else {
2704 final ItemInfo dragInfo = (ItemInfo) d.dragInfo;
2705 spanX = dragInfo.spanX;
2706 spanY = dragInfo.spanY;
2707 }
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002708
Adam Cohend41fbf52012-02-16 23:53:59 -08002709 int minSpanX = spanX;
2710 int minSpanY = spanY;
2711 if (d.dragInfo instanceof PendingAddWidgetInfo) {
2712 minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX;
2713 minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY;
2714 }
Adam Cohen482ed822012-03-02 14:15:13 -08002715
Adam Cohena65beee2011-06-27 21:32:23 -07002716 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002717 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout,
Adam Cohenf0777b92012-02-28 14:02:45 -08002718 mTargetCell);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002719 float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08002720 mDragViewVisualCenter[1], mTargetCell);
Adam Cohenec0d61d2014-02-07 16:34:51 -08002721 if (mCreateUserFolderOnDrop && willCreateUserFolder((ItemInfo) d.dragInfo,
2722 dropTargetLayout, mTargetCell, distance, true)) {
Adam Cohena65beee2011-06-27 21:32:23 -07002723 return true;
2724 }
Adam Cohenec0d61d2014-02-07 16:34:51 -08002725
2726 if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder((ItemInfo) d.dragInfo,
2727 dropTargetLayout, mTargetCell, distance)) {
Adam Cohena65beee2011-06-27 21:32:23 -07002728 return true;
2729 }
2730
Adam Cohen482ed822012-03-02 14:15:13 -08002731 int[] resultSpan = new int[2];
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002732 mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08002733 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
2734 null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP);
2735 boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
2736
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002737 // Don't accept the drop if there's no room for the item
Adam Cohen482ed822012-03-02 14:15:13 -08002738 if (!foundCell) {
Winson Chung96ef4092011-11-22 12:25:14 -08002739 // Don't show the message if we are dropping on the AllApps button and the hotseat
2740 // is full
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002741 boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
Winson Chung93eef082012-03-23 15:59:27 -07002742 if (mTargetCell != null && isHotseat) {
Winson Chung96ef4092011-11-22 12:25:14 -08002743 Hotseat hotseat = mLauncher.getHotseat();
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002744 if (hotseat.isAllAppsButtonRank(
Winson Chung96ef4092011-11-22 12:25:14 -08002745 hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) {
2746 return false;
2747 }
2748 }
2749
Winson Chung93eef082012-03-23 15:59:27 -07002750 mLauncher.showOutOfSpaceMessage(isHotseat);
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002751 return false;
2752 }
2753 }
Adam Cohendedbd962013-07-11 14:21:49 -07002754
2755 long screenId = getIdForScreen(dropTargetLayout);
2756 if (screenId == EXTRA_EMPTY_SCREEN_ID) {
2757 commitExtraEmptyScreen();
2758 }
2759
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002760 return true;
2761 }
2762
Adam Cohen482ed822012-03-02 14:15:13 -08002763 boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, float
2764 distance, boolean considerTimeout) {
2765 if (distance > mMaxDistanceForFolderCreation) return false;
Adam Cohena65beee2011-06-27 21:32:23 -07002766 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2767
Adam Cohen19f37922012-03-21 11:59:11 -07002768 if (dropOverView != null) {
2769 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2770 if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) {
2771 return false;
2772 }
2773 }
2774
Winson Chung3d503fb2011-07-13 17:25:49 -07002775 boolean hasntMoved = false;
2776 if (mDragInfo != null) {
Adam Cohen482ed822012-03-02 14:15:13 -08002777 hasntMoved = dropOverView == mDragInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002778 }
Adam Cohene0310962011-04-18 16:15:31 -07002779
Adam Cohena65beee2011-06-27 21:32:23 -07002780 if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) {
2781 return false;
2782 }
Adam Cohene0310962011-04-18 16:15:31 -07002783
Adam Cohena65beee2011-06-27 21:32:23 -07002784 boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo);
Adam Cohene0310962011-04-18 16:15:31 -07002785 boolean willBecomeShortcut =
Adam Cohenc0dcf592011-06-01 15:30:43 -07002786 (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
2787 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT);
Adam Cohene0310962011-04-18 16:15:31 -07002788
2789 return (aboveShortcut && willBecomeShortcut);
2790 }
2791
Adam Cohen482ed822012-03-02 14:15:13 -08002792 boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell,
2793 float distance) {
2794 if (distance > mMaxDistanceForFolderCreation) return false;
Adam Cohena65beee2011-06-27 21:32:23 -07002795 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
Adam Cohen19f37922012-03-21 11:59:11 -07002796
2797 if (dropOverView != null) {
2798 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2799 if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) {
2800 return false;
2801 }
2802 }
2803
Adam Cohena65beee2011-06-27 21:32:23 -07002804 if (dropOverView instanceof FolderIcon) {
2805 FolderIcon fi = (FolderIcon) dropOverView;
2806 if (fi.acceptDrop(dragInfo)) {
2807 return true;
2808 }
2809 }
2810 return false;
2811 }
2812
Winson Chung3d503fb2011-07-13 17:25:49 -07002813 boolean createUserFolderIfNecessary(View newView, long container, CellLayout target,
Adam Cohen482ed822012-03-02 14:15:13 -08002814 int[] targetCell, float distance, boolean external, DragView dragView,
2815 Runnable postAnimationRunnable) {
2816 if (distance > mMaxDistanceForFolderCreation) return false;
Adam Cohenc0dcf592011-06-01 15:30:43 -07002817 View v = target.getChildAt(targetCell[0], targetCell[1]);
Adam Cohen19f37922012-03-21 11:59:11 -07002818
Winson Chungec9a0a42011-07-20 20:42:13 -07002819 boolean hasntMoved = false;
2820 if (mDragInfo != null) {
2821 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2822 hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2823 mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2824 }
Adam Cohen10b17372011-04-15 14:21:25 -07002825
Adam Cohen19072da2011-05-31 14:30:45 -07002826 if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false;
2827 mCreateUserFolderOnDrop = false;
Adam Cohendcd297f2013-06-18 13:13:40 -07002828 final long screenId = (targetCell == null) ? mDragInfo.screenId : getIdForScreen(target);
Adam Cohen10b17372011-04-15 14:21:25 -07002829
2830 boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo);
2831 boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo);
2832
2833 if (aboveShortcut && willBecomeShortcut) {
2834 ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag();
2835 ShortcutInfo destInfo = (ShortcutInfo) v.getTag();
2836 // if the drag started here, we need to remove it from the workspace
2837 if (!external) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002838 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
Adam Cohen10b17372011-04-15 14:21:25 -07002839 }
2840
Adam Cohend0445262011-07-04 23:53:22 -07002841 Rect folderLocation = new Rect();
Adam Cohenac8c8762011-07-13 11:15:27 -07002842 float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation);
Adam Cohen10b17372011-04-15 14:21:25 -07002843 target.removeView(v);
Adam Cohend0445262011-07-04 23:53:22 -07002844
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 FolderIcon fi =
Adam Cohendcd297f2013-06-18 13:13:40 -07002846 mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002847 destInfo.cellX = -1;
2848 destInfo.cellY = -1;
2849 sourceInfo.cellX = -1;
2850 sourceInfo.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -07002851
Adam Cohen558baaf2011-08-15 15:22:57 -07002852 // If the dragView is null, we can't animate
2853 boolean animate = dragView != null;
2854 if (animate) {
2855 fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale,
2856 postAnimationRunnable);
2857 } else {
2858 fi.addItem(destInfo);
2859 fi.addItem(sourceInfo);
2860 }
Adam Cohen10b17372011-04-15 14:21:25 -07002861 return true;
2862 }
2863 return false;
2864 }
2865
Adam Cohena65beee2011-06-27 21:32:23 -07002866 boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell,
Adam Cohen482ed822012-03-02 14:15:13 -08002867 float distance, DragObject d, boolean external) {
2868 if (distance > mMaxDistanceForFolderCreation) return false;
2869
Adam Cohena65beee2011-06-27 21:32:23 -07002870 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002871 if (!mAddToExistingFolderOnDrop) return false;
2872 mAddToExistingFolderOnDrop = false;
Adam Cohen19f37922012-03-21 11:59:11 -07002873
Adam Cohenc0dcf592011-06-01 15:30:43 -07002874 if (dropOverView instanceof FolderIcon) {
2875 FolderIcon fi = (FolderIcon) dropOverView;
Adam Cohen3e8f8112011-07-02 18:03:00 -07002876 if (fi.acceptDrop(d.dragInfo)) {
2877 fi.onDrop(d);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002878
2879 // if the drag started here, we need to remove it from the workspace
2880 if (!external) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002881 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002882 }
2883 return true;
2884 }
2885 }
2886 return false;
2887 }
2888
Adam Cohend41fbf52012-02-16 23:53:59 -08002889 public void onDrop(final DragObject d) {
Adam Cohencb3382b2011-05-24 14:07:08 -07002890 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView,
Adam Cohene3e27a82011-04-15 12:07:39 -07002891 mDragViewVisualCenter);
2892
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002893 CellLayout dropTargetLayout = mDropToLayout;
2894
Adam Cohene3e27a82011-04-15 12:07:39 -07002895 // We want the point to be mapped to the dragTarget.
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002896 if (dropTargetLayout != null) {
2897 if (mLauncher.isHotseatLayout(dropTargetLayout)) {
Adam Cohenbb00ff22012-07-12 15:43:01 -07002898 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
Winson Chung3d503fb2011-07-13 17:25:49 -07002899 } else {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002900 mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null);
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 }
Adam Cohenba781612011-05-09 14:37:39 -07002902 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002903
Adam Cohened51cc92011-08-01 20:28:08 -07002904 int snapScreen = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002905 boolean resizeOnDrop = false;
Adam Cohencb3382b2011-05-24 14:07:08 -07002906 if (d.dragSource != this) {
Adam Cohene3e27a82011-04-15 12:07:39 -07002907 final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
2908 (int) mDragViewVisualCenter[1] };
Adam Cohen3e8f8112011-07-02 18:03:00 -07002909 onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d);
Patrick Dubroyce34a972010-10-19 10:34:32 -07002910 } else if (mDragInfo != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07002911 final View cell = mDragInfo.cell;
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08002912
Adam Cohend41fbf52012-02-16 23:53:59 -08002913 Runnable resizeRunnable = null;
Michael Jurka1e2f4652013-07-08 18:03:46 -07002914 if (dropTargetLayout != null && !d.cancelled) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002915 // Move internally
Winson Chung40e882b2011-07-21 19:01:11 -07002916 boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
2917 boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
2918 long container = hasMovedIntoHotseat ?
Winson Chung3d503fb2011-07-13 17:25:49 -07002919 LauncherSettings.Favorites.CONTAINER_HOTSEAT :
2920 LauncherSettings.Favorites.CONTAINER_DESKTOP;
Adam Cohendcd297f2013-06-18 13:13:40 -07002921 long screenId = (mTargetCell[0] < 0) ?
2922 mDragInfo.screenId : getIdForScreen(dropTargetLayout);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002923 int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
2924 int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
2925 // First we find the cell nearest to point at which the item is
2926 // dropped, without any consideration to whether there is an item there.
Adam Cohen482ed822012-03-02 14:15:13 -08002927
Adam Cohenc0dcf592011-06-01 15:30:43 -07002928 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int)
2929 mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
Adam Cohen482ed822012-03-02 14:15:13 -08002930 float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
2931 mDragViewVisualCenter[1], mTargetCell);
2932
Adam Cohenc0dcf592011-06-01 15:30:43 -07002933 // If the item being dropped is a shortcut and the nearest drop
Adam Cohen76078c42011-06-09 15:06:52 -07002934 // cell also contains a shortcut, then create a folder with the two shortcuts.
Winson Chung1c4cf4a2011-07-29 14:49:10 -07002935 if (!mInScrollArea && createUserFolderIfNecessary(cell, container,
Adam Cohen482ed822012-03-02 14:15:13 -08002936 dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002937 removeExtraEmptyScreen(true, null, 0, true);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002938 return;
2939 }
2940
Adam Cohen482ed822012-03-02 14:15:13 -08002941 if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell,
2942 distance, d, false)) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002943 removeExtraEmptyScreen(true, null, 0, true);
Adam Cohendf035382011-04-11 17:22:04 -07002944 return;
2945 }
2946
2947 // Aside from the special case where we're dropping a shortcut onto a shortcut,
2948 // we need to find the nearest cell location that is vacant
Adam Cohend41fbf52012-02-16 23:53:59 -08002949 ItemInfo item = (ItemInfo) d.dragInfo;
2950 int minSpanX = item.spanX;
2951 int minSpanY = item.spanY;
2952 if (item.minSpanX > 0 && item.minSpanY > 0) {
2953 minSpanX = item.minSpanX;
2954 minSpanY = item.minSpanY;
2955 }
Adam Cohen482ed822012-03-02 14:15:13 -08002956
Adam Cohend41fbf52012-02-16 23:53:59 -08002957 int[] resultSpan = new int[2];
Adam Cohenfa3c58f2013-12-06 16:10:55 -08002958 mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08002959 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell,
2960 mTargetCell, resultSpan, CellLayout.MODE_ON_DROP);
2961
Adam Cohend41fbf52012-02-16 23:53:59 -08002962 boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
Adam Cohen9e05a5e2012-09-10 15:53:09 -07002963
Adam Cohenaaa5c212012-10-05 18:14:31 -07002964 // if the widget resizes on drop
Adam Cohen9e05a5e2012-09-10 15:53:09 -07002965 if (foundCell && (cell instanceof AppWidgetHostView) &&
Adam Cohenaaa5c212012-10-05 18:14:31 -07002966 (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) {
Adam Cohend41fbf52012-02-16 23:53:59 -08002967 resizeOnDrop = true;
2968 item.spanX = resultSpan[0];
2969 item.spanY = resultSpan[1];
Adam Cohen7bdfc972012-05-22 16:50:35 -07002970 AppWidgetHostView awhv = (AppWidgetHostView) cell;
2971 AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0],
2972 resultSpan[1]);
Adam Cohend41fbf52012-02-16 23:53:59 -08002973 }
Adam Cohendf035382011-04-11 17:22:04 -07002974
Adam Cohendcd297f2013-06-18 13:13:40 -07002975 if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) {
2976 snapScreen = getPageIndexForScreenId(screenId);
2977 snapToPage(snapScreen);
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08002978 }
2979
Adam Cohend41fbf52012-02-16 23:53:59 -08002980 if (foundCell) {
2981 final ItemInfo info = (ItemInfo) cell.getTag();
Winson Chung40e882b2011-07-21 19:01:11 -07002982 if (hasMovedLayouts) {
Patrick Dubroy94383362010-10-29 15:03:24 -07002983 // Reparent the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002984 getParentCellLayoutForView(cell).removeView(cell);
Adam Cohendcd297f2013-06-18 13:13:40 -07002985 addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1],
Adam Cohend41fbf52012-02-16 23:53:59 -08002986 info.spanX, info.spanY);
Patrick Dubroy94383362010-10-29 15:03:24 -07002987 }
2988
2989 // update the item's position after drop
Patrick Dubroy94383362010-10-29 15:03:24 -07002990 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
Adam Cohen19f37922012-03-21 11:59:11 -07002991 lp.cellX = lp.tmpCellX = mTargetCell[0];
2992 lp.cellY = lp.tmpCellY = mTargetCell[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08002993 lp.cellHSpan = item.spanX;
2994 lp.cellVSpan = item.spanY;
Adam Cohen19f37922012-03-21 11:59:11 -07002995 lp.isLockedToGrid = true;
Patrick Dubroy94383362010-10-29 15:03:24 -07002996
Winson Chung3d503fb2011-07-13 17:25:49 -07002997 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2998 cell instanceof LauncherAppWidgetHostView) {
Adam Cohend4844c32011-02-18 19:25:06 -08002999 final CellLayout cellLayout = dropTargetLayout;
3000 // We post this call so that the widget has a chance to be placed
3001 // in its final location
3002
3003 final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
3004 AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo();
Adam Cohenc0a5df92012-04-02 16:53:26 -07003005 if (pinfo != null &&
3006 pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) {
Adam Cohend41fbf52012-02-16 23:53:59 -08003007 final Runnable addResizeFrame = new Runnable() {
Adam Cohend4844c32011-02-18 19:25:06 -08003008 public void run() {
Adam Cohen716b51e2011-06-30 12:09:54 -07003009 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohenc0dcf592011-06-01 15:30:43 -07003010 dragLayer.addResizeFrame(info, hostView, cellLayout);
Adam Cohend4844c32011-02-18 19:25:06 -08003011 }
Adam Cohen26976d92011-03-22 15:33:33 -07003012 };
Adam Cohend41fbf52012-02-16 23:53:59 -08003013 resizeRunnable = (new Runnable() {
Adam Cohen26976d92011-03-22 15:33:33 -07003014 public void run() {
3015 if (!isPageMoving()) {
Adam Cohend41fbf52012-02-16 23:53:59 -08003016 addResizeFrame.run();
Adam Cohen26976d92011-03-22 15:33:33 -07003017 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08003018 mDelayedResizeRunnable = addResizeFrame;
Adam Cohen26976d92011-03-22 15:33:33 -07003019 }
3020 }
Adam Cohend4844c32011-02-18 19:25:06 -08003021 });
3022 }
3023 }
3024
Adam Cohen949debe2013-09-29 14:22:48 -07003025 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX,
3026 lp.cellY, item.spanX, item.spanY);
Adam Cohend41fbf52012-02-16 23:53:59 -08003027 } else {
3028 // If we can't find a drop location, we return the item to its original position
3029 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
3030 mTargetCell[0] = lp.cellX;
3031 mTargetCell[1] = lp.cellY;
Adam Cohenf1dcdf62012-05-10 16:51:52 -07003032 CellLayout layout = (CellLayout) cell.getParent().getParent();
3033 layout.markCellsAsOccupiedForView(cell);
Patrick Dubroycd68ff52010-10-28 17:57:05 -07003034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07003036
Michael Jurka8c920dd2011-01-20 14:16:56 -08003037 final CellLayout parent = (CellLayout) cell.getParent().getParent();
Adam Cohend41fbf52012-02-16 23:53:59 -08003038 final Runnable finalResizeRunnable = resizeRunnable;
Patrick Dubroyce34a972010-10-19 10:34:32 -07003039 // Prepare it to be animated into its new position
3040 // This must be called after the view has been re-parented
Adam Cohend41fbf52012-02-16 23:53:59 -08003041 final Runnable onCompleteRunnable = new Runnable() {
Michael Jurkad74c9842011-07-10 12:44:21 -07003042 @Override
3043 public void run() {
3044 mAnimatingViewIntoPlace = false;
Michael Jurka3a0469d2012-06-21 09:38:41 -07003045 updateChildrenLayersEnabled(false);
Adam Cohend41fbf52012-02-16 23:53:59 -08003046 if (finalResizeRunnable != null) {
3047 finalResizeRunnable.run();
3048 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003049 removeExtraEmptyScreen(true, null, 0, true);
Michael Jurkad74c9842011-07-10 12:44:21 -07003050 }
3051 };
3052 mAnimatingViewIntoPlace = true;
Adam Cohenfc53cd22011-07-20 15:45:11 -07003053 if (d.dragView.hasDrawn()) {
Adam Cohend41fbf52012-02-16 23:53:59 -08003054 final ItemInfo info = (ItemInfo) cell.getTag();
3055 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) {
3056 int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE :
3057 ANIMATE_INTO_POSITION_AND_DISAPPEAR;
3058 animateWidgetDrop(info, parent, d.dragView,
3059 onCompleteRunnable, animationType, cell, false);
3060 } else {
Adam Cohen85b467b2012-02-29 15:38:46 -08003061 int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
3062 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
3063 onCompleteRunnable, this);
Adam Cohend41fbf52012-02-16 23:53:59 -08003064 }
Adam Cohenfc53cd22011-07-20 15:45:11 -07003065 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -08003066 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07003067 cell.setVisibility(VISIBLE);
3068 }
Adam Cohen716b51e2011-06-30 12:09:54 -07003069 parent.onDropChild(cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 }
3071 }
3072
Adam Cohendcd297f2013-06-18 13:13:40 -07003073 public void setFinalScrollForPageChange(int pageIndex) {
3074 CellLayout cl = (CellLayout) getChildAt(pageIndex);
3075 if (cl != null) {
Adam Cohened51cc92011-08-01 20:28:08 -07003076 mSavedScrollX = getScrollX();
Adam Cohened51cc92011-08-01 20:28:08 -07003077 mSavedTranslationX = cl.getTranslationX();
3078 mSavedRotationY = cl.getRotationY();
Adam Cohenedb40762013-07-18 16:45:45 -07003079 final int newX = getScrollForPage(pageIndex);
Adam Cohened51cc92011-08-01 20:28:08 -07003080 setScrollX(newX);
3081 cl.setTranslationX(0f);
3082 cl.setRotationY(0f);
3083 }
3084 }
3085
Adam Cohendcd297f2013-06-18 13:13:40 -07003086 public void resetFinalScrollForPageChange(int pageIndex) {
3087 if (pageIndex >= 0) {
3088 CellLayout cl = (CellLayout) getChildAt(pageIndex);
Adam Cohened51cc92011-08-01 20:28:08 -07003089 setScrollX(mSavedScrollX);
3090 cl.setTranslationX(mSavedTranslationX);
3091 cl.setRotationY(mSavedRotationY);
3092 }
3093 }
3094
Adam Cohen76078c42011-06-09 15:06:52 -07003095 public void getViewLocationRelativeToSelf(View v, int[] location) {
Adam Cohen8dfcba42011-07-07 16:38:18 -07003096 getLocationInWindow(location);
Adam Cohene3e27a82011-04-15 12:07:39 -07003097 int x = location[0];
3098 int y = location[1];
3099
Adam Cohen8dfcba42011-07-07 16:38:18 -07003100 v.getLocationInWindow(location);
Adam Cohene3e27a82011-04-15 12:07:39 -07003101 int vX = location[0];
3102 int vY = location[1];
3103
3104 location[0] = vX - x;
3105 location[1] = vY - y;
3106 }
3107
Adam Cohencb3382b2011-05-24 14:07:08 -07003108 public void onDragEnter(DragObject d) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003109 mDragEnforcer.onDragEnter();
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003110 mCreateUserFolderOnDrop = false;
3111 mAddToExistingFolderOnDrop = false;
3112
3113 mDropToLayout = null;
3114 CellLayout layout = getCurrentDropLayout();
3115 setCurrentDropLayout(layout);
3116 setCurrentDragOverlappingLayout(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07003117
Winson Chungc07918d2011-07-01 15:35:26 -07003118 // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we
3119 // don't need to show the outlines
Daniel Sandlere4f98912013-06-25 15:13:26 -04003120 if (LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc07918d2011-07-01 15:35:26 -07003121 showOutlines();
Michael Jurkad718d6a2010-10-14 15:35:17 -07003122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 }
3124
Winson Chungfe411c82013-09-26 16:07:17 -07003125 /** Return a rect that has the cellWidth/cellHeight (left, top), and
3126 * widthGap/heightGap (right, bottom) */
Adam Cohena897f392012-04-27 18:12:05 -07003127 static Rect getCellLayoutMetrics(Launcher launcher, int orientation) {
Winson Chung5f8afe62013-08-12 16:19:28 -07003128 LauncherAppState app = LauncherAppState.getInstance();
3129 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
3130
Adam Cohena897f392012-04-27 18:12:05 -07003131 Resources res = launcher.getResources();
3132 Display display = launcher.getWindowManager().getDefaultDisplay();
3133 Point smallestSize = new Point();
3134 Point largestSize = new Point();
3135 display.getCurrentSizeRange(smallestSize, largestSize);
Winson Chung892c74d2013-08-22 16:15:50 -07003136 int countX = (int) grid.numColumns;
3137 int countY = (int) grid.numRows;
Adam Cohena897f392012-04-27 18:12:05 -07003138 if (orientation == CellLayout.LANDSCAPE) {
3139 if (mLandscapeCellLayoutMetrics == null) {
Winson Chung5f8afe62013-08-12 16:19:28 -07003140 Rect padding = grid.getWorkspacePadding(CellLayout.LANDSCAPE);
Winson Chungdcd27ba2013-12-11 15:28:15 -08003141 int width = largestSize.x - padding.left - padding.right;
3142 int height = smallestSize.y - padding.top - padding.bottom;
Adam Cohena897f392012-04-27 18:12:05 -07003143 mLandscapeCellLayoutMetrics = new Rect();
Winson Chungfe411c82013-09-26 16:07:17 -07003144 mLandscapeCellLayoutMetrics.set(
3145 grid.calculateCellWidth(width, countX),
3146 grid.calculateCellHeight(height, countY), 0, 0);
Adam Cohena897f392012-04-27 18:12:05 -07003147 }
3148 return mLandscapeCellLayoutMetrics;
3149 } else if (orientation == CellLayout.PORTRAIT) {
3150 if (mPortraitCellLayoutMetrics == null) {
Winson Chung5f8afe62013-08-12 16:19:28 -07003151 Rect padding = grid.getWorkspacePadding(CellLayout.PORTRAIT);
Winson Chungdcd27ba2013-12-11 15:28:15 -08003152 int width = smallestSize.x - padding.left - padding.right;
3153 int height = largestSize.y - padding.top - padding.bottom;
Adam Cohena897f392012-04-27 18:12:05 -07003154 mPortraitCellLayoutMetrics = new Rect();
Winson Chungfe411c82013-09-26 16:07:17 -07003155 mPortraitCellLayoutMetrics.set(
3156 grid.calculateCellWidth(width, countX),
3157 grid.calculateCellHeight(height, countY), 0, 0);
Adam Cohena897f392012-04-27 18:12:05 -07003158 }
3159 return mPortraitCellLayoutMetrics;
3160 }
3161 return null;
3162 }
3163
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003164 public void onDragExit(DragObject d) {
3165 mDragEnforcer.onDragExit();
Winson Chungc07918d2011-07-01 15:35:26 -07003166
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003167 // Here we store the final page that will be dropped to, if the workspace in fact
3168 // receives the drop
3169 if (mInScrollArea) {
Winson Chungcc1cfe42012-06-18 15:09:17 -07003170 if (isPageMoving()) {
3171 // If the user drops while the page is scrolling, we should use that page as the
3172 // destination instead of the page that is being hovered over.
3173 mDropToLayout = (CellLayout) getPageAt(getNextPage());
3174 } else {
3175 mDropToLayout = mDragOverlappingLayout;
3176 }
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003177 } else {
3178 mDropToLayout = mDragTargetLayout;
3179 }
3180
3181 if (mDragMode == DRAG_MODE_CREATE_FOLDER) {
3182 mCreateUserFolderOnDrop = true;
3183 } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) {
3184 mAddToExistingFolderOnDrop = true;
Adam Cohen482ed822012-03-02 14:15:13 -08003185 }
3186
Winson Chungc07918d2011-07-01 15:35:26 -07003187 // Reset the scroll area and previous drag target
3188 onResetScrollArea();
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003189 setCurrentDropLayout(null);
3190 setCurrentDragOverlappingLayout(null);
Winson Chungc07918d2011-07-01 15:35:26 -07003191
Adam Cohen74c28d12011-11-18 14:17:11 -08003192 mSpringLoadedDragController.cancel();
Winson Chungc07918d2011-07-01 15:35:26 -07003193
3194 if (!mIsPageMoving) {
3195 hideOutlines();
3196 }
3197 }
3198
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003199 void setCurrentDropLayout(CellLayout layout) {
3200 if (mDragTargetLayout != null) {
3201 mDragTargetLayout.revertTempState();
3202 mDragTargetLayout.onDragExit();
3203 }
3204 mDragTargetLayout = layout;
3205 if (mDragTargetLayout != null) {
3206 mDragTargetLayout.onDragEnter();
3207 }
3208 cleanupReorder(true);
3209 cleanupFolderCreation();
3210 setCurrentDropOverCell(-1, -1);
3211 }
3212
3213 void setCurrentDragOverlappingLayout(CellLayout layout) {
3214 if (mDragOverlappingLayout != null) {
3215 mDragOverlappingLayout.setIsDragOverlapping(false);
3216 }
3217 mDragOverlappingLayout = layout;
3218 if (mDragOverlappingLayout != null) {
3219 mDragOverlappingLayout.setIsDragOverlapping(true);
3220 }
3221 invalidate();
3222 }
3223
3224 void setCurrentDropOverCell(int x, int y) {
3225 if (x != mDragOverX || y != mDragOverY) {
3226 mDragOverX = x;
3227 mDragOverY = y;
3228 setDragMode(DRAG_MODE_NONE);
3229 }
3230 }
3231
3232 void setDragMode(int dragMode) {
3233 if (dragMode != mDragMode) {
3234 if (dragMode == DRAG_MODE_NONE) {
3235 cleanupAddToFolder();
3236 // We don't want to cancel the re-order alarm every time the target cell changes
3237 // as this feels to slow / unresponsive.
3238 cleanupReorder(false);
3239 cleanupFolderCreation();
3240 } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) {
3241 cleanupReorder(true);
3242 cleanupFolderCreation();
3243 } else if (dragMode == DRAG_MODE_CREATE_FOLDER) {
3244 cleanupAddToFolder();
3245 cleanupReorder(true);
3246 } else if (dragMode == DRAG_MODE_REORDER) {
3247 cleanupAddToFolder();
3248 cleanupFolderCreation();
3249 }
3250 mDragMode = dragMode;
3251 }
3252 }
3253
3254 private void cleanupFolderCreation() {
3255 if (mDragFolderRingAnimator != null) {
3256 mDragFolderRingAnimator.animateToNaturalState();
Adam Cohen8ec05f92013-10-13 12:29:03 -07003257 mDragFolderRingAnimator = null;
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003258 }
Adam Cohen8ec05f92013-10-13 12:29:03 -07003259 mFolderCreationAlarm.setOnAlarmListener(null);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003260 mFolderCreationAlarm.cancelAlarm();
3261 }
3262
3263 private void cleanupAddToFolder() {
3264 if (mDragOverFolderIcon != null) {
3265 mDragOverFolderIcon.onDragExit(null);
3266 mDragOverFolderIcon = null;
3267 }
3268 }
3269
3270 private void cleanupReorder(boolean cancelAlarm) {
3271 // Any pending reorders are canceled
3272 if (cancelAlarm) {
3273 mReorderAlarm.cancelAlarm();
3274 }
3275 mLastReorderX = -1;
3276 mLastReorderY = -1;
Winson Chungc07918d2011-07-01 15:35:26 -07003277 }
3278
Michael Jurka4516c112010-10-07 15:13:47 -07003279 /*
3280 *
3281 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
3282 * coordinate space. The argument xy is modified with the return result.
3283 *
3284 * if cachedInverseMatrix is not null, this method will just use that matrix instead of
Michael Jurkad718d6a2010-10-14 15:35:17 -07003285 * computing it itself; we use this to avoid redundant matrix inversions in
Michael Jurka4516c112010-10-07 15:13:47 -07003286 * findMatchingPageForDragOver
3287 *
3288 */
3289 void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003290 xy[0] = xy[0] - v.getLeft();
3291 xy[1] = xy[1] - v.getTop();
Michael Jurka4516c112010-10-07 15:13:47 -07003292 }
3293
Adam Cohen7d30a372013-07-01 17:03:59 -07003294 boolean isPointInSelfOverHotseat(int x, int y, Rect r) {
3295 if (r == null) {
3296 r = new Rect();
3297 }
3298 mTempPt[0] = x;
3299 mTempPt[1] = y;
3300 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true);
Winson Chungabedd9f2013-09-24 15:41:09 -07003301
3302 LauncherAppState app = LauncherAppState.getInstance();
3303 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
3304 r = grid.getHotseatRect();
Adam Cohen7d30a372013-07-01 17:03:59 -07003305 if (r.contains(mTempPt[0], mTempPt[1])) {
3306 return true;
3307 }
3308 return false;
3309 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003310
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003311 void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003312 mTempPt[0] = (int) xy[0];
3313 mTempPt[1] = (int) xy[1];
3314 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true);
Winson Chung156ab5b2013-07-12 14:14:16 -07003315 mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempPt);
Adam Cohen7d30a372013-07-01 17:03:59 -07003316
3317 xy[0] = mTempPt[0];
3318 xy[1] = mTempPt[1];
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003319 }
3320
Winson Chung3d503fb2011-07-13 17:25:49 -07003321 /*
Michael Jurka4516c112010-10-07 15:13:47 -07003322 *
3323 * Convert the 2D coordinate xy from this CellLayout's coordinate space to
3324 * the parent View's coordinate space. The argument xy is modified with the return result.
3325 *
3326 */
3327 void mapPointFromChildToSelf(View v, float[] xy) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003328 xy[0] += v.getLeft();
3329 xy[1] += v.getTop();
Michael Jurka4516c112010-10-07 15:13:47 -07003330 }
3331
Adam Cohene3e27a82011-04-15 12:07:39 -07003332 static private float squaredDistance(float[] point1, float[] point2) {
Michael Jurka4516c112010-10-07 15:13:47 -07003333 float distanceX = point1[0] - point2[0];
3334 float distanceY = point2[1] - point2[1];
3335 return distanceX * distanceX + distanceY * distanceY;
Adam Cohene3e27a82011-04-15 12:07:39 -07003336 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003337
Michael Jurka4516c112010-10-07 15:13:47 -07003338 /*
3339 *
Michael Jurka4516c112010-10-07 15:13:47 -07003340 * This method returns the CellLayout that is currently being dragged to. In order to drag
3341 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
3342 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
3343 *
3344 * Return null if no CellLayout is currently being dragged over
3345 *
3346 */
3347 private CellLayout findMatchingPageForDragOver(
Adam Cohen00618752011-07-20 12:06:04 -07003348 DragView dragView, float originX, float originY, boolean exact) {
Michael Jurka4516c112010-10-07 15:13:47 -07003349 // We loop through all the screens (ie CellLayouts) and see which ones overlap
3350 // with the item being dragged and then choose the one that's closest to the touch point
Michael Jurkaa63c4522010-08-19 13:52:27 -07003351 final int screenCount = getChildCount();
3352 CellLayout bestMatchingScreen = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003353 float smallestDistSoFar = Float.MAX_VALUE;
Michael Jurka4516c112010-10-07 15:13:47 -07003354
Michael Jurkaa63c4522010-08-19 13:52:27 -07003355 for (int i = 0; i < screenCount; i++) {
Adam Cohendf041bc2013-09-22 12:32:22 -07003356 // The custom content screen is not a valid drag over option
3357 if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) {
3358 continue;
3359 }
3360
Winson Chung3d503fb2011-07-13 17:25:49 -07003361 CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurkaa63c4522010-08-19 13:52:27 -07003362
Adam Cohen00618752011-07-20 12:06:04 -07003363 final float[] touchXy = {originX, originY};
Michael Jurka4516c112010-10-07 15:13:47 -07003364 // Transform the touch coordinates to the CellLayout's local coordinates
3365 // If the touch point is within the bounds of the cell layout, we can return immediately
Michael Jurka0280c3b2010-09-17 15:00:07 -07003366 cl.getMatrix().invert(mTempInverseMatrix);
Michael Jurka4516c112010-10-07 15:13:47 -07003367 mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix);
Michael Jurkaa63c4522010-08-19 13:52:27 -07003368
Michael Jurka4516c112010-10-07 15:13:47 -07003369 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
3370 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
3371 return cl;
3372 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07003373
Winson Chung96ef4092011-11-22 12:25:14 -08003374 if (!exact) {
Michael Jurka4516c112010-10-07 15:13:47 -07003375 // Get the center of the cell layout in screen coordinates
3376 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
3377 cellLayoutCenter[0] = cl.getWidth()/2;
3378 cellLayoutCenter[1] = cl.getHeight()/2;
3379 mapPointFromChildToSelf(cl, cellLayoutCenter);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003380
Adam Cohen00618752011-07-20 12:06:04 -07003381 touchXy[0] = originX;
3382 touchXy[1] = originY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003383
Michael Jurka4516c112010-10-07 15:13:47 -07003384 // Calculate the distance between the center of the CellLayout
3385 // and the touch point
3386 float dist = squaredDistance(touchXy, cellLayoutCenter);
3387
3388 if (dist < smallestDistSoFar) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003389 smallestDistSoFar = dist;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003390 bestMatchingScreen = cl;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003391 }
Michael Jurka4516c112010-10-07 15:13:47 -07003392 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07003393 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07003394 return bestMatchingScreen;
3395 }
3396
Adam Cohene3e27a82011-04-15 12:07:39 -07003397 // This is used to compute the visual center of the dragView. This point is then
3398 // used to visualize drop locations and determine where to drop an item. The idea is that
3399 // the visual center represents the user's interpretation of where the item is, and hence
3400 // is the appropriate point to use when determining drop location.
3401 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
3402 DragView dragView, float[] recycle) {
3403 float res[];
3404 if (recycle == null) {
3405 res = new float[2];
3406 } else {
3407 res = recycle;
3408 }
3409
3410 // First off, the drag view has been shifted in a way that is not represented in the
3411 // x and y values or the x/yOffsets. Here we account for that shift.
3412 x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX);
3413 y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
3414
3415 // These represent the visual top and left of drag view if a dragRect was provided.
3416 // If a dragRect was not provided, then they correspond to the actual view left and
3417 // top, as the dragRect is in that case taken to be the entire dragView.
3418 // R.dimen.dragViewOffsetY.
3419 int left = x - xOffset;
3420 int top = y - yOffset;
3421
3422 // In order to find the visual center, we shift by half the dragRect
3423 res[0] = left + dragView.getDragRegion().width() / 2;
3424 res[1] = top + dragView.getDragRegion().height() / 2;
3425
3426 return res;
3427 }
3428
Winson Chungea359c62011-08-03 17:06:35 -07003429 private boolean isDragWidget(DragObject d) {
3430 return (d.dragInfo instanceof LauncherAppWidgetInfo ||
3431 d.dragInfo instanceof PendingAddWidgetInfo);
3432 }
3433 private boolean isExternalDragWidget(DragObject d) {
3434 return d.dragSource != this && isDragWidget(d);
3435 }
3436
Adam Cohencb3382b2011-05-24 14:07:08 -07003437 public void onDragOver(DragObject d) {
Winson Chungc07918d2011-07-01 15:35:26 -07003438 // Skip drag over events while we are dragging over side pages
Adam Cohen82ac8a22012-02-14 16:27:49 -08003439 if (mInScrollArea || mIsSwitchingState || mState == State.SMALL) return;
Winson Chungc07918d2011-07-01 15:35:26 -07003440
Winson Chung4afe9b32011-07-27 17:46:20 -07003441 Rect r = new Rect();
Winson Chung3d503fb2011-07-13 17:25:49 -07003442 CellLayout layout = null;
Winson Chungc07918d2011-07-01 15:35:26 -07003443 ItemInfo item = (ItemInfo) d.dragInfo;
3444
3445 // Ensure that we have proper spans for the item that we are dropping
3446 if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found");
Adam Cohen00618752011-07-20 12:06:04 -07003447 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
Winson Chung4afe9b32011-07-27 17:46:20 -07003448 d.dragView, mDragViewVisualCenter);
Adam Cohen00618752011-07-20 12:06:04 -07003449
Adam Cohen482ed822012-03-02 14:15:13 -08003450 final View child = (mDragInfo == null) ? null : mDragInfo.cell;
Winson Chungc07918d2011-07-01 15:35:26 -07003451 // Identify whether we have dragged over a side page
Michael Jurkad74c9842011-07-10 12:44:21 -07003452 if (isSmall()) {
Winson Chungea359c62011-08-03 17:06:35 -07003453 if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003454 if (isPointInSelfOverHotseat(d.x, d.y, r)) {
Winson Chung4afe9b32011-07-27 17:46:20 -07003455 layout = mLauncher.getHotseat().getLayout();
3456 }
3457 }
3458 if (layout == null) {
Winson Chung96ef4092011-11-22 12:25:14 -08003459 layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false);
Winson Chung4afe9b32011-07-27 17:46:20 -07003460 }
Winson Chungc07918d2011-07-01 15:35:26 -07003461 if (layout != mDragTargetLayout) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003462 setCurrentDropLayout(layout);
3463 setCurrentDragOverlappingLayout(layout);
Winson Chungc07918d2011-07-01 15:35:26 -07003464
Michael Jurkad74c9842011-07-10 12:44:21 -07003465 boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED);
Winson Chungc07918d2011-07-01 15:35:26 -07003466 if (isInSpringLoadedMode) {
Winson Chung4afe9b32011-07-27 17:46:20 -07003467 if (mLauncher.isHotseatLayout(layout)) {
3468 mSpringLoadedDragController.cancel();
3469 } else {
3470 mSpringLoadedDragController.setAlarm(mDragTargetLayout);
3471 }
Winson Chungc07918d2011-07-01 15:35:26 -07003472 }
3473 }
3474 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003475 // Test to see if we are over the hotseat otherwise just use the current page
Winson Chungea359c62011-08-03 17:06:35 -07003476 if (mLauncher.getHotseat() != null && !isDragWidget(d)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003477 if (isPointInSelfOverHotseat(d.x, d.y, r)) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003478 layout = mLauncher.getHotseat().getLayout();
3479 }
3480 }
3481 if (layout == null) {
3482 layout = getCurrentDropLayout();
3483 }
Winson Chungc07918d2011-07-01 15:35:26 -07003484 if (layout != mDragTargetLayout) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003485 setCurrentDropLayout(layout);
3486 setCurrentDragOverlappingLayout(layout);
Winson Chungc07918d2011-07-01 15:35:26 -07003487 }
3488 }
3489
3490 // Handle the drag over
3491 if (mDragTargetLayout != null) {
Winson Chungc07918d2011-07-01 15:35:26 -07003492 // We want the point to be mapped to the dragTarget.
Winson Chung3d503fb2011-07-13 17:25:49 -07003493 if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003494 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
Winson Chung3d503fb2011-07-13 17:25:49 -07003495 } else {
3496 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
3497 }
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003498
Winson Chungc07918d2011-07-01 15:35:26 -07003499 ItemInfo info = (ItemInfo) d.dragInfo;
Patrick Dubroy1262e362010-10-06 15:49:50 -07003500
Adam Cohen74c54912013-09-29 14:48:04 -07003501 int minSpanX = item.spanX;
3502 int minSpanY = item.spanY;
3503 if (item.minSpanX > 0 && item.minSpanY > 0) {
3504 minSpanX = item.minSpanX;
3505 minSpanY = item.minSpanY;
3506 }
3507
Winson Chungc07918d2011-07-01 15:35:26 -07003508 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
Adam Cohen74c54912013-09-29 14:48:04 -07003509 (int) mDragViewVisualCenter[1], minSpanX, minSpanY,
Adam Cohend024f982012-05-23 18:26:45 -07003510 mDragTargetLayout, mTargetCell);
Adam Cohen74c54912013-09-29 14:48:04 -07003511 int reorderX = mTargetCell[0];
3512 int reorderY = mTargetCell[1];
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003513
3514 setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]);
3515
Adam Cohen482ed822012-03-02 14:15:13 -08003516 float targetCellDistance = mDragTargetLayout.getDistanceFromCell(
3517 mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
3518
Winson Chungc07918d2011-07-01 15:35:26 -07003519 final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0],
3520 mTargetCell[1]);
Winson Chung785d2eb2011-04-14 16:08:02 -07003521
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003522 manageFolderFeedback(info, mDragTargetLayout, mTargetCell,
3523 targetCellDistance, dragOverView);
Adam Cohen482ed822012-03-02 14:15:13 -08003524
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003525 boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int)
3526 mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX,
3527 item.spanY, child, mTargetCell);
3528
3529 if (!nearestDropOccupied) {
Adam Cohen19f37922012-03-21 11:59:11 -07003530 mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
3531 (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1],
3532 mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false,
3533 d.dragView.getDragVisualizeOffset(), d.dragView.getDragRegion());
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003534 } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER)
Adam Cohen74c54912013-09-29 14:48:04 -07003535 && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX ||
3536 mLastReorderY != reorderY)) {
Adam Cohend024f982012-05-23 18:26:45 -07003537
Adam Cohenfa3c58f2013-12-06 16:10:55 -08003538 int[] resultSpan = new int[2];
3539 mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
3540 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY,
3541 child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT);
3542
Adam Cohen19f37922012-03-21 11:59:11 -07003543 // Otherwise, if we aren't adding to or creating a folder and there's no pending
3544 // reorder, then we schedule a reorder
Adam Cohen482ed822012-03-02 14:15:13 -08003545 ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter,
3546 minSpanX, minSpanY, item.spanX, item.spanY, d.dragView, child);
3547 mReorderAlarm.setOnAlarmListener(listener);
3548 mReorderAlarm.setAlarm(REORDER_TIMEOUT);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003549 }
3550
3551 if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER ||
3552 !nearestDropOccupied) {
3553 if (mDragTargetLayout != null) {
3554 mDragTargetLayout.revertTempState();
Michael Jurkad3ef3062010-11-23 16:23:58 -08003555 }
3556 }
Adam Cohen482ed822012-03-02 14:15:13 -08003557 }
3558 }
3559
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003560 private void manageFolderFeedback(ItemInfo info, CellLayout targetLayout,
3561 int[] targetCell, float distance, View dragOverView) {
Adam Cohen482ed822012-03-02 14:15:13 -08003562 boolean userFolderPending = willCreateUserFolder(info, targetLayout, targetCell, distance,
3563 false);
3564
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003565 if (mDragMode == DRAG_MODE_NONE && userFolderPending &&
3566 !mFolderCreationAlarm.alarmPending()) {
Adam Cohen482ed822012-03-02 14:15:13 -08003567 mFolderCreationAlarm.setOnAlarmListener(new
3568 FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1]));
3569 mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003570 return;
Adam Cohen482ed822012-03-02 14:15:13 -08003571 }
3572
3573 boolean willAddToFolder =
3574 willAddToExistingUserFolder(info, targetLayout, targetCell, distance);
3575
3576 if (willAddToFolder && mDragMode == DRAG_MODE_NONE) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003577 mDragOverFolderIcon = ((FolderIcon) dragOverView);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003578 mDragOverFolderIcon.onDragEnter(info);
Adam Cohen482ed822012-03-02 14:15:13 -08003579 if (targetLayout != null) {
3580 targetLayout.clearDragOutlines();
Winson Chungc07918d2011-07-01 15:35:26 -07003581 }
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003582 setDragMode(DRAG_MODE_ADD_TO_FOLDER);
3583 return;
Patrick Dubroy976ebec2010-08-04 20:03:37 -07003584 }
Adam Cohen482ed822012-03-02 14:15:13 -08003585
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003586 if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) {
3587 setDragMode(DRAG_MODE_NONE);
3588 }
3589 if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) {
3590 setDragMode(DRAG_MODE_NONE);
Adam Cohen482ed822012-03-02 14:15:13 -08003591 }
3592
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003593 return;
Adam Cohenc0dcf592011-06-01 15:30:43 -07003594 }
3595
Adam Cohen19072da2011-05-31 14:30:45 -07003596 class FolderCreationAlarmListener implements OnAlarmListener {
Adam Cohen69ce2e52011-07-03 19:25:21 -07003597 CellLayout layout;
3598 int cellX;
3599 int cellY;
Adam Cohen19072da2011-05-31 14:30:45 -07003600
Adam Cohen69ce2e52011-07-03 19:25:21 -07003601 public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) {
3602 this.layout = layout;
3603 this.cellX = cellX;
3604 this.cellY = cellY;
Adam Cohen19072da2011-05-31 14:30:45 -07003605 }
3606
3607 public void onAlarm(Alarm alarm) {
Adam Cohen8ec05f92013-10-13 12:29:03 -07003608 if (mDragFolderRingAnimator != null) {
3609 // This shouldn't happen ever, but just in case, make sure we clean up the mess.
3610 mDragFolderRingAnimator.animateToNaturalState();
Adam Cohen19072da2011-05-31 14:30:45 -07003611 }
Adam Cohen8ec05f92013-10-13 12:29:03 -07003612 mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null);
Adam Cohen69ce2e52011-07-03 19:25:21 -07003613 mDragFolderRingAnimator.setCell(cellX, cellY);
3614 mDragFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -07003615 mDragFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -07003616 layout.showFolderAccept(mDragFolderRingAnimator);
3617 layout.clearDragOutlines();
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003618 setDragMode(DRAG_MODE_CREATE_FOLDER);
Adam Cohen482ed822012-03-02 14:15:13 -08003619 }
3620 }
3621
3622 class ReorderAlarmListener implements OnAlarmListener {
3623 float[] dragViewCenter;
3624 int minSpanX, minSpanY, spanX, spanY;
3625 DragView dragView;
3626 View child;
3627
3628 public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX,
3629 int spanY, DragView dragView, View child) {
3630 this.dragViewCenter = dragViewCenter;
3631 this.minSpanX = minSpanX;
3632 this.minSpanY = minSpanY;
3633 this.spanX = spanX;
3634 this.spanY = spanY;
3635 this.child = child;
3636 this.dragView = dragView;
3637 }
3638
3639 public void onAlarm(Alarm alarm) {
3640 int[] resultSpan = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -07003641 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
Adam Cohen74c54912013-09-29 14:48:04 -07003642 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout,
3643 mTargetCell);
Adam Cohen19f37922012-03-21 11:59:11 -07003644 mLastReorderX = mTargetCell[0];
3645 mLastReorderY = mTargetCell[1];
3646
Adam Cohenfa3c58f2013-12-06 16:10:55 -08003647 mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08003648 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
3649 child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER);
3650
Adam Cohen19f37922012-03-21 11:59:11 -07003651 if (mTargetCell[0] < 0 || mTargetCell[1] < 0) {
3652 mDragTargetLayout.revertTempState();
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003653 } else {
3654 setDragMode(DRAG_MODE_REORDER);
Adam Cohen19f37922012-03-21 11:59:11 -07003655 }
Adam Cohen482ed822012-03-02 14:15:13 -08003656
Adam Cohen482ed822012-03-02 14:15:13 -08003657 boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY;
3658 mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
3659 (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1],
3660 mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize,
3661 dragView.getDragVisualizeOffset(), dragView.getDragRegion());
Adam Cohen19072da2011-05-31 14:30:45 -07003662 }
3663 }
3664
Winson Chunga34abf82010-11-12 12:10:35 -08003665 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -07003666 public void getHitRectRelativeToDragLayer(Rect outRect) {
Winson Chunga34abf82010-11-12 12:10:35 -08003667 // We want the workspace to have the whole area of the display (it will find the correct
3668 // cell layout to drop to in the existing drag/drop logic.
Adam Cohen7d30a372013-07-01 17:03:59 -07003669 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect);
Winson Chunga34abf82010-11-12 12:10:35 -08003670 }
3671
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003672 /**
3673 * Add the item specified by dragInfo to the given layout.
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003674 * @return true if successful
3675 */
Adam Cohen120980b2010-12-08 11:05:37 -08003676 public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) {
3677 if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) {
Patrick Dubroybbaa75c2011-03-08 18:47:40 -08003678 onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false);
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003679 return true;
3680 }
Winson Chung93eef082012-03-23 15:59:27 -07003681 mLauncher.showOutOfSpaceMessage(mLauncher.isHotseatLayout(layout));
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003682 return false;
3683 }
3684
Adam Cohend5e42732011-03-28 17:33:39 -07003685 private void onDropExternal(int[] touchXY, Object dragInfo,
3686 CellLayout cellLayout, boolean insertAtFirst) {
Adam Cohene3e27a82011-04-15 12:07:39 -07003687 onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null);
Adam Cohend5e42732011-03-28 17:33:39 -07003688 }
3689
Adam Cohen120980b2010-12-08 11:05:37 -08003690 /**
3691 * Drop an item that didn't originate on one of the workspace screens.
3692 * It may have come from Launcher (e.g. from all apps or customize), or it may have
3693 * come from another app altogether.
3694 *
3695 * NOTE: This can also be called when we are outside of a drag event, when we want
3696 * to add an item to one of the workspace screens.
3697 */
Winson Chung557d6ed2011-07-08 15:34:52 -07003698 private void onDropExternal(final int[] touchXY, final Object dragInfo,
3699 final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
3700 final Runnable exitSpringLoadedRunnable = new Runnable() {
3701 @Override
3702 public void run() {
Adam Cohenad4e15c2013-10-17 16:21:35 -07003703 removeExtraEmptyScreen(false, new Runnable() {
3704 @Override
3705 public void run() {
3706 mLauncher.exitSpringLoadedDragModeDelayed(true,
3707 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
3708 }
3709 });
Winson Chung557d6ed2011-07-08 15:34:52 -07003710 }
3711 };
Adam Cohenb7e16182011-07-15 17:55:02 -07003712
3713 ItemInfo info = (ItemInfo) dragInfo;
3714 int spanX = info.spanX;
3715 int spanY = info.spanY;
3716 if (mDragInfo != null) {
3717 spanX = mDragInfo.spanX;
3718 spanY = mDragInfo.spanY;
3719 }
3720
Winson Chung3d503fb2011-07-13 17:25:49 -07003721 final long container = mLauncher.isHotseatLayout(cellLayout) ?
3722 LauncherSettings.Favorites.CONTAINER_HOTSEAT :
3723 LauncherSettings.Favorites.CONTAINER_DESKTOP;
Adam Cohendcd297f2013-06-18 13:13:40 -07003724 final long screenId = getIdForScreen(cellLayout);
3725 if (!mLauncher.isHotseatLayout(cellLayout)
3726 && screenId != getScreenIdForPageIndex(mCurrentPage)
Winson Chung3d503fb2011-07-13 17:25:49 -07003727 && mState != State.SPRING_LOADED) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 snapToScreenId(screenId, null);
Adam Cohen76078c42011-06-09 15:06:52 -07003729 }
Adam Cohenb7e16182011-07-15 17:55:02 -07003730
3731 if (info instanceof PendingAddItemInfo) {
3732 final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
3733
Adam Cohen558baaf2011-08-15 15:22:57 -07003734 boolean findNearestVacantCell = true;
3735 if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
3736 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3737 cellLayout, mTargetCell);
Adam Cohen482ed822012-03-02 14:15:13 -08003738 float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3739 mDragViewVisualCenter[1], mTargetCell);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003740 if (willCreateUserFolder((ItemInfo) d.dragInfo, cellLayout, mTargetCell,
Adam Cohen482ed822012-03-02 14:15:13 -08003741 distance, true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo,
Adam Cohenc6cc61d2012-04-04 12:47:08 -07003742 cellLayout, mTargetCell, distance)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07003743 findNearestVacantCell = false;
3744 }
3745 }
Adam Cohen482ed822012-03-02 14:15:13 -08003746
Adam Cohend41fbf52012-02-16 23:53:59 -08003747 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohenaaa5c212012-10-05 18:14:31 -07003748 boolean updateWidgetSize = false;
Adam Cohen558baaf2011-08-15 15:22:57 -07003749 if (findNearestVacantCell) {
Adam Cohen482ed822012-03-02 14:15:13 -08003750 int minSpanX = item.spanX;
3751 int minSpanY = item.spanY;
3752 if (item.minSpanX > 0 && item.minSpanY > 0) {
3753 minSpanX = item.minSpanX;
3754 minSpanY = item.minSpanY;
3755 }
Adam Cohend41fbf52012-02-16 23:53:59 -08003756 int[] resultSpan = new int[2];
Adam Cohenfa3c58f2013-12-06 16:10:55 -08003757 mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08003758 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY,
3759 null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL);
Adam Cohen9e05a5e2012-09-10 15:53:09 -07003760
3761 if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) {
3762 updateWidgetSize = true;
3763 }
Adam Cohend41fbf52012-02-16 23:53:59 -08003764 item.spanX = resultSpan[0];
3765 item.spanY = resultSpan[1];
Adam Cohen558baaf2011-08-15 15:22:57 -07003766 }
3767
Adam Cohenb7e16182011-07-15 17:55:02 -07003768 Runnable onAnimationCompleteRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003769 @Override
3770 public void run() {
3771 // When dragging and dropping from customization tray, we deal with creating
3772 // widgets/shortcuts/folders in a slightly different way
Adam Cohenb7e16182011-07-15 17:55:02 -07003773 switch (pendingInfo.itemType) {
Winson Chung557d6ed2011-07-08 15:34:52 -07003774 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohend41fbf52012-02-16 23:53:59 -08003775 int span[] = new int[2];
3776 span[0] = item.spanX;
3777 span[1] = item.spanY;
Adam Cohenb7e16182011-07-15 17:55:02 -07003778 mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07003779 container, screenId, mTargetCell, span, null);
Winson Chung557d6ed2011-07-08 15:34:52 -07003780 break;
3781 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohenb7e16182011-07-15 17:55:02 -07003782 mLauncher.processShortcutFromDrop(pendingInfo.componentName,
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 container, screenId, mTargetCell, null);
Winson Chung557d6ed2011-07-08 15:34:52 -07003784 break;
3785 default:
Adam Cohenb7e16182011-07-15 17:55:02 -07003786 throw new IllegalStateException("Unknown item type: " +
3787 pendingInfo.itemType);
Winson Chung557d6ed2011-07-08 15:34:52 -07003788 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003789 }
Adam Cohenb7e16182011-07-15 17:55:02 -07003790 };
Adam Cohend41fbf52012-02-16 23:53:59 -08003791 View finalView = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
3792 ? ((PendingAddWidgetInfo) pendingInfo).boundWidget : null;
Adam Cohen9e05a5e2012-09-10 15:53:09 -07003793
3794 if (finalView instanceof AppWidgetHostView && updateWidgetSize) {
3795 AppWidgetHostView awhv = (AppWidgetHostView) finalView;
3796 AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, item.spanX,
3797 item.spanY);
3798 }
3799
Adam Cohend41fbf52012-02-16 23:53:59 -08003800 int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR;
3801 if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
3802 ((PendingAddWidgetInfo) pendingInfo).info.configure != null) {
3803 animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN;
3804 }
3805 animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable,
3806 animationStyle, finalView, true);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07003807 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003808 // This is for other drag/drop cases, like dragging from All Apps
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003809 View view = null;
3810
3811 switch (info.itemType) {
3812 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3813 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003814 if (info.container == NO_ID && info instanceof AppInfo) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003815 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003816 info = new ShortcutInfo((AppInfo) info);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003817 }
3818 view = mLauncher.createShortcut(R.layout.application, cellLayout,
3819 (ShortcutInfo) info);
3820 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003821 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Adam Cohenc0dcf592011-06-01 15:30:43 -07003822 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout,
3823 (FolderInfo) info, mIconCache);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003824 break;
3825 default:
3826 throw new IllegalStateException("Unknown item type: " + info.itemType);
3827 }
3828
Adam Cohenc0dcf592011-06-01 15:30:43 -07003829 // First we find the cell nearest to point at which the item is
3830 // dropped, without any consideration to whether there is an item there.
3831 if (touchXY != null) {
3832 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3833 cellLayout, mTargetCell);
Adam Cohen482ed822012-03-02 14:15:13 -08003834 float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3835 mDragViewVisualCenter[1], mTargetCell);
Winson Chung557d6ed2011-07-08 15:34:52 -07003836 d.postAnimationRunnable = exitSpringLoadedRunnable;
Adam Cohen482ed822012-03-02 14:15:13 -08003837 if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance,
3838 true, d.dragView, d.postAnimationRunnable)) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07003839 return;
3840 }
Adam Cohen482ed822012-03-02 14:15:13 -08003841 if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d,
3842 true)) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07003843 return;
3844 }
Adam Cohen10b17372011-04-15 14:21:25 -07003845 }
3846
Michael Jurkac4e772e2011-02-10 13:32:01 -08003847 if (touchXY != null) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003848 // when dragging and dropping, just find the closest free spot
Adam Cohenfa3c58f2013-12-06 16:10:55 -08003849 mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
Adam Cohen482ed822012-03-02 14:15:13 -08003850 (int) mDragViewVisualCenter[1], 1, 1, 1, 1,
Adam Cohenea889a22012-03-27 16:45:39 -07003851 null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003852 } else {
3853 cellLayout.findCellForSpan(mTargetCell, 1, 1);
3854 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003855 addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX,
Winson Chung3d503fb2011-07-13 17:25:49 -07003856 info.spanY, insertAtFirst);
Adam Cohen716b51e2011-06-30 12:09:54 -07003857 cellLayout.onDropChild(view);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07003858 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
Michael Jurkaa52570f2012-03-20 03:18:20 -07003859 cellLayout.getShortcutsAndWidgets().measureChild(view);
Adam Cohend5e42732011-03-28 17:33:39 -07003860
Adam Cohendcd297f2013-06-18 13:13:40 -07003861 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId,
Winson Chungaafa03c2010-06-11 17:34:16 -07003862 lp.cellX, lp.cellY);
Adam Cohen3e8f8112011-07-02 18:03:00 -07003863
3864 if (d.dragView != null) {
Adam Cohen4b285c52011-07-21 14:24:06 -07003865 // We wrap the animation call in the temporary set and reset of the current
3866 // cellLayout to its final transform -- this means we animate the drag view to
3867 // the correct final location.
3868 setFinalTransitionTransform(cellLayout);
Winson Chung557d6ed2011-07-08 15:34:52 -07003869 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view,
Adam Cohen28f852a2013-10-15 14:34:05 -07003870 exitSpringLoadedRunnable, this);
Adam Cohen4b285c52011-07-21 14:24:06 -07003871 resetTransitionTransform(cellLayout);
Adam Cohen3e8f8112011-07-02 18:03:00 -07003872 }
Joe Onorato00acb122009-08-04 16:04:30 -04003873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003874 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003875
Adam Cohend41fbf52012-02-16 23:53:59 -08003876 public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003877 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo.spanX,
3878 widgetInfo.spanY, widgetInfo, false);
Adam Cohend41fbf52012-02-16 23:53:59 -08003879 int visibility = layout.getVisibility();
Adam Cohened66b2b2012-01-23 17:28:51 -08003880 layout.setVisibility(VISIBLE);
3881
3882 int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
3883 int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
3884 Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
3885 Bitmap.Config.ARGB_8888);
3886 Canvas c = new Canvas(b);
3887
3888 layout.measure(width, height);
3889 layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
Adam Cohened66b2b2012-01-23 17:28:51 -08003890 layout.draw(c);
3891 c.setBitmap(null);
Adam Cohend41fbf52012-02-16 23:53:59 -08003892 layout.setVisibility(visibility);
Adam Cohened66b2b2012-01-23 17:28:51 -08003893 return b;
3894 }
3895
Adam Cohend41fbf52012-02-16 23:53:59 -08003896 private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07003897 DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell,
Michael Jurkaa2fe7862012-05-29 05:58:17 -07003898 boolean external, boolean scale) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003899 // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
3900 // location and size on the home screen.
Adam Cohend41fbf52012-02-16 23:53:59 -08003901 int spanX = info.spanX;
3902 int spanY = info.spanY;
Adam Cohened66b2b2012-01-23 17:28:51 -08003903
Adam Cohend41fbf52012-02-16 23:53:59 -08003904 Rect r = estimateItemPosition(layout, info, targetCell[0], targetCell[1], spanX, spanY);
3905 loc[0] = r.left;
3906 loc[1] = r.top;
3907
3908 setFinalTransitionTransform(layout);
3909 float cellLayoutScale =
Adam Cohen7d30a372013-07-01 17:03:59 -07003910 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true);
Adam Cohend41fbf52012-02-16 23:53:59 -08003911 resetTransitionTransform(layout);
Michael Jurkaa2fe7862012-05-29 05:58:17 -07003912
3913 float dragViewScaleX;
3914 float dragViewScaleY;
3915 if (scale) {
3916 dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth();
3917 dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight();
3918 } else {
3919 dragViewScaleX = 1f;
3920 dragViewScaleY = 1f;
3921 }
Adam Cohend41fbf52012-02-16 23:53:59 -08003922
Adam Cohened66b2b2012-01-23 17:28:51 -08003923 // The animation will scale the dragView about its center, so we need to center about
3924 // the final location.
3925 loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2;
3926 loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;
3927
Adam Cohend41fbf52012-02-16 23:53:59 -08003928 scaleXY[0] = dragViewScaleX * cellLayoutScale;
3929 scaleXY[1] = dragViewScaleY * cellLayoutScale;
3930 }
3931
3932 public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, DragView dragView,
3933 final Runnable onCompleteRunnable, int animationType, final View finalView,
3934 boolean external) {
3935 Rect from = new Rect();
3936 mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from);
3937
3938 int[] finalPos = new int[2];
3939 float scaleXY[] = new float[2];
Michael Jurkaa2fe7862012-05-29 05:58:17 -07003940 boolean scalePreview = !(info instanceof PendingAddShortcutInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08003941 getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell,
Michael Jurkaa2fe7862012-05-29 05:58:17 -07003942 external, scalePreview);
Adam Cohened66b2b2012-01-23 17:28:51 -08003943
3944 Resources res = mLauncher.getResources();
Adam Cohenad4e15c2013-10-17 16:21:35 -07003945 final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200;
Adam Cohened66b2b2012-01-23 17:28:51 -08003946
Adam Cohend41fbf52012-02-16 23:53:59 -08003947 // In the case where we've prebound the widget, we remove it from the DragLayer
3948 if (finalView instanceof AppWidgetHostView && external) {
Adam Cohen68f681b2012-05-28 15:01:16 -07003949 Log.d(TAG, "6557954 Animate widget drop, final view is appWidgetHostView");
Adam Cohend41fbf52012-02-16 23:53:59 -08003950 mLauncher.getDragLayer().removeView(finalView);
3951 }
3952 if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) {
3953 Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView);
Adam Cohened66b2b2012-01-23 17:28:51 -08003954 dragView.setCrossFadeBitmap(crossFadeBitmap);
3955 dragView.crossFade((int) (duration * 0.8f));
Adam Cohend41fbf52012-02-16 23:53:59 -08003956 } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && external) {
3957 scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]);
Adam Cohened66b2b2012-01-23 17:28:51 -08003958 }
3959
Adam Cohend41fbf52012-02-16 23:53:59 -08003960 DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung7bd1bbb2012-02-13 18:29:29 -08003961 if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) {
Adam Cohend41fbf52012-02-16 23:53:59 -08003962 mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f,
Adam Cohened66b2b2012-01-23 17:28:51 -08003963 DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration);
3964 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08003965 int endStyle;
3966 if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) {
3967 endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE;
3968 } else {
3969 endStyle = DragLayer.ANIMATION_END_DISAPPEAR;;
3970 }
3971
3972 Runnable onComplete = new Runnable() {
3973 @Override
3974 public void run() {
3975 if (finalView != null) {
3976 finalView.setVisibility(VISIBLE);
3977 }
3978 if (onCompleteRunnable != null) {
3979 onCompleteRunnable.run();
3980 }
3981 }
3982 };
3983 dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0],
3984 finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle,
3985 duration, this);
Adam Cohened66b2b2012-01-23 17:28:51 -08003986 }
3987 }
3988
Adam Cohen4b285c52011-07-21 14:24:06 -07003989 public void setFinalTransitionTransform(CellLayout layout) {
3990 if (isSwitchingState()) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003991 mCurrentScale = getScaleX();
3992 setScaleX(mNewScale);
3993 setScaleY(mNewScale);
Adam Cohen4b285c52011-07-21 14:24:06 -07003994 }
3995 }
3996 public void resetTransitionTransform(CellLayout layout) {
3997 if (isSwitchingState()) {
Adam Cohen7d30a372013-07-01 17:03:59 -07003998 setScaleX(mCurrentScale);
3999 setScaleY(mCurrentScale);
Adam Cohen4b285c52011-07-21 14:24:06 -07004000 }
4001 }
4002
Jeff Sharkey70864282009-04-07 21:08:40 -07004003 /**
4004 * Return the current {@link CellLayout}, correctly picking the destination
4005 * screen while a scroll is in progress.
4006 */
Patrick Dubroy5f445422011-02-18 14:35:21 -08004007 public CellLayout getCurrentDropLayout() {
Winson Chung360e63f2012-04-27 13:48:05 -07004008 return (CellLayout) getChildAt(getNextPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004009 }
4010
Jeff Sharkey70864282009-04-07 21:08:40 -07004011 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07004012 * Return the current CellInfo describing our current drag; this method exists
4013 * so that Launcher can sync this object with the correct info when the activity is created/
4014 * destroyed
4015 *
4016 */
4017 public CellLayout.CellInfo getDragInfo() {
4018 return mDragInfo;
4019 }
4020
Winson Chung9b9fb962013-11-15 15:39:34 -08004021 public int getCurrentPageOffsetFromCustomContent() {
Adam Cohen21cd0022013-10-09 18:57:02 -07004022 return getNextPage() - numCustomPages();
4023 }
4024
Michael Jurka0280c3b2010-09-17 15:00:07 -07004025 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07004026 * Calculate the nearest cell where the given object would be dropped.
Adam Cohene3e27a82011-04-15 12:07:39 -07004027 *
4028 * pixelX and pixelY should be in the coordinate system of layout
Jeff Sharkey70864282009-04-07 21:08:40 -07004029 */
Adam Cohendf035382011-04-11 17:22:04 -07004030 private int[] findNearestArea(int pixelX, int pixelY,
Adam Cohene3e27a82011-04-15 12:07:39 -07004031 int spanX, int spanY, CellLayout layout, int[] recycle) {
Adam Cohendf035382011-04-11 17:22:04 -07004032 return layout.findNearestArea(
Adam Cohene3e27a82011-04-15 12:07:39 -07004033 pixelX, pixelY, spanX, spanY, recycle);
Adam Cohendf035382011-04-11 17:22:04 -07004034 }
4035
Adam Cohencff6af82011-09-13 14:51:53 -07004036 void setup(DragController dragController) {
Michael Jurkac2f7f472010-12-14 15:34:42 -08004037 mSpringLoadedDragController = new SpringLoadedDragController(mLauncher);
Joe Onorato00acb122009-08-04 16:04:30 -04004038 mDragController = dragController;
Michael Jurkad74c9842011-07-10 12:44:21 -07004039
Michael Jurkad74c9842011-07-10 12:44:21 -07004040 // hardware layers on children are enabled on startup, but should be disabled until
4041 // needed
Michael Jurka3a0469d2012-06-21 09:38:41 -07004042 updateChildrenLayersEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004043 }
4044
Patrick Dubroye3887cc2011-01-20 10:43:40 -08004045 /**
4046 * Called at the end of a drag which originated on the workspace.
4047 */
Michael Jurka1e2f4652013-07-08 18:03:46 -07004048 public void onDropCompleted(final View target, final DragObject d,
4049 final boolean isFlingToDelete, final boolean success) {
4050 if (mDeferDropAfterUninstall) {
4051 mDeferredAction = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -07004052 public void run() {
4053 onDropCompleted(target, d, isFlingToDelete, success);
4054 mDeferredAction = null;
4055 }
4056 };
Michael Jurka1e2f4652013-07-08 18:03:46 -07004057 return;
4058 }
4059
4060 boolean beingCalledAfterUninstall = mDeferredAction != null;
4061
4062 if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) {
4063 if (target != this && mDragInfo != null) {
Daniel Sandlerb56e95a2013-09-04 23:13:39 -04004064 CellLayout parentCell = getParentCellLayoutForView(mDragInfo.cell);
4065 if (parentCell != null) {
4066 parentCell.removeView(mDragInfo.cell);
4067 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004068 if (mDragInfo.cell instanceof DropTarget) {
4069 mDragController.removeDropTarget((DropTarget) mDragInfo.cell);
Joe Onorato00acb122009-08-04 16:04:30 -04004070 }
Adam Cohend6e7aa32013-07-09 15:32:37 -07004071 // If we move the item to anything not on the Workspace, check if any empty
4072 // screens need to be removed. If we dropped back on the workspace, this will
4073 // be done post drop animation.
Adam Cohenad4e15c2013-10-17 16:21:35 -07004074 removeExtraEmptyScreen(true, null, 0, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004075 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07004076 } else if (mDragInfo != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004077 CellLayout cellLayout;
4078 if (mLauncher.isHotseatLayout(target)) {
4079 cellLayout = mLauncher.getHotseat().getLayout();
4080 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07004081 cellLayout = getScreenWithId(mDragInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07004082 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -08004083 if (cellLayout == null && LauncherAppState.isDogfoodBuild()) {
4084 throw new RuntimeException("Invalid state: cellLayout == null in "
4085 + "Workspace#onDropCompleted. Please file a bug. ");
4086 }
4087 if (cellLayout != null) {
4088 cellLayout.onDropChild(mDragInfo.cell);
4089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004090 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004091 if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful))
4092 && mDragInfo.cell != null) {
4093 mDragInfo.cell.setVisibility(VISIBLE);
Adam Cohen36cc09b2011-09-29 17:33:15 -07004094 }
Joe Onorato4be866d2010-10-10 11:26:02 -07004095 mDragOutline = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004096 mDragInfo = null;
4097 }
4098
Michael Jurka1e2f4652013-07-08 18:03:46 -07004099 public void deferCompleteDropAfterUninstallActivity() {
4100 mDeferDropAfterUninstall = true;
4101 }
4102
4103 /// maybe move this into a smaller part
4104 public void onUninstallActivityReturned(boolean success) {
4105 mDeferDropAfterUninstall = false;
4106 mUninstallSuccessful = success;
4107 if (mDeferredAction != null) {
4108 mDeferredAction.run();
4109 }
4110 }
4111
Adam Cohenea889a22012-03-27 16:45:39 -07004112 void updateItemLocationsInDatabase(CellLayout cl) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004113 int count = cl.getShortcutsAndWidgets().getChildCount();
Adam Cohend3461712012-03-29 17:25:17 -07004114
Adam Cohendcd297f2013-06-18 13:13:40 -07004115 long screenId = getIdForScreen(cl);
Adam Cohend3461712012-03-29 17:25:17 -07004116 int container = Favorites.CONTAINER_DESKTOP;
4117
4118 if (mLauncher.isHotseatLayout(cl)) {
Adam Cohendcd297f2013-06-18 13:13:40 -07004119 screenId = -1;
Adam Cohend3461712012-03-29 17:25:17 -07004120 container = Favorites.CONTAINER_HOTSEAT;
4121 }
4122
Adam Cohen482ed822012-03-02 14:15:13 -08004123 for (int i = 0; i < count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004124 View v = cl.getShortcutsAndWidgets().getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08004125 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07004126 // Null check required as the AllApps button doesn't have an item info
Adam Cohen487f7dd2012-06-28 18:12:10 -07004127 if (info != null && info.requiresDbUpdate) {
4128 info.requiresDbUpdate = false;
Adam Cohendcd297f2013-06-18 13:13:40 -07004129 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, info.cellX,
Adam Cohenbebf0422012-04-11 18:06:28 -07004130 info.cellY, info.spanX, info.spanY);
Adam Cohen2acce882012-03-28 19:03:19 -07004131 }
Adam Cohen482ed822012-03-02 14:15:13 -08004132 }
4133 }
4134
Adam Cohen4caf2982013-08-20 18:54:31 -07004135 ArrayList<ComponentName> getUniqueComponents(boolean stripDuplicates, ArrayList<ComponentName> duplicates) {
Adam Cohene25af792013-06-06 23:08:25 -07004136 ArrayList<ComponentName> uniqueIntents = new ArrayList<ComponentName>();
Adam Cohen4caf2982013-08-20 18:54:31 -07004137 getUniqueIntents((CellLayout) mLauncher.getHotseat().getLayout(), uniqueIntents, duplicates, false);
Adam Cohene25af792013-06-06 23:08:25 -07004138 int count = getChildCount();
4139 for (int i = 0; i < count; i++) {
4140 CellLayout cl = (CellLayout) getChildAt(i);
Adam Cohen4caf2982013-08-20 18:54:31 -07004141 getUniqueIntents(cl, uniqueIntents, duplicates, false);
Adam Cohene25af792013-06-06 23:08:25 -07004142 }
4143 return uniqueIntents;
4144 }
4145
Adam Cohen4caf2982013-08-20 18:54:31 -07004146 void getUniqueIntents(CellLayout cl, ArrayList<ComponentName> uniqueIntents,
4147 ArrayList<ComponentName> duplicates, boolean stripDuplicates) {
Adam Cohene25af792013-06-06 23:08:25 -07004148 int count = cl.getShortcutsAndWidgets().getChildCount();
4149
4150 ArrayList<View> children = new ArrayList<View>();
4151 for (int i = 0; i < count; i++) {
4152 View v = cl.getShortcutsAndWidgets().getChildAt(i);
4153 children.add(v);
4154 }
4155
4156 for (int i = 0; i < count; i++) {
4157 View v = children.get(i);
4158 ItemInfo info = (ItemInfo) v.getTag();
4159 // Null check required as the AllApps button doesn't have an item info
4160 if (info instanceof ShortcutInfo) {
4161 ShortcutInfo si = (ShortcutInfo) info;
4162 ComponentName cn = si.intent.getComponent();
4163
Adam Cohen4caf2982013-08-20 18:54:31 -07004164 Uri dataUri = si.intent.getData();
4165 // If dataUri is not null / empty or if this component isn't one that would
4166 // have previously showed up in the AllApps list, then this is a widget-type
4167 // shortcut, so ignore it.
4168 if (dataUri != null && !dataUri.equals(Uri.EMPTY)) {
Adam Cohen99894d92013-06-14 11:22:59 -07004169 continue;
4170 }
Adam Cohena0b57492013-06-14 15:33:35 -07004171
Adam Cohene25af792013-06-06 23:08:25 -07004172 if (!uniqueIntents.contains(cn)) {
4173 uniqueIntents.add(cn);
4174 } else {
Adam Cohen4caf2982013-08-20 18:54:31 -07004175 if (stripDuplicates) {
4176 cl.removeViewInLayout(v);
4177 LauncherModel.deleteItemFromDatabase(mLauncher, si);
4178 }
4179 if (duplicates != null) {
4180 duplicates.add(cn);
4181 }
Adam Cohene25af792013-06-06 23:08:25 -07004182 }
4183 }
4184 if (v instanceof FolderIcon) {
4185 FolderIcon fi = (FolderIcon) v;
4186 ArrayList<View> items = fi.getFolder().getItemsInReadingOrder();
4187 for (int j = 0; j < items.size(); j++) {
4188 if (items.get(j).getTag() instanceof ShortcutInfo) {
4189 ShortcutInfo si = (ShortcutInfo) items.get(j).getTag();
4190 ComponentName cn = si.intent.getComponent();
4191
Adam Cohen4caf2982013-08-20 18:54:31 -07004192 Uri dataUri = si.intent.getData();
4193 // If dataUri is not null / empty or if this component isn't one that would
4194 // have previously showed up in the AllApps list, then this is a widget-type
4195 // shortcut, so ignore it.
4196 if (dataUri != null && !dataUri.equals(Uri.EMPTY)) {
Adam Cohen99894d92013-06-14 11:22:59 -07004197 continue;
4198 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004199
Adam Cohene25af792013-06-06 23:08:25 -07004200 if (!uniqueIntents.contains(cn)) {
4201 uniqueIntents.add(cn);
Adam Cohen4caf2982013-08-20 18:54:31 -07004202 } else {
4203 if (stripDuplicates) {
4204 fi.getFolderInfo().remove(si);
4205 LauncherModel.deleteItemFromDatabase(mLauncher, si);
4206 }
4207 if (duplicates != null) {
4208 duplicates.add(cn);
4209 }
Adam Cohene25af792013-06-06 23:08:25 -07004210 }
4211 }
4212 }
4213 }
4214 }
4215 }
4216
4217 void saveWorkspaceToDb() {
4218 saveWorkspaceScreenToDb((CellLayout) mLauncher.getHotseat().getLayout());
4219 int count = getChildCount();
4220 for (int i = 0; i < count; i++) {
4221 CellLayout cl = (CellLayout) getChildAt(i);
4222 saveWorkspaceScreenToDb(cl);
4223 }
4224 }
4225
4226 void saveWorkspaceScreenToDb(CellLayout cl) {
4227 int count = cl.getShortcutsAndWidgets().getChildCount();
4228
Adam Cohendcd297f2013-06-18 13:13:40 -07004229 long screenId = getIdForScreen(cl);
Adam Cohene25af792013-06-06 23:08:25 -07004230 int container = Favorites.CONTAINER_DESKTOP;
4231
Winson Chungf70696d2013-06-25 16:19:53 -07004232 Hotseat hotseat = mLauncher.getHotseat();
Adam Cohene25af792013-06-06 23:08:25 -07004233 if (mLauncher.isHotseatLayout(cl)) {
Adam Cohendcd297f2013-06-18 13:13:40 -07004234 screenId = -1;
Adam Cohene25af792013-06-06 23:08:25 -07004235 container = Favorites.CONTAINER_HOTSEAT;
4236 }
4237
4238 for (int i = 0; i < count; i++) {
4239 View v = cl.getShortcutsAndWidgets().getChildAt(i);
4240 ItemInfo info = (ItemInfo) v.getTag();
4241 // Null check required as the AllApps button doesn't have an item info
4242 if (info != null) {
Winson Chungf70696d2013-06-25 16:19:53 -07004243 int cellX = info.cellX;
4244 int cellY = info.cellY;
4245 if (container == Favorites.CONTAINER_HOTSEAT) {
4246 cellX = hotseat.getCellXFromOrder((int) info.screenId);
4247 cellY = hotseat.getCellYFromOrder((int) info.screenId);
4248 }
4249 LauncherModel.addItemToDatabase(mLauncher, info, container, screenId, cellX,
4250 cellY, false);
Adam Cohene25af792013-06-06 23:08:25 -07004251 }
4252 if (v instanceof FolderIcon) {
4253 FolderIcon fi = (FolderIcon) v;
4254 fi.getFolder().addItemLocationsInDatabase();
4255 }
4256 }
4257 }
4258
Winson Chunga48487a2012-03-20 16:19:37 -07004259 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -08004260 public float getIntrinsicIconScaleFactor() {
4261 return 1f;
4262 }
4263
4264 @Override
Winson Chung043f2af2012-03-01 16:09:54 -08004265 public boolean supportsFlingToDelete() {
4266 return true;
4267 }
4268
Winson Chunga48487a2012-03-20 16:19:37 -07004269 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +00004270 public boolean supportsAppInfoDropTarget() {
4271 return false;
4272 }
4273
4274 @Override
4275 public boolean supportsDeleteDropTarget() {
4276 return true;
4277 }
4278
4279 @Override
Winson Chunga48487a2012-03-20 16:19:37 -07004280 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
4281 // Do nothing
4282 }
4283
4284 @Override
4285 public void onFlingToDeleteCompleted() {
4286 // Do nothing
4287 }
4288
Michael Jurka0280c3b2010-09-17 15:00:07 -07004289 public boolean isDropEnabled() {
4290 return true;
4291 }
4292
Michael Jurka0142d492010-08-25 17:46:15 -07004293 @Override
4294 protected void onRestoreInstanceState(Parcelable state) {
4295 super.onRestoreInstanceState(state);
4296 Launcher.setScreen(mCurrentPage);
4297 }
4298
4299 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07004300 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
4301 // We don't dispatch restoreInstanceState to our children using this code path.
Dave Hawkeya8881582013-09-17 15:55:33 -06004302 // Some pages will be restored immediately as their items are bound immediately, and
Adam Cohen1462de32012-07-24 22:34:36 -07004303 // others we will need to wait until after their items are bound.
4304 mSavedStates = container;
4305 }
4306
4307 public void restoreInstanceStateForChild(int child) {
4308 if (mSavedStates != null) {
4309 mRestoredPages.add(child);
4310 CellLayout cl = (CellLayout) getChildAt(child);
Adam Cohenb0ee0812013-12-03 10:51:45 -08004311 if (cl != null) {
4312 cl.restoreInstanceState(mSavedStates);
4313 }
Adam Cohen1462de32012-07-24 22:34:36 -07004314 }
4315 }
4316
4317 public void restoreInstanceStateForRemainingPages() {
4318 int count = getChildCount();
4319 for (int i = 0; i < count; i++) {
4320 if (!mRestoredPages.contains(i)) {
4321 restoreInstanceStateForChild(i);
4322 }
4323 }
4324 mRestoredPages.clear();
Winson Chungd8e596d2013-10-21 17:14:12 -07004325 mSavedStates = null;
Adam Cohen1462de32012-07-24 22:34:36 -07004326 }
4327
4328 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004329 public void scrollLeft() {
Michael Jurkad74c9842011-07-10 12:44:21 -07004330 if (!isSmall() && !mIsSwitchingState) {
Michael Jurka0142d492010-08-25 17:46:15 -07004331 super.scrollLeft();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004332 }
Adam Cohen95bb8002011-07-03 23:40:28 -07004333 Folder openFolder = getOpenFolder();
4334 if (openFolder != null) {
4335 openFolder.completeDragExit();
4336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004337 }
4338
Michael Jurka0142d492010-08-25 17:46:15 -07004339 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004340 public void scrollRight() {
Michael Jurkad74c9842011-07-10 12:44:21 -07004341 if (!isSmall() && !mIsSwitchingState) {
Michael Jurka0142d492010-08-25 17:46:15 -07004342 super.scrollRight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004343 }
Adam Cohen95bb8002011-07-03 23:40:28 -07004344 Folder openFolder = getOpenFolder();
4345 if (openFolder != null) {
4346 openFolder.completeDragExit();
4347 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004348 }
4349
Patrick Dubroy1262e362010-10-06 15:49:50 -07004350 @Override
Winson Chung3e0839e2011-10-03 15:15:18 -07004351 public boolean onEnterScrollArea(int x, int y, int direction) {
Winson Chung1c4cf4a2011-07-29 14:49:10 -07004352 // Ignore the scroll area if we are dragging over the hot seat
Daniel Sandlercc8befa2013-06-11 14:45:48 -04004353 boolean isPortrait = !LauncherAppState.isScreenLandscape(getContext());
Winson Chung10bfc6e2012-03-28 15:41:26 -07004354 if (mLauncher.getHotseat() != null && isPortrait) {
Winson Chung1c4cf4a2011-07-29 14:49:10 -07004355 Rect r = new Rect();
4356 mLauncher.getHotseat().getHitRect(r);
4357 if (r.contains(x, y)) {
Winson Chung3e0839e2011-10-03 15:15:18 -07004358 return false;
Winson Chung1c4cf4a2011-07-29 14:49:10 -07004359 }
4360 }
4361
Winson Chung3e0839e2011-10-03 15:15:18 -07004362 boolean result = false;
Adam Cohen61a9a5c2013-08-14 13:32:04 -07004363 if (!isSmall() && !mIsSwitchingState && getOpenFolder() == null) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07004364 mInScrollArea = true;
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08004365
Winson Chung360e63f2012-04-27 13:48:05 -07004366 final int page = getNextPage() +
Winson Chungaa15ffe2012-01-18 15:45:28 -08004367 (direction == DragController.SCROLL_LEFT ? -1 : 1);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07004368 // We always want to exit the current layout to ensure parity of enter / exit
4369 setCurrentDropLayout(null);
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08004370
Winson Chungaa15ffe2012-01-18 15:45:28 -08004371 if (0 <= page && page < getChildCount()) {
Winson Chung482a5b62013-07-31 17:19:51 -07004372 // Ensure that we are not dragging over to the custom content screen
4373 if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) {
4374 return false;
4375 }
4376
Winson Chungaa15ffe2012-01-18 15:45:28 -08004377 CellLayout layout = (CellLayout) getChildAt(page);
Adam Cohenc6cc61d2012-04-04 12:47:08 -07004378 setCurrentDragOverlappingLayout(layout);
Winson Chungc07918d2011-07-01 15:35:26 -07004379
4380 // Workspace is responsible for drawing the edge glow on adjacent pages,
4381 // so we need to redraw the workspace when this may have changed.
4382 invalidate();
Winson Chung3e0839e2011-10-03 15:15:18 -07004383 result = true;
Michael Jurkad718d6a2010-10-14 15:35:17 -07004384 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07004385 }
Winson Chung3e0839e2011-10-03 15:15:18 -07004386 return result;
Patrick Dubroy1262e362010-10-06 15:49:50 -07004387 }
4388
4389 @Override
Winson Chung3e0839e2011-10-03 15:15:18 -07004390 public boolean onExitScrollArea() {
4391 boolean result = false;
Michael Jurkad718d6a2010-10-14 15:35:17 -07004392 if (mInScrollArea) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07004393 invalidate();
4394 CellLayout layout = getCurrentDropLayout();
4395 setCurrentDropLayout(layout);
4396 setCurrentDragOverlappingLayout(layout);
4397
Adam Cohen8a18afc2011-12-13 15:57:01 -08004398 result = true;
Winson Chungc07918d2011-07-01 15:35:26 -07004399 mInScrollArea = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -07004400 }
Winson Chung3e0839e2011-10-03 15:15:18 -07004401 return result;
Patrick Dubroy1262e362010-10-06 15:49:50 -07004402 }
4403
Winson Chungc07918d2011-07-01 15:35:26 -07004404 private void onResetScrollArea() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07004405 setCurrentDragOverlappingLayout(null);
Winson Chungc07918d2011-07-01 15:35:26 -07004406 mInScrollArea = false;
4407 }
4408
Winson Chung3d503fb2011-07-13 17:25:49 -07004409 /**
4410 * Returns a specific CellLayout
4411 */
4412 CellLayout getParentCellLayoutForView(View v) {
4413 ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts();
4414 for (CellLayout layout : layouts) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004415 if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004416 return layout;
4417 }
4418 }
4419 return null;
4420 }
4421
4422 /**
4423 * Returns a list of all the CellLayouts in the workspace.
4424 */
4425 ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() {
4426 ArrayList<CellLayout> layouts = new ArrayList<CellLayout>();
4427 int screenCount = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004428 for (int screen = 0; screen < screenCount; screen++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004429 layouts.add(((CellLayout) getChildAt(screen)));
4430 }
4431 if (mLauncher.getHotseat() != null) {
4432 layouts.add(mLauncher.getHotseat().getLayout());
4433 }
4434 return layouts;
4435 }
4436
4437 /**
4438 * We should only use this to search for specific children. Do not use this method to modify
Michael Jurkaa52570f2012-03-20 03:18:20 -07004439 * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from
4440 * the hotseat and workspace pages
Winson Chung3d503fb2011-07-13 17:25:49 -07004441 */
Michael Jurkaa52570f2012-03-20 03:18:20 -07004442 ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() {
4443 ArrayList<ShortcutAndWidgetContainer> childrenLayouts =
4444 new ArrayList<ShortcutAndWidgetContainer>();
Winson Chung3d503fb2011-07-13 17:25:49 -07004445 int screenCount = getChildCount();
4446 for (int screen = 0; screen < screenCount; screen++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004447 childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets());
Winson Chung3d503fb2011-07-13 17:25:49 -07004448 }
4449 if (mLauncher.getHotseat() != null) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004450 childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets());
Winson Chung3d503fb2011-07-13 17:25:49 -07004451 }
4452 return childrenLayouts;
4453 }
4454
4455 public Folder getFolderForTag(Object tag) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004456 ArrayList<ShortcutAndWidgetContainer> childrenLayouts =
4457 getAllShortcutAndWidgetContainers();
4458 for (ShortcutAndWidgetContainer layout: childrenLayouts) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004459 int count = layout.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004460 for (int i = 0; i < count; i++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004461 View child = layout.getChildAt(i);
Winson Chung3d503fb2011-07-13 17:25:49 -07004462 if (child instanceof Folder) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004463 Folder f = (Folder) child;
Winson Chungaafa03c2010-06-11 17:34:16 -07004464 if (f.getInfo() == tag && f.getInfo().opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004465 return f;
4466 }
4467 }
4468 }
4469 }
4470 return null;
4471 }
4472
4473 public View getViewForTag(Object tag) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004474 ArrayList<ShortcutAndWidgetContainer> childrenLayouts =
4475 getAllShortcutAndWidgetContainers();
4476 for (ShortcutAndWidgetContainer layout: childrenLayouts) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004477 int count = layout.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004478 for (int i = 0; i < count; i++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004479 View child = layout.getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004480 if (child.getTag() == tag) {
4481 return child;
4482 }
4483 }
4484 }
4485 return null;
4486 }
4487
Adam Cohendf035382011-04-11 17:22:04 -07004488 void clearDropTargets() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004489 ArrayList<ShortcutAndWidgetContainer> childrenLayouts =
4490 getAllShortcutAndWidgetContainers();
4491 for (ShortcutAndWidgetContainer layout: childrenLayouts) {
Adam Cohendf035382011-04-11 17:22:04 -07004492 int childCount = layout.getChildCount();
4493 for (int j = 0; j < childCount; j++) {
4494 View v = layout.getChildAt(j);
4495 if (v instanceof DropTarget) {
4496 mDragController.removeDropTarget((DropTarget) v);
4497 }
4498 }
4499 }
4500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004501
Winson Chung83892cc2013-05-01 16:53:33 -07004502 // Removes ALL items that match a given package name, this is usually called when a package
4503 // has been removed and we want to remove all components (widgets, shortcuts, apps) that
4504 // belong to that package.
4505 void removeItemsByPackageName(final ArrayList<String> packages) {
Winson Chung64359a52013-07-08 17:17:08 -07004506 final HashSet<String> packageNames = new HashSet<String>();
Winson Chungcd810732012-06-18 16:45:43 -07004507 packageNames.addAll(packages);
Joe Onorato64e6be72010-03-05 15:05:52 -05004508
Winson Chung64359a52013-07-08 17:17:08 -07004509 // Filter out all the ItemInfos that this is going to affect
4510 final HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
4511 final HashSet<ComponentName> cns = new HashSet<ComponentName>();
Winson Chung83892cc2013-05-01 16:53:33 -07004512 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
4513 for (CellLayout layoutParent : cellLayouts) {
4514 ViewGroup layout = layoutParent.getShortcutsAndWidgets();
4515 int childCount = layout.getChildCount();
4516 for (int i = 0; i < childCount; ++i) {
4517 View view = layout.getChildAt(i);
Winson Chung64359a52013-07-08 17:17:08 -07004518 infos.add((ItemInfo) view.getTag());
Winson Chung83892cc2013-05-01 16:53:33 -07004519 }
4520 }
Winson Chung64359a52013-07-08 17:17:08 -07004521 LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() {
4522 @Override
4523 public boolean filterItem(ItemInfo parent, ItemInfo info,
4524 ComponentName cn) {
4525 if (packageNames.contains(cn.getPackageName())) {
4526 cns.add(cn);
4527 return true;
4528 }
4529 return false;
4530 }
4531 };
4532 LauncherModel.filterItemInfos(infos, filter);
Winson Chung83892cc2013-05-01 16:53:33 -07004533
Winson Chung64359a52013-07-08 17:17:08 -07004534 // Remove the affected components
Winson Chung83892cc2013-05-01 16:53:33 -07004535 removeItemsByComponentName(cns);
4536 }
4537
4538 // Removes items that match the application info specified, when applications are removed
4539 // as a part of an update, this is called to ensure that other widgets and application
4540 // shortcuts are not removed.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004541 void removeItemsByApplicationInfo(final ArrayList<AppInfo> appInfos) {
Winson Chung83892cc2013-05-01 16:53:33 -07004542 // Just create a hash table of all the specific components that this will affect
4543 HashSet<ComponentName> cns = new HashSet<ComponentName>();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004544 for (AppInfo info : appInfos) {
Winson Chung83892cc2013-05-01 16:53:33 -07004545 cns.add(info.componentName);
4546 }
4547
4548 // Remove all the things
4549 removeItemsByComponentName(cns);
4550 }
4551
4552 void removeItemsByComponentName(final HashSet<ComponentName> componentNames) {
Winson Chung3d503fb2011-07-13 17:25:49 -07004553 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
4554 for (final CellLayout layoutParent: cellLayouts) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07004555 final ViewGroup layout = layoutParent.getShortcutsAndWidgets();
Romain Guy574d20e2009-06-01 15:34:04 -07004556
Winson Chung64359a52013-07-08 17:17:08 -07004557 final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>();
4558 for (int j = 0; j < layout.getChildCount(); j++) {
4559 final View view = layout.getChildAt(j);
4560 children.put((ItemInfo) view.getTag(), view);
4561 }
Winson Chungaafa03c2010-06-11 17:34:16 -07004562
Winson Chung64359a52013-07-08 17:17:08 -07004563 final ArrayList<View> childrenToRemove = new ArrayList<View>();
4564 final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove =
4565 new HashMap<FolderInfo, ArrayList<ShortcutInfo>>();
4566 LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() {
4567 @Override
4568 public boolean filterItem(ItemInfo parent, ItemInfo info,
4569 ComponentName cn) {
4570 if (parent instanceof FolderInfo) {
4571 if (componentNames.contains(cn)) {
4572 FolderInfo folder = (FolderInfo) parent;
4573 ArrayList<ShortcutInfo> appsToRemove;
4574 if (folderAppsToRemove.containsKey(folder)) {
4575 appsToRemove = folderAppsToRemove.get(folder);
4576 } else {
4577 appsToRemove = new ArrayList<ShortcutInfo>();
4578 folderAppsToRemove.put(folder, appsToRemove);
Romain Guy629de3e2010-01-13 12:20:59 -08004579 }
Winson Chung64359a52013-07-08 17:17:08 -07004580 appsToRemove.add((ShortcutInfo) info);
4581 return true;
4582 }
4583 } else {
4584 if (componentNames.contains(cn)) {
4585 childrenToRemove.add(children.get(info));
4586 return true;
Romain Guy574d20e2009-06-01 15:34:04 -07004587 }
4588 }
Winson Chung64359a52013-07-08 17:17:08 -07004589 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004590 }
Winson Chung64359a52013-07-08 17:17:08 -07004591 };
4592 LauncherModel.filterItemInfos(children.keySet(), filter);
4593
4594 // Remove all the apps from their folders
4595 for (FolderInfo folder : folderAppsToRemove.keySet()) {
4596 ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder);
4597 for (ShortcutInfo info : appsToRemove) {
4598 folder.remove(info);
4599 }
4600 }
4601
4602 // Remove all the other children
4603 for (View child : childrenToRemove) {
4604 // Note: We can not remove the view directly from CellLayoutChildren as this
4605 // does not re-mark the spaces as unoccupied.
4606 layoutParent.removeViewInLayout(child);
4607 if (child instanceof DropTarget) {
4608 mDragController.removeDropTarget((DropTarget) child);
4609 }
4610 }
4611
4612 if (childrenToRemove.size() > 0) {
4613 layout.requestLayout();
4614 layout.invalidate();
4615 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004616 }
Winson Chung2efec4e2012-05-03 12:31:48 -07004617
Winson Chung64359a52013-07-08 17:17:08 -07004618 // Strip all the empty screens
4619 stripEmptyScreens();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004620 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004621
Winson Chung8c903312013-10-16 17:51:49 -07004622 private void updateShortcut(HashMap<ComponentName, AppInfo> appsMap, ItemInfo info,
4623 View child) {
4624 ComponentName cn = info.getIntent().getComponent();
Chris Wrenb6d4c282014-01-27 14:17:08 -05004625 if (info.getRestoredIntent() != null) {
4626 cn = info.getRestoredIntent().getComponent();
4627 }
Winson Chung8c903312013-10-16 17:51:49 -07004628 if (cn != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05004629 AppInfo appInfo = appsMap.get(cn);
Winson Chung8c903312013-10-16 17:51:49 -07004630 if ((appInfo != null) && LauncherModel.isShortcutInfoUpdateable(info)) {
4631 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
4632 BubbleTextView shortcut = (BubbleTextView) child;
Chris Wrenb6d4c282014-01-27 14:17:08 -05004633 shortcutInfo.restore();
Winson Chung8c903312013-10-16 17:51:49 -07004634 shortcutInfo.updateIcon(mIconCache);
4635 shortcutInfo.title = appInfo.title.toString();
4636 shortcut.applyFromShortcutInfo(shortcutInfo, mIconCache);
4637 }
4638 }
4639 }
4640
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004641 void updateShortcuts(ArrayList<AppInfo> apps) {
Winson Chung8c903312013-10-16 17:51:49 -07004642 // Create a map of the apps to test against
4643 final HashMap<ComponentName, AppInfo> appsMap = new HashMap<ComponentName, AppInfo>();
4644 for (AppInfo ai : apps) {
4645 appsMap.put(ai.componentName, ai);
4646 }
4647
Michael Jurkaa52570f2012-03-20 03:18:20 -07004648 ArrayList<ShortcutAndWidgetContainer> childrenLayouts = getAllShortcutAndWidgetContainers();
4649 for (ShortcutAndWidgetContainer layout: childrenLayouts) {
Winson Chung8c903312013-10-16 17:51:49 -07004650 // Update all the children shortcuts
4651 final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>();
4652 for (int j = 0; j < layout.getChildCount(); j++) {
4653 View v = layout.getChildAt(j);
4654 ItemInfo info = (ItemInfo) v.getTag();
4655 if (info instanceof FolderInfo && v instanceof FolderIcon) {
4656 FolderIcon folder = (FolderIcon) v;
4657 ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder();
4658 for (View fv : folderChildren) {
4659 info = (ItemInfo) fv.getTag();
4660 updateShortcut(appsMap, info, fv);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004661 }
Winson Chung8c903312013-10-16 17:51:49 -07004662 folder.invalidate();
4663 } else if (info instanceof ShortcutInfo) {
4664 updateShortcut(appsMap, info, v);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004665 }
4666 }
4667 }
4668 }
4669
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004670 private void moveToScreen(int page, boolean animate) {
Winson Chungde1af762011-07-21 16:44:07 -07004671 if (!isSmall()) {
4672 if (animate) {
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004673 snapToPage(page);
Winson Chungde1af762011-07-21 16:44:07 -07004674 } else {
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004675 setCurrentPage(page);
Winson Chungde1af762011-07-21 16:44:07 -07004676 }
Joe Onoratoc45b1682010-01-11 18:48:40 -05004677 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004678 View child = getChildAt(page);
Winson Chung64359a52013-07-08 17:17:08 -07004679 if (child != null) {
4680 child.requestFocus();
4681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004682 }
4683
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004684 void moveToDefaultScreen(boolean animate) {
4685 moveToScreen(mDefaultPage, animate);
4686 }
4687
4688 void moveToCustomContentScreen(boolean animate) {
4689 if (hasCustomContent()) {
4690 int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID);
4691 if (animate) {
4692 snapToPage(ccIndex);
4693 } else {
4694 setCurrentPage(ccIndex);
4695 }
4696 View child = getChildAt(ccIndex);
4697 if (child != null) {
4698 child.requestFocus();
4699 }
4700 }
Adam Cohen76128b62013-11-14 16:43:59 +00004701 exitWidgetResizeMode();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01004702 }
4703
Michael Jurka0142d492010-08-25 17:46:15 -07004704 @Override
Winson Chung7819a562013-09-19 15:55:45 -07004705 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
4706 long screenId = getScreenIdForPageIndex(pageIndex);
4707 if (screenId == EXTRA_EMPTY_SCREEN_ID) {
Michael Jurkafe0ace32013-10-03 01:05:14 -07004708 int count = mScreenOrder.size() - numCustomPages();
Winson Chung7819a562013-09-19 15:55:45 -07004709 if (count > 1) {
Winson Chungba4e52f2013-10-01 17:22:13 -07004710 return new PageIndicator.PageMarkerResources(R.drawable.ic_pageindicator_current,
Winson Chung7819a562013-09-19 15:55:45 -07004711 R.drawable.ic_pageindicator_add);
4712 }
Winson Chung82dfe582013-07-26 15:07:49 -07004713 }
Winson Chung7819a562013-09-19 15:55:45 -07004714
Winson Chung82dfe582013-07-26 15:07:49 -07004715 return super.getPageIndicatorMarker(pageIndex);
4716 }
4717
4718 @Override
Michael Jurka0142d492010-08-25 17:46:15 -07004719 public void syncPages() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004720 }
Michael Jurka0142d492010-08-25 17:46:15 -07004721
4722 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07004723 public void syncPageItems(int page, boolean immediate) {
Michael Jurka0142d492010-08-25 17:46:15 -07004724 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07004725
Adam Cohen53805212013-10-01 10:39:23 -07004726 protected String getPageIndicatorDescription() {
4727 String settings = getResources().getString(R.string.settings_button_text);
4728 return getCurrentPageDescription() + ", " + settings;
4729 }
4730
Winson Chung6a0f57d2011-06-29 20:10:49 -07004731 protected String getCurrentPageDescription() {
4732 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
Adam Cohen53805212013-10-01 10:39:23 -07004733 int delta = numCustomPages();
4734 if (hasCustomContent() && getNextPage() == 0) {
4735 return mCustomContentDescription;
4736 }
Michael Jurka8b805b12012-04-18 14:23:14 -07004737 return String.format(getContext().getString(R.string.workspace_scroll_format),
Adam Cohen53805212013-10-01 10:39:23 -07004738 page + 1 - delta, getChildCount() - delta);
Winson Chung6a0f57d2011-06-29 20:10:49 -07004739 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07004740
4741 public void getLocationInDragLayer(int[] loc) {
4742 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
4743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004744}