Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 20 | import android.animation.ObjectAnimator; |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 22 | import android.appwidget.AppWidgetHostView; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 23 | import android.appwidget.AppWidgetManager; |
| 24 | import android.appwidget.AppWidgetProviderInfo; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 30 | import android.content.res.Configuration; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
| 32 | import android.content.res.TypedArray; |
| 33 | import android.graphics.Bitmap; |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 34 | import android.graphics.Bitmap.Config; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 36 | import android.graphics.MaskFilter; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 37 | import android.graphics.Matrix; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 38 | import android.graphics.Paint; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 40 | import android.graphics.RectF; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 41 | import android.graphics.TableMaskFilter; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 43 | import android.os.AsyncTask; |
| 44 | import android.os.Process; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 45 | import android.util.AttributeSet; |
| 46 | import android.util.Log; |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 47 | import android.view.Gravity; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 48 | import android.view.KeyEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 49 | import android.view.LayoutInflater; |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 50 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 51 | import android.view.View; |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 52 | import android.view.ViewConfiguration; |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 53 | import android.view.ViewGroup; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 54 | import android.view.animation.AccelerateInterpolator; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 55 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 56 | import android.widget.GridLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 57 | import android.widget.ImageView; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 58 | import android.widget.Toast; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 59 | |
| 60 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 61 | import com.android.launcher2.DropTarget.DragObject; |
| 62 | |
| 63 | import java.util.ArrayList; |
| 64 | import java.util.Collections; |
| 65 | import java.util.Iterator; |
| 66 | import java.util.List; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 67 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 68 | /** |
| 69 | * A simple callback interface which also provides the results of the task. |
| 70 | */ |
| 71 | interface AsyncTaskCallback { |
| 72 | void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data); |
| 73 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 74 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 75 | /** |
| 76 | * The data needed to perform either of the custom AsyncTasks. |
| 77 | */ |
| 78 | class AsyncTaskPageData { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 79 | enum Type { |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 80 | LoadWidgetPreviewData |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 83 | AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR, |
| 84 | AsyncTaskCallback postR) { |
| 85 | page = p; |
| 86 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 87 | sourceImages = si; |
| 88 | generatedImages = new ArrayList<Bitmap>(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 89 | maxImageWidth = maxImageHeight = -1; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 90 | doInBackgroundCallback = bgR; |
| 91 | postExecuteCallback = postR; |
| 92 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 93 | AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 94 | AsyncTaskCallback postR) { |
| 95 | page = p; |
| 96 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 97 | generatedImages = new ArrayList<Bitmap>(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 98 | maxImageWidth = cw; |
| 99 | maxImageHeight = ch; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 100 | doInBackgroundCallback = bgR; |
| 101 | postExecuteCallback = postR; |
| 102 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 103 | void cleanup(boolean cancelled) { |
| 104 | // Clean up any references to source/generated bitmaps |
| 105 | if (sourceImages != null) { |
| 106 | if (cancelled) { |
| 107 | for (Bitmap b : sourceImages) { |
| 108 | b.recycle(); |
| 109 | } |
| 110 | } |
| 111 | sourceImages.clear(); |
| 112 | } |
| 113 | if (generatedImages != null) { |
| 114 | if (cancelled) { |
| 115 | for (Bitmap b : generatedImages) { |
| 116 | b.recycle(); |
| 117 | } |
| 118 | } |
| 119 | generatedImages.clear(); |
| 120 | } |
| 121 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 122 | int page; |
| 123 | ArrayList<Object> items; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 124 | ArrayList<Bitmap> sourceImages; |
| 125 | ArrayList<Bitmap> generatedImages; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 126 | int maxImageWidth; |
| 127 | int maxImageHeight; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 128 | AsyncTaskCallback doInBackgroundCallback; |
| 129 | AsyncTaskCallback postExecuteCallback; |
| 130 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 131 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 132 | /** |
| 133 | * A generic template for an async task used in AppsCustomize. |
| 134 | */ |
| 135 | class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 136 | AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 137 | page = p; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 138 | threadPriority = Process.THREAD_PRIORITY_DEFAULT; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 139 | dataType = ty; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 140 | } |
| 141 | @Override |
| 142 | protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) { |
| 143 | if (params.length != 1) return null; |
| 144 | // Load each of the widget previews in the background |
| 145 | params[0].doInBackgroundCallback.run(this, params[0]); |
| 146 | return params[0]; |
| 147 | } |
| 148 | @Override |
| 149 | protected void onPostExecute(AsyncTaskPageData result) { |
| 150 | // All the widget previews are loaded, so we can just callback to inflate the page |
| 151 | result.postExecuteCallback.run(this, result); |
| 152 | } |
| 153 | |
| 154 | void setThreadPriority(int p) { |
| 155 | threadPriority = p; |
| 156 | } |
| 157 | void syncThreadPriority() { |
| 158 | Process.setThreadPriority(threadPriority); |
| 159 | } |
| 160 | |
| 161 | // The page that this async task is associated with |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 162 | AsyncTaskPageData.Type dataType; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 163 | int page; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 164 | int threadPriority; |
| 165 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * The Apps/Customize page that displays all the applications, widgets, and shortcuts. |
| 169 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 170 | public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 171 | AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource, |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 172 | PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener, |
| 173 | LauncherTransitionable { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 174 | static final String LOG_TAG = "AppsCustomizePagedView"; |
| 175 | |
| 176 | /** |
| 177 | * The different content types that this paged view can show. |
| 178 | */ |
| 179 | public enum ContentType { |
| 180 | Applications, |
Winson Chung | 6a26e5b | 2011-05-26 14:36:06 -0700 | [diff] [blame] | 181 | Widgets |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Refs |
| 185 | private Launcher mLauncher; |
| 186 | private DragController mDragController; |
| 187 | private final LayoutInflater mLayoutInflater; |
| 188 | private final PackageManager mPackageManager; |
| 189 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 190 | // Save and Restore |
| 191 | private int mSaveInstanceStateItemIndex = -1; |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 192 | private PagedViewIcon mPressedIcon; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 193 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 194 | // Content |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 195 | private ArrayList<ApplicationInfo> mApps; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 196 | private ArrayList<Object> mWidgets; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 197 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 198 | // Cling |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 199 | private boolean mHasShownAllAppsCling; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 200 | private int mClingFocusedX; |
| 201 | private int mClingFocusedY; |
| 202 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 203 | // Caching |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 204 | private Canvas mCanvas; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 205 | private Drawable mDefaultWidgetBackground; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 206 | private IconCache mIconCache; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 207 | |
| 208 | // Dimens |
| 209 | private int mContentWidth; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 210 | private int mAppIconSize; |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 211 | private int mMaxAppCellCountX, mMaxAppCellCountY; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 212 | private int mWidgetCountX, mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 213 | private int mWidgetWidthGap, mWidgetHeightGap; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 214 | private final int mWidgetPreviewIconPaddedDimension; |
| 215 | private final float sWidgetPreviewIconPaddingPercentage = 0.25f; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 216 | private PagedViewCellLayout mWidgetSpacingLayout; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 217 | private int mNumAppsPages; |
| 218 | private int mNumWidgetPages; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 219 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 220 | // Relating to the scroll and overscroll effects |
| 221 | Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f); |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 222 | private static float CAMERA_DISTANCE = 6500; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 223 | private static float TRANSITION_SCALE_FACTOR = 0.74f; |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 224 | private static float TRANSITION_PIVOT = 0.65f; |
| 225 | private static float TRANSITION_MAX_ROTATION = 22; |
| 226 | private static final boolean PERFORM_OVERSCROLL_ROTATION = true; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 227 | private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 228 | private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 229 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 230 | // Previews & outlines |
| 231 | ArrayList<AppsCustomizeAsyncTask> mRunningTasks; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 232 | private static final int sPageSleepDelay = 200; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 233 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 234 | private Runnable mInflateWidgetRunnable = null; |
| 235 | private Runnable mBindWidgetRunnable = null; |
| 236 | static final int WIDGET_NO_CLEANUP_REQUIRED = -1; |
| 237 | static final int WIDGET_BOUND = 0; |
| 238 | static final int WIDGET_INFLATED = 1; |
| 239 | int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED; |
| 240 | int mWidgetLoadingId = -1; |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 241 | PendingAddWidgetInfo mCreateWidgetInfo = null; |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 242 | private boolean mDraggingWidget = false; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 243 | |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 244 | // Deferral of loading widget previews during launcher transitions |
| 245 | private boolean mInTransition; |
| 246 | private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems = |
| 247 | new ArrayList<AsyncTaskPageData>(); |
| 248 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 249 | public AppsCustomizePagedView(Context context, AttributeSet attrs) { |
| 250 | super(context, attrs); |
| 251 | mLayoutInflater = LayoutInflater.from(context); |
| 252 | mPackageManager = context.getPackageManager(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 253 | mApps = new ArrayList<ApplicationInfo>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 254 | mWidgets = new ArrayList<Object>(); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 255 | mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 256 | mCanvas = new Canvas(); |
| 257 | mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 258 | |
| 259 | // Save the default widget preview background |
| 260 | Resources resources = context.getResources(); |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 261 | mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 262 | mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 263 | |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 264 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); |
| 265 | mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1); |
| 266 | mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 267 | mWidgetWidthGap = |
| 268 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0); |
| 269 | mWidgetHeightGap = |
| 270 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 271 | mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); |
| 272 | mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 273 | mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0); |
| 274 | mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 275 | a.recycle(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 276 | mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 277 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 278 | // The padding on the non-matched dimension for the default widget preview icons |
| 279 | // (top + bottom) |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 280 | mWidgetPreviewIconPaddedDimension = |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 281 | (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage))); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 282 | mFadeInAdjacentScreens = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | protected void init() { |
| 287 | super.init(); |
Winson Chung | 6a87740 | 2011-10-26 14:51:44 -0700 | [diff] [blame] | 288 | mCenterPagesVertically = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 289 | |
| 290 | Context context = getContext(); |
| 291 | Resources r = context.getResources(); |
| 292 | setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); |
| 293 | } |
| 294 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 295 | @Override |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 296 | protected void onUnhandledTap(MotionEvent ev) { |
| 297 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 298 | // Dismiss AppsCustomize if we tap |
| 299 | mLauncher.showWorkspace(true); |
| 300 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 303 | /** Returns the item index of the center item on this page so that we can restore to this |
| 304 | * item index when we rotate. */ |
| 305 | private int getMiddleComponentIndexOnCurrentPage() { |
| 306 | int i = -1; |
| 307 | if (getPageCount() > 0) { |
| 308 | int currentPage = getCurrentPage(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 309 | if (currentPage < mNumAppsPages) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 310 | PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 311 | PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout(); |
| 312 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 313 | int childCount = childrenLayout.getChildCount(); |
| 314 | if (childCount > 0) { |
| 315 | i = (currentPage * numItemsPerPage) + (childCount / 2); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 316 | } |
| 317 | } else { |
| 318 | int numApps = mApps.size(); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 319 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 320 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
| 321 | int childCount = layout.getChildCount(); |
| 322 | if (childCount > 0) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 323 | i = numApps + |
| 324 | ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2); |
| 325 | } |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | return i; |
| 329 | } |
| 330 | |
| 331 | /** Get the index of the item to restore to if we need to restore the current page. */ |
| 332 | int getSaveInstanceStateIndex() { |
| 333 | if (mSaveInstanceStateItemIndex == -1) { |
| 334 | mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage(); |
| 335 | } |
| 336 | return mSaveInstanceStateItemIndex; |
| 337 | } |
| 338 | |
| 339 | /** Returns the page in the current orientation which is expected to contain the specified |
| 340 | * item index. */ |
| 341 | int getPageForComponent(int index) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 342 | if (index < 0) return 0; |
| 343 | |
| 344 | if (index < mApps.size()) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 345 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 346 | return (index / numItemsPerPage); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 347 | } else { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 348 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 349 | return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage); |
| 350 | } |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 353 | /** |
| 354 | * This differs from isDataReady as this is the test done if isDataReady is not set. |
| 355 | */ |
| 356 | private boolean testDataReady() { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 357 | // We only do this test once, and we default to the Applications page, so we only really |
| 358 | // have to wait for there to be apps. |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 359 | // TODO: What if one of them is validly empty |
| 360 | return !mApps.isEmpty() && !mWidgets.isEmpty(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 363 | /** Restores the page for an item at the specified index */ |
| 364 | void restorePageForIndex(int index) { |
| 365 | if (index < 0) return; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 366 | mSaveInstanceStateItemIndex = index; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 369 | private void updatePageCounts() { |
| 370 | mNumWidgetPages = (int) Math.ceil(mWidgets.size() / |
| 371 | (float) (mWidgetCountX * mWidgetCountY)); |
| 372 | mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 373 | } |
| 374 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 375 | protected void onDataReady(int width, int height) { |
| 376 | // Note that we transpose the counts in portrait so that we get a similar layout |
| 377 | boolean isLandscape = getResources().getConfiguration().orientation == |
| 378 | Configuration.ORIENTATION_LANDSCAPE; |
| 379 | int maxCellCountX = Integer.MAX_VALUE; |
| 380 | int maxCellCountY = Integer.MAX_VALUE; |
| 381 | if (LauncherApplication.isScreenLarge()) { |
| 382 | maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() : |
| 383 | LauncherModel.getCellCountY()); |
| 384 | maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() : |
| 385 | LauncherModel.getCellCountX()); |
| 386 | } |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 387 | if (mMaxAppCellCountX > -1) { |
| 388 | maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX); |
| 389 | } |
| 390 | if (mMaxAppCellCountY > -1) { |
| 391 | maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY); |
| 392 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 393 | |
| 394 | // Now that the data is ready, we can calculate the content width, the number of cells to |
| 395 | // use for each page |
| 396 | mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 397 | mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 398 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 399 | mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY); |
| 400 | mCellCountX = mWidgetSpacingLayout.getCellCountX(); |
| 401 | mCellCountY = mWidgetSpacingLayout.getCellCountY(); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 402 | updatePageCounts(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 403 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 404 | // Force a measure to update recalculate the gaps |
| 405 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 406 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
| 407 | mWidgetSpacingLayout.measure(widthSpec, heightSpec); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 408 | mContentWidth = mWidgetSpacingLayout.getContentWidth(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 409 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 410 | AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost(); |
| 411 | final boolean hostIsTransitioning = host.isTransitioning(); |
| 412 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 413 | // Restore the page |
| 414 | int page = getPageForComponent(mSaveInstanceStateItemIndex); |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 415 | invalidatePageData(Math.max(0, page), hostIsTransitioning); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 416 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 417 | // Show All Apps cling if we are finished transitioning, otherwise, we will try again when |
| 418 | // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be |
| 419 | // returned while animating) |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 420 | if (!hostIsTransitioning) { |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 421 | post(new Runnable() { |
| 422 | @Override |
| 423 | public void run() { |
| 424 | showAllAppsCling(); |
| 425 | } |
| 426 | }); |
| 427 | } |
| 428 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 429 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 430 | void showAllAppsCling() { |
| 431 | if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) { |
| 432 | mHasShownAllAppsCling = true; |
| 433 | // Calculate the position for the cling punch through |
| 434 | int[] offset = new int[2]; |
| 435 | int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY); |
| 436 | mLauncher.getDragLayer().getLocationInDragLayer(this, offset); |
| 437 | // PagedViews are centered horizontally but top aligned |
| 438 | pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + |
| 439 | offset[0]; |
| 440 | pos[1] += offset[1]; |
| 441 | mLauncher.showFirstRunAllAppsCling(pos); |
| 442 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
| 446 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 447 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 448 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 449 | if (!isDataReady()) { |
| 450 | if (testDataReady()) { |
| 451 | setDataIsReady(); |
| 452 | setMeasuredDimension(width, height); |
| 453 | onDataReady(width, height); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 458 | } |
| 459 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 460 | public void onPackagesUpdated() { |
Adam Cohen | b0fa352 | 2011-08-17 16:10:46 -0700 | [diff] [blame] | 461 | // TODO: this isn't ideal, but we actually need to delay here. This call is triggered |
| 462 | // by a broadcast receiver, and in order for it to work correctly, we need to know that |
| 463 | // the AppWidgetService has already received and processed the same broadcast. Since there |
| 464 | // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally, |
| 465 | // we should have a more precise way of ensuring the AppWidgetService is up to date. |
| 466 | postDelayed(new Runnable() { |
| 467 | public void run() { |
| 468 | updatePackages(); |
| 469 | } |
| 470 | }, 500); |
| 471 | } |
| 472 | |
| 473 | public void updatePackages() { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 474 | // Get the list of widgets and shortcuts |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 475 | boolean wasEmpty = mWidgets.isEmpty(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 476 | mWidgets.clear(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 477 | List<AppWidgetProviderInfo> widgets = |
| 478 | AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 479 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 480 | List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame] | 481 | for (AppWidgetProviderInfo widget : widgets) { |
| 482 | if (widget.minWidth > 0 && widget.minHeight > 0) { |
Winson Chung | a5c9636 | 2012-04-12 14:04:41 -0700 | [diff] [blame] | 483 | // Ensure that all widgets we show can be added on a workspace of this size |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 484 | int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget); |
| 485 | int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget); |
Winson Chung | a5c9636 | 2012-04-12 14:04:41 -0700 | [diff] [blame] | 486 | int minSpanX = Math.min(spanXY[0], minSpanXY[0]); |
| 487 | int minSpanY = Math.min(spanXY[1], minSpanXY[1]); |
Adam Cohen | 336d491 | 2012-04-13 17:57:11 -0700 | [diff] [blame] | 488 | if (minSpanX <= LauncherModel.getCellCountX() && |
| 489 | minSpanY <= LauncherModel.getCellCountY()) { |
Winson Chung | a5c9636 | 2012-04-12 14:04:41 -0700 | [diff] [blame] | 490 | mWidgets.add(widget); |
| 491 | } |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame] | 492 | } else { |
| 493 | Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" + |
| 494 | widget.minWidth + ", " + widget.minHeight + ")"); |
| 495 | } |
| 496 | } |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 497 | mWidgets.addAll(shortcuts); |
| 498 | Collections.sort(mWidgets, |
| 499 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 500 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 501 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 502 | if (wasEmpty) { |
| 503 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 504 | // a layout to do this test and invalidate the page data when ready. |
| 505 | if (testDataReady()) requestLayout(); |
| 506 | } else { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 507 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 508 | invalidatePageData(); |
| 509 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void onClick(View v) { |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 514 | // When we have exited all apps or are in transition, disregard clicks |
| 515 | if (!mLauncher.isAllAppsCustomizeOpen() || |
| 516 | mLauncher.getWorkspace().isSwitchingState()) return; |
| 517 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 518 | if (v instanceof PagedViewIcon) { |
| 519 | // Animate some feedback to the click |
| 520 | final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); |
Winson Chung | 3b187b8 | 2012-01-30 15:11:08 -0800 | [diff] [blame] | 521 | |
| 522 | // Lock the drawable state to pressed until we return to Launcher |
| 523 | if (mPressedIcon != null) { |
| 524 | mPressedIcon.lockDrawableState(); |
| 525 | } |
Winson Chung | c7450e3 | 2012-04-17 17:34:08 -0700 | [diff] [blame] | 526 | |
Winson Chung | 18f41f8 | 2012-05-09 13:28:10 -0700 | [diff] [blame] | 527 | // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled |
| 528 | // to be consistent. So re-enable the flag here, and we will re-disable it as necessary |
| 529 | // when Launcher resumes and we are still in AllApps. |
| 530 | mLauncher.updateWallpaperVisibility(true); |
Winson Chung | c7450e3 | 2012-04-17 17:34:08 -0700 | [diff] [blame] | 531 | mLauncher.startActivitySafely(v, appInfo.intent, appInfo); |
| 532 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 533 | } else if (v instanceof PagedViewWidget) { |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 534 | // Let the user know that they have to long press to add a widget |
| 535 | Toast.makeText(getContext(), R.string.long_press_widget_to_add, |
| 536 | Toast.LENGTH_SHORT).show(); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 537 | |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 538 | // Create a little animation to show that the widget can move |
| 539 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 540 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 541 | AnimatorSet bounce = new AnimatorSet(); |
| 542 | ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); |
| 543 | tyuAnim.setDuration(125); |
| 544 | ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); |
| 545 | tydAnim.setDuration(100); |
| 546 | bounce.play(tyuAnim).before(tydAnim); |
| 547 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 548 | bounce.start(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 549 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 552 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 553 | return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event); |
| 554 | } |
| 555 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 556 | /* |
| 557 | * PagedViewWithDraggableItems implementation |
| 558 | */ |
| 559 | @Override |
| 560 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 561 | // Disable dragging by pulling an app down for now. |
| 562 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 563 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 564 | private void beginDraggingApplication(View v) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 565 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 566 | mLauncher.getWorkspace().beginDragShared(v, this); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 567 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 568 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 569 | private void loadWidgetInBackground(final PendingAddWidgetInfo info) { |
| 570 | final AppWidgetProviderInfo pInfo = info.info; |
| 571 | if (pInfo.configure != null) { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | mBindWidgetRunnable = new Runnable() { |
| 576 | @Override |
| 577 | public void run() { |
| 578 | mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 579 | if (AppWidgetManager.getInstance(mLauncher) |
| 580 | .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) { |
| 581 | mWidgetCleanupState = WIDGET_BOUND; |
| 582 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 583 | } |
| 584 | }; |
| 585 | post(mBindWidgetRunnable); |
| 586 | |
| 587 | mInflateWidgetRunnable = new Runnable() { |
| 588 | @Override |
| 589 | public void run() { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 590 | AppWidgetHostView hostView = mLauncher. |
| 591 | getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 592 | info.boundWidget = hostView; |
| 593 | mWidgetCleanupState = WIDGET_INFLATED; |
Adam Cohen | ef3dd6e | 2012-02-14 20:54:05 -0800 | [diff] [blame] | 594 | hostView.setVisibility(INVISIBLE); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 595 | int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX, |
| 596 | info.spanY, info, false); |
| 597 | |
| 598 | // We want the first widget layout to be the correct size. This will be important |
| 599 | // for width size reporting to the AppWidgetManager. |
| 600 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0], |
| 601 | unScaledSize[1]); |
| 602 | lp.x = lp.y = 0; |
| 603 | lp.customPosition = true; |
| 604 | hostView.setLayoutParams(lp); |
Adam Cohen | ef3dd6e | 2012-02-14 20:54:05 -0800 | [diff] [blame] | 605 | mLauncher.getDragLayer().addView(hostView); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 606 | } |
| 607 | }; |
| 608 | post(mInflateWidgetRunnable); |
| 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public void onShortPress(View v) { |
| 613 | // We are anticipating a long press, and we use this time to load bind and instantiate |
| 614 | // the widget. This will need to be cleaned up if it turns out no long press occurs. |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 615 | mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag()); |
| 616 | loadWidgetInBackground(mCreateWidgetInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 619 | private void cleanupWidgetPreloading() { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 620 | PendingAddWidgetInfo info = mCreateWidgetInfo; |
| 621 | mCreateWidgetInfo = null; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 622 | if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) { |
| 623 | mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId); |
| 624 | } |
| 625 | if (mWidgetCleanupState == WIDGET_BOUND) { |
| 626 | removeCallbacks(mInflateWidgetRunnable); |
| 627 | } else if (mWidgetCleanupState == WIDGET_INFLATED) { |
| 628 | AppWidgetHostView widget = info.boundWidget; |
| 629 | int widgetId = widget.getAppWidgetId(); |
| 630 | mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId); |
Adam Cohen | ef3dd6e | 2012-02-14 20:54:05 -0800 | [diff] [blame] | 631 | mLauncher.getDragLayer().removeView(widget); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 632 | } |
| 633 | mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED; |
| 634 | mWidgetLoadingId = -1; |
| 635 | } |
| 636 | |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 637 | @Override |
| 638 | public void cleanUpShortPress(View v) { |
| 639 | if (!mDraggingWidget) { |
| 640 | cleanupWidgetPreloading(); |
| 641 | } |
| 642 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 643 | |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame^] | 644 | private boolean beginDraggingWidget(View v) { |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 645 | mDraggingWidget = true; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 646 | // Get the widget preview as the drag representation |
| 647 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 648 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 649 | |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame^] | 650 | // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and |
| 651 | // we abort the drag. |
| 652 | if (image.getDrawable() == null) { |
| 653 | mDraggingWidget = false; |
| 654 | return false; |
| 655 | } |
| 656 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 657 | // Compose the drag image |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 658 | Bitmap preview; |
| 659 | Bitmap outline; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 660 | float scale = 1f; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 661 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 662 | PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo; |
| 663 | createItemInfo = createWidgetInfo; |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 664 | int spanX = createItemInfo.spanX; |
| 665 | int spanY = createItemInfo.spanY; |
| 666 | int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY, |
| 667 | createWidgetInfo, true); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 668 | |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 669 | FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable(); |
| 670 | float minScale = 1.25f; |
| 671 | int minWidth, minHeight; |
| 672 | minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]); |
| 673 | minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 674 | preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage, |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 675 | createWidgetInfo.icon, spanX, spanY, minWidth, minHeight); |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 676 | |
| 677 | // Determine the image view drawable scale relative to the preview |
| 678 | float[] mv = new float[9]; |
| 679 | Matrix m = new Matrix(); |
| 680 | m.setRectToRect( |
| 681 | new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()), |
| 682 | new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(), |
| 683 | (float) previewDrawable.getIntrinsicHeight()), |
| 684 | Matrix.ScaleToFit.START); |
| 685 | m.getValues(mv); |
| 686 | scale = (float) mv[0]; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 687 | } else { |
| 688 | // Workaround for the fact that we don't keep the original ResolveInfo associated with |
| 689 | // the shortcut around. To get the icon, we just render the preview image (which has |
| 690 | // the shortcut icon) to a new drag bitmap that clips the non-icon space. |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 691 | preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 692 | mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 693 | Drawable d = image.getDrawable(); |
| 694 | mCanvas.setBitmap(preview); |
| 695 | d.draw(mCanvas); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 696 | mCanvas.setBitmap(null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 697 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 698 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 699 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 700 | // We use a custom alpha clip table for the default widget previews |
| 701 | Paint alphaClipPaint = null; |
| 702 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 703 | if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 704 | MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255); |
| 705 | alphaClipPaint = new Paint(); |
| 706 | alphaClipPaint.setMaskFilter(alphaClipTable); |
| 707 | } |
| 708 | } |
| 709 | |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 710 | // Save the preview for the outline generation, then dim the preview |
| 711 | outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(), |
| 712 | false); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 713 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 714 | // Start the drag |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 715 | alphaClipPaint = null; |
Winson Chung | 641d71d | 2012-04-26 15:58:01 -0700 | [diff] [blame] | 716 | mLauncher.lockScreenOrientation(); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 717 | mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint); |
| 718 | mDragController.startDrag(image, preview, this, createItemInfo, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 719 | DragController.DRAG_ACTION_COPY, null, scale); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 720 | outline.recycle(); |
| 721 | preview.recycle(); |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame^] | 722 | return true; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 723 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 724 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 725 | @Override |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 726 | protected boolean beginDragging(final View v) { |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 727 | if (!super.beginDragging(v)) return false; |
| 728 | |
| 729 | if (v instanceof PagedViewIcon) { |
| 730 | beginDraggingApplication(v); |
| 731 | } else if (v instanceof PagedViewWidget) { |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame^] | 732 | if (!beginDraggingWidget(v)) { |
| 733 | return false; |
| 734 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 735 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 736 | |
| 737 | // We delay entering spring-loaded mode slightly to make sure the UI |
| 738 | // thready is free of any work. |
| 739 | postDelayed(new Runnable() { |
| 740 | @Override |
| 741 | public void run() { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 742 | // We don't enter spring-loaded mode if the drag has been cancelled |
| 743 | if (mLauncher.getDragController().isDragging()) { |
| 744 | // Dismiss the cling |
| 745 | mLauncher.dismissAllAppsCling(null); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 746 | |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 747 | // Reset the alpha on the dragged icon before we drag |
| 748 | resetDrawableState(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 749 | |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 750 | // Go into spring loaded mode (must happen before we startDrag()) |
| 751 | mLauncher.enterSpringLoadedDragMode(); |
| 752 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 753 | } |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 754 | }, 150); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 755 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 756 | return true; |
| 757 | } |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 758 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 759 | /** |
| 760 | * Clean up after dragging. |
| 761 | * |
| 762 | * @param target where the item was dragged to (can be null if the item was flung) |
| 763 | */ |
| 764 | private void endDragging(View target, boolean isFlingToDelete, boolean success) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 765 | if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() && |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 766 | !(target instanceof DeleteDropTarget))) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 767 | // Exit spring loaded mode if we have not successfully dropped or have not handled the |
| 768 | // drop in Workspace |
| 769 | mLauncher.exitSpringLoadedDragMode(); |
| 770 | } |
Winson Chung | 4b919f8 | 2012-05-01 10:44:08 -0700 | [diff] [blame] | 771 | mLauncher.unlockScreenOrientation(false); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 774 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 775 | public View getContent() { |
| 776 | return null; |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 781 | mInTransition = true; |
| 782 | if (toWorkspace) { |
| 783 | cancelAllTasks(); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 788 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public void onLauncherTransitionStep(Launcher l, float t) { |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
| 797 | mInTransition = false; |
| 798 | for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) { |
| 799 | onSyncWidgetPageItems(d); |
| 800 | } |
| 801 | mDeferredSyncWidgetPageItems.clear(); |
| 802 | } |
| 803 | |
| 804 | @Override |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 805 | public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete, |
| 806 | boolean success) { |
| 807 | // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling |
| 808 | if (isFlingToDelete) return; |
| 809 | |
| 810 | endDragging(target, false, success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 811 | |
| 812 | // Display an error message if the drag failed due to there not being enough space on the |
| 813 | // target layout we were dropping on. |
| 814 | if (!success) { |
| 815 | boolean showOutOfSpaceMessage = false; |
| 816 | if (target instanceof Workspace) { |
| 817 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 818 | Workspace workspace = (Workspace) target; |
| 819 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 820 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 821 | if (layout != null) { |
| 822 | layout.calculateSpans(itemInfo); |
| 823 | showOutOfSpaceMessage = |
| 824 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 825 | } |
| 826 | } |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 827 | if (showOutOfSpaceMessage) { |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 828 | mLauncher.showOutOfSpaceMessage(false); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 829 | } |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 830 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 831 | d.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 832 | cleanupWidgetPreloading(); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 833 | } |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 834 | mDraggingWidget = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 837 | @Override |
| 838 | public void onFlingToDeleteCompleted() { |
| 839 | // We just dismiss the drag when we fling, so cleanup here |
| 840 | endDragging(null, true, true); |
| 841 | cleanupWidgetPreloading(); |
| 842 | mDraggingWidget = false; |
| 843 | } |
| 844 | |
| 845 | @Override |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 846 | public boolean supportsFlingToDelete() { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 847 | return true; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 850 | @Override |
| 851 | protected void onDetachedFromWindow() { |
| 852 | super.onDetachedFromWindow(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 853 | cancelAllTasks(); |
| 854 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 855 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 856 | public void clearAllWidgetPages() { |
| 857 | cancelAllTasks(); |
| 858 | int count = getChildCount(); |
| 859 | for (int i = 0; i < count; i++) { |
| 860 | View v = getPageAt(i); |
| 861 | if (v instanceof PagedViewGridLayout) { |
| 862 | ((PagedViewGridLayout) v).removeAllViewsOnPage(); |
| 863 | mDirtyPageContent.set(i, true); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 868 | private void cancelAllTasks() { |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 869 | // Clean up all the async tasks |
| 870 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 871 | while (iter.hasNext()) { |
| 872 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 873 | task.cancel(false); |
| 874 | iter.remove(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 875 | mDirtyPageContent.set(task.page, true); |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 876 | } |
Winson Chung | 83687b1 | 2012-04-25 16:01:01 -0700 | [diff] [blame] | 877 | mDeferredSyncWidgetPageItems.clear(); |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 880 | public void setContentType(ContentType type) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 881 | if (type == ContentType.Widgets) { |
| 882 | invalidatePageData(mNumAppsPages, true); |
| 883 | } else if (type == ContentType.Applications) { |
| 884 | invalidatePageData(0, true); |
| 885 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 888 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 889 | super.snapToPage(whichPage, delta, duration); |
| 890 | updateCurrentTab(whichPage); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 891 | |
| 892 | // Update the thread priorities given the direction lookahead |
| 893 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 894 | while (iter.hasNext()) { |
| 895 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 896 | int pageIndex = task.page; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 897 | if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) || |
| 898 | (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) { |
| 899 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 900 | } else { |
| 901 | task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); |
| 902 | } |
| 903 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | private void updateCurrentTab(int currentPage) { |
| 907 | AppsCustomizeTabHost tabHost = getTabHost(); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 908 | if (tabHost != null) { |
| 909 | String tag = tabHost.getCurrentTabTag(); |
| 910 | if (tag != null) { |
| 911 | if (currentPage >= mNumAppsPages && |
| 912 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) { |
| 913 | tabHost.setCurrentTabFromContent(ContentType.Widgets); |
| 914 | } else if (currentPage < mNumAppsPages && |
| 915 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 916 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 917 | } |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 918 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 922 | /* |
| 923 | * Apps PagedView implementation |
| 924 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 925 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 926 | int childCount = layout.getChildCount(); |
| 927 | for (int i = 0; i < childCount; ++i) { |
| 928 | layout.getChildAt(i).setVisibility(visibility); |
| 929 | } |
| 930 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 931 | private void setupPage(PagedViewCellLayout layout) { |
| 932 | layout.setCellCount(mCellCountX, mCellCountY); |
| 933 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 934 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 935 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 936 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 937 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 938 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 939 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 940 | // children that are expensive to measure, and let that happen naturally later. |
| 941 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 942 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 943 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 944 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 945 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 946 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 947 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 948 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 949 | public void syncAppsPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 950 | // ensure that we have the right number of items on the pages |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 951 | int numCells = mCellCountX * mCellCountY; |
| 952 | int startIndex = page * numCells; |
| 953 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 954 | PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 955 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 956 | layout.removeAllViewsOnPage(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 957 | ArrayList<Object> items = new ArrayList<Object>(); |
| 958 | ArrayList<Bitmap> images = new ArrayList<Bitmap>(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 959 | for (int i = startIndex; i < endIndex; ++i) { |
| 960 | ApplicationInfo info = mApps.get(i); |
| 961 | PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( |
| 962 | R.layout.apps_customize_application, layout, false); |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 963 | icon.applyFromApplicationInfo(info, true, this); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 964 | icon.setOnClickListener(this); |
| 965 | icon.setOnLongClickListener(this); |
| 966 | icon.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 967 | icon.setOnKeyListener(this); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 968 | |
| 969 | int index = i - startIndex; |
| 970 | int x = index % mCellCountX; |
| 971 | int y = index / mCellCountX; |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 972 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 973 | |
| 974 | items.add(info); |
| 975 | images.add(info.iconBitmap); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 976 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 977 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 978 | layout.createHardwareLayers(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 979 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 980 | |
| 981 | /** |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 982 | * A helper to return the priority for loading of the specified widget page. |
| 983 | */ |
| 984 | private int getWidgetPageLoadPriority(int page) { |
| 985 | // If we are snapping to another page, use that index as the target page index |
| 986 | int toPage = mCurrentPage; |
| 987 | if (mNextPage > -1) { |
| 988 | toPage = mNextPage; |
| 989 | } |
| 990 | |
| 991 | // We use the distance from the target page as an initial guess of priority, but if there |
| 992 | // are no pages of higher priority than the page specified, then bump up the priority of |
| 993 | // the specified page. |
| 994 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 995 | int minPageDiff = Integer.MAX_VALUE; |
| 996 | while (iter.hasNext()) { |
| 997 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 998 | minPageDiff = Math.abs(task.page - toPage); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | int rawPageDiff = Math.abs(page - toPage); |
| 1002 | return rawPageDiff - Math.min(rawPageDiff, minPageDiff); |
| 1003 | } |
| 1004 | /** |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1005 | * Return the appropriate thread priority for loading for a given page (we give the current |
| 1006 | * page much higher priority) |
| 1007 | */ |
| 1008 | private int getThreadPriorityForPage(int page) { |
| 1009 | // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1010 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1011 | if (pageDiff <= 0) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1012 | return Process.THREAD_PRIORITY_LESS_FAVORABLE; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1013 | } else if (pageDiff <= 1) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1014 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1015 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1016 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1017 | } |
| 1018 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1019 | private int getSleepForPage(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1020 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1021 | return Math.max(0, pageDiff * sPageSleepDelay); |
| 1022 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1023 | /** |
| 1024 | * Creates and executes a new AsyncTask to load a page of widget previews. |
| 1025 | */ |
| 1026 | private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1027 | int cellWidth, int cellHeight, int cellCountX) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1028 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1029 | // Prune all tasks that are no longer needed |
| 1030 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1031 | while (iter.hasNext()) { |
| 1032 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1033 | int taskPage = task.page; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1034 | if (taskPage < getAssociatedLowerPageBound(mCurrentPage) || |
| 1035 | taskPage > getAssociatedUpperPageBound(mCurrentPage)) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1036 | task.cancel(false); |
| 1037 | iter.remove(); |
| 1038 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1039 | task.setThreadPriority(getThreadPriorityForPage(taskPage)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1043 | // We introduce a slight delay to order the loading of side pages so that we don't thrash |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1044 | final int sleepMs = getSleepForPage(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1045 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1046 | new AsyncTaskCallback() { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1047 | @Override |
| 1048 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1049 | try { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 1050 | try { |
| 1051 | Thread.sleep(sleepMs); |
| 1052 | } catch (Exception e) {} |
| 1053 | loadWidgetPreviewsInBackground(task, data); |
| 1054 | } finally { |
| 1055 | if (task.isCancelled()) { |
| 1056 | data.cleanup(true); |
| 1057 | } |
| 1058 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1059 | } |
| 1060 | }, |
| 1061 | new AsyncTaskCallback() { |
| 1062 | @Override |
| 1063 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1064 | mRunningTasks.remove(task); |
| 1065 | if (task.isCancelled()) return; |
| 1066 | // do cleanup inside onSyncWidgetPageItems |
| 1067 | onSyncWidgetPageItems(data); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1068 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 1069 | }); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1070 | |
| 1071 | // Ensure that the task is appropriately prioritized and runs in parallel |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1072 | AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1073 | AsyncTaskPageData.Type.LoadWidgetPreviewData); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1074 | t.setThreadPriority(getThreadPriorityForPage(page)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1075 | t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); |
| 1076 | mRunningTasks.add(t); |
| 1077 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1078 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1079 | /* |
| 1080 | * Widgets PagedView implementation |
| 1081 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 1082 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1083 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 1084 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1085 | |
| 1086 | // Note: We force a measure here to get around the fact that when we do layout calculations |
Winson Chung | d52f3d8 | 2011-07-12 14:29:11 -0700 | [diff] [blame] | 1087 | // immediately after syncing, we don't have a proper width. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1088 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 1089 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1090 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1091 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1092 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1093 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1094 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) { |
| 1095 | renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 1096 | } |
Michael Jurka | 92f3d46 | 2011-11-22 21:02:29 -0800 | [diff] [blame] | 1097 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1098 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 1099 | float scale, int multiplyColor) { |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1100 | if (bitmap != null) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1101 | Canvas c = new Canvas(bitmap); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1102 | c.scale(scale, scale); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1103 | Rect oldBounds = d.copyBounds(); |
| 1104 | d.setBounds(x, y, x + w, y + h); |
| 1105 | d.draw(c); |
| 1106 | d.setBounds(oldBounds); // Restore the bounds |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 1107 | c.setBitmap(null); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1108 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1109 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1110 | private Bitmap getShortcutPreview(ResolveInfo info) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1111 | // Render the background |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1112 | int offset = 0; |
| 1113 | int bitmapSize = mAppIconSize; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1114 | Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1115 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1116 | // Render the icon |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 1117 | Drawable icon = mIconCache.getFullResIcon(info); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1118 | renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1119 | return preview; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1120 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1121 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1122 | private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId, |
| 1123 | int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) { |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1124 | // Load the preview image if possible |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1125 | String packageName = provider.getPackageName(); |
| 1126 | if (maxWidth < 0) maxWidth = Integer.MAX_VALUE; |
| 1127 | if (maxHeight < 0) maxHeight = Integer.MAX_VALUE; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1128 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1129 | Drawable drawable = null; |
| 1130 | if (previewImage != 0) { |
| 1131 | drawable = mPackageManager.getDrawable(packageName, previewImage, null); |
| 1132 | if (drawable == null) { |
| 1133 | Log.w(LOG_TAG, "Can't load widget preview drawable 0x" + |
| 1134 | Integer.toHexString(previewImage) + " for provider: " + provider); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1138 | int bitmapWidth; |
| 1139 | int bitmapHeight; |
| 1140 | boolean widgetPreviewExists = (drawable != null); |
| 1141 | if (widgetPreviewExists) { |
| 1142 | bitmapWidth = drawable.getIntrinsicWidth(); |
| 1143 | bitmapHeight = drawable.getIntrinsicHeight(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1144 | } else { |
Michael Jurka | c7e52f5 | 2012-03-26 06:20:31 -0700 | [diff] [blame] | 1145 | if (cellHSpan < 1) cellHSpan = 1; |
| 1146 | if (cellVSpan < 1) cellVSpan = 1; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1147 | // Determine the size of the bitmap for the preview image we will generate |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1148 | // TODO: This actually uses the apps customize cell layout params, where as we make want |
| 1149 | // the Workspace params for more accuracy. |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1150 | bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 1151 | bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 1152 | if (cellHSpan == cellVSpan) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1153 | // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1 |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1154 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1155 | if (cellHSpan <= 1) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1156 | bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1157 | } else { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1158 | bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1159 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1160 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | float scale = 1f; |
| 1164 | if (bitmapWidth > maxWidth) { |
| 1165 | scale = maxWidth / (float) bitmapWidth; |
| 1166 | } |
| 1167 | if (bitmapHeight * scale > maxHeight) { |
| 1168 | scale = maxHeight / (float) bitmapHeight; |
| 1169 | } |
| 1170 | if (scale != 1f) { |
| 1171 | bitmapWidth = (int) (scale * bitmapWidth); |
| 1172 | bitmapHeight = (int) (scale * bitmapHeight); |
| 1173 | } |
| 1174 | |
| 1175 | Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); |
| 1176 | |
| 1177 | if (widgetPreviewExists) { |
| 1178 | renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight); |
| 1179 | } else { |
| 1180 | // Generate a preview image if we couldn't load one |
| 1181 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
| 1182 | int smallestSide = Math.min(bitmapWidth, bitmapHeight); |
| 1183 | float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1184 | if (cellHSpan != 1 || cellVSpan != 1) { |
| 1185 | renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth, |
| 1186 | bitmapHeight); |
| 1187 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1188 | |
| 1189 | // Draw the icon in the top left corner |
| 1190 | try { |
| 1191 | Drawable icon = null; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1192 | int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2); |
| 1193 | int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1194 | if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId); |
Michael Jurka | 31234d8 | 2011-11-10 15:15:15 -0800 | [diff] [blame] | 1195 | Resources resources = mLauncher.getResources(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1196 | if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 1197 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1198 | renderDrawableToBitmap(icon, preview, hoffset, yoffset, |
| 1199 | (int) (mAppIconSize * iconScale), |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1200 | (int) (mAppIconSize * iconScale)); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1201 | } catch (Resources.NotFoundException e) {} |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1202 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1203 | return preview; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1204 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1205 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1206 | public void syncWidgetPageItems(final int page, final boolean immediate) { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1207 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1208 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1209 | // Calculate the dimensions of each cell we are giving to each widget |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1210 | final ArrayList<Object> items = new ArrayList<Object>(); |
| 1211 | int contentWidth = mWidgetSpacingLayout.getContentWidth(); |
| 1212 | final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1213 | - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1214 | int contentHeight = mWidgetSpacingLayout.getContentHeight(); |
| 1215 | final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1216 | - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1217 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1218 | // Prepare the set of widgets to load previews for in the background |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1219 | int offset = (page - mNumAppsPages) * numItemsPerPage; |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1220 | for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) { |
| 1221 | items.add(mWidgets.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1224 | // Prepopulate the pages with the other widget info, and fill in the previews later |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1225 | final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1226 | layout.setColumnCount(layout.getCellCountX()); |
| 1227 | for (int i = 0; i < items.size(); ++i) { |
| 1228 | Object rawInfo = items.get(i); |
| 1229 | PendingAddItemInfo createItemInfo = null; |
| 1230 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 1231 | R.layout.apps_customize_widget, layout, false); |
| 1232 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1233 | // Fill in the widget information |
| 1234 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
| 1235 | createItemInfo = new PendingAddWidgetInfo(info, null, null); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1236 | |
| 1237 | // Determine the widget spans and min resize spans. |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 1238 | int[] spanXY = Launcher.getSpanForWidget(mLauncher, info); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1239 | createItemInfo.spanX = spanXY[0]; |
| 1240 | createItemInfo.spanY = spanXY[1]; |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 1241 | int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1242 | createItemInfo.minSpanX = minSpanXY[0]; |
| 1243 | createItemInfo.minSpanY = minSpanXY[1]; |
| 1244 | |
| 1245 | widget.applyFromAppWidgetProviderInfo(info, -1, spanXY); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1246 | widget.setTag(createItemInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1247 | widget.setShortPressListener(this); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1248 | } else if (rawInfo instanceof ResolveInfo) { |
| 1249 | // Fill in the shortcuts information |
| 1250 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 1251 | createItemInfo = new PendingAddItemInfo(); |
| 1252 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1253 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1254 | info.activityInfo.name); |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 1255 | widget.applyFromResolveInfo(mPackageManager, info); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1256 | widget.setTag(createItemInfo); |
| 1257 | } |
| 1258 | widget.setOnClickListener(this); |
| 1259 | widget.setOnLongClickListener(this); |
| 1260 | widget.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1261 | widget.setOnKeyListener(this); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1262 | |
| 1263 | // Layout each widget |
| 1264 | int ix = i % mWidgetCountX; |
| 1265 | int iy = i / mWidgetCountX; |
| 1266 | GridLayout.LayoutParams lp = new GridLayout.LayoutParams( |
| 1267 | GridLayout.spec(iy, GridLayout.LEFT), |
| 1268 | GridLayout.spec(ix, GridLayout.TOP)); |
| 1269 | lp.width = cellWidth; |
| 1270 | lp.height = cellHeight; |
| 1271 | lp.setGravity(Gravity.TOP | Gravity.LEFT); |
| 1272 | if (ix > 0) lp.leftMargin = mWidgetWidthGap; |
| 1273 | if (iy > 0) lp.topMargin = mWidgetHeightGap; |
| 1274 | layout.addView(widget, lp); |
| 1275 | } |
| 1276 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1277 | // wait until a call on onLayout to start loading, because |
| 1278 | // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out |
| 1279 | // TODO: can we do a measure/layout immediately? |
| 1280 | layout.setOnLayoutListener(new Runnable() { |
| 1281 | public void run() { |
| 1282 | // Load the widget previews |
| 1283 | int maxPreviewWidth = cellWidth; |
| 1284 | int maxPreviewHeight = cellHeight; |
| 1285 | if (layout.getChildCount() > 0) { |
| 1286 | PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0); |
| 1287 | int[] maxSize = w.getPreviewSize(); |
| 1288 | maxPreviewWidth = maxSize[0]; |
| 1289 | maxPreviewHeight = maxSize[1]; |
| 1290 | } |
| 1291 | if (immediate) { |
| 1292 | AsyncTaskPageData data = new AsyncTaskPageData(page, items, |
| 1293 | maxPreviewWidth, maxPreviewHeight, null, null); |
| 1294 | loadWidgetPreviewsInBackground(null, data); |
| 1295 | onSyncWidgetPageItems(data); |
| 1296 | } else { |
| 1297 | prepareLoadWidgetPreviewsTask(page, items, |
| 1298 | maxPreviewWidth, maxPreviewHeight, mWidgetCountX); |
| 1299 | } |
| 1300 | } |
| 1301 | }); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1302 | } |
| 1303 | private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task, |
| 1304 | AsyncTaskPageData data) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1305 | // loadWidgetPreviewsInBackground can be called without a task to load a set of widget |
| 1306 | // previews synchronously |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1307 | if (task != null) { |
| 1308 | // Ensure that this task starts running at the correct priority |
| 1309 | task.syncThreadPriority(); |
| 1310 | } |
| 1311 | |
| 1312 | // Load each of the widget/shortcut previews |
| 1313 | ArrayList<Object> items = data.items; |
| 1314 | ArrayList<Bitmap> images = data.generatedImages; |
| 1315 | int count = items.size(); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1316 | for (int i = 0; i < count; ++i) { |
| 1317 | if (task != null) { |
| 1318 | // Ensure we haven't been cancelled yet |
| 1319 | if (task.isCancelled()) break; |
| 1320 | // Before work on each item, ensure that this task is running at the correct |
| 1321 | // priority |
| 1322 | task.syncThreadPriority(); |
| 1323 | } |
| 1324 | |
| 1325 | Object rawInfo = items.get(i); |
| 1326 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1327 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 1328 | int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info); |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 1329 | |
| 1330 | int maxWidth = Math.min(data.maxImageWidth, |
| 1331 | mWidgetSpacingLayout.estimateCellWidth(cellSpans[0])); |
| 1332 | int maxHeight = Math.min(data.maxImageHeight, |
| 1333 | mWidgetSpacingLayout.estimateCellHeight(cellSpans[1])); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1334 | Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 1335 | cellSpans[0], cellSpans[1], maxWidth, maxHeight); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1336 | images.add(b); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1337 | } else if (rawInfo instanceof ResolveInfo) { |
| 1338 | // Fill in the shortcuts information |
| 1339 | ResolveInfo info = (ResolveInfo) rawInfo; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1340 | images.add(getShortcutPreview(info)); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1343 | } |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1344 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1345 | private void onSyncWidgetPageItems(AsyncTaskPageData data) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1346 | if (mInTransition) { |
| 1347 | mDeferredSyncWidgetPageItems.add(data); |
| 1348 | return; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1349 | } |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1350 | try { |
| 1351 | int page = data.page; |
| 1352 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1353 | |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1354 | ArrayList<Object> items = data.items; |
| 1355 | int count = items.size(); |
| 1356 | for (int i = 0; i < count; ++i) { |
| 1357 | PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i); |
| 1358 | if (widget != null) { |
| 1359 | Bitmap preview = data.generatedImages.get(i); |
| 1360 | widget.applyPreview(new FastBitmapDrawable(preview), i); |
| 1361 | } |
| 1362 | } |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1363 | |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1364 | layout.createHardwareLayer(); |
| 1365 | invalidate(); |
| 1366 | |
| 1367 | // Update all thread priorities |
| 1368 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1369 | while (iter.hasNext()) { |
| 1370 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 1371 | int pageIndex = task.page; |
| 1372 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 1373 | } |
| 1374 | } finally { |
| 1375 | data.cleanup(false); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1376 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1377 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 1378 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1379 | @Override |
| 1380 | public void syncPages() { |
| 1381 | removeAllViews(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1382 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1383 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1384 | Context context = getContext(); |
| 1385 | for (int j = 0; j < mNumWidgetPages; ++j) { |
| 1386 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 1387 | mWidgetCountY); |
| 1388 | setupPage(layout); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1389 | addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT, |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1390 | LayoutParams.MATCH_PARENT)); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1393 | for (int i = 0; i < mNumAppsPages; ++i) { |
| 1394 | PagedViewCellLayout layout = new PagedViewCellLayout(context); |
| 1395 | setupPage(layout); |
| 1396 | addView(layout); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1399 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1400 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1401 | public void syncPageItems(int page, boolean immediate) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1402 | if (page < mNumAppsPages) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1403 | syncAppsPageItems(page, immediate); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1404 | } else { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1405 | syncWidgetPageItems(page, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1409 | // We want our pages to be z-ordered such that the further a page is to the left, the higher |
| 1410 | // it is in the z-order. This is important to insure touch events are handled correctly. |
| 1411 | View getPageAt(int index) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1412 | return getChildAt(indexToPage(index)); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1415 | @Override |
| 1416 | protected int indexToPage(int index) { |
| 1417 | return getChildCount() - index - 1; |
| 1418 | } |
| 1419 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1420 | // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack. |
| 1421 | @Override |
| 1422 | protected void screenScrolled(int screenCenter) { |
| 1423 | super.screenScrolled(screenCenter); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1424 | |
| 1425 | for (int i = 0; i < getChildCount(); i++) { |
| 1426 | View v = getPageAt(i); |
| 1427 | if (v != null) { |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1428 | float scrollProgress = getScrollProgress(screenCenter, v, i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1429 | |
| 1430 | float interpolatedProgress = |
| 1431 | mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0))); |
| 1432 | float scale = (1 - interpolatedProgress) + |
| 1433 | interpolatedProgress * TRANSITION_SCALE_FACTOR; |
| 1434 | float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1435 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1436 | float alpha; |
| 1437 | |
Winson Chung | d167e2a | 2012-04-26 13:13:01 -0700 | [diff] [blame] | 1438 | if (scrollProgress < 0) { |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1439 | alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation( |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1440 | 1 - Math.abs(scrollProgress)) : 1.0f; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1441 | } else { |
| 1442 | // On large screens we need to fade the page as it nears its leftmost position |
| 1443 | alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress); |
| 1444 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1445 | |
| 1446 | v.setCameraDistance(mDensity * CAMERA_DISTANCE); |
| 1447 | int pageWidth = v.getMeasuredWidth(); |
| 1448 | int pageHeight = v.getMeasuredHeight(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1449 | |
| 1450 | if (PERFORM_OVERSCROLL_ROTATION) { |
| 1451 | if (i == 0 && scrollProgress < 0) { |
| 1452 | // Overscroll to the left |
| 1453 | v.setPivotX(TRANSITION_PIVOT * pageWidth); |
| 1454 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1455 | scale = 1.0f; |
| 1456 | alpha = 1.0f; |
| 1457 | // On the first page, we don't want the page to have any lateral motion |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1458 | translationX = 0; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1459 | } else if (i == getChildCount() - 1 && scrollProgress > 0) { |
| 1460 | // Overscroll to the right |
| 1461 | v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth); |
| 1462 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1463 | scale = 1.0f; |
| 1464 | alpha = 1.0f; |
| 1465 | // On the last page, we don't want the page to have any lateral motion. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1466 | translationX = 0; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1467 | } else { |
| 1468 | v.setPivotY(pageHeight / 2.0f); |
| 1469 | v.setPivotX(pageWidth / 2.0f); |
| 1470 | v.setRotationY(0f); |
| 1471 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | v.setTranslationX(translationX); |
| 1475 | v.setScaleX(scale); |
| 1476 | v.setScaleY(scale); |
| 1477 | v.setAlpha(alpha); |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1478 | |
| 1479 | // If the view has 0 alpha, we set it to be invisible so as to prevent |
| 1480 | // it from accepting touches |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1481 | if (alpha == 0) { |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1482 | v.setVisibility(INVISIBLE); |
| 1483 | } else if (v.getVisibility() != VISIBLE) { |
| 1484 | v.setVisibility(VISIBLE); |
| 1485 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | protected void overScroll(float amount) { |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 1491 | acceleratedOverScroll(amount); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1494 | /** |
| 1495 | * Used by the parent to get the content width to set the tab bar to |
| 1496 | * @return |
| 1497 | */ |
| 1498 | public int getPageContentWidth() { |
| 1499 | return mContentWidth; |
| 1500 | } |
| 1501 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1502 | @Override |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1503 | protected void onPageEndMoving() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1504 | super.onPageEndMoving(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1505 | |
| 1506 | // We reset the save index when we change pages so that it will be recalculated on next |
| 1507 | // rotation |
| 1508 | mSaveInstanceStateItemIndex = -1; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1511 | /* |
| 1512 | * AllAppsView implementation |
| 1513 | */ |
| 1514 | @Override |
| 1515 | public void setup(Launcher launcher, DragController dragController) { |
| 1516 | mLauncher = launcher; |
| 1517 | mDragController = dragController; |
| 1518 | } |
| 1519 | @Override |
| 1520 | public void zoom(float zoom, boolean animate) { |
| 1521 | // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() |
| 1522 | } |
| 1523 | @Override |
| 1524 | public boolean isVisible() { |
| 1525 | return (getVisibility() == VISIBLE); |
| 1526 | } |
| 1527 | @Override |
| 1528 | public boolean isAnimating() { |
| 1529 | return false; |
| 1530 | } |
| 1531 | @Override |
| 1532 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 1533 | mApps = list; |
| 1534 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1535 | updatePageCounts(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1536 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1537 | // The next layout pass will trigger data-ready if both widgets and apps are set, so |
| 1538 | // request a layout to do this test and invalidate the page data when ready. |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1539 | if (testDataReady()) requestLayout(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1540 | } |
| 1541 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1542 | // We add it in place, in alphabetical order |
| 1543 | int count = list.size(); |
| 1544 | for (int i = 0; i < count; ++i) { |
| 1545 | ApplicationInfo info = list.get(i); |
| 1546 | int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 1547 | if (index < 0) { |
| 1548 | mApps.add(-(index + 1), info); |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | @Override |
| 1553 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 1554 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1555 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1556 | invalidatePageData(); |
| 1557 | } |
| 1558 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 1559 | ComponentName removeComponent = item.intent.getComponent(); |
| 1560 | int length = list.size(); |
| 1561 | for (int i = 0; i < length; ++i) { |
| 1562 | ApplicationInfo info = list.get(i); |
| 1563 | if (info.intent.getComponent().equals(removeComponent)) { |
| 1564 | return i; |
| 1565 | } |
| 1566 | } |
| 1567 | return -1; |
| 1568 | } |
| 1569 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1570 | // loop through all the apps and remove apps that have the same component |
| 1571 | int length = list.size(); |
| 1572 | for (int i = 0; i < length; ++i) { |
| 1573 | ApplicationInfo info = list.get(i); |
| 1574 | int removeIndex = findAppByComponent(mApps, info); |
| 1575 | if (removeIndex > -1) { |
| 1576 | mApps.remove(removeIndex); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | @Override |
| 1581 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 1582 | removeAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1583 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1584 | invalidatePageData(); |
| 1585 | } |
| 1586 | @Override |
| 1587 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 1588 | // We remove and re-add the updated applications list because it's properties may have |
| 1589 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 1590 | // place in the list. |
| 1591 | removeAppsWithoutInvalidate(list); |
| 1592 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1593 | updatePageCounts(); |
| 1594 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1595 | invalidatePageData(); |
| 1596 | } |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1597 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1598 | @Override |
| 1599 | public void reset() { |
Winson Chung | 649668f | 2012-01-10 13:07:16 -0800 | [diff] [blame] | 1600 | // If we have reset, then we should not continue to restore the previous state |
| 1601 | mSaveInstanceStateItemIndex = -1; |
| 1602 | |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1603 | AppsCustomizeTabHost tabHost = getTabHost(); |
| 1604 | String tag = tabHost.getCurrentTabTag(); |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 1605 | if (tag != null) { |
| 1606 | if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 1607 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 1608 | } |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1609 | } |
Winson Chung | 649668f | 2012-01-10 13:07:16 -0800 | [diff] [blame] | 1610 | |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1611 | if (mCurrentPage != 0) { |
| 1612 | invalidatePageData(0); |
| 1613 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1614 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1615 | |
| 1616 | private AppsCustomizeTabHost getTabHost() { |
| 1617 | return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane); |
| 1618 | } |
| 1619 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1620 | @Override |
| 1621 | public void dumpState() { |
| 1622 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
| 1623 | ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); |
| 1624 | dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); |
| 1625 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1626 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1627 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1628 | ArrayList<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1629 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1630 | for (Object i: list) { |
| 1631 | if (i instanceof AppWidgetProviderInfo) { |
| 1632 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 1633 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 1634 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 1635 | + " initialLayout=" + info.initialLayout |
| 1636 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 1637 | } else if (i instanceof ResolveInfo) { |
| 1638 | ResolveInfo info = (ResolveInfo) i; |
| 1639 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 1640 | + info.icon); |
| 1641 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1644 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1645 | @Override |
| 1646 | public void surrender() { |
| 1647 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 1648 | // should stop this now. |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1649 | |
| 1650 | // Stop all background tasks |
| 1651 | cancelAllTasks(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1652 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1653 | |
Winson Chung | e4e5066 | 2012-01-23 14:45:13 -0800 | [diff] [blame] | 1654 | @Override |
| 1655 | public void iconPressed(PagedViewIcon icon) { |
| 1656 | // Reset the previously pressed icon and store a reference to the pressed icon so that |
| 1657 | // we can reset it on return to Launcher (in Launcher.onResume()) |
| 1658 | if (mPressedIcon != null) { |
| 1659 | mPressedIcon.resetDrawableState(); |
| 1660 | } |
| 1661 | mPressedIcon = icon; |
| 1662 | } |
| 1663 | |
| 1664 | public void resetDrawableState() { |
| 1665 | if (mPressedIcon != null) { |
| 1666 | mPressedIcon.resetDrawableState(); |
| 1667 | mPressedIcon = null; |
| 1668 | } |
| 1669 | } |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1670 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * We load an extra page on each side to prevent flashes from scrolling and loading of the |
| 1673 | * widget previews in the background with the AsyncTasks. |
| 1674 | */ |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1675 | final static int sLookBehindPageCount = 2; |
| 1676 | final static int sLookAheadPageCount = 2; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1677 | protected int getAssociatedLowerPageBound(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1678 | final int count = getChildCount(); |
| 1679 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1680 | int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0); |
| 1681 | return windowMinIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1682 | } |
| 1683 | protected int getAssociatedUpperPageBound(int page) { |
| 1684 | final int count = getChildCount(); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1685 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1686 | int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1), |
| 1687 | count - 1); |
| 1688 | return windowMaxIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1689 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1690 | |
| 1691 | @Override |
| 1692 | protected String getCurrentPageDescription() { |
| 1693 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 1694 | int stringId = R.string.default_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1695 | int count = 0; |
| 1696 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1697 | if (page < mNumAppsPages) { |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1698 | stringId = R.string.apps_customize_apps_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1699 | count = mNumAppsPages; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1700 | } else { |
| 1701 | page -= mNumAppsPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1702 | stringId = R.string.apps_customize_widgets_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1703 | count = mNumWidgetPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1704 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1705 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1706 | return String.format(getContext().getString(stringId), page + 1, count); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1707 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1708 | } |