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 | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 20 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 22 | import android.animation.Animator.AnimatorListener; |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 23 | import android.animation.AnimatorSet; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 24 | import android.animation.ObjectAnimator; |
| 25 | import android.animation.PropertyValuesHolder; |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 26 | import android.app.WallpaperManager; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 27 | import android.appwidget.AppWidgetManager; |
| 28 | import android.appwidget.AppWidgetProviderInfo; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 29 | import android.content.ComponentName; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 33 | import android.content.pm.ProviderInfo; |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 34 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.content.res.TypedArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.graphics.Canvas; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 37 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.graphics.Rect; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 40 | import android.net.Uri; |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 41 | import android.os.IBinder; |
Adam Powell | 495f289 | 2010-04-16 16:40:55 -0700 | [diff] [blame] | 42 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.util.AttributeSet; |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 44 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 45 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | import android.view.View; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 47 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 49 | import java.util.ArrayList; |
| 50 | import java.util.HashSet; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 51 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | /** |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 53 | * The workspace is a wide area with a wallpaper and a finite number of pages. |
| 54 | * 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] | 55 | * 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] | 56 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 57 | public class Workspace extends SmoothPagedView |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 58 | implements DropTarget, DragSource, DragScroller, View.OnTouchListener { |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 59 | @SuppressWarnings({"UnusedDeclaration"}) |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 60 | private static final String TAG = "Launcher.Workspace"; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 61 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 62 | // This is how much the workspace shrinks when we enter all apps or |
| 63 | // customization mode |
| 64 | private static final float SHRINK_FACTOR = 0.16f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 65 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 66 | // Y rotation to apply to the workspace screens |
| 67 | private static final float WORKSPACE_ROTATION = 12.5f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 68 | |
| 69 | // These are extra scale factors to apply to the mini home screens |
| 70 | // so as to achieve the desired transform |
| 71 | private static final float EXTRA_SCALE_FACTOR_0 = 0.97f; |
| 72 | private static final float EXTRA_SCALE_FACTOR_1 = 1.0f; |
Adam Cohen | e54cec8 | 2010-09-13 18:19:55 -0700 | [diff] [blame] | 73 | private static final float EXTRA_SCALE_FACTOR_2 = 1.08f; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 74 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 75 | private static final int BACKGROUND_FADE_OUT_DELAY = 300; |
| 76 | private static final int BACKGROUND_FADE_OUT_DURATION = 300; |
| 77 | private static final int BACKGROUND_FADE_IN_DURATION = 100; |
| 78 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 79 | // These animators are used to fade the |
| 80 | private ObjectAnimator<Float> mBackgroundFadeIn; |
| 81 | private ObjectAnimator<Float> mBackgroundFadeOut; |
| 82 | private float mBackgroundAlpha = 0; |
| 83 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 84 | private enum ShrinkPosition { SHRINK_TO_TOP, SHRINK_TO_MIDDLE, SHRINK_TO_BOTTOM }; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 86 | private final WallpaperManager mWallpaperManager; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 87 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 88 | private int mDefaultPage; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 89 | |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 90 | private boolean mWaitingToShrinkToBottom = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 91 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | /** |
| 93 | * CellInfo for the cell that is currently being dragged |
| 94 | */ |
| 95 | private CellLayout.CellInfo mDragInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 96 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Target drop area calculated during last acceptDrop call. |
| 99 | */ |
| 100 | private int[] mTargetCell = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 102 | /** |
| 103 | * The CellLayout that is currently being dragged over |
| 104 | */ |
| 105 | private CellLayout mDragTargetLayout = null; |
| 106 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | private Launcher mLauncher; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 108 | private IconCache mIconCache; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 109 | private DragController mDragController; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | private int[] mTempCell = new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 112 | private int[] mTempEstimate = new int[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 113 | private float[] mTempOriginXY = new float[2]; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 114 | private float[] mTempDragCoordinates = new float[2]; |
| 115 | private float[] mTempDragBottomRightCoordinates = new float[2]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 116 | private Matrix mTempInverseMatrix = new Matrix(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 117 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 118 | private static final int DEFAULT_CELL_COUNT_X = 4; |
| 119 | private static final int DEFAULT_CELL_COUNT_Y = 4; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 121 | private Drawable mPreviousIndicator; |
| 122 | private Drawable mNextIndicator; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 123 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 124 | // 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] | 125 | // in all apps or customize mode) |
| 126 | private boolean mIsSmall; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 127 | |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 128 | private AnimatorListener mUnshrinkAnimationListener; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 129 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 130 | private boolean mInScrollArea = false; |
| 131 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Used to inflate the Workspace from XML. |
| 134 | * |
| 135 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 136 | * @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] | 137 | */ |
| 138 | public Workspace(Context context, AttributeSet attrs) { |
| 139 | this(context, attrs, 0); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Used to inflate the Workspace from XML. |
| 144 | * |
| 145 | * @param context The application's context. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 146 | * @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] | 147 | * @param defStyle Unused. |
| 148 | */ |
| 149 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 150 | super(context, attrs, defStyle); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 151 | mContentIsRefreshable = false; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 152 | |
| 153 | if (!LauncherApplication.isScreenXLarge()) { |
| 154 | mFadeInAdjacentScreens = false; |
| 155 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 156 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 157 | mWallpaperManager = WallpaperManager.getInstance(context); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 158 | |
| 159 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 160 | R.styleable.Workspace, defStyle, 0); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 161 | int cellCountX = a.getInt(R.styleable.Workspace_cellCountX, DEFAULT_CELL_COUNT_X); |
| 162 | int cellCountY = a.getInt(R.styleable.Workspace_cellCountY, DEFAULT_CELL_COUNT_Y); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 163 | mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | a.recycle(); |
| 165 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 166 | LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 167 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 168 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 169 | initWorkspace(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Initializes various states for this workspace. |
| 174 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 175 | protected void initWorkspace() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 176 | Context context = getContext(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 177 | mCurrentPage = mDefaultPage; |
| 178 | Launcher.setScreen(mCurrentPage); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 179 | LauncherApplication app = (LauncherApplication)context.getApplicationContext(); |
| 180 | mIconCache = app.getIconCache(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 182 | mUnshrinkAnimationListener = new AnimatorListener() { |
| 183 | public void onAnimationStart(Animator animation) {} |
| 184 | public void onAnimationEnd(Animator animation) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 185 | mIsSmall = false; |
| 186 | } |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 187 | public void onAnimationCancel(Animator animation) {} |
| 188 | public void onAnimationRepeat(Animator animation) {} |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 189 | }; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 190 | |
| 191 | mSnapVelocity = 600; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 192 | } |
| 193 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 194 | @Override |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 195 | protected int getScrollMode() { |
| 196 | if (LauncherApplication.isScreenXLarge()) { |
| 197 | return SmoothPagedView.QUINTIC_MODE; |
| 198 | } else { |
| 199 | return SmoothPagedView.OVERSHOOT_MODE; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 204 | public void addView(View child, int index, LayoutParams params) { |
| 205 | if (!(child instanceof CellLayout)) { |
| 206 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 207 | } |
| 208 | super.addView(child, index, params); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void addView(View child) { |
| 213 | if (!(child instanceof CellLayout)) { |
| 214 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 215 | } |
| 216 | super.addView(child); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void addView(View child, int index) { |
| 221 | if (!(child instanceof CellLayout)) { |
| 222 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 223 | } |
| 224 | super.addView(child, index); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void addView(View child, int width, int height) { |
| 229 | if (!(child instanceof CellLayout)) { |
| 230 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 231 | } |
| 232 | super.addView(child, width, height); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void addView(View child, LayoutParams params) { |
| 237 | if (!(child instanceof CellLayout)) { |
| 238 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 239 | } |
| 240 | super.addView(child, params); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @return The open folder on the current screen, or null if there is none |
| 245 | */ |
| 246 | Folder getOpenFolder() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 247 | CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); |
| 248 | int count = currentPage.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 249 | for (int i = 0; i < count; i++) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 250 | View child = currentPage.getChildAt(i); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 251 | if (child instanceof Folder) { |
| 252 | Folder folder = (Folder) child; |
| 253 | if (folder.getInfo().opened) |
| 254 | return folder; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | return null; |
| 258 | } |
| 259 | |
| 260 | ArrayList<Folder> getOpenFolders() { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 261 | final int screenCount = getChildCount(); |
| 262 | ArrayList<Folder> folders = new ArrayList<Folder>(screenCount); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 263 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 264 | for (int screen = 0; screen < screenCount; screen++) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 265 | CellLayout currentPage = (CellLayout) getChildAt(screen); |
| 266 | int count = currentPage.getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 267 | for (int i = 0; i < count; i++) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 268 | View child = currentPage.getChildAt(i); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 269 | if (child instanceof Folder) { |
| 270 | Folder folder = (Folder) child; |
| 271 | if (folder.getInfo().opened) |
| 272 | folders.add(folder); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return folders; |
| 279 | } |
| 280 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 281 | boolean isDefaultPageShowing() { |
| 282 | return mCurrentPage == mDefaultPage; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 286 | * Sets the current screen. |
| 287 | * |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 288 | * @param currentPage |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 289 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 290 | @Override |
| 291 | void setCurrentPage(int currentPage) { |
| 292 | super.setCurrentPage(currentPage); |
| 293 | updateWallpaperOffset(mScrollX); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 297 | * Adds the specified child in the specified screen. The position and dimension of |
| 298 | * the child are defined by x, y, spanX and spanY. |
| 299 | * |
| 300 | * @param child The child to add in one of the workspace's screens. |
| 301 | * @param screen The screen in which to add the child. |
| 302 | * @param x The X position of the child in the screen's grid. |
| 303 | * @param y The Y position of the child in the screen's grid. |
| 304 | * @param spanX The number of cells spanned horizontally by the child. |
| 305 | * @param spanY The number of cells spanned vertically by the child. |
| 306 | */ |
| 307 | void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) { |
| 308 | addInScreen(child, screen, x, y, spanX, spanY, false); |
| 309 | } |
| 310 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 311 | void addInFullScreen(View child, int screen) { |
| 312 | addInScreen(child, screen, 0, 0, -1, -1); |
| 313 | } |
| 314 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 315 | /** |
| 316 | * Adds the specified child in the specified screen. The position and dimension of |
| 317 | * the child are defined by x, y, spanX and spanY. |
| 318 | * |
| 319 | * @param child The child to add in one of the workspace's screens. |
| 320 | * @param screen The screen in which to add the child. |
| 321 | * @param x The X position of the child in the screen's grid. |
| 322 | * @param y The Y position of the child in the screen's grid. |
| 323 | * @param spanX The number of cells spanned horizontally by the child. |
| 324 | * @param spanY The number of cells spanned vertically by the child. |
| 325 | * @param insert When true, the child is inserted at the beginning of the children list. |
| 326 | */ |
| 327 | void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) { |
| 328 | if (screen < 0 || screen >= getChildCount()) { |
Daniel Sandler | 291ad12 | 2010-05-24 16:03:53 -0400 | [diff] [blame] | 329 | Log.e(TAG, "The screen must be >= 0 and < " + getChildCount() |
| 330 | + " (was " + screen + "); skipping child"); |
| 331 | return; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | final CellLayout group = (CellLayout) getChildAt(screen); |
| 335 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 336 | if (lp == null) { |
| 337 | lp = new CellLayout.LayoutParams(x, y, spanX, spanY); |
| 338 | } else { |
| 339 | lp.cellX = x; |
| 340 | lp.cellY = y; |
| 341 | lp.cellHSpan = spanX; |
| 342 | lp.cellVSpan = spanY; |
| 343 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 344 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 345 | // Get the canonical child id to uniquely represent this view in this screen |
Michael Jurka | 845ba3b | 2010-09-28 17:09:46 -0700 | [diff] [blame] | 346 | int childId = LauncherModel.getCellLayoutChildId(-1, screen, x, y, spanX, spanY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 347 | if (!group.addViewToCellLayout(child, insert ? 0 : -1, childId, lp)) { |
| 348 | // TODO: This branch occurs when the workspace is adding views |
| 349 | // outside of the defined grid |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 350 | // maybe we should be deleting these items from the LauncherModel? |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 351 | Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); |
| 352 | } |
| 353 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 354 | if (!(child instanceof Folder)) { |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 355 | child.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 356 | child.setOnLongClickListener(mLongClickListener); |
| 357 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 358 | if (child instanceof DropTarget) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 359 | mDragController.addDropTarget((DropTarget) child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 360 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 363 | public boolean onTouch(View v, MotionEvent event) { |
| 364 | // this is an intercepted event being forwarded from a cell layout |
| 365 | if (mIsSmall) { |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 366 | mLauncher.onWorkspaceClick((CellLayout) v); |
Michael Jurka | 9622622 | 2010-08-20 00:54:40 -0700 | [diff] [blame] | 367 | return true; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 368 | } |
| 369 | return false; |
Michael Jurka | 0e26059 | 2010-06-30 17:07:39 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 372 | @Override |
| 373 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 374 | if (mIsSmall) { |
| 375 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 376 | return false; |
| 377 | } |
| 378 | return super.dispatchUnhandledMove(focused, direction); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 383 | if (mIsSmall) { |
| 384 | // when the home screens are shrunken, shouldn't allow side-scrolling |
| 385 | return false; |
| 386 | } |
| 387 | return super.onInterceptTouchEvent(ev); |
| 388 | } |
| 389 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 390 | protected void onPageBeginMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 391 | if (mNextPage != INVALID_PAGE) { |
| 392 | // we're snapping to a particular screen |
Michael Jurka | 16fed41 | 2010-10-01 16:12:03 -0700 | [diff] [blame] | 393 | enableChildrenCache(mCurrentPage, mNextPage); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 394 | } else { |
| 395 | // this is when user is actively dragging a particular screen, they might |
| 396 | // swipe it either left or right (but we won't advance by more than one screen) |
Michael Jurka | 16fed41 | 2010-10-01 16:12:03 -0700 | [diff] [blame] | 397 | enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 398 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 399 | showOutlines(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 402 | protected void onPageEndMoving() { |
Michael Jurka | 16fed41 | 2010-10-01 16:12:03 -0700 | [diff] [blame] | 403 | clearChildrenCache(); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 404 | |
| 405 | // Hide the outlines, as long as we're not dragging |
| 406 | if (!mDragController.dragging()) { |
| 407 | hideOutlines(); |
| 408 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
| 412 | protected void notifyPageSwitchListener() { |
| 413 | super.notifyPageSwitchListener(); |
| 414 | |
| 415 | if (mPreviousIndicator != null) { |
| 416 | // if we know the next page, we show the indication for it right away; it looks |
| 417 | // weird if the indicators are lagging |
| 418 | int page = mNextPage; |
| 419 | if (page == INVALID_PAGE) { |
| 420 | page = mCurrentPage; |
| 421 | } |
| 422 | mPreviousIndicator.setLevel(page); |
| 423 | mNextIndicator.setLevel(page); |
| 424 | } |
| 425 | Launcher.setScreen(mCurrentPage); |
| 426 | }; |
| 427 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 428 | private void updateWallpaperOffset() { |
Romain Guy | 798300c | 2009-08-11 14:43:29 -0700 | [diff] [blame] | 429 | updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft)); |
| 430 | } |
| 431 | |
| 432 | private void updateWallpaperOffset(int scrollRange) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 433 | final boolean isStaticWallpaper = (mWallpaperManager != null) && |
| 434 | (mWallpaperManager.getWallpaperInfo() == null); |
| 435 | if (LauncherApplication.isScreenXLarge() && !isStaticWallpaper) { |
| 436 | IBinder token = getWindowToken(); |
| 437 | if (token != null) { |
| 438 | mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 ); |
| 439 | mWallpaperManager.setWallpaperOffsets(getWindowToken(), |
| 440 | Math.max(0.f, Math.min(mScrollX/(float)scrollRange, 1.f)), 0); |
| 441 | } |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 442 | } |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 443 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 444 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 445 | private float getScaleXForRotation(float degrees) { |
| 446 | return (float) (1.0f / Math.cos(Math.PI * degrees / 180.0f)); |
| 447 | } |
| 448 | |
| 449 | public void showOutlines() { |
| 450 | if (mBackgroundFadeOut != null) mBackgroundFadeOut.cancel(); |
| 451 | if (mBackgroundFadeIn != null) mBackgroundFadeIn.cancel(); |
| 452 | mBackgroundFadeIn = new ObjectAnimator<Float>(BACKGROUND_FADE_IN_DURATION, this, |
| 453 | new PropertyValuesHolder<Float>("backgroundAlpha", 1.0f)); |
| 454 | mBackgroundFadeIn.start(); |
| 455 | } |
| 456 | |
| 457 | public void hideOutlines() { |
| 458 | if (mBackgroundFadeIn != null) mBackgroundFadeIn.cancel(); |
| 459 | if (mBackgroundFadeOut != null) mBackgroundFadeOut.cancel(); |
| 460 | mBackgroundFadeOut = new ObjectAnimator<Float>(BACKGROUND_FADE_OUT_DURATION, this, |
| 461 | new PropertyValuesHolder<Float>("backgroundAlpha", 0.0f)); |
| 462 | mBackgroundFadeOut.setStartDelay(BACKGROUND_FADE_OUT_DELAY); |
| 463 | mBackgroundFadeOut.start(); |
| 464 | } |
| 465 | |
| 466 | public void setBackgroundAlpha(float alpha) { |
| 467 | mBackgroundAlpha = alpha; |
| 468 | for (int i = 0; i < getChildCount(); i++) { |
| 469 | CellLayout cl = (CellLayout) getChildAt(i); |
| 470 | cl.setBackgroundAlpha(alpha); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | public float getBackgroundAlpha() { |
| 475 | return mBackgroundAlpha; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | protected void screenScrolled(int screenCenter) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 480 | CellLayout cur = (CellLayout) getChildAt(mCurrentPage); |
| 481 | CellLayout toRight = (CellLayout) getChildAt(mCurrentPage + 1); |
| 482 | CellLayout toLeft = (CellLayout) getChildAt(mCurrentPage - 1); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 483 | |
| 484 | for (int i = 0; i < mCurrentPage - 1; i++) { |
| 485 | View v = getChildAt(i); |
| 486 | if (v != null) { |
| 487 | v.setRotationY(WORKSPACE_ROTATION); |
| 488 | v.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION)); |
| 489 | } |
| 490 | } |
| 491 | for (int i = mCurrentPage + 1; i < getChildCount(); i++) { |
| 492 | View v = getChildAt(i); |
| 493 | if (v != null) { |
| 494 | v.setRotationY(-WORKSPACE_ROTATION); |
| 495 | v.setScaleX(getScaleXForRotation(-WORKSPACE_ROTATION)); |
| 496 | } |
| 497 | } |
| 498 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 499 | int halfScreenSize = getMeasuredWidth() / 2; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 500 | int pageWidth = cur.getMeasuredWidth(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 501 | int delta = screenCenter - (getChildOffset(mCurrentPage) - |
| 502 | getRelativeChildOffset(mCurrentPage) + halfScreenSize); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 503 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 504 | float scrollProgress = Math.abs(delta/(pageWidth*1.0f + mPageSpacing)); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 505 | boolean scrollRight = (delta <= 0); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 506 | |
| 507 | float rotation; |
| 508 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 509 | if (scrollRight) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 510 | rotation = -scrollProgress * WORKSPACE_ROTATION; |
| 511 | cur.setRotationY(rotation); |
| 512 | cur.setScaleX(getScaleXForRotation(rotation)); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 513 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 514 | if (toLeft != null) { |
| 515 | rotation = WORKSPACE_ROTATION * (1 - scrollProgress); |
| 516 | toLeft.setRotationY(rotation); |
| 517 | toLeft.setScaleX(getScaleXForRotation(rotation)); |
| 518 | } |
| 519 | if (toRight != null) { |
| 520 | toRight.setRotationY(-WORKSPACE_ROTATION); |
| 521 | toRight.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION)); |
| 522 | } |
| 523 | } else { |
| 524 | rotation = scrollProgress * WORKSPACE_ROTATION; |
| 525 | cur.setRotationY(rotation); |
| 526 | cur.setScaleX(getScaleXForRotation(rotation)); |
| 527 | |
| 528 | if (toRight != null) { |
| 529 | rotation = -WORKSPACE_ROTATION * (1 - scrollProgress); |
| 530 | toRight.setRotationY(rotation); |
| 531 | toRight.setScaleX(getScaleXForRotation(rotation)); |
| 532 | } |
| 533 | if (toLeft != null) { |
| 534 | toLeft.setRotationY(WORKSPACE_ROTATION); |
| 535 | toLeft.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION)); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 540 | protected void onAttachedToWindow() { |
| 541 | super.onAttachedToWindow(); |
Joe Onorato | 956091b | 2010-02-19 12:47:40 -0800 | [diff] [blame] | 542 | computeScroll(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 543 | mDragController.setWindowToken(getWindowToken()); |
| 544 | } |
| 545 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 546 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 547 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 548 | super.onLayout(changed, left, top, right, bottom); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 549 | |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 550 | // if shrinkToBottom() is called on initialization, it has to be deferred |
| 551 | // until after the first call to onLayout so that it has the correct width |
| 552 | if (mWaitingToShrinkToBottom) { |
| 553 | shrinkToBottom(false); |
| 554 | mWaitingToShrinkToBottom = false; |
| 555 | } |
| 556 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 557 | if (LauncherApplication.isInPlaceRotationEnabled()) { |
| 558 | // When the device is rotated, the scroll position of the current screen |
| 559 | // needs to be refreshed |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 560 | setCurrentPage(getCurrentPage()); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 561 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 565 | protected void dispatchDraw(Canvas canvas) { |
| 566 | if (mIsSmall) { |
| 567 | // Draw all the workspaces if we're small |
| 568 | final int pageCount = getChildCount(); |
| 569 | final long drawingTime = getDrawingTime(); |
| 570 | for (int i = 0; i < pageCount; i++) { |
| 571 | final View page = (View) getChildAt(i); |
| 572 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 573 | drawChild(canvas, page, drawingTime); |
| 574 | } |
| 575 | } else { |
| 576 | super.dispatchDraw(canvas); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 577 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | @Override |
| 581 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 582 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 583 | final Folder openFolder = getOpenFolder(); |
| 584 | if (openFolder != null) { |
| 585 | return openFolder.requestFocus(direction, previouslyFocusedRect); |
| 586 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 587 | return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | @Override |
Romain Guy | c2e24c0 | 2009-06-01 16:11:41 -0700 | [diff] [blame] | 594 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 595 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 596 | final Folder openFolder = getOpenFolder(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 597 | if (openFolder != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 598 | openFolder.addFocusables(views, direction); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 599 | } else { |
| 600 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | @Override |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 606 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 607 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
Michael Jurka | 54dd754 | 2010-07-30 14:47:52 -0700 | [diff] [blame] | 608 | // (In XLarge mode, the workspace is shrunken below all apps, and responds to taps |
| 609 | // ie when you click on a mini-screen, it zooms back to that screen) |
Joe Onorato | 6b4adbc | 2010-09-01 12:13:25 -0700 | [diff] [blame] | 610 | if (!LauncherApplication.isScreenXLarge() && mLauncher.isAllAppsVisible()) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | } |
| 614 | return super.dispatchTouchEvent(ev); |
| 615 | } |
| 616 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 617 | void enableChildrenCache(int fromPage, int toPage) { |
| 618 | if (fromPage > toPage) { |
| 619 | final int temp = fromPage; |
| 620 | fromPage = toPage; |
| 621 | toPage = temp; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 622 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 623 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 624 | final int screenCount = getChildCount(); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 625 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 626 | fromPage = Math.max(fromPage, 0); |
| 627 | toPage = Math.min(toPage, screenCount - 1); |
Adam Powell | fea5d02 | 2010-04-29 11:42:45 -0700 | [diff] [blame] | 628 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 629 | for (int i = fromPage; i <= toPage; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 630 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 631 | layout.setChildrenDrawnWithCacheEnabled(true); |
| 632 | layout.setChildrenDrawingCacheEnabled(true); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void clearChildrenCache() { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 637 | final int screenCount = getChildCount(); |
| 638 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 639 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 640 | layout.setChildrenDrawnWithCacheEnabled(false); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public boolean onTouchEvent(MotionEvent ev) { |
Joe Onorato | 2bc6b7c | 2009-10-01 14:08:30 -0700 | [diff] [blame] | 646 | if (mLauncher.isAllAppsVisible()) { |
| 647 | // Cancel any scrolling that is in progress. |
| 648 | if (!mScroller.isFinished()) { |
| 649 | mScroller.abortAnimation(); |
| 650 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 651 | snapToPage(mCurrentPage); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 652 | return false; // We don't want the events. Let them fall through to the all apps view. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 655 | return super.onTouchEvent(ev); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 656 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 657 | |
Michael Jurka | 01f0ed4 | 2010-08-20 00:41:17 -0700 | [diff] [blame] | 658 | public boolean isSmall() { |
| 659 | return mIsSmall; |
| 660 | } |
| 661 | |
Patrick Dubroy | 6b509c1 | 2010-08-23 15:08:16 -0700 | [diff] [blame] | 662 | void shrinkToTop(boolean animated) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 663 | shrink(ShrinkPosition.SHRINK_TO_TOP, animated); |
Michael Jurka | 01f0ed4 | 2010-08-20 00:41:17 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void shrinkToMiddle() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 667 | shrink(ShrinkPosition.SHRINK_TO_MIDDLE, true); |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void shrinkToBottom() { |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 671 | shrinkToBottom(true); |
| 672 | } |
| 673 | |
| 674 | void shrinkToBottom(boolean animated) { |
| 675 | if (mFirstLayout) { |
| 676 | // (mFirstLayout == "first layout has not happened yet") |
| 677 | // if we get a call to shrink() as part of our initialization (for example, if |
| 678 | // Launcher is started in All Apps mode) then we need to wait for a layout call |
| 679 | // to get our width so we can layout the mini-screen views correctly |
| 680 | mWaitingToShrinkToBottom = true; |
| 681 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 682 | shrink(ShrinkPosition.SHRINK_TO_BOTTOM, animated); |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 683 | } |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 686 | private float getYScaleForScreen(int screen) { |
| 687 | int x = Math.abs(screen - 2); |
| 688 | |
| 689 | // TODO: This should be generalized for use with arbitrary rotation angles. |
| 690 | switch(x) { |
| 691 | case 0: return EXTRA_SCALE_FACTOR_0; |
| 692 | case 1: return EXTRA_SCALE_FACTOR_1; |
| 693 | case 2: return EXTRA_SCALE_FACTOR_2; |
| 694 | } |
| 695 | return 1.0f; |
| 696 | } |
| 697 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 698 | // we use this to shrink the workspace for the all apps view and the customize view |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 699 | private void shrink(ShrinkPosition shrinkPosition, boolean animated) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 700 | mIsSmall = true; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 701 | // we intercept and reject all touch events when we're small, so be sure to reset the state |
| 702 | mTouchState = TOUCH_STATE_REST; |
| 703 | mActivePointerId = INVALID_POINTER; |
| 704 | |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 705 | final Resources res = getResources(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 706 | final int screenWidth = getWidth(); |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 707 | final int screenHeight = getHeight(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 708 | |
| 709 | // Making the assumption that all pages have the same width as the 0th |
| 710 | final int pageWidth = getChildAt(0).getMeasuredWidth(); |
| 711 | final int pageHeight = getChildAt(0).getMeasuredHeight(); |
| 712 | |
| 713 | final int scaledPageWidth = (int) (SHRINK_FACTOR * pageWidth); |
| 714 | final int scaledPageHeight = (int) (SHRINK_FACTOR * pageHeight); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 715 | final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 716 | |
| 717 | final int screenCount = getChildCount(); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 718 | float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing; |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 719 | |
| 720 | float newY = getResources().getDimension(R.dimen.smallScreenVerticalMargin); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 721 | if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM) { |
| 722 | newY = screenHeight - newY - scaledPageHeight; |
| 723 | } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_MIDDLE) { |
| 724 | newY = screenHeight / 2 - scaledPageHeight / 2; |
Michael Jurka | 213d963 | 2010-07-28 11:29:25 -0700 | [diff] [blame] | 725 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 726 | |
| 727 | // We animate all the screens to the centered position in workspace |
| 728 | // At the same time, the screens become greyed/dimmed |
| 729 | |
| 730 | // newX is initialized to the left-most position of the centered screens |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 731 | float newX = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 732 | |
| 733 | // We are going to scale about the center of the view, so we need to adjust the positions |
| 734 | // of the views accordingly |
| 735 | newX -= (pageWidth - scaledPageWidth) / 2.0f; |
| 736 | newY -= (pageHeight - scaledPageHeight) / 2.0f; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 737 | for (int i = 0; i < screenCount; i++) { |
| 738 | CellLayout cl = (CellLayout) getChildAt(i); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 739 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 740 | float rotation = (-i + 2) * WORKSPACE_ROTATION; |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 741 | float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f)); |
| 742 | float rotationScaleY = getYScaleForScreen(i); |
| 743 | |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 744 | if (animated) { |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 745 | final int duration = res.getInteger(R.integer.config_workspaceShrinkTime); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 746 | new ObjectAnimator<Float>(duration, cl, |
| 747 | new PropertyValuesHolder<Float>("x", newX), |
| 748 | new PropertyValuesHolder<Float>("y", newY), |
Adam Cohen | e54cec8 | 2010-09-13 18:19:55 -0700 | [diff] [blame] | 749 | new PropertyValuesHolder<Float>("scaleX", SHRINK_FACTOR * rotationScaleX), |
| 750 | new PropertyValuesHolder<Float>("scaleY", SHRINK_FACTOR * rotationScaleY), |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 751 | new PropertyValuesHolder<Float>("backgroundAlpha", 1.0f), |
Michael Jurka | 16fed41 | 2010-10-01 16:12:03 -0700 | [diff] [blame] | 752 | new PropertyValuesHolder<Float>("alpha", 1.0f), |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 753 | new PropertyValuesHolder<Float>("rotationY", rotation)).start(); |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 754 | } else { |
| 755 | cl.setX((int)newX); |
| 756 | cl.setY((int)newY); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 757 | cl.setScaleX(SHRINK_FACTOR * rotationScaleX); |
| 758 | cl.setScaleY(SHRINK_FACTOR * rotationScaleY); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 759 | cl.setBackgroundAlpha(1.0f); |
| 760 | cl.setAlpha(0.0f); |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 761 | cl.setRotationY(rotation); |
Michael Jurka | 79212d8 | 2010-07-30 16:36:20 -0700 | [diff] [blame] | 762 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 763 | // increment newX for the next screen |
Adam Cohen | a985e59 | 2010-09-09 11:23:48 -0700 | [diff] [blame] | 764 | newX += scaledPageWidth + extraScaledSpacing; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 765 | cl.setOnInterceptTouchListener(this); |
| 766 | } |
| 767 | setChildrenDrawnWithCacheEnabled(true); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // We call this when we trigger an unshrink by clicking on the CellLayout cl |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 771 | public void unshrink(CellLayout clThatWasClicked) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 772 | int newCurrentPage = mCurrentPage; |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 773 | final int screenCount = getChildCount(); |
| 774 | for (int i = 0; i < screenCount; i++) { |
| 775 | if (getChildAt(i) == clThatWasClicked) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 776 | newCurrentPage = i; |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 777 | } |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 778 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 779 | unshrink(newCurrentPage); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 782 | private void unshrink(int newCurrentPage) { |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 783 | if (mIsSmall) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 784 | int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage); |
| 785 | int delta = newX - mScrollX; |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 786 | |
| 787 | final int screenCount = getChildCount(); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 788 | for (int i = 0; i < screenCount; i++) { |
| 789 | CellLayout cl = (CellLayout) getChildAt(i); |
| 790 | cl.setX(cl.getX() + delta); |
| 791 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 792 | snapToPage(newCurrentPage); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 793 | unshrink(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 794 | |
| 795 | setCurrentPage(newCurrentPage); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 796 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 799 | void unshrink() { |
| 800 | unshrink(true); |
| 801 | } |
| 802 | |
| 803 | void unshrink(boolean animated) { |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 804 | if (mIsSmall) { |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 805 | AnimatorSet s = new AnimatorSet(); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 806 | final int screenCount = getChildCount(); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 807 | |
| 808 | final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime); |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 809 | for (int i = 0; i < screenCount; i++) { |
Patrick Dubroy | 7247f63 | 2010-08-04 16:02:59 -0700 | [diff] [blame] | 810 | final CellLayout cl = (CellLayout)getChildAt(i); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 811 | float finalAlphaValue = (i == mCurrentPage) ? 1.0f : 0.0f; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 812 | float rotation = 0.0f; |
| 813 | |
| 814 | if (i < mCurrentPage) { |
| 815 | rotation = WORKSPACE_ROTATION; |
| 816 | } else if (i > mCurrentPage) { |
| 817 | rotation = -WORKSPACE_ROTATION; |
| 818 | } |
| 819 | |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 820 | if (animated) { |
| 821 | s.playTogether( |
Chet Haase | b1254a6 | 2010-09-07 13:35:00 -0700 | [diff] [blame] | 822 | new ObjectAnimator<Float>(duration, cl, "translationX", 0.0f), |
| 823 | new ObjectAnimator<Float>(duration, cl, "translationY", 0.0f), |
| 824 | new ObjectAnimator<Float>(duration, cl, "scaleX", 1.0f), |
| 825 | new ObjectAnimator<Float>(duration, cl, "scaleY", 1.0f), |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 826 | new ObjectAnimator<Float>(duration, cl, "backgroundAlpha", 0.0f), |
| 827 | new ObjectAnimator<Float>(duration, cl, "alpha", finalAlphaValue), |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 828 | new ObjectAnimator<Float>(duration, cl, "rotationY", rotation)); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 829 | } else { |
| 830 | cl.setTranslationX(0.0f); |
| 831 | cl.setTranslationY(0.0f); |
| 832 | cl.setScaleX(1.0f); |
| 833 | cl.setScaleY(1.0f); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 834 | cl.setBackgroundAlpha(0.0f); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 835 | cl.setAlpha(finalAlphaValue); |
| 836 | cl.setRotationY(rotation); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 837 | } |
Michael Jurka | b0f28bd | 2010-07-30 11:58:59 -0700 | [diff] [blame] | 838 | } |
| 839 | s.addListener(mUnshrinkAnimationListener); |
| 840 | s.start(); |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 841 | } |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 842 | } |
| 843 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 844 | void startDrag(CellLayout.CellInfo cellInfo) { |
| 845 | View child = cellInfo.cell; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 846 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 847 | // 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] | 848 | if (!child.isInTouchMode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 849 | return; |
| 850 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 851 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 852 | mDragInfo = cellInfo; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 853 | mDragInfo.screen = mCurrentPage; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 854 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 855 | CellLayout current = ((CellLayout) getChildAt(mCurrentPage)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 856 | |
| 857 | current.onDragChild(child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 858 | mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 859 | current.onDragEnter(child); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 860 | invalidate(); |
| 861 | } |
| 862 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 863 | void addApplicationShortcut(ShortcutInfo info, int screen, int cellX, int cellY, |
| 864 | boolean insertAtFirst, int intersectX, int intersectY) { |
| 865 | final CellLayout cellLayout = (CellLayout) getChildAt(screen); |
| 866 | View view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info); |
| 867 | |
| 868 | final int[] cellXY = new int[2]; |
| 869 | cellLayout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY); |
| 870 | addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst); |
| 871 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, |
| 872 | LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, |
| 873 | cellXY[0], cellXY[1]); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 874 | } |
| 875 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 876 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 877 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 878 | DragView dragView, Object dragInfo) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 879 | CellLayout cellLayout; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 880 | int originX = x - xOffset; |
| 881 | int originY = y - yOffset; |
| 882 | if (mIsSmall) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 883 | cellLayout = findMatchingPageForDragOver(dragView, originX, originY); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 884 | if (cellLayout == null) { |
| 885 | // cancel the drag if we're not over a mini-screen at time of drop |
| 886 | // TODO: maybe add a nice fade here? |
| 887 | return; |
| 888 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 889 | // get originX and originY in the local coordinate system of the screen |
| 890 | mTempOriginXY[0] = originX; |
| 891 | mTempOriginXY[1] = originY; |
| 892 | mapPointGlobalToLocal(cellLayout, mTempOriginXY); |
| 893 | originX = (int)mTempOriginXY[0]; |
| 894 | originY = (int)mTempOriginXY[1]; |
| 895 | } else { |
| 896 | cellLayout = getCurrentDropLayout(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 897 | } |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 898 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 899 | if (source != this) { |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 900 | onDropExternal(originX, originY, dragInfo, cellLayout); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 901 | } else { |
| 902 | // Move internally |
| 903 | if (mDragInfo != null) { |
| 904 | final View cell = mDragInfo.cell; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 905 | |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 906 | mTargetCell = findNearestVacantArea(originX, originY, |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 907 | mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, |
| 908 | mTargetCell); |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 909 | |
| 910 | int screen = indexOfChild(cellLayout); |
| 911 | if (screen != mDragInfo.screen) { |
| 912 | final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 913 | originalCellLayout.removeView(cell); |
| 914 | addInScreen(cell, screen, mTargetCell[0], mTargetCell[1], |
| 915 | mDragInfo.spanX, mDragInfo.spanY); |
| 916 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 917 | cellLayout.onDropChild(cell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 918 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 919 | // update the item's position after drop |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 920 | final ItemInfo info = (ItemInfo) cell.getTag(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 921 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
| 922 | cellLayout.onMove(cell, mTargetCell[0], mTargetCell[1]); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 923 | lp.cellX = mTargetCell[0]; |
| 924 | lp.cellY = mTargetCell[1]; |
Michael Jurka | 60587ef | 2010-09-30 11:06:13 -0700 | [diff] [blame] | 925 | cell.setId(LauncherModel.getCellLayoutChildId(-1, mDragInfo.screen, |
Michael Jurka | 845ba3b | 2010-09-28 17:09:46 -0700 | [diff] [blame] | 926 | mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY)); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 927 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 928 | LauncherModel.moveItemInDatabase(mLauncher, info, |
Michael Jurka | c6ee42e | 2010-09-30 12:04:50 -0700 | [diff] [blame] | 929 | LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 930 | lp.cellX, lp.cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 935 | public void onDragEnter(DragSource source, int x, int y, int xOffset, |
| 936 | int yOffset, DragView dragView, Object dragInfo) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 937 | getCurrentDropLayout().onDragEnter(dragView); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 938 | showOutlines(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 941 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 942 | DragView dragView, Object dragInfo) { |
| 943 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 944 | if (mIsSmall) { |
| 945 | // If we're shrunken, don't let anyone drag on folders/etc that are on the mini-screens |
| 946 | return null; |
| 947 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 948 | // We may need to delegate the drag to a child view. If a 1x1 item |
| 949 | // would land in a cell occupied by a DragTarget (e.g. a Folder), |
| 950 | // then drag events should be handled by that child. |
| 951 | |
| 952 | ItemInfo item = (ItemInfo)dragInfo; |
| 953 | CellLayout currentLayout = getCurrentDropLayout(); |
| 954 | |
| 955 | int dragPointX, dragPointY; |
| 956 | if (item.spanX == 1 && item.spanY == 1) { |
| 957 | // For a 1x1, calculate the drop cell exactly as in onDragOver |
| 958 | dragPointX = x - xOffset; |
| 959 | dragPointY = y - yOffset; |
| 960 | } else { |
| 961 | // Otherwise, use the exact drag coordinates |
| 962 | dragPointX = x; |
| 963 | dragPointY = y; |
| 964 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 965 | dragPointX += mScrollX - currentLayout.getLeft(); |
| 966 | dragPointY += mScrollY - currentLayout.getTop(); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 967 | |
| 968 | // If we are dragging over a cell that contains a DropTarget that will |
| 969 | // accept the drop, delegate to that DropTarget. |
| 970 | final int[] cellXY = mTempCell; |
| 971 | currentLayout.estimateDropCell(dragPointX, dragPointY, item.spanX, item.spanY, cellXY); |
| 972 | View child = currentLayout.getChildAt(cellXY[0], cellXY[1]); |
| 973 | if (child instanceof DropTarget) { |
| 974 | DropTarget target = (DropTarget)child; |
| 975 | if (target.acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) { |
| 976 | return target; |
| 977 | } |
| 978 | } |
| 979 | return null; |
| 980 | } |
| 981 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 982 | private void mapPointGlobalToLocal(View v, float[] xy) { |
| 983 | xy[0] = xy[0] + mScrollX - v.getLeft(); |
| 984 | xy[1] = xy[1] + mScrollY - v.getTop(); |
| 985 | v.getMatrix().invert(mTempInverseMatrix); |
| 986 | mTempInverseMatrix.mapPoints(xy); |
| 987 | } |
| 988 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 989 | // xy = upper left corner of item being dragged |
| 990 | // bottomRightXy = lower right corner of item being dragged |
| 991 | // This method will see which mini-screen is most overlapped by the item being dragged, and |
| 992 | // return it. It will also transform the parameters xy and bottomRightXy into the local |
| 993 | // coordinate space of the returned screen |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 994 | private CellLayout findMatchingPageForDragOver(DragView dragView, int originX, int originY) { |
| 995 | float x = originX + dragView.getScaledDragRegionXOffset(); |
| 996 | float y = originY + dragView.getScaledDragRegionYOffset(); |
| 997 | float right = x + dragView.getScaledDragRegionWidth(); |
| 998 | float bottom = y + dragView.getScaledDragRegionHeight(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 999 | |
| 1000 | // We loop through all the screens (ie CellLayouts) and see which one overlaps the most |
| 1001 | // with the item being dragged. |
| 1002 | final int screenCount = getChildCount(); |
| 1003 | CellLayout bestMatchingScreen = null; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1004 | float smallestDistSoFar = Float.MAX_VALUE; |
| 1005 | final float[] xy = mTempDragCoordinates; |
| 1006 | final float[] bottomRightXy = mTempDragBottomRightCoordinates; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1007 | for (int i = 0; i < screenCount; i++) { |
| 1008 | CellLayout cl = (CellLayout)getChildAt(i); |
| 1009 | // Transform the coordinates of the item being dragged to the CellLayout's coordinates |
| 1010 | float left = cl.getLeft(); |
| 1011 | float top = cl.getTop(); |
| 1012 | xy[0] = x + mScrollX - left; |
| 1013 | xy[1] = y + mScrollY - top; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1014 | |
| 1015 | bottomRightXy[0] = right + mScrollX - left; |
| 1016 | bottomRightXy[1] = bottom + mScrollY - top; |
| 1017 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1018 | cl.getMatrix().invert(mTempInverseMatrix); |
| 1019 | mTempInverseMatrix.mapPoints(xy); |
| 1020 | mTempInverseMatrix.mapPoints(bottomRightXy); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1021 | |
| 1022 | float dragRegionX = xy[0]; |
| 1023 | float dragRegionY = xy[1]; |
| 1024 | float dragRegionRight = bottomRightXy[0]; |
| 1025 | float dragRegionBottom = bottomRightXy[1]; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1026 | float dragRegionCenterX = (dragRegionX + dragRegionRight) / 2.0f; |
| 1027 | float dragRegionCenterY = (dragRegionY + dragRegionBottom) / 2.0f; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1028 | |
| 1029 | // Find the overlapping region |
| 1030 | float overlapLeft = Math.max(0f, dragRegionX); |
| 1031 | float overlapTop = Math.max(0f, dragRegionY); |
| 1032 | float overlapBottom = Math.min(cl.getHeight(), dragRegionBottom); |
| 1033 | float overlapRight = Math.min(cl.getWidth(), dragRegionRight); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1034 | if (overlapRight >= 0 && overlapLeft <= cl.getWidth() && |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1035 | (overlapTop >= 0 && overlapBottom <= cl.getHeight())) { |
| 1036 | // Calculate the distance between the two centers |
| 1037 | float distX = dragRegionCenterX - cl.getWidth()/2; |
| 1038 | float distY = dragRegionCenterY - cl.getHeight()/2; |
| 1039 | float dist = distX * distX + distY * distY; |
| 1040 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1041 | float overlap = (overlapRight - overlapLeft) * (overlapBottom - overlapTop); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1042 | |
| 1043 | // Calculate the closest overlapping region |
| 1044 | if (overlap > 0 && dist < smallestDistSoFar) { |
| 1045 | smallestDistSoFar = dist; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1046 | bestMatchingScreen = cl; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1050 | |
| 1051 | if (bestMatchingScreen != mDragTargetLayout) { |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1052 | if (mDragTargetLayout != null) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1053 | mDragTargetLayout.onDragExit(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1054 | } |
| 1055 | mDragTargetLayout = bestMatchingScreen; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 1056 | // TODO: Should we be calling mDragTargetLayout.onDragEnter() here? |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1057 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1058 | return bestMatchingScreen; |
| 1059 | } |
| 1060 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1061 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 1062 | DragView dragView, Object dragInfo) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1063 | CellLayout currentLayout; |
| 1064 | int originX = x - xOffset; |
| 1065 | int originY = y - yOffset; |
| 1066 | if (mIsSmall) { |
| 1067 | currentLayout = findMatchingPageForDragOver(dragView, originX, originY); |
| 1068 | |
| 1069 | if (currentLayout == null) { |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | currentLayout.setHover(true); |
| 1074 | // get originX and originY in the local coordinate system of the screen |
| 1075 | mTempOriginXY[0] = originX; |
| 1076 | mTempOriginXY[1] = originY; |
| 1077 | mapPointGlobalToLocal(currentLayout, mTempOriginXY); |
| 1078 | originX = (int)mTempOriginXY[0]; |
| 1079 | originY = (int)mTempOriginXY[1]; |
| 1080 | } else { |
| 1081 | currentLayout = getCurrentDropLayout(); |
| 1082 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1083 | |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 1084 | final ItemInfo item = (ItemInfo)dragInfo; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1085 | |
| 1086 | if (dragInfo instanceof LauncherAppWidgetInfo) { |
| 1087 | LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo)dragInfo; |
| 1088 | |
| 1089 | if (widgetInfo.spanX == -1) { |
| 1090 | // Calculate the grid spans needed to fit this widget |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1091 | int[] spans = currentLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, null); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1092 | item.spanX = spans[0]; |
| 1093 | item.spanY = spans[1]; |
| 1094 | } |
| 1095 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (source != this) { |
| 1098 | // This is a hack to fix the point used to determine which cell an icon from the all |
| 1099 | // apps screen is over |
| 1100 | if (item != null && item.spanX == 1 && currentLayout != null) { |
| 1101 | int dragRegionLeft = (dragView.getWidth() - currentLayout.getCellWidth()) / 2; |
| 1102 | |
| 1103 | originX += dragRegionLeft - dragView.getDragRegionLeft(); |
| 1104 | if (dragView.getDragRegionWidth() != currentLayout.getCellWidth()) { |
| 1105 | dragView.setDragRegion(dragView.getDragRegionLeft(), dragView.getDragRegionTop(), |
| 1106 | currentLayout.getCellWidth(), dragView.getDragRegionHeight()); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1110 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 1111 | // When touch is inside the scroll area, skip dragOver actions for the current screen |
| 1112 | if (!mInScrollArea) { |
| 1113 | if (currentLayout != mDragTargetLayout) { |
| 1114 | if (mDragTargetLayout != null) { |
| 1115 | mDragTargetLayout.onDragExit(); |
| 1116 | } |
| 1117 | currentLayout.onDragEnter(dragView); |
| 1118 | mDragTargetLayout = currentLayout; |
| 1119 | } |
| 1120 | |
| 1121 | // only visualize the drop locations for moving icons within the home screen on tablet |
| 1122 | // on phone, we also visualize icons dragged in from All Apps |
| 1123 | if ((!LauncherApplication.isScreenXLarge() || source == this) |
| 1124 | && mDragTargetLayout != null) { |
| 1125 | final View child = (mDragInfo == null) ? null : mDragInfo.cell; |
| 1126 | int localOriginX = originX - (mDragTargetLayout.getLeft() - mScrollX); |
| 1127 | int localOriginY = originY - (mDragTargetLayout.getTop() - mScrollY); |
| 1128 | mDragTargetLayout.visualizeDropLocation( |
| 1129 | child, localOriginX, localOriginY, item.spanX, item.spanY); |
| 1130 | } |
Patrick Dubroy | 976ebec | 2010-08-04 20:03:37 -0700 | [diff] [blame] | 1131 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1134 | public void onDragExit(DragSource source, int x, int y, int xOffset, |
| 1135 | int yOffset, DragView dragView, Object dragInfo) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1136 | if (mDragTargetLayout != null) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1137 | mDragTargetLayout.onDragExit(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1138 | mDragTargetLayout = null; |
| 1139 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 1140 | if (!mIsPageMoving) { |
| 1141 | hideOutlines(); |
| 1142 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1145 | private void onDropExternal(int x, int y, Object dragInfo, |
| 1146 | CellLayout cellLayout) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1147 | onDropExternal(x, y, dragInfo, cellLayout, false); |
| 1148 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1149 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1150 | /** |
| 1151 | * Add the item specified by dragInfo to the given layout. |
| 1152 | * This is basically the equivalent of onDropExternal, except it's not initiated |
| 1153 | * by drag and drop. |
| 1154 | * @return true if successful |
| 1155 | */ |
| 1156 | public boolean addExternalItemToScreen(Object dragInfo, View layout) { |
| 1157 | CellLayout cl = (CellLayout) layout; |
| 1158 | ItemInfo info = (ItemInfo) dragInfo; |
| 1159 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1160 | if (cl.findCellForSpan(mTempEstimate, info.spanX, info.spanY)) { |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1161 | onDropExternal(0, 0, dragInfo, cl, false); |
| 1162 | return true; |
| 1163 | } |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1164 | mLauncher.showOutOfSpaceMessage(); |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
| 1167 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1168 | // Drag from somewhere else |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1169 | private void onDropExternal(int x, int y, Object dragInfo, |
| 1170 | CellLayout cellLayout, boolean insertAtFirst) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1171 | int screen = indexOfChild(cellLayout); |
| 1172 | if (dragInfo instanceof PendingAddItemInfo) { |
| 1173 | PendingAddItemInfo info = (PendingAddItemInfo) dragInfo; |
| 1174 | // When dragging and dropping from customization tray, we deal with creating |
| 1175 | // widgets/shortcuts/folders in a slightly different way |
| 1176 | int[] touchXY = new int[2]; |
| 1177 | touchXY[0] = x; |
| 1178 | touchXY[1] = y; |
| 1179 | switch (info.itemType) { |
| 1180 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 1181 | mLauncher.addAppWidgetFromDrop(info.componentName, screen, touchXY); |
| 1182 | break; |
| 1183 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
| 1184 | mLauncher.addLiveFolderFromDrop(info.componentName, screen, touchXY); |
| 1185 | break; |
| 1186 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 1187 | mLauncher.processShortcutFromDrop(info.componentName, screen, touchXY); |
| 1188 | break; |
| 1189 | default: |
| 1190 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 1191 | } |
| 1192 | cellLayout.onDragExit(); |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | // This is for other drag/drop cases, like dragging from All Apps |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1197 | ItemInfo info = (ItemInfo) dragInfo; |
| 1198 | |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 1199 | View view = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1200 | |
| 1201 | switch (info.itemType) { |
| 1202 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1203 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Joe Onorato | e48e7c1 | 2010-02-19 13:10:40 -0800 | [diff] [blame] | 1204 | if (info.container == NO_ID && info instanceof ApplicationInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1205 | // Came from all apps -- make a copy |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1206 | info = new ShortcutInfo((ApplicationInfo) info); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1207 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1208 | view = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 1209 | (ShortcutInfo) info); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1210 | break; |
| 1211 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 1212 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1213 | cellLayout, ((UserFolderInfo) info)); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 1214 | break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1215 | default: |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1216 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1219 | // If the view is null, it has already been added. |
| 1220 | if (view == null) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1221 | cellLayout.onDragExit(); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1222 | } else { |
Michael Jurka | 7ded67c | 2010-09-28 13:57:39 -0700 | [diff] [blame] | 1223 | mTargetCell = findNearestVacantArea(x, y, 1, 1, null, cellLayout, mTargetCell); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1224 | addInScreen(view, indexOfChild(cellLayout), mTargetCell[0], |
| 1225 | mTargetCell[1], info.spanX, info.spanY, insertAtFirst); |
| 1226 | cellLayout.onDropChild(view); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1227 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 1228 | |
| 1229 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1230 | LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1231 | lp.cellX, lp.cellY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1232 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1233 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1234 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1235 | /** |
| 1236 | * Return the current {@link CellLayout}, correctly picking the destination |
| 1237 | * screen while a scroll is in progress. |
| 1238 | */ |
| 1239 | private CellLayout getCurrentDropLayout() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1240 | int index = mScroller.isFinished() ? mCurrentPage : mNextPage; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1241 | return (CellLayout) getChildAt(index); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1244 | /** |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1245 | * Return the current CellInfo describing our current drag; this method exists |
| 1246 | * so that Launcher can sync this object with the correct info when the activity is created/ |
| 1247 | * destroyed |
| 1248 | * |
| 1249 | */ |
| 1250 | public CellLayout.CellInfo getDragInfo() { |
| 1251 | return mDragInfo; |
| 1252 | } |
| 1253 | |
| 1254 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1255 | * {@inheritDoc} |
| 1256 | */ |
| 1257 | public boolean acceptDrop(DragSource source, int x, int y, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1258 | int xOffset, int yOffset, DragView dragView, Object dragInfo) { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1259 | CellLayout layout; |
| 1260 | if (mIsSmall) { |
| 1261 | layout = findMatchingPageForDragOver(dragView, x - xOffset, y - yOffset); |
| 1262 | if (layout == null) { |
| 1263 | // cancel the drag if we're not over a mini-screen at time of drop |
| 1264 | return false; |
| 1265 | } |
| 1266 | } else { |
| 1267 | layout = getCurrentDropLayout(); |
| 1268 | } |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1269 | final CellLayout.CellInfo dragCellInfo = mDragInfo; |
| 1270 | final int spanX = dragCellInfo == null ? 1 : dragCellInfo.spanX; |
| 1271 | final int spanY = dragCellInfo == null ? 1 : dragCellInfo.spanY; |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1272 | |
Michael Jurka | c28de51 | 2010-08-13 11:27:44 -0700 | [diff] [blame] | 1273 | final View ignoreView = dragCellInfo == null ? null : dragCellInfo.cell; |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1274 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1275 | if (layout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) { |
Michael Jurka | 0e26059 | 2010-06-30 17:07:39 -0700 | [diff] [blame] | 1276 | return true; |
| 1277 | } else { |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1278 | mLauncher.showOutOfSpaceMessage(); |
Michael Jurka | 0e26059 | 2010-06-30 17:07:39 -0700 | [diff] [blame] | 1279 | return false; |
| 1280 | } |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1281 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1282 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1283 | /** |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1284 | * Calculate the nearest cell where the given object would be dropped. |
| 1285 | */ |
Michael Jurka | 6a1435d | 2010-09-27 17:35:12 -0700 | [diff] [blame] | 1286 | private int[] findNearestVacantArea(int pixelX, int pixelY, |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1287 | int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) { |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1288 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 1289 | int localPixelX = pixelX - (layout.getLeft() - mScrollX); |
| 1290 | int localPixelY = pixelY - (layout.getTop() - mScrollY); |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 1291 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1292 | // Find the best target drop location |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1293 | return layout.findNearestVacantArea( |
Michael Jurka | fc9f07d | 2010-09-30 13:22:31 -0700 | [diff] [blame] | 1294 | localPixelX, localPixelY, spanX, spanY, ignoreView, recycle); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1295 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1296 | |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1297 | /** |
| 1298 | * Estimate the size that a child with the given dimensions will take in the current screen. |
| 1299 | */ |
| 1300 | void estimateChildSize(int minWidth, int minHeight, int[] result) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1301 | ((CellLayout)getChildAt(mCurrentPage)).estimateChildSize(minWidth, minHeight, result); |
Patrick Dubroy | 8f86ddc | 2010-07-16 13:55:32 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1304 | void setLauncher(Launcher launcher) { |
| 1305 | mLauncher = launcher; |
| 1306 | } |
| 1307 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1308 | public void setDragController(DragController dragController) { |
| 1309 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | public void onDropCompleted(View target, boolean success) { |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1313 | if (success) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1314 | if (target != this && mDragInfo != null) { |
| 1315 | final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 1316 | cellLayout.removeView(mDragInfo.cell); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1317 | if (mDragInfo.cell instanceof DropTarget) { |
| 1318 | mDragController.removeDropTarget((DropTarget)mDragInfo.cell); |
| 1319 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1320 | // final Object tag = mDragInfo.cell.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1321 | } |
| 1322 | } else { |
| 1323 | if (mDragInfo != null) { |
| 1324 | final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 1325 | cellLayout.onDropAborted(mDragInfo.cell); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | mDragInfo = null; |
| 1330 | } |
| 1331 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 1332 | public boolean isDropEnabled() { |
| 1333 | return true; |
| 1334 | } |
| 1335 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1336 | @Override |
| 1337 | protected void onRestoreInstanceState(Parcelable state) { |
| 1338 | super.onRestoreInstanceState(state); |
| 1339 | Launcher.setScreen(mCurrentPage); |
| 1340 | } |
| 1341 | |
| 1342 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1343 | public void scrollLeft() { |
Michael Jurka | 7f0f4f3 | 2010-08-23 11:33:38 -0700 | [diff] [blame] | 1344 | if (!mIsSmall) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1345 | super.scrollLeft(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1349 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1350 | public void scrollRight() { |
Michael Jurka | 7f0f4f3 | 2010-08-23 11:33:38 -0700 | [diff] [blame] | 1351 | if (!mIsSmall) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1352 | super.scrollRight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame^] | 1356 | @Override |
| 1357 | public void onEnterScrollArea(int direction) { |
| 1358 | mInScrollArea = true; |
| 1359 | final int screen = getCurrentPage() + ((direction == DragController.SCROLL_LEFT) ? -1 : 1); |
| 1360 | if (0 <= screen && screen < getChildCount()) { |
| 1361 | ((CellLayout) getChildAt(screen)).setHover(true); |
| 1362 | } |
| 1363 | |
| 1364 | if (mDragTargetLayout != null) { |
| 1365 | mDragTargetLayout.onDragExit(); |
| 1366 | mDragTargetLayout = null; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | @Override |
| 1371 | public void onExitScrollArea() { |
| 1372 | mInScrollArea = false; |
| 1373 | final int childCount = getChildCount(); |
| 1374 | for (int i = 0; i < childCount; i++) { |
| 1375 | ((CellLayout) getChildAt(i)).setHover(false); |
| 1376 | } |
| 1377 | } |
| 1378 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1379 | public Folder getFolderForTag(Object tag) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1380 | final int screenCount = getChildCount(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1381 | for (int screen = 0; screen < screenCount; screen++) { |
| 1382 | CellLayout currentScreen = ((CellLayout) getChildAt(screen)); |
| 1383 | int count = currentScreen.getChildCount(); |
| 1384 | for (int i = 0; i < count; i++) { |
| 1385 | View child = currentScreen.getChildAt(i); |
| 1386 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 1387 | if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { |
| 1388 | Folder f = (Folder) child; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1389 | if (f.getInfo() == tag && f.getInfo().opened) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1390 | return f; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | return null; |
| 1396 | } |
| 1397 | |
| 1398 | public View getViewForTag(Object tag) { |
| 1399 | int screenCount = getChildCount(); |
| 1400 | for (int screen = 0; screen < screenCount; screen++) { |
| 1401 | CellLayout currentScreen = ((CellLayout) getChildAt(screen)); |
| 1402 | int count = currentScreen.getChildCount(); |
| 1403 | for (int i = 0; i < count; i++) { |
| 1404 | View child = currentScreen.getChildAt(i); |
| 1405 | if (child.getTag() == tag) { |
| 1406 | return child; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | return null; |
| 1411 | } |
| 1412 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1413 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1414 | void removeItems(final ArrayList<ApplicationInfo> apps) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1415 | final int screenCount = getChildCount(); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1416 | final PackageManager manager = getContext().getPackageManager(); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1417 | final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext()); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1418 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1419 | final HashSet<String> packageNames = new HashSet<String>(); |
| 1420 | final int appCount = apps.size(); |
| 1421 | for (int i = 0; i < appCount; i++) { |
| 1422 | packageNames.add(apps.get(i).componentName.getPackageName()); |
| 1423 | } |
| 1424 | |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1425 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1426 | final CellLayout layout = (CellLayout) getChildAt(i); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1427 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1428 | // Avoid ANRs by treating each screen separately |
| 1429 | post(new Runnable() { |
| 1430 | public void run() { |
| 1431 | final ArrayList<View> childrenToRemove = new ArrayList<View>(); |
| 1432 | childrenToRemove.clear(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1433 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1434 | int childCount = layout.getChildCount(); |
| 1435 | for (int j = 0; j < childCount; j++) { |
| 1436 | final View view = layout.getChildAt(j); |
| 1437 | Object tag = view.getTag(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1438 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1439 | if (tag instanceof ShortcutInfo) { |
| 1440 | final ShortcutInfo info = (ShortcutInfo) tag; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1441 | final Intent intent = info.intent; |
| 1442 | final ComponentName name = intent.getComponent(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1443 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1444 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 1445 | for (String packageName: packageNames) { |
| 1446 | if (packageName.equals(name.getPackageName())) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1447 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
| 1448 | childrenToRemove.add(view); |
| 1449 | } |
| 1450 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1451 | } |
| 1452 | } else if (tag instanceof UserFolderInfo) { |
| 1453 | final UserFolderInfo info = (UserFolderInfo) tag; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1454 | final ArrayList<ShortcutInfo> contents = info.contents; |
| 1455 | final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1456 | final int contentsCount = contents.size(); |
| 1457 | boolean removedFromFolder = false; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1458 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1459 | for (int k = 0; k < contentsCount; k++) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1460 | final ShortcutInfo appInfo = contents.get(k); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1461 | final Intent intent = appInfo.intent; |
| 1462 | final ComponentName name = intent.getComponent(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1463 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1464 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 1465 | for (String packageName: packageNames) { |
| 1466 | if (packageName.equals(name.getPackageName())) { |
| 1467 | toRemove.add(appInfo); |
Brad Fitzpatrick | 73013bf | 2010-09-14 12:15:32 -0700 | [diff] [blame] | 1468 | LauncherModel.deleteItemFromDatabase(mLauncher, appInfo); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1469 | removedFromFolder = true; |
| 1470 | } |
| 1471 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1472 | } |
| 1473 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1474 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1475 | contents.removeAll(toRemove); |
| 1476 | if (removedFromFolder) { |
| 1477 | final Folder folder = getOpenFolder(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1478 | if (folder != null) |
| 1479 | folder.notifyDataSetChanged(); |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1480 | } |
| 1481 | } else if (tag instanceof LiveFolderInfo) { |
| 1482 | final LiveFolderInfo info = (LiveFolderInfo) tag; |
| 1483 | final Uri uri = info.uri; |
| 1484 | final ProviderInfo providerInfo = manager.resolveContentProvider( |
| 1485 | uri.getAuthority(), 0); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1486 | |
Joe Onorato | f11079b | 2010-04-15 11:47:28 -0700 | [diff] [blame] | 1487 | if (providerInfo != null) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1488 | for (String packageName: packageNames) { |
| 1489 | if (packageName.equals(providerInfo.packageName)) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1490 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1491 | childrenToRemove.add(view); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1492 | } |
| 1493 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1494 | } |
| 1495 | } else if (tag instanceof LauncherAppWidgetInfo) { |
| 1496 | final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag; |
| 1497 | final AppWidgetProviderInfo provider = |
| 1498 | widgets.getAppWidgetInfo(info.appWidgetId); |
Daniel Sandler | e1cc6c3 | 2010-05-07 11:49:29 -0400 | [diff] [blame] | 1499 | if (provider != null) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1500 | for (String packageName: packageNames) { |
| 1501 | if (packageName.equals(provider.provider.getPackageName())) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1502 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1503 | childrenToRemove.add(view); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1504 | } |
| 1505 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1506 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1507 | } |
| 1508 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1509 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1510 | childCount = childrenToRemove.size(); |
| 1511 | for (int j = 0; j < childCount; j++) { |
| 1512 | View child = childrenToRemove.get(j); |
| 1513 | layout.removeViewInLayout(child); |
| 1514 | if (child instanceof DropTarget) { |
| 1515 | mDragController.removeDropTarget((DropTarget)child); |
| 1516 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1517 | } |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1518 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1519 | if (childCount > 0) { |
| 1520 | layout.requestLayout(); |
| 1521 | layout.invalidate(); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1522 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1523 | } |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 1524 | }); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1525 | } |
| 1526 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1527 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1528 | void updateShortcuts(ArrayList<ApplicationInfo> apps) { |
Michael Jurka | dee0589 | 2010-07-27 10:01:56 -0700 | [diff] [blame] | 1529 | final int screenCount = getChildCount(); |
| 1530 | for (int i = 0; i < screenCount; i++) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1531 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1532 | int childCount = layout.getChildCount(); |
| 1533 | for (int j = 0; j < childCount; j++) { |
| 1534 | final View view = layout.getChildAt(j); |
| 1535 | Object tag = view.getTag(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1536 | if (tag instanceof ShortcutInfo) { |
| 1537 | ShortcutInfo info = (ShortcutInfo)tag; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1538 | // We need to check for ACTION_MAIN otherwise getComponent() might |
| 1539 | // return null for some shortcuts (for instance, for shortcuts to |
| 1540 | // web pages.) |
| 1541 | final Intent intent = info.intent; |
| 1542 | final ComponentName name = intent.getComponent(); |
| 1543 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1544 | Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { |
| 1545 | final int appCount = apps.size(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1546 | for (int k = 0; k < appCount; k++) { |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 1547 | ApplicationInfo app = apps.get(k); |
| 1548 | if (app.componentName.equals(name)) { |
| 1549 | info.setIcon(mIconCache.getIcon(info.intent)); |
| 1550 | ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null, |
| 1551 | new FastBitmapDrawable(info.getIcon(mIconCache)), |
| 1552 | null, null); |
| 1553 | } |
| 1554 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 1561 | void moveToDefaultScreen(boolean animate) { |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 1562 | if (animate) { |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1563 | if (mIsSmall) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1564 | unshrink(mDefaultPage); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1565 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1566 | snapToPage(mDefaultPage); |
Michael Jurka | 4cb3724 | 2010-08-09 21:05:32 -0700 | [diff] [blame] | 1567 | } |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 1568 | } else { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1569 | setCurrentPage(mDefaultPage); |
Joe Onorato | c45b168 | 2010-01-11 18:48:40 -0500 | [diff] [blame] | 1570 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1571 | getChildAt(mDefaultPage).requestFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 1574 | void setIndicators(Drawable previous, Drawable next) { |
| 1575 | mPreviousIndicator = previous; |
| 1576 | mNextIndicator = next; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1577 | previous.setLevel(mCurrentPage); |
| 1578 | next.setLevel(mCurrentPage); |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1581 | @Override |
| 1582 | public void syncPages() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1583 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1584 | |
| 1585 | @Override |
| 1586 | public void syncPageItems(int page) { |
| 1587 | } |
| 1588 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1589 | } |