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; |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 46 | import android.net.Uri; |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 47 | import android.os.AsyncTask; |
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; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 50 | import android.support.v4.view.ViewCompat; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | import android.util.AttributeSet; |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 52 | import android.util.Log; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 53 | import android.util.SparseArray; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 54 | import android.view.Choreographer; |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 55 | import android.view.Display; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 57 | import android.view.View; |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 58 | import android.view.ViewGroup; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 59 | import android.view.accessibility.AccessibilityManager; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 60 | import android.view.animation.DecelerateInterpolator; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 61 | import android.view.animation.Interpolator; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 62 | import android.widget.TextView; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 63 | |
| 64 | import com.android.launcher3.compat.UserHandleCompat; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 65 | import com.android.launcher3.FolderIcon.FolderRingAnimator; |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 66 | import com.android.launcher3.Launcher.CustomContentCallbacks; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 67 | import com.android.launcher3.LauncherSettings.Favorites; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 68 | |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 70 | import java.util.HashMap; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 71 | import java.util.HashSet; |
Michael Jurka | 8bd65f5 | 2012-06-25 14:56:22 -0700 | [diff] [blame] | 72 | import java.util.Iterator; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | /** |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 75 | * The workspace is a wide area with a wallpaper and a finite number of pages. |
| 76 | * 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] | 77 | * 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] | 78 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 79 | public class Workspace extends SmoothPagedView |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 80 | implements DropTarget, DragSource, DragScroller, View.OnTouchListener, |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 81 | DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener, |
| 82 | Insettable { |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 83 | private static final String TAG = "Launcher.Workspace"; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 84 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 85 | // Y rotation to apply to the workspace screens |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 86 | private static final float WORKSPACE_OVERSCROLL_ROTATION = 24f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -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 | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 101 | private static final boolean MAP_NO_RECURSE = false; |
| 102 | private static final boolean MAP_RECURSE = true; |
| 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; |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 112 | private Drawable mBackground; |
Michael Jurka | 25356e7 | 2011-03-03 14:53:11 -0800 | [diff] [blame] | 113 | boolean mDrawBackground = true; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 114 | private float mBackgroundAlpha = 0; |
| 115 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 116 | private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200; |
| 117 | private long mTouchDownTime = -1; |
| 118 | private long mCustomContentShowTime = -1; |
| 119 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 120 | private LayoutTransition mLayoutTransition; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 121 | private final WallpaperManager mWallpaperManager; |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 122 | private IBinder mWindowToken; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 123 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 124 | private int mOriginalDefaultPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 125 | private int mDefaultPage; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 127 | private ShortcutAndWidgetContainer mDragSourceInternal; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 128 | private static boolean sAccessibilityEnabled; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 129 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 130 | // 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] | 131 | final static long EXTRA_EMPTY_SCREEN_ID = -201; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 132 | private final static long CUSTOM_CONTENT_SCREEN_ID = -301; |
| 133 | |
| 134 | private HashMap<Long, CellLayout> mWorkspaceScreens = new HashMap<Long, CellLayout>(); |
| 135 | private ArrayList<Long> mScreenOrder = new ArrayList<Long>(); |
| 136 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 137 | private Runnable mRemoveEmptyScreenRunnable; |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 138 | private boolean mDeferRemoveExtraEmptyScreen = false; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 139 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 140 | /** |
| 141 | * CellInfo for the cell that is currently being dragged |
| 142 | */ |
| 143 | private CellLayout.CellInfo mDragInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 144 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Target drop area calculated during last acceptDrop call. |
| 147 | */ |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 148 | private int[] mTargetCell = new int[2]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 149 | private int mDragOverX = -1; |
| 150 | private int mDragOverY = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 152 | static Rect mLandscapeCellLayoutMetrics = null; |
| 153 | static Rect mPortraitCellLayoutMetrics = null; |
| 154 | |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 155 | CustomContentCallbacks mCustomContentCallbacks; |
| 156 | boolean mCustomContentShowing; |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 157 | private float mLastCustomContentScrollProgress = -1f; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 158 | private String mCustomContentDescription = ""; |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 159 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 160 | /** |
| 161 | * The CellLayout that is currently being dragged over |
| 162 | */ |
| 163 | private CellLayout mDragTargetLayout = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 164 | /** |
| 165 | * The CellLayout that we will show as glowing |
| 166 | */ |
| 167 | private CellLayout mDragOverlappingLayout = null; |
| 168 | |
| 169 | /** |
| 170 | * The CellLayout which will be dropped to |
| 171 | */ |
| 172 | private CellLayout mDropToLayout = null; |
| 173 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 174 | private Launcher mLauncher; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 175 | private IconCache mIconCache; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 176 | private DragController mDragController; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 177 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 178 | // These are temporary variables to prevent having to allocate a new object just to |
| 179 | // 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] | 180 | private int[] mTempCell = new int[2]; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 181 | private int[] mTempPt = new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 182 | private int[] mTempEstimate = new int[2]; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 183 | private float[] mDragViewVisualCenter = new float[2]; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 184 | private float[] mTempCellLayoutCenterCoordinates = new float[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 185 | private Matrix mTempInverseMatrix = new Matrix(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 186 | |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 187 | private SpringLoadedDragController mSpringLoadedDragController; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 188 | private float mSpringLoadedShrinkFactor; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 189 | private float mOverviewModeShrinkFactor; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 190 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 191 | // 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] | 192 | // in all apps or customize mode) |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 193 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 194 | enum State { NORMAL, SPRING_LOADED, SMALL, OVERVIEW}; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 195 | private State mState = State.NORMAL; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 196 | private boolean mIsSwitchingState = false; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 197 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 198 | boolean mAnimatingViewIntoPlace = false; |
| 199 | boolean mIsDragOccuring = false; |
| 200 | boolean mChildrenLayersEnabled = true; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 201 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 202 | private boolean mStripScreensOnPageStopMoving = false; |
| 203 | |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 204 | /** 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] | 205 | private boolean mInScrollArea = false; |
| 206 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 207 | private HolographicOutlineHelper mOutlineHelper; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 208 | private Bitmap mDragOutline = null; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 209 | private final Rect mTempRect = new Rect(); |
| 210 | private final int[] mTempXY = new int[2]; |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 211 | private int[] mTempVisiblePagesRange = new int[2]; |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 212 | private boolean mOverscrollTransformsSet; |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 213 | private float mLastOverscrollPivotX; |
Michael Jurka | f8304f0 | 2012-04-26 13:33:26 -0700 | [diff] [blame] | 214 | public static final int DRAG_BITMAP_PADDING = 2; |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 215 | private boolean mWorkspaceFadeInAdjacentScreens; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 216 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 217 | WallpaperOffsetInterpolator mWallpaperOffset; |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 218 | private boolean mWallpaperIsLiveWallpaper; |
| 219 | private int mNumPagesForWallpaperParallax; |
| 220 | private float mLastSetWallpaperOffsetSteps = 0; |
| 221 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 222 | private Runnable mDelayedResizeRunnable; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 223 | private Runnable mDelayedSnapToPageRunnable; |
Michael Jurka | 84f2ce7 | 2012-04-13 15:08:01 -0700 | [diff] [blame] | 224 | private Point mDisplaySize = new Point(); |
Adam Cohen | 9430988 | 2012-06-08 16:24:56 -0700 | [diff] [blame] | 225 | private int mCameraDistance; |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 226 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 227 | // 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] | 228 | private static final int FOLDER_CREATION_TIMEOUT = 0; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 229 | public static final int REORDER_TIMEOUT = 350; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 230 | private final Alarm mFolderCreationAlarm = new Alarm(); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 231 | private final Alarm mReorderAlarm = new Alarm(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 232 | private FolderRingAnimator mDragFolderRingAnimator = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 233 | private FolderIcon mDragOverFolderIcon = null; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 234 | private boolean mCreateUserFolderOnDrop = false; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 235 | private boolean mAddToExistingFolderOnDrop = false; |
| 236 | private DropTarget.DragEnforcer mDragEnforcer; |
Adam Cohen | 3aff81c | 2012-05-16 21:01:01 -0700 | [diff] [blame] | 237 | private float mMaxDistanceForFolderCreation; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 238 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 239 | // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) |
| 240 | private float mXDown; |
| 241 | private float mYDown; |
| 242 | final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; |
| 243 | final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; |
| 244 | final static float TOUCH_SLOP_DAMPING_FACTOR = 4; |
| 245 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 246 | // Relating to the animation of items being dropped externally |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 247 | public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0; |
| 248 | public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1; |
| 249 | public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2; |
| 250 | public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3; |
| 251 | public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 252 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 253 | // Related to dragging, folder creation and reordering |
| 254 | private static final int DRAG_MODE_NONE = 0; |
| 255 | private static final int DRAG_MODE_CREATE_FOLDER = 1; |
| 256 | private static final int DRAG_MODE_ADD_TO_FOLDER = 2; |
| 257 | private static final int DRAG_MODE_REORDER = 3; |
| 258 | private int mDragMode = DRAG_MODE_NONE; |
| 259 | private int mLastReorderX = -1; |
| 260 | private int mLastReorderY = -1; |
| 261 | |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 262 | private SparseArray<Parcelable> mSavedStates; |
| 263 | private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>(); |
| 264 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 265 | // 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] | 266 | private int mSavedScrollX; |
| 267 | private float mSavedRotationY; |
| 268 | private float mSavedTranslationX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 269 | |
| 270 | private float mCurrentScale; |
| 271 | private float mNewScale; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 272 | private float[] mOldBackgroundAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 273 | private float[] mOldAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 274 | private float[] mNewBackgroundAlphas; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 275 | private float[] mNewAlphas; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 276 | private int mLastChildCount = -1; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 277 | private float mTransitionProgress; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 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 | |
Romain Guy | eeacd56 | 2012-10-10 18:47:33 -0700 | [diff] [blame] | 283 | private final Runnable mBindPages = new Runnable() { |
| 284 | @Override |
| 285 | public void run() { |
| 286 | mLauncher.getModel().bindRemainingSynchronousPages(); |
| 287 | } |
| 288 | }; |
| 289 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 290 | /** |
| 291 | * Used to inflate the Workspace from XML. |
| 292 | * |
| 293 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 294 | * @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] | 295 | */ |
| 296 | public Workspace(Context context, AttributeSet attrs) { |
| 297 | this(context, attrs, 0); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Used to inflate the Workspace from XML. |
| 302 | * |
| 303 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 304 | * @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] | 305 | * @param defStyle Unused. |
| 306 | */ |
| 307 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 308 | super(context, attrs, defStyle); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 309 | mContentIsRefreshable = false; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 310 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 311 | mOutlineHelper = HolographicOutlineHelper.obtain(context); |
| 312 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 313 | mDragEnforcer = new DropTarget.DragEnforcer(context); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 314 | // With workspace, data is available straight from the get-go |
| 315 | setDataIsReady(); |
| 316 | |
Michael Jurka | 8bc66c7 | 2012-06-21 08:36:45 -0700 | [diff] [blame] | 317 | mLauncher = (Launcher) context; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 318 | final Resources res = getResources(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 319 | mWorkspaceFadeInAdjacentScreens = LauncherAppState.getInstance().getDynamicGrid(). |
| 320 | getDeviceProfile().shouldFadeAdjacentWorkspaceScreens(); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 321 | mFadeInAdjacentScreens = false; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 322 | mWallpaperManager = WallpaperManager.getInstance(context); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 323 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 324 | LauncherAppState app = LauncherAppState.getInstance(); |
| 325 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 326 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 327 | R.styleable.Workspace, defStyle, 0); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 328 | mSpringLoadedShrinkFactor = |
| 329 | res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 330 | mOverviewModeShrinkFactor = grid.getOverviewModeScale(); |
Adam Cohen | 9430988 | 2012-06-08 16:24:56 -0700 | [diff] [blame] | 331 | mCameraDistance = res.getInteger(R.integer.config_cameraDistance); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 332 | mOriginalDefaultPage = mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 333 | a.recycle(); |
| 334 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 335 | setOnHierarchyChangeListener(this); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 336 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 337 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 338 | initWorkspace(); |
Winson Chung | c35afb2 | 2011-02-23 13:01:49 -0800 | [diff] [blame] | 339 | |
| 340 | // Disable multitouch across the workspace/all apps/customize tray |
| 341 | setMotionEventSplittingEnabled(true); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 342 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 343 | } |
| 344 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 345 | @Override |
| 346 | public void setInsets(Rect insets) { |
| 347 | mInsets.set(insets); |
Adam Cohen | 6400b84 | 2013-11-26 15:45:38 -0800 | [diff] [blame] | 348 | |
| 349 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 350 | if (customScreen != null) { |
| 351 | View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0); |
| 352 | if (customContent instanceof Insettable) { |
| 353 | ((Insettable) customContent).setInsets(mInsets); |
| 354 | } |
| 355 | } |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 358 | // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each |
| 359 | // dimension if unsuccessful |
| 360 | public int[] estimateItemSize(int hSpan, int vSpan, |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 361 | ItemInfo itemInfo, boolean springLoaded) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 362 | int[] size = new int[2]; |
| 363 | if (getChildCount() > 0) { |
Winson Chung | ad7db6e | 2013-10-08 14:01:06 -0700 | [diff] [blame] | 364 | // Use the first non-custom page to estimate the child position |
| 365 | CellLayout cl = (CellLayout) getChildAt(numCustomPages()); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 366 | Rect r = estimateItemPosition(cl, itemInfo, 0, 0, hSpan, vSpan); |
| 367 | size[0] = r.width(); |
| 368 | size[1] = r.height(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 369 | if (springLoaded) { |
| 370 | size[0] *= mSpringLoadedShrinkFactor; |
| 371 | size[1] *= mSpringLoadedShrinkFactor; |
| 372 | } |
| 373 | return size; |
| 374 | } else { |
| 375 | size[0] = Integer.MAX_VALUE; |
| 376 | size[1] = Integer.MAX_VALUE; |
| 377 | return size; |
| 378 | } |
| 379 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 380 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 381 | public Rect estimateItemPosition(CellLayout cl, ItemInfo pendingInfo, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 382 | int hCell, int vCell, int hSpan, int vSpan) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 383 | Rect r = new Rect(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 384 | cl.cellToRect(hCell, vCell, hSpan, vSpan, r); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 385 | return r; |
| 386 | } |
| 387 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 388 | public void onDragStart(final DragSource source, Object info, int dragAction) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 389 | mIsDragOccuring = true; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 390 | updateChildrenLayersEnabled(false); |
Winson Chung | 641d71d | 2012-04-26 15:58:01 -0700 | [diff] [blame] | 391 | mLauncher.lockScreenOrientation(); |
Sandeep Siddhartha | ab2d9d7 | 2013-09-17 13:18:24 -0700 | [diff] [blame] | 392 | mLauncher.onInteractionBegin(); |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 393 | setChildrenBackgroundAlphaMultipliers(1f); |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 394 | // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging |
| 395 | InstallShortcutReceiver.enableInstallQueue(); |
| 396 | UninstallShortcutReceiver.enableUninstallQueue(); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 397 | post(new Runnable() { |
| 398 | @Override |
| 399 | public void run() { |
| 400 | if (mIsDragOccuring) { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 401 | mDeferRemoveExtraEmptyScreen = false; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 402 | addExtraEmptyScreenOnDrag(); |
| 403 | } |
| 404 | } |
| 405 | }); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 408 | |
| 409 | public void deferRemoveExtraEmptyScreen() { |
| 410 | mDeferRemoveExtraEmptyScreen = true; |
| 411 | } |
| 412 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 413 | public void onDragEnd() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 414 | System.out.println("onDrag end workspace"); |
| 415 | |
| 416 | if (!mDeferRemoveExtraEmptyScreen) { |
| 417 | removeExtraEmptyScreen(true, mDragSourceInternal != null); |
| 418 | } |
| 419 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 420 | mIsDragOccuring = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 421 | updateChildrenLayersEnabled(false); |
Winson Chung | 4b919f8 | 2012-05-01 10:44:08 -0700 | [diff] [blame] | 422 | mLauncher.unlockScreenOrientation(false); |
Winson Chung | f561bdf | 2012-05-03 11:20:19 -0700 | [diff] [blame] | 423 | |
| 424 | // Re-enable any Un/InstallShortcutReceiver and now process any queued items |
| 425 | InstallShortcutReceiver.disableAndFlushInstallQueue(getContext()); |
| 426 | UninstallShortcutReceiver.disableAndFlushUninstallQueue(getContext()); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 427 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 428 | mDragSourceInternal = null; |
Sandeep Siddhartha | ab2d9d7 | 2013-09-17 13:18:24 -0700 | [diff] [blame] | 429 | mLauncher.onInteractionEnd(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 430 | } |
| 431 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 432 | /** |
| 433 | * Initializes various states for this workspace. |
| 434 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 435 | protected void initWorkspace() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 436 | Context context = getContext(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 437 | mCurrentPage = mDefaultPage; |
| 438 | Launcher.setScreen(mCurrentPage); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 439 | LauncherAppState app = LauncherAppState.getInstance(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 440 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 441 | mIconCache = app.getIconCache(); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 442 | setWillNotDraw(false); |
Romain Guy | ce3cbd1 | 2013-02-25 15:00:36 -0800 | [diff] [blame] | 443 | setClipChildren(false); |
| 444 | setClipToPadding(false); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 445 | setChildrenDrawnWithCacheEnabled(true); |
Derek Prothro | dadd984 | 2014-01-17 13:43:50 -0500 | [diff] [blame] | 446 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 447 | setMinScale(mOverviewModeShrinkFactor); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 448 | setupLayoutTransition(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 449 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 450 | final Resources res = getResources(); |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 451 | try { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 452 | mBackground = res.getDrawable(R.drawable.apps_customize_bg); |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 453 | } catch (Resources.NotFoundException e) { |
| 454 | // In this case, we will skip drawing background protection |
| 455 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 456 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 457 | mWallpaperOffset = new WallpaperOffsetInterpolator(); |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 458 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
Michael Jurka | 84f2ce7 | 2012-04-13 15:08:01 -0700 | [diff] [blame] | 459 | display.getSize(mDisplaySize); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 460 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 461 | mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 462 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 463 | |
| 464 | // Set the wallpaper dimensions when Launcher starts up |
| 465 | setWallpaperDimension(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 468 | private void setupLayoutTransition() { |
| 469 | // We want to show layout transitions when pages are deleted, to close the gap. |
| 470 | mLayoutTransition = new LayoutTransition(); |
| 471 | mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING); |
| 472 | mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 473 | mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING); |
| 474 | mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 475 | setLayoutTransition(mLayoutTransition); |
| 476 | } |
| 477 | |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 478 | void enableLayoutTransitions() { |
| 479 | setLayoutTransition(mLayoutTransition); |
| 480 | } |
| 481 | void disableLayoutTransitions() { |
| 482 | setLayoutTransition(null); |
| 483 | } |
| 484 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 485 | @Override |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 486 | protected int getScrollMode() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 487 | return SmoothPagedView.X_LARGE_MODE; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Michael Jurka | 08ee770 | 2011-08-11 16:53:35 -0700 | [diff] [blame] | 490 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 491 | public void onChildViewAdded(View parent, View child) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 492 | if (!(child instanceof CellLayout)) { |
| 493 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 494 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 495 | CellLayout cl = ((CellLayout) child); |
| 496 | cl.setOnInterceptTouchListener(this); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 497 | cl.setClickable(true); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 498 | cl.setImportantForAccessibility(ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO); |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 499 | super.onChildViewAdded(parent, child); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 502 | protected boolean shouldDrawChild(View child) { |
| 503 | final CellLayout cl = (CellLayout) child; |
| 504 | return super.shouldDrawChild(child) && |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 505 | (mIsSwitchingState || |
| 506 | cl.getShortcutsAndWidgets().getAlpha() > 0 || |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 507 | cl.getBackgroundAlpha() > 0); |
| 508 | } |
| 509 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 510 | /** |
| 511 | * @return The open folder on the current screen, or null if there is none |
| 512 | */ |
| 513 | Folder getOpenFolder() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 514 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 515 | int count = dragLayer.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 516 | for (int i = 0; i < count; i++) { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 517 | View child = dragLayer.getChildAt(i); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 518 | if (child instanceof Folder) { |
| 519 | Folder folder = (Folder) child; |
| 520 | if (folder.getInfo().opened) |
| 521 | return folder; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | return null; |
| 525 | } |
| 526 | |
Patrick Dubroy | a0aa012 | 2011-02-24 11:42:23 -0800 | [diff] [blame] | 527 | boolean isTouchActive() { |
| 528 | return mTouchState != TOUCH_STATE_REST; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 531 | public void removeAllWorkspaceScreens() { |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 532 | // Disable all layout transitions before removing all pages to ensure that we don't get the |
| 533 | // transition animations competing with us changing the scroll when we add pages or the |
| 534 | // custom content screen |
| 535 | disableLayoutTransitions(); |
| 536 | |
| 537 | // Since we increment the current page when we call addCustomContentPage via bindScreens |
| 538 | // (and other places), we need to adjust the current page back when we clear the pages |
| 539 | if (hasCustomContent()) { |
| 540 | removeCustomContentPage(); |
| 541 | } |
| 542 | |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 543 | // Remove the pages and clear the screen models |
| 544 | removeAllViews(); |
| 545 | mScreenOrder.clear(); |
| 546 | mWorkspaceScreens.clear(); |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 547 | |
| 548 | // Re-enable the layout transitions |
| 549 | enableLayoutTransitions(); |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 552 | public long insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 553 | // Find the index to insert this view into. If the empty screen exists, then |
| 554 | // insert it before that. |
| 555 | int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 556 | if (insertIndex < 0) { |
| 557 | insertIndex = mScreenOrder.size(); |
| 558 | } |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 559 | return insertNewWorkspaceScreen(screenId, insertIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 562 | public long insertNewWorkspaceScreen(long screenId) { |
| 563 | return insertNewWorkspaceScreen(screenId, getChildCount()); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Adam Cohen | 89bddfa | 2013-08-20 11:57:13 -0700 | [diff] [blame] | 566 | public long insertNewWorkspaceScreen(long screenId, int insertIndex) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 567 | // Log to disk |
| 568 | Launcher.addDumpLog(TAG, "11683562 - insertNewWorkspaceScreen(): " + screenId + |
| 569 | " at index: " + insertIndex, true); |
| 570 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 571 | if (mWorkspaceScreens.containsKey(screenId)) { |
| 572 | throw new RuntimeException("Screen id " + screenId + " already exists!"); |
| 573 | } |
| 574 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 575 | CellLayout newScreen = (CellLayout) |
| 576 | mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null); |
| 577 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 578 | newScreen.setOnLongClickListener(mLongClickListener); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 579 | newScreen.setOnClickListener(mLauncher); |
Winson Chung | 837b241 | 2013-09-09 14:43:51 -0700 | [diff] [blame] | 580 | newScreen.setSoundEffectsEnabled(false); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 581 | mWorkspaceScreens.put(screenId, newScreen); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 582 | mScreenOrder.add(insertIndex, screenId); |
| 583 | addView(newScreen, insertIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 584 | return screenId; |
| 585 | } |
| 586 | |
Derek Prothro | dadd984 | 2014-01-17 13:43:50 -0500 | [diff] [blame] | 587 | public void createCustomContentContainer() { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 588 | CellLayout customScreen = (CellLayout) |
Adam Cohen | 41eb470 | 2013-06-27 15:08:59 -0700 | [diff] [blame] | 589 | mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null); |
Winson Chung | 59a488a | 2013-12-10 12:32:14 -0800 | [diff] [blame] | 590 | customScreen.disableBackground(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 591 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 592 | mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen); |
| 593 | mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 594 | |
Adam Cohen | 2702ea0 | 2013-08-15 16:17:42 -0700 | [diff] [blame] | 595 | // We want no padding on the custom content |
| 596 | customScreen.setPadding(0, 0, 0, 0); |
| 597 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 598 | addFullScreenPage(customScreen); |
Michael Jurka | ee0ce2b | 2013-07-02 17:24:35 -0700 | [diff] [blame] | 599 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 600 | // Ensure that the current page and default page are maintained. |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 601 | mDefaultPage = mOriginalDefaultPage + 1; |
Winson Chung | 82e5c98 | 2013-10-09 12:04:50 -0700 | [diff] [blame] | 602 | |
| 603 | // Update the custom content hint |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 604 | mLauncher.getLauncherClings().updateCustomContentHintVisibility(); |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 605 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
| 606 | mRestorePage = mRestorePage + 1; |
| 607 | } else { |
| 608 | setCurrentPage(getCurrentPage() + 1); |
| 609 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 612 | public void removeCustomContentPage() { |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 613 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 614 | if (customScreen == null) { |
| 615 | throw new RuntimeException("Expected custom content screen to exist"); |
| 616 | } |
| 617 | |
| 618 | mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID); |
| 619 | mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID); |
| 620 | removeView(customScreen); |
Adam Cohen | bb6fda6 | 2013-10-10 12:48:52 -0700 | [diff] [blame] | 621 | |
| 622 | if (mCustomContentCallbacks != null) { |
| 623 | mCustomContentCallbacks.onScrollProgressChanged(0); |
| 624 | mCustomContentCallbacks.onHide(); |
| 625 | } |
| 626 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 627 | mCustomContentCallbacks = null; |
| 628 | |
| 629 | // Ensure that the current page and default page are maintained. |
| 630 | mDefaultPage = mOriginalDefaultPage - 1; |
Winson Chung | 82e5c98 | 2013-10-09 12:04:50 -0700 | [diff] [blame] | 631 | |
| 632 | // Update the custom content hint |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 633 | mLauncher.getLauncherClings().updateCustomContentHintVisibility(); |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 634 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
| 635 | mRestorePage = mRestorePage - 1; |
| 636 | } else { |
| 637 | setCurrentPage(getCurrentPage() - 1); |
| 638 | } |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 639 | } |
| 640 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 641 | public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks, |
| 642 | String description) { |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 643 | if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) { |
| 644 | throw new RuntimeException("Expected custom content screen to exist"); |
| 645 | } |
| 646 | |
| 647 | // Add the custom content to the full screen custom page |
| 648 | CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); |
| 649 | int spanX = customScreen.getCountX(); |
| 650 | int spanY = customScreen.getCountY(); |
| 651 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY); |
| 652 | lp.canReorder = false; |
| 653 | lp.isFullscreen = true; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 654 | if (customContent instanceof Insettable) { |
| 655 | ((Insettable)customContent).setInsets(mInsets); |
| 656 | } |
Adam Cohen | 166ebd4 | 2013-11-26 14:55:45 -0800 | [diff] [blame] | 657 | |
| 658 | // Verify that the child is removed from any existing parent. |
| 659 | if (customContent.getParent() instanceof ViewGroup) { |
| 660 | ViewGroup parent = (ViewGroup) customContent.getParent(); |
| 661 | parent.removeView(customContent); |
| 662 | } |
Adam Cohen | 225ad9b | 2013-10-07 13:03:00 -0700 | [diff] [blame] | 663 | customScreen.removeAllViews(); |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 664 | customScreen.addViewToCellLayout(customContent, 0, 0, lp, true); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 665 | mCustomContentDescription = description; |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 666 | |
| 667 | mCustomContentCallbacks = callbacks; |
| 668 | } |
| 669 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 670 | public void addExtraEmptyScreenOnDrag() { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 671 | // Log to disk |
| 672 | Launcher.addDumpLog(TAG, "11683562 - addExtraEmptyScreenOnDrag()", true); |
| 673 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 674 | boolean lastChildOnScreen = false; |
| 675 | boolean childOnFinalScreen = false; |
| 676 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 677 | // Cancel any pending removal of empty screen |
| 678 | mRemoveEmptyScreenRunnable = null; |
| 679 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 680 | if (mDragSourceInternal != null) { |
| 681 | if (mDragSourceInternal.getChildCount() == 1) { |
| 682 | lastChildOnScreen = true; |
| 683 | } |
| 684 | CellLayout cl = (CellLayout) mDragSourceInternal.getParent(); |
| 685 | if (indexOfChild(cl) == getChildCount() - 1) { |
| 686 | childOnFinalScreen = true; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // If this is the last item on the final screen |
| 691 | if (lastChildOnScreen && childOnFinalScreen) { |
| 692 | return; |
| 693 | } |
| 694 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 695 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | public boolean addExtraEmptyScreen() { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 700 | // Log to disk |
| 701 | Launcher.addDumpLog(TAG, "11683562 - addExtraEmptyScreen()", true); |
| 702 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 703 | if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { |
| 704 | insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); |
| 705 | return true; |
| 706 | } |
| 707 | return false; |
| 708 | } |
| 709 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 710 | private void convertFinalScreenToEmptyScreenIfNecessary() { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 711 | // Log to disk |
| 712 | Launcher.addDumpLog(TAG, "11683562 - convertFinalScreenToEmptyScreenIfNecessary()", true); |
| 713 | |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 714 | if (mLauncher.isWorkspaceLoading()) { |
| 715 | // Invalid and dangerous operation if workspace is loading |
| 716 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 717 | return; |
| 718 | } |
| 719 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 720 | if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return; |
| 721 | long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1); |
| 722 | |
| 723 | if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return; |
| 724 | CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId); |
| 725 | |
| 726 | // 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] | 727 | if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 && |
| 728 | !finalScreen.isDropPending()) { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 729 | mWorkspaceScreens.remove(finalScreenId); |
| 730 | mScreenOrder.remove(finalScreenId); |
| 731 | |
| 732 | // if this is the last non-custom content screen, convert it to the empty screen |
| 733 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen); |
| 734 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 735 | |
Winson Chung | f0716b7 | 2013-11-18 16:09:54 -0800 | [diff] [blame] | 736 | // Update the model if we have changed any screens |
| 737 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 738 | Launcher.addDumpLog(TAG, "11683562 - extra empty screen: " + finalScreenId, true); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 742 | public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) { |
| 743 | removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 746 | public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete, |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 747 | final int delay, final boolean stripEmptyScreens) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 748 | // Log to disk |
| 749 | Launcher.addDumpLog(TAG, "11683562 - removeExtraEmptyScreen()", true); |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 750 | if (mLauncher.isWorkspaceLoading()) { |
| 751 | // Don't strip empty screens if the workspace is still loading |
| 752 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 753 | return; |
| 754 | } |
| 755 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 756 | if (delay > 0) { |
| 757 | postDelayed(new Runnable() { |
| 758 | @Override |
| 759 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 760 | removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 761 | } |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 762 | }, delay); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | convertFinalScreenToEmptyScreenIfNecessary(); |
| 767 | if (hasExtraEmptyScreen()) { |
| 768 | int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); |
| 769 | if (getNextPage() == emptyIndex) { |
| 770 | snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION); |
| 771 | fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION, |
| 772 | onComplete, stripEmptyScreens); |
| 773 | } else { |
| 774 | fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION, |
| 775 | onComplete, stripEmptyScreens); |
| 776 | } |
| 777 | return; |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 778 | } else if (stripEmptyScreens) { |
| 779 | // If we're not going to strip the empty screens after removing |
| 780 | // the extra empty screen, do it right away. |
| 781 | stripEmptyScreens(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 782 | } |
Adam Cohen | 3a14eeb | 2013-11-15 16:51:22 +0000 | [diff] [blame] | 783 | |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 784 | if (onComplete != null) { |
| 785 | onComplete.run(); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete, |
| 790 | final boolean stripEmptyScreens) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 791 | // Log to disk |
| 792 | // XXX: Do we need to update LM workspace screens below? |
| 793 | Launcher.addDumpLog(TAG, "11683562 - fadeAndRemoveEmptyScreen()", true); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 794 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 795 | PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f); |
| 796 | |
| 797 | final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
| 798 | |
| 799 | mRemoveEmptyScreenRunnable = new Runnable() { |
| 800 | @Override |
| 801 | public void run() { |
| 802 | if (hasExtraEmptyScreen()) { |
| 803 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
| 804 | mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); |
| 805 | removeView(cl); |
| 806 | if (stripEmptyScreens) { |
| 807 | stripEmptyScreens(); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | }; |
| 812 | |
| 813 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha); |
| 814 | oa.setDuration(duration); |
| 815 | oa.setStartDelay(delay); |
| 816 | oa.addListener(new AnimatorListenerAdapter() { |
| 817 | @Override |
| 818 | public void onAnimationEnd(Animator animation) { |
| 819 | if (mRemoveEmptyScreenRunnable != null) { |
| 820 | mRemoveEmptyScreenRunnable.run(); |
| 821 | } |
| 822 | if (onComplete != null) { |
| 823 | onComplete.run(); |
| 824 | } |
| 825 | } |
| 826 | }); |
| 827 | oa.start(); |
| 828 | } |
| 829 | |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 830 | public boolean hasExtraEmptyScreen() { |
| 831 | int nScreens = getChildCount(); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 832 | nScreens = nScreens - numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 833 | return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1; |
| 834 | } |
| 835 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 836 | public long commitExtraEmptyScreen() { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 837 | // Log to disk |
| 838 | Launcher.addDumpLog(TAG, "11683562 - commitExtraEmptyScreen()", true); |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 839 | if (mLauncher.isWorkspaceLoading()) { |
| 840 | // Invalid and dangerous operation if workspace is loading |
| 841 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 842 | return -1; |
| 843 | } |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 844 | |
Winson Chung | 89f9705 | 2013-09-20 11:32:26 -0700 | [diff] [blame] | 845 | int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 846 | CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 847 | mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 848 | mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); |
| 849 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 850 | long newId = LauncherAppState.getLauncherProvider().generateNewScreenId(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 851 | mWorkspaceScreens.put(newId, cl); |
| 852 | mScreenOrder.add(newId); |
| 853 | |
Winson Chung | 89f9705 | 2013-09-20 11:32:26 -0700 | [diff] [blame] | 854 | // Update the page indicator marker |
| 855 | if (getPageIndicator() != null) { |
| 856 | getPageIndicator().updateMarker(index, getPageIndicatorMarker(index)); |
| 857 | } |
| 858 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 859 | // Update the model for the new screen |
| 860 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
| 861 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 862 | return newId; |
| 863 | } |
| 864 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 865 | public CellLayout getScreenWithId(long screenId) { |
| 866 | CellLayout layout = mWorkspaceScreens.get(screenId); |
| 867 | return layout; |
| 868 | } |
| 869 | |
| 870 | public long getIdForScreen(CellLayout layout) { |
| 871 | Iterator<Long> iter = mWorkspaceScreens.keySet().iterator(); |
| 872 | while (iter.hasNext()) { |
| 873 | long id = iter.next(); |
| 874 | if (mWorkspaceScreens.get(id) == layout) { |
| 875 | return id; |
| 876 | } |
| 877 | } |
| 878 | return -1; |
| 879 | } |
| 880 | |
| 881 | public int getPageIndexForScreenId(long screenId) { |
| 882 | return indexOfChild(mWorkspaceScreens.get(screenId)); |
| 883 | } |
| 884 | |
| 885 | public long getScreenIdForPageIndex(int index) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 886 | if (0 <= index && index < mScreenOrder.size()) { |
| 887 | return mScreenOrder.get(index); |
| 888 | } |
| 889 | return -1; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 892 | ArrayList<Long> getScreenOrder() { |
| 893 | return mScreenOrder; |
| 894 | } |
| 895 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 896 | public void stripEmptyScreens() { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 897 | // Log to disk |
| 898 | Launcher.addDumpLog(TAG, "11683562 - stripEmptyScreens()", true); |
| 899 | |
Adam Cohen | 65e4303 | 2014-03-03 11:37:21 -0800 | [diff] [blame] | 900 | if (mLauncher.isWorkspaceLoading()) { |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 901 | // Don't strip empty screens if the workspace is still loading. |
| 902 | // This is dangerous and can result in data loss. |
Adam Cohen | 517a7f5 | 2014-03-01 12:12:59 -0800 | [diff] [blame] | 903 | Launcher.addDumpLog(TAG, " - workspace loading, skip", true); |
| 904 | return; |
| 905 | } |
| 906 | |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 907 | if (isPageMoving()) { |
| 908 | mStripScreensOnPageStopMoving = true; |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | int currentPage = getNextPage(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 913 | ArrayList<Long> removeScreens = new ArrayList<Long>(); |
| 914 | for (Long id: mWorkspaceScreens.keySet()) { |
| 915 | CellLayout cl = mWorkspaceScreens.get(id); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 916 | if (id >= 0 && cl.getShortcutsAndWidgets().getChildCount() == 0) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 917 | removeScreens.add(id); |
| 918 | } |
| 919 | } |
| 920 | |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 921 | // We enforce at least one page to add new items to. In the case that we remove the last |
| 922 | // such screen, we convert the last screen to the empty screen |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 923 | int minScreens = 1 + numCustomPages(); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 924 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 925 | int pageShift = 0; |
| 926 | for (Long id: removeScreens) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 927 | Launcher.addDumpLog(TAG, "11683562 - removing id: " + id, true); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 928 | CellLayout cl = mWorkspaceScreens.get(id); |
| 929 | mWorkspaceScreens.remove(id); |
| 930 | mScreenOrder.remove(id); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 931 | |
| 932 | if (getChildCount() > minScreens) { |
| 933 | if (indexOfChild(cl) < currentPage) { |
| 934 | pageShift++; |
| 935 | } |
| 936 | removeView(cl); |
| 937 | } else { |
| 938 | // 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] | 939 | mRemoveEmptyScreenRunnable = null; |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 940 | mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl); |
| 941 | mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 942 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 943 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 944 | |
| 945 | if (!removeScreens.isEmpty()) { |
| 946 | // Update the model if we have changed any screens |
| 947 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
| 948 | } |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 949 | |
| 950 | if (pageShift >= 0) { |
| 951 | setCurrentPage(currentPage - pageShift); |
| 952 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // See implementation for parameter definition. |
| 956 | void addInScreen(View child, long container, long screenId, |
| 957 | int x, int y, int spanX, int spanY) { |
| 958 | addInScreen(child, container, screenId, x, y, spanX, spanY, false, false); |
| 959 | } |
| 960 | |
| 961 | // At bind time, we use the rank (screenId) to compute x and y for hotseat items. |
| 962 | // See implementation for parameter definition. |
| 963 | void addInScreenFromBind(View child, long container, long screenId, int x, int y, |
| 964 | int spanX, int spanY) { |
| 965 | addInScreen(child, container, screenId, x, y, spanX, spanY, false, true); |
| 966 | } |
| 967 | |
| 968 | // See implementation for parameter definition. |
| 969 | void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, |
| 970 | boolean insert) { |
| 971 | addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 972 | } |
| 973 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 974 | /** |
| 975 | * Adds the specified child in the specified screen. The position and dimension of |
| 976 | * the child are defined by x, y, spanX and spanY. |
| 977 | * |
| 978 | * @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] | 979 | * @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] | 980 | * @param x The X position of the child in the screen's grid. |
| 981 | * @param y The Y position of the child in the screen's grid. |
| 982 | * @param spanX The number of cells spanned horizontally by the child. |
| 983 | * @param spanY The number of cells spanned vertically by the child. |
| 984 | * @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] | 985 | * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute |
| 986 | * the x and y position in which to place hotseat items. Otherwise |
| 987 | * 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] | 988 | */ |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 989 | void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, |
| 990 | boolean insert, boolean computeXYFromRank) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 991 | if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 992 | if (getScreenWithId(screenId) == null) { |
| 993 | Log.e(TAG, "Skipping child, screenId " + screenId + " not found"); |
Winson Chung | 8481e32 | 2013-08-09 16:06:38 -0700 | [diff] [blame] | 994 | // DEBUGGING - Print out the stack trace to see where we are adding from |
| 995 | new Throwable().printStackTrace(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 996 | return; |
| 997 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 998 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 999 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 1000 | // This should never happen |
| 1001 | throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID"); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1004 | final CellLayout layout; |
| 1005 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1006 | layout = mLauncher.getHotseat().getLayout(); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 1007 | child.setOnKeyListener(null); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1008 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 1009 | // Hide folder title in the hotseat |
| 1010 | if (child instanceof FolderIcon) { |
| 1011 | ((FolderIcon) child).setTextVisible(false); |
| 1012 | } |
| 1013 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1014 | if (computeXYFromRank) { |
| 1015 | x = mLauncher.getHotseat().getCellXFromOrder((int) screenId); |
| 1016 | y = mLauncher.getHotseat().getCellYFromOrder((int) screenId); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1017 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1018 | screenId = mLauncher.getHotseat().getOrderInHotseat(x, y); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } else { |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 1021 | // Show folder title if not in the hotseat |
| 1022 | if (child instanceof FolderIcon) { |
| 1023 | ((FolderIcon) child).setTextVisible(true); |
| 1024 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1025 | layout = getScreenWithId(screenId); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 1026 | child.setOnKeyListener(new IconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1029 | ViewGroup.LayoutParams genericLp = child.getLayoutParams(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1030 | CellLayout.LayoutParams lp; |
| 1031 | if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1032 | lp = new CellLayout.LayoutParams(x, y, spanX, spanY); |
| 1033 | } else { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1034 | lp = (CellLayout.LayoutParams) genericLp; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1035 | lp.cellX = x; |
| 1036 | lp.cellY = y; |
| 1037 | lp.cellHSpan = spanX; |
| 1038 | lp.cellVSpan = spanY; |
| 1039 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1040 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 1041 | if (spanX < 0 && spanY < 0) { |
| 1042 | lp.isLockedToGrid = false; |
| 1043 | } |
| 1044 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1045 | // 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] | 1046 | ItemInfo info = (ItemInfo) child.getTag(); |
| 1047 | int childId = mLauncher.getViewIdForItem(info); |
| 1048 | |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 1049 | boolean markCellsAsOccupied = !(child instanceof Folder); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1050 | if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1051 | // TODO: This branch occurs when the workspace is adding views |
| 1052 | // outside of the defined grid |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1053 | // maybe we should be deleting these items from the LauncherModel? |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 1054 | 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] | 1055 | } |
| 1056 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1057 | if (!(child instanceof Folder)) { |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1058 | child.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1059 | child.setOnLongClickListener(mLongClickListener); |
| 1060 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1061 | if (child instanceof DropTarget) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1062 | mDragController.addDropTarget((DropTarget) child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1063 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1066 | /** |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 1067 | * Called directly from a CellLayout (not by the framework), after we've been added as a |
| 1068 | * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout |
| 1069 | * that it should intercept touch events, which is not something that is normally supported. |
| 1070 | */ |
| 1071 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1072 | public boolean onTouch(View v, MotionEvent event) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1073 | return (isSmall() || !isFinishedSwitchingState()) |
| 1074 | || (!isSmall() && indexOfChild(v) != mCurrentPage); |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 1077 | public boolean isSwitchingState() { |
| 1078 | return mIsSwitchingState; |
| 1079 | } |
| 1080 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1081 | /** This differs from isSwitchingState in that we take into account how far the transition |
| 1082 | * has completed. */ |
Winson Chung | 9b0b2fe | 2012-02-24 13:03:34 -0800 | [diff] [blame] | 1083 | public boolean isFinishedSwitchingState() { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1084 | return !mIsSwitchingState || (mTransitionProgress > 0.5f); |
| 1085 | } |
| 1086 | |
Adam Cohen | ded9f8d | 2010-11-03 13:25:16 -0700 | [diff] [blame] | 1087 | protected void onWindowVisibilityChanged (int visibility) { |
| 1088 | mLauncher.onWindowVisibilityChanged(visibility); |
| 1089 | } |
| 1090 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1091 | @Override |
| 1092 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1093 | if (isSmall() || !isFinishedSwitchingState()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1094 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 1095 | return false; |
| 1096 | } |
| 1097 | return super.dispatchUnhandledMove(focused, direction); |
| 1098 | } |
| 1099 | |
| 1100 | @Override |
| 1101 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1102 | switch (ev.getAction() & MotionEvent.ACTION_MASK) { |
| 1103 | case MotionEvent.ACTION_DOWN: |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1104 | mXDown = ev.getX(); |
| 1105 | mYDown = ev.getY(); |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1106 | mTouchDownTime = System.currentTimeMillis(); |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1107 | break; |
| 1108 | case MotionEvent.ACTION_POINTER_UP: |
| 1109 | case MotionEvent.ACTION_UP: |
| 1110 | if (mTouchState == TOUCH_STATE_REST) { |
| 1111 | final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); |
| 1112 | if (!currentPage.lastDownOnOccupiedCell()) { |
| 1113 | onWallpaperTap(ev); |
| 1114 | } |
| 1115 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1116 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1117 | return super.onInterceptTouchEvent(ev); |
| 1118 | } |
| 1119 | |
Adam Cohen | 3d50932 | 2012-06-06 14:10:04 -0700 | [diff] [blame] | 1120 | protected void reinflateWidgetsIfNecessary() { |
| 1121 | final int clCount = getChildCount(); |
| 1122 | for (int i = 0; i < clCount; i++) { |
| 1123 | CellLayout cl = (CellLayout) getChildAt(i); |
| 1124 | ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets(); |
| 1125 | final int itemCount = swc.getChildCount(); |
| 1126 | for (int j = 0; j < itemCount; j++) { |
| 1127 | View v = swc.getChildAt(j); |
| 1128 | |
| 1129 | if (v.getTag() instanceof LauncherAppWidgetInfo) { |
| 1130 | LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag(); |
| 1131 | LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) info.hostView; |
| 1132 | if (lahv != null && lahv.orientationChangedSincedInflation()) { |
| 1133 | mLauncher.removeAppWidget(info); |
| 1134 | // Remove the current widget which is inflated with the wrong orientation |
| 1135 | cl.removeView(lahv); |
| 1136 | mLauncher.bindAppWidget(info); |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 1143 | @Override |
| 1144 | protected void determineScrollingStart(MotionEvent ev) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1145 | if (!isFinishedSwitchingState()) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1146 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1147 | float deltaX = ev.getX() - mXDown; |
| 1148 | float absDeltaX = Math.abs(deltaX); |
| 1149 | float absDeltaY = Math.abs(ev.getY() - mYDown); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1150 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1151 | if (Float.compare(absDeltaX, 0f) == 0) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1152 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1153 | float slope = absDeltaY / absDeltaX; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1154 | float theta = (float) Math.atan(slope); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1155 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1156 | if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1157 | cancelCurrentPageLongPress(); |
| 1158 | } |
| 1159 | |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1160 | boolean passRightSwipesToCustomContent = |
| 1161 | (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY; |
| 1162 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1163 | boolean swipeInIgnoreDirection = isLayoutRtl() ? deltaX < 0 : deltaX > 0; |
| 1164 | if (swipeInIgnoreDirection && getScreenIdForPageIndex(getCurrentPage()) == |
| 1165 | CUSTOM_CONTENT_SCREEN_ID && passRightSwipesToCustomContent) { |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1166 | // Pass swipes to the right to the custom content page. |
| 1167 | return; |
| 1168 | } |
| 1169 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1170 | if (theta > MAX_SWIPE_ANGLE) { |
| 1171 | // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace |
| 1172 | return; |
| 1173 | } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { |
| 1174 | // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to |
| 1175 | // increase the touch slop to make it harder to begin scrolling the workspace. This |
| 1176 | // results in vertically scrolling widgets to more easily. The higher the angle, the |
| 1177 | // more we increase touch slop. |
| 1178 | theta -= START_DAMPING_TOUCH_SLOP_ANGLE; |
| 1179 | float extraRatio = (float) |
| 1180 | Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); |
| 1181 | super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); |
| 1182 | } else { |
| 1183 | // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special |
| 1184 | super.determineScrollingStart(ev); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1185 | } |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1188 | protected void onPageBeginMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1189 | super.onPageBeginMoving(); |
| 1190 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1191 | if (isHardwareAccelerated()) { |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1192 | updateChildrenLayersEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1193 | } else { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1194 | if (mNextPage != INVALID_PAGE) { |
| 1195 | // we're snapping to a particular screen |
| 1196 | enableChildrenCache(mCurrentPage, mNextPage); |
| 1197 | } else { |
| 1198 | // this is when user is actively dragging a particular screen, they might |
| 1199 | // swipe it either left or right (but we won't advance by more than one screen) |
| 1200 | enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); |
| 1201 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1202 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1203 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1204 | // If we are not fading in adjacent screens, we still need to restore the alpha in case the |
| 1205 | // user scrolls while we are transitioning (should not affect dispatchDraw optimizations) |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1206 | if (!mWorkspaceFadeInAdjacentScreens) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1207 | for (int i = 0; i < getChildCount(); ++i) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 1208 | ((CellLayout) getPageAt(i)).setShortcutAndWidgetAlpha(1f); |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 1209 | } |
| 1210 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1213 | protected void onPageEndMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1214 | super.onPageEndMoving(); |
| 1215 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1216 | if (isHardwareAccelerated()) { |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1217 | updateChildrenLayersEnabled(false); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1218 | } else { |
| 1219 | clearChildrenCache(); |
| 1220 | } |
| 1221 | |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 1222 | if (mDragController.isDragging()) { |
| 1223 | if (isSmall()) { |
| 1224 | // If we are in springloaded mode, then force an event to check if the current touch |
| 1225 | // is under a new page (to scroll to) |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 1226 | mDragController.forceTouchMove(); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 1227 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 1228 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 1229 | |
| 1230 | if (mDelayedResizeRunnable != null) { |
| 1231 | mDelayedResizeRunnable.run(); |
| 1232 | mDelayedResizeRunnable = null; |
| 1233 | } |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (mDelayedSnapToPageRunnable != null) { |
| 1236 | mDelayedSnapToPageRunnable.run(); |
| 1237 | mDelayedSnapToPageRunnable = null; |
| 1238 | } |
Adam Cohen | accfd56 | 2013-07-12 14:40:40 -0700 | [diff] [blame] | 1239 | if (mStripScreensOnPageStopMoving) { |
| 1240 | stripEmptyScreens(); |
| 1241 | mStripScreensOnPageStopMoving = false; |
| 1242 | } |
Adam Cohen | 89cbbb9 | 2013-09-25 16:52:48 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | @Override |
| 1246 | protected void notifyPageSwitchListener() { |
| 1247 | super.notifyPageSwitchListener(); |
Adam Cohen | 674531f | 2013-12-13 15:07:14 -0800 | [diff] [blame] | 1248 | Launcher.setScreen(getNextPage()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1249 | |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 1250 | if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) { |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1251 | mCustomContentShowing = true; |
| 1252 | if (mCustomContentCallbacks != null) { |
Selim Cinek | 3a8a8f7 | 2014-01-16 10:38:38 -0800 | [diff] [blame] | 1253 | mCustomContentCallbacks.onShow(false); |
Adam Cohen | 3d1b2b4 | 2013-08-14 15:57:58 -0700 | [diff] [blame] | 1254 | mCustomContentShowTime = System.currentTimeMillis(); |
Winson Chung | 5841efa | 2013-09-30 18:06:44 -0700 | [diff] [blame] | 1255 | mLauncher.updateVoiceButtonProxyVisible(false); |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1256 | } |
Adam Cohen | 6ad0e7d | 2013-07-24 13:55:41 -0700 | [diff] [blame] | 1257 | } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) { |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1258 | mCustomContentShowing = false; |
| 1259 | if (mCustomContentCallbacks != null) { |
| 1260 | mCustomContentCallbacks.onHide(); |
| 1261 | mLauncher.resetQSBScroll(); |
Winson Chung | 5841efa | 2013-09-30 18:06:44 -0700 | [diff] [blame] | 1262 | mLauncher.updateVoiceButtonProxyVisible(false); |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1263 | } |
| 1264 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1265 | if (getPageIndicator() != null) { |
| 1266 | getPageIndicator().setContentDescription(getPageIndicatorDescription()); |
| 1267 | } |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1268 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1269 | |
Adam Cohen | 6fecd41 | 2013-10-02 17:41:50 -0700 | [diff] [blame] | 1270 | protected CustomContentCallbacks getCustomContentCallbacks() { |
| 1271 | return mCustomContentCallbacks; |
| 1272 | } |
| 1273 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1274 | protected void setWallpaperDimension() { |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1275 | new AsyncTask<Void, Void, Void>() { |
| 1276 | public Void doInBackground(Void ... args) { |
| 1277 | String spKey = WallpaperCropActivity.getSharedPreferencesKey(); |
| 1278 | SharedPreferences sp = |
| 1279 | mLauncher.getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 1280 | LauncherWallpaperPickerActivity.suggestWallpaperDimension(mLauncher.getResources(), |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1281 | sp, mLauncher.getWindowManager(), mWallpaperManager); |
| 1282 | return null; |
| 1283 | } |
| 1284 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1287 | protected void snapToPage(int whichPage, Runnable r) { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 1288 | snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r); |
| 1289 | } |
| 1290 | |
| 1291 | protected void snapToPage(int whichPage, int duration, Runnable r) { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1292 | if (mDelayedSnapToPageRunnable != null) { |
| 1293 | mDelayedSnapToPageRunnable.run(); |
| 1294 | } |
| 1295 | mDelayedSnapToPageRunnable = r; |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 1296 | snapToPage(whichPage, duration); |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1299 | protected void snapToScreenId(long screenId, Runnable r) { |
| 1300 | snapToPage(getPageIndexForScreenId(screenId), r); |
| 1301 | } |
| 1302 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1303 | class WallpaperOffsetInterpolator implements Choreographer.FrameCallback { |
| 1304 | float mFinalOffset = 0.0f; |
Michael Jurka | 046cf34 | 2013-09-14 16:40:34 +0200 | [diff] [blame] | 1305 | float mCurrentOffset = 0.5f; // to force an initial update |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1306 | boolean mWaitingForUpdate; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1307 | Choreographer mChoreographer; |
| 1308 | Interpolator mInterpolator; |
| 1309 | boolean mAnimating; |
| 1310 | long mAnimationStartTime; |
| 1311 | float mAnimationStartOffset; |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1312 | private final int ANIMATION_DURATION = 250; |
| 1313 | // 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] | 1314 | private final int MIN_PARALLAX_PAGE_SPAN = 3; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1315 | int mNumScreens; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1316 | |
| 1317 | public WallpaperOffsetInterpolator() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1318 | mChoreographer = Choreographer.getInstance(); |
| 1319 | mInterpolator = new DecelerateInterpolator(1.5f); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1322 | @Override |
| 1323 | public void doFrame(long frameTimeNanos) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1324 | updateOffset(false); |
| 1325 | } |
| 1326 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1327 | private void updateOffset(boolean force) { |
| 1328 | if (mWaitingForUpdate || force) { |
| 1329 | mWaitingForUpdate = false; |
Michael Jurka | 1bd90b0 | 2013-09-05 23:10:14 +0200 | [diff] [blame] | 1330 | if (computeScrollOffset() && mWindowToken != null) { |
| 1331 | try { |
| 1332 | mWallpaperManager.setWallpaperOffsets(mWindowToken, |
| 1333 | mWallpaperOffset.getCurrX(), 0.5f); |
| 1334 | setWallpaperOffsetSteps(); |
| 1335 | } catch (IllegalArgumentException e) { |
| 1336 | Log.e(TAG, "Error updating wallpaper offset: " + e); |
| 1337 | } |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1338 | } |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1339 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1342 | public boolean computeScrollOffset() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1343 | final float oldOffset = mCurrentOffset; |
| 1344 | if (mAnimating) { |
| 1345 | long durationSinceAnimation = System.currentTimeMillis() - mAnimationStartTime; |
| 1346 | float t0 = durationSinceAnimation / (float) ANIMATION_DURATION; |
| 1347 | float t1 = mInterpolator.getInterpolation(t0); |
| 1348 | mCurrentOffset = mAnimationStartOffset + |
| 1349 | (mFinalOffset - mAnimationStartOffset) * t1; |
| 1350 | mAnimating = durationSinceAnimation < ANIMATION_DURATION; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1351 | } else { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1352 | mCurrentOffset = mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1353 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1354 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1355 | if (Math.abs(mCurrentOffset - mFinalOffset) > 0.0000001f) { |
| 1356 | scheduleUpdate(); |
| 1357 | } |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1358 | if (Math.abs(oldOffset - mCurrentOffset) > 0.0000001f) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1359 | return true; |
| 1360 | } |
| 1361 | return false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1364 | private float wallpaperOffsetForCurrentScroll() { |
| 1365 | if (getChildCount() <= 1) { |
| 1366 | return 0; |
| 1367 | } |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1368 | |
| 1369 | // Exclude the leftmost page |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1370 | int emptyExtraPages = numEmptyScreensToIgnore(); |
| 1371 | int firstIndex = numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1372 | // 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] | 1373 | int lastIndex = getChildCount() - 1 - emptyExtraPages; |
| 1374 | if (isLayoutRtl()) { |
| 1375 | int temp = firstIndex; |
| 1376 | firstIndex = lastIndex; |
| 1377 | lastIndex = temp; |
| 1378 | } |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1379 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1380 | int firstPageScrollX = getScrollForPage(firstIndex); |
| 1381 | int scrollRange = getScrollForPage(lastIndex) - firstPageScrollX; |
| 1382 | if (scrollRange == 0) { |
| 1383 | return 0; |
| 1384 | } else { |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1385 | // TODO: do different behavior if it's a live wallpaper? |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1386 | // Sometimes the left parameter of the pages is animated during a layout transition; |
| 1387 | // this parameter offsets it to keep the wallpaper from animating as well |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1388 | int adjustedScroll = |
Michael Jurka | 8fd3adc | 2013-10-16 13:50:24 -0700 | [diff] [blame] | 1389 | getScrollX() - firstPageScrollX - getLayoutTransitionOffsetForPage(0); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1390 | float offset = Math.min(1, adjustedScroll / (float) scrollRange); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1391 | offset = Math.max(0, offset); |
| 1392 | // Don't use up all the wallpaper parallax until you have at least |
| 1393 | // MIN_PARALLAX_PAGE_SPAN pages |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1394 | int numScrollingPages = getNumScreensExcludingEmptyAndCustom(); |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1395 | int parallaxPageSpan; |
| 1396 | if (mWallpaperIsLiveWallpaper) { |
| 1397 | parallaxPageSpan = numScrollingPages - 1; |
| 1398 | } else { |
| 1399 | parallaxPageSpan = Math.max(MIN_PARALLAX_PAGE_SPAN, numScrollingPages - 1); |
| 1400 | } |
| 1401 | mNumPagesForWallpaperParallax = parallaxPageSpan; |
| 1402 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1403 | // On RTL devices, push the wallpaper offset to the right if we don't have enough |
| 1404 | // pages (ie if numScrollingPages < MIN_PARALLAX_PAGE_SPAN) |
| 1405 | int padding = isLayoutRtl() ? parallaxPageSpan - numScrollingPages + 1 : 0; |
| 1406 | return offset * (padding + numScrollingPages - 1) / parallaxPageSpan; |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1410 | private int numEmptyScreensToIgnore() { |
| 1411 | int numScrollingPages = getChildCount() - numCustomPages(); |
| 1412 | if (numScrollingPages >= MIN_PARALLAX_PAGE_SPAN && hasExtraEmptyScreen()) { |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1413 | return 1; |
| 1414 | } else { |
| 1415 | return 0; |
| 1416 | } |
| 1417 | } |
| 1418 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1419 | private int getNumScreensExcludingEmptyAndCustom() { |
| 1420 | int numScrollingPages = getChildCount() - numEmptyScreensToIgnore() - numCustomPages(); |
Michael Jurka | 2f81731 | 2013-09-20 03:03:42 +0200 | [diff] [blame] | 1421 | return numScrollingPages; |
| 1422 | } |
| 1423 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1424 | public void syncWithScroll() { |
| 1425 | float offset = wallpaperOffsetForCurrentScroll(); |
| 1426 | mWallpaperOffset.setFinalX(offset); |
| 1427 | updateOffset(true); |
| 1428 | } |
| 1429 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1430 | public float getCurrX() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1431 | return mCurrentOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | public float getFinalX() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1435 | return mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1436 | } |
| 1437 | |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1438 | private void animateToFinal() { |
| 1439 | mAnimating = true; |
| 1440 | mAnimationStartOffset = mCurrentOffset; |
| 1441 | mAnimationStartTime = System.currentTimeMillis(); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1444 | private void setWallpaperOffsetSteps() { |
| 1445 | // Set wallpaper offset steps (1 / (number of screens - 1)) |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1446 | float xOffset = 1.0f / mNumPagesForWallpaperParallax; |
| 1447 | if (xOffset != mLastSetWallpaperOffsetSteps) { |
| 1448 | mWallpaperManager.setWallpaperOffsetSteps(xOffset, 1.0f); |
| 1449 | mLastSetWallpaperOffsetSteps = xOffset; |
| 1450 | } |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1453 | public void setFinalX(float x) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1454 | scheduleUpdate(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1455 | mFinalOffset = Math.max(0f, Math.min(x, 1.0f)); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1456 | if (getNumScreensExcludingEmptyAndCustom() != mNumScreens) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1457 | if (mNumScreens > 0) { |
| 1458 | // Don't animate if we're going from 0 screens |
| 1459 | animateToFinal(); |
| 1460 | } |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1461 | mNumScreens = getNumScreensExcludingEmptyAndCustom(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1462 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1465 | private void scheduleUpdate() { |
| 1466 | if (!mWaitingForUpdate) { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1467 | mChoreographer.postFrameCallback(this); |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1468 | mWaitingForUpdate = true; |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1469 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | public void jumpToFinal() { |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1473 | mCurrentOffset = mFinalOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1474 | } |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 1475 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1476 | |
Michael Jurka | 340c5f3 | 2010-10-21 16:49:19 -0700 | [diff] [blame] | 1477 | @Override |
| 1478 | public void computeScroll() { |
| 1479 | super.computeScroll(); |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1480 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 1483 | @Override |
| 1484 | public void announceForAccessibility(CharSequence text) { |
| 1485 | // Don't announce if apps is on top of us. |
| 1486 | if (!mLauncher.isAllAppsVisible()) { |
| 1487 | super.announceForAccessibility(text); |
| 1488 | } |
| 1489 | } |
| 1490 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1491 | void showOutlines() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1492 | if (!isSmall() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1493 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
| 1494 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1495 | mChildrenOutlineFadeInAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 1.0f); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1496 | mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION); |
| 1497 | mChildrenOutlineFadeInAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1498 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1501 | void hideOutlines() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1502 | if (!isSmall() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1503 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
| 1504 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1505 | mChildrenOutlineFadeOutAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 0.0f); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1506 | mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION); |
| 1507 | mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY); |
| 1508 | mChildrenOutlineFadeOutAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1509 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 1512 | public void showOutlinesTemporarily() { |
| 1513 | if (!mIsPageMoving && !isTouchActive()) { |
| 1514 | snapToPage(mCurrentPage); |
| 1515 | } |
| 1516 | } |
| 1517 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1518 | public void setChildrenOutlineAlpha(float alpha) { |
| 1519 | mChildrenOutlineAlpha = alpha; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1520 | for (int i = 0; i < getChildCount(); i++) { |
| 1521 | CellLayout cl = (CellLayout) getChildAt(i); |
| 1522 | cl.setBackgroundAlpha(alpha); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1526 | public float getChildrenOutlineAlpha() { |
| 1527 | return mChildrenOutlineAlpha; |
| 1528 | } |
| 1529 | |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1530 | void disableBackground() { |
| 1531 | mDrawBackground = false; |
| 1532 | } |
| 1533 | void enableBackground() { |
| 1534 | mDrawBackground = true; |
| 1535 | } |
| 1536 | |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1537 | private void animateBackgroundGradient(float finalAlpha, boolean animated) { |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 1538 | if (mBackground == null) return; |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1539 | if (mBackgroundFadeInAnimation != null) { |
| 1540 | mBackgroundFadeInAnimation.cancel(); |
| 1541 | mBackgroundFadeInAnimation = null; |
| 1542 | } |
| 1543 | if (mBackgroundFadeOutAnimation != null) { |
| 1544 | mBackgroundFadeOutAnimation.cancel(); |
| 1545 | mBackgroundFadeOutAnimation = null; |
| 1546 | } |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1547 | float startAlpha = getBackgroundAlpha(); |
| 1548 | if (finalAlpha != startAlpha) { |
| 1549 | if (animated) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 1550 | mBackgroundFadeOutAnimation = |
| 1551 | LauncherAnimUtils.ofFloat(this, startAlpha, finalAlpha); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 1552 | mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() { |
| 1553 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1554 | setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue()); |
| 1555 | } |
| 1556 | }); |
| 1557 | mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 1558 | mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION); |
| 1559 | mBackgroundFadeOutAnimation.start(); |
| 1560 | } else { |
| 1561 | setBackgroundAlpha(finalAlpha); |
| 1562 | } |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1563 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | public void setBackgroundAlpha(float alpha) { |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1567 | if (alpha != mBackgroundAlpha) { |
| 1568 | mBackgroundAlpha = alpha; |
| 1569 | invalidate(); |
| 1570 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1573 | public float getBackgroundAlpha() { |
| 1574 | return mBackgroundAlpha; |
| 1575 | } |
| 1576 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1577 | float backgroundAlphaInterpolator(float r) { |
| 1578 | float pivotA = 0.1f; |
| 1579 | float pivotB = 0.4f; |
| 1580 | if (r < pivotA) { |
| 1581 | return 0; |
| 1582 | } else if (r > pivotB) { |
| 1583 | return 1.0f; |
| 1584 | } else { |
| 1585 | return (r - pivotA)/(pivotB - pivotA); |
| 1586 | } |
| 1587 | } |
| 1588 | |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1589 | private void updatePageAlphaValues(int screenCenter) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1590 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 1591 | if (mWorkspaceFadeInAdjacentScreens && |
| 1592 | mState == State.NORMAL && |
| 1593 | !mIsSwitchingState && |
| 1594 | !isInOverscroll) { |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 1595 | for (int i = numCustomPages(); i < getChildCount(); i++) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1596 | CellLayout child = (CellLayout) getChildAt(i); |
| 1597 | if (child != null) { |
| 1598 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1599 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1600 | child.getShortcutsAndWidgets().setAlpha(alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1601 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1602 | } |
| 1603 | } |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | private void setChildrenBackgroundAlphaMultipliers(float a) { |
| 1607 | for (int i = 0; i < getChildCount(); i++) { |
| 1608 | CellLayout child = (CellLayout) getChildAt(i); |
| 1609 | child.setBackgroundAlphaMultiplier(a); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Winson Chung | 98ca0f7 | 2013-07-29 12:58:51 -0700 | [diff] [blame] | 1613 | public boolean hasCustomContent() { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1614 | return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID); |
| 1615 | } |
| 1616 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 1617 | public int numCustomPages() { |
| 1618 | return hasCustomContent() ? 1 : 0; |
| 1619 | } |
| 1620 | |
Adam Cohen | bffe745 | 2013-07-22 18:21:45 -0700 | [diff] [blame] | 1621 | public boolean isOnOrMovingToCustomContent() { |
| 1622 | return hasCustomContent() && getNextPage() == 0; |
| 1623 | } |
| 1624 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1625 | private void updateStateForCustomContent(int screenCenter) { |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1626 | float translationX = 0; |
| 1627 | float progress = 0; |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 1628 | if (hasCustomContent()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1629 | int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID); |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1630 | |
Adam Cohen | a45de07 | 2013-10-11 16:07:47 -0700 | [diff] [blame] | 1631 | int scrollDelta = getScrollX() - getScrollForPage(index) - |
| 1632 | getLayoutTransitionOffsetForPage(index); |
| 1633 | float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index); |
| 1634 | translationX = scrollRange - scrollDelta; |
| 1635 | progress = (scrollRange - scrollDelta) / scrollRange; |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1636 | |
| 1637 | if (isLayoutRtl()) { |
| 1638 | translationX = Math.min(0, translationX); |
| 1639 | } else { |
| 1640 | translationX = Math.max(0, translationX); |
| 1641 | } |
Adam Cohen | 955806d | 2013-07-19 16:36:43 -0700 | [diff] [blame] | 1642 | progress = Math.max(0, progress); |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1643 | } |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1644 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1645 | if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return; |
Adam Cohen | 84add1d | 2013-10-14 16:29:02 -0700 | [diff] [blame] | 1646 | |
| 1647 | CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); |
| 1648 | if (progress > 0 && cc.getVisibility() != VISIBLE && !isSmall()) { |
| 1649 | cc.setVisibility(VISIBLE); |
| 1650 | } |
| 1651 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1652 | mLastCustomContentScrollProgress = progress; |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1653 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1654 | setBackgroundAlpha(progress * 0.8f); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 1655 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1656 | if (mLauncher.getHotseat() != null) { |
| 1657 | mLauncher.getHotseat().setTranslationX(translationX); |
| 1658 | } |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1659 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1660 | if (getPageIndicator() != null) { |
| 1661 | getPageIndicator().setTranslationX(translationX); |
| 1662 | } |
Adam Cohen | c36fa5c | 2013-08-29 11:54:42 -0700 | [diff] [blame] | 1663 | |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 1664 | if (mCustomContentCallbacks != null) { |
| 1665 | mCustomContentCallbacks.onScrollProgressChanged(progress); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1669 | @Override |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1670 | protected OnClickListener getPageIndicatorClickListener() { |
| 1671 | AccessibilityManager am = (AccessibilityManager) |
| 1672 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 1673 | if (!am.isTouchExplorationEnabled()) { |
| 1674 | return null; |
| 1675 | } |
| 1676 | OnClickListener listener = new OnClickListener() { |
| 1677 | @Override |
| 1678 | public void onClick(View arg0) { |
| 1679 | enterOverviewMode(); |
| 1680 | } |
| 1681 | }; |
| 1682 | return listener; |
| 1683 | } |
| 1684 | |
| 1685 | @Override |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1686 | protected void screenScrolled(int screenCenter) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1687 | final boolean isRtl = isLayoutRtl(); |
Michael Jurka | a3d30ad | 2012-05-08 13:43:43 -0700 | [diff] [blame] | 1688 | super.screenScrolled(screenCenter); |
| 1689 | |
| 1690 | updatePageAlphaValues(screenCenter); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1691 | updateStateForCustomContent(screenCenter); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1692 | enableHwLayersOnVisiblePages(); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1693 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1694 | boolean shouldOverScroll = (mOverScrollX < 0 && (!hasCustomContent() || isLayoutRtl())) || |
| 1695 | (mOverScrollX > mMaxScrollX && (!hasCustomContent() || !isLayoutRtl())); |
| 1696 | |
| 1697 | if (shouldOverScroll) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1698 | int index = 0; |
| 1699 | float pivotX = 0f; |
| 1700 | final float leftBiasedPivot = 0.25f; |
| 1701 | final float rightBiasedPivot = 0.75f; |
| 1702 | final int lowerIndex = 0; |
| 1703 | final int upperIndex = getChildCount() - 1; |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 1704 | |
| 1705 | final boolean isLeftPage = mOverScrollX < 0; |
| 1706 | index = (!isRtl && isLeftPage) || (isRtl && !isLeftPage) ? lowerIndex : upperIndex; |
| 1707 | pivotX = isLeftPage ? rightBiasedPivot : leftBiasedPivot; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1708 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1709 | CellLayout cl = (CellLayout) getChildAt(index); |
| 1710 | float scrollProgress = getScrollProgress(screenCenter, cl, index); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1711 | cl.setOverScrollAmount(Math.abs(scrollProgress), isLeftPage); |
| 1712 | float rotation = -WORKSPACE_OVERSCROLL_ROTATION * scrollProgress; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1713 | cl.setRotationY(rotation); |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 1714 | |
| 1715 | if (!mOverscrollTransformsSet || Float.compare(mLastOverscrollPivotX, pivotX) != 0) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 1716 | mOverscrollTransformsSet = true; |
Adam Cohen | a29f504 | 2013-09-26 14:29:35 -0700 | [diff] [blame] | 1717 | mLastOverscrollPivotX = pivotX; |
Adam Cohen | 9430988 | 2012-06-08 16:24:56 -0700 | [diff] [blame] | 1718 | cl.setCameraDistance(mDensity * mCameraDistance); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1719 | cl.setPivotX(cl.getMeasuredWidth() * pivotX); |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 1720 | cl.setPivotY(cl.getMeasuredHeight() * 0.5f); |
| 1721 | cl.setOverscrollTransformsDirty(true); |
| 1722 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1723 | } else { |
Adam Cohen | 4de0974 | 2013-12-12 16:16:39 -0800 | [diff] [blame] | 1724 | if (mOverscrollTransformsSet && getChildCount() > 0) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 1725 | mOverscrollTransformsSet = false; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1726 | ((CellLayout) getChildAt(0)).resetOverscrollTransforms(); |
| 1727 | ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms(); |
Adam Cohen | 7842d7f | 2011-09-12 15:28:15 -0700 | [diff] [blame] | 1728 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | @Override |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1733 | protected void overScroll(float amount) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 1734 | acceleratedOverScroll(amount); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1737 | protected void onAttachedToWindow() { |
| 1738 | super.onAttachedToWindow(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1739 | mWindowToken = getWindowToken(); |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 1740 | computeScroll(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1741 | mDragController.setWindowToken(mWindowToken); |
| 1742 | } |
| 1743 | |
| 1744 | protected void onDetachedFromWindow() { |
Romain Guy | e666195 | 2013-08-08 19:13:22 -0700 | [diff] [blame] | 1745 | super.onDetachedFromWindow(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1746 | mWindowToken = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1747 | } |
| 1748 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1749 | protected void onResume() { |
| 1750 | if (getPageIndicator() != null) { |
| 1751 | // In case accessibility state has changed, we need to perform this on every |
| 1752 | // attach to window |
Adam Cohen | d36d947 | 2013-10-10 15:32:41 -0700 | [diff] [blame] | 1753 | OnClickListener listener = getPageIndicatorClickListener(); |
| 1754 | if (listener != null) { |
| 1755 | getPageIndicator().setOnClickListener(listener); |
| 1756 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1757 | } |
| 1758 | AccessibilityManager am = (AccessibilityManager) |
| 1759 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 1760 | sAccessibilityEnabled = am.isEnabled(); |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 1761 | |
| 1762 | // Update wallpaper dimensions if they were changed since last onResume |
| 1763 | // (we also always set the wallpaper dimensions in the constructor) |
| 1764 | if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) { |
| 1765 | setWallpaperDimension(); |
| 1766 | } |
Michael Jurka | 2a4f492 | 2014-01-29 16:32:39 +0100 | [diff] [blame] | 1767 | mWallpaperIsLiveWallpaper = mWallpaperManager.getWallpaperInfo() != null; |
| 1768 | // Force the wallpaper offset steps to be set again, because another app might have changed |
| 1769 | // them |
| 1770 | mLastSetWallpaperOffsetSteps = 0f; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1773 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1774 | 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] | 1775 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
Michael Jurka | fe09cb7 | 2013-08-27 15:48:58 +0200 | [diff] [blame] | 1776 | mWallpaperOffset.syncWithScroll(); |
Michael Jurka | cc07e7a | 2013-08-26 20:56:35 +0200 | [diff] [blame] | 1777 | mWallpaperOffset.jumpToFinal(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1778 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1779 | super.onLayout(changed, left, top, right, bottom); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | @Override |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1783 | protected void onDraw(Canvas canvas) { |
| 1784 | // Draw the background gradient if necessary |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1785 | if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) { |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 1786 | int alpha = (int) (mBackgroundAlpha * 255); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1787 | mBackground.setAlpha(alpha); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1788 | mBackground.setBounds(getScrollX(), 0, getScrollX() + getMeasuredWidth(), |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1789 | getMeasuredHeight()); |
| 1790 | mBackground.draw(canvas); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1791 | } |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 1792 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1793 | super.onDraw(canvas); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1794 | |
| 1795 | // Call back to LauncherModel to finish binding after the first draw |
Romain Guy | eeacd56 | 2012-10-10 18:47:33 -0700 | [diff] [blame] | 1796 | post(mBindPages); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1797 | } |
| 1798 | |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1799 | boolean isDrawingBackgroundGradient() { |
| 1800 | return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground); |
| 1801 | } |
| 1802 | |
Michael Jurka | dfab7f0 | 2011-11-18 13:01:04 -0800 | [diff] [blame] | 1803 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1804 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1805 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1806 | final Folder openFolder = getOpenFolder(); |
| 1807 | if (openFolder != null) { |
| 1808 | return openFolder.requestFocus(direction, previouslyFocusedRect); |
| 1809 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1810 | return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | @Override |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1817 | public int getDescendantFocusability() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1818 | if (isSmall()) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1819 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 1820 | } |
| 1821 | return super.getDescendantFocusability(); |
| 1822 | } |
| 1823 | |
| 1824 | @Override |
Romain Guy | c2e24c0 | 2009-06-01 16:11:41 -0700 | [diff] [blame] | 1825 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1826 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1827 | final Folder openFolder = getOpenFolder(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1828 | if (openFolder != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1829 | openFolder.addFocusables(views, direction); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1830 | } else { |
| 1831 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1836 | public boolean isSmall() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1837 | return mState == State.SMALL || mState == State.SPRING_LOADED || mState == State.OVERVIEW; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1840 | void enableChildrenCache(int fromPage, int toPage) { |
| 1841 | if (fromPage > toPage) { |
| 1842 | final int temp = fromPage; |
| 1843 | fromPage = toPage; |
| 1844 | toPage = temp; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 1845 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1846 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1847 | final int screenCount = getChildCount(); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1848 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1849 | fromPage = Math.max(fromPage, 0); |
| 1850 | toPage = Math.min(toPage, screenCount - 1); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1851 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1852 | for (int i = fromPage; i <= toPage; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1853 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1854 | layout.setChildrenDrawnWithCacheEnabled(true); |
| 1855 | layout.setChildrenDrawingCacheEnabled(true); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | void clearChildrenCache() { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1860 | final int screenCount = getChildCount(); |
| 1861 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1862 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1863 | layout.setChildrenDrawnWithCacheEnabled(false); |
Adam Cohen | 8182e5b | 2011-08-01 15:43:31 -0700 | [diff] [blame] | 1864 | // In software mode, we don't want the items to continue to be drawn into bitmaps |
| 1865 | if (!isHardwareAccelerated()) { |
| 1866 | layout.setChildrenDrawingCacheEnabled(false); |
| 1867 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1871 | private void updateChildrenLayersEnabled(boolean force) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1872 | boolean small = mState == State.SMALL || mState == State.OVERVIEW || mIsSwitchingState; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1873 | boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1874 | |
| 1875 | if (enableChildrenLayers != mChildrenLayersEnabled) { |
| 1876 | mChildrenLayersEnabled = enableChildrenLayers; |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1877 | if (mChildrenLayersEnabled) { |
| 1878 | enableHwLayersOnVisiblePages(); |
| 1879 | } else { |
| 1880 | for (int i = 0; i < getPageCount(); i++) { |
| 1881 | final CellLayout cl = (CellLayout) getChildAt(i); |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1882 | cl.enableHardwareLayer(false); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | private void enableHwLayersOnVisiblePages() { |
| 1889 | if (mChildrenLayersEnabled) { |
| 1890 | final int screenCount = getChildCount(); |
| 1891 | getVisiblePages(mTempVisiblePagesRange); |
| 1892 | int leftScreen = mTempVisiblePagesRange[0]; |
| 1893 | int rightScreen = mTempVisiblePagesRange[1]; |
| 1894 | if (leftScreen == rightScreen) { |
| 1895 | // make sure we're caching at least two pages always |
| 1896 | if (rightScreen < screenCount - 1) { |
| 1897 | rightScreen++; |
| 1898 | } else if (leftScreen > 0) { |
| 1899 | leftScreen--; |
| 1900 | } |
| 1901 | } |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1902 | |
| 1903 | final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1904 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1905 | final CellLayout layout = (CellLayout) getPageAt(i); |
Chris Craik | 01f2d7f | 2013-10-01 14:41:56 -0700 | [diff] [blame] | 1906 | |
| 1907 | // enable layers between left and right screen inclusive, except for the |
| 1908 | // customScreen, which may animate its content during transitions. |
| 1909 | boolean enableLayer = layout != customScreen && |
| 1910 | leftScreen <= i && i <= rightScreen && shouldDrawChild(layout); |
| 1911 | layout.enableHardwareLayer(enableLayer); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1912 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1913 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1916 | public void buildPageHardwareLayers() { |
| 1917 | // force layers to be enabled just for the call to buildLayer |
| 1918 | updateChildrenLayersEnabled(true); |
| 1919 | if (getWindowToken() != null) { |
| 1920 | final int childCount = getChildCount(); |
| 1921 | for (int i = 0; i < childCount; i++) { |
| 1922 | CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | d51f33a | 2012-06-28 15:35:26 -0700 | [diff] [blame] | 1923 | cl.buildHardwareLayer(); |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | updateChildrenLayersEnabled(false); |
| 1927 | } |
| 1928 | |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1929 | protected void onWallpaperTap(MotionEvent ev) { |
| 1930 | final int[] position = mTempCell; |
| 1931 | getLocationOnScreen(position); |
| 1932 | |
| 1933 | int pointerIndex = ev.getActionIndex(); |
| 1934 | position[0] += (int) ev.getX(pointerIndex); |
| 1935 | position[1] += (int) ev.getY(pointerIndex); |
| 1936 | |
| 1937 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 1938 | ev.getAction() == MotionEvent.ACTION_UP |
| 1939 | ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, |
| 1940 | position[0], position[1], 0, null); |
| 1941 | } |
| 1942 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1943 | /* |
| 1944 | * This interpolator emulates the rate at which the perceived scale of an object changes |
| 1945 | * as its distance from a camera increases. When this interpolator is applied to a scale |
| 1946 | * 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] | 1947 | * from the camera. |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1948 | */ |
| 1949 | static class ZInterpolator implements TimeInterpolator { |
| 1950 | private float focalLength; |
| 1951 | |
| 1952 | public ZInterpolator(float foc) { |
| 1953 | focalLength = foc; |
| 1954 | } |
| 1955 | |
Adam Cohen | eed565d | 2010-11-15 11:30:05 -0800 | [diff] [blame] | 1956 | public float getInterpolation(float input) { |
| 1957 | return (1.0f - focalLength / (focalLength + input)) / |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1958 | (1.0f - focalLength / (focalLength + 1.0f)); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1962 | /* |
| 1963 | * The exact reverse of ZInterpolator. |
| 1964 | */ |
| 1965 | static class InverseZInterpolator implements TimeInterpolator { |
| 1966 | private ZInterpolator zInterpolator; |
| 1967 | public InverseZInterpolator(float foc) { |
| 1968 | zInterpolator = new ZInterpolator(foc); |
| 1969 | } |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1970 | public float getInterpolation(float input) { |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1971 | return 1 - zInterpolator.getInterpolation(1 - input); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1975 | /* |
| 1976 | * ZInterpolator compounded with an ease-out. |
| 1977 | */ |
| 1978 | static class ZoomOutInterpolator implements TimeInterpolator { |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1979 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f); |
| 1980 | private final ZInterpolator zInterpolator = new ZInterpolator(0.13f); |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1981 | |
| 1982 | public float getInterpolation(float input) { |
| 1983 | return decelerate.getInterpolation(zInterpolator.getInterpolation(input)); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * InvereZInterpolator compounded with an ease-out. |
| 1989 | */ |
| 1990 | static class ZoomInInterpolator implements TimeInterpolator { |
| 1991 | private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f); |
| 1992 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f); |
| 1993 | |
| 1994 | public float getInterpolation(float input) { |
| 1995 | return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input)); |
| 1996 | } |
| 1997 | } |
| 1998 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1999 | private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 2000 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 2001 | /* |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2002 | * |
| 2003 | * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we |
| 2004 | * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace |
| 2005 | * |
| 2006 | * These methods mark the appropriate pages as accepting drops (which alters their visual |
| 2007 | * appearance). |
| 2008 | * |
| 2009 | */ |
| 2010 | public void onDragStartedWithItem(View v) { |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2011 | final Canvas canvas = new Canvas(); |
| 2012 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2013 | // The outline is used to visualize where the item will land if dropped |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2014 | mDragOutline = createDragOutline(v, canvas, DRAG_BITMAP_PADDING); |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2017 | private Rect getDrawableBounds(Drawable d) { |
| 2018 | Rect bounds = new Rect(); |
| 2019 | d.copyBounds(bounds); |
| 2020 | if (bounds.width() == 0 || bounds.height() == 0) { |
| 2021 | bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); |
| 2022 | } |
| 2023 | return bounds; |
| 2024 | } |
| 2025 | |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2026 | public void onExternalDragStartedWithItem(View v) { |
| 2027 | final Canvas canvas = new Canvas(); |
| 2028 | |
| 2029 | // Compose a drag bitmap with the view scaled to the icon size |
| 2030 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2031 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2032 | int iconSize = grid.iconSizePx; |
| 2033 | int bmpWidth = v.getMeasuredWidth(); |
| 2034 | int bmpHeight = v.getMeasuredHeight(); |
| 2035 | |
| 2036 | // If this is a text view, use its drawable instead |
| 2037 | if (v instanceof TextView) { |
| 2038 | TextView tv = (TextView) v; |
| 2039 | Drawable d = tv.getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2040 | Rect bounds = getDrawableBounds(d); |
| 2041 | bmpWidth = bounds.width(); |
| 2042 | bmpHeight = bounds.height(); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | // Compose the bitmap to create the icon from |
| 2046 | Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight, |
| 2047 | Bitmap.Config.ARGB_8888); |
| 2048 | Canvas c = new Canvas(b); |
| 2049 | drawDragView(v, c, 0, true); |
| 2050 | c.setBitmap(null); |
| 2051 | |
| 2052 | // The outline is used to visualize where the item will land if dropped |
| 2053 | mDragOutline = createDragOutline(b, canvas, DRAG_BITMAP_PADDING, iconSize, iconSize, true); |
| 2054 | } |
| 2055 | |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2056 | public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2057 | final Canvas canvas = new Canvas(); |
| 2058 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 2059 | int[] size = estimateItemSize(info.spanX, info.spanY, info, false); |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 2060 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2061 | // The outline is used to visualize where the item will land if dropped |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2062 | mDragOutline = createDragOutline(b, canvas, DRAG_BITMAP_PADDING, size[0], |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2063 | size[1], clipAlpha); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 2066 | public void exitWidgetResizeMode() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2067 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 2068 | dragLayer.clearAllResizeFrames(); |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2071 | private void initAnimationArrays() { |
| 2072 | final int childCount = getChildCount(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2073 | if (mLastChildCount == childCount) return; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2074 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2075 | mOldBackgroundAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2076 | mOldAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2077 | mNewBackgroundAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2078 | mNewAlphas = new float[childCount]; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2081 | Animator getChangeStateAnimation(final State state, boolean animated) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2082 | return getChangeStateAnimation(state, animated, 0, -1); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2085 | @Override |
| 2086 | protected void getOverviewModePages(int[] range) { |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 2087 | int start = numCustomPages(); |
Adam Cohen | 1003be9 | 2013-09-16 14:09:28 -0700 | [diff] [blame] | 2088 | int end = getChildCount() - 1; |
| 2089 | |
| 2090 | range[0] = Math.max(0, Math.min(start, getChildCount() - 1)); |
| 2091 | range[1] = Math.max(0, end); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | protected void onStartReordering() { |
| 2095 | super.onStartReordering(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2096 | showOutlines(); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 2097 | // Reordering handles its own animations, disable the automatic ones. |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 2098 | disableLayoutTransitions(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | protected void onEndReordering() { |
| 2102 | super.onEndReordering(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2103 | |
Adam Cohen | dcb173d | 2014-04-01 13:33:58 -0700 | [diff] [blame] | 2104 | if (mLauncher.isWorkspaceLoading()) { |
| 2105 | // Invalid and dangerous operation if workspace is loading |
| 2106 | return; |
| 2107 | } |
| 2108 | |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2109 | hideOutlines(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2110 | mScreenOrder.clear(); |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2111 | int count = getChildCount(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2112 | for (int i = 0; i < count; i++) { |
| 2113 | CellLayout cl = ((CellLayout) getChildAt(i)); |
| 2114 | mScreenOrder.add(getIdForScreen(cl)); |
| 2115 | } |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 2116 | |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2117 | mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); |
Adam Cohen | 22cba7f | 2013-07-19 16:14:00 -0700 | [diff] [blame] | 2118 | |
| 2119 | // Re-enable auto layout transitions for page deletion. |
Winson Chung | 964df6b | 2013-10-11 15:55:37 -0700 | [diff] [blame] | 2120 | enableLayoutTransitions(); |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2123 | public boolean isInOverviewMode() { |
| 2124 | return mState == State.OVERVIEW; |
| 2125 | } |
| 2126 | |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2127 | public boolean enterOverviewMode() { |
| 2128 | if (mTouchState != TOUCH_STATE_REST) { |
| 2129 | return false; |
| 2130 | } |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2131 | enableOverviewMode(true, -1, true); |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2132 | return true; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2135 | public void exitOverviewMode(boolean animated) { |
| 2136 | exitOverviewMode(-1, animated); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2139 | public void exitOverviewMode(int snapPage, boolean animated) { |
| 2140 | enableOverviewMode(false, snapPage, animated); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2143 | private void enableOverviewMode(boolean enable, int snapPage, boolean animated) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2144 | State finalState = Workspace.State.OVERVIEW; |
| 2145 | if (!enable) { |
| 2146 | finalState = Workspace.State.NORMAL; |
| 2147 | } |
| 2148 | |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2149 | Animator workspaceAnim = getChangeStateAnimation(finalState, animated, 0, snapPage); |
| 2150 | if (workspaceAnim != null) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2151 | onTransitionPrepare(); |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2152 | workspaceAnim.addListener(new AnimatorListenerAdapter() { |
| 2153 | @Override |
| 2154 | public void onAnimationEnd(Animator arg0) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2155 | onTransitionEnd(); |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2156 | } |
| 2157 | }); |
| 2158 | workspaceAnim.start(); |
| 2159 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2162 | int getOverviewModeTranslationY() { |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2163 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2164 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2165 | Rect overviewBar = grid.getOverviewModeButtonBarRect(); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2166 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2167 | int availableHeight = getViewportHeight(); |
| 2168 | int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight()); |
| 2169 | int offsetFromTopEdge = (availableHeight - scaledHeight) / 2; |
| 2170 | int offsetToCenterInOverview = (availableHeight - mInsets.top - overviewBar.height() |
| 2171 | - scaledHeight) / 2; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2172 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 2173 | return -offsetFromTopEdge + mInsets.top + offsetToCenterInOverview; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Winson Chung | 5841efa | 2013-09-30 18:06:44 -0700 | [diff] [blame] | 2176 | boolean shouldVoiceButtonProxyBeVisible() { |
| 2177 | if (isOnOrMovingToCustomContent()) { |
| 2178 | return false; |
| 2179 | } |
| 2180 | if (mState != State.NORMAL) { |
| 2181 | return false; |
| 2182 | } |
| 2183 | return true; |
| 2184 | } |
| 2185 | |
Adam Cohen | edaaa30 | 2013-10-01 17:33:27 -0700 | [diff] [blame] | 2186 | public void updateInteractionForState() { |
| 2187 | if (mState != State.NORMAL) { |
| 2188 | mLauncher.onInteractionBegin(); |
| 2189 | } else { |
| 2190 | mLauncher.onInteractionEnd(); |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | private void setState(State state) { |
| 2195 | mState = state; |
| 2196 | updateInteractionForState(); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2197 | updateAccessibilityFlags(); |
| 2198 | } |
| 2199 | |
| 2200 | private void updateAccessibilityFlags() { |
| 2201 | int accessible = mState == State.NORMAL ? |
| 2202 | ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES : |
| 2203 | ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; |
| 2204 | setImportantForAccessibility(accessible); |
Adam Cohen | edaaa30 | 2013-10-01 17:33:27 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2207 | Animator getChangeStateAnimation(final State state, boolean animated, int delay, int snapPage) { |
Michael Jurka | bdf7855 | 2011-10-31 14:34:25 -0700 | [diff] [blame] | 2208 | if (mState == state) { |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2209 | return null; |
Michael Jurka | 99633da | 2011-07-27 22:40:17 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2212 | // Initialize animation arrays for the first time if necessary |
| 2213 | initAnimationArrays(); |
| 2214 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 2215 | AnimatorSet anim = animated ? LauncherAnimUtils.createAnimatorSet() : null; |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 2216 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2217 | final State oldState = mState; |
| 2218 | final boolean oldStateIsNormal = (oldState == State.NORMAL); |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 2219 | final boolean oldStateIsSpringLoaded = (oldState == State.SPRING_LOADED); |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2220 | final boolean oldStateIsSmall = (oldState == State.SMALL); |
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); |
| 2225 | final boolean stateIsSmall = (state == State.SMALL); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2226 | final boolean stateIsOverview = (state == State.OVERVIEW); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2227 | float finalBackgroundAlpha = (stateIsSpringLoaded || stateIsOverview) ? 1.0f : 0f; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2228 | float finalHotseatAndPageIndicatorAlpha = (stateIsOverview || stateIsSmall) ? 0f : 1f; |
| 2229 | float finalOverviewPanelAlpha = stateIsOverview ? 1f : 0f; |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2230 | float finalSearchBarAlpha = !stateIsNormal ? 0f : 1f; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2231 | float finalWorkspaceTranslationY = stateIsOverview ? getOverviewModeTranslationY() : 0; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2232 | |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2233 | boolean workspaceToAllApps = (oldStateIsNormal && stateIsSmall); |
| 2234 | boolean allAppsToWorkspace = (oldStateIsSmall && stateIsNormal); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2235 | boolean workspaceToOverview = (oldStateIsNormal && stateIsOverview); |
| 2236 | boolean overviewToWorkspace = (oldStateIsOverview && stateIsNormal); |
| 2237 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2238 | mNewScale = 1.0f; |
Adam Cohen | beff8c6 | 2011-08-31 17:46:01 -0700 | [diff] [blame] | 2239 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2240 | if (oldStateIsOverview) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2241 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2242 | } else if (stateIsOverview) { |
| 2243 | enableFreeScroll(); |
| 2244 | } |
| 2245 | |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2246 | if (state != State.NORMAL) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2247 | if (stateIsSpringLoaded) { |
| 2248 | mNewScale = mSpringLoadedShrinkFactor; |
| 2249 | } else if (stateIsOverview) { |
| 2250 | mNewScale = mOverviewModeShrinkFactor; |
| 2251 | } else if (stateIsSmall){ |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 2252 | mNewScale = mOverviewModeShrinkFactor - 0.3f; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2253 | } |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2254 | if (workspaceToAllApps) { |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 2255 | updateChildrenLayersEnabled(false); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2256 | } |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2257 | } |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2258 | |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2259 | final int duration; |
| 2260 | if (workspaceToAllApps) { |
| 2261 | duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime); |
| 2262 | } else if (workspaceToOverview || overviewToWorkspace) { |
| 2263 | duration = getResources().getInteger(R.integer.config_overviewTransitionTime); |
| 2264 | } else { |
| 2265 | duration = getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime); |
| 2266 | } |
| 2267 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2268 | if (snapPage == -1) { |
| 2269 | snapPage = getPageNearestToCenterOfScreen(); |
| 2270 | } |
| 2271 | snapToPage(snapPage, duration, mZoomInInterpolator); |
| 2272 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2273 | for (int i = 0; i < getChildCount(); i++) { |
| 2274 | final CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2275 | boolean isCurrentPage = (i == snapPage); |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 2276 | float initialAlpha = cl.getShortcutsAndWidgets().getAlpha(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2277 | float finalAlpha; |
| 2278 | if (stateIsSmall) { |
| 2279 | finalAlpha = 0f; |
| 2280 | } else if (stateIsNormal && mWorkspaceFadeInAdjacentScreens) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2281 | finalAlpha = (i == snapPage || i < numCustomPages()) ? 1f : 0f; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2282 | } else { |
| 2283 | finalAlpha = 1f; |
| 2284 | } |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2285 | |
| 2286 | // If we are animating to/from the small state, then hide the side pages and fade the |
| 2287 | // current page in |
| 2288 | if (!mIsSwitchingState) { |
| 2289 | if (workspaceToAllApps || allAppsToWorkspace) { |
| 2290 | if (allAppsToWorkspace && isCurrentPage) { |
| 2291 | initialAlpha = 0f; |
| 2292 | } else if (!isCurrentPage) { |
| 2293 | initialAlpha = finalAlpha = 0f; |
| 2294 | } |
| 2295 | cl.setShortcutAndWidgetAlpha(initialAlpha); |
| 2296 | } |
| 2297 | } |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2298 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2299 | mOldAlphas[i] = initialAlpha; |
| 2300 | mNewAlphas[i] = finalAlpha; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2301 | if (animated) { |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2302 | mOldBackgroundAlphas[i] = cl.getBackgroundAlpha(); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2303 | mNewBackgroundAlphas[i] = finalBackgroundAlpha; |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2304 | } else { |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2305 | cl.setBackgroundAlpha(finalBackgroundAlpha); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2306 | cl.setShortcutAndWidgetAlpha(finalAlpha); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2307 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 2308 | } |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 2309 | |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2310 | final View searchBar = mLauncher.getQsbBar(); |
| 2311 | final View overviewPanel = mLauncher.getOverviewPanel(); |
| 2312 | final View hotseat = mLauncher.getHotseat(); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2313 | final View pageIndicator = getPageIndicator(); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2314 | if (animated) { |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2315 | anim.setDuration(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2316 | LauncherViewPropertyAnimator scale = new LauncherViewPropertyAnimator(this); |
| 2317 | scale.scaleX(mNewScale) |
| 2318 | .scaleY(mNewScale) |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2319 | .translationY(finalWorkspaceTranslationY) |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2320 | .setInterpolator(mZoomInInterpolator); |
| 2321 | anim.play(scale); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2322 | for (int index = 0; index < getChildCount(); index++) { |
| 2323 | final int i = index; |
| 2324 | final CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2325 | float currentAlpha = cl.getShortcutsAndWidgets().getAlpha(); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2326 | if (mOldAlphas[i] == 0 && mNewAlphas[i] == 0) { |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 2327 | cl.setBackgroundAlpha(mNewBackgroundAlphas[i]); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2328 | cl.setShortcutAndWidgetAlpha(mNewAlphas[i]); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2329 | } else { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2330 | if (mOldAlphas[i] != mNewAlphas[i] || currentAlpha != mNewAlphas[i]) { |
Michael Jurka | 6c8a579 | 2012-03-22 05:24:37 -0700 | [diff] [blame] | 2331 | LauncherViewPropertyAnimator alphaAnim = |
| 2332 | new LauncherViewPropertyAnimator(cl.getShortcutsAndWidgets()); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2333 | alphaAnim.alpha(mNewAlphas[i]) |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2334 | .setInterpolator(mZoomInInterpolator); |
| 2335 | anim.play(alphaAnim); |
| 2336 | } |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2337 | if (mOldBackgroundAlphas[i] != 0 || |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2338 | mNewBackgroundAlphas[i] != 0) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 2339 | ValueAnimator bgAnim = |
Adam Cohen | fa2450a | 2013-10-11 18:20:27 -0700 | [diff] [blame] | 2340 | LauncherAnimUtils.ofFloat(cl, 0f, 1f); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2341 | bgAnim.setInterpolator(mZoomInInterpolator); |
| 2342 | bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() { |
| 2343 | public void onAnimationUpdate(float a, float b) { |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 2344 | cl.setBackgroundAlpha( |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2345 | a * mOldBackgroundAlphas[i] + |
| 2346 | b * mNewBackgroundAlphas[i]); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 2347 | } |
| 2348 | }); |
| 2349 | anim.play(bgAnim); |
| 2350 | } |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2351 | } |
| 2352 | } |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2353 | Animator pageIndicatorAlpha = null; |
| 2354 | if (pageIndicator != null) { |
| 2355 | pageIndicatorAlpha = new LauncherViewPropertyAnimator(pageIndicator) |
| 2356 | .alpha(finalHotseatAndPageIndicatorAlpha).withLayer(); |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2357 | pageIndicatorAlpha.addListener(new AlphaUpdateListener(pageIndicator)); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2358 | } else { |
| 2359 | // create a dummy animation so we don't need to do null checks later |
| 2360 | pageIndicatorAlpha = ValueAnimator.ofFloat(0, 0); |
Winson Chung | 6670073 | 2013-08-20 16:56:15 -0700 | [diff] [blame] | 2361 | } |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2362 | |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2363 | Animator hotseatAlpha = new LauncherViewPropertyAnimator(hotseat) |
| 2364 | .alpha(finalHotseatAndPageIndicatorAlpha).withLayer(); |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2365 | hotseatAlpha.addListener(new AlphaUpdateListener(hotseat)); |
| 2366 | |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2367 | Animator searchBarAlpha = new LauncherViewPropertyAnimator(searchBar) |
| 2368 | .alpha(finalSearchBarAlpha).withLayer(); |
Michael Jurka | 1c00fd0 | 2013-12-02 16:11:22 -0800 | [diff] [blame] | 2369 | searchBarAlpha.addListener(new AlphaUpdateListener(searchBar)); |
| 2370 | |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2371 | Animator overviewPanelAlpha = new LauncherViewPropertyAnimator(overviewPanel) |
| 2372 | .alpha(finalOverviewPanelAlpha).withLayer(); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2373 | overviewPanelAlpha.addListener(new AlphaUpdateListener(overviewPanel)); |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2374 | |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2375 | if (workspaceToOverview) { |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2376 | pageIndicatorAlpha.setInterpolator(new DecelerateInterpolator(2)); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2377 | hotseatAlpha.setInterpolator(new DecelerateInterpolator(2)); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2378 | overviewPanelAlpha.setInterpolator(null); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2379 | } else if (overviewToWorkspace) { |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2380 | pageIndicatorAlpha.setInterpolator(null); |
| 2381 | hotseatAlpha.setInterpolator(null); |
Adam Cohen | 58993ad | 2013-10-11 17:57:38 -0700 | [diff] [blame] | 2382 | overviewPanelAlpha.setInterpolator(new DecelerateInterpolator(2)); |
| 2383 | } |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2384 | searchBarAlpha.setInterpolator(null); |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2385 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2386 | anim.play(overviewPanelAlpha); |
| 2387 | anim.play(hotseatAlpha); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2388 | anim.play(searchBarAlpha); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2389 | anim.play(pageIndicatorAlpha); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2390 | anim.setStartDelay(delay); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2391 | } else { |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 2392 | overviewPanel.setAlpha(finalOverviewPanelAlpha); |
| 2393 | AlphaUpdateListener.updateVisibility(overviewPanel); |
| 2394 | hotseat.setAlpha(finalHotseatAndPageIndicatorAlpha); |
| 2395 | AlphaUpdateListener.updateVisibility(hotseat); |
Michael Jurka | 853f5a4 | 2013-11-25 14:32:21 +0100 | [diff] [blame] | 2396 | if (pageIndicator != null) { |
| 2397 | pageIndicator.setAlpha(finalHotseatAndPageIndicatorAlpha); |
| 2398 | AlphaUpdateListener.updateVisibility(pageIndicator); |
Winson Chung | 6670073 | 2013-08-20 16:56:15 -0700 | [diff] [blame] | 2399 | } |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2400 | searchBar.setAlpha(finalSearchBarAlpha); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2401 | AlphaUpdateListener.updateVisibility(searchBar); |
| 2402 | updateCustomContentVisibility(); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2403 | setScaleX(mNewScale); |
| 2404 | setScaleY(mNewScale); |
| 2405 | setTranslationY(finalWorkspaceTranslationY); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2406 | } |
Winson Chung | 5841efa | 2013-09-30 18:06:44 -0700 | [diff] [blame] | 2407 | mLauncher.updateVoiceButtonProxyVisible(false); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 2408 | |
Winson Chung | 1b7c1d3 | 2011-10-25 12:09:38 -0700 | [diff] [blame] | 2409 | if (stateIsSpringLoaded) { |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 2410 | // Right now we're covered by Apps Customize |
| 2411 | // Show the background gradient immediately, so the gradient will |
| 2412 | // be showing once AppsCustomize disappears |
| 2413 | animateBackgroundGradient(getResources().getInteger( |
| 2414 | R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, false); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2415 | } else if (stateIsOverview) { |
| 2416 | animateBackgroundGradient(getResources().getInteger( |
| 2417 | R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, true); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 2418 | } else { |
| 2419 | // Fade the background gradient away |
Adam Cohen | ed307df | 2013-10-02 09:37:31 -0700 | [diff] [blame] | 2420 | animateBackgroundGradient(0f, animated); |
Michael Jurka | 7bdb25a | 2011-08-03 15:16:44 -0700 | [diff] [blame] | 2421 | } |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2422 | return anim; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 2425 | static class AlphaUpdateListener implements AnimatorUpdateListener, AnimatorListener { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2426 | View view; |
| 2427 | public AlphaUpdateListener(View v) { |
| 2428 | view = v; |
| 2429 | } |
| 2430 | |
| 2431 | @Override |
| 2432 | public void onAnimationUpdate(ValueAnimator arg0) { |
Michael Jurka | 336fd4f | 2013-09-12 00:05:02 +0200 | [diff] [blame] | 2433 | updateVisibility(view); |
| 2434 | } |
| 2435 | |
| 2436 | public static void updateVisibility(View view) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2437 | // We want to avoid the extra layout pass by setting the views to GONE unless |
| 2438 | // accessibility is on, in which case not setting them to GONE causes a glitch. |
| 2439 | int invisibleState = sAccessibilityEnabled ? GONE : INVISIBLE; |
| 2440 | if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != invisibleState) { |
| 2441 | view.setVisibility(invisibleState); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2442 | } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD |
| 2443 | && view.getVisibility() != VISIBLE) { |
| 2444 | view.setVisibility(VISIBLE); |
| 2445 | } |
| 2446 | } |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 2447 | |
| 2448 | @Override |
| 2449 | public void onAnimationCancel(Animator arg0) { |
| 2450 | } |
| 2451 | |
| 2452 | @Override |
| 2453 | public void onAnimationEnd(Animator arg0) { |
| 2454 | updateVisibility(view); |
| 2455 | } |
| 2456 | |
| 2457 | @Override |
| 2458 | public void onAnimationRepeat(Animator arg0) { |
| 2459 | } |
| 2460 | |
| 2461 | @Override |
| 2462 | public void onAnimationStart(Animator arg0) { |
| 2463 | // We want the views to be visible for animation, so fade-in/out is visible |
| 2464 | view.setVisibility(VISIBLE); |
| 2465 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2468 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 2469 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2470 | onTransitionPrepare(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 2474 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
| 2475 | } |
| 2476 | |
| 2477 | @Override |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 2478 | public void onLauncherTransitionStep(Launcher l, float t) { |
| 2479 | mTransitionProgress = t; |
| 2480 | } |
| 2481 | |
| 2482 | @Override |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2483 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2484 | onTransitionEnd(); |
| 2485 | } |
| 2486 | |
| 2487 | private void onTransitionPrepare() { |
| 2488 | mIsSwitchingState = true; |
Winson Chung | f4bd236 | 2013-10-07 17:11:27 -0700 | [diff] [blame] | 2489 | |
| 2490 | // Invalidate here to ensure that the pages are rendered during the state change transition. |
| 2491 | invalidate(); |
| 2492 | |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2493 | updateChildrenLayersEnabled(false); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2494 | hideCustomContentIfNecessary(); |
| 2495 | } |
| 2496 | |
| 2497 | void updateCustomContentVisibility() { |
| 2498 | int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE; |
| 2499 | if (hasCustomContent()) { |
| 2500 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | void showCustomContentIfNecessary() { |
| 2505 | boolean show = mState == Workspace.State.NORMAL; |
| 2506 | if (show && hasCustomContent()) { |
| 2507 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE); |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | void hideCustomContentIfNecessary() { |
| 2512 | boolean hide = mState != Workspace.State.NORMAL; |
| 2513 | if (hide && hasCustomContent()) { |
Adam Cohen | 1774a8a | 2013-11-15 10:56:16 +0000 | [diff] [blame] | 2514 | disableLayoutTransitions(); |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2515 | mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE); |
Adam Cohen | 1774a8a | 2013-11-15 10:56:16 +0000 | [diff] [blame] | 2516 | enableLayoutTransitions(); |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | private void onTransitionEnd() { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2521 | mIsSwitchingState = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 2522 | updateChildrenLayersEnabled(false); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2523 | // The code in getChangeStateAnimation to determine initialAlpha and finalAlpha will ensure |
| 2524 | // ensure that only the current page is visible during (and subsequently, after) the |
| 2525 | // transition animation. If fade adjacent pages is disabled, then re-enable the page |
| 2526 | // visibility after the transition animation. |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 2527 | if (!mWorkspaceFadeInAdjacentScreens) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2528 | for (int i = 0; i < getChildCount(); i++) { |
| 2529 | final CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 2530 | cl.setShortcutAndWidgetAlpha(1f); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2531 | } |
Adam Cohen | 76128b6 | 2013-11-14 16:43:59 +0000 | [diff] [blame] | 2532 | } else { |
| 2533 | for (int i = 0; i < numCustomPages(); i++) { |
| 2534 | final CellLayout cl = (CellLayout) getChildAt(i); |
| 2535 | cl.setShortcutAndWidgetAlpha(1f); |
| 2536 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2537 | } |
Adam Cohen | 3f452c8 | 2013-09-19 11:57:17 -0700 | [diff] [blame] | 2538 | showCustomContentIfNecessary(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | @Override |
| 2542 | public View getContent() { |
| 2543 | return this; |
| 2544 | } |
| 2545 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2546 | /** |
| 2547 | * Draw the View v into the given Canvas. |
| 2548 | * |
| 2549 | * @param v the view to draw |
| 2550 | * @param destCanvas the canvas to draw on |
| 2551 | * @param padding the horizontal and vertical padding to use when drawing |
| 2552 | */ |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2553 | private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2554 | final Rect clipRect = mTempRect; |
| 2555 | v.getDrawingRect(clipRect); |
| 2556 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2557 | boolean textVisible = false; |
| 2558 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2559 | destCanvas.save(); |
| 2560 | if (v instanceof TextView && pruneToDrawable) { |
| 2561 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2562 | Rect bounds = getDrawableBounds(d); |
| 2563 | clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2564 | destCanvas.translate(padding / 2, padding / 2); |
| 2565 | d.draw(destCanvas); |
| 2566 | } else { |
| 2567 | if (v instanceof FolderIcon) { |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2568 | // For FolderIcons the text can bleed into the icon area, and so we need to |
| 2569 | // hide the text completely (which can't be achieved by clipping). |
| 2570 | if (((FolderIcon) v).getTextVisible()) { |
| 2571 | ((FolderIcon) v).setTextVisible(false); |
| 2572 | textVisible = true; |
| 2573 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2574 | } else if (v instanceof BubbleTextView) { |
| 2575 | final BubbleTextView tv = (BubbleTextView) v; |
| 2576 | clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + |
| 2577 | tv.getLayout().getLineTop(0); |
| 2578 | } else if (v instanceof TextView) { |
| 2579 | final TextView tv = (TextView) v; |
| 2580 | clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() + |
| 2581 | tv.getLayout().getLineTop(0); |
| 2582 | } |
| 2583 | destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); |
| 2584 | destCanvas.clipRect(clipRect, Op.REPLACE); |
| 2585 | v.draw(destCanvas); |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 2586 | |
| 2587 | // Restore text visibility of FolderIcon if necessary |
| 2588 | if (textVisible) { |
| 2589 | ((FolderIcon) v).setTextVisible(true); |
| 2590 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2591 | } |
| 2592 | destCanvas.restore(); |
| 2593 | } |
| 2594 | |
| 2595 | /** |
| 2596 | * Returns a new bitmap to show when the given View is being dragged around. |
| 2597 | * Responsibility for the bitmap is transferred to the caller. |
| 2598 | */ |
| 2599 | public Bitmap createDragBitmap(View v, Canvas canvas, int padding) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2600 | Bitmap b; |
| 2601 | |
| 2602 | if (v instanceof TextView) { |
| 2603 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
Mathew Inwood | 8590029 | 2014-04-16 14:17:39 +0100 | [diff] [blame] | 2604 | Rect bounds = getDrawableBounds(d); |
| 2605 | b = Bitmap.createBitmap(bounds.width() + padding, |
| 2606 | bounds.height() + padding, Bitmap.Config.ARGB_8888); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2607 | } else { |
| 2608 | b = Bitmap.createBitmap( |
| 2609 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2612 | canvas.setBitmap(b); |
| 2613 | drawDragView(v, canvas, padding, true); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2614 | canvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2615 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2616 | return b; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | /** |
| 2620 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 2621 | * Responsibility for the bitmap is transferred to the caller. |
| 2622 | */ |
| 2623 | private Bitmap createDragOutline(View v, Canvas canvas, int padding) { |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2624 | final int outlineColor = getResources().getColor(R.color.outline_color); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2625 | final Bitmap b = Bitmap.createBitmap( |
| 2626 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 2627 | |
| 2628 | canvas.setBitmap(b); |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2629 | drawDragView(v, canvas, padding, true); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 2630 | mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2631 | canvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2632 | return b; |
| 2633 | } |
| 2634 | |
| 2635 | /** |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2636 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 2637 | * Responsibility for the bitmap is transferred to the caller. |
| 2638 | */ |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 2639 | private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h, |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2640 | boolean clipAlpha) { |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 2641 | final int outlineColor = getResources().getColor(R.color.outline_color); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2642 | final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2643 | canvas.setBitmap(b); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2644 | |
| 2645 | Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); |
| 2646 | float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), |
| 2647 | (h - padding) / (float) orig.getHeight()); |
| 2648 | int scaledWidth = (int) (scaleFactor * orig.getWidth()); |
| 2649 | int scaledHeight = (int) (scaleFactor * orig.getHeight()); |
| 2650 | Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); |
| 2651 | |
| 2652 | // center the image |
| 2653 | dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); |
| 2654 | |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 2655 | canvas.drawBitmap(orig, src, dst, null); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 2656 | mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor, |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 2657 | clipAlpha); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2658 | canvas.setBitmap(null); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2659 | |
| 2660 | return b; |
| 2661 | } |
| 2662 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2663 | void startDrag(CellLayout.CellInfo cellInfo) { |
| 2664 | View child = cellInfo.cell; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2665 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2666 | // 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] | 2667 | if (!child.isInTouchMode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2668 | return; |
| 2669 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2670 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2671 | mDragInfo = cellInfo; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2672 | child.setVisibility(INVISIBLE); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2673 | CellLayout layout = (CellLayout) child.getParent().getParent(); |
| 2674 | layout.prepareChildForDrag(child); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2675 | |
| 2676 | child.clearFocus(); |
| 2677 | child.setPressed(false); |
| 2678 | |
| 2679 | final Canvas canvas = new Canvas(); |
| 2680 | |
| 2681 | // The outline is used to visualize where the item will land if dropped |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2682 | mDragOutline = createDragOutline(child, canvas, DRAG_BITMAP_PADDING); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2683 | beginDragShared(child, this); |
| 2684 | } |
| 2685 | |
| 2686 | public void beginDragShared(View child, DragSource source) { |
Anjali Koppal | 62d18ed | 2014-03-10 17:04:03 -0700 | [diff] [blame] | 2687 | mLauncher.onDragStarted(child); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2688 | // The drag bitmap follows the touch point around on the screen |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2689 | final Bitmap b = createDragBitmap(child, new Canvas(), DRAG_BITMAP_PADDING); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2690 | |
| 2691 | final int bmpWidth = b.getWidth(); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 2692 | final int bmpHeight = b.getHeight(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2693 | |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2694 | float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2695 | int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); |
| 2696 | int dragLayerY = Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2 |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 2697 | - DRAG_BITMAP_PADDING / 2); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2698 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2699 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2700 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2701 | Point dragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2702 | Rect dragRect = null; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2703 | if (child instanceof BubbleTextView || child instanceof PagedViewIcon) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2704 | int iconSize = grid.iconSizePx; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2705 | int top = child.getPaddingTop(); |
| 2706 | int left = (bmpWidth - iconSize) / 2; |
| 2707 | int right = left + iconSize; |
| 2708 | int bottom = top + iconSize; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2709 | dragLayerY += top; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 2710 | // Note: The drag region is used to calculate drag layer offsets, but the |
| 2711 | // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2712 | dragVisualizeOffset = new Point(-DRAG_BITMAP_PADDING / 2, DRAG_BITMAP_PADDING / 2); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2713 | dragRect = new Rect(left, top, right, bottom); |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 2714 | } else if (child instanceof FolderIcon) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2715 | int previewSize = grid.folderIconSizePx; |
| 2716 | dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 2719 | // Clear the pressed state if necessary |
| 2720 | if (child instanceof BubbleTextView) { |
| 2721 | BubbleTextView icon = (BubbleTextView) child; |
| 2722 | icon.clearPressedOrFocusedBackground(); |
Winson Chung | c6c0367 | 2014-03-06 10:12:02 -0800 | [diff] [blame] | 2723 | } else if (child instanceof FolderIcon) { |
| 2724 | // Dismiss the folder cling if we haven't already |
| 2725 | mLauncher.getLauncherClings().markFolderClingDismissed(); |
Winson Chung | 1e9cbfe | 2011-09-30 16:52:26 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Adam Cohen | 2f32ad2 | 2013-11-13 11:29:49 +0000 | [diff] [blame] | 2728 | if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) { |
| 2729 | String msg = "Drag started with a view that has no tag set. This " |
| 2730 | + "will cause a crash (issue 11627249) down the line. " |
| 2731 | + "View: " + child + " tag: " + child.getTag(); |
| 2732 | throw new IllegalStateException(msg); |
| 2733 | } |
| 2734 | |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 2735 | DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2736 | DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale); |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 2737 | dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2738 | |
| 2739 | if (child.getParent() instanceof ShortcutAndWidgetContainer) { |
| 2740 | mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent(); |
| 2741 | } |
| 2742 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2743 | b.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Winson Chung | babb53e | 2014-04-14 17:12:49 -0700 | [diff] [blame] | 2746 | public void beginExternalDragShared(View child, DragSource source) { |
| 2747 | LauncherAppState app = LauncherAppState.getInstance(); |
| 2748 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 2749 | int iconSize = grid.iconSizePx; |
| 2750 | |
| 2751 | // Notify launcher of drag start |
| 2752 | mLauncher.onDragStarted(child); |
| 2753 | |
| 2754 | // Compose a new drag bitmap that is of the icon size |
| 2755 | final Bitmap tmpB = createDragBitmap(child, new Canvas(), DRAG_BITMAP_PADDING); |
| 2756 | Bitmap b = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); |
| 2757 | Paint p = new Paint(); |
| 2758 | p.setFilterBitmap(true); |
| 2759 | Canvas c = new Canvas(b); |
| 2760 | c.drawBitmap(tmpB, new Rect(0, 0, tmpB.getWidth(), tmpB.getHeight()), |
| 2761 | new Rect(0, 0, iconSize, iconSize), p); |
| 2762 | c.setBitmap(null); |
| 2763 | |
| 2764 | // Find the child's location on the screen |
| 2765 | int bmpWidth = tmpB.getWidth(); |
| 2766 | float iconScale = (float) bmpWidth / iconSize; |
| 2767 | float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY) * iconScale; |
| 2768 | int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); |
| 2769 | int dragLayerY = Math.round(mTempXY[1]); |
| 2770 | |
| 2771 | // Note: The drag region is used to calculate drag layer offsets, but the |
| 2772 | // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. |
| 2773 | Point dragVisualizeOffset = new Point(-DRAG_BITMAP_PADDING / 2, DRAG_BITMAP_PADDING / 2); |
| 2774 | Rect dragRect = new Rect(0, 0, iconSize, iconSize); |
| 2775 | |
| 2776 | if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) { |
| 2777 | String msg = "Drag started with a view that has no tag set. This " |
| 2778 | + "will cause a crash (issue 11627249) down the line. " |
| 2779 | + "View: " + child + " tag: " + child.getTag(); |
| 2780 | throw new IllegalStateException(msg); |
| 2781 | } |
| 2782 | |
| 2783 | // Start the drag |
| 2784 | DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), |
| 2785 | DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale); |
| 2786 | dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); |
| 2787 | |
| 2788 | // Recycle temporary bitmaps |
| 2789 | tmpB.recycle(); |
| 2790 | } |
| 2791 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2792 | void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, long screenId, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2793 | int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) { |
| 2794 | View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2795 | |
| 2796 | final int[] cellXY = new int[2]; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2797 | target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2798 | addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1, insertAtFirst); |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 2799 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2800 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, cellXY[0], |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2801 | cellXY[1]); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2802 | } |
| 2803 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2804 | public boolean transitionStateShouldAllowDrop() { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 2805 | return ((!isSwitchingState() || mTransitionProgress > 0.5f) && mState != State.SMALL); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2808 | /** |
| 2809 | * {@inheritDoc} |
| 2810 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2811 | public boolean acceptDrop(DragObject d) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2812 | // 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] | 2813 | CellLayout dropTargetLayout = mDropToLayout; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2814 | if (d.dragSource != this) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2815 | // 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] | 2816 | if (dropTargetLayout == null) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2817 | return false; |
| 2818 | } |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2819 | if (!transitionStateShouldAllowDrop()) return false; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2820 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2821 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
| 2822 | d.dragView, mDragViewVisualCenter); |
| 2823 | |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2824 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2825 | if (mLauncher.isHotseatLayout(dropTargetLayout)) { |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 2826 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2827 | } else { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2828 | mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); |
Adam Cohen | 2e9f4fb | 2011-08-30 22:46:55 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2831 | int spanX = 1; |
| 2832 | int spanY = 1; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2833 | if (mDragInfo != null) { |
| 2834 | final CellLayout.CellInfo dragCellInfo = mDragInfo; |
| 2835 | spanX = dragCellInfo.spanX; |
| 2836 | spanY = dragCellInfo.spanY; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2837 | } else { |
| 2838 | final ItemInfo dragInfo = (ItemInfo) d.dragInfo; |
| 2839 | spanX = dragInfo.spanX; |
| 2840 | spanY = dragInfo.spanY; |
| 2841 | } |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2842 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 2843 | int minSpanX = spanX; |
| 2844 | int minSpanY = spanY; |
| 2845 | if (d.dragInfo instanceof PendingAddWidgetInfo) { |
| 2846 | minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX; |
| 2847 | minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY; |
| 2848 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2849 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2850 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2851 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout, |
Adam Cohen | f0777b9 | 2012-02-28 14:02:45 -0800 | [diff] [blame] | 2852 | mTargetCell); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2853 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2854 | mDragViewVisualCenter[1], mTargetCell); |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 2855 | if (mCreateUserFolderOnDrop && willCreateUserFolder((ItemInfo) d.dragInfo, |
| 2856 | dropTargetLayout, mTargetCell, distance, true)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2857 | return true; |
| 2858 | } |
Adam Cohen | ec0d61d | 2014-02-07 16:34:51 -0800 | [diff] [blame] | 2859 | |
| 2860 | if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder((ItemInfo) d.dragInfo, |
| 2861 | dropTargetLayout, mTargetCell, distance)) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2862 | return true; |
| 2863 | } |
| 2864 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2865 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 2866 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2867 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 2868 | null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP); |
| 2869 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
| 2870 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2871 | // 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] | 2872 | if (!foundCell) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2873 | // Don't show the message if we are dropping on the AllApps button and the hotseat |
| 2874 | // is full |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 2875 | boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 2876 | if (mTargetCell != null && isHotseat) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2877 | Hotseat hotseat = mLauncher.getHotseat(); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 2878 | if (hotseat.isAllAppsButtonRank( |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 2879 | hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) { |
| 2880 | return false; |
| 2881 | } |
| 2882 | } |
| 2883 | |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 2884 | mLauncher.showOutOfSpaceMessage(isHotseat); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2885 | return false; |
| 2886 | } |
| 2887 | } |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 2888 | |
| 2889 | long screenId = getIdForScreen(dropTargetLayout); |
| 2890 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
| 2891 | commitExtraEmptyScreen(); |
| 2892 | } |
| 2893 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2894 | return true; |
| 2895 | } |
| 2896 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2897 | boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, float |
| 2898 | distance, boolean considerTimeout) { |
| 2899 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2900 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
| 2901 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2902 | if (dropOverView != null) { |
| 2903 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
| 2904 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) { |
| 2905 | return false; |
| 2906 | } |
| 2907 | } |
| 2908 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2909 | boolean hasntMoved = false; |
| 2910 | if (mDragInfo != null) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2911 | hasntMoved = dropOverView == mDragInfo.cell; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2912 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2913 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2914 | if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { |
| 2915 | return false; |
| 2916 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2917 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2918 | boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2919 | boolean willBecomeShortcut = |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2920 | (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 2921 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2922 | |
| 2923 | return (aboveShortcut && willBecomeShortcut); |
| 2924 | } |
| 2925 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2926 | boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell, |
| 2927 | float distance) { |
| 2928 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2929 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2930 | |
| 2931 | if (dropOverView != null) { |
| 2932 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); |
| 2933 | if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.tmpCellY)) { |
| 2934 | return false; |
| 2935 | } |
| 2936 | } |
| 2937 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2938 | if (dropOverView instanceof FolderIcon) { |
| 2939 | FolderIcon fi = (FolderIcon) dropOverView; |
| 2940 | if (fi.acceptDrop(dragInfo)) { |
| 2941 | return true; |
| 2942 | } |
| 2943 | } |
| 2944 | return false; |
| 2945 | } |
| 2946 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2947 | boolean createUserFolderIfNecessary(View newView, long container, CellLayout target, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 2948 | int[] targetCell, float distance, boolean external, DragView dragView, |
| 2949 | Runnable postAnimationRunnable) { |
| 2950 | if (distance > mMaxDistanceForFolderCreation) return false; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2951 | View v = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 2952 | |
Winson Chung | ec9a0a4 | 2011-07-20 20:42:13 -0700 | [diff] [blame] | 2953 | boolean hasntMoved = false; |
| 2954 | if (mDragInfo != null) { |
| 2955 | CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); |
| 2956 | hasntMoved = (mDragInfo.cellX == targetCell[0] && |
| 2957 | mDragInfo.cellY == targetCell[1]) && (cellParent == target); |
| 2958 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2959 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2960 | if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; |
| 2961 | mCreateUserFolderOnDrop = false; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2962 | final long screenId = (targetCell == null) ? mDragInfo.screenId : getIdForScreen(target); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2963 | |
| 2964 | boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); |
| 2965 | boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); |
| 2966 | |
| 2967 | if (aboveShortcut && willBecomeShortcut) { |
| 2968 | ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); |
| 2969 | ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); |
| 2970 | // if the drag started here, we need to remove it from the workspace |
| 2971 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2972 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2975 | Rect folderLocation = new Rect(); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2976 | float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2977 | target.removeView(v); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2978 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2979 | FolderIcon fi = |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2980 | mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 2981 | destInfo.cellX = -1; |
| 2982 | destInfo.cellY = -1; |
| 2983 | sourceInfo.cellX = -1; |
| 2984 | sourceInfo.cellY = -1; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2985 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 2986 | // If the dragView is null, we can't animate |
| 2987 | boolean animate = dragView != null; |
| 2988 | if (animate) { |
| 2989 | fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale, |
| 2990 | postAnimationRunnable); |
| 2991 | } else { |
| 2992 | fi.addItem(destInfo); |
| 2993 | fi.addItem(sourceInfo); |
| 2994 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2995 | return true; |
| 2996 | } |
| 2997 | return false; |
| 2998 | } |
| 2999 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 3000 | boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3001 | float distance, DragObject d, boolean external) { |
| 3002 | if (distance > mMaxDistanceForFolderCreation) return false; |
| 3003 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 3004 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3005 | if (!mAddToExistingFolderOnDrop) return false; |
| 3006 | mAddToExistingFolderOnDrop = false; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3007 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3008 | if (dropOverView instanceof FolderIcon) { |
| 3009 | FolderIcon fi = (FolderIcon) dropOverView; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3010 | if (fi.acceptDrop(d.dragInfo)) { |
| 3011 | fi.onDrop(d); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3012 | |
| 3013 | // if the drag started here, we need to remove it from the workspace |
| 3014 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3015 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3016 | } |
| 3017 | return true; |
| 3018 | } |
| 3019 | } |
| 3020 | return false; |
| 3021 | } |
| 3022 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3023 | public void onDrop(final DragObject d) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3024 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3025 | mDragViewVisualCenter); |
| 3026 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3027 | CellLayout dropTargetLayout = mDropToLayout; |
| 3028 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3029 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3030 | if (dropTargetLayout != null) { |
| 3031 | if (mLauncher.isHotseatLayout(dropTargetLayout)) { |
Adam Cohen | bb00ff2 | 2012-07-12 15:43:01 -0700 | [diff] [blame] | 3032 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3033 | } else { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3034 | mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3035 | } |
Adam Cohen | ba78161 | 2011-05-09 14:37:39 -0700 | [diff] [blame] | 3036 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 3037 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3038 | int snapScreen = -1; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3039 | boolean resizeOnDrop = false; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3040 | if (d.dragSource != this) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3041 | final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], |
| 3042 | (int) mDragViewVisualCenter[1] }; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3043 | onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3044 | } else if (mDragInfo != null) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3045 | final View cell = mDragInfo.cell; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3046 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3047 | Runnable resizeRunnable = null; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 3048 | if (dropTargetLayout != null && !d.cancelled) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3049 | // Move internally |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 3050 | boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout); |
| 3051 | boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
| 3052 | long container = hasMovedIntoHotseat ? |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3053 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 3054 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3055 | long screenId = (mTargetCell[0] < 0) ? |
| 3056 | mDragInfo.screenId : getIdForScreen(dropTargetLayout); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3057 | int spanX = mDragInfo != null ? mDragInfo.spanX : 1; |
| 3058 | int spanY = mDragInfo != null ? mDragInfo.spanY : 1; |
| 3059 | // First we find the cell nearest to point at which the item is |
| 3060 | // dropped, without any consideration to whether there is an item there. |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3061 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3062 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) |
| 3063 | mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3064 | float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3065 | mDragViewVisualCenter[1], mTargetCell); |
| 3066 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3067 | // If the item being dropped is a shortcut and the nearest drop |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3068 | // 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] | 3069 | if (!mInScrollArea && createUserFolderIfNecessary(cell, container, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3070 | dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3071 | return; |
| 3072 | } |
| 3073 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3074 | if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, |
| 3075 | distance, d, false)) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3076 | return; |
| 3077 | } |
| 3078 | |
| 3079 | // Aside from the special case where we're dropping a shortcut onto a shortcut, |
| 3080 | // we need to find the nearest cell location that is vacant |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3081 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 3082 | int minSpanX = item.spanX; |
| 3083 | int minSpanY = item.spanY; |
| 3084 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3085 | minSpanX = item.minSpanX; |
| 3086 | minSpanY = item.minSpanY; |
| 3087 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3088 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3089 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3090 | mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3091 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, |
| 3092 | mTargetCell, resultSpan, CellLayout.MODE_ON_DROP); |
| 3093 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3094 | boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3095 | |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3096 | // if the widget resizes on drop |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3097 | if (foundCell && (cell instanceof AppWidgetHostView) && |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3098 | (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3099 | resizeOnDrop = true; |
| 3100 | item.spanX = resultSpan[0]; |
| 3101 | item.spanY = resultSpan[1]; |
Adam Cohen | 7bdfc97 | 2012-05-22 16:50:35 -0700 | [diff] [blame] | 3102 | AppWidgetHostView awhv = (AppWidgetHostView) cell; |
| 3103 | AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], |
| 3104 | resultSpan[1]); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3105 | } |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3106 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3107 | if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) { |
| 3108 | snapScreen = getPageIndexForScreenId(screenId); |
| 3109 | snapToPage(snapScreen); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3112 | if (foundCell) { |
| 3113 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 3114 | if (hasMovedLayouts) { |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3115 | // Reparent the view |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3116 | getParentCellLayoutForView(cell).removeView(cell); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3117 | addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1], |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3118 | info.spanX, info.spanY); |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | // update the item's position after drop |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3122 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3123 | lp.cellX = lp.tmpCellX = mTargetCell[0]; |
| 3124 | lp.cellY = lp.tmpCellY = mTargetCell[1]; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3125 | lp.cellHSpan = item.spanX; |
| 3126 | lp.cellVSpan = item.spanY; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3127 | lp.isLockedToGrid = true; |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 3128 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3129 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 3130 | cell instanceof LauncherAppWidgetHostView) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3131 | final CellLayout cellLayout = dropTargetLayout; |
| 3132 | // We post this call so that the widget has a chance to be placed |
| 3133 | // in its final location |
| 3134 | |
| 3135 | final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; |
| 3136 | AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo(); |
Adam Cohen | c0a5df9 | 2012-04-02 16:53:26 -0700 | [diff] [blame] | 3137 | if (pinfo != null && |
| 3138 | pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3139 | final Runnable addResizeFrame = new Runnable() { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3140 | public void run() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3141 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3142 | dragLayer.addResizeFrame(info, hostView, cellLayout); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3143 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3144 | }; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3145 | resizeRunnable = (new Runnable() { |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3146 | public void run() { |
| 3147 | if (!isPageMoving()) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3148 | addResizeFrame.run(); |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3149 | } else { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3150 | mDelayedResizeRunnable = addResizeFrame; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 3151 | } |
| 3152 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 3153 | }); |
| 3154 | } |
| 3155 | } |
| 3156 | |
Adam Cohen | 949debe | 2013-09-29 14:22:48 -0700 | [diff] [blame] | 3157 | LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX, |
| 3158 | lp.cellY, item.spanX, item.spanY); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3159 | } else { |
| 3160 | // If we can't find a drop location, we return the item to its original position |
| 3161 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
| 3162 | mTargetCell[0] = lp.cellX; |
| 3163 | mTargetCell[1] = lp.cellY; |
Adam Cohen | f1dcdf6 | 2012-05-10 16:51:52 -0700 | [diff] [blame] | 3164 | CellLayout layout = (CellLayout) cell.getParent().getParent(); |
| 3165 | layout.markCellsAsOccupiedForView(cell); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 3166 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3167 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3168 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 3169 | final CellLayout parent = (CellLayout) cell.getParent().getParent(); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3170 | final Runnable finalResizeRunnable = resizeRunnable; |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3171 | // Prepare it to be animated into its new position |
| 3172 | // This must be called after the view has been re-parented |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3173 | final Runnable onCompleteRunnable = new Runnable() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3174 | @Override |
| 3175 | public void run() { |
| 3176 | mAnimatingViewIntoPlace = false; |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 3177 | updateChildrenLayersEnabled(false); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3178 | if (finalResizeRunnable != null) { |
| 3179 | finalResizeRunnable.run(); |
| 3180 | } |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3181 | } |
| 3182 | }; |
| 3183 | mAnimatingViewIntoPlace = true; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3184 | if (d.dragView.hasDrawn()) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3185 | final ItemInfo info = (ItemInfo) cell.getTag(); |
| 3186 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) { |
| 3187 | int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : |
| 3188 | ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
| 3189 | animateWidgetDrop(info, parent, d.dragView, |
| 3190 | onCompleteRunnable, animationType, cell, false); |
| 3191 | } else { |
Adam Cohen | 85b467b | 2012-02-29 15:38:46 -0800 | [diff] [blame] | 3192 | int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION; |
| 3193 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, |
| 3194 | onCompleteRunnable, this); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3195 | } |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3196 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 3197 | d.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 3198 | cell.setVisibility(VISIBLE); |
| 3199 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3200 | parent.onDropChild(cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3201 | } |
| 3202 | } |
| 3203 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3204 | public void setFinalScrollForPageChange(int pageIndex) { |
| 3205 | CellLayout cl = (CellLayout) getChildAt(pageIndex); |
| 3206 | if (cl != null) { |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3207 | mSavedScrollX = getScrollX(); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3208 | mSavedTranslationX = cl.getTranslationX(); |
| 3209 | mSavedRotationY = cl.getRotationY(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 3210 | final int newX = getScrollForPage(pageIndex); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3211 | setScrollX(newX); |
| 3212 | cl.setTranslationX(0f); |
| 3213 | cl.setRotationY(0f); |
| 3214 | } |
| 3215 | } |
| 3216 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3217 | public void resetFinalScrollForPageChange(int pageIndex) { |
| 3218 | if (pageIndex >= 0) { |
| 3219 | CellLayout cl = (CellLayout) getChildAt(pageIndex); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 3220 | setScrollX(mSavedScrollX); |
| 3221 | cl.setTranslationX(mSavedTranslationX); |
| 3222 | cl.setRotationY(mSavedRotationY); |
| 3223 | } |
| 3224 | } |
| 3225 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3226 | public void getViewLocationRelativeToSelf(View v, int[] location) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3227 | getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3228 | int x = location[0]; |
| 3229 | int y = location[1]; |
| 3230 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3231 | v.getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3232 | int vX = location[0]; |
| 3233 | int vY = location[1]; |
| 3234 | |
| 3235 | location[0] = vX - x; |
| 3236 | location[1] = vY - y; |
| 3237 | } |
| 3238 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3239 | public void onDragEnter(DragObject d) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3240 | mDragEnforcer.onDragEnter(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3241 | mCreateUserFolderOnDrop = false; |
| 3242 | mAddToExistingFolderOnDrop = false; |
| 3243 | |
| 3244 | mDropToLayout = null; |
| 3245 | CellLayout layout = getCurrentDropLayout(); |
| 3246 | setCurrentDropLayout(layout); |
| 3247 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 3248 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3249 | // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we |
| 3250 | // don't need to show the outlines |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 3251 | if (LauncherAppState.getInstance().isScreenLarge()) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3252 | showOutlines(); |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3253 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3254 | } |
| 3255 | |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3256 | /** Return a rect that has the cellWidth/cellHeight (left, top), and |
| 3257 | * widthGap/heightGap (right, bottom) */ |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3258 | static Rect getCellLayoutMetrics(Launcher launcher, int orientation) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3259 | LauncherAppState app = LauncherAppState.getInstance(); |
| 3260 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 3261 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3262 | Resources res = launcher.getResources(); |
| 3263 | Display display = launcher.getWindowManager().getDefaultDisplay(); |
| 3264 | Point smallestSize = new Point(); |
| 3265 | Point largestSize = new Point(); |
| 3266 | display.getCurrentSizeRange(smallestSize, largestSize); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 3267 | int countX = (int) grid.numColumns; |
| 3268 | int countY = (int) grid.numRows; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3269 | if (orientation == CellLayout.LANDSCAPE) { |
| 3270 | if (mLandscapeCellLayoutMetrics == null) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3271 | Rect padding = grid.getWorkspacePadding(CellLayout.LANDSCAPE); |
Winson Chung | dcd27ba | 2013-12-11 15:28:15 -0800 | [diff] [blame] | 3272 | int width = largestSize.x - padding.left - padding.right; |
| 3273 | int height = smallestSize.y - padding.top - padding.bottom; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3274 | mLandscapeCellLayoutMetrics = new Rect(); |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3275 | mLandscapeCellLayoutMetrics.set( |
| 3276 | grid.calculateCellWidth(width, countX), |
| 3277 | grid.calculateCellHeight(height, countY), 0, 0); |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3278 | } |
| 3279 | return mLandscapeCellLayoutMetrics; |
| 3280 | } else if (orientation == CellLayout.PORTRAIT) { |
| 3281 | if (mPortraitCellLayoutMetrics == null) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 3282 | Rect padding = grid.getWorkspacePadding(CellLayout.PORTRAIT); |
Winson Chung | dcd27ba | 2013-12-11 15:28:15 -0800 | [diff] [blame] | 3283 | int width = smallestSize.x - padding.left - padding.right; |
| 3284 | int height = largestSize.y - padding.top - padding.bottom; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3285 | mPortraitCellLayoutMetrics = new Rect(); |
Winson Chung | fe411c8 | 2013-09-26 16:07:17 -0700 | [diff] [blame] | 3286 | mPortraitCellLayoutMetrics.set( |
| 3287 | grid.calculateCellWidth(width, countX), |
| 3288 | grid.calculateCellHeight(height, countY), 0, 0); |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 3289 | } |
| 3290 | return mPortraitCellLayoutMetrics; |
| 3291 | } |
| 3292 | return null; |
| 3293 | } |
| 3294 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3295 | public void onDragExit(DragObject d) { |
| 3296 | mDragEnforcer.onDragExit(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3297 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3298 | // Here we store the final page that will be dropped to, if the workspace in fact |
| 3299 | // receives the drop |
| 3300 | if (mInScrollArea) { |
Winson Chung | cc1cfe4 | 2012-06-18 15:09:17 -0700 | [diff] [blame] | 3301 | if (isPageMoving()) { |
| 3302 | // If the user drops while the page is scrolling, we should use that page as the |
| 3303 | // destination instead of the page that is being hovered over. |
| 3304 | mDropToLayout = (CellLayout) getPageAt(getNextPage()); |
| 3305 | } else { |
| 3306 | mDropToLayout = mDragOverlappingLayout; |
| 3307 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3308 | } else { |
| 3309 | mDropToLayout = mDragTargetLayout; |
| 3310 | } |
| 3311 | |
| 3312 | if (mDragMode == DRAG_MODE_CREATE_FOLDER) { |
| 3313 | mCreateUserFolderOnDrop = true; |
| 3314 | } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 3315 | mAddToExistingFolderOnDrop = true; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3316 | } |
| 3317 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3318 | // Reset the scroll area and previous drag target |
| 3319 | onResetScrollArea(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3320 | setCurrentDropLayout(null); |
| 3321 | setCurrentDragOverlappingLayout(null); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3322 | |
Adam Cohen | 74c28d1 | 2011-11-18 14:17:11 -0800 | [diff] [blame] | 3323 | mSpringLoadedDragController.cancel(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3324 | |
| 3325 | if (!mIsPageMoving) { |
| 3326 | hideOutlines(); |
| 3327 | } |
| 3328 | } |
| 3329 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3330 | void setCurrentDropLayout(CellLayout layout) { |
| 3331 | if (mDragTargetLayout != null) { |
| 3332 | mDragTargetLayout.revertTempState(); |
| 3333 | mDragTargetLayout.onDragExit(); |
| 3334 | } |
| 3335 | mDragTargetLayout = layout; |
| 3336 | if (mDragTargetLayout != null) { |
| 3337 | mDragTargetLayout.onDragEnter(); |
| 3338 | } |
| 3339 | cleanupReorder(true); |
| 3340 | cleanupFolderCreation(); |
| 3341 | setCurrentDropOverCell(-1, -1); |
| 3342 | } |
| 3343 | |
| 3344 | void setCurrentDragOverlappingLayout(CellLayout layout) { |
| 3345 | if (mDragOverlappingLayout != null) { |
| 3346 | mDragOverlappingLayout.setIsDragOverlapping(false); |
| 3347 | } |
| 3348 | mDragOverlappingLayout = layout; |
| 3349 | if (mDragOverlappingLayout != null) { |
| 3350 | mDragOverlappingLayout.setIsDragOverlapping(true); |
| 3351 | } |
| 3352 | invalidate(); |
| 3353 | } |
| 3354 | |
| 3355 | void setCurrentDropOverCell(int x, int y) { |
| 3356 | if (x != mDragOverX || y != mDragOverY) { |
| 3357 | mDragOverX = x; |
| 3358 | mDragOverY = y; |
| 3359 | setDragMode(DRAG_MODE_NONE); |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | void setDragMode(int dragMode) { |
| 3364 | if (dragMode != mDragMode) { |
| 3365 | if (dragMode == DRAG_MODE_NONE) { |
| 3366 | cleanupAddToFolder(); |
| 3367 | // We don't want to cancel the re-order alarm every time the target cell changes |
| 3368 | // as this feels to slow / unresponsive. |
| 3369 | cleanupReorder(false); |
| 3370 | cleanupFolderCreation(); |
| 3371 | } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) { |
| 3372 | cleanupReorder(true); |
| 3373 | cleanupFolderCreation(); |
| 3374 | } else if (dragMode == DRAG_MODE_CREATE_FOLDER) { |
| 3375 | cleanupAddToFolder(); |
| 3376 | cleanupReorder(true); |
| 3377 | } else if (dragMode == DRAG_MODE_REORDER) { |
| 3378 | cleanupAddToFolder(); |
| 3379 | cleanupFolderCreation(); |
| 3380 | } |
| 3381 | mDragMode = dragMode; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | private void cleanupFolderCreation() { |
| 3386 | if (mDragFolderRingAnimator != null) { |
| 3387 | mDragFolderRingAnimator.animateToNaturalState(); |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3388 | mDragFolderRingAnimator = null; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3389 | } |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3390 | mFolderCreationAlarm.setOnAlarmListener(null); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3391 | mFolderCreationAlarm.cancelAlarm(); |
| 3392 | } |
| 3393 | |
| 3394 | private void cleanupAddToFolder() { |
| 3395 | if (mDragOverFolderIcon != null) { |
| 3396 | mDragOverFolderIcon.onDragExit(null); |
| 3397 | mDragOverFolderIcon = null; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | private void cleanupReorder(boolean cancelAlarm) { |
| 3402 | // Any pending reorders are canceled |
| 3403 | if (cancelAlarm) { |
| 3404 | mReorderAlarm.cancelAlarm(); |
| 3405 | } |
| 3406 | mLastReorderX = -1; |
| 3407 | mLastReorderY = -1; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3410 | /* |
| 3411 | * |
| 3412 | * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's |
| 3413 | * coordinate space. The argument xy is modified with the return result. |
| 3414 | * |
| 3415 | * 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] | 3416 | * computing it itself; we use this to avoid redundant matrix inversions in |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3417 | * findMatchingPageForDragOver |
| 3418 | * |
| 3419 | */ |
| 3420 | void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3421 | xy[0] = xy[0] - v.getLeft(); |
| 3422 | xy[1] = xy[1] - v.getTop(); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3425 | boolean isPointInSelfOverHotseat(int x, int y, Rect r) { |
| 3426 | if (r == null) { |
| 3427 | r = new Rect(); |
| 3428 | } |
| 3429 | mTempPt[0] = x; |
| 3430 | mTempPt[1] = y; |
| 3431 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true); |
Winson Chung | abedd9f | 2013-09-24 15:41:09 -0700 | [diff] [blame] | 3432 | |
| 3433 | LauncherAppState app = LauncherAppState.getInstance(); |
| 3434 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 3435 | r = grid.getHotseatRect(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3436 | if (r.contains(mTempPt[0], mTempPt[1])) { |
| 3437 | return true; |
| 3438 | } |
| 3439 | return false; |
| 3440 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3441 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3442 | void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3443 | mTempPt[0] = (int) xy[0]; |
| 3444 | mTempPt[1] = (int) xy[1]; |
| 3445 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempPt, true); |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 3446 | mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempPt); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3447 | |
| 3448 | xy[0] = mTempPt[0]; |
| 3449 | xy[1] = mTempPt[1]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3452 | /* |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3453 | * |
| 3454 | * Convert the 2D coordinate xy from this CellLayout's coordinate space to |
| 3455 | * the parent View's coordinate space. The argument xy is modified with the return result. |
| 3456 | * |
| 3457 | */ |
| 3458 | void mapPointFromChildToSelf(View v, float[] xy) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3459 | xy[0] += v.getLeft(); |
| 3460 | xy[1] += v.getTop(); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3461 | } |
| 3462 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3463 | static private float squaredDistance(float[] point1, float[] point2) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3464 | float distanceX = point1[0] - point2[0]; |
| 3465 | float distanceY = point2[1] - point2[1]; |
| 3466 | return distanceX * distanceX + distanceY * distanceY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3467 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3468 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3469 | /* |
| 3470 | * |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3471 | * This method returns the CellLayout that is currently being dragged to. In order to drag |
| 3472 | * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second |
| 3473 | * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one |
| 3474 | * |
| 3475 | * Return null if no CellLayout is currently being dragged over |
| 3476 | * |
| 3477 | */ |
| 3478 | private CellLayout findMatchingPageForDragOver( |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3479 | DragView dragView, float originX, float originY, boolean exact) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3480 | // We loop through all the screens (ie CellLayouts) and see which ones overlap |
| 3481 | // 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] | 3482 | final int screenCount = getChildCount(); |
| 3483 | CellLayout bestMatchingScreen = null; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3484 | float smallestDistSoFar = Float.MAX_VALUE; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3485 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3486 | for (int i = 0; i < screenCount; i++) { |
Adam Cohen | df041bc | 2013-09-22 12:32:22 -0700 | [diff] [blame] | 3487 | // The custom content screen is not a valid drag over option |
| 3488 | if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) { |
| 3489 | continue; |
| 3490 | } |
| 3491 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3492 | CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3493 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3494 | final float[] touchXy = {originX, originY}; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3495 | // Transform the touch coordinates to the CellLayout's local coordinates |
| 3496 | // 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] | 3497 | cl.getMatrix().invert(mTempInverseMatrix); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3498 | mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3499 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3500 | if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && |
| 3501 | touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { |
| 3502 | return cl; |
| 3503 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3504 | |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 3505 | if (!exact) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3506 | // Get the center of the cell layout in screen coordinates |
| 3507 | final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; |
| 3508 | cellLayoutCenter[0] = cl.getWidth()/2; |
| 3509 | cellLayoutCenter[1] = cl.getHeight()/2; |
| 3510 | mapPointFromChildToSelf(cl, cellLayoutCenter); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3511 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3512 | touchXy[0] = originX; |
| 3513 | touchXy[1] = originY; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3514 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3515 | // Calculate the distance between the center of the CellLayout |
| 3516 | // and the touch point |
| 3517 | float dist = squaredDistance(touchXy, cellLayoutCenter); |
| 3518 | |
| 3519 | if (dist < smallestDistSoFar) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3520 | smallestDistSoFar = dist; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3521 | bestMatchingScreen = cl; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3522 | } |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 3523 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3524 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 3525 | return bestMatchingScreen; |
| 3526 | } |
| 3527 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3528 | // This is used to compute the visual center of the dragView. This point is then |
| 3529 | // used to visualize drop locations and determine where to drop an item. The idea is that |
| 3530 | // the visual center represents the user's interpretation of where the item is, and hence |
| 3531 | // is the appropriate point to use when determining drop location. |
| 3532 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 3533 | DragView dragView, float[] recycle) { |
| 3534 | float res[]; |
| 3535 | if (recycle == null) { |
| 3536 | res = new float[2]; |
| 3537 | } else { |
| 3538 | res = recycle; |
| 3539 | } |
| 3540 | |
| 3541 | // First off, the drag view has been shifted in a way that is not represented in the |
| 3542 | // x and y values or the x/yOffsets. Here we account for that shift. |
| 3543 | x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 3544 | y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 3545 | |
| 3546 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 3547 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 3548 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 3549 | // R.dimen.dragViewOffsetY. |
| 3550 | int left = x - xOffset; |
| 3551 | int top = y - yOffset; |
| 3552 | |
| 3553 | // In order to find the visual center, we shift by half the dragRect |
| 3554 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 3555 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 3556 | |
| 3557 | return res; |
| 3558 | } |
| 3559 | |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 3560 | private boolean isDragWidget(DragObject d) { |
| 3561 | return (d.dragInfo instanceof LauncherAppWidgetInfo || |
| 3562 | d.dragInfo instanceof PendingAddWidgetInfo); |
| 3563 | } |
| 3564 | private boolean isExternalDragWidget(DragObject d) { |
| 3565 | return d.dragSource != this && isDragWidget(d); |
| 3566 | } |
| 3567 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 3568 | public void onDragOver(DragObject d) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3569 | // Skip drag over events while we are dragging over side pages |
Adam Cohen | 82ac8a2 | 2012-02-14 16:27:49 -0800 | [diff] [blame] | 3570 | if (mInScrollArea || mIsSwitchingState || mState == State.SMALL) return; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3571 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3572 | Rect r = new Rect(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3573 | CellLayout layout = null; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3574 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 3575 | |
| 3576 | // Ensure that we have proper spans for the item that we are dropping |
| 3577 | 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] | 3578 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3579 | d.dragView, mDragViewVisualCenter); |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 3580 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3581 | final View child = (mDragInfo == null) ? null : mDragInfo.cell; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3582 | // Identify whether we have dragged over a side page |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3583 | if (isSmall()) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 3584 | if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3585 | if (isPointInSelfOverHotseat(d.x, d.y, r)) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3586 | layout = mLauncher.getHotseat().getLayout(); |
| 3587 | } |
| 3588 | } |
| 3589 | if (layout == null) { |
Winson Chung | 96ef409 | 2011-11-22 12:25:14 -0800 | [diff] [blame] | 3590 | layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false); |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3591 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3592 | if (layout != mDragTargetLayout) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3593 | setCurrentDropLayout(layout); |
| 3594 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3595 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3596 | boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3597 | if (isInSpringLoadedMode) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 3598 | if (mLauncher.isHotseatLayout(layout)) { |
| 3599 | mSpringLoadedDragController.cancel(); |
| 3600 | } else { |
| 3601 | mSpringLoadedDragController.setAlarm(mDragTargetLayout); |
| 3602 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | } else { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3606 | // 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] | 3607 | if (mLauncher.getHotseat() != null && !isDragWidget(d)) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3608 | if (isPointInSelfOverHotseat(d.x, d.y, r)) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3609 | layout = mLauncher.getHotseat().getLayout(); |
| 3610 | } |
| 3611 | } |
| 3612 | if (layout == null) { |
| 3613 | layout = getCurrentDropLayout(); |
| 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 | } |
| 3619 | } |
| 3620 | |
| 3621 | // Handle the drag over |
| 3622 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3623 | // We want the point to be mapped to the dragTarget. |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3624 | if (mLauncher.isHotseatLayout(mDragTargetLayout)) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3625 | mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3626 | } else { |
| 3627 | mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); |
| 3628 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3629 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3630 | ItemInfo info = (ItemInfo) d.dragInfo; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 3631 | |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3632 | int minSpanX = item.spanX; |
| 3633 | int minSpanY = item.spanY; |
| 3634 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3635 | minSpanX = item.minSpanX; |
| 3636 | minSpanY = item.minSpanY; |
| 3637 | } |
| 3638 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3639 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3640 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 3641 | mDragTargetLayout, mTargetCell); |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3642 | int reorderX = mTargetCell[0]; |
| 3643 | int reorderY = mTargetCell[1]; |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3644 | |
| 3645 | setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]); |
| 3646 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3647 | float targetCellDistance = mDragTargetLayout.getDistanceFromCell( |
| 3648 | mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); |
| 3649 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3650 | final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], |
| 3651 | mTargetCell[1]); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 3652 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3653 | manageFolderFeedback(info, mDragTargetLayout, mTargetCell, |
| 3654 | targetCellDistance, dragOverView); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3655 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3656 | boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int) |
| 3657 | mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX, |
| 3658 | item.spanY, child, mTargetCell); |
| 3659 | |
| 3660 | if (!nearestDropOccupied) { |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3661 | mDragTargetLayout.visualizeDropLocation(child, mDragOutline, |
| 3662 | (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], |
| 3663 | mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, |
| 3664 | d.dragView.getDragVisualizeOffset(), d.dragView.getDragRegion()); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3665 | } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER) |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3666 | && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX || |
| 3667 | mLastReorderY != reorderY)) { |
Adam Cohen | d024f98 | 2012-05-23 18:26:45 -0700 | [diff] [blame] | 3668 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3669 | int[] resultSpan = new int[2]; |
| 3670 | mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
| 3671 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY, |
| 3672 | child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT); |
| 3673 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3674 | // Otherwise, if we aren't adding to or creating a folder and there's no pending |
| 3675 | // reorder, then we schedule a reorder |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3676 | ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter, |
| 3677 | minSpanX, minSpanY, item.spanX, item.spanY, d.dragView, child); |
| 3678 | mReorderAlarm.setOnAlarmListener(listener); |
| 3679 | mReorderAlarm.setAlarm(REORDER_TIMEOUT); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER || |
| 3683 | !nearestDropOccupied) { |
| 3684 | if (mDragTargetLayout != null) { |
| 3685 | mDragTargetLayout.revertTempState(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3686 | } |
| 3687 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3688 | } |
| 3689 | } |
| 3690 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3691 | private void manageFolderFeedback(ItemInfo info, CellLayout targetLayout, |
| 3692 | int[] targetCell, float distance, View dragOverView) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3693 | boolean userFolderPending = willCreateUserFolder(info, targetLayout, targetCell, distance, |
| 3694 | false); |
| 3695 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3696 | if (mDragMode == DRAG_MODE_NONE && userFolderPending && |
| 3697 | !mFolderCreationAlarm.alarmPending()) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3698 | mFolderCreationAlarm.setOnAlarmListener(new |
| 3699 | FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1])); |
| 3700 | mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3701 | return; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | boolean willAddToFolder = |
| 3705 | willAddToExistingUserFolder(info, targetLayout, targetCell, distance); |
| 3706 | |
| 3707 | if (willAddToFolder && mDragMode == DRAG_MODE_NONE) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3708 | mDragOverFolderIcon = ((FolderIcon) dragOverView); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3709 | mDragOverFolderIcon.onDragEnter(info); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3710 | if (targetLayout != null) { |
| 3711 | targetLayout.clearDragOutlines(); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3712 | } |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3713 | setDragMode(DRAG_MODE_ADD_TO_FOLDER); |
| 3714 | return; |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 3715 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3716 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3717 | if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) { |
| 3718 | setDragMode(DRAG_MODE_NONE); |
| 3719 | } |
| 3720 | if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) { |
| 3721 | setDragMode(DRAG_MODE_NONE); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3722 | } |
| 3723 | |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3724 | return; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3727 | class FolderCreationAlarmListener implements OnAlarmListener { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3728 | CellLayout layout; |
| 3729 | int cellX; |
| 3730 | int cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3731 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3732 | public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { |
| 3733 | this.layout = layout; |
| 3734 | this.cellX = cellX; |
| 3735 | this.cellY = cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3739 | if (mDragFolderRingAnimator != null) { |
| 3740 | // This shouldn't happen ever, but just in case, make sure we clean up the mess. |
| 3741 | mDragFolderRingAnimator.animateToNaturalState(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3742 | } |
Adam Cohen | 8ec05f9 | 2013-10-13 12:29:03 -0700 | [diff] [blame] | 3743 | mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3744 | mDragFolderRingAnimator.setCell(cellX, cellY); |
| 3745 | mDragFolderRingAnimator.setCellLayout(layout); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3746 | mDragFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 3747 | layout.showFolderAccept(mDragFolderRingAnimator); |
| 3748 | layout.clearDragOutlines(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3749 | setDragMode(DRAG_MODE_CREATE_FOLDER); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | class ReorderAlarmListener implements OnAlarmListener { |
| 3754 | float[] dragViewCenter; |
| 3755 | int minSpanX, minSpanY, spanX, spanY; |
| 3756 | DragView dragView; |
| 3757 | View child; |
| 3758 | |
| 3759 | public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX, |
| 3760 | int spanY, DragView dragView, View child) { |
| 3761 | this.dragViewCenter = dragViewCenter; |
| 3762 | this.minSpanX = minSpanX; |
| 3763 | this.minSpanY = minSpanY; |
| 3764 | this.spanX = spanX; |
| 3765 | this.spanY = spanY; |
| 3766 | this.child = child; |
| 3767 | this.dragView = dragView; |
| 3768 | } |
| 3769 | |
| 3770 | public void onAlarm(Alarm alarm) { |
| 3771 | int[] resultSpan = new int[2]; |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3772 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
Adam Cohen | 74c5491 | 2013-09-29 14:48:04 -0700 | [diff] [blame] | 3773 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout, |
| 3774 | mTargetCell); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3775 | mLastReorderX = mTargetCell[0]; |
| 3776 | mLastReorderY = mTargetCell[1]; |
| 3777 | |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3778 | mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3779 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, |
| 3780 | child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER); |
| 3781 | |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3782 | if (mTargetCell[0] < 0 || mTargetCell[1] < 0) { |
| 3783 | mDragTargetLayout.revertTempState(); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3784 | } else { |
| 3785 | setDragMode(DRAG_MODE_REORDER); |
Adam Cohen | 19f3792 | 2012-03-21 11:59:11 -0700 | [diff] [blame] | 3786 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3787 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3788 | boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY; |
| 3789 | mDragTargetLayout.visualizeDropLocation(child, mDragOutline, |
| 3790 | (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], |
| 3791 | mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, |
| 3792 | dragView.getDragVisualizeOffset(), dragView.getDragRegion()); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3796 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3797 | public void getHitRectRelativeToDragLayer(Rect outRect) { |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3798 | // We want the workspace to have the whole area of the display (it will find the correct |
| 3799 | // cell layout to drop to in the existing drag/drop logic. |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 3800 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect); |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3803 | /** |
| 3804 | * Add the item specified by dragInfo to the given layout. |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3805 | * @return true if successful |
| 3806 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 3807 | public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) { |
| 3808 | if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) { |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 3809 | onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3810 | return true; |
| 3811 | } |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 3812 | mLauncher.showOutOfSpaceMessage(mLauncher.isHotseatLayout(layout)); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 3813 | return false; |
| 3814 | } |
| 3815 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3816 | private void onDropExternal(int[] touchXY, Object dragInfo, |
| 3817 | CellLayout cellLayout, boolean insertAtFirst) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3818 | onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 3821 | /** |
| 3822 | * Drop an item that didn't originate on one of the workspace screens. |
| 3823 | * It may have come from Launcher (e.g. from all apps or customize), or it may have |
| 3824 | * come from another app altogether. |
| 3825 | * |
| 3826 | * NOTE: This can also be called when we are outside of a drag event, when we want |
| 3827 | * to add an item to one of the workspace screens. |
| 3828 | */ |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3829 | private void onDropExternal(final int[] touchXY, final Object dragInfo, |
| 3830 | final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { |
| 3831 | final Runnable exitSpringLoadedRunnable = new Runnable() { |
| 3832 | @Override |
| 3833 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 3834 | mLauncher.exitSpringLoadedDragModeDelayed(true, |
| 3835 | Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3836 | } |
| 3837 | }; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3838 | |
| 3839 | ItemInfo info = (ItemInfo) dragInfo; |
| 3840 | int spanX = info.spanX; |
| 3841 | int spanY = info.spanY; |
| 3842 | if (mDragInfo != null) { |
| 3843 | spanX = mDragInfo.spanX; |
| 3844 | spanY = mDragInfo.spanY; |
| 3845 | } |
| 3846 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3847 | final long container = mLauncher.isHotseatLayout(cellLayout) ? |
| 3848 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 3849 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3850 | final long screenId = getIdForScreen(cellLayout); |
| 3851 | if (!mLauncher.isHotseatLayout(cellLayout) |
| 3852 | && screenId != getScreenIdForPageIndex(mCurrentPage) |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3853 | && mState != State.SPRING_LOADED) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3854 | snapToScreenId(screenId, null); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3855 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3856 | |
| 3857 | if (info instanceof PendingAddItemInfo) { |
| 3858 | final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo; |
| 3859 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3860 | boolean findNearestVacantCell = true; |
| 3861 | if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 3862 | mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, |
| 3863 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3864 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3865 | mDragViewVisualCenter[1], mTargetCell); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3866 | if (willCreateUserFolder((ItemInfo) d.dragInfo, cellLayout, mTargetCell, |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3867 | distance, true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo, |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 3868 | cellLayout, mTargetCell, distance)) { |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3869 | findNearestVacantCell = false; |
| 3870 | } |
| 3871 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3872 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3873 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 3874 | boolean updateWidgetSize = false; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3875 | if (findNearestVacantCell) { |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3876 | int minSpanX = item.spanX; |
| 3877 | int minSpanY = item.spanY; |
| 3878 | if (item.minSpanX > 0 && item.minSpanY > 0) { |
| 3879 | minSpanX = item.minSpanX; |
| 3880 | minSpanY = item.minSpanY; |
| 3881 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3882 | int[] resultSpan = new int[2]; |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3883 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3884 | (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, |
| 3885 | null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3886 | |
| 3887 | if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) { |
| 3888 | updateWidgetSize = true; |
| 3889 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3890 | item.spanX = resultSpan[0]; |
| 3891 | item.spanY = resultSpan[1]; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3894 | Runnable onAnimationCompleteRunnable = new Runnable() { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3895 | @Override |
| 3896 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame^] | 3897 | // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when |
| 3898 | // adding an item that may not be dropped right away (due to a config activity) |
| 3899 | // we defer the removal until the activity returns. |
| 3900 | deferRemoveExtraEmptyScreen(); |
| 3901 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3902 | // When dragging and dropping from customization tray, we deal with creating |
| 3903 | // widgets/shortcuts/folders in a slightly different way |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3904 | switch (pendingInfo.itemType) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3905 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3906 | int span[] = new int[2]; |
| 3907 | span[0] = item.spanX; |
| 3908 | span[1] = item.spanY; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3909 | mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3910 | container, screenId, mTargetCell, span, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3911 | break; |
| 3912 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3913 | mLauncher.processShortcutFromDrop(pendingInfo.componentName, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3914 | container, screenId, mTargetCell, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3915 | break; |
| 3916 | default: |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3917 | throw new IllegalStateException("Unknown item type: " + |
| 3918 | pendingInfo.itemType); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3919 | } |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3920 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3921 | }; |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3922 | View finalView = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET |
| 3923 | ? ((PendingAddWidgetInfo) pendingInfo).boundWidget : null; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 3924 | |
| 3925 | if (finalView instanceof AppWidgetHostView && updateWidgetSize) { |
| 3926 | AppWidgetHostView awhv = (AppWidgetHostView) finalView; |
| 3927 | AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, item.spanX, |
| 3928 | item.spanY); |
| 3929 | } |
| 3930 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 3931 | int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR; |
| 3932 | if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && |
| 3933 | ((PendingAddWidgetInfo) pendingInfo).info.configure != null) { |
| 3934 | animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN; |
| 3935 | } |
| 3936 | animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, |
| 3937 | animationStyle, finalView, true); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 3938 | } else { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3939 | // This is for other drag/drop cases, like dragging from All Apps |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3940 | View view = null; |
| 3941 | |
| 3942 | switch (info.itemType) { |
| 3943 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 3944 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 3945 | if (info.container == NO_ID && info instanceof AppInfo) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3946 | // Came from all apps -- make a copy |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 3947 | info = new ShortcutInfo((AppInfo) info); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3948 | } |
| 3949 | view = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 3950 | (ShortcutInfo) info); |
| 3951 | break; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3952 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3953 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, |
| 3954 | (FolderInfo) info, mIconCache); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3955 | break; |
| 3956 | default: |
| 3957 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 3958 | } |
| 3959 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3960 | // First we find the cell nearest to point at which the item is |
| 3961 | // dropped, without any consideration to whether there is an item there. |
| 3962 | if (touchXY != null) { |
| 3963 | mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, |
| 3964 | cellLayout, mTargetCell); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3965 | float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], |
| 3966 | mDragViewVisualCenter[1], mTargetCell); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3967 | d.postAnimationRunnable = exitSpringLoadedRunnable; |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3968 | if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance, |
| 3969 | true, d.dragView, d.postAnimationRunnable)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3970 | return; |
| 3971 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3972 | if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d, |
| 3973 | true)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3974 | return; |
| 3975 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 3976 | } |
| 3977 | |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 3978 | if (touchXY != null) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3979 | // when dragging and dropping, just find the closest free spot |
Adam Cohen | fa3c58f | 2013-12-06 16:10:55 -0800 | [diff] [blame] | 3980 | mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 3981 | (int) mDragViewVisualCenter[1], 1, 1, 1, 1, |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 3982 | null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3983 | } else { |
| 3984 | cellLayout.findCellForSpan(mTargetCell, 1, 1); |
| 3985 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3986 | addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3987 | info.spanY, insertAtFirst); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3988 | cellLayout.onDropChild(view); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 3989 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 3990 | cellLayout.getShortcutsAndWidgets().measureChild(view); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3991 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 3992 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3993 | lp.cellX, lp.cellY); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3994 | |
| 3995 | if (d.dragView != null) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3996 | // We wrap the animation call in the temporary set and reset of the current |
| 3997 | // cellLayout to its final transform -- this means we animate the drag view to |
| 3998 | // the correct final location. |
| 3999 | setFinalTransitionTransform(cellLayout); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 4000 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, |
Adam Cohen | 28f852a | 2013-10-15 14:34:05 -0700 | [diff] [blame] | 4001 | exitSpringLoadedRunnable, this); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4002 | resetTransitionTransform(cellLayout); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 4003 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 4004 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4005 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 4006 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4007 | public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4008 | int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo.spanX, |
| 4009 | widgetInfo.spanY, widgetInfo, false); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4010 | int visibility = layout.getVisibility(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4011 | layout.setVisibility(VISIBLE); |
| 4012 | |
| 4013 | int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY); |
| 4014 | int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY); |
| 4015 | Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1], |
| 4016 | Bitmap.Config.ARGB_8888); |
| 4017 | Canvas c = new Canvas(b); |
| 4018 | |
| 4019 | layout.measure(width, height); |
| 4020 | layout.layout(0, 0, unScaledSize[0], unScaledSize[1]); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4021 | layout.draw(c); |
| 4022 | c.setBitmap(null); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4023 | layout.setVisibility(visibility); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4024 | return b; |
| 4025 | } |
| 4026 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4027 | private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, |
Adam Cohen | 9d5b7d8 | 2012-05-09 18:00:44 -0700 | [diff] [blame] | 4028 | DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4029 | boolean external, boolean scale) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4030 | // Now we animate the dragView, (ie. the widget or shortcut preview) into its final |
| 4031 | // location and size on the home screen. |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4032 | int spanX = info.spanX; |
| 4033 | int spanY = info.spanY; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4034 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4035 | Rect r = estimateItemPosition(layout, info, targetCell[0], targetCell[1], spanX, spanY); |
| 4036 | loc[0] = r.left; |
| 4037 | loc[1] = r.top; |
| 4038 | |
| 4039 | setFinalTransitionTransform(layout); |
| 4040 | float cellLayoutScale = |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4041 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4042 | resetTransitionTransform(layout); |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4043 | |
| 4044 | float dragViewScaleX; |
| 4045 | float dragViewScaleY; |
| 4046 | if (scale) { |
| 4047 | dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth(); |
| 4048 | dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight(); |
| 4049 | } else { |
| 4050 | dragViewScaleX = 1f; |
| 4051 | dragViewScaleY = 1f; |
| 4052 | } |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4053 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4054 | // The animation will scale the dragView about its center, so we need to center about |
| 4055 | // the final location. |
| 4056 | loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2; |
| 4057 | loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; |
| 4058 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4059 | scaleXY[0] = dragViewScaleX * cellLayoutScale; |
| 4060 | scaleXY[1] = dragViewScaleY * cellLayoutScale; |
| 4061 | } |
| 4062 | |
| 4063 | public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, DragView dragView, |
| 4064 | final Runnable onCompleteRunnable, int animationType, final View finalView, |
| 4065 | boolean external) { |
| 4066 | Rect from = new Rect(); |
| 4067 | mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from); |
| 4068 | |
| 4069 | int[] finalPos = new int[2]; |
| 4070 | float scaleXY[] = new float[2]; |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4071 | boolean scalePreview = !(info instanceof PendingAddShortcutInfo); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4072 | getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell, |
Michael Jurka | a2fe786 | 2012-05-29 05:58:17 -0700 | [diff] [blame] | 4073 | external, scalePreview); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4074 | |
| 4075 | Resources res = mLauncher.getResources(); |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 4076 | final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4077 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4078 | // In the case where we've prebound the widget, we remove it from the DragLayer |
| 4079 | if (finalView instanceof AppWidgetHostView && external) { |
Adam Cohen | 68f681b | 2012-05-28 15:01:16 -0700 | [diff] [blame] | 4080 | Log.d(TAG, "6557954 Animate widget drop, final view is appWidgetHostView"); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4081 | mLauncher.getDragLayer().removeView(finalView); |
| 4082 | } |
| 4083 | if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) { |
| 4084 | Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4085 | dragView.setCrossFadeBitmap(crossFadeBitmap); |
| 4086 | dragView.crossFade((int) (duration * 0.8f)); |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4087 | } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && external) { |
| 4088 | scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4089 | } |
| 4090 | |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4091 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 4092 | if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4093 | mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4094 | DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration); |
| 4095 | } else { |
Adam Cohen | d41fbf5 | 2012-02-16 23:53:59 -0800 | [diff] [blame] | 4096 | int endStyle; |
| 4097 | if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) { |
| 4098 | endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE; |
| 4099 | } else { |
| 4100 | endStyle = DragLayer.ANIMATION_END_DISAPPEAR;; |
| 4101 | } |
| 4102 | |
| 4103 | Runnable onComplete = new Runnable() { |
| 4104 | @Override |
| 4105 | public void run() { |
| 4106 | if (finalView != null) { |
| 4107 | finalView.setVisibility(VISIBLE); |
| 4108 | } |
| 4109 | if (onCompleteRunnable != null) { |
| 4110 | onCompleteRunnable.run(); |
| 4111 | } |
| 4112 | } |
| 4113 | }; |
| 4114 | dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0], |
| 4115 | finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle, |
| 4116 | duration, this); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4120 | public void setFinalTransitionTransform(CellLayout layout) { |
| 4121 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4122 | mCurrentScale = getScaleX(); |
| 4123 | setScaleX(mNewScale); |
| 4124 | setScaleY(mNewScale); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4125 | } |
| 4126 | } |
| 4127 | public void resetTransitionTransform(CellLayout layout) { |
| 4128 | if (isSwitchingState()) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 4129 | setScaleX(mCurrentScale); |
| 4130 | setScaleY(mCurrentScale); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 4131 | } |
| 4132 | } |
| 4133 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4134 | /** |
| 4135 | * Return the current {@link CellLayout}, correctly picking the destination |
| 4136 | * screen while a scroll is in progress. |
| 4137 | */ |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 4138 | public CellLayout getCurrentDropLayout() { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 4139 | return (CellLayout) getChildAt(getNextPage()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4140 | } |
| 4141 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4142 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4143 | * Return the current CellInfo describing our current drag; this method exists |
| 4144 | * so that Launcher can sync this object with the correct info when the activity is created/ |
| 4145 | * destroyed |
| 4146 | * |
| 4147 | */ |
| 4148 | public CellLayout.CellInfo getDragInfo() { |
| 4149 | return mDragInfo; |
| 4150 | } |
| 4151 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 4152 | public int getCurrentPageOffsetFromCustomContent() { |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 4153 | return getNextPage() - numCustomPages(); |
| 4154 | } |
| 4155 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4156 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4157 | * Calculate the nearest cell where the given object would be dropped. |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4158 | * |
| 4159 | * pixelX and pixelY should be in the coordinate system of layout |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 4160 | */ |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4161 | private int[] findNearestArea(int pixelX, int pixelY, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4162 | int spanX, int spanY, CellLayout layout, int[] recycle) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4163 | return layout.findNearestArea( |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 4164 | pixelX, pixelY, spanX, spanY, recycle); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 4167 | void setup(DragController dragController) { |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 4168 | mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 4169 | mDragController = dragController; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4170 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4171 | // hardware layers on children are enabled on startup, but should be disabled until |
| 4172 | // needed |
Michael Jurka | 3a0469d | 2012-06-21 09:38:41 -0700 | [diff] [blame] | 4173 | updateChildrenLayersEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4174 | } |
| 4175 | |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 4176 | /** |
| 4177 | * Called at the end of a drag which originated on the workspace. |
| 4178 | */ |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4179 | public void onDropCompleted(final View target, final DragObject d, |
| 4180 | final boolean isFlingToDelete, final boolean success) { |
| 4181 | if (mDeferDropAfterUninstall) { |
| 4182 | mDeferredAction = new Runnable() { |
Adam Cohen | ad4e15c | 2013-10-17 16:21:35 -0700 | [diff] [blame] | 4183 | public void run() { |
| 4184 | onDropCompleted(target, d, isFlingToDelete, success); |
| 4185 | mDeferredAction = null; |
| 4186 | } |
| 4187 | }; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4188 | return; |
| 4189 | } |
| 4190 | |
| 4191 | boolean beingCalledAfterUninstall = mDeferredAction != null; |
| 4192 | |
| 4193 | if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) { |
| 4194 | if (target != this && mDragInfo != null) { |
Daniel Sandler | b56e95a | 2013-09-04 23:13:39 -0400 | [diff] [blame] | 4195 | CellLayout parentCell = getParentCellLayoutForView(mDragInfo.cell); |
| 4196 | if (parentCell != null) { |
| 4197 | parentCell.removeView(mDragInfo.cell); |
| 4198 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4199 | if (mDragInfo.cell instanceof DropTarget) { |
| 4200 | mDragController.removeDropTarget((DropTarget) mDragInfo.cell); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 4201 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4202 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 4203 | } else if (mDragInfo != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4204 | CellLayout cellLayout; |
| 4205 | if (mLauncher.isHotseatLayout(target)) { |
| 4206 | cellLayout = mLauncher.getHotseat().getLayout(); |
| 4207 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4208 | cellLayout = getScreenWithId(mDragInfo.screenId); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4209 | } |
Jorim Jaggi | eedb00a | 2014-01-13 13:45:07 -0800 | [diff] [blame] | 4210 | if (cellLayout == null && LauncherAppState.isDogfoodBuild()) { |
| 4211 | throw new RuntimeException("Invalid state: cellLayout == null in " |
| 4212 | + "Workspace#onDropCompleted. Please file a bug. "); |
| 4213 | } |
| 4214 | if (cellLayout != null) { |
| 4215 | cellLayout.onDropChild(mDragInfo.cell); |
| 4216 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4217 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4218 | if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful)) |
| 4219 | && mDragInfo.cell != null) { |
| 4220 | mDragInfo.cell.setVisibility(VISIBLE); |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 4221 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 4222 | mDragOutline = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4223 | mDragInfo = null; |
| 4224 | } |
| 4225 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 4226 | public void deferCompleteDropAfterUninstallActivity() { |
| 4227 | mDeferDropAfterUninstall = true; |
| 4228 | } |
| 4229 | |
| 4230 | /// maybe move this into a smaller part |
| 4231 | public void onUninstallActivityReturned(boolean success) { |
| 4232 | mDeferDropAfterUninstall = false; |
| 4233 | mUninstallSuccessful = success; |
| 4234 | if (mDeferredAction != null) { |
| 4235 | mDeferredAction.run(); |
| 4236 | } |
| 4237 | } |
| 4238 | |
Adam Cohen | ea889a2 | 2012-03-27 16:45:39 -0700 | [diff] [blame] | 4239 | void updateItemLocationsInDatabase(CellLayout cl) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4240 | int count = cl.getShortcutsAndWidgets().getChildCount(); |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4241 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4242 | long screenId = getIdForScreen(cl); |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4243 | int container = Favorites.CONTAINER_DESKTOP; |
| 4244 | |
| 4245 | if (mLauncher.isHotseatLayout(cl)) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4246 | screenId = -1; |
Adam Cohen | d346171 | 2012-03-29 17:25:17 -0700 | [diff] [blame] | 4247 | container = Favorites.CONTAINER_HOTSEAT; |
| 4248 | } |
| 4249 | |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4250 | for (int i = 0; i < count; i++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4251 | View v = cl.getShortcutsAndWidgets().getChildAt(i); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4252 | ItemInfo info = (ItemInfo) v.getTag(); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 4253 | // 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] | 4254 | if (info != null && info.requiresDbUpdate) { |
| 4255 | info.requiresDbUpdate = false; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4256 | LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, info.cellX, |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 4257 | info.cellY, info.spanX, info.spanY); |
Adam Cohen | 2acce88 | 2012-03-28 19:03:19 -0700 | [diff] [blame] | 4258 | } |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4262 | ArrayList<ComponentName> getUniqueComponents(boolean stripDuplicates, ArrayList<ComponentName> duplicates) { |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4263 | ArrayList<ComponentName> uniqueIntents = new ArrayList<ComponentName>(); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4264 | getUniqueIntents((CellLayout) mLauncher.getHotseat().getLayout(), uniqueIntents, duplicates, false); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4265 | int count = getChildCount(); |
| 4266 | for (int i = 0; i < count; i++) { |
| 4267 | CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4268 | getUniqueIntents(cl, uniqueIntents, duplicates, false); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4269 | } |
| 4270 | return uniqueIntents; |
| 4271 | } |
| 4272 | |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4273 | void getUniqueIntents(CellLayout cl, ArrayList<ComponentName> uniqueIntents, |
| 4274 | ArrayList<ComponentName> duplicates, boolean stripDuplicates) { |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4275 | int count = cl.getShortcutsAndWidgets().getChildCount(); |
| 4276 | |
| 4277 | ArrayList<View> children = new ArrayList<View>(); |
| 4278 | for (int i = 0; i < count; i++) { |
| 4279 | View v = cl.getShortcutsAndWidgets().getChildAt(i); |
| 4280 | children.add(v); |
| 4281 | } |
| 4282 | |
| 4283 | for (int i = 0; i < count; i++) { |
| 4284 | View v = children.get(i); |
| 4285 | ItemInfo info = (ItemInfo) v.getTag(); |
| 4286 | // Null check required as the AllApps button doesn't have an item info |
| 4287 | if (info instanceof ShortcutInfo) { |
| 4288 | ShortcutInfo si = (ShortcutInfo) info; |
| 4289 | ComponentName cn = si.intent.getComponent(); |
| 4290 | |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4291 | Uri dataUri = si.intent.getData(); |
| 4292 | // If dataUri is not null / empty or if this component isn't one that would |
| 4293 | // have previously showed up in the AllApps list, then this is a widget-type |
| 4294 | // shortcut, so ignore it. |
| 4295 | if (dataUri != null && !dataUri.equals(Uri.EMPTY)) { |
Adam Cohen | 99894d9 | 2013-06-14 11:22:59 -0700 | [diff] [blame] | 4296 | continue; |
| 4297 | } |
Adam Cohen | a0b5749 | 2013-06-14 15:33:35 -0700 | [diff] [blame] | 4298 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4299 | if (!uniqueIntents.contains(cn)) { |
| 4300 | uniqueIntents.add(cn); |
| 4301 | } else { |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4302 | if (stripDuplicates) { |
| 4303 | cl.removeViewInLayout(v); |
| 4304 | LauncherModel.deleteItemFromDatabase(mLauncher, si); |
| 4305 | } |
| 4306 | if (duplicates != null) { |
| 4307 | duplicates.add(cn); |
| 4308 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4309 | } |
| 4310 | } |
| 4311 | if (v instanceof FolderIcon) { |
| 4312 | FolderIcon fi = (FolderIcon) v; |
| 4313 | ArrayList<View> items = fi.getFolder().getItemsInReadingOrder(); |
| 4314 | for (int j = 0; j < items.size(); j++) { |
| 4315 | if (items.get(j).getTag() instanceof ShortcutInfo) { |
| 4316 | ShortcutInfo si = (ShortcutInfo) items.get(j).getTag(); |
| 4317 | ComponentName cn = si.intent.getComponent(); |
| 4318 | |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4319 | Uri dataUri = si.intent.getData(); |
| 4320 | // If dataUri is not null / empty or if this component isn't one that would |
| 4321 | // have previously showed up in the AllApps list, then this is a widget-type |
| 4322 | // shortcut, so ignore it. |
| 4323 | if (dataUri != null && !dataUri.equals(Uri.EMPTY)) { |
Adam Cohen | 99894d9 | 2013-06-14 11:22:59 -0700 | [diff] [blame] | 4324 | continue; |
| 4325 | } |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4326 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4327 | if (!uniqueIntents.contains(cn)) { |
| 4328 | uniqueIntents.add(cn); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 4329 | } else { |
| 4330 | if (stripDuplicates) { |
| 4331 | fi.getFolderInfo().remove(si); |
| 4332 | LauncherModel.deleteItemFromDatabase(mLauncher, si); |
| 4333 | } |
| 4334 | if (duplicates != null) { |
| 4335 | duplicates.add(cn); |
| 4336 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | } |
| 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | void saveWorkspaceToDb() { |
| 4345 | saveWorkspaceScreenToDb((CellLayout) mLauncher.getHotseat().getLayout()); |
| 4346 | int count = getChildCount(); |
| 4347 | for (int i = 0; i < count; i++) { |
| 4348 | CellLayout cl = (CellLayout) getChildAt(i); |
| 4349 | saveWorkspaceScreenToDb(cl); |
| 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | void saveWorkspaceScreenToDb(CellLayout cl) { |
| 4354 | int count = cl.getShortcutsAndWidgets().getChildCount(); |
| 4355 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4356 | long screenId = getIdForScreen(cl); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4357 | int container = Favorites.CONTAINER_DESKTOP; |
| 4358 | |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 4359 | Hotseat hotseat = mLauncher.getHotseat(); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4360 | if (mLauncher.isHotseatLayout(cl)) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 4361 | screenId = -1; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4362 | container = Favorites.CONTAINER_HOTSEAT; |
| 4363 | } |
| 4364 | |
| 4365 | for (int i = 0; i < count; i++) { |
| 4366 | View v = cl.getShortcutsAndWidgets().getChildAt(i); |
| 4367 | ItemInfo info = (ItemInfo) v.getTag(); |
| 4368 | // Null check required as the AllApps button doesn't have an item info |
| 4369 | if (info != null) { |
Winson Chung | f70696d | 2013-06-25 16:19:53 -0700 | [diff] [blame] | 4370 | int cellX = info.cellX; |
| 4371 | int cellY = info.cellY; |
| 4372 | if (container == Favorites.CONTAINER_HOTSEAT) { |
| 4373 | cellX = hotseat.getCellXFromOrder((int) info.screenId); |
| 4374 | cellY = hotseat.getCellYFromOrder((int) info.screenId); |
| 4375 | } |
| 4376 | LauncherModel.addItemToDatabase(mLauncher, info, container, screenId, cellX, |
| 4377 | cellY, false); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 4378 | } |
| 4379 | if (v instanceof FolderIcon) { |
| 4380 | FolderIcon fi = (FolderIcon) v; |
| 4381 | fi.getFolder().addItemLocationsInDatabase(); |
| 4382 | } |
| 4383 | } |
| 4384 | } |
| 4385 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4386 | @Override |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 4387 | public float getIntrinsicIconScaleFactor() { |
| 4388 | return 1f; |
| 4389 | } |
| 4390 | |
| 4391 | @Override |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 4392 | public boolean supportsFlingToDelete() { |
| 4393 | return true; |
| 4394 | } |
| 4395 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4396 | @Override |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 4397 | public boolean supportsAppInfoDropTarget() { |
| 4398 | return false; |
| 4399 | } |
| 4400 | |
| 4401 | @Override |
| 4402 | public boolean supportsDeleteDropTarget() { |
| 4403 | return true; |
| 4404 | } |
| 4405 | |
| 4406 | @Override |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 4407 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { |
| 4408 | // Do nothing |
| 4409 | } |
| 4410 | |
| 4411 | @Override |
| 4412 | public void onFlingToDeleteCompleted() { |
| 4413 | // Do nothing |
| 4414 | } |
| 4415 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 4416 | public boolean isDropEnabled() { |
| 4417 | return true; |
| 4418 | } |
| 4419 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4420 | @Override |
| 4421 | protected void onRestoreInstanceState(Parcelable state) { |
| 4422 | super.onRestoreInstanceState(state); |
| 4423 | Launcher.setScreen(mCurrentPage); |
| 4424 | } |
| 4425 | |
| 4426 | @Override |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4427 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 4428 | // We don't dispatch restoreInstanceState to our children using this code path. |
Dave Hawkey | a888158 | 2013-09-17 15:55:33 -0600 | [diff] [blame] | 4429 | // 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] | 4430 | // others we will need to wait until after their items are bound. |
| 4431 | mSavedStates = container; |
| 4432 | } |
| 4433 | |
| 4434 | public void restoreInstanceStateForChild(int child) { |
| 4435 | if (mSavedStates != null) { |
| 4436 | mRestoredPages.add(child); |
| 4437 | CellLayout cl = (CellLayout) getChildAt(child); |
Adam Cohen | b0ee081 | 2013-12-03 10:51:45 -0800 | [diff] [blame] | 4438 | if (cl != null) { |
| 4439 | cl.restoreInstanceState(mSavedStates); |
| 4440 | } |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | public void restoreInstanceStateForRemainingPages() { |
| 4445 | int count = getChildCount(); |
| 4446 | for (int i = 0; i < count; i++) { |
| 4447 | if (!mRestoredPages.contains(i)) { |
| 4448 | restoreInstanceStateForChild(i); |
| 4449 | } |
| 4450 | } |
| 4451 | mRestoredPages.clear(); |
Winson Chung | d8e596d | 2013-10-21 17:14:12 -0700 | [diff] [blame] | 4452 | mSavedStates = null; |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4456 | public void scrollLeft() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4457 | if (!isSmall() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4458 | super.scrollLeft(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4459 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 4460 | Folder openFolder = getOpenFolder(); |
| 4461 | if (openFolder != null) { |
| 4462 | openFolder.completeDragExit(); |
| 4463 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4466 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4467 | public void scrollRight() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 4468 | if (!isSmall() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4469 | super.scrollRight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4470 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 4471 | Folder openFolder = getOpenFolder(); |
| 4472 | if (openFolder != null) { |
| 4473 | openFolder.completeDragExit(); |
| 4474 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4475 | } |
| 4476 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4477 | @Override |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4478 | public boolean onEnterScrollArea(int x, int y, int direction) { |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4479 | // Ignore the scroll area if we are dragging over the hot seat |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 4480 | boolean isPortrait = !LauncherAppState.isScreenLandscape(getContext()); |
Winson Chung | 10bfc6e | 2012-03-28 15:41:26 -0700 | [diff] [blame] | 4481 | if (mLauncher.getHotseat() != null && isPortrait) { |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4482 | Rect r = new Rect(); |
| 4483 | mLauncher.getHotseat().getHitRect(r); |
| 4484 | if (r.contains(x, y)) { |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4485 | return false; |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 4486 | } |
| 4487 | } |
| 4488 | |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4489 | boolean result = false; |
Adam Cohen | 61a9a5c | 2013-08-14 13:32:04 -0700 | [diff] [blame] | 4490 | if (!isSmall() && !mIsSwitchingState && getOpenFolder() == null) { |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4491 | mInScrollArea = true; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 4492 | |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 4493 | final int page = getNextPage() + |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4494 | (direction == DragController.SCROLL_LEFT ? -1 : 1); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4495 | // We always want to exit the current layout to ensure parity of enter / exit |
| 4496 | setCurrentDropLayout(null); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 4497 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4498 | if (0 <= page && page < getChildCount()) { |
Winson Chung | 482a5b6 | 2013-07-31 17:19:51 -0700 | [diff] [blame] | 4499 | // Ensure that we are not dragging over to the custom content screen |
| 4500 | if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) { |
| 4501 | return false; |
| 4502 | } |
| 4503 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 4504 | CellLayout layout = (CellLayout) getChildAt(page); |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4505 | setCurrentDragOverlappingLayout(layout); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4506 | |
| 4507 | // Workspace is responsible for drawing the edge glow on adjacent pages, |
| 4508 | // so we need to redraw the workspace when this may have changed. |
| 4509 | invalidate(); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4510 | result = true; |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4511 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4512 | } |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4513 | return result; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | @Override |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4517 | public boolean onExitScrollArea() { |
| 4518 | boolean result = false; |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 4519 | if (mInScrollArea) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4520 | invalidate(); |
| 4521 | CellLayout layout = getCurrentDropLayout(); |
| 4522 | setCurrentDropLayout(layout); |
| 4523 | setCurrentDragOverlappingLayout(layout); |
| 4524 | |
Adam Cohen | 8a18afc | 2011-12-13 15:57:01 -0800 | [diff] [blame] | 4525 | result = true; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4526 | mInScrollArea = false; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4527 | } |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 4528 | return result; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4531 | private void onResetScrollArea() { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 4532 | setCurrentDragOverlappingLayout(null); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 4533 | mInScrollArea = false; |
| 4534 | } |
| 4535 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4536 | /** |
| 4537 | * Returns a specific CellLayout |
| 4538 | */ |
| 4539 | CellLayout getParentCellLayoutForView(View v) { |
| 4540 | ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts(); |
| 4541 | for (CellLayout layout : layouts) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4542 | if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4543 | return layout; |
| 4544 | } |
| 4545 | } |
| 4546 | return null; |
| 4547 | } |
| 4548 | |
| 4549 | /** |
| 4550 | * Returns a list of all the CellLayouts in the workspace. |
| 4551 | */ |
| 4552 | ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() { |
| 4553 | ArrayList<CellLayout> layouts = new ArrayList<CellLayout>(); |
| 4554 | int screenCount = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4555 | for (int screen = 0; screen < screenCount; screen++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4556 | layouts.add(((CellLayout) getChildAt(screen))); |
| 4557 | } |
| 4558 | if (mLauncher.getHotseat() != null) { |
| 4559 | layouts.add(mLauncher.getHotseat().getLayout()); |
| 4560 | } |
| 4561 | return layouts; |
| 4562 | } |
| 4563 | |
| 4564 | /** |
| 4565 | * 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] | 4566 | * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from |
| 4567 | * the hotseat and workspace pages |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4568 | */ |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4569 | ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() { |
| 4570 | ArrayList<ShortcutAndWidgetContainer> childrenLayouts = |
| 4571 | new ArrayList<ShortcutAndWidgetContainer>(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4572 | int screenCount = getChildCount(); |
| 4573 | for (int screen = 0; screen < screenCount; screen++) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4574 | childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4575 | } |
| 4576 | if (mLauncher.getHotseat() != null) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4577 | childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4578 | } |
| 4579 | return childrenLayouts; |
| 4580 | } |
| 4581 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4582 | public Folder getFolderForTag(final Object tag) { |
| 4583 | final Folder[] value = new Folder[1]; |
| 4584 | mapOverShortcuts(MAP_NO_RECURSE, new ShortcutOperator() { |
| 4585 | @Override |
| 4586 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4587 | if (v instanceof Folder) { |
| 4588 | Folder f = (Folder) v; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 4589 | if (f.getInfo() == tag && f.getInfo().opened) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4590 | value[0] = f; |
| 4591 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4592 | } |
| 4593 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4594 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4595 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4596 | }); |
| 4597 | return value[0]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4598 | } |
| 4599 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4600 | public View getViewForTag(final Object tag) { |
| 4601 | final View[] value = new View[1]; |
| 4602 | mapOverShortcuts(MAP_NO_RECURSE, new ShortcutOperator() { |
| 4603 | @Override |
| 4604 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4605 | if (v.getTag() == tag) { |
| 4606 | value[0] = v; |
| 4607 | return true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4608 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4609 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4610 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4611 | }); |
| 4612 | return value[0]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4613 | } |
| 4614 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4615 | void clearDropTargets() { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4616 | mapOverShortcuts(MAP_NO_RECURSE, new ShortcutOperator() { |
| 4617 | @Override |
| 4618 | public boolean evaluate(ItemInfo info, View v, View parent) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4619 | if (v instanceof DropTarget) { |
| 4620 | mDragController.removeDropTarget((DropTarget) v); |
| 4621 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4622 | // not done, process all the shortcuts |
| 4623 | return false; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4624 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4625 | }); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 4626 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4627 | |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4628 | // Removes ALL items that match a given package name, this is usually called when a package |
| 4629 | // has been removed and we want to remove all components (widgets, shortcuts, apps) that |
| 4630 | // belong to that package. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4631 | void removeItemsByPackageName(final ArrayList<String> packages, final UserHandleCompat user) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4632 | final HashSet<String> packageNames = new HashSet<String>(); |
Winson Chung | cd81073 | 2012-06-18 16:45:43 -0700 | [diff] [blame] | 4633 | packageNames.addAll(packages); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 4634 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4635 | // Filter out all the ItemInfos that this is going to affect |
| 4636 | final HashSet<ItemInfo> infos = new HashSet<ItemInfo>(); |
| 4637 | final HashSet<ComponentName> cns = new HashSet<ComponentName>(); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4638 | ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); |
| 4639 | for (CellLayout layoutParent : cellLayouts) { |
| 4640 | ViewGroup layout = layoutParent.getShortcutsAndWidgets(); |
| 4641 | int childCount = layout.getChildCount(); |
| 4642 | for (int i = 0; i < childCount; ++i) { |
| 4643 | View view = layout.getChildAt(i); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4644 | infos.add((ItemInfo) view.getTag()); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4645 | } |
| 4646 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4647 | LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { |
| 4648 | @Override |
| 4649 | public boolean filterItem(ItemInfo parent, ItemInfo info, |
| 4650 | ComponentName cn) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4651 | if (packageNames.contains(cn.getPackageName()) |
| 4652 | && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4653 | cns.add(cn); |
| 4654 | return true; |
| 4655 | } |
| 4656 | return false; |
| 4657 | } |
| 4658 | }; |
| 4659 | LauncherModel.filterItemInfos(infos, filter); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4660 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4661 | // Remove the affected components |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4662 | removeItemsByComponentName(cns, user); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | // Removes items that match the application info specified, when applications are removed |
| 4666 | // as a part of an update, this is called to ensure that other widgets and application |
| 4667 | // shortcuts are not removed. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4668 | void removeItemsByApplicationInfo(final ArrayList<AppInfo> appInfos, UserHandleCompat user) { |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4669 | // Just create a hash table of all the specific components that this will affect |
| 4670 | HashSet<ComponentName> cns = new HashSet<ComponentName>(); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 4671 | for (AppInfo info : appInfos) { |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4672 | cns.add(info.componentName); |
| 4673 | } |
| 4674 | |
| 4675 | // Remove all the things |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4676 | removeItemsByComponentName(cns, user); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 4677 | } |
| 4678 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4679 | void removeItemsByComponentName(final HashSet<ComponentName> componentNames, |
| 4680 | final UserHandleCompat user) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 4681 | ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); |
| 4682 | for (final CellLayout layoutParent: cellLayouts) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 4683 | final ViewGroup layout = layoutParent.getShortcutsAndWidgets(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 4684 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4685 | final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>(); |
| 4686 | for (int j = 0; j < layout.getChildCount(); j++) { |
| 4687 | final View view = layout.getChildAt(j); |
| 4688 | children.put((ItemInfo) view.getTag(), view); |
| 4689 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 4690 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4691 | final ArrayList<View> childrenToRemove = new ArrayList<View>(); |
| 4692 | final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove = |
| 4693 | new HashMap<FolderInfo, ArrayList<ShortcutInfo>>(); |
| 4694 | LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { |
| 4695 | @Override |
| 4696 | public boolean filterItem(ItemInfo parent, ItemInfo info, |
| 4697 | ComponentName cn) { |
| 4698 | if (parent instanceof FolderInfo) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4699 | if (componentNames.contains(cn) && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4700 | FolderInfo folder = (FolderInfo) parent; |
| 4701 | ArrayList<ShortcutInfo> appsToRemove; |
| 4702 | if (folderAppsToRemove.containsKey(folder)) { |
| 4703 | appsToRemove = folderAppsToRemove.get(folder); |
| 4704 | } else { |
| 4705 | appsToRemove = new ArrayList<ShortcutInfo>(); |
| 4706 | folderAppsToRemove.put(folder, appsToRemove); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 4707 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4708 | appsToRemove.add((ShortcutInfo) info); |
| 4709 | return true; |
| 4710 | } |
| 4711 | } else { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 4712 | if (componentNames.contains(cn) && info.user.equals(user)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4713 | childrenToRemove.add(children.get(info)); |
| 4714 | return true; |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 4715 | } |
| 4716 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4717 | return false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4718 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4719 | }; |
| 4720 | LauncherModel.filterItemInfos(children.keySet(), filter); |
| 4721 | |
| 4722 | // Remove all the apps from their folders |
| 4723 | for (FolderInfo folder : folderAppsToRemove.keySet()) { |
| 4724 | ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder); |
| 4725 | for (ShortcutInfo info : appsToRemove) { |
| 4726 | folder.remove(info); |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | // Remove all the other children |
| 4731 | for (View child : childrenToRemove) { |
| 4732 | // Note: We can not remove the view directly from CellLayoutChildren as this |
| 4733 | // does not re-mark the spaces as unoccupied. |
| 4734 | layoutParent.removeViewInLayout(child); |
| 4735 | if (child instanceof DropTarget) { |
| 4736 | mDragController.removeDropTarget((DropTarget) child); |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | if (childrenToRemove.size() > 0) { |
| 4741 | layout.requestLayout(); |
| 4742 | layout.invalidate(); |
| 4743 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4744 | } |
Winson Chung | 2efec4e | 2012-05-03 12:31:48 -0700 | [diff] [blame] | 4745 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4746 | // Strip all the empty screens |
| 4747 | stripEmptyScreens(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4748 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4749 | |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4750 | private void updateShortcut(HashMap<ComponentName, AppInfo> appsMap, ItemInfo info, |
| 4751 | View child) { |
| 4752 | ComponentName cn = info.getIntent().getComponent(); |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 4753 | if (info.getRestoredIntent() != null) { |
| 4754 | cn = info.getRestoredIntent().getComponent(); |
| 4755 | } |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4756 | if (cn != null) { |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 4757 | AppInfo appInfo = appsMap.get(cn); |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4758 | if ((appInfo != null) && LauncherModel.isShortcutInfoUpdateable(info)) { |
| 4759 | ShortcutInfo shortcutInfo = (ShortcutInfo) info; |
| 4760 | BubbleTextView shortcut = (BubbleTextView) child; |
Chris Wren | b6d4c28 | 2014-01-27 14:17:08 -0500 | [diff] [blame] | 4761 | shortcutInfo.restore(); |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4762 | shortcutInfo.updateIcon(mIconCache); |
| 4763 | shortcutInfo.title = appInfo.title.toString(); |
| 4764 | shortcut.applyFromShortcutInfo(shortcutInfo, mIconCache); |
| 4765 | } |
| 4766 | } |
| 4767 | } |
| 4768 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4769 | interface ShortcutOperator { |
| 4770 | /** |
| 4771 | * Process the next shortcut, possibly with side-effect on {@link ShortcutOperator#value}. |
| 4772 | * |
| 4773 | * @param info info for the shortcut |
| 4774 | * @param view view for the shortcut |
| 4775 | * @param parent containing folder, or null |
| 4776 | * @return true if done, false to continue the map |
| 4777 | */ |
| 4778 | public boolean evaluate(ItemInfo info, View view, View parent); |
| 4779 | } |
| 4780 | |
| 4781 | /** |
| 4782 | * Map the operator over the shortcuts, return the first-non-null value. |
| 4783 | * |
| 4784 | * @param recurse true: iterate over folder children. false: op get the folders themselves. |
| 4785 | * @param op the operator to map over the shortcuts |
| 4786 | */ |
| 4787 | void mapOverShortcuts(boolean recurse, ShortcutOperator op) { |
| 4788 | ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers(); |
| 4789 | final int containerCount = containers.size(); |
| 4790 | for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) { |
| 4791 | ShortcutAndWidgetContainer container = containers.get(containerIdx); |
| 4792 | // map over all the shortcuts on the workspace |
| 4793 | final int itemCount = container.getChildCount(); |
| 4794 | for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) { |
| 4795 | View item = container.getChildAt(itemIdx); |
| 4796 | ItemInfo info = (ItemInfo) item.getTag(); |
| 4797 | if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) { |
| 4798 | FolderIcon folder = (FolderIcon) item; |
| 4799 | ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder(); |
| 4800 | // map over all the children in the folder |
| 4801 | final int childCount = folderChildren.size(); |
| 4802 | for (int childIdx = 0; childIdx < childCount; childIdx++) { |
| 4803 | View child = folderChildren.get(childIdx); |
| 4804 | info = (ItemInfo) child.getTag(); |
| 4805 | if (op.evaluate(info, child, folder)) { |
| 4806 | return; |
| 4807 | } |
| 4808 | } |
| 4809 | } else { |
| 4810 | if (op.evaluate(info, item, null)) { |
| 4811 | return; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | } |
| 4817 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 4818 | void updateShortcuts(ArrayList<AppInfo> apps) { |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4819 | // Create a map of the apps to test against |
| 4820 | final HashMap<ComponentName, AppInfo> appsMap = new HashMap<ComponentName, AppInfo>(); |
| 4821 | for (AppInfo ai : apps) { |
| 4822 | appsMap.put(ai.componentName, ai); |
| 4823 | } |
| 4824 | |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4825 | mapOverShortcuts(MAP_RECURSE, new ShortcutOperator() { |
| 4826 | @Override |
| 4827 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4828 | if (info instanceof ShortcutInfo) { |
Winson Chung | 8c90331 | 2013-10-16 17:51:49 -0700 | [diff] [blame] | 4829 | updateShortcut(appsMap, info, v); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4830 | if (parent != null) { |
| 4831 | parent.invalidate(); |
| 4832 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4833 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4834 | // process all the shortcuts |
| 4835 | return false; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4836 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 4837 | }); |
| 4838 | } |
| 4839 | |
| 4840 | public void updatePackageState(final String pkgName, final int state) { |
| 4841 | mapOverShortcuts(MAP_RECURSE, new ShortcutOperator() { |
| 4842 | @Override |
| 4843 | public boolean evaluate(ItemInfo info, View v, View parent) { |
| 4844 | if (info instanceof ShortcutInfo |
| 4845 | && ((ShortcutInfo) info).isPromiseFor(pkgName) |
| 4846 | && v instanceof BubbleTextView) { |
| 4847 | ((BubbleTextView)v).setState(state); |
| 4848 | } |
| 4849 | // process all the shortcuts |
| 4850 | return false; |
| 4851 | } |
| 4852 | }); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 4853 | } |
| 4854 | |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4855 | private void moveToScreen(int page, boolean animate) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4856 | if (!isSmall()) { |
| 4857 | if (animate) { |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4858 | snapToPage(page); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4859 | } else { |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4860 | setCurrentPage(page); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 4861 | } |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 4862 | } |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4863 | View child = getChildAt(page); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 4864 | if (child != null) { |
| 4865 | child.requestFocus(); |
| 4866 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4867 | } |
| 4868 | |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4869 | void moveToDefaultScreen(boolean animate) { |
| 4870 | moveToScreen(mDefaultPage, animate); |
| 4871 | } |
| 4872 | |
| 4873 | void moveToCustomContentScreen(boolean animate) { |
| 4874 | if (hasCustomContent()) { |
| 4875 | int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID); |
| 4876 | if (animate) { |
| 4877 | snapToPage(ccIndex); |
| 4878 | } else { |
| 4879 | setCurrentPage(ccIndex); |
| 4880 | } |
| 4881 | View child = getChildAt(ccIndex); |
| 4882 | if (child != null) { |
| 4883 | child.requestFocus(); |
| 4884 | } |
| 4885 | } |
Adam Cohen | 76128b6 | 2013-11-14 16:43:59 +0000 | [diff] [blame] | 4886 | exitWidgetResizeMode(); |
Allan Wojciechowski | af110e8 | 2013-09-12 10:48:23 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4889 | @Override |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4890 | protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) { |
| 4891 | long screenId = getScreenIdForPageIndex(pageIndex); |
| 4892 | if (screenId == EXTRA_EMPTY_SCREEN_ID) { |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 4893 | int count = mScreenOrder.size() - numCustomPages(); |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4894 | if (count > 1) { |
Winson Chung | ba4e52f | 2013-10-01 17:22:13 -0700 | [diff] [blame] | 4895 | return new PageIndicator.PageMarkerResources(R.drawable.ic_pageindicator_current, |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4896 | R.drawable.ic_pageindicator_add); |
| 4897 | } |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 4898 | } |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 4899 | |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 4900 | return super.getPageIndicatorMarker(pageIndex); |
| 4901 | } |
| 4902 | |
| 4903 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4904 | public void syncPages() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4905 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4906 | |
| 4907 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 4908 | public void syncPageItems(int page, boolean immediate) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 4909 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4910 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4911 | protected String getPageIndicatorDescription() { |
| 4912 | String settings = getResources().getString(R.string.settings_button_text); |
| 4913 | return getCurrentPageDescription() + ", " + settings; |
| 4914 | } |
| 4915 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4916 | protected String getCurrentPageDescription() { |
| 4917 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4918 | int delta = numCustomPages(); |
| 4919 | if (hasCustomContent() && getNextPage() == 0) { |
| 4920 | return mCustomContentDescription; |
| 4921 | } |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 4922 | return String.format(getContext().getString(R.string.workspace_scroll_format), |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 4923 | page + 1 - delta, getChildCount() - delta); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 4924 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 4925 | |
| 4926 | public void getLocationInDragLayer(int[] loc) { |
| 4927 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 4928 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 4929 | } |