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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
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; |
Michael Jurka | 8edd75c | 2010-12-17 20:15:06 -0800 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 23 | import android.animation.TimeInterpolator; |
| 24 | import android.animation.ValueAnimator; |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 25 | import android.animation.Animator.AnimatorListener; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 27 | import android.app.AlertDialog; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 28 | import android.app.WallpaperManager; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 29 | import android.appwidget.AppWidgetManager; |
| 30 | import android.appwidget.AppWidgetProviderInfo; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 31 | import android.content.ClipData; |
| 32 | import android.content.ClipDescription; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 33 | import android.content.ComponentName; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 36 | import android.content.pm.PackageManager; |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 37 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.content.res.TypedArray; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 39 | import android.graphics.Bitmap; |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 40 | import android.graphics.Camera; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.graphics.Canvas; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 42 | import android.graphics.Color; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 43 | import android.graphics.Matrix; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 44 | import android.graphics.Paint; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 45 | import android.graphics.Rect; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 46 | import android.graphics.RectF; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 47 | import android.graphics.Region.Op; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 48 | import android.graphics.drawable.Drawable; |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 49 | import android.os.IBinder; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 50 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | import android.util.AttributeSet; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 52 | import android.util.DisplayMetrics; |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 53 | import android.util.Log; |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 54 | import android.util.Pair; |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 55 | import android.view.Display; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 56 | import android.view.DragEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 57 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | import android.view.View; |
Winson Chung | 6e31408 | 2011-01-27 16:46:51 -0800 | [diff] [blame] | 59 | import android.view.ViewGroup; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 60 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 61 | import android.widget.ImageView; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 62 | import android.widget.TextView; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 63 | import android.widget.Toast; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 64 | |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 65 | import com.android.launcher.R; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 66 | import com.android.launcher2.FolderIcon.FolderRingAnimator; |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 67 | import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData; |
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; |
| 70 | import java.util.HashSet; |
| 71 | import java.util.List; |
| 72 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 73 | /** |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 74 | * The workspace is a wide area with a wallpaper and a finite number of pages. |
| 75 | * 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] | 76 | * 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] | 77 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 78 | public class Workspace extends SmoothPagedView |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 79 | implements DropTarget, DragSource, DragScroller, View.OnTouchListener, |
| 80 | DragController.DragListener { |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 81 | @SuppressWarnings({"UnusedDeclaration"}) |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 82 | private static final String TAG = "Launcher.Workspace"; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 83 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 84 | // Y rotation to apply to the workspace screens |
| 85 | private static final float WORKSPACE_ROTATION = 12.5f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 86 | |
| 87 | // These are extra scale factors to apply to the mini home screens |
| 88 | // so as to achieve the desired transform |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 89 | private static final float EXTRA_SCALE_FACTOR_0 = 0.972f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 90 | private static final float EXTRA_SCALE_FACTOR_1 = 1.0f; |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 91 | private static final float EXTRA_SCALE_FACTOR_2 = 1.10f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 92 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 93 | private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0; |
| 94 | private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375; |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 95 | private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 96 | |
Winson Chung | f135c6c | 2010-11-18 16:32:08 -0800 | [diff] [blame] | 97 | private static final int BACKGROUND_FADE_OUT_DURATION = 350; |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 98 | private static final int BACKGROUND_FADE_IN_DURATION = 350; |
| 99 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 100 | private static final int ADJACENT_SCREEN_DROP_DURATION = 300; |
| 101 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 102 | // These animators are used to fade the children's outlines |
| 103 | private ObjectAnimator mChildrenOutlineFadeInAnimation; |
| 104 | private ObjectAnimator mChildrenOutlineFadeOutAnimation; |
| 105 | private float mChildrenOutlineAlpha = 0; |
| 106 | |
| 107 | // 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] | 108 | private ValueAnimator mBackgroundFadeInAnimation; |
| 109 | private ValueAnimator mBackgroundFadeOutAnimation; |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 110 | private Drawable mBackground; |
Michael Jurka | 25356e7 | 2011-03-03 14:53:11 -0800 | [diff] [blame] | 111 | boolean mDrawBackground = true; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 112 | private float mBackgroundAlpha = 0; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 113 | private float mOverScrollMaxBackgroundAlpha = 0.0f; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 114 | private int mOverScrollPageIndex = -1; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 115 | private AnimatorSet mDividerAnimator; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 116 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 117 | private final WallpaperManager mWallpaperManager; |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 118 | private IBinder mWindowToken; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 119 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 120 | private int mDefaultPage; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 121 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | /** |
| 123 | * CellInfo for the cell that is currently being dragged |
| 124 | */ |
| 125 | private CellLayout.CellInfo mDragInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 126 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Target drop area calculated during last acceptDrop call. |
| 129 | */ |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 130 | private int[] mTargetCell = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 131 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 132 | /** |
| 133 | * The CellLayout that is currently being dragged over |
| 134 | */ |
| 135 | private CellLayout mDragTargetLayout = null; |
| 136 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | private Launcher mLauncher; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 138 | private IconCache mIconCache; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 139 | private DragController mDragController; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 140 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 141 | // These are temporary variables to prevent having to allocate a new object just to |
| 142 | // 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] | 143 | private int[] mTempCell = new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 144 | private int[] mTempEstimate = new int[2]; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 145 | private float[] mDragViewVisualCenter = new float[2]; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 146 | private float[] mTempDragCoordinates = new float[2]; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 147 | private float[] mTempCellLayoutCenterCoordinates = new float[2]; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 148 | private float[] mTempDragBottomRightCoordinates = new float[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 149 | private Matrix mTempInverseMatrix = new Matrix(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 150 | |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 151 | private SpringLoadedDragController mSpringLoadedDragController; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 152 | private float mSpringLoadedShrinkFactor; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 153 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 154 | private static final int DEFAULT_CELL_COUNT_X = 4; |
| 155 | private static final int DEFAULT_CELL_COUNT_Y = 4; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 156 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 157 | // 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] | 158 | // in all apps or customize mode) |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 159 | |
| 160 | enum State { NORMAL, SPRING_LOADED, SMALL }; |
| 161 | private State mState; |
| 162 | private boolean mIsSwitchingState = false; |
| 163 | |
| 164 | private boolean mSwitchStateAfterFirstLayout = false; |
| 165 | private State mStateAfterFirstLayout; |
| 166 | |
| 167 | private AnimatorSet mAnimator; |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 168 | private AnimatorListener mShrinkAnimationListener; |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 169 | private AnimatorListener mUnshrinkAnimationListener; |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 170 | // Working around the face that cancelled animations may not actually be cancelled if they |
| 171 | // are cancelled before starting |
| 172 | private boolean mShrinkAnimationEnabled; |
| 173 | private boolean mUnshrinkAnimationEnabled; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 174 | |
| 175 | boolean mAnimatingViewIntoPlace = false; |
| 176 | boolean mIsDragOccuring = false; |
| 177 | boolean mChildrenLayersEnabled = true; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 178 | |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 179 | /** 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] | 180 | private boolean mInScrollArea = false; |
| 181 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 182 | private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper(); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 183 | private Bitmap mDragOutline = null; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 184 | private final Rect mTempRect = new Rect(); |
| 185 | private final int[] mTempXY = new int[2]; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 186 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 187 | // Paint used to draw external drop outline |
| 188 | private final Paint mExternalDragOutlinePaint = new Paint(); |
| 189 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 190 | // Camera and Matrix used to determine the final position of a neighboring CellLayout |
| 191 | private final Matrix mMatrix = new Matrix(); |
| 192 | private final Camera mCamera = new Camera(); |
| 193 | private final float mTempFloat2[] = new float[2]; |
| 194 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 195 | enum WallpaperVerticalOffset { TOP, MIDDLE, BOTTOM }; |
| 196 | int mWallpaperWidth; |
| 197 | int mWallpaperHeight; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 198 | WallpaperOffsetInterpolator mWallpaperOffset; |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 199 | boolean mUpdateWallpaperOffsetImmediately = false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 200 | boolean mSyncWallpaperOffsetWithScroll = true; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 201 | private Runnable mDelayedResizeRunnable; |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 202 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 203 | // Variables relating to the creation of user folders by hovering shortcuts over shortcuts |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 204 | private static final int FOLDER_CREATION_TIMEOUT = 250; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 205 | private final Alarm mFolderCreationAlarm = new Alarm(); |
| 206 | private FolderRingAnimator mDragFolderRingAnimator = null; |
| 207 | private View mLastDragOverView = null; |
| 208 | private boolean mCreateUserFolderOnDrop = false; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 209 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 210 | // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) |
| 211 | private float mXDown; |
| 212 | private float mYDown; |
| 213 | final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; |
| 214 | final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; |
| 215 | final static float TOUCH_SLOP_DAMPING_FACTOR = 4; |
| 216 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 217 | // 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] | 218 | private int mSavedScrollX; |
| 219 | private float mSavedRotationY; |
| 220 | private float mSavedTranslationX; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 221 | private float mCurrentScaleX; |
| 222 | private float mCurrentScaleY; |
| 223 | private float mCurrentRotationY; |
| 224 | private float mCurrentTranslationX; |
| 225 | private float mCurrentTranslationY; |
| 226 | private float[] mOldTranslationXs; |
| 227 | private float[] mOldTranslationYs; |
| 228 | private float[] mOldScaleXs; |
| 229 | private float[] mOldScaleYs; |
| 230 | private float[] mOldBackgroundAlphas; |
| 231 | private float[] mOldBackgroundAlphaMultipliers; |
| 232 | private float[] mOldAlphas; |
| 233 | private float[] mOldRotationYs; |
| 234 | private float[] mNewTranslationXs; |
| 235 | private float[] mNewTranslationYs; |
| 236 | private float[] mNewScaleXs; |
| 237 | private float[] mNewScaleYs; |
| 238 | private float[] mNewBackgroundAlphas; |
| 239 | private float[] mNewBackgroundAlphaMultipliers; |
| 240 | private float[] mNewAlphas; |
| 241 | private float[] mNewRotationYs; |
| 242 | private float mTransitionProgress; |
| 243 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Used to inflate the Workspace from XML. |
| 246 | * |
| 247 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 248 | * @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] | 249 | */ |
| 250 | public Workspace(Context context, AttributeSet attrs) { |
| 251 | this(context, attrs, 0); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Used to inflate the Workspace from XML. |
| 256 | * |
| 257 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 258 | * @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] | 259 | * @param defStyle Unused. |
| 260 | */ |
| 261 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 262 | super(context, attrs, defStyle); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 263 | mContentIsRefreshable = false; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 264 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 265 | // With workspace, data is available straight from the get-go |
| 266 | setDataIsReady(); |
| 267 | |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 268 | if (!LauncherApplication.isScreenLarge()) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 269 | mCenterPagesVertically = false; |
| 270 | if (!LauncherApplication.isScreenLandscape(context)) { |
| 271 | mFadeInAdjacentScreens = false; |
| 272 | } |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 273 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 274 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 275 | mWallpaperManager = WallpaperManager.getInstance(context); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 276 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 277 | int cellCountX = DEFAULT_CELL_COUNT_X; |
| 278 | int cellCountY = DEFAULT_CELL_COUNT_Y; |
| 279 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 280 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 281 | R.styleable.Workspace, defStyle, 0); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 282 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 283 | final Resources res = context.getResources(); |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 284 | if (LauncherApplication.isScreenLarge()) { |
Michael Jurka | f61249b | 2011-05-27 16:27:15 -0700 | [diff] [blame] | 285 | // Determine number of rows/columns dynamically |
| 286 | // TODO: This code currently fails on tablets with an aspect ratio < 1.3. |
| 287 | // Around that ratio we should make cells the same size in portrait and |
| 288 | // landscape |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 289 | TypedArray actionBarSizeTypedArray = |
| 290 | context.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize }); |
Michael Jurka | f61249b | 2011-05-27 16:27:15 -0700 | [diff] [blame] | 291 | final float actionBarHeight = actionBarSizeTypedArray.getDimension(0, 0f); |
| 292 | final float systemBarHeight = res.getDimension(R.dimen.status_bar_height); |
| 293 | final float smallestScreenDim = res.getConfiguration().smallestScreenWidthDp; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 294 | |
Michael Jurka | f61249b | 2011-05-27 16:27:15 -0700 | [diff] [blame] | 295 | cellCountX = 1; |
| 296 | while (CellLayout.widthInPortrait(res, cellCountX + 1) <= smallestScreenDim) { |
| 297 | cellCountX++; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 298 | } |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 299 | |
Michael Jurka | f61249b | 2011-05-27 16:27:15 -0700 | [diff] [blame] | 300 | cellCountY = 1; |
| 301 | while (actionBarHeight + CellLayout.heightInLandscape(res, cellCountY + 1) |
| 302 | <= smallestScreenDim - systemBarHeight) { |
| 303 | cellCountY++; |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 304 | } |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 307 | mSpringLoadedShrinkFactor = |
| 308 | res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; |
| 309 | |
Michael Jurka | f6440da | 2011-04-05 14:50:34 -0700 | [diff] [blame] | 310 | // if the value is manually specified, use that instead |
| 311 | cellCountX = a.getInt(R.styleable.Workspace_cellCountX, cellCountX); |
| 312 | cellCountY = a.getInt(R.styleable.Workspace_cellCountY, cellCountY); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 313 | mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 314 | a.recycle(); |
| 315 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 316 | LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 317 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 318 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 319 | initWorkspace(); |
Winson Chung | c35afb2 | 2011-02-23 13:01:49 -0800 | [diff] [blame] | 320 | |
| 321 | // Disable multitouch across the workspace/all apps/customize tray |
| 322 | setMotionEventSplittingEnabled(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 325 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 326 | mIsDragOccuring = true; |
| 327 | updateChildrenLayersEnabled(); |
Adam Cohen | 95383fe | 2011-07-17 18:06:55 -0700 | [diff] [blame] | 328 | mLauncher.lockScreenOrientation(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | public void onDragEnd() { |
| 332 | mIsDragOccuring = false; |
| 333 | updateChildrenLayersEnabled(); |
Adam Cohen | 95383fe | 2011-07-17 18:06:55 -0700 | [diff] [blame] | 334 | mLauncher.unlockScreenOrientation(); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 335 | } |
| 336 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Initializes various states for this workspace. |
| 339 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 340 | protected void initWorkspace() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 341 | Context context = getContext(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 342 | mCurrentPage = mDefaultPage; |
| 343 | Launcher.setScreen(mCurrentPage); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 344 | LauncherApplication app = (LauncherApplication)context.getApplicationContext(); |
| 345 | mIconCache = app.getIconCache(); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 346 | mExternalDragOutlinePaint.setAntiAlias(true); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 347 | setWillNotDraw(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 348 | |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 349 | try { |
| 350 | final Resources res = getResources(); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 351 | mBackground = res.getDrawable(R.drawable.apps_customize_bg); |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 352 | } catch (Resources.NotFoundException e) { |
| 353 | // In this case, we will skip drawing background protection |
| 354 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 355 | |
Michael Jurka | 8edd75c | 2010-12-17 20:15:06 -0800 | [diff] [blame] | 356 | mUnshrinkAnimationListener = new AnimatorListenerAdapter() { |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 357 | @Override |
Michael Jurka | c0e8fca | 2010-10-06 16:41:29 -0700 | [diff] [blame] | 358 | public void onAnimationStart(Animator animation) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 359 | mIsSwitchingState = true; |
Michael Jurka | c0e8fca | 2010-10-06 16:41:29 -0700 | [diff] [blame] | 360 | } |
Michael Jurka | 0bb8563 | 2011-01-26 00:00:44 -0800 | [diff] [blame] | 361 | |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 362 | @Override |
Michael Jurka | 8edd75c | 2010-12-17 20:15:06 -0800 | [diff] [blame] | 363 | public void onAnimationEnd(Animator animation) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 364 | mIsSwitchingState = false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 365 | mSyncWallpaperOffsetWithScroll = true; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 366 | mWallpaperOffset.setOverrideHorizontalCatchupConstant(false); |
Patrick Dubroy | 60b7c53 | 2011-01-16 17:19:32 -0800 | [diff] [blame] | 367 | mAnimator = null; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 368 | updateChildrenLayersEnabled(); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 369 | } |
| 370 | }; |
| 371 | mShrinkAnimationListener = new AnimatorListenerAdapter() { |
| 372 | @Override |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 373 | public void onAnimationStart(Animator animation) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 374 | mIsSwitchingState = true; |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 375 | } |
| 376 | @Override |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 377 | public void onAnimationEnd(Animator animation) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 378 | mIsSwitchingState = false; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 379 | mWallpaperOffset.setOverrideHorizontalCatchupConstant(false); |
Patrick Dubroy | 60b7c53 | 2011-01-16 17:19:32 -0800 | [diff] [blame] | 380 | mAnimator = null; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 381 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 382 | }; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 383 | mSnapVelocity = 600; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 384 | mWallpaperOffset = new WallpaperOffsetInterpolator(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 385 | } |
| 386 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 387 | @Override |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 388 | protected int getScrollMode() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 389 | return SmoothPagedView.X_LARGE_MODE; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 392 | private void onAddView(View child) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 393 | if (!(child instanceof CellLayout)) { |
| 394 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 395 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 396 | CellLayout cl = ((CellLayout) child); |
| 397 | cl.setOnInterceptTouchListener(this); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 398 | cl.setClickable(true); |
| 399 | cl.enableHardwareLayers(); |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 400 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 401 | |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 402 | @Override |
| 403 | public void addView(View child, int index, LayoutParams params) { |
| 404 | onAddView(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 405 | super.addView(child, index, params); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void addView(View child) { |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 410 | onAddView(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 411 | super.addView(child); |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void addView(View child, int index) { |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 416 | onAddView(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 417 | super.addView(child, index); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public void addView(View child, int width, int height) { |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 422 | onAddView(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | super.addView(child, width, height); |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public void addView(View child, LayoutParams params) { |
Michael Jurka | e5fb0f2 | 2011-04-11 13:27:46 -0700 | [diff] [blame] | 428 | onAddView(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 429 | super.addView(child, params); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @return The open folder on the current screen, or null if there is none |
| 434 | */ |
| 435 | Folder getOpenFolder() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 436 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 437 | int count = dragLayer.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 438 | for (int i = 0; i < count; i++) { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 439 | View child = dragLayer.getChildAt(i); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 440 | if (child instanceof Folder) { |
| 441 | Folder folder = (Folder) child; |
| 442 | if (folder.getInfo().opened) |
| 443 | return folder; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | return null; |
| 447 | } |
| 448 | |
Patrick Dubroy | a0aa012 | 2011-02-24 11:42:23 -0800 | [diff] [blame] | 449 | boolean isTouchActive() { |
| 450 | return mTouchState != TOUCH_STATE_REST; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 454 | * Adds the specified child in the specified screen. The position and dimension of |
| 455 | * the child are defined by x, y, spanX and spanY. |
| 456 | * |
| 457 | * @param child The child to add in one of the workspace's screens. |
| 458 | * @param screen The screen in which to add the child. |
| 459 | * @param x The X position of the child in the screen's grid. |
| 460 | * @param y The Y position of the child in the screen's grid. |
| 461 | * @param spanX The number of cells spanned horizontally by the child. |
| 462 | * @param spanY The number of cells spanned vertically by the child. |
| 463 | */ |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 464 | void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY) { |
| 465 | addInScreen(child, container, screen, x, y, spanX, spanY, false); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 466 | } |
| 467 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 468 | /** |
| 469 | * Adds the specified child in the specified screen. The position and dimension of |
| 470 | * the child are defined by x, y, spanX and spanY. |
| 471 | * |
| 472 | * @param child The child to add in one of the workspace's screens. |
| 473 | * @param screen The screen in which to add the child. |
| 474 | * @param x The X position of the child in the screen's grid. |
| 475 | * @param y The Y position of the child in the screen's grid. |
| 476 | * @param spanX The number of cells spanned horizontally by the child. |
| 477 | * @param spanY The number of cells spanned vertically by the child. |
| 478 | * @param insert When true, the child is inserted at the beginning of the children list. |
| 479 | */ |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 480 | void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY, |
| 481 | boolean insert) { |
| 482 | if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 483 | if (screen < 0 || screen >= getChildCount()) { |
| 484 | Log.e(TAG, "The screen must be >= 0 and < " + getChildCount() |
| 485 | + " (was " + screen + "); skipping child"); |
| 486 | return; |
| 487 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 490 | final CellLayout layout; |
| 491 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 492 | layout = mLauncher.getHotseat().getLayout(); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 493 | child.setOnKeyListener(null); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 494 | |
| 495 | if (screen < 0) { |
| 496 | screen = mLauncher.getHotseat().getOrderInHotseat(x, y); |
| 497 | } else { |
| 498 | // Note: We do this to ensure that the hotseat is always laid out in the orientation |
| 499 | // of the hotseat in order regardless of which orientation they were added |
| 500 | x = mLauncher.getHotseat().getCellXFromOrder(screen); |
| 501 | y = mLauncher.getHotseat().getCellYFromOrder(screen); |
| 502 | } |
| 503 | } else { |
| 504 | layout = (CellLayout) getChildAt(screen); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 505 | child.setOnKeyListener(new BubbleTextViewKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 506 | } |
| 507 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 508 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 509 | if (lp == null) { |
| 510 | lp = new CellLayout.LayoutParams(x, y, spanX, spanY); |
| 511 | } else { |
| 512 | lp.cellX = x; |
| 513 | lp.cellY = y; |
| 514 | lp.cellHSpan = spanX; |
| 515 | lp.cellVSpan = spanY; |
| 516 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 517 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 518 | if (spanX < 0 && spanY < 0) { |
| 519 | lp.isLockedToGrid = false; |
| 520 | } |
| 521 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 522 | // Get the canonical child id to uniquely represent this view in this screen |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 523 | int childId = LauncherModel.getCellLayoutChildId(container, screen, x, y, spanX, spanY); |
Michael Jurka | f3ca3ab | 2010-10-20 17:08:24 -0700 | [diff] [blame] | 524 | boolean markCellsAsOccupied = !(child instanceof Folder); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 525 | if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 526 | // TODO: This branch occurs when the workspace is adding views |
| 527 | // outside of the defined grid |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 528 | // maybe we should be deleting these items from the LauncherModel? |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 529 | Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); |
| 530 | } |
| 531 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 532 | if (!(child instanceof Folder)) { |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 533 | child.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 534 | child.setOnLongClickListener(mLongClickListener); |
| 535 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 536 | if (child instanceof DropTarget) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 537 | mDragController.addDropTarget((DropTarget) child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 538 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 541 | /** |
| 542 | * Check if the point (x, y) hits a given page. |
| 543 | */ |
| 544 | private boolean hitsPage(int index, float x, float y) { |
| 545 | final View page = getChildAt(index); |
| 546 | if (page != null) { |
| 547 | float[] localXY = { x, y }; |
| 548 | mapPointFromSelfToChild(page, localXY); |
| 549 | return (localXY[0] >= 0 && localXY[0] < page.getWidth() |
| 550 | && localXY[1] >= 0 && localXY[1] < page.getHeight()); |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | protected boolean hitsPreviousPage(float x, float y) { |
| 557 | // mNextPage is set to INVALID_PAGE whenever we are stationary. |
| 558 | // Calculating "next page" this way ensures that you scroll to whatever page you tap on |
| 559 | final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage; |
| 560 | return hitsPage(current - 1, x, y); |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | protected boolean hitsNextPage(float x, float y) { |
| 565 | // mNextPage is set to INVALID_PAGE whenever we are stationary. |
| 566 | // Calculating "next page" this way ensures that you scroll to whatever page you tap on |
| 567 | final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage; |
| 568 | return hitsPage(current + 1, x, y); |
| 569 | } |
| 570 | |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 571 | /** |
| 572 | * Called directly from a CellLayout (not by the framework), after we've been added as a |
| 573 | * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout |
| 574 | * that it should intercept touch events, which is not something that is normally supported. |
| 575 | */ |
| 576 | @Override |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 577 | public boolean onTouch(View v, MotionEvent event) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 578 | return (isSmall() || mIsSwitchingState); |
Patrick Dubroy | e708c52 | 2011-03-01 16:03:43 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 581 | public boolean isSwitchingState() { |
| 582 | return mIsSwitchingState; |
| 583 | } |
| 584 | |
Adam Cohen | ded9f8d | 2010-11-03 13:25:16 -0700 | [diff] [blame] | 585 | protected void onWindowVisibilityChanged (int visibility) { |
| 586 | mLauncher.onWindowVisibilityChanged(visibility); |
| 587 | } |
| 588 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 589 | @Override |
| 590 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 591 | if (isSmall() || mIsSwitchingState) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 592 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 593 | return false; |
| 594 | } |
| 595 | return super.dispatchUnhandledMove(focused, direction); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 600 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 601 | mXDown = ev.getX(); |
| 602 | mYDown = ev.getY(); |
| 603 | } |
| 604 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 605 | return super.onInterceptTouchEvent(ev); |
| 606 | } |
| 607 | |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 608 | @Override |
| 609 | protected void determineScrollingStart(MotionEvent ev) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 610 | if (!isSmall() && !mIsSwitchingState) { |
Adam Cohen | 69c42da | 2011-02-03 16:38:07 -0800 | [diff] [blame] | 611 | float deltaX = Math.abs(ev.getX() - mXDown); |
| 612 | float deltaY = Math.abs(ev.getY() - mYDown); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 613 | |
Adam Cohen | 69c42da | 2011-02-03 16:38:07 -0800 | [diff] [blame] | 614 | if (Float.compare(deltaX, 0f) == 0) return; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 615 | |
Adam Cohen | 69c42da | 2011-02-03 16:38:07 -0800 | [diff] [blame] | 616 | float slope = deltaY / deltaX; |
| 617 | float theta = (float) Math.atan(slope); |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 618 | |
Adam Cohen | 69c42da | 2011-02-03 16:38:07 -0800 | [diff] [blame] | 619 | if (deltaX > mTouchSlop || deltaY > mTouchSlop) { |
| 620 | cancelCurrentPageLongPress(); |
| 621 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 622 | |
Adam Cohen | 69c42da | 2011-02-03 16:38:07 -0800 | [diff] [blame] | 623 | if (theta > MAX_SWIPE_ANGLE) { |
| 624 | // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace |
| 625 | return; |
| 626 | } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { |
| 627 | // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to |
| 628 | // increase the touch slop to make it harder to begin scrolling the workspace. This |
| 629 | // results in vertically scrolling widgets to more easily. The higher the angle, the |
| 630 | // more we increase touch slop. |
| 631 | theta -= START_DAMPING_TOUCH_SLOP_ANGLE; |
| 632 | float extraRatio = (float) |
| 633 | Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); |
| 634 | super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); |
| 635 | } else { |
| 636 | // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special |
| 637 | super.determineScrollingStart(ev); |
| 638 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 639 | } |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 642 | @Override |
| 643 | protected boolean isScrollingIndicatorEnabled() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 644 | return mState != State.SPRING_LOADED; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 647 | protected void onPageBeginMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 648 | super.onPageBeginMoving(); |
| 649 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 650 | if (isHardwareAccelerated()) { |
| 651 | updateChildrenLayersEnabled(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 652 | } else { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 653 | if (mNextPage != INVALID_PAGE) { |
| 654 | // we're snapping to a particular screen |
| 655 | enableChildrenCache(mCurrentPage, mNextPage); |
| 656 | } else { |
| 657 | // this is when user is actively dragging a particular screen, they might |
| 658 | // swipe it either left or right (but we won't advance by more than one screen) |
| 659 | enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); |
| 660 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 661 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 662 | |
| 663 | // Only show page outlines as we pan if we are on large screen |
| 664 | if (LauncherApplication.isScreenLarge()) { |
| 665 | showOutlines(); |
| 666 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 669 | protected void onPageEndMoving() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 670 | super.onPageEndMoving(); |
| 671 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 672 | if (isHardwareAccelerated()) { |
| 673 | updateChildrenLayersEnabled(); |
| 674 | } else { |
| 675 | clearChildrenCache(); |
| 676 | } |
| 677 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 678 | // Hide the outlines, as long as we're not dragging |
| 679 | if (!mDragController.dragging()) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 680 | // Only hide page outlines as we pan if we are on large screen |
| 681 | if (LauncherApplication.isScreenLarge()) { |
| 682 | hideOutlines(); |
| 683 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 684 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 685 | mOverScrollMaxBackgroundAlpha = 0.0f; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 686 | mOverScrollPageIndex = -1; |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 687 | |
| 688 | if (mDelayedResizeRunnable != null) { |
| 689 | mDelayedResizeRunnable.run(); |
| 690 | mDelayedResizeRunnable = null; |
| 691 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | @Override |
| 695 | protected void notifyPageSwitchListener() { |
| 696 | super.notifyPageSwitchListener(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 697 | Launcher.setScreen(mCurrentPage); |
| 698 | }; |
| 699 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 700 | // As a ratio of screen height, the total distance we want the parallax effect to span |
| 701 | // vertically |
| 702 | private float wallpaperTravelToScreenHeightRatio(int width, int height) { |
| 703 | return 1.1f; |
| 704 | } |
| 705 | |
| 706 | // As a ratio of screen height, the total distance we want the parallax effect to span |
| 707 | // horizontally |
| 708 | private float wallpaperTravelToScreenWidthRatio(int width, int height) { |
| 709 | float aspectRatio = width / (float) height; |
| 710 | |
| 711 | // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width |
| 712 | // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width |
| 713 | // We will use these two data points to extrapolate how much the wallpaper parallax effect |
| 714 | // to span (ie travel) at any aspect ratio: |
| 715 | |
| 716 | final float ASPECT_RATIO_LANDSCAPE = 16/10f; |
| 717 | final float ASPECT_RATIO_PORTRAIT = 10/16f; |
| 718 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; |
| 719 | final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; |
| 720 | |
| 721 | // To find out the desired width at different aspect ratios, we use the following two |
| 722 | // formulas, where the coefficient on x is the aspect ratio (width/height): |
| 723 | // (16/10)x + y = 1.5 |
| 724 | // (10/16)x + y = 1.2 |
| 725 | // We solve for x and y and end up with a final formula: |
| 726 | final float x = |
| 727 | (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / |
| 728 | (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); |
| 729 | final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; |
| 730 | return x * aspectRatio + y; |
| 731 | } |
| 732 | |
| 733 | // The range of scroll values for Workspace |
| 734 | private int getScrollRange() { |
| 735 | return getChildOffset(getChildCount() - 1) - getChildOffset(0); |
| 736 | } |
| 737 | |
| 738 | protected void setWallpaperDimension() { |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 739 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 740 | DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 741 | display.getRealMetrics(displayMetrics); |
| 742 | final int maxDim = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels); |
| 743 | final int minDim = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 744 | |
| 745 | // We need to ensure that there is enough extra space in the wallpaper for the intended |
| 746 | // parallax effects |
| 747 | mWallpaperWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); |
| 748 | mWallpaperHeight = (int)(maxDim * wallpaperTravelToScreenHeightRatio(maxDim, minDim)); |
Winson Chung | f7640c8 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 749 | new Thread("setWallpaperDimension") { |
| 750 | public void run() { |
| 751 | mWallpaperManager.suggestDesiredDimensions(mWallpaperWidth, mWallpaperHeight); |
| 752 | } |
| 753 | }.start(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 756 | public void setVerticalWallpaperOffset(float offset) { |
| 757 | mWallpaperOffset.setFinalY(offset); |
| 758 | } |
| 759 | public float getVerticalWallpaperOffset() { |
| 760 | return mWallpaperOffset.getCurrY(); |
| 761 | } |
| 762 | public void setHorizontalWallpaperOffset(float offset) { |
| 763 | mWallpaperOffset.setFinalX(offset); |
| 764 | } |
| 765 | public float getHorizontalWallpaperOffset() { |
| 766 | return mWallpaperOffset.getCurrX(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 769 | private float wallpaperOffsetForCurrentScroll() { |
| 770 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
Michael Jurka | f70c781 | 2011-03-07 16:53:12 -0800 | [diff] [blame] | 771 | final boolean isStaticWallpaper = (mWallpaperManager.getWallpaperInfo() == null); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 772 | // The wallpaper travel width is how far, from left to right, the wallpaper will move |
| 773 | // at this orientation (for example, in portrait mode we don't move all the way to the |
| 774 | // edges of the wallpaper, or otherwise the parallax effect would be too strong) |
| 775 | int wallpaperTravelWidth = (int) (display.getWidth() * |
| 776 | wallpaperTravelToScreenWidthRatio(display.getWidth(), display.getHeight())); |
Michael Jurka | f70c781 | 2011-03-07 16:53:12 -0800 | [diff] [blame] | 777 | if (!isStaticWallpaper) { |
| 778 | wallpaperTravelWidth = mWallpaperWidth; |
| 779 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 780 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 781 | // Set wallpaper offset steps (1 / (number of screens - 1)) |
| 782 | // We have 3 vertical offset states (centered, and then top/bottom aligned |
| 783 | // for all apps/customize) |
| 784 | mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f / (3 - 1)); |
| 785 | |
Michael Jurka | 09d6eb0 | 2011-01-28 16:05:09 -0800 | [diff] [blame] | 786 | int scrollRange = getScrollRange(); |
| 787 | float scrollProgressOffset = 0; |
| 788 | |
| 789 | // Account for overscroll: you only see the absolute edge of the wallpaper if |
| 790 | // you overscroll as far as you can in landscape mode. Only do this for static wallpapers |
| 791 | // because live wallpapers (and probably 3rd party wallpaper providers) rely on the offset |
| 792 | // being even intervals from 0 to 1 (eg [0, 0.25, 0.5, 0.75, 1]) |
Michael Jurka | 09d6eb0 | 2011-01-28 16:05:09 -0800 | [diff] [blame] | 793 | if (isStaticWallpaper) { |
| 794 | int overscrollOffset = (int) (maxOverScroll() * display.getWidth()); |
| 795 | scrollProgressOffset += overscrollOffset / (float) getScrollRange(); |
| 796 | scrollRange += 2 * overscrollOffset; |
| 797 | } |
| 798 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 799 | float scrollProgress = |
Michael Jurka | 09d6eb0 | 2011-01-28 16:05:09 -0800 | [diff] [blame] | 800 | mScrollX / (float) scrollRange + scrollProgressOffset; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 801 | float offsetInDips = wallpaperTravelWidth * scrollProgress + |
Michael Jurka | f70c781 | 2011-03-07 16:53:12 -0800 | [diff] [blame] | 802 | (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 803 | float offset = offsetInDips / (float) mWallpaperWidth; |
| 804 | return offset; |
| 805 | } |
| 806 | private void syncWallpaperOffsetWithScroll() { |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 807 | final boolean enableWallpaperEffects = isHardwareAccelerated(); |
| 808 | if (enableWallpaperEffects) { |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 809 | mWallpaperOffset.setFinalX(wallpaperOffsetForCurrentScroll()); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | public void updateWallpaperOffsetImmediately() { |
| 814 | mUpdateWallpaperOffsetImmediately = true; |
| 815 | } |
| 816 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 817 | private void updateWallpaperOffsets() { |
| 818 | boolean updateNow = false; |
| 819 | boolean keepUpdating = true; |
| 820 | if (mUpdateWallpaperOffsetImmediately) { |
| 821 | updateNow = true; |
| 822 | keepUpdating = false; |
| 823 | mWallpaperOffset.jumpToFinal(); |
| 824 | mUpdateWallpaperOffsetImmediately = false; |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 825 | } else { |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 826 | updateNow = keepUpdating = mWallpaperOffset.computeScrollOffset(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 827 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 828 | if (updateNow) { |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 829 | if (mWindowToken != null) { |
| 830 | mWallpaperManager.setWallpaperOffsets(mWindowToken, |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 831 | mWallpaperOffset.getCurrX(), mWallpaperOffset.getCurrY()); |
| 832 | } |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 833 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 834 | if (keepUpdating) { |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 835 | fastInvalidate(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 836 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | class WallpaperOffsetInterpolator { |
| 840 | float mFinalHorizontalWallpaperOffset = 0.0f; |
| 841 | float mFinalVerticalWallpaperOffset = 0.5f; |
| 842 | float mHorizontalWallpaperOffset = 0.0f; |
| 843 | float mVerticalWallpaperOffset = 0.5f; |
| 844 | long mLastWallpaperOffsetUpdateTime; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 845 | boolean mIsMovingFast; |
| 846 | boolean mOverrideHorizontalCatchupConstant; |
| 847 | float mHorizontalCatchupConstant = 0.35f; |
| 848 | float mVerticalCatchupConstant = 0.35f; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 849 | |
| 850 | public WallpaperOffsetInterpolator() { |
| 851 | } |
| 852 | |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 853 | public void setOverrideHorizontalCatchupConstant(boolean override) { |
| 854 | mOverrideHorizontalCatchupConstant = override; |
| 855 | } |
| 856 | |
| 857 | public void setHorizontalCatchupConstant(float f) { |
| 858 | mHorizontalCatchupConstant = f; |
| 859 | } |
| 860 | |
| 861 | public void setVerticalCatchupConstant(float f) { |
| 862 | mVerticalCatchupConstant = f; |
| 863 | } |
| 864 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 865 | public boolean computeScrollOffset() { |
| 866 | if (Float.compare(mHorizontalWallpaperOffset, mFinalHorizontalWallpaperOffset) == 0 && |
| 867 | Float.compare(mVerticalWallpaperOffset, mFinalVerticalWallpaperOffset) == 0) { |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 868 | mIsMovingFast = false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 869 | return false; |
| 870 | } |
| 871 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
| 872 | boolean isLandscape = display.getWidth() > display.getHeight(); |
| 873 | |
| 874 | long currentTime = System.currentTimeMillis(); |
| 875 | long timeSinceLastUpdate = currentTime - mLastWallpaperOffsetUpdateTime; |
| 876 | timeSinceLastUpdate = Math.min((long) (1000/30f), timeSinceLastUpdate); |
| 877 | timeSinceLastUpdate = Math.max(1L, timeSinceLastUpdate); |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 878 | |
| 879 | float xdiff = Math.abs(mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset); |
| 880 | if (!mIsMovingFast && xdiff > 0.07) { |
| 881 | mIsMovingFast = true; |
| 882 | } |
| 883 | |
| 884 | float fractionToCatchUpIn1MsHorizontal; |
| 885 | if (mOverrideHorizontalCatchupConstant) { |
| 886 | fractionToCatchUpIn1MsHorizontal = mHorizontalCatchupConstant; |
| 887 | } else if (mIsMovingFast) { |
| 888 | fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.5f : 0.75f; |
| 889 | } else { |
| 890 | // slow |
| 891 | fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.27f : 0.5f; |
| 892 | } |
| 893 | float fractionToCatchUpIn1MsVertical = mVerticalCatchupConstant; |
| 894 | |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 895 | fractionToCatchUpIn1MsHorizontal /= 33f; |
| 896 | fractionToCatchUpIn1MsVertical /= 33f; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 897 | |
| 898 | final float UPDATE_THRESHOLD = 0.00001f; |
| 899 | float hOffsetDelta = mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset; |
| 900 | float vOffsetDelta = mFinalVerticalWallpaperOffset - mVerticalWallpaperOffset; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 901 | boolean jumpToFinalValue = Math.abs(hOffsetDelta) < UPDATE_THRESHOLD && |
| 902 | Math.abs(vOffsetDelta) < UPDATE_THRESHOLD; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 903 | if (jumpToFinalValue) { |
| 904 | mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset; |
| 905 | mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset; |
| 906 | } else { |
| 907 | float percentToCatchUpVertical = |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 908 | Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsVertical); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 909 | float percentToCatchUpHorizontal = |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 910 | Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsHorizontal); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 911 | mHorizontalWallpaperOffset += percentToCatchUpHorizontal * hOffsetDelta; |
| 912 | mVerticalWallpaperOffset += percentToCatchUpVertical * vOffsetDelta; |
| 913 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 914 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 915 | mLastWallpaperOffsetUpdateTime = System.currentTimeMillis(); |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | public float getCurrX() { |
| 920 | return mHorizontalWallpaperOffset; |
| 921 | } |
| 922 | |
| 923 | public float getFinalX() { |
| 924 | return mFinalHorizontalWallpaperOffset; |
| 925 | } |
| 926 | |
| 927 | public float getCurrY() { |
| 928 | return mVerticalWallpaperOffset; |
| 929 | } |
| 930 | |
| 931 | public float getFinalY() { |
| 932 | return mFinalVerticalWallpaperOffset; |
| 933 | } |
| 934 | |
| 935 | public void setFinalX(float x) { |
| 936 | mFinalHorizontalWallpaperOffset = Math.max(0f, Math.min(x, 1.0f)); |
| 937 | } |
| 938 | |
| 939 | public void setFinalY(float y) { |
| 940 | mFinalVerticalWallpaperOffset = Math.max(0f, Math.min(y, 1.0f)); |
| 941 | } |
| 942 | |
| 943 | public void jumpToFinal() { |
| 944 | mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset; |
| 945 | mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset; |
| 946 | } |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 947 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 948 | |
Michael Jurka | 340c5f3 | 2010-10-21 16:49:19 -0700 | [diff] [blame] | 949 | @Override |
| 950 | public void computeScroll() { |
| 951 | super.computeScroll(); |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 952 | if (mSyncWallpaperOffsetWithScroll) { |
| 953 | syncWallpaperOffsetWithScroll(); |
| 954 | } |
Michael Jurka | 340c5f3 | 2010-10-21 16:49:19 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 957 | void showOutlines() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 958 | if (!isSmall() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 959 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
| 960 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
| 961 | mChildrenOutlineFadeInAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 1.0f); |
| 962 | mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION); |
| 963 | mChildrenOutlineFadeInAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 964 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 967 | void hideOutlines() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 968 | if (!isSmall() && !mIsSwitchingState) { |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 969 | if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); |
| 970 | if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); |
| 971 | mChildrenOutlineFadeOutAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 0.0f); |
| 972 | mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION); |
| 973 | mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY); |
| 974 | mChildrenOutlineFadeOutAnimation.start(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 975 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Patrick Dubroy | 94f78a5 | 2011-02-28 17:39:16 -0800 | [diff] [blame] | 978 | public void showOutlinesTemporarily() { |
| 979 | if (!mIsPageMoving && !isTouchActive()) { |
| 980 | snapToPage(mCurrentPage); |
| 981 | } |
| 982 | } |
| 983 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 984 | public void setChildrenOutlineAlpha(float alpha) { |
| 985 | mChildrenOutlineAlpha = alpha; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 986 | for (int i = 0; i < getChildCount(); i++) { |
| 987 | CellLayout cl = (CellLayout) getChildAt(i); |
| 988 | cl.setBackgroundAlpha(alpha); |
| 989 | } |
| 990 | } |
| 991 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 992 | public float getChildrenOutlineAlpha() { |
| 993 | return mChildrenOutlineAlpha; |
| 994 | } |
| 995 | |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 996 | void disableBackground() { |
| 997 | mDrawBackground = false; |
| 998 | } |
| 999 | void enableBackground() { |
| 1000 | mDrawBackground = true; |
| 1001 | } |
| 1002 | |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1003 | private void showBackgroundGradientForAllApps(boolean animated) { |
| 1004 | showBackgroundGradient(animated); |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1007 | private void showBackgroundGradient(boolean animated) { |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 1008 | if (mBackground == null) return; |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1009 | if (mBackgroundFadeOutAnimation != null) { |
| 1010 | mBackgroundFadeOutAnimation.cancel(); |
| 1011 | mBackgroundFadeOutAnimation = null; |
| 1012 | } |
| 1013 | if (mBackgroundFadeInAnimation != null) { |
| 1014 | mBackgroundFadeInAnimation.cancel(); |
| 1015 | mBackgroundFadeInAnimation = null; |
| 1016 | } |
| 1017 | final float finalAlpha = 1f; |
| 1018 | if (animated) { |
| 1019 | mBackgroundFadeInAnimation = ValueAnimator.ofFloat(getBackgroundAlpha(), finalAlpha); |
| 1020 | mBackgroundFadeInAnimation.addUpdateListener(new AnimatorUpdateListener() { |
| 1021 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1022 | setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue()); |
| 1023 | } |
| 1024 | }); |
| 1025 | mBackgroundFadeInAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 1026 | mBackgroundFadeInAnimation.setDuration(BACKGROUND_FADE_IN_DURATION); |
| 1027 | mBackgroundFadeInAnimation.start(); |
| 1028 | } else { |
| 1029 | setBackgroundAlpha(finalAlpha); |
| 1030 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1033 | private void hideBackgroundGradient(float finalAlpha, boolean animated) { |
Winson Chung | b4b7fa7 | 2010-11-18 14:38:53 -0800 | [diff] [blame] | 1034 | if (mBackground == null) return; |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1035 | if (mBackgroundFadeInAnimation != null) { |
| 1036 | mBackgroundFadeInAnimation.cancel(); |
| 1037 | mBackgroundFadeInAnimation = null; |
| 1038 | } |
| 1039 | if (mBackgroundFadeOutAnimation != null) { |
| 1040 | mBackgroundFadeOutAnimation.cancel(); |
| 1041 | mBackgroundFadeOutAnimation = null; |
| 1042 | } |
| 1043 | if (animated) { |
| 1044 | mBackgroundFadeOutAnimation = ValueAnimator.ofFloat(getBackgroundAlpha(), finalAlpha); |
| 1045 | mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() { |
| 1046 | public void onAnimationUpdate(ValueAnimator animation) { |
| 1047 | setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue()); |
| 1048 | } |
| 1049 | }); |
| 1050 | mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 1051 | mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION); |
| 1052 | mBackgroundFadeOutAnimation.start(); |
| 1053 | } else { |
| 1054 | setBackgroundAlpha(finalAlpha); |
| 1055 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | public void setBackgroundAlpha(float alpha) { |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1059 | if (alpha != mBackgroundAlpha) { |
| 1060 | mBackgroundAlpha = alpha; |
| 1061 | invalidate(); |
| 1062 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1065 | public float getBackgroundAlpha() { |
| 1066 | return mBackgroundAlpha; |
| 1067 | } |
| 1068 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 1069 | /** |
| 1070 | * Due to 3D transformations, if two CellLayouts are theoretically touching each other, |
| 1071 | * on the xy plane, when one is rotated along the y-axis, the gap between them is perceived |
| 1072 | * as being larger. This method computes what offset the rotated view should be translated |
| 1073 | * in order to minimize this perceived gap. |
| 1074 | * @param degrees Angle of the view |
| 1075 | * @param width Width of the view |
| 1076 | * @param height Height of the view |
| 1077 | * @return Offset to be used in a View.setTranslationX() call |
| 1078 | */ |
| 1079 | private float getOffsetXForRotation(float degrees, int width, int height) { |
| 1080 | mMatrix.reset(); |
| 1081 | mCamera.save(); |
| 1082 | mCamera.rotateY(Math.abs(degrees)); |
| 1083 | mCamera.getMatrix(mMatrix); |
| 1084 | mCamera.restore(); |
| 1085 | |
| 1086 | mMatrix.preTranslate(-width * 0.5f, -height * 0.5f); |
| 1087 | mMatrix.postTranslate(width * 0.5f, height * 0.5f); |
| 1088 | mTempFloat2[0] = width; |
| 1089 | mTempFloat2[1] = height; |
| 1090 | mMatrix.mapPoints(mTempFloat2); |
| 1091 | return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f); |
| 1092 | } |
| 1093 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1094 | float backgroundAlphaInterpolator(float r) { |
| 1095 | float pivotA = 0.1f; |
| 1096 | float pivotB = 0.4f; |
| 1097 | if (r < pivotA) { |
| 1098 | return 0; |
| 1099 | } else if (r > pivotB) { |
| 1100 | return 1.0f; |
| 1101 | } else { |
| 1102 | return (r - pivotA)/(pivotB - pivotA); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | float overScrollBackgroundAlphaInterpolator(float r) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1107 | float threshold = 0.08f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1108 | |
| 1109 | if (r > mOverScrollMaxBackgroundAlpha) { |
| 1110 | mOverScrollMaxBackgroundAlpha = r; |
| 1111 | } else if (r < mOverScrollMaxBackgroundAlpha) { |
| 1112 | r = mOverScrollMaxBackgroundAlpha; |
| 1113 | } |
| 1114 | |
| 1115 | return Math.min(r / threshold, 1.0f); |
| 1116 | } |
| 1117 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1118 | @Override |
| 1119 | protected void screenScrolled(int screenCenter) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1120 | super.screenScrolled(screenCenter); |
| 1121 | |
Adam Cohen | c3bc260 | 2011-03-30 11:49:12 -0700 | [diff] [blame] | 1122 | // If the screen is not xlarge, then don't rotate the CellLayouts |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1123 | // NOTE: If we don't update the side pages alpha, then we should not hide the side pages. |
| 1124 | // see unshrink(). |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 1125 | if (!LauncherApplication.isScreenLarge()) return; |
Adam Cohen | c3bc260 | 2011-03-30 11:49:12 -0700 | [diff] [blame] | 1126 | |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 1127 | final int halfScreenSize = getMeasuredWidth() / 2; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1128 | |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 1129 | for (int i = 0; i < getChildCount(); i++) { |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1130 | CellLayout cl = (CellLayout) getChildAt(i); |
| 1131 | if (cl != null) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1132 | int totalDistance = getScaledMeasuredWidth(cl) + mPageSpacing; |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 1133 | int delta = screenCenter - (getChildOffset(i) - |
| 1134 | getRelativeChildOffset(i) + halfScreenSize); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1135 | |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 1136 | float scrollProgress = delta / (totalDistance * 1.0f); |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 1137 | scrollProgress = Math.min(scrollProgress, 1.0f); |
| 1138 | scrollProgress = Math.max(scrollProgress, -1.0f); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1139 | |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 1140 | if (mState != State.SPRING_LOADED) { |
| 1141 | // If the current page (i) is being overscrolled, we use a different |
| 1142 | // set of rules for setting the background alpha multiplier. |
| 1143 | if ((mScrollX < 0 && i == 0) || (mScrollX > mMaxScrollX && |
| 1144 | i == getChildCount() -1 )) { |
| 1145 | cl.setBackgroundAlphaMultiplier( |
| 1146 | overScrollBackgroundAlphaInterpolator(Math.abs(scrollProgress))); |
| 1147 | mOverScrollPageIndex = i; |
| 1148 | } else if (mOverScrollPageIndex != i) { |
| 1149 | cl.setBackgroundAlphaMultiplier( |
| 1150 | backgroundAlphaInterpolator(Math.abs(scrollProgress))); |
| 1151 | } |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1152 | } |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1153 | |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 1154 | float rotation = WORKSPACE_ROTATION * scrollProgress; |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 1155 | float translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight()); |
| 1156 | cl.setTranslationX(translationX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1157 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1158 | cl.setRotationY(rotation); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1163 | protected void onAttachedToWindow() { |
| 1164 | super.onAttachedToWindow(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1165 | mWindowToken = getWindowToken(); |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 1166 | computeScroll(); |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1167 | mDragController.setWindowToken(mWindowToken); |
| 1168 | } |
| 1169 | |
| 1170 | protected void onDetachedFromWindow() { |
| 1171 | mWindowToken = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1174 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 1175 | 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] | 1176 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 1177 | mUpdateWallpaperOffsetImmediately = true; |
| 1178 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1179 | super.onLayout(changed, left, top, right, bottom); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1180 | |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 1181 | // if shrinkToBottom() is called on initialization, it has to be deferred |
| 1182 | // until after the first call to onLayout so that it has the correct width |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1183 | if (mSwitchStateAfterFirstLayout) { |
| 1184 | mSwitchStateAfterFirstLayout = false; |
Adam Cohen | 696a679 | 2010-12-09 11:46:38 -0800 | [diff] [blame] | 1185 | // shrink can trigger a synchronous onLayout call, so we |
| 1186 | // post this to avoid a stack overflow / tangled onLayout calls |
| 1187 | post(new Runnable() { |
| 1188 | public void run() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1189 | shrink(mStateAfterFirstLayout, false); |
Adam Cohen | 696a679 | 2010-12-09 11:46:38 -0800 | [diff] [blame] | 1190 | } |
| 1191 | }); |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 1192 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | @Override |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1196 | protected void onDraw(Canvas canvas) { |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1197 | updateWallpaperOffsets(); |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1198 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1199 | // Draw the background gradient if necessary |
Michael Jurka | 9c6fbed | 2011-03-02 17:41:34 -0800 | [diff] [blame] | 1200 | if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) { |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 1201 | int alpha = (int) (mBackgroundAlpha * 255); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1202 | mBackground.setAlpha(alpha); |
| 1203 | mBackground.setBounds(mScrollX, 0, mScrollX + getMeasuredWidth(), |
| 1204 | getMeasuredHeight()); |
| 1205 | mBackground.draw(canvas); |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1206 | } |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 1207 | |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1208 | super.onDraw(canvas); |
| 1209 | } |
| 1210 | |
| 1211 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1212 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1213 | super.dispatchDraw(canvas); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 1214 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1215 | if (mInScrollArea && !LauncherApplication.isScreenLarge()) { |
| 1216 | final int width = getWidth(); |
| 1217 | final int height = getHeight(); |
| 1218 | final int pageHeight = getChildAt(0).getHeight(); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 1219 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1220 | // This determines the height of the glowing edge: 90% of the page height |
| 1221 | final int padding = (int) ((height - pageHeight) * 0.5f + pageHeight * 0.1f); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 1222 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1223 | final CellLayout leftPage = (CellLayout) getChildAt(mCurrentPage - 1); |
| 1224 | final CellLayout rightPage = (CellLayout) getChildAt(mCurrentPage + 1); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 1225 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1226 | if (leftPage != null && leftPage.getIsDragOverlapping()) { |
| 1227 | final Drawable d = getResources().getDrawable(R.drawable.page_hover_left_holo); |
| 1228 | d.setBounds(mScrollX, padding, mScrollX + d.getIntrinsicWidth(), height - padding); |
| 1229 | d.draw(canvas); |
| 1230 | } else if (rightPage != null && rightPage.getIsDragOverlapping()) { |
| 1231 | final Drawable d = getResources().getDrawable(R.drawable.page_hover_right_holo); |
| 1232 | d.setBounds(mScrollX + width - d.getIntrinsicWidth(), padding, mScrollX + width, height - padding); |
| 1233 | d.draw(canvas); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 1234 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1235 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | @Override |
| 1239 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1240 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1241 | final Folder openFolder = getOpenFolder(); |
| 1242 | if (openFolder != null) { |
| 1243 | return openFolder.requestFocus(direction, previouslyFocusedRect); |
| 1244 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1245 | return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | @Override |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1252 | public int getDescendantFocusability() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1253 | if (isSmall()) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1254 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 1255 | } |
| 1256 | return super.getDescendantFocusability(); |
| 1257 | } |
| 1258 | |
| 1259 | @Override |
Romain Guy | c2e24c0 | 2009-06-01 16:11:41 -0700 | [diff] [blame] | 1260 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 1261 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1262 | final Folder openFolder = getOpenFolder(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1263 | if (openFolder != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1264 | openFolder.addFocusables(views, direction); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1265 | } else { |
| 1266 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1271 | public boolean isSmall() { |
Adam Cohen | 3384c7b | 2011-07-17 15:48:21 -0700 | [diff] [blame] | 1272 | return mState == State.SMALL || mState == State.SPRING_LOADED; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1275 | void enableChildrenCache(int fromPage, int toPage) { |
| 1276 | if (fromPage > toPage) { |
| 1277 | final int temp = fromPage; |
| 1278 | fromPage = toPage; |
| 1279 | toPage = temp; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 1280 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1281 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1282 | final int screenCount = getChildCount(); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1283 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1284 | fromPage = Math.max(fromPage, 0); |
| 1285 | toPage = Math.min(toPage, screenCount - 1); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 1286 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1287 | for (int i = fromPage; i <= toPage; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1288 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1289 | layout.setChildrenDrawnWithCacheEnabled(true); |
| 1290 | layout.setChildrenDrawingCacheEnabled(true); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | void clearChildrenCache() { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1295 | final int screenCount = getChildCount(); |
| 1296 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1297 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1298 | layout.setChildrenDrawnWithCacheEnabled(false); |
Adam Cohen | 8182e5b | 2011-08-01 15:43:31 -0700 | [diff] [blame] | 1299 | // In software mode, we don't want the items to continue to be drawn into bitmaps |
| 1300 | if (!isHardwareAccelerated()) { |
| 1301 | layout.setChildrenDrawingCacheEnabled(false); |
| 1302 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1306 | private void updateChildrenLayersEnabled() { |
| 1307 | boolean small = |
| 1308 | isSmall() || mIsSwitchingState || mState == State.SPRING_LOADED; |
| 1309 | boolean dragging = mAnimatingViewIntoPlace || mIsDragOccuring; |
| 1310 | boolean enableChildrenLayers = small || dragging || isPageMoving(); |
| 1311 | |
| 1312 | if (enableChildrenLayers != mChildrenLayersEnabled) { |
| 1313 | mChildrenLayersEnabled = enableChildrenLayers; |
| 1314 | for (int i = 0; i < getPageCount(); i++) { |
| 1315 | ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(enableChildrenLayers); |
| 1316 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1317 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1320 | @Override |
| 1321 | protected void onWallpaperTap(MotionEvent ev) { |
| 1322 | final int[] position = mTempCell; |
| 1323 | getLocationOnScreen(position); |
| 1324 | |
| 1325 | int pointerIndex = ev.getActionIndex(); |
| 1326 | position[0] += (int) ev.getX(pointerIndex); |
| 1327 | position[1] += (int) ev.getY(pointerIndex); |
| 1328 | |
| 1329 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 1330 | ev.getAction() == MotionEvent.ACTION_UP |
| 1331 | ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, |
| 1332 | position[0], position[1], 0, null); |
| 1333 | } |
| 1334 | |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1335 | private float getYScaleForScreen(int screen) { |
| 1336 | int x = Math.abs(screen - 2); |
| 1337 | |
| 1338 | // TODO: This should be generalized for use with arbitrary rotation angles. |
| 1339 | switch(x) { |
| 1340 | case 0: return EXTRA_SCALE_FACTOR_0; |
| 1341 | case 1: return EXTRA_SCALE_FACTOR_1; |
| 1342 | case 2: return EXTRA_SCALE_FACTOR_2; |
| 1343 | } |
| 1344 | return 1.0f; |
| 1345 | } |
| 1346 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1347 | public void shrink(State shrinkState) { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1348 | shrink(shrinkState, true); |
| 1349 | } |
Winson Chung | 9171e6d | 2010-11-17 17:39:27 -0800 | [diff] [blame] | 1350 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1351 | // we use this to shrink the workspace for the all apps view and the customize view |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 1352 | public void shrink(final State shrinkState, boolean animated) { |
Michael Jurka | 883f55b | 2010-10-21 15:47:14 -0700 | [diff] [blame] | 1353 | if (mFirstLayout) { |
| 1354 | // (mFirstLayout == "first layout has not happened yet") |
| 1355 | // if we get a call to shrink() as part of our initialization (for example, if |
| 1356 | // Launcher is started in All Apps mode) then we need to wait for a layout call |
| 1357 | // to get our width so we can layout the mini-screen views correctly |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1358 | mSwitchStateAfterFirstLayout = true; |
| 1359 | mStateAfterFirstLayout = shrinkState; |
Michael Jurka | 883f55b | 2010-10-21 15:47:14 -0700 | [diff] [blame] | 1360 | return; |
| 1361 | } |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1362 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1363 | // Stop any scrolling, move to the current page right away |
Michael Jurka | a997ac2 | 2010-10-27 18:10:55 -0700 | [diff] [blame] | 1364 | setCurrentPage((mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1365 | |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1366 | CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); |
Patrick Dubroy | 0dfcf68 | 2011-01-18 17:08:11 -0800 | [diff] [blame] | 1367 | if (currentPage == null) { |
| 1368 | Log.w(TAG, "currentPage is NULL! mCurrentPage " + mCurrentPage |
| 1369 | + " mNextPage " + mNextPage); |
| 1370 | return; |
| 1371 | } |
Adam Cohen | ddb8219 | 2010-11-10 16:32:54 -0800 | [diff] [blame] | 1372 | if (currentPage.getBackgroundAlphaMultiplier() < 1.0f) { |
| 1373 | currentPage.setBackgroundAlpha(0.0f); |
| 1374 | } |
Adam Cohen | 1b0aaac | 2010-10-28 11:11:18 -0700 | [diff] [blame] | 1375 | currentPage.setBackgroundAlphaMultiplier(1.0f); |
| 1376 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1377 | mState = shrinkState; |
| 1378 | updateChildrenLayersEnabled(); |
Patrick Dubroy | 0dfcf68 | 2011-01-18 17:08:11 -0800 | [diff] [blame] | 1379 | |
| 1380 | // we intercept and reject all touch events when we're small, so be sure to reset the state |
| 1381 | mTouchState = TOUCH_STATE_REST; |
| 1382 | mActivePointerId = INVALID_POINTER; |
| 1383 | |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 1384 | final Resources res = getResources(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1385 | final int screenWidth = getWidth(); |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 1386 | final int screenHeight = getHeight(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1387 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1388 | // How much the workspace shrinks when we enter all apps or customization mode |
| 1389 | final float shrinkFactor = res.getInteger(R.integer.config_workspaceShrinkPercent) / 100.0f; |
| 1390 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1391 | // Making the assumption that all pages have the same width as the 0th |
| 1392 | final int pageWidth = getChildAt(0).getMeasuredWidth(); |
| 1393 | final int pageHeight = getChildAt(0).getMeasuredHeight(); |
| 1394 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1395 | final int scaledPageWidth = (int) (shrinkFactor * pageWidth); |
| 1396 | final int scaledPageHeight = (int) (shrinkFactor * pageHeight); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1397 | final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1398 | |
| 1399 | final int screenCount = getChildCount(); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1400 | float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing; |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 1401 | |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1402 | // We shrink and disappear to nothing |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 1403 | boolean isPortrait = getMeasuredHeight() > getMeasuredWidth(); |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1404 | float y = screenHeight - scaledPageHeight - (isPortrait ? |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 1405 | getResources().getDimension(R.dimen.allAppsSmallScreenVerticalMarginPortrait) : |
| 1406 | getResources().getDimension(R.dimen.allAppsSmallScreenVerticalMarginLandscape)); |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1407 | float finalAlpha = 0.0f; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1408 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1409 | int duration = res.getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime); |
Adam Cohen | f16e571 | 2011-01-13 13:31:45 -0800 | [diff] [blame] | 1410 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1411 | // We animate all the screens to the centered position in workspace |
| 1412 | // At the same time, the screens become greyed/dimmed |
| 1413 | |
| 1414 | // newX is initialized to the left-most position of the centered screens |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1415 | float x = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1416 | |
| 1417 | // We are going to scale about the center of the view, so we need to adjust the positions |
| 1418 | // of the views accordingly |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1419 | x -= (pageWidth - scaledPageWidth) / 2.0f; |
| 1420 | y -= (pageHeight - scaledPageHeight) / 2.0f; |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1421 | |
| 1422 | if (mAnimator != null) { |
| 1423 | mAnimator.cancel(); |
| 1424 | } |
Adam Cohen | f16e571 | 2011-01-13 13:31:45 -0800 | [diff] [blame] | 1425 | |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1426 | mAnimator = new AnimatorSet(); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 1427 | // Workaround the AnimatorSet cancel bug... |
| 1428 | mUnshrinkAnimationEnabled = false; |
| 1429 | mShrinkAnimationEnabled = true; |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1430 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1431 | initAnimationArrays(); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1432 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1433 | for (int i = 0; i < screenCount; i++) { |
Adam Cohen | f16e571 | 2011-01-13 13:31:45 -0800 | [diff] [blame] | 1434 | final CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1435 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1436 | float rotation = (-i + 2) * WORKSPACE_ROTATION; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1437 | float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f)); |
| 1438 | float rotationScaleY = getYScaleForScreen(i); |
| 1439 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1440 | mOldAlphas[i] = cl.getAlpha(); |
| 1441 | mNewAlphas[i] = finalAlpha; |
| 1442 | if (animated && (mOldAlphas[i] != 0f || mNewAlphas[i] != 0f)) { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 1443 | // if the CellLayout will be visible during the animation, force building its |
| 1444 | // hardware layer immediately so we don't see a blip later in the animation |
| 1445 | cl.buildChildrenLayer(); |
| 1446 | } |
Michael Jurka | 3e5ad58 | 2011-02-03 22:46:34 -0800 | [diff] [blame] | 1447 | if (animated) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1448 | mOldTranslationXs[i] = cl.getX(); |
| 1449 | mOldTranslationYs[i] = cl.getY(); |
| 1450 | mOldScaleXs[i] = cl.getScaleX(); |
| 1451 | mOldScaleYs[i] = cl.getScaleY(); |
| 1452 | mOldBackgroundAlphas[i] = cl.getBackgroundAlpha(); |
| 1453 | mOldRotationYs[i] = cl.getRotationY(); |
| 1454 | mNewTranslationXs[i] = x; |
| 1455 | mNewTranslationYs[i] = y; |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1456 | mNewScaleXs[i] = shrinkFactor * rotationScaleX; |
| 1457 | mNewScaleYs[i] = shrinkFactor * rotationScaleY; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1458 | mNewBackgroundAlphas[i] = finalAlpha; |
| 1459 | mNewRotationYs[i] = rotation; |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 1460 | } else { |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1461 | cl.setX((int)x); |
| 1462 | cl.setY((int)y); |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1463 | cl.setScaleX(shrinkFactor * rotationScaleX); |
| 1464 | cl.setScaleY(shrinkFactor * rotationScaleY); |
Michael Jurka | 883f55b | 2010-10-21 15:47:14 -0700 | [diff] [blame] | 1465 | cl.setBackgroundAlpha(finalAlpha); |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1466 | cl.setFastAlpha(finalAlpha); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 1467 | cl.setRotationY(rotation); |
Michael Jurka | 3e5ad58 | 2011-02-03 22:46:34 -0800 | [diff] [blame] | 1468 | mShrinkAnimationListener.onAnimationEnd(null); |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 1469 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1470 | // increment newX for the next screen |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1471 | x += scaledPageWidth + extraScaledSpacing; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1472 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1473 | |
| 1474 | float wallpaperOffset = 0.5f; |
| 1475 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
| 1476 | int wallpaperTravelHeight = (int) (display.getHeight() * |
| 1477 | wallpaperTravelToScreenHeightRatio(display.getWidth(), display.getHeight())); |
| 1478 | float offsetFromCenter = (wallpaperTravelHeight / (float) mWallpaperHeight) / 2f; |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1479 | boolean isLandscape = display.getWidth() > display.getHeight(); |
| 1480 | |
Michael Jurka | ff0e01b | 2011-07-08 17:08:51 -0700 | [diff] [blame] | 1481 | // on phones, don't scroll the wallpaper horizontally or vertically when switching |
| 1482 | // to/from all apps |
| 1483 | final boolean enableWallpaperEffects = |
| 1484 | isHardwareAccelerated() && LauncherApplication.isScreenLarge(); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1485 | if (enableWallpaperEffects) { |
| 1486 | switch (shrinkState) { |
| 1487 | // animating in |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1488 | case SPRING_LOADED: |
| 1489 | wallpaperOffset = 0.5f; |
| 1490 | mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.34f : 0.32f); |
| 1491 | break; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1492 | case SMALL: |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1493 | // allapps |
| 1494 | wallpaperOffset = 0.5f - offsetFromCenter; |
| 1495 | mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.34f : 0.32f); |
| 1496 | break; |
| 1497 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1500 | setLayoutScale(1.0f); |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1501 | if (animated) { |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1502 | if (enableWallpaperEffects) { |
| 1503 | mWallpaperOffset.setHorizontalCatchupConstant(0.46f); |
| 1504 | mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); |
| 1505 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1506 | |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1507 | mSyncWallpaperOffsetWithScroll = false; |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1508 | |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1509 | ValueAnimator animWithInterpolator = |
| 1510 | ValueAnimator.ofFloat(0f, 1f).setDuration(duration); |
| 1511 | animWithInterpolator.setInterpolator(mZoomOutInterpolator); |
| 1512 | |
| 1513 | final float oldHorizontalWallpaperOffset = getHorizontalWallpaperOffset(); |
| 1514 | final float oldVerticalWallpaperOffset = getVerticalWallpaperOffset(); |
| 1515 | final float newHorizontalWallpaperOffset = 0.5f; |
| 1516 | final float newVerticalWallpaperOffset = wallpaperOffset; |
Michael Jurka | c2e26a0 | 2011-03-24 15:20:26 -0700 | [diff] [blame] | 1517 | animWithInterpolator.addUpdateListener(new LauncherAnimatorUpdateListener() { |
| 1518 | public void onAnimationUpdate(float a, float b) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 1519 | if (!mShrinkAnimationEnabled) return; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1520 | mTransitionProgress = b; |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 1521 | if (b == 0f) { |
| 1522 | // an optimization, and required for correct behavior. |
| 1523 | return; |
| 1524 | } |
Michael Jurka | a71cb17 | 2011-07-10 15:04:55 -0700 | [diff] [blame] | 1525 | invalidate(); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1526 | if (enableWallpaperEffects) { |
| 1527 | setHorizontalWallpaperOffset( |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1528 | a * oldHorizontalWallpaperOffset + b * newHorizontalWallpaperOffset); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1529 | setVerticalWallpaperOffset( |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1530 | a * oldVerticalWallpaperOffset + b * newVerticalWallpaperOffset); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1531 | } |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1532 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1533 | final CellLayout cl = (CellLayout) getChildAt(i); |
| 1534 | cl.fastInvalidate(); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1535 | cl.setFastX(a * mOldTranslationXs[i] + b * mNewTranslationXs[i]); |
| 1536 | cl.setFastY(a * mOldTranslationYs[i] + b * mNewTranslationYs[i]); |
| 1537 | cl.setFastScaleX(a * mOldScaleXs[i] + b * mNewScaleXs[i]); |
| 1538 | cl.setFastScaleY(a * mOldScaleYs[i] + b * mNewScaleYs[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1539 | cl.setFastBackgroundAlpha( |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1540 | a * mOldBackgroundAlphas[i] + b * mNewBackgroundAlphas[i]); |
| 1541 | cl.setFastAlpha(a * mOldAlphas[i] + b * mNewAlphas[i]); |
| 1542 | cl.setFastRotationY(a * mOldRotationYs[i] + b * mNewRotationYs[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | }); |
| 1546 | mAnimator.playTogether(animWithInterpolator); |
Michael Jurka | bea1519 | 2010-11-17 12:33:46 -0800 | [diff] [blame] | 1547 | mAnimator.addListener(mShrinkAnimationListener); |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1548 | mAnimator.start(); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1549 | } else if (enableWallpaperEffects) { |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1550 | setVerticalWallpaperOffset(wallpaperOffset); |
| 1551 | setHorizontalWallpaperOffset(0.5f); |
| 1552 | updateWallpaperOffsetImmediately(); |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1553 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1554 | setChildrenDrawnWithCacheEnabled(true); |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 1555 | |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1556 | showBackgroundGradientForAllApps(animated); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Michael Jurka | c1a036b | 2011-07-25 17:04:23 -0700 | [diff] [blame] | 1559 | @Override |
| 1560 | protected void updateAdjacentPagesAlpha() { |
| 1561 | if (!isSmall()) { |
| 1562 | super.updateAdjacentPagesAlpha(); |
| 1563 | } |
| 1564 | } |
| 1565 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1566 | /* |
| 1567 | * This interpolator emulates the rate at which the perceived scale of an object changes |
| 1568 | * as its distance from a camera increases. When this interpolator is applied to a scale |
| 1569 | * animation on a view, it evokes the sense that the object is shrinking due to moving away |
| 1570 | * from the camera. |
| 1571 | */ |
| 1572 | static class ZInterpolator implements TimeInterpolator { |
| 1573 | private float focalLength; |
| 1574 | |
| 1575 | public ZInterpolator(float foc) { |
| 1576 | focalLength = foc; |
| 1577 | } |
| 1578 | |
Adam Cohen | eed565d | 2010-11-15 11:30:05 -0800 | [diff] [blame] | 1579 | public float getInterpolation(float input) { |
| 1580 | return (1.0f - focalLength / (focalLength + input)) / |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1581 | (1.0f - focalLength / (focalLength + 1.0f)); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1585 | /* |
| 1586 | * The exact reverse of ZInterpolator. |
| 1587 | */ |
| 1588 | static class InverseZInterpolator implements TimeInterpolator { |
| 1589 | private ZInterpolator zInterpolator; |
| 1590 | public InverseZInterpolator(float foc) { |
| 1591 | zInterpolator = new ZInterpolator(foc); |
| 1592 | } |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1593 | public float getInterpolation(float input) { |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1594 | return 1 - zInterpolator.getInterpolation(1 - input); |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1598 | /* |
| 1599 | * ZInterpolator compounded with an ease-out. |
| 1600 | */ |
| 1601 | static class ZoomOutInterpolator implements TimeInterpolator { |
| 1602 | private final ZInterpolator zInterpolator = new ZInterpolator(0.2f); |
Michael Jurka | ea57348 | 2011-02-03 19:48:18 -0800 | [diff] [blame] | 1603 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(1.8f); |
Adam Cohen | 61033d3 | 2010-11-15 18:29:44 -0800 | [diff] [blame] | 1604 | |
| 1605 | public float getInterpolation(float input) { |
| 1606 | return decelerate.getInterpolation(zInterpolator.getInterpolation(input)); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * InvereZInterpolator compounded with an ease-out. |
| 1612 | */ |
| 1613 | static class ZoomInInterpolator implements TimeInterpolator { |
| 1614 | private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f); |
| 1615 | private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f); |
| 1616 | |
| 1617 | public float getInterpolation(float input) { |
| 1618 | return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input)); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | private final ZoomOutInterpolator mZoomOutInterpolator = new ZoomOutInterpolator(); |
| 1623 | private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1624 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1625 | /* |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1626 | * |
| 1627 | * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we |
| 1628 | * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace |
| 1629 | * |
| 1630 | * These methods mark the appropriate pages as accepting drops (which alters their visual |
| 1631 | * appearance). |
| 1632 | * |
| 1633 | */ |
| 1634 | public void onDragStartedWithItem(View v) { |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1635 | final Canvas canvas = new Canvas(); |
| 1636 | |
| 1637 | // We need to add extra padding to the bitmap to make room for the glow effect |
| 1638 | final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
| 1639 | |
| 1640 | // The outline is used to visualize where the item will land if dropped |
| 1641 | mDragOutline = createDragOutline(v, canvas, bitmapPadding); |
| 1642 | } |
| 1643 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1644 | public void onDragStartedWithItemSpans(int spanX, int spanY, Bitmap b) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1645 | final Canvas canvas = new Canvas(); |
| 1646 | |
| 1647 | // We need to add extra padding to the bitmap to make room for the glow effect |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 1648 | final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1649 | |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 1650 | CellLayout cl = (CellLayout) getChildAt(0); |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1651 | |
| 1652 | int[] size = cl.cellSpansToSize(spanX, spanY); |
| 1653 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1654 | // The outline is used to visualize where the item will land if dropped |
Adam Cohen | 6639687 | 2011-04-15 17:50:36 -0700 | [diff] [blame] | 1655 | mDragOutline = createDragOutline(b, canvas, bitmapPadding, size[0], size[1]); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was |
| 1659 | // never dragged over |
Patrick Dubroy | 7bccb42 | 2011-01-20 14:50:55 -0800 | [diff] [blame] | 1660 | public void onDragStopped(boolean success) { |
| 1661 | // In the success case, DragController has already called onDragExit() |
| 1662 | if (!success) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 1663 | doDragExit(null); |
Patrick Dubroy | 7bccb42 | 2011-01-20 14:50:55 -0800 | [diff] [blame] | 1664 | } |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1667 | // We call this when we trigger an unshrink by clicking on the CellLayout cl |
| 1668 | public void unshrink(CellLayout clThatWasClicked) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1669 | unshrink(clThatWasClicked, false); |
| 1670 | } |
| 1671 | |
| 1672 | public void unshrink(CellLayout clThatWasClicked, boolean springLoaded) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1673 | int newCurrentPage = indexOfChild(clThatWasClicked); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1674 | if (isSmall()) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1675 | if (springLoaded) { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1676 | setLayoutScale(mSpringLoadedShrinkFactor); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1677 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1678 | scrollToNewPageWithoutMovingPages(newCurrentPage); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1679 | unshrink(true, springLoaded); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | public void enterSpringLoadedDragMode(CellLayout clThatWasClicked) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1685 | unshrink(clThatWasClicked, true); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1686 | } |
| 1687 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1688 | public void exitSpringLoadedDragMode(State shrinkState) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1689 | shrink(shrinkState); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 1692 | public void exitWidgetResizeMode() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 1693 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 1694 | dragLayer.clearAllResizeFrames(); |
Patrick Dubroy | 758a923 | 2011-03-03 19:54:56 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1697 | void unshrink(boolean animated) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1698 | unshrink(animated, false); |
| 1699 | } |
| 1700 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1701 | private void initAnimationArrays() { |
| 1702 | final int childCount = getChildCount(); |
| 1703 | if (mOldTranslationXs != null) return; |
| 1704 | mOldTranslationXs = new float[childCount]; |
| 1705 | mOldTranslationYs = new float[childCount]; |
| 1706 | mOldScaleXs = new float[childCount]; |
| 1707 | mOldScaleYs = new float[childCount]; |
| 1708 | mOldBackgroundAlphas = new float[childCount]; |
| 1709 | mOldBackgroundAlphaMultipliers = new float[childCount]; |
| 1710 | mOldAlphas = new float[childCount]; |
| 1711 | mOldRotationYs = new float[childCount]; |
| 1712 | mNewTranslationXs = new float[childCount]; |
| 1713 | mNewTranslationYs = new float[childCount]; |
| 1714 | mNewScaleXs = new float[childCount]; |
| 1715 | mNewScaleYs = new float[childCount]; |
| 1716 | mNewBackgroundAlphas = new float[childCount]; |
| 1717 | mNewBackgroundAlphaMultipliers = new float[childCount]; |
| 1718 | mNewAlphas = new float[childCount]; |
| 1719 | mNewRotationYs = new float[childCount]; |
| 1720 | } |
| 1721 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 1722 | void unshrink(boolean animated, final boolean springLoaded) { |
Michael Jurka | 99633da | 2011-07-27 22:40:17 -0700 | [diff] [blame] | 1723 | if (mFirstLayout) { |
| 1724 | // (mFirstLayout == "first layout has not happened yet") |
| 1725 | // cancel any pending shrinks that were set earlier |
| 1726 | mSwitchStateAfterFirstLayout = false; |
| 1727 | mStateAfterFirstLayout = State.NORMAL; |
| 1728 | return; |
| 1729 | } |
| 1730 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1731 | if (isSmall()) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1732 | float finalScaleFactor = 1.0f; |
| 1733 | float finalBackgroundAlpha = 0.0f; |
| 1734 | if (springLoaded) { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1735 | finalScaleFactor = mSpringLoadedShrinkFactor; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1736 | finalBackgroundAlpha = 1.0f; |
Adam Cohen | 3384c7b | 2011-07-17 15:48:21 -0700 | [diff] [blame] | 1737 | mState = State.SPRING_LOADED; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1738 | } else { |
Adam Cohen | 3384c7b | 2011-07-17 15:48:21 -0700 | [diff] [blame] | 1739 | mState = State.NORMAL; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1740 | } |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1741 | if (mAnimator != null) { |
| 1742 | mAnimator.cancel(); |
| 1743 | } |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 1744 | |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1745 | mAnimator = new AnimatorSet(); |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 1746 | |
| 1747 | // Workaround the AnimatorSet cancel bug... |
| 1748 | mShrinkAnimationEnabled = false; |
| 1749 | mUnshrinkAnimationEnabled = true; |
| 1750 | |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 1751 | final int screenCount = getChildCount(); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1752 | initAnimationArrays(); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1753 | |
| 1754 | final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 1755 | for (int i = 0; i < screenCount; i++) { |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 1756 | final CellLayout cl = (CellLayout)getChildAt(i); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1757 | float finalAlphaValue = 0f; |
| 1758 | float rotation = 0f; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1759 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 1760 | // Set the final alpha depending on whether we are fading side pages. On phone ui, |
| 1761 | // we don't do any of the rotation, or the fading alpha in portrait. See the |
| 1762 | // ctor and screenScrolled(). |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 1763 | if (mFadeInAdjacentScreens && !springLoaded) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 1764 | finalAlphaValue = (i == mCurrentPage) ? 1f : 0f; |
| 1765 | } else { |
| 1766 | finalAlphaValue = 1f; |
| 1767 | } |
| 1768 | |
| 1769 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1770 | if (i < mCurrentPage) { |
| 1771 | rotation = WORKSPACE_ROTATION; |
| 1772 | } else if (i > mCurrentPage) { |
| 1773 | rotation = -WORKSPACE_ROTATION; |
| 1774 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1775 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1776 | float finalAlphaMultiplierValue = 1f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1777 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1778 | float translation = 0f; |
| 1779 | |
| 1780 | // If the screen is not xlarge, then don't rotate the CellLayouts |
| 1781 | // NOTE: If we don't update the side pages alpha, then we should not hide the side |
| 1782 | // pages. see unshrink(). |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 1783 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1784 | translation = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight()); |
| 1785 | } |
Adam Cohen | cbbaf98 | 2010-11-12 14:50:33 -0800 | [diff] [blame] | 1786 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1787 | mOldAlphas[i] = cl.getAlpha(); |
| 1788 | mNewAlphas[i] = finalAlphaValue; |
Michael Jurka | 3e5ad58 | 2011-02-03 22:46:34 -0800 | [diff] [blame] | 1789 | if (animated) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1790 | mOldTranslationXs[i] = cl.getTranslationX(); |
| 1791 | mOldTranslationYs[i] = cl.getTranslationY(); |
| 1792 | mOldScaleXs[i] = cl.getScaleX(); |
| 1793 | mOldScaleYs[i] = cl.getScaleY(); |
| 1794 | mOldBackgroundAlphas[i] = cl.getBackgroundAlpha(); |
| 1795 | mOldBackgroundAlphaMultipliers[i] = cl.getBackgroundAlphaMultiplier(); |
| 1796 | mOldRotationYs[i] = cl.getRotationY(); |
Adam Cohen | 4904f66 | 2011-01-17 16:50:28 -0800 | [diff] [blame] | 1797 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1798 | mNewTranslationXs[i] = translation; |
| 1799 | mNewTranslationYs[i] = 0f; |
| 1800 | mNewScaleXs[i] = finalScaleFactor; |
| 1801 | mNewScaleYs[i] = finalScaleFactor; |
| 1802 | mNewBackgroundAlphas[i] = finalBackgroundAlpha; |
| 1803 | mNewBackgroundAlphaMultipliers[i] = finalAlphaMultiplierValue; |
| 1804 | mNewRotationYs[i] = rotation; |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1805 | } else { |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 1806 | cl.setTranslationX(translation); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1807 | cl.setTranslationY(0.0f); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1808 | cl.setScaleX(finalScaleFactor); |
| 1809 | cl.setScaleY(finalScaleFactor); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1810 | cl.setBackgroundAlpha(0.0f); |
Winson Chung | ae222ab | 2011-01-10 11:45:27 -0800 | [diff] [blame] | 1811 | cl.setBackgroundAlphaMultiplier(finalAlphaMultiplierValue); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1812 | cl.setAlpha(finalAlphaValue); |
| 1813 | cl.setRotationY(rotation); |
Michael Jurka | 3e5ad58 | 2011-02-03 22:46:34 -0800 | [diff] [blame] | 1814 | mUnshrinkAnimationListener.onAnimationEnd(null); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1815 | } |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 1816 | } |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 1817 | |
| 1818 | // Unshrink the hotset the same amount we are unshrinking the screens |
| 1819 | if (mLauncher.getHotseat() != null) { |
| 1820 | View hotseat = mLauncher.getHotseat().getLayout(); |
| 1821 | hotseat.setScaleX(finalScaleFactor); |
| 1822 | hotseat.setScaleY(finalScaleFactor); |
| 1823 | } |
| 1824 | |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1825 | Display display = mLauncher.getWindowManager().getDefaultDisplay(); |
| 1826 | boolean isLandscape = display.getWidth() > display.getHeight(); |
Michael Jurka | ff0e01b | 2011-07-08 17:08:51 -0700 | [diff] [blame] | 1827 | // on phones, don't scroll the wallpaper horizontally or vertically when switching |
| 1828 | // to/from all apps |
| 1829 | final boolean enableWallpaperEffects = |
| 1830 | isHardwareAccelerated() && LauncherApplication.isScreenLarge(); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1831 | if (enableWallpaperEffects) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1832 | switch (mState) { |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1833 | // animating out |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1834 | case SPRING_LOADED: |
| 1835 | if (animated) { |
| 1836 | mWallpaperOffset.setHorizontalCatchupConstant(isLandscape ? 0.49f : 0.46f); |
| 1837 | mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.49f : 0.46f); |
| 1838 | mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); |
| 1839 | } |
| 1840 | break; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 1841 | case SMALL: |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1842 | // all apps |
| 1843 | if (animated) { |
| 1844 | mWallpaperOffset.setHorizontalCatchupConstant(isLandscape ? 0.65f : 0.65f); |
| 1845 | mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.65f : 0.65f); |
| 1846 | mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); |
| 1847 | } |
| 1848 | break; |
| 1849 | } |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1850 | } |
Michael Jurka | ab1983f | 2011-01-18 15:50:17 -0800 | [diff] [blame] | 1851 | if (animated) { |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1852 | ValueAnimator animWithInterpolator = |
| 1853 | ValueAnimator.ofFloat(0f, 1f).setDuration(duration); |
| 1854 | animWithInterpolator.setInterpolator(mZoomInInterpolator); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 1855 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1856 | final float oldHorizontalWallpaperOffset = enableWallpaperEffects ? |
| 1857 | getHorizontalWallpaperOffset() : 0; |
| 1858 | final float oldVerticalWallpaperOffset = enableWallpaperEffects ? |
| 1859 | getVerticalWallpaperOffset() : 0; |
| 1860 | final float newHorizontalWallpaperOffset = enableWallpaperEffects ? |
| 1861 | wallpaperOffsetForCurrentScroll() : 0; |
| 1862 | final float newVerticalWallpaperOffset = enableWallpaperEffects ? 0.5f : 0; |
Michael Jurka | c2e26a0 | 2011-03-24 15:20:26 -0700 | [diff] [blame] | 1863 | animWithInterpolator.addUpdateListener(new LauncherAnimatorUpdateListener() { |
| 1864 | public void onAnimationUpdate(float a, float b) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 1865 | if (!mUnshrinkAnimationEnabled) return; |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1866 | mTransitionProgress = b; |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 1867 | if (b == 0f) { |
| 1868 | // an optimization, but not required |
| 1869 | return; |
| 1870 | } |
Michael Jurka | 280969c | 2011-07-11 11:26:10 -0700 | [diff] [blame] | 1871 | invalidate(); |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1872 | if (enableWallpaperEffects) { |
| 1873 | setHorizontalWallpaperOffset(a * oldHorizontalWallpaperOffset |
| 1874 | + b * newHorizontalWallpaperOffset); |
| 1875 | setVerticalWallpaperOffset(a * oldVerticalWallpaperOffset |
| 1876 | + b * newVerticalWallpaperOffset); |
| 1877 | } |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1878 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1879 | final CellLayout cl = (CellLayout) getChildAt(i); |
| 1880 | cl.fastInvalidate(); |
| 1881 | cl.setFastTranslationX( |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1882 | a * mOldTranslationXs[i] + b * mNewTranslationXs[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1883 | cl.setFastTranslationY( |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1884 | a * mOldTranslationYs[i] + b * mNewTranslationYs[i]); |
| 1885 | cl.setFastScaleX(a * mOldScaleXs[i] + b * mNewScaleXs[i]); |
| 1886 | cl.setFastScaleY(a * mOldScaleYs[i] + b * mNewScaleYs[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1887 | cl.setFastBackgroundAlpha( |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1888 | a * mOldBackgroundAlphas[i] + b * mNewBackgroundAlphas[i]); |
| 1889 | cl.setBackgroundAlphaMultiplier(a * mOldBackgroundAlphaMultipliers[i] + |
| 1890 | b * mNewBackgroundAlphaMultipliers[i]); |
| 1891 | cl.setFastAlpha(a * mOldAlphas[i] + b * mNewAlphas[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | }); |
| 1895 | |
| 1896 | ValueAnimator rotationAnim = |
| 1897 | ValueAnimator.ofFloat(0f, 1f).setDuration(duration); |
| 1898 | rotationAnim.setInterpolator(new DecelerateInterpolator(2.0f)); |
Michael Jurka | c2e26a0 | 2011-03-24 15:20:26 -0700 | [diff] [blame] | 1899 | rotationAnim.addUpdateListener(new LauncherAnimatorUpdateListener() { |
| 1900 | public void onAnimationUpdate(float a, float b) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 1901 | if (!mUnshrinkAnimationEnabled) return; |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1902 | // don't invalidate workspace because we did it above |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 1903 | if (b == 0f) { |
| 1904 | // an optimization, but not required |
| 1905 | return; |
| 1906 | } |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1907 | for (int i = 0; i < screenCount; i++) { |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1908 | final CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 1909 | cl.setFastRotationY(a * mOldRotationYs[i] + b * mNewRotationYs[i]); |
Michael Jurka | 742574b | 2011-02-02 23:51:01 -0800 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | }); |
| 1913 | |
| 1914 | mAnimator.playTogether(animWithInterpolator, rotationAnim); |
Michael Jurka | c86756c | 2010-10-10 15:15:47 -0700 | [diff] [blame] | 1915 | // If we call this when we're not animated, onAnimationEnd is never called on |
| 1916 | // the listener; make sure we only use the listener when we're actually animating |
Michael Jurka | 3c4c20f | 2010-10-28 15:36:06 -0700 | [diff] [blame] | 1917 | mAnimator.addListener(mUnshrinkAnimationListener); |
| 1918 | mAnimator.start(); |
Michael Jurka | ca5b836 | 2011-01-27 13:23:26 -0800 | [diff] [blame] | 1919 | } else { |
Winson Chung | b46a2d1 | 2011-04-22 14:13:05 -0700 | [diff] [blame] | 1920 | if (enableWallpaperEffects) { |
| 1921 | setHorizontalWallpaperOffset(wallpaperOffsetForCurrentScroll()); |
| 1922 | setVerticalWallpaperOffset(0.5f); |
| 1923 | updateWallpaperOffsetImmediately(); |
| 1924 | } |
Michael Jurka | c86756c | 2010-10-10 15:15:47 -0700 | [diff] [blame] | 1925 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1926 | } |
Winson Chung | 8d006d5 | 2010-11-29 15:55:29 -0800 | [diff] [blame] | 1927 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 1928 | hideBackgroundGradient(springLoaded ? getResources().getInteger( |
Michael Jurka | b9e1497 | 2011-07-25 17:57:40 -0700 | [diff] [blame] | 1929 | R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f : 0f, animated); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1932 | /** |
| 1933 | * Draw the View v into the given Canvas. |
| 1934 | * |
| 1935 | * @param v the view to draw |
| 1936 | * @param destCanvas the canvas to draw on |
| 1937 | * @param padding the horizontal and vertical padding to use when drawing |
| 1938 | */ |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1939 | private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) { |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1940 | final Rect clipRect = mTempRect; |
| 1941 | v.getDrawingRect(clipRect); |
| 1942 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1943 | destCanvas.save(); |
| 1944 | if (v instanceof TextView && pruneToDrawable) { |
| 1945 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
| 1946 | clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding); |
| 1947 | destCanvas.translate(padding / 2, padding / 2); |
| 1948 | d.draw(destCanvas); |
| 1949 | } else { |
| 1950 | if (v instanceof FolderIcon) { |
| 1951 | clipRect.bottom = getResources().getDimensionPixelSize(R.dimen.folder_preview_size); |
| 1952 | } else if (v instanceof BubbleTextView) { |
| 1953 | final BubbleTextView tv = (BubbleTextView) v; |
| 1954 | clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + |
| 1955 | tv.getLayout().getLineTop(0); |
| 1956 | } else if (v instanceof TextView) { |
| 1957 | final TextView tv = (TextView) v; |
| 1958 | clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() + |
| 1959 | tv.getLayout().getLineTop(0); |
| 1960 | } |
| 1961 | destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); |
| 1962 | destCanvas.clipRect(clipRect, Op.REPLACE); |
| 1963 | v.draw(destCanvas); |
| 1964 | } |
| 1965 | destCanvas.restore(); |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * Returns a new bitmap to show when the given View is being dragged around. |
| 1970 | * Responsibility for the bitmap is transferred to the caller. |
| 1971 | */ |
| 1972 | public Bitmap createDragBitmap(View v, Canvas canvas, int padding) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 1973 | final int outlineColor = getResources().getColor(android.R.color.holo_blue_light); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1974 | Bitmap b; |
| 1975 | |
| 1976 | if (v instanceof TextView) { |
| 1977 | Drawable d = ((TextView) v).getCompoundDrawables()[1]; |
| 1978 | b = Bitmap.createBitmap(d.getIntrinsicWidth() + padding, |
| 1979 | d.getIntrinsicHeight() + padding, Bitmap.Config.ARGB_8888); |
| 1980 | } else { |
| 1981 | b = Bitmap.createBitmap( |
| 1982 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1985 | canvas.setBitmap(b); |
| 1986 | drawDragView(v, canvas, padding, true); |
| 1987 | mOutlineHelper.applyOuterBlur(b, canvas, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 1988 | canvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1989 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 1990 | return b; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 1995 | * Responsibility for the bitmap is transferred to the caller. |
| 1996 | */ |
| 1997 | private Bitmap createDragOutline(View v, Canvas canvas, int padding) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 1998 | final int outlineColor = getResources().getColor(android.R.color.holo_blue_light); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 1999 | final Bitmap b = Bitmap.createBitmap( |
| 2000 | v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); |
| 2001 | |
| 2002 | canvas.setBitmap(b); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2003 | drawDragView(v, canvas, padding, false); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 2004 | mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2005 | canvas.setBitmap(null); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2006 | return b; |
| 2007 | } |
| 2008 | |
| 2009 | /** |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2010 | * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. |
| 2011 | * Responsibility for the bitmap is transferred to the caller. |
| 2012 | */ |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2013 | private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 2014 | final int outlineColor = getResources().getColor(android.R.color.holo_blue_light); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2015 | final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2016 | canvas.setBitmap(b); |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2017 | |
| 2018 | Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); |
| 2019 | float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), |
| 2020 | (h - padding) / (float) orig.getHeight()); |
| 2021 | int scaledWidth = (int) (scaleFactor * orig.getWidth()); |
| 2022 | int scaledHeight = (int) (scaleFactor * orig.getHeight()); |
| 2023 | Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); |
| 2024 | |
| 2025 | // center the image |
| 2026 | dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); |
| 2027 | |
| 2028 | Paint p = new Paint(); |
| 2029 | p.setFilterBitmap(true); |
| 2030 | canvas.drawBitmap(orig, src, dst, p); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2031 | mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2032 | canvas.setBitmap(null); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2033 | |
| 2034 | return b; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2038 | * Creates a drag outline to represent a drop (that we don't have the actual information for |
| 2039 | * yet). May be changed in the future to alter the drop outline slightly depending on the |
| 2040 | * clip description mime data. |
| 2041 | */ |
| 2042 | private Bitmap createExternalDragOutline(Canvas canvas, int padding) { |
| 2043 | Resources r = getResources(); |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 2044 | final int outlineColor = r.getColor(android.R.color.holo_blue_light); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2045 | final int iconWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width); |
| 2046 | final int iconHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height); |
| 2047 | final int rectRadius = r.getDimensionPixelSize(R.dimen.external_drop_icon_rect_radius); |
| 2048 | final int inset = (int) (Math.min(iconWidth, iconHeight) * 0.2f); |
| 2049 | final Bitmap b = Bitmap.createBitmap( |
| 2050 | iconWidth + padding, iconHeight + padding, Bitmap.Config.ARGB_8888); |
| 2051 | |
| 2052 | canvas.setBitmap(b); |
| 2053 | canvas.drawRoundRect(new RectF(inset, inset, iconWidth - inset, iconHeight - inset), |
| 2054 | rectRadius, rectRadius, mExternalDragOutlinePaint); |
Adam Cohen | 5bb50bd | 2010-12-03 11:39:55 -0800 | [diff] [blame] | 2055 | mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 2056 | canvas.setBitmap(null); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2057 | return b; |
| 2058 | } |
| 2059 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2060 | void startDrag(CellLayout.CellInfo cellInfo) { |
| 2061 | View child = cellInfo.cell; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2062 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2063 | // 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] | 2064 | if (!child.isInTouchMode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2065 | return; |
| 2066 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 2067 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2068 | mDragInfo = cellInfo; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2069 | child.setVisibility(GONE); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2070 | |
| 2071 | child.clearFocus(); |
| 2072 | child.setPressed(false); |
| 2073 | |
| 2074 | final Canvas canvas = new Canvas(); |
| 2075 | |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 2076 | // We need to add extra padding to the bitmap to make room for the glow effect |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 2077 | final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 2078 | |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2079 | // The outline is used to visualize where the item will land if dropped |
Patrick Dubroy | 8e58e91 | 2010-10-14 13:21:48 -0700 | [diff] [blame] | 2080 | mDragOutline = createDragOutline(child, canvas, bitmapPadding); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2081 | beginDragShared(child, this); |
| 2082 | } |
| 2083 | |
| 2084 | public void beginDragShared(View child, DragSource source) { |
| 2085 | // We need to add extra padding to the bitmap to make room for the glow effect |
| 2086 | final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2087 | |
| 2088 | // The drag bitmap follows the touch point around on the screen |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2089 | final Bitmap b = createDragBitmap(child, new Canvas(), bitmapPadding); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2090 | |
| 2091 | final int bmpWidth = b.getWidth(); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2092 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 2093 | mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); |
| 2094 | final int dragLayerX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2095 | int dragLayerY = mTempXY[1] - bitmapPadding / 2; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2096 | |
| 2097 | Rect dragRect = null; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 2098 | if (child instanceof BubbleTextView) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2099 | int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size); |
| 2100 | int top = child.getPaddingTop(); |
| 2101 | int left = (bmpWidth - iconSize) / 2; |
| 2102 | int right = left + iconSize; |
| 2103 | int bottom = top + iconSize; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2104 | dragLayerY += top; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2105 | dragRect = new Rect(left, top, right, bottom); |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 2106 | } else if (child instanceof FolderIcon) { |
| 2107 | int previewSize = getResources().getDimensionPixelSize(R.dimen.folder_preview_size); |
| 2108 | dragRect = new Rect(0, 0, child.getWidth(), previewSize); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2111 | mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2112 | DragController.DRAG_ACTION_MOVE, dragRect); |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 2113 | b.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2114 | } |
| 2115 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2116 | void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, int screen, |
| 2117 | int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) { |
| 2118 | View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2119 | |
| 2120 | final int[] cellXY = new int[2]; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2121 | target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY); |
| 2122 | addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst); |
| 2123 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, cellXY[0], |
| 2124 | cellXY[1]); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2127 | public boolean transitionStateShouldAllowDrop() { |
| 2128 | return (!isSwitchingState() || mTransitionProgress > 0.5f); |
| 2129 | } |
| 2130 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2131 | /** |
| 2132 | * {@inheritDoc} |
| 2133 | */ |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2134 | public boolean acceptDrop(DragObject d) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2135 | // If it's an external drop (e.g. from All Apps), check if it should be accepted |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2136 | if (d.dragSource != this) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2137 | // Don't accept the drop if we're not over a screen at time of drop |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2138 | if (mDragTargetLayout == null) { |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2139 | return false; |
| 2140 | } |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 2141 | if (!transitionStateShouldAllowDrop()) return false; |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2142 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2143 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
| 2144 | d.dragView, mDragViewVisualCenter); |
| 2145 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2146 | int spanX = 1; |
| 2147 | int spanY = 1; |
| 2148 | View ignoreView = null; |
| 2149 | if (mDragInfo != null) { |
| 2150 | final CellLayout.CellInfo dragCellInfo = mDragInfo; |
| 2151 | spanX = dragCellInfo.spanX; |
| 2152 | spanY = dragCellInfo.spanY; |
| 2153 | ignoreView = dragCellInfo.cell; |
| 2154 | } else { |
| 2155 | final ItemInfo dragInfo = (ItemInfo) d.dragInfo; |
| 2156 | spanX = dragInfo.spanX; |
| 2157 | spanY = dragInfo.spanY; |
| 2158 | } |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2159 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2160 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
| 2161 | (int) mDragViewVisualCenter[1], spanX, spanY, mDragTargetLayout, mTargetCell); |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2162 | if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell, true)) { |
| 2163 | return true; |
| 2164 | } |
| 2165 | if (willAddToExistingUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, |
| 2166 | mTargetCell)) { |
| 2167 | return true; |
| 2168 | } |
| 2169 | |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2170 | |
| 2171 | // Don't accept the drop if there's no room for the item |
| 2172 | if (!mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) { |
| 2173 | mLauncher.showOutOfSpaceMessage(); |
| 2174 | return false; |
| 2175 | } |
| 2176 | } |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2180 | boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, |
| 2181 | boolean considerTimeout) { |
| 2182 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
| 2183 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2184 | boolean hasntMoved = false; |
| 2185 | if (mDragInfo != null) { |
| 2186 | CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); |
| 2187 | hasntMoved = (mDragInfo.cellX == targetCell[0] && |
| 2188 | mDragInfo.cellY == targetCell[1]) && (cellParent == target); |
| 2189 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2190 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2191 | if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { |
| 2192 | return false; |
| 2193 | } |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2194 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2195 | boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2196 | boolean willBecomeShortcut = |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2197 | (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 2198 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT); |
Adam Cohen | e031096 | 2011-04-18 16:15:31 -0700 | [diff] [blame] | 2199 | |
| 2200 | return (aboveShortcut && willBecomeShortcut); |
| 2201 | } |
| 2202 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2203 | boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell) { |
| 2204 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
| 2205 | if (dropOverView instanceof FolderIcon) { |
| 2206 | FolderIcon fi = (FolderIcon) dropOverView; |
| 2207 | if (fi.acceptDrop(dragInfo)) { |
| 2208 | return true; |
| 2209 | } |
| 2210 | } |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2214 | boolean createUserFolderIfNecessary(View newView, long container, CellLayout target, |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2215 | int[] targetCell, boolean external, DragView dragView, Runnable postAnimationRunnable) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2216 | View v = target.getChildAt(targetCell[0], targetCell[1]); |
Winson Chung | ec9a0a4 | 2011-07-20 20:42:13 -0700 | [diff] [blame] | 2217 | boolean hasntMoved = false; |
| 2218 | if (mDragInfo != null) { |
| 2219 | CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); |
| 2220 | hasntMoved = (mDragInfo.cellX == targetCell[0] && |
| 2221 | mDragInfo.cellY == targetCell[1]) && (cellParent == target); |
| 2222 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2223 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2224 | if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; |
| 2225 | mCreateUserFolderOnDrop = false; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2226 | final int screen = (targetCell == null) ? mDragInfo.screen : indexOfChild(target); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2227 | |
| 2228 | boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); |
| 2229 | boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); |
| 2230 | |
| 2231 | if (aboveShortcut && willBecomeShortcut) { |
| 2232 | ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); |
| 2233 | ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); |
| 2234 | // if the drag started here, we need to remove it from the workspace |
| 2235 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2236 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2239 | Rect folderLocation = new Rect(); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2240 | float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2241 | target.removeView(v); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2242 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2243 | FolderIcon fi = |
| 2244 | mLauncher.addFolder(target, container, screen, targetCell[0], targetCell[1]); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 2245 | destInfo.cellX = -1; |
| 2246 | destInfo.cellY = -1; |
| 2247 | sourceInfo.cellX = -1; |
| 2248 | sourceInfo.cellY = -1; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 2249 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 2250 | fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale, |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2251 | postAnimationRunnable); |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 2252 | return true; |
| 2253 | } |
| 2254 | return false; |
| 2255 | } |
| 2256 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2257 | boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2258 | DragObject d, boolean external) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 2259 | View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2260 | if (dropOverView instanceof FolderIcon) { |
| 2261 | FolderIcon fi = (FolderIcon) dropOverView; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2262 | if (fi.acceptDrop(d.dragInfo)) { |
| 2263 | fi.onDrop(d); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2264 | |
| 2265 | // if the drag started here, we need to remove it from the workspace |
| 2266 | if (!external) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2267 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2268 | } |
| 2269 | return true; |
| 2270 | } |
| 2271 | } |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2275 | public void onDrop(DragObject d) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2276 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2277 | mDragViewVisualCenter); |
| 2278 | |
| 2279 | // We want the point to be mapped to the dragTarget. |
Adam Cohen | ba78161 | 2011-05-09 14:37:39 -0700 | [diff] [blame] | 2280 | if (mDragTargetLayout != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2281 | if (mLauncher.isHotseatLayout(mDragTargetLayout)) { |
| 2282 | mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter); |
| 2283 | } else { |
| 2284 | mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); |
| 2285 | } |
Adam Cohen | ba78161 | 2011-05-09 14:37:39 -0700 | [diff] [blame] | 2286 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 2287 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 2288 | CellLayout dropTargetLayout = mDragTargetLayout; |
Michael Jurka | b09cac5 | 2011-01-05 20:23:39 -0800 | [diff] [blame] | 2289 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 2290 | int snapScreen = -1; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2291 | if (d.dragSource != this) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2292 | final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], |
| 2293 | (int) mDragViewVisualCenter[1] }; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2294 | onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2295 | } else if (mDragInfo != null) { |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2296 | final View cell = mDragInfo.cell; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 2297 | |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 2298 | if (dropTargetLayout != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2299 | // Move internally |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 2300 | boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout); |
| 2301 | boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout); |
| 2302 | long container = hasMovedIntoHotseat ? |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2303 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 2304 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
| 2305 | int screen = (mTargetCell[0] < 0) ? |
| 2306 | mDragInfo.screen : indexOfChild(dropTargetLayout); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2307 | int spanX = mDragInfo != null ? mDragInfo.spanX : 1; |
| 2308 | int spanY = mDragInfo != null ? mDragInfo.spanY : 1; |
| 2309 | // First we find the cell nearest to point at which the item is |
| 2310 | // dropped, without any consideration to whether there is an item there. |
| 2311 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) |
| 2312 | mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); |
| 2313 | // If the item being dropped is a shortcut and the nearest drop |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 2314 | // 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] | 2315 | if (!mInScrollArea && createUserFolderIfNecessary(cell, container, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2316 | dropTargetLayout, mTargetCell, false, d.dragView, null)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2317 | return; |
| 2318 | } |
| 2319 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 2320 | if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, d, false)) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2321 | return; |
| 2322 | } |
| 2323 | |
| 2324 | // Aside from the special case where we're dropping a shortcut onto a shortcut, |
| 2325 | // we need to find the nearest cell location that is vacant |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2326 | mTargetCell = findNearestVacantArea((int) mDragViewVisualCenter[0], |
| 2327 | (int) mDragViewVisualCenter[1], mDragInfo.spanX, mDragInfo.spanY, cell, |
| 2328 | dropTargetLayout, mTargetCell); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 2329 | |
Michael Jurka | 7cfc282 | 2011-08-02 20:19:24 -0700 | [diff] [blame] | 2330 | if (mCurrentPage != screen && !hasMovedIntoHotseat) { |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 2331 | snapScreen = screen; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 2332 | snapToPage(screen); |
| 2333 | } |
| 2334 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2335 | if (mTargetCell[0] >= 0 && mTargetCell[1] >= 0) { |
Winson Chung | 40e882b | 2011-07-21 19:01:11 -0700 | [diff] [blame] | 2336 | if (hasMovedLayouts) { |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 2337 | // Reparent the view |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2338 | getParentCellLayoutForView(cell).removeView(cell); |
| 2339 | addInScreen(cell, container, screen, mTargetCell[0], mTargetCell[1], |
| 2340 | mDragInfo.spanX, mDragInfo.spanY); |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | // update the item's position after drop |
| 2344 | final ItemInfo info = (ItemInfo) cell.getTag(); |
| 2345 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 2346 | dropTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]); |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 2347 | lp.cellX = mTargetCell[0]; |
| 2348 | lp.cellY = mTargetCell[1]; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2349 | cell.setId(LauncherModel.getCellLayoutChildId(container, mDragInfo.screen, |
Patrick Dubroy | 9438336 | 2010-10-29 15:03:24 -0700 | [diff] [blame] | 2350 | mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY)); |
| 2351 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2352 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 2353 | cell instanceof LauncherAppWidgetHostView) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2354 | final CellLayout cellLayout = dropTargetLayout; |
| 2355 | // We post this call so that the widget has a chance to be placed |
| 2356 | // in its final location |
| 2357 | |
| 2358 | final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; |
| 2359 | AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo(); |
Adam Cohen | 27c09b0 | 2011-02-28 14:45:11 -0800 | [diff] [blame] | 2360 | if (pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) { |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 2361 | final Runnable resizeRunnable = new Runnable() { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2362 | public void run() { |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2363 | DragLayer dragLayer = mLauncher.getDragLayer(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2364 | dragLayer.addResizeFrame(info, hostView, cellLayout); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2365 | } |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 2366 | }; |
| 2367 | post(new Runnable() { |
| 2368 | public void run() { |
| 2369 | if (!isPageMoving()) { |
| 2370 | resizeRunnable.run(); |
| 2371 | } else { |
| 2372 | mDelayedResizeRunnable = resizeRunnable; |
| 2373 | } |
| 2374 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 2375 | }); |
| 2376 | } |
| 2377 | } |
| 2378 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2379 | LauncherModel.moveItemInDatabase(mLauncher, info, container, screen, lp.cellX, |
| 2380 | lp.cellY); |
Patrick Dubroy | cd68ff5 | 2010-10-28 17:57:05 -0700 | [diff] [blame] | 2381 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2382 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2383 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 2384 | final CellLayout parent = (CellLayout) cell.getParent().getParent(); |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 2385 | |
| 2386 | // Prepare it to be animated into its new position |
| 2387 | // This must be called after the view has been re-parented |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2388 | final Runnable disableHardwareLayersRunnable = new Runnable() { |
| 2389 | @Override |
| 2390 | public void run() { |
| 2391 | mAnimatingViewIntoPlace = false; |
| 2392 | updateChildrenLayersEnabled(); |
| 2393 | } |
| 2394 | }; |
| 2395 | mAnimatingViewIntoPlace = true; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2396 | if (d.dragView.hasDrawn()) { |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 2397 | int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION; |
| 2398 | setFinalScrollForPageChange(snapScreen); |
| 2399 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2400 | disableHardwareLayersRunnable); |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 2401 | resetFinalScrollForPageChange(snapScreen); |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 2402 | } else { |
| 2403 | cell.setVisibility(VISIBLE); |
| 2404 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 2405 | parent.onDropChild(cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
Adam Cohen | ed51cc9 | 2011-08-01 20:28:08 -0700 | [diff] [blame] | 2409 | public void setFinalScrollForPageChange(int screen) { |
| 2410 | if (screen >= 0) { |
| 2411 | mSavedScrollX = getScrollX(); |
| 2412 | CellLayout cl = (CellLayout) getChildAt(screen); |
| 2413 | mSavedTranslationX = cl.getTranslationX(); |
| 2414 | mSavedRotationY = cl.getRotationY(); |
| 2415 | final int newX = getChildOffset(screen) - getRelativeChildOffset(screen); |
| 2416 | setScrollX(newX); |
| 2417 | cl.setTranslationX(0f); |
| 2418 | cl.setRotationY(0f); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | public void resetFinalScrollForPageChange(int screen) { |
| 2423 | if (screen >= 0) { |
| 2424 | CellLayout cl = (CellLayout) getChildAt(screen); |
| 2425 | setScrollX(mSavedScrollX); |
| 2426 | cl.setTranslationX(mSavedTranslationX); |
| 2427 | cl.setRotationY(mSavedRotationY); |
| 2428 | } |
| 2429 | } |
| 2430 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 2431 | public void getViewLocationRelativeToSelf(View v, int[] location) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 2432 | getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2433 | int x = location[0]; |
| 2434 | int y = location[1]; |
| 2435 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 2436 | v.getLocationInWindow(location); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2437 | int vX = location[0]; |
| 2438 | int vY = location[1]; |
| 2439 | |
| 2440 | location[0] = vX - x; |
| 2441 | location[1] = vY - y; |
| 2442 | } |
| 2443 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2444 | public void onDragEnter(DragObject d) { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 2445 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2446 | mDragTargetLayout.setIsDragOverlapping(false); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 2447 | mDragTargetLayout.onDragExit(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 2448 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2449 | mDragTargetLayout = getCurrentDropLayout(); |
| 2450 | mDragTargetLayout.setIsDragOverlapping(true); |
| 2451 | mDragTargetLayout.onDragEnter(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 2452 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2453 | // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we |
| 2454 | // don't need to show the outlines |
| 2455 | if (LauncherApplication.isScreenLarge()) { |
| 2456 | showOutlines(); |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 2457 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2460 | private void doDragExit(DragObject d) { |
| 2461 | // Clean up folders |
| 2462 | cleanupFolderCreation(d); |
| 2463 | |
| 2464 | // Reset the scroll area and previous drag target |
| 2465 | onResetScrollArea(); |
| 2466 | |
| 2467 | if (mDragTargetLayout != null) { |
| 2468 | mDragTargetLayout.setIsDragOverlapping(false); |
| 2469 | mDragTargetLayout.onDragExit(); |
| 2470 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2471 | mLastDragOverView = null; |
| 2472 | |
| 2473 | if (!mIsPageMoving) { |
| 2474 | hideOutlines(); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | public void onDragExit(DragObject d) { |
| 2479 | doDragExit(d); |
| 2480 | } |
| 2481 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2482 | public DropTarget getDropTargetDelegate(DragObject d) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 2483 | return null; |
| 2484 | } |
| 2485 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2486 | /** |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2487 | * Tests to see if the drop will be accepted by Launcher, and if so, includes additional data |
| 2488 | * in the returned structure related to the widgets that match the drop (or a null list if it is |
| 2489 | * a shortcut drop). If the drop is not accepted then a null structure is returned. |
| 2490 | */ |
| 2491 | private Pair<Integer, List<WidgetMimeTypeHandlerData>> validateDrag(DragEvent event) { |
| 2492 | final LauncherModel model = mLauncher.getModel(); |
| 2493 | final ClipDescription desc = event.getClipDescription(); |
| 2494 | final int mimeTypeCount = desc.getMimeTypeCount(); |
| 2495 | for (int i = 0; i < mimeTypeCount; ++i) { |
| 2496 | final String mimeType = desc.getMimeType(i); |
| 2497 | if (mimeType.equals(InstallShortcutReceiver.SHORTCUT_MIMETYPE)) { |
| 2498 | return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, null); |
| 2499 | } else { |
| 2500 | final List<WidgetMimeTypeHandlerData> widgets = |
| 2501 | model.resolveWidgetsForMimeType(mContext, mimeType); |
| 2502 | if (widgets.size() > 0) { |
| 2503 | return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, widgets); |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | return null; |
| 2508 | } |
| 2509 | |
| 2510 | /** |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2511 | * Global drag and drop handler |
| 2512 | */ |
| 2513 | @Override |
| 2514 | public boolean onDragEvent(DragEvent event) { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2515 | final ClipDescription desc = event.getClipDescription(); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2516 | final CellLayout layout = (CellLayout) getChildAt(mCurrentPage); |
| 2517 | final int[] pos = new int[2]; |
| 2518 | layout.getLocationOnScreen(pos); |
| 2519 | // We need to offset the drag coordinates to layout coordinate space |
| 2520 | final int x = (int) event.getX() - pos[0]; |
| 2521 | final int y = (int) event.getY() - pos[1]; |
| 2522 | |
| 2523 | switch (event.getAction()) { |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2524 | case DragEvent.ACTION_DRAG_STARTED: { |
| 2525 | // Validate this drag |
| 2526 | Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event); |
| 2527 | if (test != null) { |
| 2528 | boolean isShortcut = (test.second == null); |
| 2529 | if (isShortcut) { |
| 2530 | // Check if we have enough space on this screen to add a new shortcut |
| 2531 | if (!layout.findCellForSpan(pos, 1, 1)) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 2532 | mLauncher.showOutOfSpaceMessage(); |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2533 | return false; |
| 2534 | } |
| 2535 | } |
| 2536 | } else { |
| 2537 | // Show error message if we couldn't accept any of the items |
| 2538 | Toast.makeText(mContext, mContext.getString(R.string.external_drop_widget_error), |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2539 | Toast.LENGTH_SHORT).show(); |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2543 | // Create the drag outline |
| 2544 | // We need to add extra padding to the bitmap to make room for the glow effect |
| 2545 | final Canvas canvas = new Canvas(); |
Michael Jurka | 38b4f7c | 2010-12-14 16:46:39 -0800 | [diff] [blame] | 2546 | final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2547 | mDragOutline = createExternalDragOutline(canvas, bitmapPadding); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2548 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2549 | // Show the current page outlines to indicate that we can accept this drop |
| 2550 | showOutlines(); |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 2551 | layout.setIsDragOccuring(true); |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2552 | layout.onDragEnter(); |
| 2553 | layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2554 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2555 | return true; |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2556 | } |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2557 | case DragEvent.ACTION_DRAG_LOCATION: |
| 2558 | // Visualize the drop location |
| 2559 | layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1); |
| 2560 | return true; |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2561 | case DragEvent.ACTION_DROP: { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2562 | // Try and add any shortcuts |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2563 | final LauncherModel model = mLauncher.getModel(); |
| 2564 | final ClipData data = event.getClipData(); |
Winson Chung | 55cef26 | 2010-10-28 14:14:18 -0700 | [diff] [blame] | 2565 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2566 | // We assume that the mime types are ordered in descending importance of |
| 2567 | // representation. So we enumerate the list of mime types and alert the |
| 2568 | // user if any widgets can handle the drop. Only the most preferred |
| 2569 | // representation will be handled. |
| 2570 | pos[0] = x; |
| 2571 | pos[1] = y; |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2572 | Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event); |
| 2573 | if (test != null) { |
| 2574 | final int index = test.first; |
| 2575 | final List<WidgetMimeTypeHandlerData> widgets = test.second; |
| 2576 | final boolean isShortcut = (widgets == null); |
| 2577 | final String mimeType = desc.getMimeType(index); |
| 2578 | if (isShortcut) { |
Dianne Hackborn | 0d5aad7 | 2011-01-17 15:28:58 -0800 | [diff] [blame] | 2579 | final Intent intent = data.getItemAt(index).getIntent(); |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2580 | Object info = model.infoFromShortcutIntent(mContext, intent, data.getIcon()); |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 2581 | onDropExternal(new int[] { x, y }, info, layout, false); |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2582 | } else { |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2583 | if (widgets.size() == 1) { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2584 | // If there is only one item, then go ahead and add and configure |
| 2585 | // that widget |
| 2586 | final AppWidgetProviderInfo widgetInfo = widgets.get(0).widgetInfo; |
| 2587 | final PendingAddWidgetInfo createInfo = |
| 2588 | new PendingAddWidgetInfo(widgetInfo, mimeType, data); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2589 | mLauncher.addAppWidgetFromDrop(createInfo, |
| 2590 | LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentPage, null, pos); |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2591 | } else { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2592 | // Show the widget picker dialog if there is more than one widget |
| 2593 | // that can handle this data type |
| 2594 | final InstallWidgetReceiver.WidgetListAdapter adapter = |
| 2595 | new InstallWidgetReceiver.WidgetListAdapter(mLauncher, mimeType, |
| 2596 | data, widgets, layout, mCurrentPage, pos); |
| 2597 | final AlertDialog.Builder builder = |
| 2598 | new AlertDialog.Builder(mContext); |
| 2599 | builder.setAdapter(adapter, adapter); |
| 2600 | builder.setCancelable(true); |
| 2601 | builder.setTitle(mContext.getString( |
| 2602 | R.string.external_drop_widget_pick_title)); |
| 2603 | builder.setIcon(R.drawable.ic_no_applications); |
| 2604 | builder.show(); |
Winson Chung | 55cef26 | 2010-10-28 14:14:18 -0700 | [diff] [blame] | 2605 | } |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2606 | } |
| 2607 | } |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2608 | return true; |
Winson Chung | 580e277 | 2010-11-10 16:03:00 -0800 | [diff] [blame] | 2609 | } |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2610 | case DragEvent.ACTION_DRAG_ENDED: |
| 2611 | // Hide the page outlines after the drop |
Michael Jurka | 33945b2 | 2010-12-21 18:19:38 -0800 | [diff] [blame] | 2612 | layout.setIsDragOccuring(false); |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2613 | layout.onDragExit(); |
| 2614 | hideOutlines(); |
| 2615 | return true; |
| 2616 | } |
| 2617 | return super.onDragEvent(event); |
| 2618 | } |
| 2619 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2620 | /* |
| 2621 | * |
| 2622 | * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's |
| 2623 | * coordinate space. The argument xy is modified with the return result. |
| 2624 | * |
| 2625 | */ |
| 2626 | void mapPointFromSelfToChild(View v, float[] xy) { |
| 2627 | mapPointFromSelfToChild(v, xy, null); |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * |
| 2632 | * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's |
| 2633 | * coordinate space. The argument xy is modified with the return result. |
| 2634 | * |
| 2635 | * 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] | 2636 | * computing it itself; we use this to avoid redundant matrix inversions in |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2637 | * findMatchingPageForDragOver |
| 2638 | * |
| 2639 | */ |
| 2640 | void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) { |
| 2641 | if (cachedInverseMatrix == null) { |
| 2642 | v.getMatrix().invert(mTempInverseMatrix); |
| 2643 | cachedInverseMatrix = mTempInverseMatrix; |
| 2644 | } |
| 2645 | xy[0] = xy[0] + mScrollX - v.getLeft(); |
| 2646 | xy[1] = xy[1] + mScrollY - v.getTop(); |
| 2647 | cachedInverseMatrix.mapPoints(xy); |
| 2648 | } |
| 2649 | |
| 2650 | /* |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2651 | * Maps a point from the Workspace's coordinate system to another sibling view's. (Workspace |
| 2652 | * covers the full screen) |
| 2653 | */ |
| 2654 | void mapPointFromSelfToSibling(View v, float[] xy) { |
| 2655 | xy[0] = xy[0] - v.getLeft(); |
| 2656 | xy[1] = xy[1] - v.getTop(); |
| 2657 | } |
| 2658 | |
| 2659 | /* |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2660 | * |
| 2661 | * Convert the 2D coordinate xy from this CellLayout's coordinate space to |
| 2662 | * the parent View's coordinate space. The argument xy is modified with the return result. |
| 2663 | * |
| 2664 | */ |
| 2665 | void mapPointFromChildToSelf(View v, float[] xy) { |
| 2666 | v.getMatrix().mapPoints(xy); |
| 2667 | xy[0] -= (mScrollX - v.getLeft()); |
| 2668 | xy[1] -= (mScrollY - v.getTop()); |
| 2669 | } |
| 2670 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2671 | static private float squaredDistance(float[] point1, float[] point2) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2672 | float distanceX = point1[0] - point2[0]; |
| 2673 | float distanceY = point2[1] - point2[1]; |
| 2674 | return distanceX * distanceX + distanceY * distanceY; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2675 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2676 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2677 | /* |
| 2678 | * |
| 2679 | * Returns true if the passed CellLayout cl overlaps with dragView |
| 2680 | * |
| 2681 | */ |
| 2682 | boolean overlaps(CellLayout cl, DragView dragView, |
| 2683 | int dragViewX, int dragViewY, Matrix cachedInverseMatrix) { |
| 2684 | // Transform the coordinates of the item being dragged to the CellLayout's coordinates |
| 2685 | final float[] draggedItemTopLeft = mTempDragCoordinates; |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2686 | draggedItemTopLeft[0] = dragViewX; |
| 2687 | draggedItemTopLeft[1] = dragViewY; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2688 | final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates; |
Michael Jurka | f12c75c | 2011-01-25 22:41:40 -0800 | [diff] [blame] | 2689 | draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getDragRegionWidth(); |
| 2690 | draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getDragRegionHeight(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2691 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2692 | // Transform the dragged item's top left coordinates |
| 2693 | // to the CellLayout's local coordinates |
| 2694 | mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix); |
| 2695 | float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]); |
| 2696 | float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]); |
| 2697 | |
| 2698 | if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) { |
| 2699 | // Transform the dragged item's bottom right coordinates |
| 2700 | // to the CellLayout's local coordinates |
| 2701 | mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix); |
| 2702 | float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]); |
| 2703 | float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]); |
| 2704 | |
| 2705 | if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) { |
| 2706 | float overlap = (overlapRegionRight - overlapRegionLeft) * |
| 2707 | (overlapRegionBottom - overlapRegionTop); |
| 2708 | if (overlap > 0) { |
| 2709 | return true; |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
| 2716 | /* |
| 2717 | * |
| 2718 | * This method returns the CellLayout that is currently being dragged to. In order to drag |
| 2719 | * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second |
| 2720 | * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one |
| 2721 | * |
| 2722 | * Return null if no CellLayout is currently being dragged over |
| 2723 | * |
| 2724 | */ |
| 2725 | private CellLayout findMatchingPageForDragOver( |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2726 | DragView dragView, float originX, float originY, boolean exact) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2727 | // We loop through all the screens (ie CellLayouts) and see which ones overlap |
| 2728 | // 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] | 2729 | final int screenCount = getChildCount(); |
| 2730 | CellLayout bestMatchingScreen = null; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2731 | float smallestDistSoFar = Float.MAX_VALUE; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2732 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2733 | for (int i = 0; i < screenCount; i++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2734 | CellLayout cl = (CellLayout) getChildAt(i); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2735 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2736 | final float[] touchXy = {originX, originY}; |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2737 | // Transform the touch coordinates to the CellLayout's local coordinates |
| 2738 | // 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] | 2739 | cl.getMatrix().invert(mTempInverseMatrix); |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2740 | mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2741 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2742 | if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && |
| 2743 | touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { |
| 2744 | return cl; |
| 2745 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2746 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2747 | if (!exact && overlaps(cl, dragView, (int) originX, (int) originY, mTempInverseMatrix)) { |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2748 | // Get the center of the cell layout in screen coordinates |
| 2749 | final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; |
| 2750 | cellLayoutCenter[0] = cl.getWidth()/2; |
| 2751 | cellLayoutCenter[1] = cl.getHeight()/2; |
| 2752 | mapPointFromChildToSelf(cl, cellLayoutCenter); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2753 | |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2754 | touchXy[0] = originX; |
| 2755 | touchXy[1] = originY; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2756 | |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2757 | // Calculate the distance between the center of the CellLayout |
| 2758 | // and the touch point |
| 2759 | float dist = squaredDistance(touchXy, cellLayoutCenter); |
| 2760 | |
| 2761 | if (dist < smallestDistSoFar) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2762 | smallestDistSoFar = dist; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2763 | bestMatchingScreen = cl; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2764 | } |
Michael Jurka | 4516c11 | 2010-10-07 15:13:47 -0700 | [diff] [blame] | 2765 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2766 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 2767 | return bestMatchingScreen; |
| 2768 | } |
| 2769 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2770 | // This is used to compute the visual center of the dragView. This point is then |
| 2771 | // used to visualize drop locations and determine where to drop an item. The idea is that |
| 2772 | // the visual center represents the user's interpretation of where the item is, and hence |
| 2773 | // is the appropriate point to use when determining drop location. |
| 2774 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 2775 | DragView dragView, float[] recycle) { |
| 2776 | float res[]; |
| 2777 | if (recycle == null) { |
| 2778 | res = new float[2]; |
| 2779 | } else { |
| 2780 | res = recycle; |
| 2781 | } |
| 2782 | |
| 2783 | // First off, the drag view has been shifted in a way that is not represented in the |
| 2784 | // x and y values or the x/yOffsets. Here we account for that shift. |
| 2785 | x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX); |
| 2786 | y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 2787 | |
| 2788 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 2789 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 2790 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 2791 | // R.dimen.dragViewOffsetY. |
| 2792 | int left = x - xOffset; |
| 2793 | int top = y - yOffset; |
| 2794 | |
| 2795 | // In order to find the visual center, we shift by half the dragRect |
| 2796 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 2797 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 2798 | |
| 2799 | return res; |
| 2800 | } |
| 2801 | |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 2802 | private boolean isDragWidget(DragObject d) { |
| 2803 | return (d.dragInfo instanceof LauncherAppWidgetInfo || |
| 2804 | d.dragInfo instanceof PendingAddWidgetInfo); |
| 2805 | } |
| 2806 | private boolean isExternalDragWidget(DragObject d) { |
| 2807 | return d.dragSource != this && isDragWidget(d); |
| 2808 | } |
| 2809 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 2810 | public void onDragOver(DragObject d) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2811 | // Skip drag over events while we are dragging over side pages |
| 2812 | if (mInScrollArea) return; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2813 | if (mIsSwitchingState) return; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2814 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2815 | Rect r = new Rect(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2816 | CellLayout layout = null; |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2817 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 2818 | |
| 2819 | // Ensure that we have proper spans for the item that we are dropping |
| 2820 | 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] | 2821 | mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2822 | d.dragView, mDragViewVisualCenter); |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2823 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2824 | // Identify whether we have dragged over a side page |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2825 | if (isSmall()) { |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame^] | 2826 | if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2827 | mLauncher.getHotseat().getHitRect(r); |
| 2828 | if (r.contains(d.x, d.y)) { |
| 2829 | layout = mLauncher.getHotseat().getLayout(); |
| 2830 | } |
| 2831 | } |
| 2832 | if (layout == null) { |
| 2833 | layout = findMatchingPageForDragOver(d.dragView, mDragViewVisualCenter[0], |
Adam Cohen | 0061875 | 2011-07-20 12:06:04 -0700 | [diff] [blame] | 2834 | mDragViewVisualCenter[1], true); |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2835 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2836 | if (layout != mDragTargetLayout) { |
| 2837 | // Cancel all intermediate folder states |
| 2838 | cleanupFolderCreation(d); |
| 2839 | |
| 2840 | if (mDragTargetLayout != null) { |
| 2841 | mDragTargetLayout.setIsDragOverlapping(false); |
| 2842 | mDragTargetLayout.onDragExit(); |
| 2843 | } |
| 2844 | mDragTargetLayout = layout; |
| 2845 | if (mDragTargetLayout != null) { |
| 2846 | mDragTargetLayout.setIsDragOverlapping(true); |
| 2847 | mDragTargetLayout.onDragEnter(); |
| 2848 | } else { |
| 2849 | mLastDragOverView = null; |
| 2850 | } |
| 2851 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 2852 | boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2853 | if (isInSpringLoadedMode) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 2854 | if (mLauncher.isHotseatLayout(layout)) { |
| 2855 | mSpringLoadedDragController.cancel(); |
| 2856 | } else { |
| 2857 | mSpringLoadedDragController.setAlarm(mDragTargetLayout); |
| 2858 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | } else { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2862 | // 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^] | 2863 | if (mLauncher.getHotseat() != null && !isDragWidget(d)) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2864 | mLauncher.getHotseat().getHitRect(r); |
| 2865 | if (r.contains(d.x, d.y)) { |
| 2866 | layout = mLauncher.getHotseat().getLayout(); |
| 2867 | } |
| 2868 | } |
| 2869 | if (layout == null) { |
| 2870 | layout = getCurrentDropLayout(); |
| 2871 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2872 | if (layout != mDragTargetLayout) { |
| 2873 | if (mDragTargetLayout != null) { |
| 2874 | mDragTargetLayout.setIsDragOverlapping(false); |
| 2875 | mDragTargetLayout.onDragExit(); |
| 2876 | } |
| 2877 | mDragTargetLayout = layout; |
| 2878 | mDragTargetLayout.setIsDragOverlapping(true); |
| 2879 | mDragTargetLayout.onDragEnter(); |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | // Handle the drag over |
| 2884 | if (mDragTargetLayout != null) { |
| 2885 | final View child = (mDragInfo == null) ? null : mDragInfo.cell; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2886 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2887 | // We want the point to be mapped to the dragTarget. |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2888 | if (mLauncher.isHotseatLayout(mDragTargetLayout)) { |
| 2889 | mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter); |
| 2890 | } else { |
| 2891 | mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); |
| 2892 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2893 | ItemInfo info = (ItemInfo) d.dragInfo; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 2894 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2895 | mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], |
| 2896 | (int) mDragViewVisualCenter[1], 1, 1, mDragTargetLayout, mTargetCell); |
| 2897 | final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], |
| 2898 | mTargetCell[1]); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2899 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2900 | boolean userFolderPending = willCreateUserFolder(info, mDragTargetLayout, |
| 2901 | mTargetCell, false); |
| 2902 | boolean isOverFolder = dragOverView instanceof FolderIcon; |
| 2903 | if (dragOverView != mLastDragOverView) { |
| 2904 | cancelFolderCreation(); |
| 2905 | if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) { |
| 2906 | ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2907 | } |
| 2908 | } |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 2909 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2910 | if (userFolderPending && dragOverView != mLastDragOverView) { |
| 2911 | mFolderCreationAlarm.setOnAlarmListener(new |
| 2912 | FolderCreationAlarmListener(mDragTargetLayout, mTargetCell[0], mTargetCell[1])); |
| 2913 | mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); |
| 2914 | } |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 2915 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2916 | if (dragOverView != mLastDragOverView && isOverFolder) { |
| 2917 | ((FolderIcon) dragOverView).onDragEnter(d.dragInfo); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 2918 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2919 | mDragTargetLayout.clearDragOutlines(); |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 2920 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 2921 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2922 | mLastDragOverView = dragOverView; |
| 2923 | |
| 2924 | if (!mCreateUserFolderOnDrop && !isOverFolder) { |
| 2925 | mDragTargetLayout.visualizeDropLocation(child, mDragOutline, |
| 2926 | (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], |
| 2927 | item.spanX, item.spanY); |
| 2928 | } |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 2929 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 2932 | private void cleanupFolderCreation(DragObject d) { |
| 2933 | if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) { |
| 2934 | mDragFolderRingAnimator.animateToNaturalState(); |
| 2935 | } |
| 2936 | if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) { |
| 2937 | if (d != null) { |
| 2938 | ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo); |
| 2939 | } |
| 2940 | } |
| 2941 | mFolderCreationAlarm.cancelAlarm(); |
| 2942 | } |
| 2943 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 2944 | private void cancelFolderCreation() { |
| 2945 | if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) { |
| 2946 | mDragFolderRingAnimator.animateToNaturalState(); |
| 2947 | } |
| 2948 | mCreateUserFolderOnDrop = false; |
| 2949 | mFolderCreationAlarm.cancelAlarm(); |
| 2950 | } |
| 2951 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2952 | class FolderCreationAlarmListener implements OnAlarmListener { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2953 | CellLayout layout; |
| 2954 | int cellX; |
| 2955 | int cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2956 | |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2957 | public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { |
| 2958 | this.layout = layout; |
| 2959 | this.cellX = cellX; |
| 2960 | this.cellY = cellY; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2964 | if (mDragFolderRingAnimator == null) { |
| 2965 | mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null); |
| 2966 | } |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2967 | mDragFolderRingAnimator.setCell(cellX, cellY); |
| 2968 | mDragFolderRingAnimator.setCellLayout(layout); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2969 | mDragFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 2970 | layout.showFolderAccept(mDragFolderRingAnimator); |
| 2971 | layout.clearDragOutlines(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2972 | mCreateUserFolderOnDrop = true; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
Winson Chung | a34abf8 | 2010-11-12 12:10:35 -0800 | [diff] [blame] | 2976 | @Override |
| 2977 | public void getHitRect(Rect outRect) { |
| 2978 | // We want the workspace to have the whole area of the display (it will find the correct |
| 2979 | // cell layout to drop to in the existing drag/drop logic. |
| 2980 | final Display d = mLauncher.getWindowManager().getDefaultDisplay(); |
| 2981 | outRect.set(0, 0, d.getWidth(), d.getHeight()); |
| 2982 | } |
| 2983 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 2984 | /** |
| 2985 | * Add the item specified by dragInfo to the given layout. |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 2986 | * @return true if successful |
| 2987 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 2988 | public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) { |
| 2989 | if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) { |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 2990 | onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 2991 | return true; |
| 2992 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 2993 | mLauncher.showOutOfSpaceMessage(); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 2994 | return false; |
| 2995 | } |
| 2996 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 2997 | private void onDropExternal(int[] touchXY, Object dragInfo, |
| 2998 | CellLayout cellLayout, boolean insertAtFirst) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 2999 | onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 3002 | /** |
| 3003 | * Drop an item that didn't originate on one of the workspace screens. |
| 3004 | * It may have come from Launcher (e.g. from all apps or customize), or it may have |
| 3005 | * come from another app altogether. |
| 3006 | * |
| 3007 | * NOTE: This can also be called when we are outside of a drag event, when we want |
| 3008 | * to add an item to one of the workspace screens. |
| 3009 | */ |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3010 | private void onDropExternal(final int[] touchXY, final Object dragInfo, |
| 3011 | final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { |
| 3012 | final Runnable exitSpringLoadedRunnable = new Runnable() { |
| 3013 | @Override |
| 3014 | public void run() { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 3015 | mLauncher.exitSpringLoadedDragModeDelayed(true, false); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3016 | } |
| 3017 | }; |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3018 | |
| 3019 | ItemInfo info = (ItemInfo) dragInfo; |
| 3020 | int spanX = info.spanX; |
| 3021 | int spanY = info.spanY; |
| 3022 | if (mDragInfo != null) { |
| 3023 | spanX = mDragInfo.spanX; |
| 3024 | spanY = mDragInfo.spanY; |
| 3025 | } |
| 3026 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3027 | final long container = mLauncher.isHotseatLayout(cellLayout) ? |
| 3028 | LauncherSettings.Favorites.CONTAINER_HOTSEAT : |
| 3029 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3030 | final int screen = indexOfChild(cellLayout); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3031 | if (!mLauncher.isHotseatLayout(cellLayout) && screen != mCurrentPage |
| 3032 | && mState != State.SPRING_LOADED) { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 3033 | snapToPage(screen); |
| 3034 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3035 | |
| 3036 | if (info instanceof PendingAddItemInfo) { |
| 3037 | final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo; |
| 3038 | |
Adam Cohen | fbba09b | 2011-07-18 21:43:05 -0700 | [diff] [blame] | 3039 | mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], spanX, spanY, null, |
| 3040 | cellLayout, mTargetCell); |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3041 | Runnable onAnimationCompleteRunnable = new Runnable() { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3042 | @Override |
| 3043 | public void run() { |
| 3044 | // When dragging and dropping from customization tray, we deal with creating |
| 3045 | // widgets/shortcuts/folders in a slightly different way |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3046 | switch (pendingInfo.itemType) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3047 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3048 | mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3049 | container, screen, mTargetCell, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3050 | break; |
| 3051 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3052 | mLauncher.processShortcutFromDrop(pendingInfo.componentName, |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3053 | container, screen, mTargetCell, null); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3054 | break; |
| 3055 | default: |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3056 | throw new IllegalStateException("Unknown item type: " + |
| 3057 | pendingInfo.itemType); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3058 | } |
| 3059 | cellLayout.onDragExit(); |
| 3060 | } |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3061 | }; |
| 3062 | |
| 3063 | // Now we animate the dragView, (ie. the widget or shortcut preview) into its final |
| 3064 | // location and size on the home screen. |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3065 | int loc[] = new int[2]; |
| 3066 | cellLayout.cellToPoint(mTargetCell[0], mTargetCell[1], loc); |
| 3067 | |
| 3068 | RectF r = new RectF(); |
| 3069 | cellLayout.cellToRect(mTargetCell[0], mTargetCell[1], spanX, spanY, r); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3070 | setFinalTransitionTransform(cellLayout); |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3071 | float cellLayoutScale = |
| 3072 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(cellLayout, loc); |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3073 | resetTransitionTransform(cellLayout); |
Adam Cohen | b7e1618 | 2011-07-15 17:55:02 -0700 | [diff] [blame] | 3074 | |
| 3075 | float dragViewScale = r.width() / d.dragView.getMeasuredWidth(); |
| 3076 | // The animation will scale the dragView about its center, so we need to center about |
| 3077 | // the final location. |
| 3078 | loc[0] -= (d.dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2; |
| 3079 | loc[1] -= (d.dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; |
| 3080 | |
| 3081 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, loc, |
| 3082 | dragViewScale * cellLayoutScale, onAnimationCompleteRunnable); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 3083 | } else { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3084 | // This is for other drag/drop cases, like dragging from All Apps |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3085 | View view = null; |
| 3086 | |
| 3087 | switch (info.itemType) { |
| 3088 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 3089 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 3090 | if (info.container == NO_ID && info instanceof ApplicationInfo) { |
| 3091 | // Came from all apps -- make a copy |
| 3092 | info = new ShortcutInfo((ApplicationInfo) info); |
| 3093 | } |
| 3094 | view = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 3095 | (ShortcutInfo) info); |
| 3096 | break; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3097 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3098 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, |
| 3099 | (FolderInfo) info, mIconCache); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3100 | break; |
| 3101 | default: |
| 3102 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 3103 | } |
| 3104 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3105 | // First we find the cell nearest to point at which the item is |
| 3106 | // dropped, without any consideration to whether there is an item there. |
| 3107 | if (touchXY != null) { |
| 3108 | mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, |
| 3109 | cellLayout, mTargetCell); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3110 | d.postAnimationRunnable = exitSpringLoadedRunnable; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3111 | if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, true, |
| 3112 | d.dragView, d.postAnimationRunnable)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3113 | return; |
| 3114 | } |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3115 | if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, d, true)) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3116 | return; |
| 3117 | } |
Adam Cohen | 10b1737 | 2011-04-15 14:21:25 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 3120 | if (touchXY != null) { |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3121 | // when dragging and dropping, just find the closest free spot |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3122 | mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, null, |
| 3123 | cellLayout, mTargetCell); |
Michael Jurka | 9c6de3d | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3124 | } else { |
| 3125 | cellLayout.findCellForSpan(mTargetCell, 1, 1); |
| 3126 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3127 | addInScreen(view, container, screen, mTargetCell[0], mTargetCell[1], info.spanX, |
| 3128 | info.spanY, insertAtFirst); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 3129 | cellLayout.onDropChild(view); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 3130 | cellLayout.animateDrop(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 3131 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 3132 | cellLayout.getChildrenLayout().measureChild(view); |
| 3133 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3134 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3135 | lp.cellX, lp.cellY); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3136 | |
| 3137 | if (d.dragView != null) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3138 | // We wrap the animation call in the temporary set and reset of the current |
| 3139 | // cellLayout to its final transform -- this means we animate the drag view to |
| 3140 | // the correct final location. |
| 3141 | setFinalTransitionTransform(cellLayout); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 3142 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3143 | exitSpringLoadedRunnable); |
| 3144 | resetTransitionTransform(cellLayout); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 3145 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 3146 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3147 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3148 | |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3149 | public void setFinalTransitionTransform(CellLayout layout) { |
| 3150 | if (isSwitchingState()) { |
| 3151 | int index = indexOfChild(layout); |
| 3152 | mCurrentScaleX = layout.getScaleX(); |
| 3153 | mCurrentScaleY = layout.getScaleY(); |
| 3154 | mCurrentTranslationX = layout.getTranslationX(); |
| 3155 | mCurrentTranslationY = layout.getTranslationY(); |
| 3156 | mCurrentRotationY = layout.getRotationY(); |
| 3157 | layout.setScaleX(mNewScaleXs[index]); |
| 3158 | layout.setScaleY(mNewScaleYs[index]); |
| 3159 | layout.setTranslationX(mNewTranslationXs[index]); |
| 3160 | layout.setTranslationY(mNewTranslationYs[index]); |
| 3161 | layout.setRotationY(mNewRotationYs[index]); |
| 3162 | } |
| 3163 | } |
| 3164 | public void resetTransitionTransform(CellLayout layout) { |
| 3165 | if (isSwitchingState()) { |
Adam Cohen | 4b285c5 | 2011-07-21 14:24:06 -0700 | [diff] [blame] | 3166 | mCurrentScaleX = layout.getScaleX(); |
| 3167 | mCurrentScaleY = layout.getScaleY(); |
| 3168 | mCurrentTranslationX = layout.getTranslationX(); |
| 3169 | mCurrentTranslationY = layout.getTranslationY(); |
| 3170 | mCurrentRotationY = layout.getRotationY(); |
| 3171 | layout.setScaleX(mCurrentScaleX); |
| 3172 | layout.setScaleY(mCurrentScaleY); |
| 3173 | layout.setTranslationX(mCurrentTranslationX); |
| 3174 | layout.setTranslationY(mCurrentTranslationY); |
| 3175 | layout.setRotationY(mCurrentRotationY); |
| 3176 | } |
| 3177 | } |
| 3178 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3179 | /** |
| 3180 | * Return the current {@link CellLayout}, correctly picking the destination |
| 3181 | * screen while a scroll is in progress. |
| 3182 | */ |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 3183 | public CellLayout getCurrentDropLayout() { |
| 3184 | return (CellLayout) getChildAt(mNextPage == INVALID_PAGE ? mCurrentPage : mNextPage); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3185 | } |
| 3186 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3187 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3188 | * Return the current CellInfo describing our current drag; this method exists |
| 3189 | * so that Launcher can sync this object with the correct info when the activity is created/ |
| 3190 | * destroyed |
| 3191 | * |
| 3192 | */ |
| 3193 | public CellLayout.CellInfo getDragInfo() { |
| 3194 | return mDragInfo; |
| 3195 | } |
| 3196 | |
| 3197 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3198 | * Calculate the nearest cell where the given object would be dropped. |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3199 | * |
| 3200 | * pixelX and pixelY should be in the coordinate system of layout |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3201 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 3202 | private int[] findNearestVacantArea(int pixelX, int pixelY, |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3203 | int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3204 | return layout.findNearestVacantArea( |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3205 | pixelX, pixelY, spanX, spanY, ignoreView, recycle); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 3206 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3207 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3208 | /** |
| 3209 | * Calculate the nearest cell where the given object would be dropped. |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3210 | * |
| 3211 | * pixelX and pixelY should be in the coordinate system of layout |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3212 | */ |
| 3213 | private int[] findNearestArea(int pixelX, int pixelY, |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3214 | int spanX, int spanY, CellLayout layout, int[] recycle) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3215 | return layout.findNearestArea( |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 3216 | pixelX, pixelY, spanX, spanY, recycle); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3217 | } |
| 3218 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 3219 | void setup(Launcher launcher, DragController dragController) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3220 | mLauncher = launcher; |
Michael Jurka | c2f7f47 | 2010-12-14 15:34:42 -0800 | [diff] [blame] | 3221 | mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 3222 | mDragController = dragController; |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3223 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3224 | // hardware layers on children are enabled on startup, but should be disabled until |
| 3225 | // needed |
| 3226 | updateChildrenLayersEnabled(); |
| 3227 | setWallpaperDimension(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3228 | } |
| 3229 | |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 3230 | /** |
| 3231 | * Called at the end of a drag which originated on the workspace. |
| 3232 | */ |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3233 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3234 | if (success) { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3235 | if (target != this) { |
| 3236 | if (mDragInfo != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3237 | getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3238 | if (mDragInfo.cell instanceof DropTarget) { |
| 3239 | mDragController.removeDropTarget((DropTarget) mDragInfo.cell); |
| 3240 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 3241 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3242 | } |
Patrick Dubroy | ce34a97 | 2010-10-19 10:34:32 -0700 | [diff] [blame] | 3243 | } else if (mDragInfo != null) { |
Patrick Dubroy | e3887cc | 2011-01-20 10:43:40 -0800 | [diff] [blame] | 3244 | // NOTE: When 'success' is true, onDragExit is called by the DragController before |
| 3245 | // calling onDropCompleted(). We call it ourselves here, but maybe this should be |
| 3246 | // moved into DragController.cancelDrag(). |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3247 | doDragExit(null); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3248 | CellLayout cellLayout; |
| 3249 | if (mLauncher.isHotseatLayout(target)) { |
| 3250 | cellLayout = mLauncher.getHotseat().getLayout(); |
| 3251 | } else { |
| 3252 | cellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 3253 | } |
| 3254 | cellLayout.onDropChild(mDragInfo.cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3255 | } |
Joe Onorato | 4be866d | 2010-10-10 11:26:02 -0700 | [diff] [blame] | 3256 | mDragOutline = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3257 | mDragInfo = null; |
| 3258 | } |
| 3259 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 3260 | public boolean isDropEnabled() { |
| 3261 | return true; |
| 3262 | } |
| 3263 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3264 | @Override |
| 3265 | protected void onRestoreInstanceState(Parcelable state) { |
| 3266 | super.onRestoreInstanceState(state); |
| 3267 | Launcher.setScreen(mCurrentPage); |
| 3268 | } |
| 3269 | |
| 3270 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3271 | public void scrollLeft() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3272 | if (!isSmall() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3273 | super.scrollLeft(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3274 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 3275 | Folder openFolder = getOpenFolder(); |
| 3276 | if (openFolder != null) { |
| 3277 | openFolder.completeDragExit(); |
| 3278 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3279 | } |
| 3280 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3281 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3282 | public void scrollRight() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3283 | if (!isSmall() && !mIsSwitchingState) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3284 | super.scrollRight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3285 | } |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 3286 | Folder openFolder = getOpenFolder(); |
| 3287 | if (openFolder != null) { |
| 3288 | openFolder.completeDragExit(); |
| 3289 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3290 | } |
| 3291 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 3292 | @Override |
Winson Chung | 1c4cf4a | 2011-07-29 14:49:10 -0700 | [diff] [blame] | 3293 | public void onEnterScrollArea(int x, int y, int direction) { |
| 3294 | // Ignore the scroll area if we are dragging over the hot seat |
| 3295 | if (mLauncher.getHotseat() != null) { |
| 3296 | Rect r = new Rect(); |
| 3297 | mLauncher.getHotseat().getHitRect(r); |
| 3298 | if (r.contains(x, y)) { |
| 3299 | return; |
| 3300 | } |
| 3301 | } |
| 3302 | |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 3303 | if (!isSmall() && !mIsSwitchingState) { |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3304 | mInScrollArea = true; |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3305 | |
| 3306 | final int page = mCurrentPage + (direction == DragController.SCROLL_LEFT ? -1 : 1); |
| 3307 | final CellLayout layout = (CellLayout) getChildAt(page); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 3308 | cancelFolderCreation(); |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 3309 | |
| 3310 | if (layout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3311 | // Exit the current layout and mark the overlapping layout |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 3312 | if (mDragTargetLayout != null) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3313 | mDragTargetLayout.setIsDragOverlapping(false); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 3314 | mDragTargetLayout.onDragExit(); |
Patrick Dubroy | 0207c52 | 2010-11-03 22:12:02 -0700 | [diff] [blame] | 3315 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3316 | mDragTargetLayout = layout; |
| 3317 | mDragTargetLayout.setIsDragOverlapping(true); |
| 3318 | |
| 3319 | // Workspace is responsible for drawing the edge glow on adjacent pages, |
| 3320 | // so we need to redraw the workspace when this may have changed. |
| 3321 | invalidate(); |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3322 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | @Override |
| 3327 | public void onExitScrollArea() { |
Michael Jurka | d718d6a | 2010-10-14 15:35:17 -0700 | [diff] [blame] | 3328 | if (mInScrollArea) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3329 | if (mDragTargetLayout != null) { |
| 3330 | // Unmark the overlapping layout and re-enter the current layout |
| 3331 | mDragTargetLayout.setIsDragOverlapping(false); |
| 3332 | mDragTargetLayout = getCurrentDropLayout(); |
| 3333 | mDragTargetLayout.onDragEnter(); |
| 3334 | |
| 3335 | // Workspace is responsible for drawing the edge glow on adjacent pages, |
| 3336 | // so we need to redraw the workspace when this may have changed. |
| 3337 | invalidate(); |
| 3338 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3339 | mInScrollArea = false; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
| 3342 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3343 | private void onResetScrollArea() { |
| 3344 | if (mDragTargetLayout != null) { |
| 3345 | // Unmark the overlapping layout |
| 3346 | mDragTargetLayout.setIsDragOverlapping(false); |
| 3347 | |
| 3348 | // Workspace is responsible for drawing the edge glow on adjacent pages, |
| 3349 | // so we need to redraw the workspace when this may have changed. |
| 3350 | invalidate(); |
| 3351 | } |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 3352 | mInScrollArea = false; |
| 3353 | } |
| 3354 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3355 | /** |
| 3356 | * Returns a specific CellLayout |
| 3357 | */ |
| 3358 | CellLayout getParentCellLayoutForView(View v) { |
| 3359 | ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts(); |
| 3360 | for (CellLayout layout : layouts) { |
| 3361 | if (layout.getChildrenLayout().indexOfChild(v) > -1) { |
| 3362 | return layout; |
| 3363 | } |
| 3364 | } |
| 3365 | return null; |
| 3366 | } |
| 3367 | |
| 3368 | /** |
| 3369 | * Returns a list of all the CellLayouts in the workspace. |
| 3370 | */ |
| 3371 | ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() { |
| 3372 | ArrayList<CellLayout> layouts = new ArrayList<CellLayout>(); |
| 3373 | int screenCount = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3374 | for (int screen = 0; screen < screenCount; screen++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3375 | layouts.add(((CellLayout) getChildAt(screen))); |
| 3376 | } |
| 3377 | if (mLauncher.getHotseat() != null) { |
| 3378 | layouts.add(mLauncher.getHotseat().getLayout()); |
| 3379 | } |
| 3380 | return layouts; |
| 3381 | } |
| 3382 | |
| 3383 | /** |
| 3384 | * We should only use this to search for specific children. Do not use this method to modify |
| 3385 | * CellLayoutChildren directly. |
| 3386 | */ |
| 3387 | ArrayList<CellLayoutChildren> getWorkspaceAndHotseatCellLayoutChildren() { |
| 3388 | ArrayList<CellLayoutChildren> childrenLayouts = new ArrayList<CellLayoutChildren>(); |
| 3389 | int screenCount = getChildCount(); |
| 3390 | for (int screen = 0; screen < screenCount; screen++) { |
| 3391 | childrenLayouts.add(((CellLayout) getChildAt(screen)).getChildrenLayout()); |
| 3392 | } |
| 3393 | if (mLauncher.getHotseat() != null) { |
| 3394 | childrenLayouts.add(mLauncher.getHotseat().getLayout().getChildrenLayout()); |
| 3395 | } |
| 3396 | return childrenLayouts; |
| 3397 | } |
| 3398 | |
| 3399 | public Folder getFolderForTag(Object tag) { |
| 3400 | ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); |
| 3401 | for (CellLayoutChildren layout: childrenLayouts) { |
| 3402 | int count = layout.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3403 | for (int i = 0; i < count; i++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3404 | View child = layout.getChildAt(i); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3405 | if (child instanceof Folder) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3406 | Folder f = (Folder) child; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3407 | if (f.getInfo() == tag && f.getInfo().opened) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3408 | return f; |
| 3409 | } |
| 3410 | } |
| 3411 | } |
| 3412 | } |
| 3413 | return null; |
| 3414 | } |
| 3415 | |
| 3416 | public View getViewForTag(Object tag) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3417 | ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); |
| 3418 | for (CellLayoutChildren layout: childrenLayouts) { |
| 3419 | int count = layout.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3420 | for (int i = 0; i < count; i++) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3421 | View child = layout.getChildAt(i); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3422 | if (child.getTag() == tag) { |
| 3423 | return child; |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | return null; |
| 3428 | } |
| 3429 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3430 | void clearDropTargets() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3431 | ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); |
| 3432 | for (CellLayoutChildren layout: childrenLayouts) { |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 3433 | int childCount = layout.getChildCount(); |
| 3434 | for (int j = 0; j < childCount; j++) { |
| 3435 | View v = layout.getChildAt(j); |
| 3436 | if (v instanceof DropTarget) { |
| 3437 | mDragController.removeDropTarget((DropTarget) v); |
| 3438 | } |
| 3439 | } |
| 3440 | } |
| 3441 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3442 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3443 | void removeItems(final ArrayList<ApplicationInfo> apps) { |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3444 | final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext()); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3445 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3446 | final HashSet<String> packageNames = new HashSet<String>(); |
| 3447 | final int appCount = apps.size(); |
| 3448 | for (int i = 0; i < appCount; i++) { |
| 3449 | packageNames.add(apps.get(i).componentName.getPackageName()); |
| 3450 | } |
| 3451 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3452 | ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); |
| 3453 | for (final CellLayout layoutParent: cellLayouts) { |
Winson Chung | 7a25a9e | 2011-01-30 13:33:56 -0800 | [diff] [blame] | 3454 | final ViewGroup layout = layoutParent.getChildrenLayout(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3455 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3456 | // Avoid ANRs by treating each screen separately |
| 3457 | post(new Runnable() { |
| 3458 | public void run() { |
| 3459 | final ArrayList<View> childrenToRemove = new ArrayList<View>(); |
| 3460 | childrenToRemove.clear(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3461 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3462 | int childCount = layout.getChildCount(); |
| 3463 | for (int j = 0; j < childCount; j++) { |
| 3464 | final View view = layout.getChildAt(j); |
| 3465 | Object tag = view.getTag(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3466 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3467 | if (tag instanceof ShortcutInfo) { |
| 3468 | final ShortcutInfo info = (ShortcutInfo) tag; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3469 | final Intent intent = info.intent; |
| 3470 | final ComponentName name = intent.getComponent(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3471 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3472 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 3473 | for (String packageName: packageNames) { |
| 3474 | if (packageName.equals(name.getPackageName())) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3475 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
| 3476 | childrenToRemove.add(view); |
| 3477 | } |
| 3478 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3479 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3480 | } else if (tag instanceof FolderInfo) { |
| 3481 | final FolderInfo info = (FolderInfo) tag; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3482 | final ArrayList<ShortcutInfo> contents = info.contents; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3483 | final int contentsCount = contents.size(); |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 3484 | final ArrayList<ShortcutInfo> appsToRemoveFromFolder = |
| 3485 | new ArrayList<ShortcutInfo>(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3486 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3487 | for (int k = 0; k < contentsCount; k++) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3488 | final ShortcutInfo appInfo = contents.get(k); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3489 | final Intent intent = appInfo.intent; |
| 3490 | final ComponentName name = intent.getComponent(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3491 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3492 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 3493 | for (String packageName: packageNames) { |
| 3494 | if (packageName.equals(name.getPackageName())) { |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 3495 | appsToRemoveFromFolder.add(appInfo); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3496 | } |
| 3497 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3498 | } |
| 3499 | } |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 3500 | for (ShortcutInfo item: appsToRemoveFromFolder) { |
| 3501 | info.remove(item); |
| 3502 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3503 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3504 | } else if (tag instanceof LauncherAppWidgetInfo) { |
| 3505 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag; |
| 3506 | final AppWidgetProviderInfo provider = |
| 3507 | widgets.getAppWidgetInfo(info.appWidgetId); |
Daniel Sandler | e1cc6c3 | 2010-05-07 11:49:29 -0400 | [diff] [blame] | 3508 | if (provider != null) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3509 | for (String packageName: packageNames) { |
| 3510 | if (packageName.equals(provider.provider.getPackageName())) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3511 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3512 | childrenToRemove.add(view); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3513 | } |
| 3514 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3515 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3516 | } |
| 3517 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3518 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3519 | childCount = childrenToRemove.size(); |
| 3520 | for (int j = 0; j < childCount; j++) { |
| 3521 | View child = childrenToRemove.get(j); |
Winson Chung | 7a25a9e | 2011-01-30 13:33:56 -0800 | [diff] [blame] | 3522 | // Note: We can not remove the view directly from CellLayoutChildren as this |
| 3523 | // does not re-mark the spaces as unoccupied. |
| 3524 | layoutParent.removeViewInLayout(child); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3525 | if (child instanceof DropTarget) { |
| 3526 | mDragController.removeDropTarget((DropTarget)child); |
| 3527 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 3528 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3529 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3530 | if (childCount > 0) { |
| 3531 | layout.requestLayout(); |
| 3532 | layout.invalidate(); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 3533 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3534 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 3535 | }); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3536 | } |
| 3537 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3538 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3539 | void updateShortcuts(ArrayList<ApplicationInfo> apps) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 3540 | ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); |
| 3541 | for (CellLayoutChildren layout: childrenLayouts) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3542 | int childCount = layout.getChildCount(); |
| 3543 | for (int j = 0; j < childCount; j++) { |
| 3544 | final View view = layout.getChildAt(j); |
| 3545 | Object tag = view.getTag(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3546 | if (tag instanceof ShortcutInfo) { |
| 3547 | ShortcutInfo info = (ShortcutInfo)tag; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3548 | // We need to check for ACTION_MAIN otherwise getComponent() might |
| 3549 | // return null for some shortcuts (for instance, for shortcuts to |
| 3550 | // web pages.) |
| 3551 | final Intent intent = info.intent; |
| 3552 | final ComponentName name = intent.getComponent(); |
| 3553 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3554 | Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 3555 | final int appCount = apps.size(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 3556 | for (int k = 0; k < appCount; k++) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 3557 | ApplicationInfo app = apps.get(k); |
| 3558 | if (app.componentName.equals(name)) { |
| 3559 | info.setIcon(mIconCache.getIcon(info.intent)); |
| 3560 | ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null, |
| 3561 | new FastBitmapDrawable(info.getIcon(mIconCache)), |
| 3562 | null, null); |
| 3563 | } |
| 3564 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 3571 | void moveToDefaultScreen(boolean animate) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 3572 | if (!isSmall()) { |
| 3573 | if (animate) { |
| 3574 | snapToPage(mDefaultPage); |
| 3575 | } else { |
| 3576 | setCurrentPage(mDefaultPage); |
| 3577 | } |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 3578 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3579 | getChildAt(mDefaultPage).requestFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3580 | } |
| 3581 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3582 | @Override |
| 3583 | public void syncPages() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3584 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 3585 | |
| 3586 | @Override |
| 3587 | public void syncPageItems(int page) { |
| 3588 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 3589 | |
| 3590 | @Override |
| 3591 | protected String getCurrentPageDescription() { |
| 3592 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 3593 | return String.format(mContext.getString(R.string.workspace_scroll_format), |
| 3594 | page + 1, getChildCount()); |
| 3595 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 3596 | |
| 3597 | public void getLocationInDragLayer(int[] loc) { |
| 3598 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 3599 | } |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 3600 | |
| 3601 | /** |
| 3602 | * Return true because we want the scrolling indicator to stretch to fit the space. |
| 3603 | */ |
| 3604 | protected boolean hasElasticScrollIndicator() { |
| 3605 | return true; |
| 3606 | } |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 3607 | |
| 3608 | void showDockDivider(boolean immediately) { |
| 3609 | final ViewGroup parent = (ViewGroup) getParent(); |
| 3610 | final View qsbDivider = (ImageView) (parent.findViewById(R.id.qsb_divider)); |
| 3611 | final View dockDivider = (ImageView) (parent.findViewById(R.id.dock_divider)); |
| 3612 | if (qsbDivider != null && dockDivider != null) { |
| 3613 | qsbDivider.setVisibility(View.VISIBLE); |
| 3614 | dockDivider.setVisibility(View.VISIBLE); |
| 3615 | if (mDividerAnimator != null) { |
| 3616 | mDividerAnimator.cancel(); |
| 3617 | mDividerAnimator = null; |
| 3618 | } |
| 3619 | if (immediately) { |
| 3620 | qsbDivider.setAlpha(1f); |
| 3621 | dockDivider.setAlpha(1f); |
| 3622 | } else { |
| 3623 | mDividerAnimator = new AnimatorSet(); |
| 3624 | mDividerAnimator.playTogether(ObjectAnimator.ofFloat(qsbDivider, "alpha", 1f), |
| 3625 | ObjectAnimator.ofFloat(dockDivider, "alpha", 1f)); |
| 3626 | mDividerAnimator.setDuration(sScrollIndicatorFadeInDuration); |
| 3627 | mDividerAnimator.start(); |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | void hideDockDivider(boolean immediately) { |
| 3633 | final ViewGroup parent = (ViewGroup) getParent(); |
| 3634 | final View qsbDivider = (ImageView) (parent.findViewById(R.id.qsb_divider)); |
| 3635 | final View dockDivider = (ImageView) (parent.findViewById(R.id.dock_divider)); |
| 3636 | if (qsbDivider != null && dockDivider != null) { |
| 3637 | if (mDividerAnimator != null) { |
| 3638 | mDividerAnimator.cancel(); |
| 3639 | mDividerAnimator = null; |
| 3640 | } |
| 3641 | if (immediately) { |
| 3642 | qsbDivider.setVisibility(View.GONE); |
| 3643 | dockDivider.setVisibility(View.GONE); |
| 3644 | qsbDivider.setAlpha(0f); |
| 3645 | dockDivider.setAlpha(0f); |
| 3646 | } else { |
| 3647 | mDividerAnimator = new AnimatorSet(); |
| 3648 | mDividerAnimator.playTogether(ObjectAnimator.ofFloat(qsbDivider, "alpha", 0f), |
| 3649 | ObjectAnimator.ofFloat(dockDivider, "alpha", 0f)); |
| 3650 | mDividerAnimator.addListener(new AnimatorListenerAdapter() { |
| 3651 | private boolean cancelled = false; |
| 3652 | @Override |
| 3653 | public void onAnimationCancel(android.animation.Animator animation) { |
| 3654 | cancelled = true; |
| 3655 | } |
| 3656 | @Override |
| 3657 | public void onAnimationEnd(android.animation.Animator animation) { |
| 3658 | if (!cancelled) { |
| 3659 | qsbDivider.setVisibility(View.GONE); |
| 3660 | dockDivider.setVisibility(View.GONE); |
| 3661 | } |
| 3662 | } |
| 3663 | }); |
| 3664 | mDividerAnimator.setDuration(sScrollIndicatorFadeOutDuration); |
| 3665 | mDividerAnimator.start(); |
| 3666 | } |
| 3667 | } |
| 3668 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3669 | } |