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