The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 20 | import android.animation.Animator.AnimatorListener; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorListenerAdapter; |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 22 | import android.animation.AnimatorSet; |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 23 | import android.animation.LayoutTransition; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 24 | import android.animation.ObjectAnimator; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 25 | import android.animation.PropertyValuesHolder; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 26 | import android.animation.TimeInterpolator; |
| 27 | import android.animation.ValueAnimator; |
| 28 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 29 | import android.app.WallpaperManager; |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 30 | import android.appwidget.AppWidgetHostView; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 31 | import android.appwidget.AppWidgetProviderInfo; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 32 | import android.content.ComponentName; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Michael Jurka | adc574c | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 34 | import android.content.SharedPreferences; |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 35 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 37 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.graphics.Canvas; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 39 | import android.graphics.Matrix; |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 40 | import android.graphics.Paint; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 41 | import android.graphics.Point; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 42 | import android.graphics.PointF; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.graphics.Rect; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 44 | import android.graphics.Region.Op; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 45 | import android.graphics.drawable.Drawable; |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 46 | import android.os.AsyncTask; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 47 | import android.os.Handler; |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 49 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | import android.util.AttributeSet; |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 51 | import android.util.Log; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 52 | import android.util.SparseArray; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 53 | import android.view.Choreographer; |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 54 | import android.view.Display; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | import android.view.View; |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 57 | import android.view.ViewGroup; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 58 | import android.view.accessibility.AccessibilityManager; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 59 | import android.view.animation.DecelerateInterpolator; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 60 | import android.view.animation.Interpolator; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 61 | import android.widget.TextView; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 62 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 63 | import com.android.launcher3.FolderIcon.FolderRingAnimator; |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 64 | import com.android.launcher3.Launcher.CustomContentCallbacks; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 65 | import com.android.launcher3.Launcher.LauncherOverlay; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 66 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 67 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 68 | import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 69 | import com.android.launcher3.compat.UserHandleCompat; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 70 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 71 | import java.util.ArrayList; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 72 | import java.util.HashMap; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 73 | import java.util.HashSet; |
Michael Jurka | 8bd65f5 | 2012-06-25 14:56:22 -0700 | [diff] [blame] | 74 | import java.util.Iterator; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 75 | import java.util.concurrent.atomic.AtomicInteger; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | /** |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 78 | * The workspace is a wide area with a wallpaper and a finite number of pages. |
| 79 | * Each page contains a number of icons, folders or widgets the user can |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 80 | * interact with. A workspace is meant to be used with a fixed width only. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 81 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 82 | public class Workspace extends SmoothPagedView |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 83 | implements DropTarget, DragSource, DragScroller, View.OnTouchListener, |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 84 | DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener, |
| 85 | Insettable { |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 86 | private static final String TAG = "Launcher.Workspace"; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 87 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 88 | private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0; |
| 89 | private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375; |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 90 | private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 91 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 92 | protected static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400; |
| 93 | protected static final int FADE_EMPTY_SCREEN_DURATION = 150; |
| 94 | |
Winson Chung | f135c6c | 2010-11-18 16:32:08 -0800 | [diff] [blame] | 95 | private static final int BACKGROUND_FADE_OUT_DURATION = 350; |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 96 | private static final int ADJACENT_SCREEN_DROP_DURATION = 300; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 97 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 98 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 99 | private static final float ALPHA_CUTOFF_THRESHOLD = 0.01f; |
| 100 | |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 101 | static final boolean MAP_NO_RECURSE = false; |
| 102 | static final boolean MAP_RECURSE = true; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 103 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 104 | // These animators are used to fade the children's outlines |
| 105 | private ObjectAnimator mChildrenOutlineFadeInAnimation; |
| 106 | private ObjectAnimator mChildrenOutlineFadeOutAnimation; |
| 107 | private float mChildrenOutlineAlpha = 0; |
| 108 | |
| 109 | // These properties refer to the background protection gradient used for AllApps and Customize |
Michael Jurka | e0f5a61 | 2011-02-07 16:45:41 -0800 | [diff] [blame] | 110 | private ValueAnimator mBackgroundFadeInAnimation; |
| 111 | private ValueAnimator mBackgroundFadeOutAnimation; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 112 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 113 | private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200; |
| 114 | private long mTouchDownTime = -1; |
| 115 | private long mCustomContentShowTime = -1; |
| 116 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 117 | private LayoutTransition mLayoutTransition; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 118 | private final WallpaperManager mWallpaperManager; |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 119 | private IBinder mWindowToken; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 120 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 121 | private int mOriginalDefaultPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 122 | private int mDefaultPage; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 124 | private ShortcutAndWidgetContainer mDragSourceInternal; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 125 | private static boolean sAccessibilityEnabled; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 126 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 127 | // The screen id used for the empty screen always present to the right. |
Adrian Roos | 8f3f683 | 2014-04-28 15:45:52 +0200 | [diff] [blame] | 128 | final static long EXTRA_EMPTY_SCREEN_ID = -201; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 129 | private final static long CUSTOM_CONTENT_SCREEN_ID = -301; |
| 130 | |
| 131 | private HashMap<Long, CellLayout> mWorkspaceScreens = new HashMap<Long, CellLayout>(); |
| 132 | private ArrayList<Long> mScreenOrder = new ArrayList<Long>(); |
| 133 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 134 | private Runnable mRemoveEmptyScreenRunnable; |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 135 | private boolean mDeferRemoveExtraEmptyScreen = false; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 136 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | /** |
| 138 | * CellInfo for the cell that is currently being dragged |
| 139 | */ |
| 140 | private CellLayout.CellInfo mDragInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 141 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Target drop area calculated during last acceptDrop call. |
| 144 | */ |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 145 | private int[] mTargetCell = new int[2]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 146 | private int mDragOverX = -1; |
| 147 | private int mDragOverY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 149 | static Rect mLandscapeCellLayoutMetrics = null; |
| 150 | static Rect mPortraitCellLayoutMetrics = null; |
| 151 | |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 152 | CustomContentCallbacks mCustomContentCallbacks; |
| 153 | boolean mCustomContentShowing; |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 154 | private float mLastCustomContentScrollProgress = -1f; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 155 | private String mCustomContentDescription = ""; |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 156 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 157 | /** |
| 158 | * The CellLayout that is currently being dragged over |
| 159 | */ |
| 160 | private CellLayout mDragTargetLayout = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 161 | /** |
| 162 | * The CellLayout that we will show as glowing |
| 163 | */ |
| 164 | private CellLayout mDragOverlappingLayout = null; |
| 165 | |
| 166 | /** |
| 167 | * The CellLayout which will be dropped to |
| 168 | */ |
| 169 | private CellLayout mDropToLayout = null; |
| 170 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 171 | private Launcher mLauncher; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 172 | private IconCache mIconCache; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 173 | private DragController mDragController; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 174 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 175 | // These are temporary variables to prevent having to allocate a new object just to |
| 176 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | private int[] mTempCell = new int[2]; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 178 | private int[] mTempPt = new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 179 | private int[] mTempEstimate = new int[2]; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 180 | private float[] mDragViewVisualCenter = new float[2]; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 181 | private float[] mTempCellLayoutCenterCoordinates = new float[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 182 | private Matrix mTempInverseMatrix = new Matrix(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 183 | |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 184 | private SpringLoadedDragController mSpringLoadedDragController; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 185 | private float mSpringLoadedShrinkFactor; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 186 | private float mOverviewModeShrinkFactor; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 187 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 188 | // State variable that indicates whether the pages are small (ie when you're |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 189 | // in all apps or customize mode) |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 190 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 191 | enum State { NORMAL, NORMAL_HIDDEN, SPRING_LOADED, OVERVIEW, OVERVIEW_HIDDEN}; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 192 | private State mState = State.NORMAL; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 193 | private boolean mIsSwitchingState = false; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 194 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 195 | boolean mAnimatingViewIntoPlace = false; |
| 196 | boolean mIsDragOccuring = false; |
| 197 | boolean mChildrenLayersEnabled = true; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 198 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 199 | private boolean mStripScreensOnPageStopMoving = false; |
| 200 | |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 201 | /** Is the user is dragging an item near the edge of a page? */ |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 202 | private boolean mInScrollArea = false; |
| 203 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 204 | private HolographicOutlineHelper mOutlineHelper; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 205 | private Bitmap mDragOutline = null; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 206 | private static final Rect sTempRect = new Rect(); |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 207 | private final int[] mTempXY = new int[2]; |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 208 | private int[] mTempVisiblePagesRange = new int[2]; |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 209 | private boolean mOverscrollEffectSet; |
Michael Jurka | f8304f0 | 2012-04-26 13:33:26 -0700 | [diff] [blame] | 210 | public static final int DRAG_BITMAP_PADDING = 2; |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 211 | private boolean mWorkspaceFadeInAdjacentScreens; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 212 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 213 | WallpaperOffsetInterpolator mWallpaperOffset; |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 214 | private boolean mWallpaperIsLiveWallpaper; |
| 215 | private int mNumPagesForWallpaperParallax; |
| 216 | private float mLastSetWallpaperOffsetSteps = 0; |
| 217 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 218 | private Runnable mDelayedResizeRunnable; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 219 | private Runnable mDelayedSnapToPageRunnable; |
Michael Jurka | 84f2ce7 | 2012-04-13 15:08:01 -0700 | [diff] [blame] | 220 | private Point mDisplaySize = new Point(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 221 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 222 | // Variables relating to the creation of user folders by hovering shortcuts over shortcuts |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 223 | private static final int FOLDER_CREATION_TIMEOUT = 0; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 224 | public static final int REORDER_TIMEOUT = 350; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 225 | private final Alarm mFolderCreationAlarm = new Alarm(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 226 | private final Alarm mReorderAlarm = new Alarm(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 227 | private FolderRingAnimator mDragFolderRingAnimator = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 228 | private FolderIcon mDragOverFolderIcon = null; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 229 | private boolean mCreateUserFolderOnDrop = false; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 230 | private boolean mAddToExistingFolderOnDrop = false; |
| 231 | private DropTarget.DragEnforcer mDragEnforcer; |
Adam Cohen | 3aff81c | 2012-05-16 21:01:01 -0700 | [diff] [blame] | 232 | private float mMaxDistanceForFolderCreation; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 233 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 234 | private final Canvas mCanvas = new Canvas(); |
| 235 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 236 | // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) |
| 237 | private float mXDown; |
| 238 | private float mYDown; |
| 239 | final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; |
| 240 | final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; |
| 241 | final static float TOUCH_SLOP_DAMPING_FACTOR = 4; |
| 242 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 243 | // Relating to the animation of items being dropped externally |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 244 | public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0; |
| 245 | public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1; |
| 246 | public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2; |
| 247 | public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3; |
| 248 | public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 249 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 250 | // Related to dragging, folder creation and reordering |
| 251 | private static final int DRAG_MODE_NONE = 0; |
| 252 | private static final int DRAG_MODE_CREATE_FOLDER = 1; |
| 253 | private static final int DRAG_MODE_ADD_TO_FOLDER = 2; |
| 254 | private static final int DRAG_MODE_REORDER = 3; |
| 255 | private int mDragMode = DRAG_MODE_NONE; |
| 256 | private int mLastReorderX = -1; |
| 257 | private int mLastReorderY = -1; |
| 258 | |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 259 | private SparseArray<Parcelable> mSavedStates; |
| 260 | private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>(); |
| 261 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 262 | // These variables are used for storing the initial and final values during workspace animations |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 263 | private int mSavedScrollX; |
| 264 | private float mSavedRotationY; |
| 265 | private float mSavedTranslationX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 266 | |
| 267 | private float mCurrentScale; |
| 268 | private float mNewScale; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 269 | private float[] mOldBackgroundAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 270 | private float[] mOldAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 271 | private float[] mNewBackgroundAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 272 | private float[] mNewAlphas; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 273 | private int mLastChildCount = -1; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 274 | private float mTransitionProgress; |
Adam Cohen | 08072c0 | 2014-11-18 17:53:44 -0800 | [diff] [blame] | 275 | private Animator mStateAnimator = null; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 276 | |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 277 | float mOverScrollEffect = 0f; |
| 278 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 279 | private Runnable mDeferredAction; |
| 280 | private boolean mDeferDropAfterUninstall; |
| 281 | private boolean mUninstallSuccessful; |
| 282 | |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 283 | // State related to Launcher Overlay |
| 284 | LauncherOverlay mLauncherOverlay; |
| 285 | boolean mScrollInteractionBegan; |
| 286 | boolean mStartedSendingScrollEvents; |
| 287 | boolean mShouldSendPageSettled; |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 288 | int mLastOverlaySroll = 0; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 289 | |
Romain Guy | eeacd56 | 2012-10-10 18:47:33 -0700 | [diff] [blame] | 290 | private final Runnable mBindPages = new Runnable() { |
| 291 | @Override |
| 292 | public void run() { |
| 293 | mLauncher.getModel().bindRemainingSynchronousPages(); |
| 294 | } |
| 295 | }; |
| 296 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 297 | /** |
| 298 | * Used to inflate the Workspace from XML. |
| 299 | * |
| 300 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 301 | * @param attrs The attributes set containing the Workspace's customization values. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 302 | */ |
| 303 | public Workspace(Context context, AttributeSet attrs) { |
| 304 | this(context, attrs, 0); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Used to inflate the Workspace from XML. |
| 309 | * |
| 310 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 311 | * @param attrs The attributes set containing the Workspace's customization values. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 312 | * @param defStyle Unused. |
| 313 | */ |
| 314 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 315 | super(context, attrs, defStyle); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 316 | mContentIsRefreshable = false; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 317 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 318 | mOutlineHelper = HolographicOutlineHelper.obtain(context); |
| 319 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 320 | mDragEnforcer = new DropTarget.DragEnforcer(context); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 321 | // With workspace, data is available straight from the get-go |
| 322 | setDataIsReady(); |
| 323 | |
Michael Jurka | 8bc66c7 | 2012-06-21 08:36:45 -0700 | [diff] [blame] | 324 | mLauncher = (Launcher) context; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 325 | final Resources res = getResources(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 326 | mWorkspaceFadeInAdjacentScreens = LauncherAppState.getInstance().getDynamicGrid(). |
| 327 | getDeviceProfile().shouldFadeAdjacentWorkspaceScreens(); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 328 | mFadeInAdjacentScreens = false; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 329 | mWallpaperManager = WallpaperManager.getInstance(context); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 330 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 331 | LauncherAppState app = LauncherAppState.getInstance(); |
| 332 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 333 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 334 | R.styleable.Workspace, defStyle, 0); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 335 | mSpringLoadedShrinkFactor = |
| 336 | res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 337 | mOverviewModeShrinkFactor = grid.getOverviewModeScale(); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 338 | mOriginalDefaultPage = mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 339 | a.recycle(); |
| 340 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 341 | setOnHierarchyChangeListener(this); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 342 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 343 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 344 | initWorkspace(); |
Winson Chung | c35afb2 | 2011-02-23 13:01:49 -0800 | [diff] [blame] | 345 | |
| 346 | // Disable multitouch across the workspace/all apps/customize tray |
| 347 | setMotionEventSplittingEnabled(true); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 348 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 349 | } |
| 350 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 351 | @Override |
| 352 | public void setInsets(Rect insets) { |
| 353 | mInsets.set(insets); |
Adam Cohen | 6400b84 | 2013-11-26 15:45:38 -0800 | [diff] [blame] | 354 | |
| 355 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 356 | if (customScreen != null) { |
| 357 | View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0); |
| 358 | if (customContent instanceof Insettable) { |
| 359 | ((Insettable) customContent).setInsets(mInsets); |
| 360 | } |
| 361 | } |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 364 | // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each |
| 365 | // dimension if unsuccessful |
| 366 | public int[] estimateItemSize(int hSpan, int vSpan, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 367 | ItemInfo itemInfo, boolean springLoaded) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 368 | int[] size = new int[2]; |
| 369 | if (getChildCount() > 0) { |
Winson Chung | ad7db6e | 2013-10-08 14:01:06 -0700 | [diff] [blame] | 370 | // Use the first non-custom page to estimate the child position |
| 371 | CellLayout cl = (CellLayout) getChildAt(numCustomPages()); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 372 | Rect r = estimateItemPosition(cl, itemInfo, 0, 0, hSpan, vSpan); |
| 373 | size[0] = r.width(); |
| 374 | size[1] = r.height(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 375 | if (springLoaded) { |
| 376 | size[0] *= mSpringLoadedShrinkFactor; |
| 377 | size[1] *= mSpringLoadedShrinkFactor; |
| 378 | } |
| 379 | return size; |
| 380 | } else { |
| 381 | size[0] = Integer.MAX_VALUE; |
| 382 | size[1] = Integer.MAX_VALUE; |
| 383 | return size; |
| 384 | } |
| 385 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 386 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 387 | public Rect estimateItemPosition(CellLayout cl, ItemInfo pendingInfo, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 388 | int hCell, int vCell, int hSpan, int vSpan) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 389 | Rect r = new Rect(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 390 | cl.cellToRect(hCell, vCell, hSpan, vSpan, r); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 391 | return r; |
| 392 | } |
| 393 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 394 | public void onDragStart(final DragSource source, Object info, int dragAction) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 395 | mIsDragOccuring = true; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 396 | updateChildrenLayersEnabled(false); |
Winson Chung | 641d71d | 2012-04-26 15:58:01 -0700 | [diff] [blame] | 397 | mLauncher.lockScreenOrientation(); |
Sandeep Siddhartha | ab2d9d7 | 2013-09-17 13:18:24 -0700 | [diff] [blame] | 398 | mLauncher.onInteractionBegin(); |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 399 | setChildrenBackgroundAlphaMultipliers(1f); |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 400 | // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging |
| 401 | InstallShortcutReceiver.enableInstallQueue(); |
| 402 | UninstallShortcutReceiver.enableUninstallQueue(); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 403 | post(new Runnable() { |
| 404 | @Override |
| 405 | public void run() { |
| 406 | if (mIsDragOccuring) { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 407 | mDeferRemoveExtraEmptyScreen = false; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 408 | addExtraEmptyScreenOnDrag(); |
| 409 | } |
| 410 | } |
| 411 | }); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 414 | |
| 415 | public void deferRemoveExtraEmptyScreen() { |
| 416 | mDeferRemoveExtraEmptyScreen = true; |
| 417 | } |
| 418 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 419 | public void onDragEnd() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 420 | if (!mDeferRemoveExtraEmptyScreen) { |
| 421 | removeExtraEmptyScreen(true, mDragSourceInternal != null); |
| 422 | } |
| 423 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 424 | mIsDragOccuring = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 425 | updateChildrenLayersEnabled(false); |
Winson Chung | 4b919f8 | 2012-05-01 10:44:08 -0700 | [diff] [blame] | 426 | mLauncher.unlockScreenOrientation(false); |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 427 | |
| 428 | // Re-enable any Un/InstallShortcutReceiver and now process any queued items |
| 429 | InstallShortcutReceiver.disableAndFlushInstallQueue(getContext()); |
| 430 | UninstallShortcutReceiver.disableAndFlushUninstallQueue(getContext()); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 431 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 432 | mDragSourceInternal = null; |
Sandeep Siddhartha | ab2d9d7 | 2013-09-17 13:18:24 -0700 | [diff] [blame] | 433 | mLauncher.onInteractionEnd(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 434 | } |
| 435 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 436 | /** |
| 437 | * Initializes various states for this workspace. |
| 438 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 439 | protected void initWorkspace() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 440 | mCurrentPage = mDefaultPage; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 441 | LauncherAppState app = LauncherAppState.getInstance(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 442 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 443 | mIconCache = app.getIconCache(); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 444 | setWillNotDraw(false); |
Romain Guy | ce3cbd1 | 2013-02-25 15:00:36 -0800 | [diff] [blame] | 445 | setClipChildren(false); |
| 446 | setClipToPadding(false); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 447 | setChildrenDrawnWithCacheEnabled(true); |
Derek Prothro | dadd984 | 2014-01-17 13:43:50 -0500 | [diff] [blame] | 448 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 449 | setMinScale(mOverviewModeShrinkFactor); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 450 | setupLayoutTransition(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 451 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 452 | mWallpaperOffset = new WallpaperOffsetInterpolator(); |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 453 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
Michael Jurka | 84f2ce7 | 2012-04-13 15:08:01 -0700 | [diff] [blame] | 454 | display.getSize(mDisplaySize); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 455 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 456 | mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 457 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 458 | |
| 459 | // Set the wallpaper dimensions when Launcher starts up |
Adam Cohen | 824fcb3 | 2014-05-21 23:01:25 +0000 | [diff] [blame] | 460 | setWallpaperDimension(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 463 | private void setupLayoutTransition() { |
| 464 | // We want to show layout transitions when pages are deleted, to close the gap. |
| 465 | mLayoutTransition = new LayoutTransition(); |
| 466 | mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING); |
| 467 | mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 468 | mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING); |
| 469 | mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 470 | setLayoutTransition(mLayoutTransition); |
| 471 | } |
| 472 | |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 473 | void enableLayoutTransitions() { |
| 474 | setLayoutTransition(mLayoutTransition); |
| 475 | } |
| 476 | void disableLayoutTransitions() { |
| 477 | setLayoutTransition(null); |
| 478 | } |
| 479 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 480 | @Override |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 481 | protected int getScrollMode() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 482 | return SmoothPagedView.X_LARGE_MODE; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 485 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 486 | public void onChildViewAdded(View parent, View child) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 487 | if (!(child instanceof CellLayout)) { |
| 488 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 489 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 490 | CellLayout cl = ((CellLayout) child); |
| 491 | cl.setOnInterceptTouchListener(this); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 492 | cl.setClickable(true); |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 493 | cl.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 494 | super.onChildViewAdded(parent, child); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 497 | protected boolean shouldDrawChild(View child) { |
| 498 | final CellLayout cl = (CellLayout) child; |
| 499 | return super.shouldDrawChild(child) && |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 500 | (mIsSwitchingState || |
| 501 | cl.getShortcutsAndWidgets().getAlpha() > 0 || |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 502 | cl.getBackgroundAlpha() > 0); |
| 503 | } |
| 504 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 505 | /** |
| 506 | * @return The open folder on the current screen, or null if there is none |
| 507 | */ |
| 508 | Folder getOpenFolder() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 509 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 510 | int count = dragLayer.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 511 | for (int i = 0; i < count; i++) { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 512 | View child = dragLayer.getChildAt(i); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 513 | if (child instanceof Folder) { |
| 514 | Folder folder = (Folder) child; |
| 515 | if (folder.getInfo().opened) |
| 516 | return folder; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | return null; |
| 520 | } |
| 521 | |
Patrick Dubroy | a0aa012 | 2011-02-24 11:42:23 -0800 | [diff] [blame] | 522 | boolean isTouchActive() { |
| 523 | return mTouchState != TOUCH_STATE_REST; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 526 | public void removeAllWorkspaceScreens() { |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 527 | // Disable all layout transitions before removing all pages to ensure that we don't get the |
| 528 | // transition animations competing with us changing the scroll when we add pages or the |
| 529 | // custom content screen |
| 530 | disableLayoutTransitions(); |
| 531 | |
| 532 | // Since we increment the current page when we call addCustomContentPage via bindScreens |
| 533 | // (and other places), we need to adjust the current page back when we clear the pages |
| 534 | if (hasCustomContent()) { |
| 535 | removeCustomContentPage(); |
| 536 | } |
| 537 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 538 | // Remove the pages and clear the screen models |
| 539 | removeAllViews(); |
| 540 | mScreenOrder.clear(); |
| 541 | mWorkspaceScreens.clear(); |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 542 | |
| 543 | // Re-enable the layout transitions |
| 544 | enableLayoutTransitions(); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 547 | public long insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 548 | // Find the index to insert this view into. If the empty screen exists, then |
| 549 | // insert it before that. |
| 550 | int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 551 | if (insertIndex < 0) { |
| 552 | insertIndex = mScreenOrder.size(); |
| 553 | } |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 554 | return insertNewWorkspaceScreen(screenId, insertIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 557 | public long insertNewWorkspaceScreen(long screenId) { |
| 558 | return insertNewWorkspaceScreen(screenId, getChildCount()); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 561 | public long insertNewWorkspaceScreen(long screenId, int insertIndex) { |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 562 | if (mWorkspaceScreens.containsKey(screenId)) { |
| 563 | throw new RuntimeException("Screen id " + screenId + " already exists!"); |
| 564 | } |
| 565 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 566 | CellLayout newScreen = (CellLayout) |
| 567 | mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null); |
| 568 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 569 | newScreen.setOnLongClickListener(mLongClickListener); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 570 | newScreen.setOnClickListener(mLauncher); |
Winson Chung | 837b241 | 2013-09-09 14:43:51 -0700 | [diff] [blame] | 571 | newScreen.setSoundEffectsEnabled(false); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 572 | mWorkspaceScreens.put(screenId, newScreen); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 573 | mScreenOrder.add(insertIndex, screenId); |
| 574 | addView(newScreen, insertIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 575 | return screenId; |
| 576 | } |
| 577 | |
Derek Prothro | dadd984 | 2014-01-17 13:43:50 -0500 | [diff] [blame] | 578 | public void createCustomContentContainer() { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 579 | CellLayout customScreen = (CellLayout) |
Adam Cohen | 41eb470 | 2013-06-27 15:08:59 -0700 | [diff] [blame] | 580 | mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 581 | customScreen.disableBackground(); |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 582 | customScreen.disableDragTarget(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 583 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 584 | mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen); |
| 585 | mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 586 | |
Adam Cohen | 2702ea0 | 2013-08-15 16:17:42 -0700 | [diff] [blame] | 587 | // We want no padding on the custom content |
| 588 | customScreen.setPadding(0, 0, 0, 0); |
| 589 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 590 | addFullScreenPage(customScreen); |
Michael Jurka | ee0ce2b | 2013-07-02 17:24:35 -0700 | [diff] [blame] | 591 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 592 | // Ensure that the current page and default page are maintained. |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 593 | mDefaultPage = mOriginalDefaultPage + 1; |
Winson Chung | 82e5c98 | 2013-10-09 12:04:50 -0700 | [diff] [blame] | 594 | |
| 595 | // Update the custom content hint |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 596 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
| 597 | mRestorePage = mRestorePage + 1; |
| 598 | } else { |
| 599 | setCurrentPage(getCurrentPage() + 1); |
| 600 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 603 | public void removeCustomContentPage() { |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 604 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 605 | if (customScreen == null) { |
| 606 | throw new RuntimeException("Expected custom content screen to exist"); |
| 607 | } |
| 608 | |
| 609 | mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID); |
| 610 | mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID); |
| 611 | removeView(customScreen); |
Adam Cohen | bb6fda6 | 2013-10-10 12:48:52 -0700 | [diff] [blame] | 612 | |
| 613 | if (mCustomContentCallbacks != null) { |
| 614 | mCustomContentCallbacks.onScrollProgressChanged(0); |
| 615 | mCustomContentCallbacks.onHide(); |
| 616 | } |
| 617 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 618 | mCustomContentCallbacks = null; |
| 619 | |
| 620 | // Ensure that the current page and default page are maintained. |
| 621 | mDefaultPage = mOriginalDefaultPage - 1; |
Winson Chung | 82e5c98 | 2013-10-09 12:04:50 -0700 | [diff] [blame] | 622 | |
| 623 | // Update the custom content hint |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 624 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
| 625 | mRestorePage = mRestorePage - 1; |
| 626 | } else { |
| 627 | setCurrentPage(getCurrentPage() - 1); |
| 628 | } |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 629 | } |
| 630 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 631 | public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks, |
| 632 | String description) { |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 633 | if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) { |
| 634 | throw new RuntimeException("Expected custom content screen to exist"); |
| 635 | } |
| 636 | |
| 637 | // Add the custom content to the full screen custom page |
| 638 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 639 | int spanX = customScreen.getCountX(); |
| 640 | int spanY = customScreen.getCountY(); |
| 641 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY); |
| 642 | lp.canReorder = false; |
| 643 | lp.isFullscreen = true; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 644 | if (customContent instanceof Insettable) { |
| 645 | ((Insettable)customContent).setInsets(mInsets); |
| 646 | } |
Adam Cohen | 166ebd4 | 2013-11-26 14:55:45 -0800 | [diff] [blame] | 647 | |
| 648 | // Verify that the child is removed from any existing parent. |
| 649 | if (customContent.getParent() instanceof ViewGroup) { |
| 650 | ViewGroup parent = (ViewGroup) customContent.getParent(); |
| 651 | parent.removeView(customContent); |
| 652 | } |
Adam Cohen | 225ad9b | 2013-10-07 13:03:00 -0700 | [diff] [blame] | 653 | customScreen.removeAllViews(); |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 654 | customScreen.addViewToCellLayout(customContent, 0, 0, lp, true); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 655 | mCustomContentDescription = description; |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 656 | |
| 657 | mCustomContentCallbacks = callbacks; |
| 658 | } |
| 659 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 660 | public void addExtraEmptyScreenOnDrag() { |
| 661 | boolean lastChildOnScreen = false; |
| 662 | boolean childOnFinalScreen = false; |
| 663 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 664 | // Cancel any pending removal of empty screen |
| 665 | mRemoveEmptyScreenRunnable = null; |
| 666 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 667 | if (mDragSourceInternal != null) { |
| 668 | if (mDragSourceInternal.getChildCount() == 1) { |
| 669 | lastChildOnScreen = true; |
| 670 | } |
| 671 | CellLayout cl = (CellLayout) mDragSourceInternal.getParent(); |
| 672 | if (indexOfChild(cl) == getChildCount() - 1) { |
| 673 | childOnFinalScreen = true; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | // If this is the last item on the final screen |
| 678 | if (lastChildOnScreen && childOnFinalScreen) { |
| 679 | return; |
| 680 | } |
| 681 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 682 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | public boolean addExtraEmptyScreen() { |
| 687 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 688 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 689 | return true; |
| 690 | } |
| 691 | return false; |
| 692 | } |
| 693 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 694 | private void convertFinalScreenToEmptyScreenIfNecessary() { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 695 | if (mLauncher.isWorkspaceLoading()) { |
| 696 | // Invalid and dangerous operation if workspace is loading |
| 697 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 698 | return; |
| 699 | } |
| 700 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 701 | if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return; |
| 702 | long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1); |
| 703 | |
| 704 | if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return; |
| 705 | CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId); |
| 706 | |
| 707 | // If the final screen is empty, convert it to the extra empty screen |
Adam Cohen | 917e388 | 2013-10-31 15:03:35 -0700 | [diff] [blame] | 708 | if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 && |
| 709 | !finalScreen.isDropPending()) { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 710 | mWorkspaceScreens.remove(finalScreenId); |
| 711 | mScreenOrder.remove(finalScreenId); |
| 712 | |
| 713 | // if this is the last non-custom content screen, convert it to the empty screen |
| 714 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen); |
| 715 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 716 | |
Winson Chung | f0716b7 | 2013-11-18 16:09:54 -0800 | [diff] [blame] | 717 | // Update the model if we have changed any screens |
| 718 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 722 | public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) { |
| 723 | removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 726 | public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete, |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 727 | final int delay, final boolean stripEmptyScreens) { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 728 | if (mLauncher.isWorkspaceLoading()) { |
| 729 | // Don't strip empty screens if the workspace is still loading |
| 730 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 731 | return; |
| 732 | } |
| 733 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 734 | if (delay > 0) { |
| 735 | postDelayed(new Runnable() { |
| 736 | @Override |
| 737 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 738 | removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 739 | } |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 740 | }, delay); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | convertFinalScreenToEmptyScreenIfNecessary(); |
| 745 | if (hasExtraEmptyScreen()) { |
| 746 | int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 747 | if (getNextPage() == emptyIndex) { |
| 748 | snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION); |
| 749 | fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION, |
| 750 | onComplete, stripEmptyScreens); |
| 751 | } else { |
| 752 | fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION, |
| 753 | onComplete, stripEmptyScreens); |
| 754 | } |
| 755 | return; |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 756 | } else if (stripEmptyScreens) { |
| 757 | // If we're not going to strip the empty screens after removing |
| 758 | // the extra empty screen, do it right away. |
| 759 | stripEmptyScreens(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 760 | } |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 761 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 762 | if (onComplete != null) { |
| 763 | onComplete.run(); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete, |
| 768 | final boolean stripEmptyScreens) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 769 | // XXX: Do we need to update LM workspace screens below? |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 770 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 771 | PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f); |
| 772 | |
| 773 | final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
| 774 | |
| 775 | mRemoveEmptyScreenRunnable = new Runnable() { |
| 776 | @Override |
| 777 | public void run() { |
| 778 | if (hasExtraEmptyScreen()) { |
| 779 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
| 780 | mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); |
| 781 | removeView(cl); |
| 782 | if (stripEmptyScreens) { |
| 783 | stripEmptyScreens(); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | }; |
| 788 | |
| 789 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha); |
| 790 | oa.setDuration(duration); |
| 791 | oa.setStartDelay(delay); |
| 792 | oa.addListener(new AnimatorListenerAdapter() { |
| 793 | @Override |
| 794 | public void onAnimationEnd(Animator animation) { |
| 795 | if (mRemoveEmptyScreenRunnable != null) { |
| 796 | mRemoveEmptyScreenRunnable.run(); |
| 797 | } |
| 798 | if (onComplete != null) { |
| 799 | onComplete.run(); |
| 800 | } |
| 801 | } |
| 802 | }); |
| 803 | oa.start(); |
| 804 | } |
| 805 | |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 806 | public boolean hasExtraEmptyScreen() { |
| 807 | int nScreens = getChildCount(); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 808 | nScreens = nScreens - numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 809 | return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1; |
| 810 | } |
| 811 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 812 | public long commitExtraEmptyScreen() { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 813 | if (mLauncher.isWorkspaceLoading()) { |
| 814 | // Invalid and dangerous operation if workspace is loading |
| 815 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 816 | return -1; |
| 817 | } |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 818 | |
Winson Chung | 89f9705 | 2013-09-20 11:32:26 -0700 | [diff] [blame] | 819 | int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 820 | CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 821 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 822 | mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); |
| 823 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 824 | long newId = LauncherAppState.getLauncherProvider().generateNewScreenId(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 825 | mWorkspaceScreens.put(newId, cl); |
| 826 | mScreenOrder.add(newId); |
| 827 | |
Winson Chung | 89f9705 | 2013-09-20 11:32:26 -0700 | [diff] [blame] | 828 | // Update the page indicator marker |
| 829 | if (getPageIndicator() != null) { |
| 830 | getPageIndicator().updateMarker(index, getPageIndicatorMarker(index)); |
| 831 | } |
| 832 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 833 | // Update the model for the new screen |
| 834 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
| 835 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 836 | return newId; |
| 837 | } |
| 838 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 839 | public CellLayout getScreenWithId(long screenId) { |
| 840 | CellLayout layout = mWorkspaceScreens.get(screenId); |
| 841 | return layout; |
| 842 | } |
| 843 | |
| 844 | public long getIdForScreen(CellLayout layout) { |
| 845 | Iterator<Long> iter = mWorkspaceScreens.keySet().iterator(); |
| 846 | while (iter.hasNext()) { |
| 847 | long id = iter.next(); |
| 848 | if (mWorkspaceScreens.get(id) == layout) { |
| 849 | return id; |
| 850 | } |
| 851 | } |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | public int getPageIndexForScreenId(long screenId) { |
| 856 | return indexOfChild(mWorkspaceScreens.get(screenId)); |
| 857 | } |
| 858 | |
| 859 | public long getScreenIdForPageIndex(int index) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 860 | if (0 <= index && index < mScreenOrder.size()) { |
| 861 | return mScreenOrder.get(index); |
| 862 | } |
| 863 | return -1; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 866 | ArrayList<Long> getScreenOrder() { |
| 867 | return mScreenOrder; |
| 868 | } |
| 869 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 870 | public void stripEmptyScreens() { |
Adam Cohen | 65e4303 | 2014-03-03 11:37:21 -0800 | [diff] [blame] | 871 | if (mLauncher.isWorkspaceLoading()) { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 872 | // Don't strip empty screens if the workspace is still loading. |
| 873 | // This is dangerous and can result in data loss. |
Adam Cohen | 517a7f5 | 2014-03-01 12:12:59 -0800 | [diff] [blame] | 874 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 875 | return; |
| 876 | } |
| 877 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 878 | if (isPageMoving()) { |
| 879 | mStripScreensOnPageStopMoving = true; |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | int currentPage = getNextPage(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 884 | ArrayList<Long> removeScreens = new ArrayList<Long>(); |
| 885 | for (Long id: mWorkspaceScreens.keySet()) { |
| 886 | CellLayout cl = mWorkspaceScreens.get(id); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 887 | if (id >= 0 && cl.getShortcutsAndWidgets().getChildCount() == 0) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 888 | removeScreens.add(id); |
| 889 | } |
| 890 | } |
| 891 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 892 | // We enforce at least one page to add new items to. In the case that we remove the last |
| 893 | // such screen, we convert the last screen to the empty screen |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 894 | int minScreens = 1 + numCustomPages(); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 895 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 896 | int pageShift = 0; |
| 897 | for (Long id: removeScreens) { |
| 898 | CellLayout cl = mWorkspaceScreens.get(id); |
| 899 | mWorkspaceScreens.remove(id); |
| 900 | mScreenOrder.remove(id); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 901 | |
| 902 | if (getChildCount() > minScreens) { |
| 903 | if (indexOfChild(cl) < currentPage) { |
| 904 | pageShift++; |
| 905 | } |
| 906 | removeView(cl); |
| 907 | } else { |
| 908 | // if this is the last non-custom content screen, convert it to the empty screen |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 909 | mRemoveEmptyScreenRunnable = null; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 910 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl); |
| 911 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 912 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 913 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 914 | |
| 915 | if (!removeScreens.isEmpty()) { |
| 916 | // Update the model if we have changed any screens |
| 917 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
| 918 | } |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 919 | |
| 920 | if (pageShift >= 0) { |
| 921 | setCurrentPage(currentPage - pageShift); |
| 922 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | // See implementation for parameter definition. |
| 926 | void addInScreen(View child, long container, long screenId, |
| 927 | int x, int y, int spanX, int spanY) { |
| 928 | addInScreen(child, container, screenId, x, y, spanX, spanY, false, false); |
| 929 | } |
| 930 | |
| 931 | // At bind time, we use the rank (screenId) to compute x and y for hotseat items. |
| 932 | // See implementation for parameter definition. |
| 933 | void addInScreenFromBind(View child, long container, long screenId, int x, int y, |
| 934 | int spanX, int spanY) { |
| 935 | addInScreen(child, container, screenId, x, y, spanX, spanY, false, true); |
| 936 | } |
| 937 | |
| 938 | // See implementation for parameter definition. |
| 939 | void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, |
| 940 | boolean insert) { |
| 941 | addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 942 | } |
| 943 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 944 | /** |
| 945 | * Adds the specified child in the specified screen. The position and dimension of |
| 946 | * the child are defined by x, y, spanX and spanY. |
| 947 | * |
| 948 | * @param child The child to add in one of the workspace's screens. |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 949 | * @param screenId The screen in which to add the child. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 950 | * @param x The X position of the child in the screen's grid. |
| 951 | * @param y The Y position of the child in the screen's grid. |
| 952 | * @param spanX The number of cells spanned horizontally by the child. |
| 953 | * @param spanY The number of cells spanned vertically by the child. |
| 954 | * @param insert When true, the child is inserted at the beginning of the children list. |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 955 | * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute |
| 956 | * the x and y position in which to place hotseat items. Otherwise |
| 957 | * we use the x and y position to compute the rank. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 958 | */ |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 959 | void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, |
| 960 | boolean insert, boolean computeXYFromRank) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 961 | if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 962 | if (getScreenWithId(screenId) == null) { |
| 963 | Log.e(TAG, "Skipping child, screenId " + screenId + " not found"); |
Winson Chung | 8481e32 | 2013-08-09 16:06:38 -0700 | [diff] [blame] | 964 | // DEBUGGING - Print out the stack trace to see where we are adding from |
| 965 | new Throwable().printStackTrace(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 966 | return; |
| 967 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 968 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 969 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 970 | // This should never happen |
| 971 | throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID"); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 974 | final CellLayout layout; |
| 975 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 976 | layout = mLauncher.getHotseat().getLayout(); |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 977 | child.setOnKeyListener(new HotseatIconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 978 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 979 | // Hide folder title in the hotseat |
| 980 | if (child instanceof FolderIcon) { |
| 981 | ((FolderIcon) child).setTextVisible(false); |
| 982 | } |
| 983 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 984 | if (computeXYFromRank) { |
| 985 | x = mLauncher.getHotseat().getCellXFromOrder((int) screenId); |
| 986 | y = mLauncher.getHotseat().getCellYFromOrder((int) screenId); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 987 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 988 | screenId = mLauncher.getHotseat().getOrderInHotseat(x, y); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 989 | } |
| 990 | } else { |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 991 | // Show folder title if not in the hotseat |
| 992 | if (child instanceof FolderIcon) { |
| 993 | ((FolderIcon) child).setTextVisible(true); |
| 994 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 995 | layout = getScreenWithId(screenId); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 996 | child.setOnKeyListener(new IconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 999 | ViewGroup.LayoutParams genericLp = child.getLayoutParams(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1000 | CellLayout.LayoutParams lp; |
| 1001 | if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1002 | lp = new CellLayout.LayoutParams(x, y, spanX, spanY); |
| 1003 | } else { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1004 | lp = (CellLayout.LayoutParams) genericLp; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1005 | lp.cellX = x; |
| 1006 | lp.cellY = y; |
| 1007 | lp.cellHSpan = spanX; |
| 1008 | lp.cellVSpan = spanY; |
| 1009 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1010 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1011 | if (spanX < 0 && spanY < 0) { |
| 1012 | lp.isLockedToGrid = false; |
| 1013 | } |
| 1014 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1015 | // Get the canonical child id to uniquely represent this view in this screen |
Adam Cohen | c76e1dd | 2013-11-14 11:09:14 +0000 | [diff] [blame] | 1016 | ItemInfo info = (ItemInfo) child.getTag(); |
| 1017 | int childId = mLauncher.getViewIdForItem(info); |
| 1018 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 1019 | boolean markCellsAsOccupied = !(child instanceof Folder); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1020 | if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1021 | // TODO: This branch occurs when the workspace is adding views |
| 1022 | // outside of the defined grid |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1023 | // maybe we should be deleting these items from the LauncherModel? |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 1024 | Launcher.addDumpLog(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout", true); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1027 | if (!(child instanceof Folder)) { |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1028 | child.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1029 | child.setOnLongClickListener(mLongClickListener); |
| 1030 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1031 | if (child instanceof DropTarget) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1032 | mDragController.addDropTarget((DropTarget) child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1033 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1036 | /** |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 1037 | * Called directly from a CellLayout (not by the framework), after we've been added as a |
| 1038 | * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout |
| 1039 | * that it should intercept touch events, which is not something that is normally supported. |
| 1040 | */ |
| 1041 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1042 | public boolean onTouch(View v, MotionEvent event) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1043 | return (workspaceInModalState() || !isFinishedSwitchingState()) |
| 1044 | || (!workspaceInModalState() && indexOfChild(v) != mCurrentPage); |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 1047 | public boolean isSwitchingState() { |
| 1048 | return mIsSwitchingState; |
| 1049 | } |
| 1050 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1051 | /** This differs from isSwitchingState in that we take into account how far the transition |
| 1052 | * has completed. */ |
Winson Chung | 9b0b2fe | 2012-02-24 13:03:34 -0800 | [diff] [blame] | 1053 | public boolean isFinishedSwitchingState() { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1054 | return !mIsSwitchingState || (mTransitionProgress > 0.5f); |
| 1055 | } |
| 1056 | |
Adam Cohen | ded9f8d | 2010-11-03 13:25:16 -0700 | [diff] [blame] | 1057 | protected void onWindowVisibilityChanged (int visibility) { |
| 1058 | mLauncher.onWindowVisibilityChanged(visibility); |
| 1059 | } |
| 1060 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1061 | @Override |
| 1062 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1063 | if (workspaceInModalState() || !isFinishedSwitchingState()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1064 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 1065 | return false; |
| 1066 | } |
| 1067 | return super.dispatchUnhandledMove(focused, direction); |
| 1068 | } |
| 1069 | |
| 1070 | @Override |
| 1071 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1072 | switch (ev.getAction() & MotionEvent.ACTION_MASK) { |
| 1073 | case MotionEvent.ACTION_DOWN: |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1074 | mXDown = ev.getX(); |
| 1075 | mYDown = ev.getY(); |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1076 | mTouchDownTime = System.currentTimeMillis(); |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | case MotionEvent.ACTION_POINTER_UP: |
| 1079 | case MotionEvent.ACTION_UP: |
| 1080 | if (mTouchState == TOUCH_STATE_REST) { |
| 1081 | final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); |
Adam Cohen | 83e5941 | 2014-09-17 16:46:55 -0700 | [diff] [blame] | 1082 | if (currentPage != null && !currentPage.lastDownOnOccupiedCell()) { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1083 | onWallpaperTap(ev); |
| 1084 | } |
| 1085 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1086 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1087 | return super.onInterceptTouchEvent(ev); |
| 1088 | } |
| 1089 | |
Jan-Willem Maarse | d3fbe68 | 2014-08-19 15:27:48 -0700 | [diff] [blame] | 1090 | @Override |
| 1091 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1092 | // Ignore pointer scroll events if the custom content doesn't allow scrolling. |
| 1093 | if ((getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID) |
| 1094 | && (mCustomContentCallbacks != null) |
| 1095 | && !mCustomContentCallbacks.isScrollingAllowed()) { |
| 1096 | return false; |
| 1097 | } |
| 1098 | return super.onGenericMotionEvent(event); |
| 1099 | } |
| 1100 | |
Adam Cohen | 3d50932 | 2012-06-06 14:10:04 -0700 | [diff] [blame] | 1101 | protected void reinflateWidgetsIfNecessary() { |
| 1102 | final int clCount = getChildCount(); |
| 1103 | for (int i = 0; i < clCount; i++) { |
| 1104 | CellLayout cl = (CellLayout) getChildAt(i); |
| 1105 | ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets(); |
| 1106 | final int itemCount = swc.getChildCount(); |
| 1107 | for (int j = 0; j < itemCount; j++) { |
| 1108 | View v = swc.getChildAt(j); |
| 1109 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1110 | if (v != null && v.getTag() instanceof LauncherAppWidgetInfo) { |
Adam Cohen | 3d50932 | 2012-06-06 14:10:04 -0700 | [diff] [blame] | 1111 | LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag(); |
| 1112 | LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) info.hostView; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1113 | if (lahv != null && lahv.isReinflateRequired()) { |
Adam Cohen | 3d50932 | 2012-06-06 14:10:04 -0700 | [diff] [blame] | 1114 | mLauncher.removeAppWidget(info); |
| 1115 | // Remove the current widget which is inflated with the wrong orientation |
| 1116 | cl.removeView(lahv); |
| 1117 | mLauncher.bindAppWidget(info); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 1124 | @Override |
| 1125 | protected void determineScrollingStart(MotionEvent ev) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1126 | if (!isFinishedSwitchingState()) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1127 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1128 | float deltaX = ev.getX() - mXDown; |
| 1129 | float absDeltaX = Math.abs(deltaX); |
| 1130 | float absDeltaY = Math.abs(ev.getY() - mYDown); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1131 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1132 | if (Float.compare(absDeltaX, 0f) == 0) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1133 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1134 | float slope = absDeltaY / absDeltaX; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1135 | float theta = (float) Math.atan(slope); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1136 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1137 | if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1138 | cancelCurrentPageLongPress(); |
| 1139 | } |
| 1140 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1141 | boolean passRightSwipesToCustomContent = |
| 1142 | (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY; |
| 1143 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1144 | boolean swipeInIgnoreDirection = isLayoutRtl() ? deltaX < 0 : deltaX > 0; |
Jan-Willem Maarse | 2ff91c4 | 2014-07-10 15:58:12 -0700 | [diff] [blame] | 1145 | boolean onCustomContentScreen = |
| 1146 | getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID; |
| 1147 | if (swipeInIgnoreDirection && onCustomContentScreen && passRightSwipesToCustomContent) { |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1148 | // Pass swipes to the right to the custom content page. |
| 1149 | return; |
| 1150 | } |
| 1151 | |
Jan-Willem Maarse | 2ff91c4 | 2014-07-10 15:58:12 -0700 | [diff] [blame] | 1152 | if (onCustomContentScreen && (mCustomContentCallbacks != null) |
| 1153 | && !mCustomContentCallbacks.isScrollingAllowed()) { |
| 1154 | // Don't allow workspace scrolling if the current custom content screen doesn't allow |
| 1155 | // scrolling. |
| 1156 | return; |
| 1157 | } |
| 1158 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1159 | if (theta > MAX_SWIPE_ANGLE) { |
| 1160 | // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace |
| 1161 | return; |
| 1162 | } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { |
| 1163 | // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to |
| 1164 | // increase the touch slop to make it harder to begin scrolling the workspace. This |
| 1165 | // results in vertically scrolling widgets to more easily. The higher the angle, the |
| 1166 | // more we increase touch slop. |
| 1167 | theta -= START_DAMPING_TOUCH_SLOP_ANGLE; |
| 1168 | float extraRatio = (float) |
| 1169 | Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); |
| 1170 | super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); |
| 1171 | } else { |
| 1172 | // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special |
| 1173 | super.determineScrollingStart(ev); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1174 | } |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1177 | protected void onPageBeginMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1178 | super.onPageBeginMoving(); |
| 1179 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1180 | if (isHardwareAccelerated()) { |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1181 | updateChildrenLayersEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1182 | } else { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1183 | if (mNextPage != INVALID_PAGE) { |
| 1184 | // we're snapping to a particular screen |
| 1185 | enableChildrenCache(mCurrentPage, mNextPage); |
| 1186 | } else { |
| 1187 | // this is when user is actively dragging a particular screen, they might |
| 1188 | // swipe it either left or right (but we won't advance by more than one screen) |
| 1189 | enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); |
| 1190 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1194 | protected void onPageEndMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1195 | super.onPageEndMoving(); |
| 1196 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1197 | if (isHardwareAccelerated()) { |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1198 | updateChildrenLayersEnabled(false); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | clearChildrenCache(); |
| 1201 | } |
| 1202 | |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 1203 | if (mDragController.isDragging()) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1204 | if (workspaceInModalState()) { |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 1205 | // If we are in springloaded mode, then force an event to check if the current touch |
| 1206 | // is under a new page (to scroll to) |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 1207 | mDragController.forceTouchMove(); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 1208 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1209 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 1210 | |
| 1211 | if (mDelayedResizeRunnable != null) { |
| 1212 | mDelayedResizeRunnable.run(); |
| 1213 | mDelayedResizeRunnable = null; |
| 1214 | } |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1215 | |
| 1216 | if (mDelayedSnapToPageRunnable != null) { |
| 1217 | mDelayedSnapToPageRunnable.run(); |
| 1218 | mDelayedSnapToPageRunnable = null; |
| 1219 | } |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 1220 | if (mStripScreensOnPageStopMoving) { |
| 1221 | stripEmptyScreens(); |
| 1222 | mStripScreensOnPageStopMoving = false; |
| 1223 | } |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1224 | |
| 1225 | if (mShouldSendPageSettled) { |
| 1226 | mLauncherOverlay.onScrollSettled(); |
| 1227 | mShouldSendPageSettled = false; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | protected void onScrollInteractionBegin() { |
| 1232 | super.onScrollInteractionEnd(); |
| 1233 | mScrollInteractionBegan = true; |
| 1234 | } |
| 1235 | |
| 1236 | protected void onScrollInteractionEnd() { |
| 1237 | super.onScrollInteractionEnd(); |
| 1238 | mScrollInteractionBegan = false; |
| 1239 | if (mStartedSendingScrollEvents) { |
| 1240 | mStartedSendingScrollEvents = false; |
| 1241 | mLauncherOverlay.onScrollInteractionEnd(); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | public void setLauncherOverlay(LauncherOverlay overlay) { |
| 1246 | mLauncherOverlay = overlay; |
| 1247 | } |
| 1248 | |
| 1249 | @Override |
| 1250 | protected void overScroll(float amount) { |
| 1251 | boolean isRtl = isLayoutRtl(); |
| 1252 | boolean shouldOverScroll = (amount <= 0 && (!hasCustomContent() || isRtl)) || |
| 1253 | (amount >= 0 && (!hasCustomContent() || !isRtl)); |
| 1254 | |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1255 | boolean shouldScrollOverlay = mLauncherOverlay != null && |
| 1256 | ((amount <= 0 && !isRtl) || (amount >= 0 && isRtl)); |
| 1257 | |
| 1258 | boolean shouldZeroOverlay = mLauncherOverlay != null && mLastOverlaySroll != 0 && |
| 1259 | ((amount >= 0 && !isRtl) || (amount <= 0 && isRtl)); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1260 | |
| 1261 | if (shouldScrollOverlay) { |
| 1262 | if (!mStartedSendingScrollEvents && mScrollInteractionBegan) { |
| 1263 | mStartedSendingScrollEvents = true; |
| 1264 | mLauncherOverlay.onScrollInteractionBegin(); |
| 1265 | mShouldSendPageSettled = true; |
| 1266 | } |
| 1267 | int screenSize = getViewportWidth(); |
| 1268 | float f = (amount / screenSize); |
| 1269 | |
| 1270 | int progress = (int) Math.abs((f * 100)); |
| 1271 | |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1272 | mLastOverlaySroll = progress; |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 1273 | mLauncherOverlay.onScrollChange(progress, isRtl); |
| 1274 | } else if (shouldOverScroll) { |
| 1275 | dampedOverScroll(amount); |
| 1276 | mOverScrollEffect = acceleratedOverFactor(amount); |
| 1277 | } else { |
| 1278 | mOverScrollEffect = 0; |
| 1279 | } |
Adam Cohen | 8c4ca92 | 2014-10-24 17:40:34 -0700 | [diff] [blame] | 1280 | |
| 1281 | if (shouldZeroOverlay) { |
| 1282 | mLauncherOverlay.onScrollChange(0, isRtl); |
| 1283 | } |
Adam Cohen | 89cbbb9 | 2013-09-25 16:52:48 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | @Override |
| 1287 | protected void notifyPageSwitchListener() { |
| 1288 | super.notifyPageSwitchListener(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1289 | |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 1290 | if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) { |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1291 | mCustomContentShowing = true; |
| 1292 | if (mCustomContentCallbacks != null) { |
Selim Cinek | 3a8a8f7 | 2014-01-16 10:38:38 -0800 | [diff] [blame] | 1293 | mCustomContentCallbacks.onShow(false); |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1294 | mCustomContentShowTime = System.currentTimeMillis(); |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1295 | } |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 1296 | } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) { |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1297 | mCustomContentShowing = false; |
| 1298 | if (mCustomContentCallbacks != null) { |
| 1299 | mCustomContentCallbacks.onHide(); |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1302 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1303 | |
Adam Cohen | 6fecd41 | 2013-10-02 17:41:50 -0700 | [diff] [blame] | 1304 | protected CustomContentCallbacks getCustomContentCallbacks() { |
| 1305 | return mCustomContentCallbacks; |
| 1306 | } |
| 1307 | |
Adam Cohen | 824fcb3 | 2014-05-21 23:01:25 +0000 | [diff] [blame] | 1308 | protected void setWallpaperDimension() { |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1309 | new AsyncTask<Void, Void, Void>() { |
| 1310 | public Void doInBackground(Void ... args) { |
| 1311 | String spKey = WallpaperCropActivity.getSharedPreferencesKey(); |
| 1312 | SharedPreferences sp = |
| 1313 | mLauncher.getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); |
Adam Cohen | c29217b | 2014-05-18 03:17:15 +0000 | [diff] [blame] | 1314 | LauncherWallpaperPickerActivity.suggestWallpaperDimension(mLauncher.getResources(), |
Adam Cohen | ea90f83 | 2014-05-21 15:03:34 -0700 | [diff] [blame] | 1315 | sp, mLauncher.getWindowManager(), mWallpaperManager, |
| 1316 | mLauncher.overrideWallpaperDimensions()); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1317 | return null; |
| 1318 | } |
| 1319 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1322 | protected void snapToPage(int whichPage, Runnable r) { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 1323 | snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r); |
| 1324 | } |
| 1325 | |
| 1326 | protected void snapToPage(int whichPage, int duration, Runnable r) { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1327 | if (mDelayedSnapToPageRunnable != null) { |
| 1328 | mDelayedSnapToPageRunnable.run(); |
| 1329 | } |
| 1330 | mDelayedSnapToPageRunnable = r; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 1331 | snapToPage(whichPage, duration); |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Adam Cohen | db364c3 | 2014-05-20 14:23:40 -0700 | [diff] [blame] | 1334 | public void snapToScreenId(long screenId) { |
| 1335 | snapToScreenId(screenId, null); |
| 1336 | } |
| 1337 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1338 | protected void snapToScreenId(long screenId, Runnable r) { |
| 1339 | snapToPage(getPageIndexForScreenId(screenId), r); |
| 1340 | } |
| 1341 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1342 | class WallpaperOffsetInterpolator implements Choreographer.FrameCallback { |
| 1343 | float mFinalOffset = 0.0f; |
Michael Jurka | 046cf34 | 2013-09-14 16:40:34 +0200 | [diff] [blame] | 1344 | float mCurrentOffset = 0.5f; // to force an initial update |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1345 | boolean mWaitingForUpdate; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1346 | Choreographer mChoreographer; |
| 1347 | Interpolator mInterpolator; |
| 1348 | boolean mAnimating; |
| 1349 | long mAnimationStartTime; |
| 1350 | float mAnimationStartOffset; |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1351 | private final int ANIMATION_DURATION = 250; |
| 1352 | // Don't use all the wallpaper for parallax until you have at least this many pages |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1353 | private final int MIN_PARALLAX_PAGE_SPAN = 3; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1354 | int mNumScreens; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1355 | |
| 1356 | public WallpaperOffsetInterpolator() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1357 | mChoreographer = Choreographer.getInstance(); |
| 1358 | mInterpolator = new DecelerateInterpolator(1.5f); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1361 | @Override |
| 1362 | public void doFrame(long frameTimeNanos) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1363 | updateOffset(false); |
| 1364 | } |
| 1365 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1366 | private void updateOffset(boolean force) { |
| 1367 | if (mWaitingForUpdate || force) { |
| 1368 | mWaitingForUpdate = false; |
Michael Jurka | 1bd90b0 | 2013-09-05 23:10:14 +0200 | [diff] [blame] | 1369 | if (computeScrollOffset() && mWindowToken != null) { |
| 1370 | try { |
| 1371 | mWallpaperManager.setWallpaperOffsets(mWindowToken, |
| 1372 | mWallpaperOffset.getCurrX(), 0.5f); |
| 1373 | setWallpaperOffsetSteps(); |
| 1374 | } catch (IllegalArgumentException e) { |
| 1375 | Log.e(TAG, "Error updating wallpaper offset: " + e); |
| 1376 | } |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1377 | } |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1378 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1381 | public boolean computeScrollOffset() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1382 | final float oldOffset = mCurrentOffset; |
| 1383 | if (mAnimating) { |
| 1384 | long durationSinceAnimation = System.currentTimeMillis() - mAnimationStartTime; |
| 1385 | float t0 = durationSinceAnimation / (float) ANIMATION_DURATION; |
| 1386 | float t1 = mInterpolator.getInterpolation(t0); |
| 1387 | mCurrentOffset = mAnimationStartOffset + |
| 1388 | (mFinalOffset - mAnimationStartOffset) * t1; |
| 1389 | mAnimating = durationSinceAnimation < ANIMATION_DURATION; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1390 | } else { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1391 | mCurrentOffset = mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1392 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1393 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1394 | if (Math.abs(mCurrentOffset - mFinalOffset) > 0.0000001f) { |
| 1395 | scheduleUpdate(); |
| 1396 | } |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1397 | if (Math.abs(oldOffset - mCurrentOffset) > 0.0000001f) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1398 | return true; |
| 1399 | } |
| 1400 | return false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1403 | private float wallpaperOffsetForCurrentScroll() { |
| 1404 | if (getChildCount() <= 1) { |
| 1405 | return 0; |
| 1406 | } |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1407 | |
| 1408 | // Exclude the leftmost page |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1409 | int emptyExtraPages = numEmptyScreensToIgnore(); |
| 1410 | int firstIndex = numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1411 | // Exclude the last extra empty screen (if we have > MIN_PARALLAX_PAGE_SPAN pages) |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1412 | int lastIndex = getChildCount() - 1 - emptyExtraPages; |
| 1413 | if (isLayoutRtl()) { |
| 1414 | int temp = firstIndex; |
| 1415 | firstIndex = lastIndex; |
| 1416 | lastIndex = temp; |
| 1417 | } |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1418 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1419 | int firstPageScrollX = getScrollForPage(firstIndex); |
| 1420 | int scrollRange = getScrollForPage(lastIndex) - firstPageScrollX; |
| 1421 | if (scrollRange == 0) { |
| 1422 | return 0; |
| 1423 | } else { |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1424 | // TODO: do different behavior if it's a live wallpaper? |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1425 | // Sometimes the left parameter of the pages is animated during a layout transition; |
| 1426 | // this parameter offsets it to keep the wallpaper from animating as well |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1427 | int adjustedScroll = |
Michael Jurka | 8fd3adc | 2013-10-16 13:50:24 -0700 | [diff] [blame] | 1428 | getScrollX() - firstPageScrollX - getLayoutTransitionOffsetForPage(0); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1429 | float offset = Math.min(1, adjustedScroll / (float) scrollRange); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1430 | offset = Math.max(0, offset); |
| 1431 | // Don't use up all the wallpaper parallax until you have at least |
| 1432 | // MIN_PARALLAX_PAGE_SPAN pages |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1433 | int numScrollingPages = getNumScreensExcludingEmptyAndCustom(); |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1434 | int parallaxPageSpan; |
| 1435 | if (mWallpaperIsLiveWallpaper) { |
| 1436 | parallaxPageSpan = numScrollingPages - 1; |
| 1437 | } else { |
| 1438 | parallaxPageSpan = Math.max(MIN_PARALLAX_PAGE_SPAN, numScrollingPages - 1); |
| 1439 | } |
| 1440 | mNumPagesForWallpaperParallax = parallaxPageSpan; |
| 1441 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1442 | // On RTL devices, push the wallpaper offset to the right if we don't have enough |
| 1443 | // pages (ie if numScrollingPages < MIN_PARALLAX_PAGE_SPAN) |
| 1444 | int padding = isLayoutRtl() ? parallaxPageSpan - numScrollingPages + 1 : 0; |
| 1445 | return offset * (padding + numScrollingPages - 1) / parallaxPageSpan; |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1449 | private int numEmptyScreensToIgnore() { |
| 1450 | int numScrollingPages = getChildCount() - numCustomPages(); |
| 1451 | if (numScrollingPages >= MIN_PARALLAX_PAGE_SPAN && hasExtraEmptyScreen()) { |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1452 | return 1; |
| 1453 | } else { |
| 1454 | return 0; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1458 | private int getNumScreensExcludingEmptyAndCustom() { |
| 1459 | int numScrollingPages = getChildCount() - numEmptyScreensToIgnore() - numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1460 | return numScrollingPages; |
| 1461 | } |
| 1462 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1463 | public void syncWithScroll() { |
| 1464 | float offset = wallpaperOffsetForCurrentScroll(); |
| 1465 | mWallpaperOffset.setFinalX(offset); |
| 1466 | updateOffset(true); |
| 1467 | } |
| 1468 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1469 | public float getCurrX() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1470 | return mCurrentOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | public float getFinalX() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1474 | return mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1477 | private void animateToFinal() { |
| 1478 | mAnimating = true; |
| 1479 | mAnimationStartOffset = mCurrentOffset; |
| 1480 | mAnimationStartTime = System.currentTimeMillis(); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1483 | private void setWallpaperOffsetSteps() { |
| 1484 | // Set wallpaper offset steps (1 / (number of screens - 1)) |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1485 | float xOffset = 1.0f / mNumPagesForWallpaperParallax; |
| 1486 | if (xOffset != mLastSetWallpaperOffsetSteps) { |
| 1487 | mWallpaperManager.setWallpaperOffsetSteps(xOffset, 1.0f); |
| 1488 | mLastSetWallpaperOffsetSteps = xOffset; |
| 1489 | } |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1492 | public void setFinalX(float x) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1493 | scheduleUpdate(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1494 | mFinalOffset = Math.max(0f, Math.min(x, 1.0f)); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1495 | if (getNumScreensExcludingEmptyAndCustom() != mNumScreens) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1496 | if (mNumScreens > 0) { |
| 1497 | // Don't animate if we're going from 0 screens |
| 1498 | animateToFinal(); |
| 1499 | } |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1500 | mNumScreens = getNumScreensExcludingEmptyAndCustom(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1501 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1504 | private void scheduleUpdate() { |
| 1505 | if (!mWaitingForUpdate) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1506 | mChoreographer.postFrameCallback(this); |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1507 | mWaitingForUpdate = true; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1508 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | public void jumpToFinal() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1512 | mCurrentOffset = mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1513 | } |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 1514 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1515 | |
Michael Jurka | 340c5f3 | 2010-10-21 16:49:19 -0700 | [diff] [blame] | 1516 | @Override |
| 1517 | public void computeScroll() { |
| 1518 | super.computeScroll(); |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1519 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 1522 | @Override |
| 1523 | public void announceForAccessibility(CharSequence text) { |
| 1524 | // Don't announce if apps is on top of us. |
| 1525 | if (!mLauncher.isAllAppsVisible()) { |
| 1526 | super.announceForAccessibility(text); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1530 | void showOutlines() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1531 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1532 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
| 1533 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1534 | mChildrenOutlineFadeInAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 1.0f); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1535 | mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION); |
| 1536 | mChildrenOutlineFadeInAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1537 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1540 | void hideOutlines() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1541 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1542 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
| 1543 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1544 | mChildrenOutlineFadeOutAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 0.0f); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1545 | mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION); |
| 1546 | mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY); |
| 1547 | mChildrenOutlineFadeOutAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1548 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1551 | public void showOutlinesTemporarily() { |
| 1552 | if (!mIsPageMoving && !isTouchActive()) { |
| 1553 | snapToPage(mCurrentPage); |
| 1554 | } |
| 1555 | } |
| 1556 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1557 | public void setChildrenOutlineAlpha(float alpha) { |
| 1558 | mChildrenOutlineAlpha = alpha; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1559 | for (int i = 0; i < getChildCount(); i++) { |
| 1560 | CellLayout cl = (CellLayout) getChildAt(i); |
| 1561 | cl.setBackgroundAlpha(alpha); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1565 | public float getChildrenOutlineAlpha() { |
| 1566 | return mChildrenOutlineAlpha; |
| 1567 | } |
| 1568 | |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1569 | private void animateBackgroundGradient(float finalAlpha, boolean animated) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1570 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
| 1571 | |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1572 | if (mBackgroundFadeInAnimation != null) { |
| 1573 | mBackgroundFadeInAnimation.cancel(); |
| 1574 | mBackgroundFadeInAnimation = null; |
| 1575 | } |
| 1576 | if (mBackgroundFadeOutAnimation != null) { |
| 1577 | mBackgroundFadeOutAnimation.cancel(); |
| 1578 | mBackgroundFadeOutAnimation = null; |
| 1579 | } |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1580 | float startAlpha = dragLayer.getBackgroundAlpha(); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1581 | if (finalAlpha != startAlpha) { |
| 1582 | if (animated) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 1583 | mBackgroundFadeOutAnimation = |
| 1584 | LauncherAnimUtils.ofFloat(this, startAlpha, finalAlpha); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1585 | mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() { |
| 1586 | public void onAnimationUpdate(ValueAnimator animation) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1587 | dragLayer.setBackgroundAlpha( |
| 1588 | ((Float)animation.getAnimatedValue()).floatValue()); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1589 | } |
| 1590 | }); |
| 1591 | mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 1592 | mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION); |
| 1593 | mBackgroundFadeOutAnimation.start(); |
| 1594 | } else { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1595 | dragLayer.setBackgroundAlpha(finalAlpha); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1596 | } |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1597 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1600 | float backgroundAlphaInterpolator(float r) { |
| 1601 | float pivotA = 0.1f; |
| 1602 | float pivotB = 0.4f; |
| 1603 | if (r < pivotA) { |
| 1604 | return 0; |
| 1605 | } else if (r > pivotB) { |
| 1606 | return 1.0f; |
| 1607 | } else { |
| 1608 | return (r - pivotA)/(pivotB - pivotA); |
| 1609 | } |
| 1610 | } |
| 1611 | |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1612 | private void updatePageAlphaValues(int screenCenter) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1613 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 1614 | if (mWorkspaceFadeInAdjacentScreens && |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 1615 | !workspaceInModalState() && |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1616 | !mIsSwitchingState && |
| 1617 | !isInOverscroll) { |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 1618 | for (int i = numCustomPages(); i < getChildCount(); i++) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1619 | CellLayout child = (CellLayout) getChildAt(i); |
| 1620 | if (child != null) { |
| 1621 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1622 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1623 | child.getShortcutsAndWidgets().setAlpha(alpha); |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 1624 | //child.setBackgroundAlphaMultiplier(1 - alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1625 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1626 | } |
| 1627 | } |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | private void setChildrenBackgroundAlphaMultipliers(float a) { |
| 1631 | for (int i = 0; i < getChildCount(); i++) { |
| 1632 | CellLayout child = (CellLayout) getChildAt(i); |
| 1633 | child.setBackgroundAlphaMultiplier(a); |
| 1634 | } |
| 1635 | } |
| 1636 | |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 1637 | public boolean hasCustomContent() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1638 | return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID); |
| 1639 | } |
| 1640 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1641 | public int numCustomPages() { |
| 1642 | return hasCustomContent() ? 1 : 0; |
| 1643 | } |
| 1644 | |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1645 | public boolean isOnOrMovingToCustomContent() { |
| 1646 | return hasCustomContent() && getNextPage() == 0; |
| 1647 | } |
| 1648 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1649 | private void updateStateForCustomContent(int screenCenter) { |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1650 | float translationX = 0; |
| 1651 | float progress = 0; |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 1652 | if (hasCustomContent()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1653 | int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID); |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1654 | |
Adam Cohen | a45de07 | 2013-10-11 16:07:47 -0700 | [diff] [blame] | 1655 | int scrollDelta = getScrollX() - getScrollForPage(index) - |
| 1656 | getLayoutTransitionOffsetForPage(index); |
| 1657 | float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index); |
| 1658 | translationX = scrollRange - scrollDelta; |
| 1659 | progress = (scrollRange - scrollDelta) / scrollRange; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1660 | |
| 1661 | if (isLayoutRtl()) { |
| 1662 | translationX = Math.min(0, translationX); |
| 1663 | } else { |
| 1664 | translationX = Math.max(0, translationX); |
| 1665 | } |
Adam Cohen | 955806d | 2013-07-19 16:36:43 -0700 | [diff] [blame] | 1666 | progress = Math.max(0, progress); |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1667 | } |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1668 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1669 | if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return; |
Adam Cohen | 84add1d | 2013-10-14 16:29:02 -0700 | [diff] [blame] | 1670 | |
| 1671 | CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1672 | if (progress > 0 && cc.getVisibility() != VISIBLE && !workspaceInModalState()) { |
Adam Cohen | 84add1d | 2013-10-14 16:29:02 -0700 | [diff] [blame] | 1673 | cc.setVisibility(VISIBLE); |
| 1674 | } |
| 1675 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1676 | mLastCustomContentScrollProgress = progress; |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1677 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1678 | mLauncher.getDragLayer().setBackgroundAlpha(progress * 0.8f); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 1679 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1680 | if (mLauncher.getHotseat() != null) { |
| 1681 | mLauncher.getHotseat().setTranslationX(translationX); |
| 1682 | } |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1683 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1684 | if (getPageIndicator() != null) { |
| 1685 | getPageIndicator().setTranslationX(translationX); |
| 1686 | } |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1687 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1688 | if (mCustomContentCallbacks != null) { |
| 1689 | mCustomContentCallbacks.onScrollProgressChanged(progress); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1693 | @Override |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1694 | protected OnClickListener getPageIndicatorClickListener() { |
| 1695 | AccessibilityManager am = (AccessibilityManager) |
| 1696 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 1697 | if (!am.isTouchExplorationEnabled()) { |
| 1698 | return null; |
| 1699 | } |
| 1700 | OnClickListener listener = new OnClickListener() { |
| 1701 | @Override |
| 1702 | public void onClick(View arg0) { |
| 1703 | enterOverviewMode(); |
| 1704 | } |
| 1705 | }; |
| 1706 | return listener; |
| 1707 | } |
| 1708 | |
| 1709 | @Override |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1710 | protected void screenScrolled(int screenCenter) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1711 | final boolean isRtl = isLayoutRtl(); |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1712 | super.screenScrolled(screenCenter); |
| 1713 | |
| 1714 | updatePageAlphaValues(screenCenter); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1715 | updateStateForCustomContent(screenCenter); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1716 | enableHwLayersOnVisiblePages(); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1717 | |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 1718 | boolean shouldOverScroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1719 | |
| 1720 | if (shouldOverScroll) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1721 | int index = 0; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1722 | final int lowerIndex = 0; |
| 1723 | final int upperIndex = getChildCount() - 1; |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 1724 | |
| 1725 | final boolean isLeftPage = mOverScrollX < 0; |
| 1726 | index = (!isRtl && isLeftPage) || (isRtl && !isLeftPage) ? lowerIndex : upperIndex; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1727 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1728 | CellLayout cl = (CellLayout) getChildAt(index); |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 1729 | float effect = Math.abs(mOverScrollEffect); |
| 1730 | cl.setOverScrollAmount(Math.abs(effect), isLeftPage); |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 1731 | |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 1732 | mOverscrollEffectSet = true; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1733 | } else { |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 1734 | if (mOverscrollEffectSet && getChildCount() > 0) { |
| 1735 | mOverscrollEffectSet = false; |
| 1736 | ((CellLayout) getChildAt(0)).setOverScrollAmount(0, false); |
| 1737 | ((CellLayout) getChildAt(getChildCount() - 1)).setOverScrollAmount(0, false); |
Adam Cohen | 7842d7f | 2011-09-12 15:28:15 -0700 | [diff] [blame] | 1738 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1742 | protected void onAttachedToWindow() { |
| 1743 | super.onAttachedToWindow(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1744 | mWindowToken = getWindowToken(); |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 1745 | computeScroll(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1746 | mDragController.setWindowToken(mWindowToken); |
| 1747 | } |
| 1748 | |
| 1749 | protected void onDetachedFromWindow() { |
Romain Guy | e666195 | 2013-08-08 19:13:22 -0700 | [diff] [blame] | 1750 | super.onDetachedFromWindow(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1751 | mWindowToken = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1752 | } |
| 1753 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1754 | protected void onResume() { |
| 1755 | if (getPageIndicator() != null) { |
| 1756 | // In case accessibility state has changed, we need to perform this on every |
| 1757 | // attach to window |
Adam Cohen | d36d947 | 2013-10-10 15:32:41 -0700 | [diff] [blame] | 1758 | OnClickListener listener = getPageIndicatorClickListener(); |
| 1759 | if (listener != null) { |
| 1760 | getPageIndicator().setOnClickListener(listener); |
| 1761 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1762 | } |
| 1763 | AccessibilityManager am = (AccessibilityManager) |
| 1764 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 1765 | sAccessibilityEnabled = am.isEnabled(); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1766 | |
| 1767 | // Update wallpaper dimensions if they were changed since last onResume |
| 1768 | // (we also always set the wallpaper dimensions in the constructor) |
| 1769 | if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) { |
Adam Cohen | 824fcb3 | 2014-05-21 23:01:25 +0000 | [diff] [blame] | 1770 | setWallpaperDimension(); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1771 | } |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1772 | mWallpaperIsLiveWallpaper = mWallpaperManager.getWallpaperInfo() != null; |
| 1773 | // Force the wallpaper offset steps to be set again, because another app might have changed |
| 1774 | // them |
| 1775 | mLastSetWallpaperOffsetSteps = 0f; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1778 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1779 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1780 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1781 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1782 | mWallpaperOffset.jumpToFinal(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1783 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1784 | super.onLayout(changed, left, top, right, bottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | @Override |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1788 | protected void onDraw(Canvas canvas) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1789 | super.onDraw(canvas); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1790 | |
| 1791 | // Call back to LauncherModel to finish binding after the first draw |
Romain Guy | eeacd56 | 2012-10-10 18:47:33 -0700 | [diff] [blame] | 1792 | post(mBindPages); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
Michael Jurka | dfab7f0 | 2011-11-18 13:01:04 -0800 | [diff] [blame] | 1795 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1796 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1797 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1798 | final Folder openFolder = getOpenFolder(); |
| 1799 | if (openFolder != null) { |
| 1800 | return openFolder.requestFocus(direction, previouslyFocusedRect); |
| 1801 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1802 | return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | return false; |
| 1806 | } |
| 1807 | |
| 1808 | @Override |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1809 | public int getDescendantFocusability() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1810 | if (workspaceInModalState()) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1811 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 1812 | } |
| 1813 | return super.getDescendantFocusability(); |
| 1814 | } |
| 1815 | |
| 1816 | @Override |
Romain Guy | c2e24c0 | 2009-06-01 16:11:41 -0700 | [diff] [blame] | 1817 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1818 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1819 | final Folder openFolder = getOpenFolder(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1820 | if (openFolder != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1821 | openFolder.addFocusables(views, direction); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1822 | } else { |
| 1823 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1828 | public boolean workspaceInModalState() { |
| 1829 | return mState != State.NORMAL; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1832 | void enableChildrenCache(int fromPage, int toPage) { |
| 1833 | if (fromPage > toPage) { |
| 1834 | final int temp = fromPage; |
| 1835 | fromPage = toPage; |
| 1836 | toPage = temp; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 1837 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1838 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1839 | final int screenCount = getChildCount(); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1840 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1841 | fromPage = Math.max(fromPage, 0); |
| 1842 | toPage = Math.min(toPage, screenCount - 1); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1843 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1844 | for (int i = fromPage; i <= toPage; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1845 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1846 | layout.setChildrenDrawnWithCacheEnabled(true); |
| 1847 | layout.setChildrenDrawingCacheEnabled(true); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | void clearChildrenCache() { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1852 | final int screenCount = getChildCount(); |
| 1853 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1854 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1855 | layout.setChildrenDrawnWithCacheEnabled(false); |
Adam Cohen | 8182e5b | 2011-08-01 15:43:31 -0700 | [diff] [blame] | 1856 | // In software mode, we don't want the items to continue to be drawn into bitmaps |
| 1857 | if (!isHardwareAccelerated()) { |
| 1858 | layout.setChildrenDrawingCacheEnabled(false); |
| 1859 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1863 | private void updateChildrenLayersEnabled(boolean force) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1864 | boolean small = mState == State.OVERVIEW || mIsSwitchingState; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1865 | boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1866 | |
| 1867 | if (enableChildrenLayers != mChildrenLayersEnabled) { |
| 1868 | mChildrenLayersEnabled = enableChildrenLayers; |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1869 | if (mChildrenLayersEnabled) { |
| 1870 | enableHwLayersOnVisiblePages(); |
| 1871 | } else { |
| 1872 | for (int i = 0; i < getPageCount(); i++) { |
| 1873 | final CellLayout cl = (CellLayout) getChildAt(i); |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1874 | cl.enableHardwareLayer(false); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | private void enableHwLayersOnVisiblePages() { |
| 1881 | if (mChildrenLayersEnabled) { |
| 1882 | final int screenCount = getChildCount(); |
| 1883 | getVisiblePages(mTempVisiblePagesRange); |
| 1884 | int leftScreen = mTempVisiblePagesRange[0]; |
| 1885 | int rightScreen = mTempVisiblePagesRange[1]; |
| 1886 | if (leftScreen == rightScreen) { |
| 1887 | // make sure we're caching at least two pages always |
| 1888 | if (rightScreen < screenCount - 1) { |
| 1889 | rightScreen++; |
| 1890 | } else if (leftScreen > 0) { |
| 1891 | leftScreen--; |
| 1892 | } |
| 1893 | } |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1894 | |
| 1895 | final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1896 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1897 | final CellLayout layout = (CellLayout) getPageAt(i); |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1898 | |
| 1899 | // enable layers between left and right screen inclusive, except for the |
| 1900 | // customScreen, which may animate its content during transitions. |
| 1901 | boolean enableLayer = layout != customScreen && |
| 1902 | leftScreen <= i && i <= rightScreen && shouldDrawChild(layout); |
| 1903 | layout.enableHardwareLayer(enableLayer); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1904 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1905 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1906 | } |
| 1907 | |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1908 | public void buildPageHardwareLayers() { |
| 1909 | // force layers to be enabled just for the call to buildLayer |
| 1910 | updateChildrenLayersEnabled(true); |
| 1911 | if (getWindowToken() != null) { |
| 1912 | final int childCount = getChildCount(); |
| 1913 | for (int i = 0; i < childCount; i++) { |
| 1914 | CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1915 | cl.buildHardwareLayer(); |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | updateChildrenLayersEnabled(false); |
| 1919 | } |
| 1920 | |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1921 | protected void onWallpaperTap(MotionEvent ev) { |
| 1922 | final int[] position = mTempCell; |
| 1923 | getLocationOnScreen(position); |
| 1924 | |
| 1925 | int pointerIndex = ev.getActionIndex(); |
| 1926 | position[0] += (int) ev.getX(pointerIndex); |
| 1927 | position[1] += (int) ev.getY(pointerIndex); |
| 1928 | |
| 1929 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 1930 | ev.getAction() == MotionEvent.ACTION_UP |
| 1931 | ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, |
| 1932 | position[0], position[1], 0, null); |
| 1933 | } |
| 1934 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1935 | /* |
| 1936 | * This interpolator emulates the rate at which the perceived scale of an object changes |
| 1937 | * as its distance from a camera increases. When this interpolator is applied to a scale |
| 1938 | * animation on a view, it evokes the sense that the object is shrinking due to moving away |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 1939 | * from the camera. |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1940 | */ |
| 1941 | static class ZInterpolator implements TimeInterpolator { |
| 1942 | private float focalLength; |
| 1943 | |
| 1944 | public ZInterpolator(float foc) { |
| 1945 | focalLength = foc; |
| 1946 | } |
| 1947 | |
Adam Cohen | eed565d | 2010-11-15 11:30:05 -0800 | [diff] [blame] | 1948 | public float getInterpolation(float input) { |
| 1949 | return (1.0f - focalLength / (focalLength + input)) / |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1950 | (1.0f - focalLength / (focalLength + 1.0f)); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1954 | /* |
| 1955 | * The exact reverse of ZInterpolator. |
| 1956 | */ |
| 1957 | static class InverseZInterpolator implements TimeInterpolator { |
| 1958 | private ZInterpolator zInterpolator; |
| 1959 | public InverseZInterpolator(float foc) { |
| 1960 | zInterpolator = new ZInterpolator(foc); |
| 1961 | } |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1962 | public float getInterpolation(float input) { |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1963 | return 1 - zInterpolator.getInterpolation(1 - input); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1967 | /* |
| 1968 | * ZInterpolator compounded with an ease-out. |
| 1969 | */ |
| 1970 | static class ZoomOutInterpolator implements TimeInterpolator { |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1971 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f); |
| 1972 | private final ZInterpolator zInterpolator = new ZInterpolator(0.13f); |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1973 | |
| 1974 | public float getInterpolation(float input) { |
| 1975 | return decelerate.getInterpolation(zInterpolator.getInterpolation(input)); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | /* |
| 1980 | * InvereZInterpolator compounded with an ease-out. |
| 1981 | */ |
| 1982 | static class ZoomInInterpolator implements TimeInterpolator { |
| 1983 | private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f); |
| 1984 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f); |
| 1985 | |
| 1986 | public float getInterpolation(float input) { |
| 1987 | return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input)); |
| 1988 | } |
| 1989 | } |
| 1990 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1991 | private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1992 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1993 | /* |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1994 | * |
| 1995 | * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we |
| 1996 | * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace |
| 1997 | * |
| 1998 | * These methods mark the appropriate pages as accepting drops (which alters their visual |
| 1999 | * appearance). |
| 2000 | * |
| 2001 | */ |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2002 | private static Rect getDrawableBounds(Drawable d) { |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2003 | Rect bounds = new Rect(); |
| 2004 | d.copyBounds(bounds); |
| 2005 | if (bounds.width() == 0 || bounds.height() == 0) { |
| 2006 | bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2007 | } else { |
| 2008 | bounds.offsetTo(0, 0); |
| 2009 | } |
| 2010 | if (d instanceof PreloadIconDrawable) { |
| 2011 | int inset = -((PreloadIconDrawable) d).getOutset(); |
| 2012 | bounds.inset(inset, inset); |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2013 | } |
| 2014 | return bounds; |
| 2015 | } |
| 2016 | |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2017 | public void onExternalDragStartedWithItem(View v) { |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2018 | // Compose a drag bitmap with the view scaled to the icon size |
| 2019 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2020 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2021 | int iconSize = grid.iconSizePx; |
| 2022 | int bmpWidth = v.getMeasuredWidth(); |
| 2023 | int bmpHeight = v.getMeasuredHeight(); |
| 2024 | |
| 2025 | // If this is a text view, use its drawable instead |
| 2026 | if (v instanceof TextView) { |
| 2027 | TextView tv = (TextView) v; |
| 2028 | Drawable d = tv.getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2029 | Rect bounds = getDrawableBounds(d); |
| 2030 | bmpWidth = bounds.width(); |
| 2031 | bmpHeight = bounds.height(); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | // Compose the bitmap to create the icon from |
| 2035 | Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight, |
| 2036 | Bitmap.Config.ARGB_8888); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2037 | mCanvas.setBitmap(b); |
| 2038 | drawDragView(v, mCanvas, 0); |
| 2039 | mCanvas.setBitmap(null); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2040 | |
| 2041 | // The outline is used to visualize where the item will land if dropped |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2042 | mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, iconSize, iconSize, true); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2045 | public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 2046 | int[] size = estimateItemSize(info.spanX, info.spanY, info, false); |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2047 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2048 | // The outline is used to visualize where the item will land if dropped |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2049 | mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, size[0], size[1], clipAlpha); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 2052 | public void exitWidgetResizeMode() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2053 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 2054 | dragLayer.clearAllResizeFrames(); |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2057 | private void initAnimationArrays() { |
| 2058 | final int childCount = getChildCount(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2059 | if (mLastChildCount == childCount) return; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2060 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2061 | mOldBackgroundAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2062 | mOldAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2063 | mNewBackgroundAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2064 | mNewAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2067 | Animator getChangeStateAnimation(final State state, boolean animated, |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2068 | HashMap<View, Integer> layerViews) { |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2069 | return getChangeStateAnimation(state, animated, 0, -1, layerViews); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2072 | @Override |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2073 | protected void getFreeScrollPageRange(int[] range) { |
| 2074 | getOverviewModePages(range); |
| 2075 | } |
| 2076 | |
| 2077 | private void getOverviewModePages(int[] range) { |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 2078 | int start = numCustomPages(); |
Adam Cohen | 1003be9 | 2013-09-16 14:09:28 -0700 | [diff] [blame] | 2079 | int end = getChildCount() - 1; |
| 2080 | |
| 2081 | range[0] = Math.max(0, Math.min(start, getChildCount() - 1)); |
| 2082 | range[1] = Math.max(0, end); |
Adam Cohen | 6f127a6 | 2014-06-12 14:54:41 -0700 | [diff] [blame] | 2083 | } |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2084 | |
| 2085 | protected void onStartReordering() { |
| 2086 | super.onStartReordering(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2087 | showOutlines(); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 2088 | // Reordering handles its own animations, disable the automatic ones. |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 2089 | disableLayoutTransitions(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | protected void onEndReordering() { |
| 2093 | super.onEndReordering(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2094 | |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 2095 | if (mLauncher.isWorkspaceLoading()) { |
| 2096 | // Invalid and dangerous operation if workspace is loading |
| 2097 | return; |
| 2098 | } |
| 2099 | |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2100 | hideOutlines(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2101 | mScreenOrder.clear(); |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2102 | int count = getChildCount(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2103 | for (int i = 0; i < count; i++) { |
| 2104 | CellLayout cl = ((CellLayout) getChildAt(i)); |
| 2105 | mScreenOrder.add(getIdForScreen(cl)); |
| 2106 | } |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 2107 | |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2108 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 2109 | |
| 2110 | // Re-enable auto layout transitions for page deletion. |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 2111 | enableLayoutTransitions(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2114 | public boolean isInOverviewMode() { |
| 2115 | return mState == State.OVERVIEW; |
| 2116 | } |
| 2117 | |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2118 | public boolean enterOverviewMode() { |
| 2119 | if (mTouchState != TOUCH_STATE_REST) { |
| 2120 | return false; |
| 2121 | } |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2122 | enableOverviewMode(true, -1, true); |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2123 | return true; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2126 | public void exitOverviewMode(boolean animated) { |
| 2127 | exitOverviewMode(-1, animated); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2130 | public void exitOverviewMode(int snapPage, boolean animated) { |
| 2131 | enableOverviewMode(false, snapPage, animated); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2134 | private void enableOverviewMode(boolean enable, int snapPage, boolean animated) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2135 | State finalState = Workspace.State.OVERVIEW; |
| 2136 | if (!enable) { |
| 2137 | finalState = Workspace.State.NORMAL; |
| 2138 | } |
| 2139 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2140 | Animator workspaceAnim = getChangeStateAnimation(finalState, animated, 0, snapPage); |
| 2141 | if (workspaceAnim != null) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2142 | onTransitionPrepare(); |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2143 | workspaceAnim.addListener(new AnimatorListenerAdapter() { |
| 2144 | @Override |
| 2145 | public void onAnimationEnd(Animator arg0) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2146 | onTransitionEnd(); |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2147 | } |
| 2148 | }); |
| 2149 | workspaceAnim.start(); |
| 2150 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2153 | int getOverviewModeTranslationY() { |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2154 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2155 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2156 | Rect overviewBar = grid.getOverviewModeButtonBarRect(); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2157 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2158 | int availableHeight = getViewportHeight(); |
| 2159 | int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight()); |
| 2160 | int offsetFromTopEdge = (availableHeight - scaledHeight) / 2; |
| 2161 | int offsetToCenterInOverview = (availableHeight - mInsets.top - overviewBar.height() |
| 2162 | - scaledHeight) / 2; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2163 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2164 | return -offsetFromTopEdge + mInsets.top + offsetToCenterInOverview; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Adam Cohen | edaaa30 | 2013-10-01 17:33:27 -0700 | [diff] [blame] | 2167 | public void updateInteractionForState() { |
| 2168 | if (mState != State.NORMAL) { |
| 2169 | mLauncher.onInteractionBegin(); |
| 2170 | } else { |
| 2171 | mLauncher.onInteractionEnd(); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | private void setState(State state) { |
| 2176 | mState = state; |
| 2177 | updateInteractionForState(); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2178 | updateAccessibilityFlags(); |
| 2179 | } |
| 2180 | |
Adam Cohen | e25c5d1 | 2014-06-18 10:34:58 -0700 | [diff] [blame] | 2181 | State getState() { |
| 2182 | return mState; |
| 2183 | } |
| 2184 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2185 | private void updateAccessibilityFlags() { |
| 2186 | int accessible = mState == State.NORMAL ? |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 2187 | IMPORTANT_FOR_ACCESSIBILITY_YES : |
| 2188 | IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2189 | setImportantForAccessibility(accessible); |
Adam Cohen | edaaa30 | 2013-10-01 17:33:27 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2192 | private static final int HIDE_WORKSPACE_DURATION = 100; |
| 2193 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2194 | Animator getChangeStateAnimation(final State state, boolean animated, int delay, int snapPage) { |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2195 | return getChangeStateAnimation(state, animated, delay, snapPage, null); |
| 2196 | } |
| 2197 | |
| 2198 | Animator getChangeStateAnimation(final State state, boolean animated, int delay, int snapPage, |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2199 | HashMap<View, Integer> layerViews) { |
Michael Jurka | bdf7855 | 2011-10-31 14:34:25 -0700 | [diff] [blame] | 2200 | if (mState == state) { |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2201 | return null; |
Michael Jurka | 99633da | 2011-07-27 22:40:17 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2204 | // Initialize animation arrays for the first time if necessary |
| 2205 | initAnimationArrays(); |
| 2206 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 2207 | AnimatorSet anim = animated ? LauncherAnimUtils.createAnimatorSet() : null; |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 2208 | |
Adam Cohen | 08072c0 | 2014-11-18 17:53:44 -0800 | [diff] [blame] | 2209 | // We only want a single instance of a workspace animation to be running at once, so |
| 2210 | // we cancel any incomplete transition. |
| 2211 | if (mStateAnimator != null) { |
| 2212 | mStateAnimator.cancel(); |
| 2213 | } |
| 2214 | mStateAnimator = anim; |
| 2215 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2216 | final State oldState = mState; |
| 2217 | final boolean oldStateIsNormal = (oldState == State.NORMAL); |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 2218 | final boolean oldStateIsSpringLoaded = (oldState == State.SPRING_LOADED); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2219 | final boolean oldStateIsNormalHidden = (oldState == State.NORMAL_HIDDEN); |
| 2220 | final boolean oldStateIsOverviewHidden = (oldState == State.OVERVIEW_HIDDEN); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2221 | final boolean oldStateIsOverview = (oldState == State.OVERVIEW); |
Adam Cohen | edaaa30 | 2013-10-01 17:33:27 -0700 | [diff] [blame] | 2222 | setState(state); |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2223 | final boolean stateIsNormal = (state == State.NORMAL); |
| 2224 | final boolean stateIsSpringLoaded = (state == State.SPRING_LOADED); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2225 | final boolean stateIsNormalHidden = (state == State.NORMAL_HIDDEN); |
| 2226 | final boolean stateIsOverviewHidden = (state == State.OVERVIEW_HIDDEN); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2227 | final boolean stateIsOverview = (state == State.OVERVIEW); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2228 | float finalBackgroundAlpha = (stateIsSpringLoaded || stateIsOverview) ? 1.0f : 0f; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2229 | float finalHotseatAndPageIndicatorAlpha = (stateIsNormal || stateIsSpringLoaded) ? 1f : 0f; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2230 | float finalOverviewPanelAlpha = stateIsOverview ? 1f : 0f; |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2231 | float finalSearchBarAlpha = !stateIsNormal ? 0f : 1f; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2232 | float finalWorkspaceTranslationY = stateIsOverview || stateIsOverviewHidden ? |
| 2233 | getOverviewModeTranslationY() : 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2234 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2235 | boolean workspaceToAllApps = (oldStateIsNormal && stateIsNormalHidden); |
| 2236 | boolean overviewToAllApps = (oldStateIsOverview && stateIsOverviewHidden); |
| 2237 | boolean allAppsToWorkspace = (stateIsNormalHidden && stateIsNormal); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2238 | boolean workspaceToOverview = (oldStateIsNormal && stateIsOverview); |
| 2239 | boolean overviewToWorkspace = (oldStateIsOverview && stateIsNormal); |
| 2240 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2241 | mNewScale = 1.0f; |
Adam Cohen | beff8c6 | 2011-08-31 17:46:01 -0700 | [diff] [blame] | 2242 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2243 | if (oldStateIsOverview) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2244 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2245 | } else if (stateIsOverview) { |
| 2246 | enableFreeScroll(); |
| 2247 | } |
| 2248 | |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2249 | if (state != State.NORMAL) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2250 | if (stateIsSpringLoaded) { |
| 2251 | mNewScale = mSpringLoadedShrinkFactor; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2252 | } else if (stateIsOverview || stateIsOverviewHidden) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2253 | mNewScale = mOverviewModeShrinkFactor; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2254 | } |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2255 | } |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2256 | |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2257 | final int duration; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2258 | if (workspaceToAllApps || overviewToAllApps) { |
| 2259 | duration = HIDE_WORKSPACE_DURATION; //getResources().getInteger(R.integer.config_workspaceUnshrinkTime); |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2260 | } else if (workspaceToOverview || overviewToWorkspace) { |
| 2261 | duration = getResources().getInteger(R.integer.config_overviewTransitionTime); |
| 2262 | } else { |
| 2263 | duration = getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime); |
| 2264 | } |
| 2265 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2266 | if (snapPage == -1) { |
| 2267 | snapPage = getPageNearestToCenterOfScreen(); |
| 2268 | } |
| 2269 | snapToPage(snapPage, duration, mZoomInInterpolator); |
| 2270 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2271 | for (int i = 0; i < getChildCount(); i++) { |
| 2272 | final CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2273 | boolean isCurrentPage = (i == snapPage); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2274 | float initialAlpha = cl.getShortcutsAndWidgets().getAlpha(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2275 | float finalAlpha; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2276 | if (stateIsNormalHidden || stateIsOverviewHidden) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2277 | finalAlpha = 0f; |
| 2278 | } else if (stateIsNormal && mWorkspaceFadeInAdjacentScreens) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2279 | finalAlpha = (i == snapPage || i < numCustomPages()) ? 1f : 0f; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2280 | } else { |
| 2281 | finalAlpha = 1f; |
| 2282 | } |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2283 | |
| 2284 | // If we are animating to/from the small state, then hide the side pages and fade the |
| 2285 | // current page in |
| 2286 | if (!mIsSwitchingState) { |
| 2287 | if (workspaceToAllApps || allAppsToWorkspace) { |
| 2288 | if (allAppsToWorkspace && isCurrentPage) { |
| 2289 | initialAlpha = 0f; |
| 2290 | } else if (!isCurrentPage) { |
| 2291 | initialAlpha = finalAlpha = 0f; |
| 2292 | } |
| 2293 | cl.setShortcutAndWidgetAlpha(initialAlpha); |
| 2294 | } |
| 2295 | } |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2296 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2297 | mOldAlphas[i] = initialAlpha; |
| 2298 | mNewAlphas[i] = finalAlpha; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2299 | if (animated) { |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2300 | mOldBackgroundAlphas[i] = cl.getBackgroundAlpha(); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2301 | mNewBackgroundAlphas[i] = finalBackgroundAlpha; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2302 | } else { |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2303 | cl.setBackgroundAlpha(finalBackgroundAlpha); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2304 | cl.setShortcutAndWidgetAlpha(finalAlpha); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2305 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 2306 | } |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 2307 | |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2308 | final View searchBar = mLauncher.getQsbBar(); |
| 2309 | final View overviewPanel = mLauncher.getOverviewPanel(); |
| 2310 | final View hotseat = mLauncher.getHotseat(); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2311 | final View pageIndicator = getPageIndicator(); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2312 | if (animated) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2313 | LauncherViewPropertyAnimator scale = new LauncherViewPropertyAnimator(this); |
| 2314 | scale.scaleX(mNewScale) |
| 2315 | .scaleY(mNewScale) |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2316 | .translationY(finalWorkspaceTranslationY) |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2317 | .setDuration(duration) |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2318 | .setInterpolator(mZoomInInterpolator); |
| 2319 | anim.play(scale); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2320 | for (int index = 0; index < getChildCount(); index++) { |
| 2321 | final int i = index; |
| 2322 | final CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2323 | float currentAlpha = cl.getShortcutsAndWidgets().getAlpha(); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2324 | if (mOldAlphas[i] == 0 && mNewAlphas[i] == 0) { |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 2325 | cl.setBackgroundAlpha(mNewBackgroundAlphas[i]); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2326 | cl.setShortcutAndWidgetAlpha(mNewAlphas[i]); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2327 | } else { |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2328 | if (layerViews != null) { |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2329 | layerViews.put(cl, Launcher.BUILD_LAYER); |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2330 | } |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2331 | if (mOldAlphas[i] != mNewAlphas[i] || currentAlpha != mNewAlphas[i]) { |
Michael Jurka | 6c8a579 | 2012-03-22 05:24:37 -0700 | [diff] [blame] | 2332 | LauncherViewPropertyAnimator alphaAnim = |
| 2333 | new LauncherViewPropertyAnimator(cl.getShortcutsAndWidgets()); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2334 | alphaAnim.alpha(mNewAlphas[i]) |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2335 | .setDuration(duration) |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2336 | .setInterpolator(mZoomInInterpolator); |
| 2337 | anim.play(alphaAnim); |
| 2338 | } |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2339 | if (mOldBackgroundAlphas[i] != 0 || |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2340 | mNewBackgroundAlphas[i] != 0) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 2341 | ValueAnimator bgAnim = |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2342 | LauncherAnimUtils.ofFloat(cl, 0f, 1f); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2343 | bgAnim.setInterpolator(mZoomInInterpolator); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2344 | bgAnim.setDuration(duration); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2345 | bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() { |
| 2346 | public void onAnimationUpdate(float a, float b) { |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 2347 | cl.setBackgroundAlpha( |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2348 | a * mOldBackgroundAlphas[i] + |
| 2349 | b * mNewBackgroundAlphas[i]); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2350 | } |
| 2351 | }); |
| 2352 | anim.play(bgAnim); |
| 2353 | } |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2354 | } |
| 2355 | } |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2356 | Animator pageIndicatorAlpha = null; |
| 2357 | if (pageIndicator != null) { |
| 2358 | pageIndicatorAlpha = new LauncherViewPropertyAnimator(pageIndicator) |
| 2359 | .alpha(finalHotseatAndPageIndicatorAlpha).withLayer(); |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2360 | pageIndicatorAlpha.addListener(new AlphaUpdateListener(pageIndicator)); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2361 | } else { |
| 2362 | // create a dummy animation so we don't need to do null checks later |
| 2363 | pageIndicatorAlpha = ValueAnimator.ofFloat(0, 0); |
Winson Chung | 6670073 | 2013-08-20 16:56:15 -0700 | [diff] [blame] | 2364 | } |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2365 | |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2366 | LauncherViewPropertyAnimator hotseatAlpha = new LauncherViewPropertyAnimator(hotseat) |
| 2367 | .alpha(finalHotseatAndPageIndicatorAlpha); |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2368 | hotseatAlpha.addListener(new AlphaUpdateListener(hotseat)); |
| 2369 | |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2370 | LauncherViewPropertyAnimator overviewPanelAlpha = |
| 2371 | new LauncherViewPropertyAnimator(overviewPanel).alpha(finalOverviewPanelAlpha); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2372 | overviewPanelAlpha.addListener(new AlphaUpdateListener(overviewPanel)); |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2373 | |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2374 | // For animation optimations, we may need to provide the Launcher transition |
| 2375 | // with a set of views on which to force build layers in certain scenarios. |
| 2376 | hotseat.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2377 | overviewPanel.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 2378 | if (layerViews != null) { |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2379 | // If layerViews is not null, we add these views, and indicate that |
| 2380 | // the caller can manage layer state. |
| 2381 | layerViews.put(hotseat, Launcher.BUILD_AND_SET_LAYER); |
| 2382 | layerViews.put(overviewPanel, Launcher.BUILD_AND_SET_LAYER); |
| 2383 | } else { |
| 2384 | // Otherwise let the animator handle layer management. |
| 2385 | hotseatAlpha.withLayer(); |
| 2386 | overviewPanelAlpha.withLayer(); |
Adam Cohen | 2854d25 | 2014-08-27 16:04:07 -0700 | [diff] [blame] | 2387 | } |
| 2388 | |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2389 | if (workspaceToOverview) { |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2390 | pageIndicatorAlpha.setInterpolator(new DecelerateInterpolator(2)); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2391 | hotseatAlpha.setInterpolator(new DecelerateInterpolator(2)); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2392 | overviewPanelAlpha.setInterpolator(null); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2393 | } else if (overviewToWorkspace) { |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2394 | pageIndicatorAlpha.setInterpolator(null); |
| 2395 | hotseatAlpha.setInterpolator(null); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2396 | overviewPanelAlpha.setInterpolator(new DecelerateInterpolator(2)); |
| 2397 | } |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2398 | |
| 2399 | overviewPanelAlpha.setDuration(duration); |
| 2400 | pageIndicatorAlpha.setDuration(duration); |
| 2401 | hotseatAlpha.setDuration(duration); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 2402 | |
| 2403 | if (searchBar != null) { |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2404 | LauncherViewPropertyAnimator searchBarAlpha = new LauncherViewPropertyAnimator(searchBar) |
| 2405 | .alpha(finalSearchBarAlpha); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 2406 | searchBarAlpha.addListener(new AlphaUpdateListener(searchBar)); |
| 2407 | searchBar.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 2408 | if (layerViews != null) { |
Adam Cohen | 80e6beb | 2015-02-13 16:14:33 -0800 | [diff] [blame] | 2409 | // If layerViews is not null, we add these views, and indicate that |
| 2410 | // the caller can manage layer state. |
| 2411 | layerViews.put(searchBar, Launcher.BUILD_AND_SET_LAYER); |
| 2412 | } else { |
| 2413 | // Otherwise let the animator handle layer management. |
| 2414 | searchBarAlpha.withLayer(); |
Adam Cohen | c2d6e89 | 2014-10-16 09:49:24 -0700 | [diff] [blame] | 2415 | } |
| 2416 | searchBarAlpha.setDuration(duration); |
| 2417 | anim.play(searchBarAlpha); |
| 2418 | } |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2419 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2420 | anim.play(overviewPanelAlpha); |
| 2421 | anim.play(hotseatAlpha); |
| 2422 | anim.play(pageIndicatorAlpha); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2423 | anim.setStartDelay(delay); |
Adam Cohen | 08072c0 | 2014-11-18 17:53:44 -0800 | [diff] [blame] | 2424 | anim.addListener(new AnimatorListenerAdapter() { |
| 2425 | @Override |
| 2426 | public void onAnimationEnd(Animator animation) { |
| 2427 | mStateAnimator = null; |
| 2428 | } |
| 2429 | }); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2430 | } else { |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2431 | overviewPanel.setAlpha(finalOverviewPanelAlpha); |
| 2432 | AlphaUpdateListener.updateVisibility(overviewPanel); |
| 2433 | hotseat.setAlpha(finalHotseatAndPageIndicatorAlpha); |
| 2434 | AlphaUpdateListener.updateVisibility(hotseat); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2435 | if (pageIndicator != null) { |
| 2436 | pageIndicator.setAlpha(finalHotseatAndPageIndicatorAlpha); |
| 2437 | AlphaUpdateListener.updateVisibility(pageIndicator); |
Winson Chung | 6670073 | 2013-08-20 16:56:15 -0700 | [diff] [blame] | 2438 | } |
Sunny Goyal | 416541c | 2014-11-14 11:59:57 -0800 | [diff] [blame] | 2439 | if (searchBar != null) { |
| 2440 | searchBar.setAlpha(finalSearchBarAlpha); |
| 2441 | AlphaUpdateListener.updateVisibility(searchBar); |
| 2442 | } |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2443 | updateCustomContentVisibility(); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2444 | setScaleX(mNewScale); |
| 2445 | setScaleY(mNewScale); |
| 2446 | setTranslationY(finalWorkspaceTranslationY); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2447 | } |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2448 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2449 | if (stateIsNormal) { |
Adam Cohen | ed307df | 2013-10-02 09:37:31 -0700 | [diff] [blame] | 2450 | animateBackgroundGradient(0f, animated); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2451 | } else { |
| 2452 | animateBackgroundGradient(getResources().getInteger( |
| 2453 | R.integer.config_workspaceScrimAlpha) / 100f, animated); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 2454 | } |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2455 | return anim; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 2458 | static class AlphaUpdateListener implements AnimatorUpdateListener, AnimatorListener { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2459 | View view; |
| 2460 | public AlphaUpdateListener(View v) { |
| 2461 | view = v; |
| 2462 | } |
| 2463 | |
| 2464 | @Override |
| 2465 | public void onAnimationUpdate(ValueAnimator arg0) { |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2466 | updateVisibility(view); |
| 2467 | } |
| 2468 | |
| 2469 | public static void updateVisibility(View view) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2470 | // We want to avoid the extra layout pass by setting the views to GONE unless |
| 2471 | // accessibility is on, in which case not setting them to GONE causes a glitch. |
| 2472 | int invisibleState = sAccessibilityEnabled ? GONE : INVISIBLE; |
| 2473 | if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != invisibleState) { |
| 2474 | view.setVisibility(invisibleState); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2475 | } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD |
| 2476 | && view.getVisibility() != VISIBLE) { |
| 2477 | view.setVisibility(VISIBLE); |
| 2478 | } |
| 2479 | } |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 2480 | |
| 2481 | @Override |
| 2482 | public void onAnimationCancel(Animator arg0) { |
| 2483 | } |
| 2484 | |
| 2485 | @Override |
| 2486 | public void onAnimationEnd(Animator arg0) { |
| 2487 | updateVisibility(view); |
| 2488 | } |
| 2489 | |
| 2490 | @Override |
| 2491 | public void onAnimationRepeat(Animator arg0) { |
| 2492 | } |
| 2493 | |
| 2494 | @Override |
| 2495 | public void onAnimationStart(Animator arg0) { |
| 2496 | // We want the views to be visible for animation, so fade-in/out is visible |
| 2497 | view.setVisibility(VISIBLE); |
| 2498 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2501 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 2502 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2503 | onTransitionPrepare(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 2507 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
| 2508 | } |
| 2509 | |
| 2510 | @Override |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 2511 | public void onLauncherTransitionStep(Launcher l, float t) { |
| 2512 | mTransitionProgress = t; |
| 2513 | } |
| 2514 | |
| 2515 | @Override |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2516 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2517 | onTransitionEnd(); |
| 2518 | } |
| 2519 | |
| 2520 | private void onTransitionPrepare() { |
| 2521 | mIsSwitchingState = true; |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2522 | |
| 2523 | // Invalidate here to ensure that the pages are rendered during the state change transition. |
| 2524 | invalidate(); |
| 2525 | |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2526 | updateChildrenLayersEnabled(false); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2527 | hideCustomContentIfNecessary(); |
| 2528 | } |
| 2529 | |
| 2530 | void updateCustomContentVisibility() { |
| 2531 | int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE; |
| 2532 | if (hasCustomContent()) { |
| 2533 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility); |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | void showCustomContentIfNecessary() { |
| 2538 | boolean show = mState == Workspace.State.NORMAL; |
| 2539 | if (show && hasCustomContent()) { |
| 2540 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | void hideCustomContentIfNecessary() { |
| 2545 | boolean hide = mState != Workspace.State.NORMAL; |
| 2546 | if (hide && hasCustomContent()) { |
Adam Cohen | 1774a8a | 2013-11-15 10:56:16 +0000 | [diff] [blame] | 2547 | disableLayoutTransitions(); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2548 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE); |
Adam Cohen | 1774a8a | 2013-11-15 10:56:16 +0000 | [diff] [blame] | 2549 | enableLayoutTransitions(); |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | private void onTransitionEnd() { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2554 | mIsSwitchingState = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 2555 | updateChildrenLayersEnabled(false); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2556 | showCustomContentIfNecessary(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | @Override |
| 2560 | public View getContent() { |
| 2561 | return this; |
| 2562 | } |
| 2563 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2564 | /** |
| 2565 | * Draw the View v into the given Canvas. |
| 2566 | * |
| 2567 | * @param v the view to draw |
| 2568 | * @param destCanvas the canvas to draw on |
| 2569 | * @param padding the horizontal and vertical padding to use when drawing |
| 2570 | */ |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2571 | private static void drawDragView(View v, Canvas destCanvas, int padding) { |
| 2572 | final Rect clipRect = sTempRect; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2573 | v.getDrawingRect(clipRect); |
| 2574 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2575 | boolean textVisible = false; |
| 2576 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2577 | destCanvas.save(); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2578 | if (v instanceof TextView) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2579 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2580 | Rect bounds = getDrawableBounds(d); |
| 2581 | clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2582 | destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2583 | d.draw(destCanvas); |
| 2584 | } else { |
| 2585 | if (v instanceof FolderIcon) { |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2586 | // For FolderIcons the text can bleed into the icon area, and so we need to |
| 2587 | // hide the text completely (which can't be achieved by clipping). |
| 2588 | if (((FolderIcon) v).getTextVisible()) { |
| 2589 | ((FolderIcon) v).setTextVisible(false); |
| 2590 | textVisible = true; |
| 2591 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2592 | } |
| 2593 | destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); |
| 2594 | destCanvas.clipRect(clipRect, Op.REPLACE); |
| 2595 | v.draw(destCanvas); |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2596 | |
| 2597 | // Restore text visibility of FolderIcon if necessary |
| 2598 | if (textVisible) { |
| 2599 | ((FolderIcon) v).setTextVisible(true); |
| 2600 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2601 | } |
| 2602 | destCanvas.restore(); |
| 2603 | } |
| 2604 | |
| 2605 | /** |
| 2606 | * Returns a new bitmap to show when the given View is being dragged around. |
| 2607 | * Responsibility for the bitmap is transferred to the caller. |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2608 | * @param expectedPadding padding to add to the drag view. If a different padding was used |
| 2609 | * its value will be changed |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2610 | */ |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2611 | public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2612 | Bitmap b; |
| 2613 | |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2614 | int padding = expectedPadding.get(); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2615 | if (v instanceof TextView) { |
| 2616 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2617 | Rect bounds = getDrawableBounds(d); |
| 2618 | b = Bitmap.createBitmap(bounds.width() + padding, |
| 2619 | bounds.height() + padding, Bitmap.Config.ARGB_8888); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2620 | expectedPadding.set(padding - bounds.left - bounds.top); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2621 | } else { |
| 2622 | b = Bitmap.createBitmap( |
| 2623 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2626 | mCanvas.setBitmap(b); |
| 2627 | drawDragView(v, mCanvas, padding); |
| 2628 | mCanvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2629 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2630 | return b; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /** |
| 2634 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 2635 | * Responsibility for the bitmap is transferred to the caller. |
| 2636 | */ |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2637 | private Bitmap createDragOutline(View v, int padding) { |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2638 | final int outlineColor = getResources().getColor(R.color.outline_color); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2639 | final Bitmap b = Bitmap.createBitmap( |
| 2640 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 2641 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2642 | mCanvas.setBitmap(b); |
| 2643 | drawDragView(v, mCanvas, padding); |
| 2644 | mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor); |
| 2645 | mCanvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2646 | return b; |
| 2647 | } |
| 2648 | |
| 2649 | /** |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2650 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 2651 | * Responsibility for the bitmap is transferred to the caller. |
| 2652 | */ |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2653 | private Bitmap createDragOutline(Bitmap orig, int padding, int w, int h, |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2654 | boolean clipAlpha) { |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2655 | final int outlineColor = getResources().getColor(R.color.outline_color); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2656 | final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2657 | mCanvas.setBitmap(b); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2658 | |
| 2659 | Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); |
| 2660 | float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), |
| 2661 | (h - padding) / (float) orig.getHeight()); |
| 2662 | int scaledWidth = (int) (scaleFactor * orig.getWidth()); |
| 2663 | int scaledHeight = (int) (scaleFactor * orig.getHeight()); |
| 2664 | Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); |
| 2665 | |
| 2666 | // center the image |
| 2667 | dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); |
| 2668 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2669 | mCanvas.drawBitmap(orig, src, dst, null); |
| 2670 | mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor, |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2671 | clipAlpha); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2672 | mCanvas.setBitmap(null); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2673 | |
| 2674 | return b; |
| 2675 | } |
| 2676 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2677 | void startDrag(CellLayout.CellInfo cellInfo) { |
| 2678 | View child = cellInfo.cell; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2679 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2680 | // Make sure the drag was started by a long press as opposed to a long click. |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 2681 | if (!child.isInTouchMode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2682 | return; |
| 2683 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2684 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2685 | mDragInfo = cellInfo; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2686 | child.setVisibility(INVISIBLE); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2687 | CellLayout layout = (CellLayout) child.getParent().getParent(); |
| 2688 | layout.prepareChildForDrag(child); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2689 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2690 | beginDragShared(child, this); |
| 2691 | } |
| 2692 | |
| 2693 | public void beginDragShared(View child, DragSource source) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2694 | child.clearFocus(); |
| 2695 | child.setPressed(false); |
| 2696 | |
| 2697 | // The outline is used to visualize where the item will land if dropped |
| 2698 | mDragOutline = createDragOutline(child, DRAG_BITMAP_PADDING); |
| 2699 | |
Anjali Koppal | 62d18ed | 2014-03-10 17:04:03 -0700 | [diff] [blame] | 2700 | mLauncher.onDragStarted(child); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2701 | // The drag bitmap follows the touch point around on the screen |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2702 | AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2703 | final Bitmap b = createDragBitmap(child, padding); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2704 | |
| 2705 | final int bmpWidth = b.getWidth(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 2706 | final int bmpHeight = b.getHeight(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2707 | |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2708 | float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2709 | int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); |
| 2710 | int dragLayerY = Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2 |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2711 | - padding.get() / 2); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2712 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2713 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2714 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2715 | Point dragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2716 | Rect dragRect = null; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2717 | if (child instanceof BubbleTextView) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2718 | int iconSize = grid.iconSizePx; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2719 | int top = child.getPaddingTop(); |
| 2720 | int left = (bmpWidth - iconSize) / 2; |
| 2721 | int right = left + iconSize; |
| 2722 | int bottom = top + iconSize; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2723 | dragLayerY += top; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2724 | // Note: The drag region is used to calculate drag layer offsets, but the |
| 2725 | // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2726 | dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2727 | dragRect = new Rect(left, top, right, bottom); |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 2728 | } else if (child instanceof FolderIcon) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2729 | int previewSize = grid.folderIconSizePx; |
| 2730 | dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 2733 | // Clear the pressed state if necessary |
| 2734 | if (child instanceof BubbleTextView) { |
| 2735 | BubbleTextView icon = (BubbleTextView) child; |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2736 | icon.clearPressedBackground(); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
Adam Cohen | 2f32ad2 | 2013-11-13 11:29:49 +0000 | [diff] [blame] | 2739 | if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) { |
| 2740 | String msg = "Drag started with a view that has no tag set. This " |
| 2741 | + "will cause a crash (issue 11627249) down the line. " |
| 2742 | + "View: " + child + " tag: " + child.getTag(); |
| 2743 | throw new IllegalStateException(msg); |
| 2744 | } |
| 2745 | |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 2746 | DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2747 | DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale); |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 2748 | dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2749 | |
| 2750 | if (child.getParent() instanceof ShortcutAndWidgetContainer) { |
| 2751 | mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent(); |
| 2752 | } |
| 2753 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2754 | b.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2755 | } |
| 2756 | |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2757 | public void beginExternalDragShared(View child, DragSource source) { |
| 2758 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2759 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2760 | int iconSize = grid.iconSizePx; |
| 2761 | |
| 2762 | // Notify launcher of drag start |
| 2763 | mLauncher.onDragStarted(child); |
| 2764 | |
| 2765 | // Compose a new drag bitmap that is of the icon size |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2766 | AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2767 | final Bitmap tmpB = createDragBitmap(child, padding); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2768 | Bitmap b = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); |
| 2769 | Paint p = new Paint(); |
| 2770 | p.setFilterBitmap(true); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2771 | mCanvas.setBitmap(b); |
| 2772 | mCanvas.drawBitmap(tmpB, new Rect(0, 0, tmpB.getWidth(), tmpB.getHeight()), |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2773 | new Rect(0, 0, iconSize, iconSize), p); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 2774 | mCanvas.setBitmap(null); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2775 | |
| 2776 | // Find the child's location on the screen |
| 2777 | int bmpWidth = tmpB.getWidth(); |
| 2778 | float iconScale = (float) bmpWidth / iconSize; |
| 2779 | float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY) * iconScale; |
| 2780 | int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); |
| 2781 | int dragLayerY = Math.round(mTempXY[1]); |
| 2782 | |
| 2783 | // Note: The drag region is used to calculate drag layer offsets, but the |
| 2784 | // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 2785 | Point dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2786 | Rect dragRect = new Rect(0, 0, iconSize, iconSize); |
| 2787 | |
| 2788 | if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) { |
| 2789 | String msg = "Drag started with a view that has no tag set. This " |
| 2790 | + "will cause a crash (issue 11627249) down the line. " |
| 2791 | + "View: " + child + " tag: " + child.getTag(); |
| 2792 | throw new IllegalStateException(msg); |
| 2793 | } |
| 2794 | |
| 2795 | // Start the drag |
| 2796 | DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), |
| 2797 | DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale); |
| 2798 | dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); |
| 2799 | |
| 2800 | // Recycle temporary bitmaps |
| 2801 | tmpB.recycle(); |
| 2802 | } |
| 2803 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2804 | void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, long screenId, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2805 | int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) { |
| 2806 | View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2807 | |
| 2808 | final int[] cellXY = new int[2]; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2809 | target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2810 | addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1, insertAtFirst); |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 2811 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2812 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, cellXY[0], |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2813 | cellXY[1]); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2816 | public boolean transitionStateShouldAllowDrop() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 2817 | return ((!isSwitchingState() || mTransitionProgress > 0.5f) && |
| 2818 | (mState == State.NORMAL || mState == State.SPRING_LOADED)); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2821 | /** |
| 2822 | * {@inheritDoc} |
| 2823 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2824 | public boolean acceptDrop(DragObject d) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2825 | // If it's an external drop (e.g. from All Apps), check if it should be accepted |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2826 | CellLayout dropTargetLayout = mDropToLayout; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2827 | if (d.dragSource != this) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2828 | // Don't accept the drop if we're not over a screen at time of drop |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2829 | if (dropTargetLayout == null) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2830 | return false; |
| 2831 | } |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2832 | if (!transitionStateShouldAllowDrop()) return false; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2833 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2834 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
| 2835 | d.dragView, mDragViewVisualCenter); |
| 2836 | |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2837 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2838 | if (mLauncher.isHotseatLayout(dropTargetLayout)) { |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2839 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2840 | } else { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2841 | mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2844 | int spanX = 1; |
| 2845 | int spanY = 1; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2846 | if (mDragInfo != null) { |
| 2847 | final CellLayout.CellInfo dragCellInfo = mDragInfo; |
| 2848 | spanX = dragCellInfo.spanX; |
| 2849 | spanY = dragCellInfo.spanY; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2850 | } else { |
| 2851 | final ItemInfo dragInfo = (ItemInfo) d.dragInfo; |
| 2852 | spanX = dragInfo.spanX; |
| 2853 | spanY = dragInfo.spanY; |
| 2854 | } |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2855 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2856 | int minSpanX = spanX; |
| 2857 | int minSpanY = spanY; |
| 2858 | if (d.dragInfo instanceof PendingAddWidgetInfo) { |
| 2859 | minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX; |
| 2860 | minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY; |
| 2861 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2862 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2863 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2864 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout, |
Adam Cohen | f0777b9 | 2012-02-28 14:02:45 -0800 | [diff] [blame] | 2865 | mTargetCell); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2866 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2867 | mDragViewVisualCenter[1], mTargetCell); |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 2868 | if (mCreateUserFolderOnDrop && willCreateUserFolder((ItemInfo) d.dragInfo, |
| 2869 | dropTargetLayout, mTargetCell, distance, true)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2870 | return true; |
| 2871 | } |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 2872 | |
| 2873 | if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder((ItemInfo) d.dragInfo, |
| 2874 | dropTargetLayout, mTargetCell, distance)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2875 | return true; |
| 2876 | } |
| 2877 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2878 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2879 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2880 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 2881 | null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP); |
| 2882 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
| 2883 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2884 | // Don't accept the drop if there's no room for the item |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2885 | if (!foundCell) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2886 | // Don't show the message if we are dropping on the AllApps button and the hotseat |
| 2887 | // is full |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2888 | boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 2889 | if (mTargetCell != null && isHotseat) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2890 | Hotseat hotseat = mLauncher.getHotseat(); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 2891 | if (hotseat.isAllAppsButtonRank( |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2892 | hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) { |
| 2893 | return false; |
| 2894 | } |
| 2895 | } |
| 2896 | |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 2897 | mLauncher.showOutOfSpaceMessage(isHotseat); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2898 | return false; |
| 2899 | } |
| 2900 | } |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2901 | |
| 2902 | long screenId = getIdForScreen(dropTargetLayout); |
| 2903 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
| 2904 | commitExtraEmptyScreen(); |
| 2905 | } |
| 2906 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2907 | return true; |
| 2908 | } |
| 2909 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2910 | boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, float |
| 2911 | distance, boolean considerTimeout) { |
| 2912 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2913 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
| 2914 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2915 | if (dropOverView != null) { |
| 2916 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
| 2917 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) { |
| 2918 | return false; |
| 2919 | } |
| 2920 | } |
| 2921 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2922 | boolean hasntMoved = false; |
| 2923 | if (mDragInfo != null) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2924 | hasntMoved = dropOverView == mDragInfo.cell; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2925 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2926 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2927 | if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { |
| 2928 | return false; |
| 2929 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2930 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2931 | boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2932 | boolean willBecomeShortcut = |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2933 | (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 2934 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2935 | |
| 2936 | return (aboveShortcut && willBecomeShortcut); |
| 2937 | } |
| 2938 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2939 | boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell, |
| 2940 | float distance) { |
| 2941 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2942 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2943 | |
| 2944 | if (dropOverView != null) { |
| 2945 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
| 2946 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) { |
| 2947 | return false; |
| 2948 | } |
| 2949 | } |
| 2950 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2951 | if (dropOverView instanceof FolderIcon) { |
| 2952 | FolderIcon fi = (FolderIcon) dropOverView; |
| 2953 | if (fi.acceptDrop(dragInfo)) { |
| 2954 | return true; |
| 2955 | } |
| 2956 | } |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2960 | boolean createUserFolderIfNecessary(View newView, long container, CellLayout target, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2961 | int[] targetCell, float distance, boolean external, DragView dragView, |
| 2962 | Runnable postAnimationRunnable) { |
| 2963 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2964 | View v = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2965 | |
Winson Chung | ec9a0a4 | 2011-07-20 20:42:13 -0700 | [diff] [blame] | 2966 | boolean hasntMoved = false; |
| 2967 | if (mDragInfo != null) { |
| 2968 | CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); |
| 2969 | hasntMoved = (mDragInfo.cellX == targetCell[0] && |
| 2970 | mDragInfo.cellY == targetCell[1]) && (cellParent == target); |
| 2971 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2972 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2973 | if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; |
| 2974 | mCreateUserFolderOnDrop = false; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2975 | final long screenId = (targetCell == null) ? mDragInfo.screenId : getIdForScreen(target); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2976 | |
| 2977 | boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); |
| 2978 | boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); |
| 2979 | |
| 2980 | if (aboveShortcut && willBecomeShortcut) { |
| 2981 | ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); |
| 2982 | ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); |
| 2983 | // if the drag started here, we need to remove it from the workspace |
| 2984 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2985 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2986 | } |
| 2987 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2988 | Rect folderLocation = new Rect(); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2989 | float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2990 | target.removeView(v); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2991 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2992 | FolderIcon fi = |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2993 | mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 2994 | destInfo.cellX = -1; |
| 2995 | destInfo.cellY = -1; |
| 2996 | sourceInfo.cellX = -1; |
| 2997 | sourceInfo.cellY = -1; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2998 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2999 | // If the dragView is null, we can't animate |
| 3000 | boolean animate = dragView != null; |
| 3001 | if (animate) { |
| 3002 | fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale, |
| 3003 | postAnimationRunnable); |
| 3004 | } else { |
| 3005 | fi.addItem(destInfo); |
| 3006 | fi.addItem(sourceInfo); |
| 3007 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 3008 | return true; |
| 3009 | } |
| 3010 | return false; |
| 3011 | } |
| 3012 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 3013 | boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3014 | float distance, DragObject d, boolean external) { |
| 3015 | if (distance > mMaxDistanceForFolderCreation) return false; |
| 3016 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 3017 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3018 | if (!mAddToExistingFolderOnDrop) return false; |
| 3019 | mAddToExistingFolderOnDrop = false; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3020 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3021 | if (dropOverView instanceof FolderIcon) { |
| 3022 | FolderIcon fi = (FolderIcon) dropOverView; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3023 | if (fi.acceptDrop(d.dragInfo)) { |
| 3024 | fi.onDrop(d); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3025 | |
| 3026 | // if the drag started here, we need to remove it from the workspace |
| 3027 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3028 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3029 | } |
| 3030 | return true; |
| 3031 | } |
| 3032 | } |
| 3033 | return false; |
| 3034 | } |
| 3035 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3036 | public void onDrop(final DragObject d) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3037 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3038 | mDragViewVisualCenter); |
| 3039 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3040 | CellLayout dropTargetLayout = mDropToLayout; |
| 3041 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3042 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3043 | if (dropTargetLayout != null) { |
| 3044 | if (mLauncher.isHotseatLayout(dropTargetLayout)) { |
Adam Cohen | bb00ff2 | 2012-07-12 15:43:01 -0700 | [diff] [blame] | 3045 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3046 | } else { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3047 | mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3048 | } |
Adam Cohen | ba78161 | 2011-05-09 14:37:39 -0700 | [diff] [blame] | 3049 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 3050 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3051 | int snapScreen = -1; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3052 | boolean resizeOnDrop = false; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3053 | if (d.dragSource != this) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3054 | final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], |
| 3055 | (int) mDragViewVisualCenter[1] }; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3056 | onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3057 | } else if (mDragInfo != null) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3058 | final View cell = mDragInfo.cell; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3059 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3060 | Runnable resizeRunnable = null; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 3061 | if (dropTargetLayout != null && !d.cancelled) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3062 | // Move internally |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 3063 | boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout); |
| 3064 | boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
| 3065 | long container = hasMovedIntoHotseat ? |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3066 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 3067 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3068 | long screenId = (mTargetCell[0] < 0) ? |
| 3069 | mDragInfo.screenId : getIdForScreen(dropTargetLayout); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3070 | int spanX = mDragInfo != null ? mDragInfo.spanX : 1; |
| 3071 | int spanY = mDragInfo != null ? mDragInfo.spanY : 1; |
| 3072 | // First we find the cell nearest to point at which the item is |
| 3073 | // dropped, without any consideration to whether there is an item there. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3074 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3075 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) |
| 3076 | mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3077 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3078 | mDragViewVisualCenter[1], mTargetCell); |
| 3079 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3080 | // If the item being dropped is a shortcut and the nearest drop |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3081 | // cell also contains a shortcut, then create a folder with the two shortcuts. |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 3082 | if (!mInScrollArea && createUserFolderIfNecessary(cell, container, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3083 | dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3084 | return; |
| 3085 | } |
| 3086 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3087 | if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, |
| 3088 | distance, d, false)) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3089 | return; |
| 3090 | } |
| 3091 | |
| 3092 | // Aside from the special case where we're dropping a shortcut onto a shortcut, |
| 3093 | // we need to find the nearest cell location that is vacant |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3094 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 3095 | int minSpanX = item.spanX; |
| 3096 | int minSpanY = item.spanY; |
| 3097 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3098 | minSpanX = item.minSpanX; |
| 3099 | minSpanY = item.minSpanY; |
| 3100 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3101 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3102 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3103 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3104 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, |
| 3105 | mTargetCell, resultSpan, CellLayout.MODE_ON_DROP); |
| 3106 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3107 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3108 | |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3109 | // if the widget resizes on drop |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3110 | if (foundCell && (cell instanceof AppWidgetHostView) && |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3111 | (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3112 | resizeOnDrop = true; |
| 3113 | item.spanX = resultSpan[0]; |
| 3114 | item.spanY = resultSpan[1]; |
Adam Cohen | 7bdfc97 | 2012-05-22 16:50:35 -0700 | [diff] [blame] | 3115 | AppWidgetHostView awhv = (AppWidgetHostView) cell; |
| 3116 | AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], |
| 3117 | resultSpan[1]); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3118 | } |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3119 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3120 | if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) { |
| 3121 | snapScreen = getPageIndexForScreenId(screenId); |
| 3122 | snapToPage(snapScreen); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3125 | if (foundCell) { |
| 3126 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 3127 | if (hasMovedLayouts) { |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3128 | // Reparent the view |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 3129 | CellLayout parentCell = getParentCellLayoutForView(cell); |
| 3130 | if (parentCell != null) { |
| 3131 | parentCell.removeView(cell); |
| 3132 | } else if (LauncherAppState.isDogfoodBuild()) { |
| 3133 | throw new NullPointerException("mDragInfo.cell has null parent"); |
| 3134 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3135 | addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1], |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3136 | info.spanX, info.spanY); |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | // update the item's position after drop |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3140 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3141 | lp.cellX = lp.tmpCellX = mTargetCell[0]; |
| 3142 | lp.cellY = lp.tmpCellY = mTargetCell[1]; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3143 | lp.cellHSpan = item.spanX; |
| 3144 | lp.cellVSpan = item.spanY; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3145 | lp.isLockedToGrid = true; |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3146 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3147 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 3148 | cell instanceof LauncherAppWidgetHostView) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3149 | final CellLayout cellLayout = dropTargetLayout; |
| 3150 | // We post this call so that the widget has a chance to be placed |
| 3151 | // in its final location |
| 3152 | |
| 3153 | final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; |
Sunny Goyal | 2434d40 | 2015-02-17 11:44:15 -0800 | [diff] [blame] | 3154 | AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo(); |
| 3155 | if (pInfo != null && pInfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3156 | final Runnable addResizeFrame = new Runnable() { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3157 | public void run() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3158 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3159 | dragLayer.addResizeFrame(info, hostView, cellLayout); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3160 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3161 | }; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3162 | resizeRunnable = (new Runnable() { |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3163 | public void run() { |
| 3164 | if (!isPageMoving()) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3165 | addResizeFrame.run(); |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3166 | } else { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3167 | mDelayedResizeRunnable = addResizeFrame; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3168 | } |
| 3169 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3170 | }); |
| 3171 | } |
| 3172 | } |
| 3173 | |
Adam Cohen | 949debe | 2013-09-29 14:22:48 -0700 | [diff] [blame] | 3174 | LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX, |
| 3175 | lp.cellY, item.spanX, item.spanY); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3176 | } else { |
| 3177 | // If we can't find a drop location, we return the item to its original position |
| 3178 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
| 3179 | mTargetCell[0] = lp.cellX; |
| 3180 | mTargetCell[1] = lp.cellY; |
Adam Cohen | f1dcdf6 | 2012-05-10 16:51:52 -0700 | [diff] [blame] | 3181 | CellLayout layout = (CellLayout) cell.getParent().getParent(); |
| 3182 | layout.markCellsAsOccupiedForView(cell); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 3183 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3184 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3185 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 3186 | final CellLayout parent = (CellLayout) cell.getParent().getParent(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3187 | final Runnable finalResizeRunnable = resizeRunnable; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3188 | // Prepare it to be animated into its new position |
| 3189 | // This must be called after the view has been re-parented |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3190 | final Runnable onCompleteRunnable = new Runnable() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3191 | @Override |
| 3192 | public void run() { |
| 3193 | mAnimatingViewIntoPlace = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 3194 | updateChildrenLayersEnabled(false); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3195 | if (finalResizeRunnable != null) { |
| 3196 | finalResizeRunnable.run(); |
| 3197 | } |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3198 | } |
| 3199 | }; |
| 3200 | mAnimatingViewIntoPlace = true; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3201 | if (d.dragView.hasDrawn()) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3202 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 3203 | boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 3204 | || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 3205 | if (isWidget) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3206 | int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : |
| 3207 | ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
| 3208 | animateWidgetDrop(info, parent, d.dragView, |
| 3209 | onCompleteRunnable, animationType, cell, false); |
| 3210 | } else { |
Adam Cohen | 85b467b | 2012-02-29 15:38:46 -0800 | [diff] [blame] | 3211 | int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION; |
| 3212 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, |
| 3213 | onCompleteRunnable, this); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3214 | } |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3215 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 3216 | d.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3217 | cell.setVisibility(VISIBLE); |
| 3218 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3219 | parent.onDropChild(cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3220 | } |
| 3221 | } |
| 3222 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3223 | public void setFinalScrollForPageChange(int pageIndex) { |
| 3224 | CellLayout cl = (CellLayout) getChildAt(pageIndex); |
| 3225 | if (cl != null) { |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3226 | mSavedScrollX = getScrollX(); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3227 | mSavedTranslationX = cl.getTranslationX(); |
| 3228 | mSavedRotationY = cl.getRotationY(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 3229 | final int newX = getScrollForPage(pageIndex); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3230 | setScrollX(newX); |
| 3231 | cl.setTranslationX(0f); |
| 3232 | cl.setRotationY(0f); |
| 3233 | } |
| 3234 | } |
| 3235 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3236 | public void resetFinalScrollForPageChange(int pageIndex) { |
| 3237 | if (pageIndex >= 0) { |
| 3238 | CellLayout cl = (CellLayout) getChildAt(pageIndex); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3239 | setScrollX(mSavedScrollX); |
| 3240 | cl.setTranslationX(mSavedTranslationX); |
| 3241 | cl.setRotationY(mSavedRotationY); |
| 3242 | } |
| 3243 | } |
| 3244 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3245 | public void getViewLocationRelativeToSelf(View v, int[] location) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3246 | getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3247 | int x = location[0]; |
| 3248 | int y = location[1]; |
| 3249 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3250 | v.getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3251 | int vX = location[0]; |
| 3252 | int vY = location[1]; |
| 3253 | |
| 3254 | location[0] = vX - x; |
| 3255 | location[1] = vY - y; |
| 3256 | } |
| 3257 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3258 | public void onDragEnter(DragObject d) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3259 | mDragEnforcer.onDragEnter(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3260 | mCreateUserFolderOnDrop = false; |
| 3261 | mAddToExistingFolderOnDrop = false; |
| 3262 | |
| 3263 | mDropToLayout = null; |
| 3264 | CellLayout layout = getCurrentDropLayout(); |
| 3265 | setCurrentDropLayout(layout); |
| 3266 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 3267 | |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 3268 | if (!workspaceInModalState()) { |
| 3269 | mLauncher.getDragLayer().showPageHints(); |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3270 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3271 | } |
| 3272 | |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3273 | /** Return a rect that has the cellWidth/cellHeight (left, top), and |
| 3274 | * widthGap/heightGap (right, bottom) */ |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3275 | static Rect getCellLayoutMetrics(Launcher launcher, int orientation) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3276 | LauncherAppState app = LauncherAppState.getInstance(); |
| 3277 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 3278 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3279 | Display display = launcher.getWindowManager().getDefaultDisplay(); |
| 3280 | Point smallestSize = new Point(); |
| 3281 | Point largestSize = new Point(); |
| 3282 | display.getCurrentSizeRange(smallestSize, largestSize); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 3283 | int countX = (int) grid.numColumns; |
| 3284 | int countY = (int) grid.numRows; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3285 | if (orientation == CellLayout.LANDSCAPE) { |
| 3286 | if (mLandscapeCellLayoutMetrics == null) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3287 | Rect padding = grid.getWorkspacePadding(CellLayout.LANDSCAPE); |
Winson Chung | dcd27ba | 2013-12-11 15:28:15 -0800 | [diff] [blame] | 3288 | int width = largestSize.x - padding.left - padding.right; |
| 3289 | int height = smallestSize.y - padding.top - padding.bottom; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3290 | mLandscapeCellLayoutMetrics = new Rect(); |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3291 | mLandscapeCellLayoutMetrics.set( |
| 3292 | grid.calculateCellWidth(width, countX), |
| 3293 | grid.calculateCellHeight(height, countY), 0, 0); |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3294 | } |
| 3295 | return mLandscapeCellLayoutMetrics; |
| 3296 | } else if (orientation == CellLayout.PORTRAIT) { |
| 3297 | if (mPortraitCellLayoutMetrics == null) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3298 | Rect padding = grid.getWorkspacePadding(CellLayout.PORTRAIT); |
Winson Chung | dcd27ba | 2013-12-11 15:28:15 -0800 | [diff] [blame] | 3299 | int width = smallestSize.x - padding.left - padding.right; |
| 3300 | int height = largestSize.y - padding.top - padding.bottom; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3301 | mPortraitCellLayoutMetrics = new Rect(); |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3302 | mPortraitCellLayoutMetrics.set( |
| 3303 | grid.calculateCellWidth(width, countX), |
| 3304 | grid.calculateCellHeight(height, countY), 0, 0); |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3305 | } |
| 3306 | return mPortraitCellLayoutMetrics; |
| 3307 | } |
| 3308 | return null; |
| 3309 | } |
| 3310 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3311 | public void onDragExit(DragObject d) { |
| 3312 | mDragEnforcer.onDragExit(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3313 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3314 | // Here we store the final page that will be dropped to, if the workspace in fact |
| 3315 | // receives the drop |
| 3316 | if (mInScrollArea) { |
Winson Chung | cc1cfe4 | 2012-06-18 15:09:17 -0700 | [diff] [blame] | 3317 | if (isPageMoving()) { |
| 3318 | // If the user drops while the page is scrolling, we should use that page as the |
| 3319 | // destination instead of the page that is being hovered over. |
| 3320 | mDropToLayout = (CellLayout) getPageAt(getNextPage()); |
| 3321 | } else { |
| 3322 | mDropToLayout = mDragOverlappingLayout; |
| 3323 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3324 | } else { |
| 3325 | mDropToLayout = mDragTargetLayout; |
| 3326 | } |
| 3327 | |
| 3328 | if (mDragMode == DRAG_MODE_CREATE_FOLDER) { |
| 3329 | mCreateUserFolderOnDrop = true; |
| 3330 | } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 3331 | mAddToExistingFolderOnDrop = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3332 | } |
| 3333 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3334 | // Reset the scroll area and previous drag target |
| 3335 | onResetScrollArea(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3336 | setCurrentDropLayout(null); |
| 3337 | setCurrentDragOverlappingLayout(null); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3338 | |
Adam Cohen | 74c28d1 | 2011-11-18 14:17:11 -0800 | [diff] [blame] | 3339 | mSpringLoadedDragController.cancel(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3340 | |
| 3341 | if (!mIsPageMoving) { |
| 3342 | hideOutlines(); |
| 3343 | } |
Adam Cohen | c50438c | 2014-08-19 17:43:05 -0700 | [diff] [blame] | 3344 | mLauncher.getDragLayer().hidePageHints(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3347 | void setCurrentDropLayout(CellLayout layout) { |
| 3348 | if (mDragTargetLayout != null) { |
| 3349 | mDragTargetLayout.revertTempState(); |
| 3350 | mDragTargetLayout.onDragExit(); |
| 3351 | } |
| 3352 | mDragTargetLayout = layout; |
| 3353 | if (mDragTargetLayout != null) { |
| 3354 | mDragTargetLayout.onDragEnter(); |
| 3355 | } |
| 3356 | cleanupReorder(true); |
| 3357 | cleanupFolderCreation(); |
| 3358 | setCurrentDropOverCell(-1, -1); |
| 3359 | } |
| 3360 | |
| 3361 | void setCurrentDragOverlappingLayout(CellLayout layout) { |
| 3362 | if (mDragOverlappingLayout != null) { |
| 3363 | mDragOverlappingLayout.setIsDragOverlapping(false); |
| 3364 | } |
| 3365 | mDragOverlappingLayout = layout; |
| 3366 | if (mDragOverlappingLayout != null) { |
| 3367 | mDragOverlappingLayout.setIsDragOverlapping(true); |
| 3368 | } |
| 3369 | invalidate(); |
| 3370 | } |
| 3371 | |
| 3372 | void setCurrentDropOverCell(int x, int y) { |
| 3373 | if (x != mDragOverX || y != mDragOverY) { |
| 3374 | mDragOverX = x; |
| 3375 | mDragOverY = y; |
| 3376 | setDragMode(DRAG_MODE_NONE); |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | void setDragMode(int dragMode) { |
| 3381 | if (dragMode != mDragMode) { |
| 3382 | if (dragMode == DRAG_MODE_NONE) { |
| 3383 | cleanupAddToFolder(); |
| 3384 | // We don't want to cancel the re-order alarm every time the target cell changes |
| 3385 | // as this feels to slow / unresponsive. |
| 3386 | cleanupReorder(false); |
| 3387 | cleanupFolderCreation(); |
| 3388 | } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 3389 | cleanupReorder(true); |
| 3390 | cleanupFolderCreation(); |
| 3391 | } else if (dragMode == DRAG_MODE_CREATE_FOLDER) { |
| 3392 | cleanupAddToFolder(); |
| 3393 | cleanupReorder(true); |
| 3394 | } else if (dragMode == DRAG_MODE_REORDER) { |
| 3395 | cleanupAddToFolder(); |
| 3396 | cleanupFolderCreation(); |
| 3397 | } |
| 3398 | mDragMode = dragMode; |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | private void cleanupFolderCreation() { |
| 3403 | if (mDragFolderRingAnimator != null) { |
| 3404 | mDragFolderRingAnimator.animateToNaturalState(); |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3405 | mDragFolderRingAnimator = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3406 | } |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3407 | mFolderCreationAlarm.setOnAlarmListener(null); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3408 | mFolderCreationAlarm.cancelAlarm(); |
| 3409 | } |
| 3410 | |
| 3411 | private void cleanupAddToFolder() { |
| 3412 | if (mDragOverFolderIcon != null) { |
| 3413 | mDragOverFolderIcon.onDragExit(null); |
| 3414 | mDragOverFolderIcon = null; |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | private void cleanupReorder(boolean cancelAlarm) { |
| 3419 | // Any pending reorders are canceled |
| 3420 | if (cancelAlarm) { |
| 3421 | mReorderAlarm.cancelAlarm(); |
| 3422 | } |
| 3423 | mLastReorderX = -1; |
| 3424 | mLastReorderY = -1; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3425 | } |
| 3426 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3427 | /* |
| 3428 | * |
| 3429 | * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's |
| 3430 | * coordinate space. The argument xy is modified with the return result. |
| 3431 | * |
| 3432 | * if cachedInverseMatrix is not null, this method will just use that matrix instead of |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3433 | * computing it itself; we use this to avoid redundant matrix inversions in |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3434 | * findMatchingPageForDragOver |
| 3435 | * |
| 3436 | */ |
| 3437 | void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3438 | xy[0] = xy[0] - v.getLeft(); |
| 3439 | xy[1] = xy[1] - v.getTop(); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3442 | boolean isPointInSelfOverHotseat(int x, int y, Rect r) { |
| 3443 | if (r == null) { |
| 3444 | r = new Rect(); |
| 3445 | } |
| 3446 | mTempPt[0] = x; |
| 3447 | mTempPt[1] = y; |
| 3448 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true); |
Winson Chung | abedd9f | 2013-09-24 15:41:09 -0700 | [diff] [blame] | 3449 | |
| 3450 | LauncherAppState app = LauncherAppState.getInstance(); |
| 3451 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 3452 | r = grid.getHotseatRect(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3453 | if (r.contains(mTempPt[0], mTempPt[1])) { |
| 3454 | return true; |
| 3455 | } |
| 3456 | return false; |
| 3457 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3458 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3459 | void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3460 | mTempPt[0] = (int) xy[0]; |
| 3461 | mTempPt[1] = (int) xy[1]; |
| 3462 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true); |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 3463 | mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempPt); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3464 | |
| 3465 | xy[0] = mTempPt[0]; |
| 3466 | xy[1] = mTempPt[1]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3467 | } |
| 3468 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3469 | /* |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3470 | * |
| 3471 | * Convert the 2D coordinate xy from this CellLayout's coordinate space to |
| 3472 | * the parent View's coordinate space. The argument xy is modified with the return result. |
| 3473 | * |
| 3474 | */ |
| 3475 | void mapPointFromChildToSelf(View v, float[] xy) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3476 | xy[0] += v.getLeft(); |
| 3477 | xy[1] += v.getTop(); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3480 | static private float squaredDistance(float[] point1, float[] point2) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3481 | float distanceX = point1[0] - point2[0]; |
| 3482 | float distanceY = point2[1] - point2[1]; |
| 3483 | return distanceX * distanceX + distanceY * distanceY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3484 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3485 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3486 | /* |
| 3487 | * |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3488 | * This method returns the CellLayout that is currently being dragged to. In order to drag |
| 3489 | * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second |
| 3490 | * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one |
| 3491 | * |
| 3492 | * Return null if no CellLayout is currently being dragged over |
| 3493 | * |
| 3494 | */ |
| 3495 | private CellLayout findMatchingPageForDragOver( |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3496 | DragView dragView, float originX, float originY, boolean exact) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3497 | // We loop through all the screens (ie CellLayouts) and see which ones overlap |
| 3498 | // with the item being dragged and then choose the one that's closest to the touch point |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3499 | final int screenCount = getChildCount(); |
| 3500 | CellLayout bestMatchingScreen = null; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3501 | float smallestDistSoFar = Float.MAX_VALUE; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3502 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3503 | for (int i = 0; i < screenCount; i++) { |
Adam Cohen | df041bc | 2013-09-22 12:32:22 -0700 | [diff] [blame] | 3504 | // The custom content screen is not a valid drag over option |
| 3505 | if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) { |
| 3506 | continue; |
| 3507 | } |
| 3508 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3509 | CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3510 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3511 | final float[] touchXy = {originX, originY}; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3512 | // Transform the touch coordinates to the CellLayout's local coordinates |
| 3513 | // If the touch point is within the bounds of the cell layout, we can return immediately |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3514 | cl.getMatrix().invert(mTempInverseMatrix); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3515 | mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3516 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3517 | if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && |
| 3518 | touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { |
| 3519 | return cl; |
| 3520 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3521 | |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 3522 | if (!exact) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3523 | // Get the center of the cell layout in screen coordinates |
| 3524 | final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; |
| 3525 | cellLayoutCenter[0] = cl.getWidth()/2; |
| 3526 | cellLayoutCenter[1] = cl.getHeight()/2; |
| 3527 | mapPointFromChildToSelf(cl, cellLayoutCenter); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3528 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3529 | touchXy[0] = originX; |
| 3530 | touchXy[1] = originY; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3531 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3532 | // Calculate the distance between the center of the CellLayout |
| 3533 | // and the touch point |
| 3534 | float dist = squaredDistance(touchXy, cellLayoutCenter); |
| 3535 | |
| 3536 | if (dist < smallestDistSoFar) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3537 | smallestDistSoFar = dist; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3538 | bestMatchingScreen = cl; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3539 | } |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3540 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3541 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3542 | return bestMatchingScreen; |
| 3543 | } |
| 3544 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3545 | // This is used to compute the visual center of the dragView. This point is then |
| 3546 | // used to visualize drop locations and determine where to drop an item. The idea is that |
| 3547 | // the visual center represents the user's interpretation of where the item is, and hence |
| 3548 | // is the appropriate point to use when determining drop location. |
| 3549 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 3550 | DragView dragView, float[] recycle) { |
| 3551 | float res[]; |
| 3552 | if (recycle == null) { |
| 3553 | res = new float[2]; |
| 3554 | } else { |
| 3555 | res = recycle; |
| 3556 | } |
| 3557 | |
| 3558 | // First off, the drag view has been shifted in a way that is not represented in the |
| 3559 | // x and y values or the x/yOffsets. Here we account for that shift. |
| 3560 | x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 3561 | y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 3562 | |
| 3563 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 3564 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 3565 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 3566 | // R.dimen.dragViewOffsetY. |
| 3567 | int left = x - xOffset; |
| 3568 | int top = y - yOffset; |
| 3569 | |
| 3570 | // In order to find the visual center, we shift by half the dragRect |
| 3571 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 3572 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 3573 | |
| 3574 | return res; |
| 3575 | } |
| 3576 | |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 3577 | private boolean isDragWidget(DragObject d) { |
| 3578 | return (d.dragInfo instanceof LauncherAppWidgetInfo || |
| 3579 | d.dragInfo instanceof PendingAddWidgetInfo); |
| 3580 | } |
| 3581 | private boolean isExternalDragWidget(DragObject d) { |
| 3582 | return d.dragSource != this && isDragWidget(d); |
| 3583 | } |
| 3584 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3585 | public void onDragOver(DragObject d) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3586 | // Skip drag over events while we are dragging over side pages |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 3587 | if (mInScrollArea || !transitionStateShouldAllowDrop()) return; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3588 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3589 | Rect r = new Rect(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3590 | CellLayout layout = null; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3591 | ItemInfo item = (ItemInfo) d.dragInfo; |
Sunny Goyal | 25611b1 | 2014-07-22 09:52:37 -0700 | [diff] [blame] | 3592 | if (item == null) { |
| 3593 | if (LauncherAppState.isDogfoodBuild()) { |
| 3594 | throw new NullPointerException("DragObject has null info"); |
| 3595 | } |
| 3596 | return; |
| 3597 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3598 | |
| 3599 | // Ensure that we have proper spans for the item that we are dropping |
| 3600 | if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found"); |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3601 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3602 | d.dragView, mDragViewVisualCenter); |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3603 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3604 | final View child = (mDragInfo == null) ? null : mDragInfo.cell; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3605 | // Identify whether we have dragged over a side page |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 3606 | if (workspaceInModalState()) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 3607 | if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3608 | if (isPointInSelfOverHotseat(d.x, d.y, r)) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3609 | layout = mLauncher.getHotseat().getLayout(); |
| 3610 | } |
| 3611 | } |
| 3612 | if (layout == null) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 3613 | layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false); |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3614 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3615 | if (layout != mDragTargetLayout) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3616 | setCurrentDropLayout(layout); |
| 3617 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3618 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3619 | boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3620 | if (isInSpringLoadedMode) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3621 | if (mLauncher.isHotseatLayout(layout)) { |
| 3622 | mSpringLoadedDragController.cancel(); |
| 3623 | } else { |
| 3624 | mSpringLoadedDragController.setAlarm(mDragTargetLayout); |
| 3625 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | } else { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3629 | // Test to see if we are over the hotseat otherwise just use the current page |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 3630 | if (mLauncher.getHotseat() != null && !isDragWidget(d)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3631 | if (isPointInSelfOverHotseat(d.x, d.y, r)) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3632 | layout = mLauncher.getHotseat().getLayout(); |
| 3633 | } |
| 3634 | } |
| 3635 | if (layout == null) { |
| 3636 | layout = getCurrentDropLayout(); |
| 3637 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3638 | if (layout != mDragTargetLayout) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3639 | setCurrentDropLayout(layout); |
| 3640 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | // Handle the drag over |
| 3645 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3646 | // We want the point to be mapped to the dragTarget. |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3647 | if (mLauncher.isHotseatLayout(mDragTargetLayout)) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3648 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3649 | } else { |
| 3650 | mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); |
| 3651 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3652 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3653 | ItemInfo info = (ItemInfo) d.dragInfo; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 3654 | |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3655 | int minSpanX = item.spanX; |
| 3656 | int minSpanY = item.spanY; |
| 3657 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3658 | minSpanX = item.minSpanX; |
| 3659 | minSpanY = item.minSpanY; |
| 3660 | } |
| 3661 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3662 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3663 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 3664 | mDragTargetLayout, mTargetCell); |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3665 | int reorderX = mTargetCell[0]; |
| 3666 | int reorderY = mTargetCell[1]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3667 | |
| 3668 | setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]); |
| 3669 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3670 | float targetCellDistance = mDragTargetLayout.getDistanceFromCell( |
| 3671 | mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); |
| 3672 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3673 | final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], |
| 3674 | mTargetCell[1]); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 3675 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3676 | manageFolderFeedback(info, mDragTargetLayout, mTargetCell, |
| 3677 | targetCellDistance, dragOverView); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3678 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3679 | boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int) |
| 3680 | mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX, |
| 3681 | item.spanY, child, mTargetCell); |
| 3682 | |
| 3683 | if (!nearestDropOccupied) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3684 | mDragTargetLayout.visualizeDropLocation(child, mDragOutline, |
| 3685 | (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], |
| 3686 | mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, |
| 3687 | d.dragView.getDragVisualizeOffset(), d.dragView.getDragRegion()); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3688 | } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER) |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3689 | && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX || |
| 3690 | mLastReorderY != reorderY)) { |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 3691 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3692 | int[] resultSpan = new int[2]; |
| 3693 | mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
| 3694 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY, |
| 3695 | child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT); |
| 3696 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3697 | // Otherwise, if we aren't adding to or creating a folder and there's no pending |
| 3698 | // reorder, then we schedule a reorder |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3699 | ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter, |
| 3700 | minSpanX, minSpanY, item.spanX, item.spanY, d.dragView, child); |
| 3701 | mReorderAlarm.setOnAlarmListener(listener); |
| 3702 | mReorderAlarm.setAlarm(REORDER_TIMEOUT); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER || |
| 3706 | !nearestDropOccupied) { |
| 3707 | if (mDragTargetLayout != null) { |
| 3708 | mDragTargetLayout.revertTempState(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3709 | } |
| 3710 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3711 | } |
| 3712 | } |
| 3713 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3714 | private void manageFolderFeedback(ItemInfo info, CellLayout targetLayout, |
| 3715 | int[] targetCell, float distance, View dragOverView) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3716 | boolean userFolderPending = willCreateUserFolder(info, targetLayout, targetCell, distance, |
| 3717 | false); |
| 3718 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3719 | if (mDragMode == DRAG_MODE_NONE && userFolderPending && |
| 3720 | !mFolderCreationAlarm.alarmPending()) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3721 | mFolderCreationAlarm.setOnAlarmListener(new |
| 3722 | FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1])); |
| 3723 | mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3724 | return; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | boolean willAddToFolder = |
| 3728 | willAddToExistingUserFolder(info, targetLayout, targetCell, distance); |
| 3729 | |
| 3730 | if (willAddToFolder && mDragMode == DRAG_MODE_NONE) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3731 | mDragOverFolderIcon = ((FolderIcon) dragOverView); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3732 | mDragOverFolderIcon.onDragEnter(info); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3733 | if (targetLayout != null) { |
| 3734 | targetLayout.clearDragOutlines(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3735 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3736 | setDragMode(DRAG_MODE_ADD_TO_FOLDER); |
| 3737 | return; |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 3738 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3739 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3740 | if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) { |
| 3741 | setDragMode(DRAG_MODE_NONE); |
| 3742 | } |
| 3743 | if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) { |
| 3744 | setDragMode(DRAG_MODE_NONE); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3745 | } |
| 3746 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3747 | return; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3750 | class FolderCreationAlarmListener implements OnAlarmListener { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3751 | CellLayout layout; |
| 3752 | int cellX; |
| 3753 | int cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3754 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3755 | public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { |
| 3756 | this.layout = layout; |
| 3757 | this.cellX = cellX; |
| 3758 | this.cellY = cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3762 | if (mDragFolderRingAnimator != null) { |
| 3763 | // This shouldn't happen ever, but just in case, make sure we clean up the mess. |
| 3764 | mDragFolderRingAnimator.animateToNaturalState(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3765 | } |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3766 | mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3767 | mDragFolderRingAnimator.setCell(cellX, cellY); |
| 3768 | mDragFolderRingAnimator.setCellLayout(layout); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3769 | mDragFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3770 | layout.showFolderAccept(mDragFolderRingAnimator); |
| 3771 | layout.clearDragOutlines(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3772 | setDragMode(DRAG_MODE_CREATE_FOLDER); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | class ReorderAlarmListener implements OnAlarmListener { |
| 3777 | float[] dragViewCenter; |
| 3778 | int minSpanX, minSpanY, spanX, spanY; |
| 3779 | DragView dragView; |
| 3780 | View child; |
| 3781 | |
| 3782 | public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX, |
| 3783 | int spanY, DragView dragView, View child) { |
| 3784 | this.dragViewCenter = dragViewCenter; |
| 3785 | this.minSpanX = minSpanX; |
| 3786 | this.minSpanY = minSpanY; |
| 3787 | this.spanX = spanX; |
| 3788 | this.spanY = spanY; |
| 3789 | this.child = child; |
| 3790 | this.dragView = dragView; |
| 3791 | } |
| 3792 | |
| 3793 | public void onAlarm(Alarm alarm) { |
| 3794 | int[] resultSpan = new int[2]; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3795 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3796 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout, |
| 3797 | mTargetCell); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3798 | mLastReorderX = mTargetCell[0]; |
| 3799 | mLastReorderY = mTargetCell[1]; |
| 3800 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3801 | mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3802 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 3803 | child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER); |
| 3804 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3805 | if (mTargetCell[0] < 0 || mTargetCell[1] < 0) { |
| 3806 | mDragTargetLayout.revertTempState(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3807 | } else { |
| 3808 | setDragMode(DRAG_MODE_REORDER); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3809 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3810 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3811 | boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY; |
| 3812 | mDragTargetLayout.visualizeDropLocation(child, mDragOutline, |
| 3813 | (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], |
| 3814 | mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, |
| 3815 | dragView.getDragVisualizeOffset(), dragView.getDragRegion()); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3816 | } |
| 3817 | } |
| 3818 | |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3819 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3820 | public void getHitRectRelativeToDragLayer(Rect outRect) { |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3821 | // We want the workspace to have the whole area of the display (it will find the correct |
| 3822 | // cell layout to drop to in the existing drag/drop logic. |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3823 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect); |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3824 | } |
| 3825 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3826 | /** |
| 3827 | * Add the item specified by dragInfo to the given layout. |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3828 | * @return true if successful |
| 3829 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 3830 | public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) { |
| 3831 | if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) { |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 3832 | onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3833 | return true; |
| 3834 | } |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 3835 | mLauncher.showOutOfSpaceMessage(mLauncher.isHotseatLayout(layout)); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3836 | return false; |
| 3837 | } |
| 3838 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3839 | private void onDropExternal(int[] touchXY, Object dragInfo, |
| 3840 | CellLayout cellLayout, boolean insertAtFirst) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3841 | onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 3844 | /** |
| 3845 | * Drop an item that didn't originate on one of the workspace screens. |
| 3846 | * It may have come from Launcher (e.g. from all apps or customize), or it may have |
| 3847 | * come from another app altogether. |
| 3848 | * |
| 3849 | * NOTE: This can also be called when we are outside of a drag event, when we want |
| 3850 | * to add an item to one of the workspace screens. |
| 3851 | */ |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3852 | private void onDropExternal(final int[] touchXY, final Object dragInfo, |
| 3853 | final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { |
| 3854 | final Runnable exitSpringLoadedRunnable = new Runnable() { |
| 3855 | @Override |
| 3856 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 3857 | mLauncher.exitSpringLoadedDragModeDelayed(true, |
| 3858 | Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3859 | } |
| 3860 | }; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3861 | |
| 3862 | ItemInfo info = (ItemInfo) dragInfo; |
| 3863 | int spanX = info.spanX; |
| 3864 | int spanY = info.spanY; |
| 3865 | if (mDragInfo != null) { |
| 3866 | spanX = mDragInfo.spanX; |
| 3867 | spanY = mDragInfo.spanY; |
| 3868 | } |
| 3869 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3870 | final long container = mLauncher.isHotseatLayout(cellLayout) ? |
| 3871 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 3872 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3873 | final long screenId = getIdForScreen(cellLayout); |
| 3874 | if (!mLauncher.isHotseatLayout(cellLayout) |
| 3875 | && screenId != getScreenIdForPageIndex(mCurrentPage) |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3876 | && mState != State.SPRING_LOADED) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3877 | snapToScreenId(screenId, null); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3878 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3879 | |
| 3880 | if (info instanceof PendingAddItemInfo) { |
| 3881 | final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo; |
| 3882 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3883 | boolean findNearestVacantCell = true; |
| 3884 | if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 3885 | mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, |
| 3886 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3887 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3888 | mDragViewVisualCenter[1], mTargetCell); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3889 | if (willCreateUserFolder((ItemInfo) d.dragInfo, cellLayout, mTargetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3890 | distance, true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo, |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3891 | cellLayout, mTargetCell, distance)) { |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3892 | findNearestVacantCell = false; |
| 3893 | } |
| 3894 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3895 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3896 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3897 | boolean updateWidgetSize = false; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3898 | if (findNearestVacantCell) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3899 | int minSpanX = item.spanX; |
| 3900 | int minSpanY = item.spanY; |
| 3901 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3902 | minSpanX = item.minSpanX; |
| 3903 | minSpanY = item.minSpanY; |
| 3904 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3905 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3906 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3907 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, |
| 3908 | null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3909 | |
| 3910 | if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) { |
| 3911 | updateWidgetSize = true; |
| 3912 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3913 | item.spanX = resultSpan[0]; |
| 3914 | item.spanY = resultSpan[1]; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3917 | Runnable onAnimationCompleteRunnable = new Runnable() { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3918 | @Override |
| 3919 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 3920 | // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when |
| 3921 | // adding an item that may not be dropped right away (due to a config activity) |
| 3922 | // we defer the removal until the activity returns. |
| 3923 | deferRemoveExtraEmptyScreen(); |
| 3924 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3925 | // When dragging and dropping from customization tray, we deal with creating |
| 3926 | // widgets/shortcuts/folders in a slightly different way |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 3927 | mLauncher.addPendingItem(pendingInfo, container, screenId, mTargetCell, |
| 3928 | item.spanX, item.spanY); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3929 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3930 | }; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 3931 | boolean isWidget = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 3932 | || pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 3933 | |
| 3934 | View finalView = isWidget ? ((PendingAddWidgetInfo) pendingInfo).boundWidget : null; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3935 | |
| 3936 | if (finalView instanceof AppWidgetHostView && updateWidgetSize) { |
| 3937 | AppWidgetHostView awhv = (AppWidgetHostView) finalView; |
| 3938 | AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, item.spanX, |
| 3939 | item.spanY); |
| 3940 | } |
| 3941 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3942 | int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 3943 | if (isWidget && ((PendingAddWidgetInfo) pendingInfo).info != null && |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3944 | ((PendingAddWidgetInfo) pendingInfo).info.configure != null) { |
| 3945 | animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN; |
| 3946 | } |
| 3947 | animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, |
| 3948 | animationStyle, finalView, true); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 3949 | } else { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3950 | // This is for other drag/drop cases, like dragging from All Apps |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3951 | View view = null; |
| 3952 | |
| 3953 | switch (info.itemType) { |
| 3954 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 3955 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 3956 | if (info.container == NO_ID && info instanceof AppInfo) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3957 | // Came from all apps -- make a copy |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 3958 | info = ((AppInfo) info).makeShortcut(); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3959 | } |
| 3960 | view = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 3961 | (ShortcutInfo) info); |
| 3962 | break; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3963 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3964 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, |
| 3965 | (FolderInfo) info, mIconCache); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3966 | break; |
| 3967 | default: |
| 3968 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 3969 | } |
| 3970 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3971 | // First we find the cell nearest to point at which the item is |
| 3972 | // dropped, without any consideration to whether there is an item there. |
| 3973 | if (touchXY != null) { |
| 3974 | mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, |
| 3975 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3976 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3977 | mDragViewVisualCenter[1], mTargetCell); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3978 | d.postAnimationRunnable = exitSpringLoadedRunnable; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3979 | if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance, |
| 3980 | true, d.dragView, d.postAnimationRunnable)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3981 | return; |
| 3982 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3983 | if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d, |
| 3984 | true)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3985 | return; |
| 3986 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 3987 | } |
| 3988 | |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 3989 | if (touchXY != null) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3990 | // when dragging and dropping, just find the closest free spot |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3991 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3992 | (int) mDragViewVisualCenter[1], 1, 1, 1, 1, |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 3993 | null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3994 | } else { |
| 3995 | cellLayout.findCellForSpan(mTargetCell, 1, 1); |
| 3996 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 3997 | // Add the item to DB before adding to screen ensures that the container and other |
| 3998 | // values of the info is properly updated. |
| 3999 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, |
| 4000 | mTargetCell[0], mTargetCell[1]); |
| 4001 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4002 | addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4003 | info.spanY, insertAtFirst); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 4004 | cellLayout.onDropChild(view); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4005 | cellLayout.getShortcutsAndWidgets().measureChild(view); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 4006 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 4007 | if (d.dragView != null) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4008 | // We wrap the animation call in the temporary set and reset of the current |
| 4009 | // cellLayout to its final transform -- this means we animate the drag view to |
| 4010 | // the correct final location. |
| 4011 | setFinalTransitionTransform(cellLayout); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 4012 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, |
Adam Cohen | 28f852a | 2013-10-15 14:34:05 -0700 | [diff] [blame] | 4013 | exitSpringLoadedRunnable, this); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4014 | resetTransitionTransform(cellLayout); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 4015 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 4016 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4017 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 4018 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4019 | public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4020 | int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo.spanX, |
| 4021 | widgetInfo.spanY, widgetInfo, false); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4022 | int visibility = layout.getVisibility(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4023 | layout.setVisibility(VISIBLE); |
| 4024 | |
| 4025 | int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY); |
| 4026 | int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY); |
| 4027 | Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1], |
| 4028 | Bitmap.Config.ARGB_8888); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 4029 | mCanvas.setBitmap(b); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4030 | |
| 4031 | layout.measure(width, height); |
| 4032 | layout.layout(0, 0, unScaledSize[0], unScaledSize[1]); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 4033 | layout.draw(mCanvas); |
| 4034 | mCanvas.setBitmap(null); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4035 | layout.setVisibility(visibility); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4036 | return b; |
| 4037 | } |
| 4038 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4039 | private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, |
Adam Cohen | 9d5b7d8 | 2012-05-09 18:00:44 -0700 | [diff] [blame] | 4040 | DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4041 | boolean external, boolean scale) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4042 | // Now we animate the dragView, (ie. the widget or shortcut preview) into its final |
| 4043 | // location and size on the home screen. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4044 | int spanX = info.spanX; |
| 4045 | int spanY = info.spanY; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4046 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4047 | Rect r = estimateItemPosition(layout, info, targetCell[0], targetCell[1], spanX, spanY); |
| 4048 | loc[0] = r.left; |
| 4049 | loc[1] = r.top; |
| 4050 | |
| 4051 | setFinalTransitionTransform(layout); |
| 4052 | float cellLayoutScale = |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4053 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4054 | resetTransitionTransform(layout); |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4055 | |
| 4056 | float dragViewScaleX; |
| 4057 | float dragViewScaleY; |
| 4058 | if (scale) { |
| 4059 | dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth(); |
| 4060 | dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight(); |
| 4061 | } else { |
| 4062 | dragViewScaleX = 1f; |
| 4063 | dragViewScaleY = 1f; |
| 4064 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4065 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4066 | // The animation will scale the dragView about its center, so we need to center about |
| 4067 | // the final location. |
| 4068 | loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2; |
| 4069 | loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; |
| 4070 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4071 | scaleXY[0] = dragViewScaleX * cellLayoutScale; |
| 4072 | scaleXY[1] = dragViewScaleY * cellLayoutScale; |
| 4073 | } |
| 4074 | |
| 4075 | public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, DragView dragView, |
| 4076 | final Runnable onCompleteRunnable, int animationType, final View finalView, |
| 4077 | boolean external) { |
| 4078 | Rect from = new Rect(); |
| 4079 | mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from); |
| 4080 | |
| 4081 | int[] finalPos = new int[2]; |
| 4082 | float scaleXY[] = new float[2]; |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4083 | boolean scalePreview = !(info instanceof PendingAddShortcutInfo); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4084 | getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell, |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4085 | external, scalePreview); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4086 | |
| 4087 | Resources res = mLauncher.getResources(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 4088 | final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4089 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4090 | // In the case where we've prebound the widget, we remove it from the DragLayer |
| 4091 | if (finalView instanceof AppWidgetHostView && external) { |
| 4092 | mLauncher.getDragLayer().removeView(finalView); |
| 4093 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 4094 | |
| 4095 | boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET || |
| 4096 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4097 | if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) { |
| 4098 | Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4099 | dragView.setCrossFadeBitmap(crossFadeBitmap); |
| 4100 | dragView.crossFade((int) (duration * 0.8f)); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 4101 | } else if (isWidget && external) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4102 | scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4103 | } |
| 4104 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4105 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 4106 | if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4107 | mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4108 | DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration); |
| 4109 | } else { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4110 | int endStyle; |
| 4111 | if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) { |
| 4112 | endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE; |
| 4113 | } else { |
| 4114 | endStyle = DragLayer.ANIMATION_END_DISAPPEAR;; |
| 4115 | } |
| 4116 | |
| 4117 | Runnable onComplete = new Runnable() { |
| 4118 | @Override |
| 4119 | public void run() { |
| 4120 | if (finalView != null) { |
| 4121 | finalView.setVisibility(VISIBLE); |
| 4122 | } |
| 4123 | if (onCompleteRunnable != null) { |
| 4124 | onCompleteRunnable.run(); |
| 4125 | } |
| 4126 | } |
| 4127 | }; |
| 4128 | dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0], |
| 4129 | finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle, |
| 4130 | duration, this); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4131 | } |
| 4132 | } |
| 4133 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4134 | public void setFinalTransitionTransform(CellLayout layout) { |
| 4135 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4136 | mCurrentScale = getScaleX(); |
| 4137 | setScaleX(mNewScale); |
| 4138 | setScaleY(mNewScale); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | public void resetTransitionTransform(CellLayout layout) { |
| 4142 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4143 | setScaleX(mCurrentScale); |
| 4144 | setScaleY(mCurrentScale); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4148 | /** |
| 4149 | * Return the current {@link CellLayout}, correctly picking the destination |
| 4150 | * screen while a scroll is in progress. |
| 4151 | */ |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 4152 | public CellLayout getCurrentDropLayout() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 4153 | return (CellLayout) getChildAt(getNextPage()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4156 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4157 | * Return the current CellInfo describing our current drag; this method exists |
| 4158 | * so that Launcher can sync this object with the correct info when the activity is created/ |
| 4159 | * destroyed |
| 4160 | * |
| 4161 | */ |
| 4162 | public CellLayout.CellInfo getDragInfo() { |
| 4163 | return mDragInfo; |
| 4164 | } |
| 4165 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 4166 | public int getCurrentPageOffsetFromCustomContent() { |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 4167 | return getNextPage() - numCustomPages(); |
| 4168 | } |
| 4169 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4170 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4171 | * Calculate the nearest cell where the given object would be dropped. |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4172 | * |
| 4173 | * pixelX and pixelY should be in the coordinate system of layout |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4174 | */ |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4175 | private int[] findNearestArea(int pixelX, int pixelY, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4176 | int spanX, int spanY, CellLayout layout, int[] recycle) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4177 | return layout.findNearestArea( |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4178 | pixelX, pixelY, spanX, spanY, recycle); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 4181 | void setup(DragController dragController) { |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 4182 | mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 4183 | mDragController = dragController; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4184 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4185 | // hardware layers on children are enabled on startup, but should be disabled until |
| 4186 | // needed |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 4187 | updateChildrenLayersEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 4190 | /** |
| 4191 | * Called at the end of a drag which originated on the workspace. |
| 4192 | */ |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4193 | public void onDropCompleted(final View target, final DragObject d, |
| 4194 | final boolean isFlingToDelete, final boolean success) { |
| 4195 | if (mDeferDropAfterUninstall) { |
| 4196 | mDeferredAction = new Runnable() { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 4197 | public void run() { |
| 4198 | onDropCompleted(target, d, isFlingToDelete, success); |
| 4199 | mDeferredAction = null; |
| 4200 | } |
| 4201 | }; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4202 | return; |
| 4203 | } |
| 4204 | |
| 4205 | boolean beingCalledAfterUninstall = mDeferredAction != null; |
| 4206 | |
| 4207 | if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) { |
| 4208 | if (target != this && mDragInfo != null) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 4209 | removeWorkspaceItem(mDragInfo.cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4210 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 4211 | } else if (mDragInfo != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4212 | CellLayout cellLayout; |
| 4213 | if (mLauncher.isHotseatLayout(target)) { |
| 4214 | cellLayout = mLauncher.getHotseat().getLayout(); |
| 4215 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4216 | cellLayout = getScreenWithId(mDragInfo.screenId); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4217 | } |
Jorim Jaggi | eedb00a | 2014-01-13 13:45:07 -0800 | [diff] [blame] | 4218 | if (cellLayout == null && LauncherAppState.isDogfoodBuild()) { |
| 4219 | throw new RuntimeException("Invalid state: cellLayout == null in " |
| 4220 | + "Workspace#onDropCompleted. Please file a bug. "); |
| 4221 | } |
| 4222 | if (cellLayout != null) { |
| 4223 | cellLayout.onDropChild(mDragInfo.cell); |
| 4224 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4225 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4226 | if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful)) |
| 4227 | && mDragInfo.cell != null) { |
| 4228 | mDragInfo.cell.setVisibility(VISIBLE); |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 4229 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 4230 | mDragOutline = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4231 | mDragInfo = null; |
| 4232 | } |
| 4233 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 4234 | /** |
| 4235 | * For opposite operation. See {@link #addInScreen}. |
| 4236 | */ |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 4237 | public void removeWorkspaceItem(View v) { |
| 4238 | CellLayout parentCell = getParentCellLayoutForView(v); |
| 4239 | if (parentCell != null) { |
| 4240 | parentCell.removeView(v); |
| 4241 | } else if (LauncherAppState.isDogfoodBuild()) { |
| 4242 | throw new NullPointerException("mDragInfo.cell has null parent"); |
| 4243 | } |
| 4244 | if (v instanceof DropTarget) { |
| 4245 | mDragController.removeDropTarget((DropTarget) v); |
| 4246 | } |
| 4247 | } |
| 4248 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4249 | public void deferCompleteDropAfterUninstallActivity() { |
| 4250 | mDeferDropAfterUninstall = true; |
| 4251 | } |
| 4252 | |
| 4253 | /// maybe move this into a smaller part |
| 4254 | public void onUninstallActivityReturned(boolean success) { |
| 4255 | mDeferDropAfterUninstall = false; |
| 4256 | mUninstallSuccessful = success; |
| 4257 | if (mDeferredAction != null) { |
| 4258 | mDeferredAction.run(); |
| 4259 | } |
| 4260 | } |
| 4261 | |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 4262 | void updateItemLocationsInDatabase(CellLayout cl) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4263 | int count = cl.getShortcutsAndWidgets().getChildCount(); |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4264 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4265 | long screenId = getIdForScreen(cl); |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4266 | int container = Favorites.CONTAINER_DESKTOP; |
| 4267 | |
| 4268 | if (mLauncher.isHotseatLayout(cl)) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4269 | screenId = -1; |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4270 | container = Favorites.CONTAINER_HOTSEAT; |
| 4271 | } |
| 4272 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4273 | for (int i = 0; i < count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4274 | View v = cl.getShortcutsAndWidgets().getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4275 | ItemInfo info = (ItemInfo) v.getTag(); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 4276 | // Null check required as the AllApps button doesn't have an item info |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 4277 | if (info != null && info.requiresDbUpdate) { |
| 4278 | info.requiresDbUpdate = false; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4279 | LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, info.cellX, |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 4280 | info.cellY, info.spanX, info.spanY); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 4281 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4282 | } |
| 4283 | } |
| 4284 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4285 | void saveWorkspaceToDb() { |
| 4286 | saveWorkspaceScreenToDb((CellLayout) mLauncher.getHotseat().getLayout()); |
| 4287 | int count = getChildCount(); |
| 4288 | for (int i = 0; i < count; i++) { |
| 4289 | CellLayout cl = (CellLayout) getChildAt(i); |
| 4290 | saveWorkspaceScreenToDb(cl); |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | void saveWorkspaceScreenToDb(CellLayout cl) { |
| 4295 | int count = cl.getShortcutsAndWidgets().getChildCount(); |
| 4296 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4297 | long screenId = getIdForScreen(cl); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4298 | int container = Favorites.CONTAINER_DESKTOP; |
| 4299 | |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 4300 | Hotseat hotseat = mLauncher.getHotseat(); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4301 | if (mLauncher.isHotseatLayout(cl)) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4302 | screenId = -1; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4303 | container = Favorites.CONTAINER_HOTSEAT; |
| 4304 | } |
| 4305 | |
| 4306 | for (int i = 0; i < count; i++) { |
| 4307 | View v = cl.getShortcutsAndWidgets().getChildAt(i); |
| 4308 | ItemInfo info = (ItemInfo) v.getTag(); |
| 4309 | // Null check required as the AllApps button doesn't have an item info |
| 4310 | if (info != null) { |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 4311 | int cellX = info.cellX; |
| 4312 | int cellY = info.cellY; |
| 4313 | if (container == Favorites.CONTAINER_HOTSEAT) { |
| 4314 | cellX = hotseat.getCellXFromOrder((int) info.screenId); |
| 4315 | cellY = hotseat.getCellYFromOrder((int) info.screenId); |
| 4316 | } |
| 4317 | LauncherModel.addItemToDatabase(mLauncher, info, container, screenId, cellX, |
| 4318 | cellY, false); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4319 | } |
| 4320 | if (v instanceof FolderIcon) { |
| 4321 | FolderIcon fi = (FolderIcon) v; |
| 4322 | fi.getFolder().addItemLocationsInDatabase(); |
| 4323 | } |
| 4324 | } |
| 4325 | } |
| 4326 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4327 | @Override |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 4328 | public float getIntrinsicIconScaleFactor() { |
| 4329 | return 1f; |
| 4330 | } |
| 4331 | |
| 4332 | @Override |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 4333 | public boolean supportsFlingToDelete() { |
| 4334 | return true; |
| 4335 | } |
| 4336 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4337 | @Override |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 4338 | public boolean supportsAppInfoDropTarget() { |
| 4339 | return false; |
| 4340 | } |
| 4341 | |
| 4342 | @Override |
| 4343 | public boolean supportsDeleteDropTarget() { |
| 4344 | return true; |
| 4345 | } |
| 4346 | |
| 4347 | @Override |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4348 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { |
| 4349 | // Do nothing |
| 4350 | } |
| 4351 | |
| 4352 | @Override |
| 4353 | public void onFlingToDeleteCompleted() { |
| 4354 | // Do nothing |
| 4355 | } |
| 4356 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4357 | public boolean isDropEnabled() { |
| 4358 | return true; |
| 4359 | } |
| 4360 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4361 | @Override |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4362 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 4363 | // We don't dispatch restoreInstanceState to our children using this code path. |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 4364 | // Some pages will be restored immediately as their items are bound immediately, and |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4365 | // others we will need to wait until after their items are bound. |
| 4366 | mSavedStates = container; |
| 4367 | } |
| 4368 | |
| 4369 | public void restoreInstanceStateForChild(int child) { |
| 4370 | if (mSavedStates != null) { |
| 4371 | mRestoredPages.add(child); |
| 4372 | CellLayout cl = (CellLayout) getChildAt(child); |
Adam Cohen | b0ee081 | 2013-12-03 10:51:45 -0800 | [diff] [blame] | 4373 | if (cl != null) { |
| 4374 | cl.restoreInstanceState(mSavedStates); |
| 4375 | } |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | public void restoreInstanceStateForRemainingPages() { |
| 4380 | int count = getChildCount(); |
| 4381 | for (int i = 0; i < count; i++) { |
| 4382 | if (!mRestoredPages.contains(i)) { |
| 4383 | restoreInstanceStateForChild(i); |
| 4384 | } |
| 4385 | } |
| 4386 | mRestoredPages.clear(); |
Winson Chung | d8e596d | 2013-10-21 17:14:12 -0700 | [diff] [blame] | 4387 | mSavedStates = null; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4391 | public void scrollLeft() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 4392 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4393 | super.scrollLeft(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4394 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 4395 | Folder openFolder = getOpenFolder(); |
| 4396 | if (openFolder != null) { |
| 4397 | openFolder.completeDragExit(); |
| 4398 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4399 | } |
| 4400 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4401 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4402 | public void scrollRight() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 4403 | if (!workspaceInModalState() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4404 | super.scrollRight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4405 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 4406 | Folder openFolder = getOpenFolder(); |
| 4407 | if (openFolder != null) { |
| 4408 | openFolder.completeDragExit(); |
| 4409 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4412 | @Override |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4413 | public boolean onEnterScrollArea(int x, int y, int direction) { |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4414 | // Ignore the scroll area if we are dragging over the hot seat |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 4415 | boolean isPortrait = !LauncherAppState.isScreenLandscape(getContext()); |
Winson Chung | 10bfc6e | 2012-03-28 15:41:26 -0700 | [diff] [blame] | 4416 | if (mLauncher.getHotseat() != null && isPortrait) { |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4417 | Rect r = new Rect(); |
| 4418 | mLauncher.getHotseat().getHitRect(r); |
| 4419 | if (r.contains(x, y)) { |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4420 | return false; |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4424 | boolean result = false; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 4425 | if (!workspaceInModalState() && !mIsSwitchingState && getOpenFolder() == null) { |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4426 | mInScrollArea = true; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 4427 | |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 4428 | final int page = getNextPage() + |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4429 | (direction == DragController.SCROLL_LEFT ? -1 : 1); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4430 | // We always want to exit the current layout to ensure parity of enter / exit |
| 4431 | setCurrentDropLayout(null); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 4432 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4433 | if (0 <= page && page < getChildCount()) { |
Winson Chung | 482a5b6 | 2013-07-31 17:19:51 -0700 | [diff] [blame] | 4434 | // Ensure that we are not dragging over to the custom content screen |
| 4435 | if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) { |
| 4436 | return false; |
| 4437 | } |
| 4438 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4439 | CellLayout layout = (CellLayout) getChildAt(page); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4440 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4441 | |
| 4442 | // Workspace is responsible for drawing the edge glow on adjacent pages, |
| 4443 | // so we need to redraw the workspace when this may have changed. |
| 4444 | invalidate(); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4445 | result = true; |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4446 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4447 | } |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4448 | return result; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | @Override |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4452 | public boolean onExitScrollArea() { |
| 4453 | boolean result = false; |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4454 | if (mInScrollArea) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4455 | invalidate(); |
| 4456 | CellLayout layout = getCurrentDropLayout(); |
| 4457 | setCurrentDropLayout(layout); |
| 4458 | setCurrentDragOverlappingLayout(layout); |
| 4459 | |
Adam Cohen | 8a18afc | 2011-12-13 15:57:01 -0800 | [diff] [blame] | 4460 | result = true; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4461 | mInScrollArea = false; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4462 | } |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4463 | return result; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4466 | private void onResetScrollArea() { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4467 | setCurrentDragOverlappingLayout(null); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4468 | mInScrollArea = false; |
| 4469 | } |
| 4470 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4471 | /** |
| 4472 | * Returns a specific CellLayout |
| 4473 | */ |
| 4474 | CellLayout getParentCellLayoutForView(View v) { |
| 4475 | ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts(); |
| 4476 | for (CellLayout layout : layouts) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4477 | if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4478 | return layout; |
| 4479 | } |
| 4480 | } |
| 4481 | return null; |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * Returns a list of all the CellLayouts in the workspace. |
| 4486 | */ |
| 4487 | ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() { |
| 4488 | ArrayList<CellLayout> layouts = new ArrayList<CellLayout>(); |
| 4489 | int screenCount = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4490 | for (int screen = 0; screen < screenCount; screen++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4491 | layouts.add(((CellLayout) getChildAt(screen))); |
| 4492 | } |
| 4493 | if (mLauncher.getHotseat() != null) { |
| 4494 | layouts.add(mLauncher.getHotseat().getLayout()); |
| 4495 | } |
| 4496 | return layouts; |
| 4497 | } |
| 4498 | |
| 4499 | /** |
| 4500 | * We should only use this to search for specific children. Do not use this method to modify |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4501 | * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from |
| 4502 | * the hotseat and workspace pages |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4503 | */ |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4504 | ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() { |
| 4505 | ArrayList<ShortcutAndWidgetContainer> childrenLayouts = |
| 4506 | new ArrayList<ShortcutAndWidgetContainer>(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4507 | int screenCount = getChildCount(); |
| 4508 | for (int screen = 0; screen < screenCount; screen++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4509 | childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4510 | } |
| 4511 | if (mLauncher.getHotseat() != null) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4512 | childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4513 | } |
| 4514 | return childrenLayouts; |
| 4515 | } |
| 4516 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4517 | public Folder getFolderForTag(final Object tag) { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 4518 | return (Folder) getFirstMatch(new ItemOperator() { |
| 4519 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4520 | @Override |
| 4521 | public boolean evaluate(ItemInfo info, View v, View parent) { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 4522 | return (v instanceof Folder) && (((Folder) v).getInfo() == tag) |
| 4523 | && ((Folder) v).getInfo().opened; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4524 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4525 | }); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4528 | public View getViewForTag(final Object tag) { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 4529 | return getFirstMatch(new ItemOperator() { |
| 4530 | |
| 4531 | @Override |
| 4532 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4533 | return info == tag; |
| 4534 | } |
| 4535 | }); |
| 4536 | } |
| 4537 | |
| 4538 | public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) { |
| 4539 | return (LauncherAppWidgetHostView) getFirstMatch(new ItemOperator() { |
| 4540 | |
| 4541 | @Override |
| 4542 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4543 | return (info instanceof LauncherAppWidgetInfo) && |
| 4544 | ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId; |
| 4545 | } |
| 4546 | }); |
| 4547 | } |
| 4548 | |
| 4549 | private View getFirstMatch(final ItemOperator operator) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4550 | final View[] value = new View[1]; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4551 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4552 | @Override |
| 4553 | public boolean evaluate(ItemInfo info, View v, View parent) { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 4554 | if (operator.evaluate(info, v, parent)) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4555 | value[0] = v; |
| 4556 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4557 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4558 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4559 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4560 | }); |
| 4561 | return value[0]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4562 | } |
| 4563 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4564 | void clearDropTargets() { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4565 | mapOverItems(MAP_NO_RECURSE, new ItemOperator() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4566 | @Override |
| 4567 | public boolean evaluate(ItemInfo info, View v, View parent) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4568 | if (v instanceof DropTarget) { |
| 4569 | mDragController.removeDropTarget((DropTarget) v); |
| 4570 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4571 | // not done, process all the shortcuts |
| 4572 | return false; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4573 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4574 | }); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4575 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4576 | |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 4577 | public void disableShortcutsByPackageName(final ArrayList<String> packages, |
| 4578 | final UserHandleCompat user, final int reason) { |
| 4579 | final HashSet<String> packageNames = new HashSet<String>(); |
| 4580 | packageNames.addAll(packages); |
| 4581 | |
| 4582 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 4583 | @Override |
| 4584 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4585 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView) { |
| 4586 | ShortcutInfo shortcutInfo = (ShortcutInfo) info; |
| 4587 | ComponentName cn = shortcutInfo.getTargetComponent(); |
| 4588 | if (user.equals(shortcutInfo.user) && cn != null |
| 4589 | && packageNames.contains(cn.getPackageName())) { |
| 4590 | shortcutInfo.isDisabled |= reason; |
| 4591 | BubbleTextView shortcut = (BubbleTextView) v; |
| 4592 | shortcut.applyFromShortcutInfo(shortcutInfo, mIconCache, true, false); |
| 4593 | |
| 4594 | if (parent != null) { |
| 4595 | parent.invalidate(); |
| 4596 | } |
| 4597 | } |
| 4598 | } |
| 4599 | // process all the shortcuts |
| 4600 | return false; |
| 4601 | } |
| 4602 | }); |
| 4603 | } |
| 4604 | |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4605 | // Removes ALL items that match a given package name, this is usually called when a package |
| 4606 | // has been removed and we want to remove all components (widgets, shortcuts, apps) that |
| 4607 | // belong to that package. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4608 | void removeItemsByPackageName(final ArrayList<String> packages, final UserHandleCompat user) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4609 | final HashSet<String> packageNames = new HashSet<String>(); |
Winson Chung | cd81073 | 2012-06-18 16:45:43 -0700 | [diff] [blame] | 4610 | packageNames.addAll(packages); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 4611 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4612 | // Filter out all the ItemInfos that this is going to affect |
| 4613 | final HashSet<ItemInfo> infos = new HashSet<ItemInfo>(); |
| 4614 | final HashSet<ComponentName> cns = new HashSet<ComponentName>(); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4615 | ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); |
| 4616 | for (CellLayout layoutParent : cellLayouts) { |
| 4617 | ViewGroup layout = layoutParent.getShortcutsAndWidgets(); |
| 4618 | int childCount = layout.getChildCount(); |
| 4619 | for (int i = 0; i < childCount; ++i) { |
| 4620 | View view = layout.getChildAt(i); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4621 | infos.add((ItemInfo) view.getTag()); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4622 | } |
| 4623 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4624 | LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { |
| 4625 | @Override |
| 4626 | public boolean filterItem(ItemInfo parent, ItemInfo info, |
| 4627 | ComponentName cn) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4628 | if (packageNames.contains(cn.getPackageName()) |
| 4629 | && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4630 | cns.add(cn); |
| 4631 | return true; |
| 4632 | } |
| 4633 | return false; |
| 4634 | } |
| 4635 | }; |
| 4636 | LauncherModel.filterItemInfos(infos, filter); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4637 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4638 | // Remove the affected components |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4639 | removeItemsByComponentName(cns, user); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4640 | } |
| 4641 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4642 | /** |
| 4643 | * Removes items that match the item info specified. When applications are removed |
| 4644 | * as a part of an update, this is called to ensure that other widgets and application |
| 4645 | * shortcuts are not removed. |
| 4646 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4647 | void removeItemsByComponentName(final HashSet<ComponentName> componentNames, |
| 4648 | final UserHandleCompat user) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4649 | ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); |
| 4650 | for (final CellLayout layoutParent: cellLayouts) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4651 | final ViewGroup layout = layoutParent.getShortcutsAndWidgets(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 4652 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4653 | final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>(); |
| 4654 | for (int j = 0; j < layout.getChildCount(); j++) { |
| 4655 | final View view = layout.getChildAt(j); |
| 4656 | children.put((ItemInfo) view.getTag(), view); |
| 4657 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 4658 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4659 | final ArrayList<View> childrenToRemove = new ArrayList<View>(); |
| 4660 | final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove = |
| 4661 | new HashMap<FolderInfo, ArrayList<ShortcutInfo>>(); |
| 4662 | LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { |
| 4663 | @Override |
| 4664 | public boolean filterItem(ItemInfo parent, ItemInfo info, |
| 4665 | ComponentName cn) { |
| 4666 | if (parent instanceof FolderInfo) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4667 | if (componentNames.contains(cn) && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4668 | FolderInfo folder = (FolderInfo) parent; |
| 4669 | ArrayList<ShortcutInfo> appsToRemove; |
| 4670 | if (folderAppsToRemove.containsKey(folder)) { |
| 4671 | appsToRemove = folderAppsToRemove.get(folder); |
| 4672 | } else { |
| 4673 | appsToRemove = new ArrayList<ShortcutInfo>(); |
| 4674 | folderAppsToRemove.put(folder, appsToRemove); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 4675 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4676 | appsToRemove.add((ShortcutInfo) info); |
| 4677 | return true; |
| 4678 | } |
| 4679 | } else { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4680 | if (componentNames.contains(cn) && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4681 | childrenToRemove.add(children.get(info)); |
| 4682 | return true; |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 4683 | } |
| 4684 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4685 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4686 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4687 | }; |
| 4688 | LauncherModel.filterItemInfos(children.keySet(), filter); |
| 4689 | |
| 4690 | // Remove all the apps from their folders |
| 4691 | for (FolderInfo folder : folderAppsToRemove.keySet()) { |
| 4692 | ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder); |
| 4693 | for (ShortcutInfo info : appsToRemove) { |
| 4694 | folder.remove(info); |
| 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | // Remove all the other children |
| 4699 | for (View child : childrenToRemove) { |
| 4700 | // Note: We can not remove the view directly from CellLayoutChildren as this |
| 4701 | // does not re-mark the spaces as unoccupied. |
| 4702 | layoutParent.removeViewInLayout(child); |
| 4703 | if (child instanceof DropTarget) { |
| 4704 | mDragController.removeDropTarget((DropTarget) child); |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | if (childrenToRemove.size() > 0) { |
| 4709 | layout.requestLayout(); |
| 4710 | layout.invalidate(); |
| 4711 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4712 | } |
Winson Chung | 2efec4e | 2012-05-03 12:31:48 -0700 | [diff] [blame] | 4713 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4714 | // Strip all the empty screens |
| 4715 | stripEmptyScreens(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4716 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4717 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4718 | interface ItemOperator { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4719 | /** |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4720 | * Process the next itemInfo, possibly with side-effect on {@link ItemOperator#value}. |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4721 | * |
| 4722 | * @param info info for the shortcut |
| 4723 | * @param view view for the shortcut |
| 4724 | * @param parent containing folder, or null |
| 4725 | * @return true if done, false to continue the map |
| 4726 | */ |
| 4727 | public boolean evaluate(ItemInfo info, View view, View parent); |
| 4728 | } |
| 4729 | |
| 4730 | /** |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4731 | * Map the operator over the shortcuts and widgets, return the first-non-null value. |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4732 | * |
| 4733 | * @param recurse true: iterate over folder children. false: op get the folders themselves. |
| 4734 | * @param op the operator to map over the shortcuts |
| 4735 | */ |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4736 | void mapOverItems(boolean recurse, ItemOperator op) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4737 | ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers(); |
| 4738 | final int containerCount = containers.size(); |
| 4739 | for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) { |
| 4740 | ShortcutAndWidgetContainer container = containers.get(containerIdx); |
| 4741 | // map over all the shortcuts on the workspace |
| 4742 | final int itemCount = container.getChildCount(); |
| 4743 | for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) { |
| 4744 | View item = container.getChildAt(itemIdx); |
| 4745 | ItemInfo info = (ItemInfo) item.getTag(); |
| 4746 | if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) { |
| 4747 | FolderIcon folder = (FolderIcon) item; |
| 4748 | ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder(); |
| 4749 | // map over all the children in the folder |
| 4750 | final int childCount = folderChildren.size(); |
| 4751 | for (int childIdx = 0; childIdx < childCount; childIdx++) { |
| 4752 | View child = folderChildren.get(childIdx); |
| 4753 | info = (ItemInfo) child.getTag(); |
| 4754 | if (op.evaluate(info, child, folder)) { |
| 4755 | return; |
| 4756 | } |
| 4757 | } |
| 4758 | } else { |
| 4759 | if (op.evaluate(info, item, null)) { |
| 4760 | return; |
| 4761 | } |
| 4762 | } |
| 4763 | } |
| 4764 | } |
| 4765 | } |
| 4766 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 4767 | void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) { |
| 4768 | final HashSet<ShortcutInfo> updates = new HashSet<ShortcutInfo>(shortcuts); |
| 4769 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 4770 | @Override |
| 4771 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4772 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView && |
| 4773 | updates.contains(info)) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4774 | ShortcutInfo si = (ShortcutInfo) info; |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 4775 | BubbleTextView shortcut = (BubbleTextView) v; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4776 | boolean oldPromiseState = shortcut.getCompoundDrawables()[1] |
| 4777 | instanceof PreloadIconDrawable; |
| 4778 | shortcut.applyFromShortcutInfo(si, mIconCache, true, |
| 4779 | si.isPromise() != oldPromiseState); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 4780 | |
| 4781 | if (parent != null) { |
| 4782 | parent.invalidate(); |
| 4783 | } |
| 4784 | } |
| 4785 | // process all the shortcuts |
| 4786 | return false; |
| 4787 | } |
| 4788 | }); |
| 4789 | } |
| 4790 | |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 4791 | public void removeAbandonedPromise(String packageName, UserHandleCompat user) { |
| 4792 | ArrayList<String> packages = new ArrayList<String>(1); |
| 4793 | packages.add(packageName); |
| 4794 | LauncherModel.deletePackageFromDatabase(mLauncher, packageName, user); |
| 4795 | removeItemsByPackageName(packages, user); |
Chris Wren | 40c5ed3 | 2014-06-24 18:24:23 -0400 | [diff] [blame] | 4796 | } |
| 4797 | |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 4798 | public void updatePackageBadge(final String packageName, final UserHandleCompat user) { |
| 4799 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 4800 | @Override |
| 4801 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4802 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView) { |
| 4803 | ShortcutInfo shortcutInfo = (ShortcutInfo) info; |
| 4804 | ComponentName cn = shortcutInfo.getTargetComponent(); |
| 4805 | if (user.equals(shortcutInfo.user) && cn != null |
| 4806 | && shortcutInfo.isPromise() |
| 4807 | && packageName.equals(cn.getPackageName())) { |
| 4808 | if (shortcutInfo.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 4809 | // For auto install apps update the icon as well as label. |
| 4810 | mIconCache.getTitleAndIcon(shortcutInfo, |
| 4811 | shortcutInfo.promisedIntent, user, true); |
| 4812 | } else { |
| 4813 | // Only update the icon for restored apps. |
| 4814 | shortcutInfo.updateIcon(mIconCache); |
| 4815 | } |
| 4816 | BubbleTextView shortcut = (BubbleTextView) v; |
| 4817 | shortcut.applyFromShortcutInfo(shortcutInfo, mIconCache, true, false); |
| 4818 | |
| 4819 | if (parent != null) { |
| 4820 | parent.invalidate(); |
| 4821 | } |
| 4822 | } |
| 4823 | } |
| 4824 | // process all the shortcuts |
| 4825 | return false; |
| 4826 | } |
| 4827 | }); |
| 4828 | } |
| 4829 | |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 4830 | public void updatePackageState(ArrayList<PackageInstallInfo> installInfos) { |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 4831 | for (final PackageInstallInfo installInfo : installInfos) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4832 | if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) { |
| 4833 | continue; |
| 4834 | } |
| 4835 | |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 4836 | mapOverItems(MAP_RECURSE, new ItemOperator() { |
| 4837 | @Override |
| 4838 | public boolean evaluate(ItemInfo info, View v, View parent) { |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 4839 | if (info instanceof ShortcutInfo && v instanceof BubbleTextView) { |
| 4840 | ShortcutInfo si = (ShortcutInfo) info; |
| 4841 | ComponentName cn = si.getTargetComponent(); |
| 4842 | if (si.isPromise() && (cn != null) |
| 4843 | && installInfo.packageName.equals(cn.getPackageName())) { |
| 4844 | si.setInstallProgress(installInfo.progress); |
| 4845 | if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) { |
| 4846 | // Mark this info as broken. |
| 4847 | si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
| 4848 | } |
| 4849 | ((BubbleTextView)v).applyState(false); |
| 4850 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4851 | } else if (v instanceof PendingAppWidgetHostView |
| 4852 | && info instanceof LauncherAppWidgetInfo |
| 4853 | && ((LauncherAppWidgetInfo) info).providerName.getPackageName() |
| 4854 | .equals(installInfo.packageName)) { |
| 4855 | ((LauncherAppWidgetInfo) info).installProgress = installInfo.progress; |
| 4856 | ((PendingAppWidgetHostView) v).applyState(); |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 4857 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4858 | |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 4859 | // process all the shortcuts |
| 4860 | return false; |
| 4861 | } |
| 4862 | }); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 4863 | } |
| 4864 | } |
| 4865 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4866 | void widgetsRestored(ArrayList<LauncherAppWidgetInfo> changedInfo) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4867 | if (!changedInfo.isEmpty()) { |
| 4868 | DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo, |
| 4869 | mLauncher.getAppWidgetHost()); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 4870 | if (LauncherModel.getProviderInfo(getContext(), |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4871 | changedInfo.get(0).providerName) != null) { |
| 4872 | // Re-inflate the widgets which have changed status |
| 4873 | widgetRefresh.run(); |
| 4874 | } else { |
| 4875 | // widgetRefresh will automatically run when the packages are updated. |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 4876 | // For now just update the progress bars |
| 4877 | for (LauncherAppWidgetInfo info : changedInfo) { |
| 4878 | if (info.hostView instanceof PendingAppWidgetHostView) { |
| 4879 | info.installProgress = 100; |
| 4880 | ((PendingAppWidgetHostView) info.hostView).applyState(); |
| 4881 | } |
| 4882 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4883 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 4884 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4887 | private void moveToScreen(int page, boolean animate) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 4888 | if (!workspaceInModalState()) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4889 | if (animate) { |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4890 | snapToPage(page); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4891 | } else { |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4892 | setCurrentPage(page); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4893 | } |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 4894 | } |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4895 | View child = getChildAt(page); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4896 | if (child != null) { |
| 4897 | child.requestFocus(); |
| 4898 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4901 | void moveToDefaultScreen(boolean animate) { |
| 4902 | moveToScreen(mDefaultPage, animate); |
| 4903 | } |
| 4904 | |
| 4905 | void moveToCustomContentScreen(boolean animate) { |
| 4906 | if (hasCustomContent()) { |
| 4907 | int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID); |
| 4908 | if (animate) { |
| 4909 | snapToPage(ccIndex); |
| 4910 | } else { |
| 4911 | setCurrentPage(ccIndex); |
| 4912 | } |
| 4913 | View child = getChildAt(ccIndex); |
| 4914 | if (child != null) { |
| 4915 | child.requestFocus(); |
| 4916 | } |
| 4917 | } |
Adam Cohen | 76128b6 | 2013-11-14 16:43:59 +0000 | [diff] [blame] | 4918 | exitWidgetResizeMode(); |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4919 | } |
| 4920 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4921 | @Override |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4922 | protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) { |
| 4923 | long screenId = getScreenIdForPageIndex(pageIndex); |
| 4924 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 4925 | int count = mScreenOrder.size() - numCustomPages(); |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4926 | if (count > 1) { |
Winson Chung | ba4e52f | 2013-10-01 17:22:13 -0700 | [diff] [blame] | 4927 | return new PageIndicator.PageMarkerResources(R.drawable.ic_pageindicator_current, |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4928 | R.drawable.ic_pageindicator_add); |
| 4929 | } |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 4930 | } |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4931 | |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 4932 | return super.getPageIndicatorMarker(pageIndex); |
| 4933 | } |
| 4934 | |
| 4935 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4936 | public void syncPages() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4937 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4938 | |
| 4939 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 4940 | public void syncPageItems(int page, boolean immediate) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4941 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4942 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4943 | protected String getPageIndicatorDescription() { |
| 4944 | String settings = getResources().getString(R.string.settings_button_text); |
| 4945 | return getCurrentPageDescription() + ", " + settings; |
| 4946 | } |
| 4947 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4948 | protected String getCurrentPageDescription() { |
| 4949 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4950 | int delta = numCustomPages(); |
| 4951 | if (hasCustomContent() && getNextPage() == 0) { |
| 4952 | return mCustomContentDescription; |
| 4953 | } |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 4954 | return String.format(getContext().getString(R.string.workspace_scroll_format), |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4955 | page + 1 - delta, getChildCount() - delta); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4956 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 4957 | |
| 4958 | public void getLocationInDragLayer(int[] loc) { |
| 4959 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 4960 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 4961 | |
| 4962 | /** |
| 4963 | * Used as a workaround to ensure that the AppWidgetService receives the |
| 4964 | * PACKAGE_ADDED broadcast before updating widgets. |
| 4965 | */ |
| 4966 | private class DeferredWidgetRefresh implements Runnable { |
| 4967 | private final ArrayList<LauncherAppWidgetInfo> mInfos; |
| 4968 | private final LauncherAppWidgetHost mHost; |
| 4969 | private final Handler mHandler; |
| 4970 | |
| 4971 | private boolean mRefreshPending; |
| 4972 | |
| 4973 | public DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos, |
| 4974 | LauncherAppWidgetHost host) { |
| 4975 | mInfos = infos; |
| 4976 | mHost = host; |
| 4977 | mHandler = new Handler(); |
| 4978 | mRefreshPending = true; |
| 4979 | |
| 4980 | mHost.addProviderChangeListener(this); |
| 4981 | // Force refresh after 10 seconds, if we don't get the provider changed event. |
| 4982 | // This could happen when the provider is no longer available in the app. |
| 4983 | mHandler.postDelayed(this, 10000); |
| 4984 | } |
| 4985 | |
| 4986 | @Override |
| 4987 | public void run() { |
| 4988 | mHost.removeProviderChangeListener(this); |
| 4989 | mHandler.removeCallbacks(this); |
| 4990 | |
| 4991 | if (!mRefreshPending) { |
| 4992 | return; |
| 4993 | } |
| 4994 | |
| 4995 | mRefreshPending = false; |
| 4996 | |
| 4997 | for (LauncherAppWidgetInfo info : mInfos) { |
| 4998 | if (info.hostView instanceof PendingAppWidgetHostView) { |
| 4999 | PendingAppWidgetHostView view = (PendingAppWidgetHostView) info.hostView; |
| 5000 | mLauncher.removeAppWidget(info); |
| 5001 | |
| 5002 | CellLayout cl = (CellLayout) view.getParent().getParent(); |
| 5003 | // Remove the current widget |
| 5004 | cl.removeView(view); |
| 5005 | mLauncher.bindAppWidget(info); |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 5010 | } |