blob: d55260c86b0a23e48e3abb552c638244101776ff [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080033import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070035import android.graphics.ColorMatrix;
36import android.graphics.ColorMatrixColorFilter;
Winson Chung72d59842012-02-22 13:51:36 -080037import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070039import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080041import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070042import android.graphics.Shader;
Michael Jurkadac85912012-05-18 15:04:49 -070043import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070045import android.os.AsyncTask;
46import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.util.AttributeSet;
48import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070049import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080050import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070052import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070053import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070054import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070055import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070056import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070057import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070058import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070059import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070060
61import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import com.android.launcher2.DropTarget.DragObject;
63
Michael Jurka9bc8eba2012-05-21 20:36:44 -070064import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065import java.util.ArrayList;
66import java.util.Collections;
67import java.util.Iterator;
68import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070069
Winson Chungb44b5242011-06-13 11:32:14 -070070/**
71 * A simple callback interface which also provides the results of the task.
72 */
73interface AsyncTaskCallback {
74 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
75}
Winson Chung4e076542011-06-23 13:04:10 -070076
Winson Chungb44b5242011-06-13 11:32:14 -070077/**
78 * The data needed to perform either of the custom AsyncTasks.
79 */
80class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070081 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080082 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070083 }
84
Winson Chungb44b5242011-06-13 11:32:14 -070085 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
86 AsyncTaskCallback postR) {
87 page = p;
88 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070089 sourceImages = si;
90 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070091 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070092 doInBackgroundCallback = bgR;
93 postExecuteCallback = postR;
94 }
Michael Jurka038f9d82011-11-03 13:50:45 -070095 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070096 AsyncTaskCallback postR) {
97 page = p;
98 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070099 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 maxImageWidth = cw;
101 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 doInBackgroundCallback = bgR;
103 postExecuteCallback = postR;
104 }
Winson Chung09945932011-09-20 14:22:40 -0700105 void cleanup(boolean cancelled) {
106 // Clean up any references to source/generated bitmaps
107 if (sourceImages != null) {
108 if (cancelled) {
109 for (Bitmap b : sourceImages) {
110 b.recycle();
111 }
112 }
113 sourceImages.clear();
114 }
115 if (generatedImages != null) {
116 if (cancelled) {
117 for (Bitmap b : generatedImages) {
118 b.recycle();
119 }
120 }
121 generatedImages.clear();
122 }
123 }
Winson Chungb44b5242011-06-13 11:32:14 -0700124 int page;
125 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700126 ArrayList<Bitmap> sourceImages;
127 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700128 int maxImageWidth;
129 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700130 AsyncTaskCallback doInBackgroundCallback;
131 AsyncTaskCallback postExecuteCallback;
132}
Winson Chung4e076542011-06-23 13:04:10 -0700133
Winson Chungb44b5242011-06-13 11:32:14 -0700134/**
135 * A generic template for an async task used in AppsCustomize.
136 */
137class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700138 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700139 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700141 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 }
143 @Override
144 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
145 if (params.length != 1) return null;
146 // Load each of the widget previews in the background
147 params[0].doInBackgroundCallback.run(this, params[0]);
148 return params[0];
149 }
150 @Override
151 protected void onPostExecute(AsyncTaskPageData result) {
152 // All the widget previews are loaded, so we can just callback to inflate the page
153 result.postExecuteCallback.run(this, result);
154 }
155
156 void setThreadPriority(int p) {
157 threadPriority = p;
158 }
159 void syncThreadPriority() {
160 Process.setThreadPriority(threadPriority);
161 }
162
163 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700164 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700165 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700166 int threadPriority;
167}
Winson Chungb44b5242011-06-13 11:32:14 -0700168
Michael Jurkadac85912012-05-18 15:04:49 -0700169abstract class WeakReferenceThreadLocal<T> {
170 private ThreadLocal<WeakReference<T>> mThreadLocal;
171 public WeakReferenceThreadLocal() {
172 mThreadLocal = new ThreadLocal<WeakReference<T>>();
173 }
174
175 abstract T initialValue();
176
177 public void set(T t) {
178 mThreadLocal.set(new WeakReference<T>(t));
179 }
180
181 public T get() {
182 WeakReference<T> reference = mThreadLocal.get();
183 T obj;
184 if (reference == null) {
185 obj = initialValue();
186 mThreadLocal.set(new WeakReference<T>(obj));
187 return obj;
188 } else {
189 obj = reference.get();
190 if (obj == null) {
191 obj = initialValue();
192 mThreadLocal.set(new WeakReference<T>(obj));
193 }
194 return obj;
195 }
196 }
197}
198
199class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
200 @Override
201 protected Canvas initialValue() {
202 return new Canvas();
203 }
204}
205
206class PaintCache extends WeakReferenceThreadLocal<Paint> {
207 @Override
208 protected Paint initialValue() {
209 return null;
210 }
211}
212
213class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
214 @Override
215 protected Bitmap initialValue() {
216 return null;
217 }
218}
219
220class RectCache extends WeakReferenceThreadLocal<Rect> {
221 @Override
222 protected Rect initialValue() {
223 return new Rect();
224 }
225}
226
Winson Chungb44b5242011-06-13 11:32:14 -0700227/**
228 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
229 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700230public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700231 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700232 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
233 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700234 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700235
236 /**
237 * The different content types that this paged view can show.
238 */
239 public enum ContentType {
240 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700241 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700242 }
243
244 // Refs
245 private Launcher mLauncher;
246 private DragController mDragController;
247 private final LayoutInflater mLayoutInflater;
248 private final PackageManager mPackageManager;
249
Winson Chung5afbf7b2011-07-25 11:53:08 -0700250 // Save and Restore
251 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800252 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700253
Winson Chung785d2eb2011-04-14 16:08:02 -0700254 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700255 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700256 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700257
Winson Chung7d7541e2011-09-16 20:14:36 -0700258 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800259 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700260 private int mClingFocusedX;
261 private int mClingFocusedY;
262
Winson Chung1ed747a2011-05-03 16:18:34 -0700263 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700264 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700265 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700266
267 // Dimens
268 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700269 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800270 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700271 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700272 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700273 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700274 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700275 private int mNumAppsPages;
276 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700277
Adam Cohen22f823d2011-09-01 17:22:18 -0700278 // Relating to the scroll and overscroll effects
279 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700280 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700281 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700282 private static float TRANSITION_PIVOT = 0.65f;
283 private static float TRANSITION_MAX_ROTATION = 22;
284 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700285 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700286 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700287
Winson Chungb44b5242011-06-13 11:32:14 -0700288 // Previews & outlines
289 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800290 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700291
Adam Cohened66b2b2012-01-23 17:28:51 -0800292 private Runnable mInflateWidgetRunnable = null;
293 private Runnable mBindWidgetRunnable = null;
294 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700295 static final int WIDGET_PRELOAD_PENDING = 0;
296 static final int WIDGET_BOUND = 1;
297 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800298 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
299 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800300 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800301 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800302
Michael Jurka39e5d172012-03-12 18:36:12 -0700303 // Deferral of loading widget previews during launcher transitions
304 private boolean mInTransition;
305 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
306 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700307 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
308 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700309
Michael Jurkadac85912012-05-18 15:04:49 -0700310 // Used for drawing shortcut previews
311 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
312 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
313 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
314
315 // Used for drawing widget previews
316 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
317 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
318 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
319 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
320
Winson Chung785d2eb2011-04-14 16:08:02 -0700321 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
322 super(context, attrs);
323 mLayoutInflater = LayoutInflater.from(context);
324 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700325 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700326 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700327 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700328 mCanvas = new Canvas();
329 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700330
331 // Save the default widget preview background
332 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700333 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700334
Winson Chung6032e7e2011-11-08 15:47:17 -0800335 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
336 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
337 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700338 mWidgetWidthGap =
339 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
340 mWidgetHeightGap =
341 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700342 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
343 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700344 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
345 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700346 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700347 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700348
Winson Chung1ed747a2011-05-03 16:18:34 -0700349 // The padding on the non-matched dimension for the default widget preview icons
350 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700351 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700352
353 // Unless otherwise specified this view is important for accessibility.
354 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
355 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
356 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700357 }
358
359 @Override
360 protected void init() {
361 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700362 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700363
364 Context context = getContext();
365 Resources r = context.getResources();
366 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
367 }
368
Winson Chungf0ea4d32011-06-06 14:27:16 -0700369 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700370 protected void onUnhandledTap(MotionEvent ev) {
371 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700372 // Dismiss AppsCustomize if we tap
373 mLauncher.showWorkspace(true);
374 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 }
376
Winson Chung5afbf7b2011-07-25 11:53:08 -0700377 /** Returns the item index of the center item on this page so that we can restore to this
378 * item index when we rotate. */
379 private int getMiddleComponentIndexOnCurrentPage() {
380 int i = -1;
381 if (getPageCount() > 0) {
382 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700383 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700384 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700385 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
386 int numItemsPerPage = mCellCountX * mCellCountY;
387 int childCount = childrenLayout.getChildCount();
388 if (childCount > 0) {
389 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700390 }
391 } else {
392 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700393 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700394 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
395 int childCount = layout.getChildCount();
396 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700397 i = numApps +
398 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
399 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700400 }
401 }
402 return i;
403 }
404
405 /** Get the index of the item to restore to if we need to restore the current page. */
406 int getSaveInstanceStateIndex() {
407 if (mSaveInstanceStateItemIndex == -1) {
408 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
409 }
410 return mSaveInstanceStateItemIndex;
411 }
412
413 /** Returns the page in the current orientation which is expected to contain the specified
414 * item index. */
415 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700416 if (index < 0) return 0;
417
418 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700419 int numItemsPerPage = mCellCountX * mCellCountY;
420 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700421 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700422 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700423 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
424 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700425 }
426
Winson Chung5afbf7b2011-07-25 11:53:08 -0700427 /** Restores the page for an item at the specified index */
428 void restorePageForIndex(int index) {
429 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700430 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700431 }
432
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700433 private void updatePageCounts() {
434 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
435 (float) (mWidgetCountX * mWidgetCountY));
436 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
437 }
438
Winson Chungf0ea4d32011-06-06 14:27:16 -0700439 protected void onDataReady(int width, int height) {
440 // Note that we transpose the counts in portrait so that we get a similar layout
441 boolean isLandscape = getResources().getConfiguration().orientation ==
442 Configuration.ORIENTATION_LANDSCAPE;
443 int maxCellCountX = Integer.MAX_VALUE;
444 int maxCellCountY = Integer.MAX_VALUE;
445 if (LauncherApplication.isScreenLarge()) {
446 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
447 LauncherModel.getCellCountY());
448 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
449 LauncherModel.getCellCountX());
450 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800451 if (mMaxAppCellCountX > -1) {
452 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
453 }
454 if (mMaxAppCellCountY > -1) {
455 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
456 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457
458 // Now that the data is ready, we can calculate the content width, the number of cells to
459 // use for each page
460 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
461 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
462 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
463 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
464 mCellCountX = mWidgetSpacingLayout.getCellCountX();
465 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700466 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700467
Winson Chungdb1138b2011-06-30 14:39:35 -0700468 // Force a measure to update recalculate the gaps
469 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
470 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
471 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700473
Michael Jurkae326f182011-11-21 14:05:46 -0800474 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
475 final boolean hostIsTransitioning = host.isTransitioning();
476
Adam Cohen0cd3b642011-10-14 14:58:00 -0700477 // Restore the page
478 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800479 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700480
Winson Chung3f4e1422011-11-17 14:58:51 -0800481 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
482 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
483 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800484 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800485 post(new Runnable() {
486 @Override
487 public void run() {
488 showAllAppsCling();
489 }
490 });
491 }
492 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700493
Winson Chung3f4e1422011-11-17 14:58:51 -0800494 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700495 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800496 mHasShownAllAppsCling = true;
497 // Calculate the position for the cling punch through
498 int[] offset = new int[2];
499 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
500 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
501 // PagedViews are centered horizontally but top aligned
502 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
503 offset[0];
504 pos[1] += offset[1];
505 mLauncher.showFirstRunAllAppsCling(pos);
506 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700507 }
508
509 @Override
510 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
511 int width = MeasureSpec.getSize(widthMeasureSpec);
512 int height = MeasureSpec.getSize(heightMeasureSpec);
513 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700514 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700515 setDataIsReady();
516 setMeasuredDimension(width, height);
517 onDataReady(width, height);
518 }
519 }
520
521 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
522 }
523
Winson Chung785d2eb2011-04-14 16:08:02 -0700524 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700525 // Get the list of widgets and shortcuts
526 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700527 List<AppWidgetProviderInfo> widgets =
528 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
530 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800531 for (AppWidgetProviderInfo widget : widgets) {
532 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700533 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700534 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
535 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700536 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
537 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700538 if (minSpanX <= LauncherModel.getCellCountX() &&
539 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700540 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700541 } else {
542 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
543 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700544 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800545 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700546 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800547 widget.minWidth + ", " + widget.minHeight + ")");
548 }
549 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700550 mWidgets.addAll(shortcuts);
551 Collections.sort(mWidgets,
552 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700553 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700554 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700555 }
556
557 @Override
558 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700559 // When we have exited all apps or are in transition, disregard clicks
560 if (!mLauncher.isAllAppsCustomizeOpen() ||
561 mLauncher.getWorkspace().isSwitchingState()) return;
562
Winson Chung4b576be2011-04-27 17:40:20 -0700563 if (v instanceof PagedViewIcon) {
564 // Animate some feedback to the click
565 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800566
567 // Lock the drawable state to pressed until we return to Launcher
568 if (mPressedIcon != null) {
569 mPressedIcon.lockDrawableState();
570 }
Winson Chungc7450e32012-04-17 17:34:08 -0700571
Winson Chung18f41f82012-05-09 13:28:10 -0700572 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
573 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
574 // when Launcher resumes and we are still in AllApps.
575 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700576 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
577
Winson Chung4b576be2011-04-27 17:40:20 -0700578 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700579 // Let the user know that they have to long press to add a widget
580 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
581 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700582
Winson Chungd2e87b32011-06-02 10:53:07 -0700583 // Create a little animation to show that the widget can move
584 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
585 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700586 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
587 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700588 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700589 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700590 tydAnim.setDuration(100);
591 bounce.play(tyuAnim).before(tydAnim);
592 bounce.setInterpolator(new AccelerateInterpolator());
593 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700594 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700595 }
596
Winson Chungc6f10b92011-11-14 11:39:07 -0800597 public boolean onKey(View v, int keyCode, KeyEvent event) {
598 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
599 }
600
Winson Chung785d2eb2011-04-14 16:08:02 -0700601 /*
602 * PagedViewWithDraggableItems implementation
603 */
604 @Override
605 protected void determineDraggingStart(android.view.MotionEvent ev) {
606 // Disable dragging by pulling an app down for now.
607 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700608
Winson Chung4b576be2011-04-27 17:40:20 -0700609 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700610 mLauncher.getWorkspace().onDragStartedWithItem(v);
611 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700612 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700613
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700614 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 final AppWidgetProviderInfo pInfo = info.info;
616 if (pInfo.configure != null) {
617 return;
618 }
619
Adam Cohen21a170b2012-05-30 15:17:06 -0700620 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 mBindWidgetRunnable = new Runnable() {
622 @Override
623 public void run() {
624 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700625 if (AppWidgetManager.getInstance(mLauncher)
626 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
627 mWidgetCleanupState = WIDGET_BOUND;
628 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800629 }
630 };
631 post(mBindWidgetRunnable);
632
633 mInflateWidgetRunnable = new Runnable() {
634 @Override
635 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 AppWidgetHostView hostView = mLauncher.
637 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 info.boundWidget = hostView;
639 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800640 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700641 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
642 info.spanY, info, false);
643
644 // We want the first widget layout to be the correct size. This will be important
645 // for width size reporting to the AppWidgetManager.
646 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
647 unScaledSize[1]);
648 lp.x = lp.y = 0;
649 lp.customPosition = true;
650 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800651 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 }
653 };
654 post(mInflateWidgetRunnable);
655 }
656
657 @Override
658 public void onShortPress(View v) {
659 // We are anticipating a long press, and we use this time to load bind and instantiate
660 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700661 if (mCreateWidgetInfo != null) {
662 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
663 cleanupWidgetPreloading(false);
664 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800665 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700666 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 }
668
Adam Cohen0e56cc92012-05-11 15:57:05 -0700669 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
670 if (!widgetWasAdded) {
671 // If the widget was not added, we may need to do further cleanup.
672 PendingAddWidgetInfo info = mCreateWidgetInfo;
673 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700674
675 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700676 // We never did any preloading, so just remove pending callbacks to do so
677 removeCallbacks(mBindWidgetRunnable);
678 removeCallbacks(mInflateWidgetRunnable);
679 } else if (mWidgetCleanupState == WIDGET_BOUND) {
680 // Delete the widget id which was allocated
681 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700682 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
683 }
684
685 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700686 removeCallbacks(mInflateWidgetRunnable);
687 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700688 // Delete the widget id which was allocated
689 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700690 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
691 }
692
Adam Cohen0e56cc92012-05-11 15:57:05 -0700693 // The widget was inflated and added to the DragLayer -- remove it.
694 AppWidgetHostView widget = info.boundWidget;
695 mLauncher.getDragLayer().removeView(widget);
696 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 }
698 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
699 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700700 mCreateWidgetInfo = null;
701 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 }
703
Adam Cohen7a326642012-02-22 12:03:22 -0800704 @Override
705 public void cleanUpShortPress(View v) {
706 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700707 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800708 }
709 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800710
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700711 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800712 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700713 // Get the widget preview as the drag representation
714 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700715 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700716
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700717 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
718 // we abort the drag.
719 if (image.getDrawable() == null) {
720 mDraggingWidget = false;
721 return false;
722 }
723
Winson Chung4b576be2011-04-27 17:40:20 -0700724 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800725 Bitmap preview;
726 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800727 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700728 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700729 // This can happen in some weird cases involving multi-touch. We can't start dragging
730 // the widget if this is null, so we break out.
731 if (mCreateWidgetInfo == null) {
732 return false;
733 }
734
Adam Cohen1b36dc32012-02-13 19:27:37 -0800735 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
736 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700737 int spanX = createItemInfo.spanX;
738 int spanY = createItemInfo.spanY;
739 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
740 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700741
Winson Chung72d59842012-02-22 13:51:36 -0800742 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
743 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700744 int maxWidth, maxHeight;
745 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
746 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800747 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700748 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800749
750 // Determine the image view drawable scale relative to the preview
751 float[] mv = new float[9];
752 Matrix m = new Matrix();
753 m.setRectToRect(
754 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
755 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
756 (float) previewDrawable.getIntrinsicHeight()),
757 Matrix.ScaleToFit.START);
758 m.getValues(mv);
759 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700760 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700761 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
762 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
763 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
764 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
765
Winson Chung1120e032011-11-22 16:11:31 -0800766 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700767 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700768 renderDrawableToBitmap(icon, preview, 0, 0,
769 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700770 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700771 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700772 createItemInfo.spanX = createItemInfo.spanY = 1;
773 }
Winson Chung4b576be2011-04-27 17:40:20 -0700774
Michael Jurka8c3339b2012-06-14 16:18:21 -0700775 // Don't clip alpha values for the drag outline if we're using the default widget preview
776 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
777 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700778
Winson Chung1120e032011-11-22 16:11:31 -0800779 // Save the preview for the outline generation, then dim the preview
780 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
781 false);
Winson Chung1120e032011-11-22 16:11:31 -0800782
Winson Chung4b576be2011-04-27 17:40:20 -0700783 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700784 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700785 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800786 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800787 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800788 outline.recycle();
789 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700790 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700791 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800792
Winson Chung4b576be2011-04-27 17:40:20 -0700793 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700795 if (!super.beginDragging(v)) return false;
796
797 if (v instanceof PagedViewIcon) {
798 beginDraggingApplication(v);
799 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700800 if (!beginDraggingWidget(v)) {
801 return false;
802 }
Winson Chung4b576be2011-04-27 17:40:20 -0700803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804
805 // We delay entering spring-loaded mode slightly to make sure the UI
806 // thready is free of any work.
807 postDelayed(new Runnable() {
808 @Override
809 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800810 // We don't enter spring-loaded mode if the drag has been cancelled
811 if (mLauncher.getDragController().isDragging()) {
812 // Dismiss the cling
813 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800814
Adam Cohen1b36dc32012-02-13 19:27:37 -0800815 // Reset the alpha on the dragged icon before we drag
816 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800817
Adam Cohen1b36dc32012-02-13 19:27:37 -0800818 // Go into spring loaded mode (must happen before we startDrag())
819 mLauncher.enterSpringLoadedDragMode();
820 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800821 }
Winson Chung72d59842012-02-22 13:51:36 -0800822 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800823
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 return true;
825 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800826
Winson Chunga48487a2012-03-20 16:19:37 -0700827 /**
828 * Clean up after dragging.
829 *
830 * @param target where the item was dragged to (can be null if the item was flung)
831 */
832 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700833 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700834 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700835 // Exit spring loaded mode if we have not successfully dropped or have not handled the
836 // drop in Workspace
837 mLauncher.exitSpringLoadedDragMode();
838 }
Winson Chung4b919f82012-05-01 10:44:08 -0700839 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700840 }
841
Winson Chung785d2eb2011-04-14 16:08:02 -0700842 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700843 public View getContent() {
844 return null;
845 }
846
847 @Override
848 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700849 mInTransition = true;
850 if (toWorkspace) {
851 cancelAllTasks();
852 }
853 }
854
855 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700856 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700857 }
858
859 @Override
860 public void onLauncherTransitionStep(Launcher l, float t) {
861 }
862
863 @Override
864 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
865 mInTransition = false;
866 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
867 onSyncWidgetPageItems(d);
868 }
869 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700870 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
871 r.run();
872 }
873 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700874 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700875 }
876
877 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700878 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
879 boolean success) {
880 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
881 if (isFlingToDelete) return;
882
883 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700884
885 // Display an error message if the drag failed due to there not being enough space on the
886 // target layout we were dropping on.
887 if (!success) {
888 boolean showOutOfSpaceMessage = false;
889 if (target instanceof Workspace) {
890 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
891 Workspace workspace = (Workspace) target;
892 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700893 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700894 if (layout != null) {
895 layout.calculateSpans(itemInfo);
896 showOutOfSpaceMessage =
897 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
898 }
899 }
Winson Chungfc79c802011-05-02 13:35:34 -0700900 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700901 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700902 }
Adam Cohen7a326642012-02-22 12:03:22 -0800903
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800904 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700905 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700906 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800907 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700908 }
909
Winson Chunga48487a2012-03-20 16:19:37 -0700910 @Override
911 public void onFlingToDeleteCompleted() {
912 // We just dismiss the drag when we fling, so cleanup here
913 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700914 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700915 mDraggingWidget = false;
916 }
917
918 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800919 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700920 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800921 }
922
Winson Chung7f0acdd2011-09-19 18:34:19 -0700923 @Override
924 protected void onDetachedFromWindow() {
925 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700926 cancelAllTasks();
927 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700928
Michael Jurkae326f182011-11-21 14:05:46 -0800929 public void clearAllWidgetPages() {
930 cancelAllTasks();
931 int count = getChildCount();
932 for (int i = 0; i < count; i++) {
933 View v = getPageAt(i);
934 if (v instanceof PagedViewGridLayout) {
935 ((PagedViewGridLayout) v).removeAllViewsOnPage();
936 mDirtyPageContent.set(i, true);
937 }
938 }
939 }
940
Adam Cohen0cd3b642011-10-14 14:58:00 -0700941 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700942 // Clean up all the async tasks
943 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
944 while (iter.hasNext()) {
945 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
946 task.cancel(false);
947 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700948 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700949
950 // We've already preallocated the views for the data to load into, so clear them as well
951 View v = getPageAt(task.page);
952 if (v instanceof PagedViewGridLayout) {
953 ((PagedViewGridLayout) v).removeAllViewsOnPage();
954 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700955 }
Winson Chung83687b12012-04-25 16:01:01 -0700956 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700957 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700958 }
959
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700961 if (type == ContentType.Widgets) {
962 invalidatePageData(mNumAppsPages, true);
963 } else if (type == ContentType.Applications) {
964 invalidatePageData(0, true);
965 }
Winson Chungb44b5242011-06-13 11:32:14 -0700966 }
967
Adam Cohen0cd3b642011-10-14 14:58:00 -0700968 protected void snapToPage(int whichPage, int delta, int duration) {
969 super.snapToPage(whichPage, delta, duration);
970 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800971
972 // Update the thread priorities given the direction lookahead
973 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
974 while (iter.hasNext()) {
975 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700976 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800977 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
978 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
979 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
980 } else {
981 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
982 }
983 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700984 }
985
986 private void updateCurrentTab(int currentPage) {
987 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800988 if (tabHost != null) {
989 String tag = tabHost.getCurrentTabTag();
990 if (tag != null) {
991 if (currentPage >= mNumAppsPages &&
992 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
993 tabHost.setCurrentTabFromContent(ContentType.Widgets);
994 } else if (currentPage < mNumAppsPages &&
995 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
996 tabHost.setCurrentTabFromContent(ContentType.Applications);
997 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700998 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700999 }
1000 }
1001
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 /*
1003 * Apps PagedView implementation
1004 */
Winson Chung63257c12011-05-05 17:06:13 -07001005 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1006 int childCount = layout.getChildCount();
1007 for (int i = 0; i < childCount; ++i) {
1008 layout.getChildAt(i).setVisibility(visibility);
1009 }
1010 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001011 private void setupPage(PagedViewCellLayout layout) {
1012 layout.setCellCount(mCellCountX, mCellCountY);
1013 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1014 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1015 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1016
Winson Chung63257c12011-05-05 17:06:13 -07001017 // Note: We force a measure here to get around the fact that when we do layout calculations
1018 // immediately after syncing, we don't have a proper width. That said, we already know the
1019 // expected page width, so we can actually optimize by hiding all the TextView-based
1020 // children that are expensive to measure, and let that happen naturally later.
1021 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001022 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001024 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001026 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001028
Winson Chungf314b0e2011-08-16 11:54:27 -07001029 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001031 int numCells = mCellCountX * mCellCountY;
1032 int startIndex = page * numCells;
1033 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001034 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001035
Winson Chung785d2eb2011-04-14 16:08:02 -07001036 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001037 ArrayList<Object> items = new ArrayList<Object>();
1038 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001039 for (int i = startIndex; i < endIndex; ++i) {
1040 ApplicationInfo info = mApps.get(i);
1041 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1042 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001043 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001044 icon.setOnClickListener(this);
1045 icon.setOnLongClickListener(this);
1046 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001047 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001048
1049 int index = i - startIndex;
1050 int x = index % mCellCountX;
1051 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001052 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001053
1054 items.add(info);
1055 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001056 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001057
Winson Chungf0ea4d32011-06-06 14:27:16 -07001058 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001059 }
Winson Chungb44b5242011-06-13 11:32:14 -07001060
1061 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001062 * A helper to return the priority for loading of the specified widget page.
1063 */
1064 private int getWidgetPageLoadPriority(int page) {
1065 // If we are snapping to another page, use that index as the target page index
1066 int toPage = mCurrentPage;
1067 if (mNextPage > -1) {
1068 toPage = mNextPage;
1069 }
1070
1071 // We use the distance from the target page as an initial guess of priority, but if there
1072 // are no pages of higher priority than the page specified, then bump up the priority of
1073 // the specified page.
1074 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1075 int minPageDiff = Integer.MAX_VALUE;
1076 while (iter.hasNext()) {
1077 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001078 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001079 }
1080
1081 int rawPageDiff = Math.abs(page - toPage);
1082 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1083 }
1084 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001085 * Return the appropriate thread priority for loading for a given page (we give the current
1086 * page much higher priority)
1087 */
1088 private int getThreadPriorityForPage(int page) {
1089 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001090 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001091 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001092 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001093 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001094 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001095 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001096 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001097 }
1098 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001099 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001100 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001101 return Math.max(0, pageDiff * sPageSleepDelay);
1102 }
Winson Chungb44b5242011-06-13 11:32:14 -07001103 /**
1104 * Creates and executes a new AsyncTask to load a page of widget previews.
1105 */
1106 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001107 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001108
Winson Chungb44b5242011-06-13 11:32:14 -07001109 // Prune all tasks that are no longer needed
1110 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1111 while (iter.hasNext()) {
1112 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001113 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001114 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1115 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001116 task.cancel(false);
1117 iter.remove();
1118 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001119 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001120 }
1121 }
1122
Winson Chungf314b0e2011-08-16 11:54:27 -07001123 // We introduce a slight delay to order the loading of side pages so that we don't thrash
Michael Jurka39e5d172012-03-12 18:36:12 -07001124 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001125 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001126 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001127 @Override
1128 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001129 try {
Winson Chung09945932011-09-20 14:22:40 -07001130 try {
1131 Thread.sleep(sleepMs);
1132 } catch (Exception e) {}
1133 loadWidgetPreviewsInBackground(task, data);
1134 } finally {
1135 if (task.isCancelled()) {
1136 data.cleanup(true);
1137 }
1138 }
Winson Chungb44b5242011-06-13 11:32:14 -07001139 }
1140 },
1141 new AsyncTaskCallback() {
1142 @Override
1143 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001144 mRunningTasks.remove(task);
1145 if (task.isCancelled()) return;
1146 // do cleanup inside onSyncWidgetPageItems
1147 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001148 }
Winson Chung09945932011-09-20 14:22:40 -07001149 });
Winson Chungb44b5242011-06-13 11:32:14 -07001150
1151 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001152 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001153 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001154 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001155 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1156 mRunningTasks.add(t);
1157 }
Winson Chungb44b5242011-06-13 11:32:14 -07001158
Winson Chung785d2eb2011-04-14 16:08:02 -07001159 /*
1160 * Widgets PagedView implementation
1161 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001162 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001163 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1164 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001165
1166 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001167 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001168 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1169 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001170 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001171 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001172 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001173
Winson Chung5fc72b32011-10-11 17:53:58 -07001174 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001175 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001176 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001177
Winson Chung5fc72b32011-10-11 17:53:58 -07001178 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001179 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001180 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001181 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001182 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001183 Rect oldBounds = d.copyBounds();
1184 d.setBounds(x, y, x + w, y + h);
1185 d.draw(c);
1186 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001187 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001188 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001189 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001190
Michael Jurkadac85912012-05-18 15:04:49 -07001191 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1192 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1193 final Canvas c = mCachedShortcutPreviewCanvas.get();
1194 if (tempBitmap == null ||
1195 tempBitmap.getWidth() != maxWidth ||
1196 tempBitmap.getHeight() != maxHeight) {
1197 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1198 mCachedShortcutPreviewBitmap.set(tempBitmap);
1199 } else {
1200 c.setBitmap(tempBitmap);
1201 c.drawColor(0, PorterDuff.Mode.CLEAR);
1202 c.setBitmap(null);
1203 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001204 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001205 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001206
1207 int paddingTop =
1208 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1209 int paddingLeft =
1210 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1211 int paddingRight =
1212 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1213
1214 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001215
1216 renderDrawableToBitmap(
1217 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1218
1219 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1220 c.setBitmap(preview);
1221 Paint p = mCachedShortcutPreviewPaint.get();
1222 if (p == null) {
1223 p = new Paint();
1224 ColorMatrix colorMatrix = new ColorMatrix();
1225 colorMatrix.setSaturation(0);
1226 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1227 p.setAlpha((int) (255 * 0.06f));
1228 //float density = 1f;
1229 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1230 mCachedShortcutPreviewPaint.set(p);
1231 }
1232 c.drawBitmap(tempBitmap, 0, 0, p);
1233 c.setBitmap(null);
1234
1235 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1236
Winson Chungb44b5242011-06-13 11:32:14 -07001237 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001238 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001239
Michael Jurkadac85912012-05-18 15:04:49 -07001240 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1241 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1242 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001243 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 String packageName = provider.getPackageName();
1245 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1246 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001247
Michael Jurka038f9d82011-11-03 13:50:45 -07001248 Drawable drawable = null;
1249 if (previewImage != 0) {
1250 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1251 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001252 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001253 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001254 }
1255 }
1256
Michael Jurka038f9d82011-11-03 13:50:45 -07001257 int bitmapWidth;
1258 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001259 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001260 boolean widgetPreviewExists = (drawable != null);
1261 if (widgetPreviewExists) {
1262 bitmapWidth = drawable.getIntrinsicWidth();
1263 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001264 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001265 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001266 if (cellHSpan < 1) cellHSpan = 1;
1267 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001268
1269 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1270 .getDrawable(R.drawable.widget_preview_tile);
1271 final int previewDrawableWidth = previewDrawable
1272 .getIntrinsicWidth();
1273 final int previewDrawableHeight = previewDrawable
1274 .getIntrinsicHeight();
1275 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1276 bitmapHeight = previewDrawableHeight * cellVSpan;
1277
1278 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1279 Config.ARGB_8888);
1280 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1281 c.setBitmap(defaultPreview);
1282 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1283 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1284 Shader.TileMode.REPEAT);
1285 previewDrawable.draw(c);
1286 c.setBitmap(null);
1287
1288 // Draw the icon in the top left corner
1289 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1290 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1291 float iconScale = Math.min((float) smallestSide
1292 / (mAppIconSize + 2 * minOffset), 1f);
1293
1294 try {
1295 Drawable icon = null;
1296 int hoffset =
1297 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1298 int yoffset =
1299 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1300 if (iconId > 0)
1301 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001302 if (icon != null) {
1303 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1304 yoffset, (int) (mAppIconSize * iconScale),
1305 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001306 }
Michael Jurkadac85912012-05-18 15:04:49 -07001307 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001308 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001309 }
1310
Michael Jurkadac85912012-05-18 15:04:49 -07001311 // Scale to fit width only - let the widget preview be clipped in the
1312 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001313 float scale = 1f;
1314 if (bitmapWidth > maxWidth) {
1315 scale = maxWidth / (float) bitmapWidth;
1316 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001317 if (scale != 1f) {
1318 bitmapWidth = (int) (scale * bitmapWidth);
1319 bitmapHeight = (int) (scale * bitmapHeight);
1320 }
1321
Michael Jurkadac85912012-05-18 15:04:49 -07001322 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1323 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001324
Michael Jurkadac85912012-05-18 15:04:49 -07001325 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001326 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001327 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1328 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001329 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001330 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1331 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1332 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1333 c.setBitmap(preview);
1334 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1335 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1336
1337 Paint p = mCachedAppWidgetPreviewPaint.get();
1338 if (p == null) {
1339 p = new Paint();
1340 p.setFilterBitmap(true);
1341 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001342 }
Michael Jurkadac85912012-05-18 15:04:49 -07001343 c.drawBitmap(defaultPreview, src, dest, p);
1344 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001345 }
Winson Chungb44b5242011-06-13 11:32:14 -07001346 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001348
Michael Jurka038f9d82011-11-03 13:50:45 -07001349 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001350 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001351
Winson Chungd2945262011-06-24 15:22:14 -07001352 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001353 final ArrayList<Object> items = new ArrayList<Object>();
1354 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1355 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001356 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001357 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1358 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001359 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001360
Winson Chunge4a647f2011-09-30 14:41:25 -07001361 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001362 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001363 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1364 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001365 }
1366
Winson Chunge4a647f2011-09-30 14:41:25 -07001367 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001368 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001369 layout.setColumnCount(layout.getCellCountX());
1370 for (int i = 0; i < items.size(); ++i) {
1371 Object rawInfo = items.get(i);
1372 PendingAddItemInfo createItemInfo = null;
1373 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1374 R.layout.apps_customize_widget, layout, false);
1375 if (rawInfo instanceof AppWidgetProviderInfo) {
1376 // Fill in the widget information
1377 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1378 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001379
1380 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001381 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001382 createItemInfo.spanX = spanXY[0];
1383 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001384 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001385 createItemInfo.minSpanX = minSpanXY[0];
1386 createItemInfo.minSpanY = minSpanXY[1];
1387
1388 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001389 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001390 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001391 } else if (rawInfo instanceof ResolveInfo) {
1392 // Fill in the shortcuts information
1393 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001394 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001395 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1396 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1397 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001398 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001399 widget.setTag(createItemInfo);
1400 }
1401 widget.setOnClickListener(this);
1402 widget.setOnLongClickListener(this);
1403 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001404 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001405
1406 // Layout each widget
1407 int ix = i % mWidgetCountX;
1408 int iy = i / mWidgetCountX;
1409 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1410 GridLayout.spec(iy, GridLayout.LEFT),
1411 GridLayout.spec(ix, GridLayout.TOP));
1412 lp.width = cellWidth;
1413 lp.height = cellHeight;
1414 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1415 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1416 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1417 layout.addView(widget, lp);
1418 }
1419
Michael Jurka038f9d82011-11-03 13:50:45 -07001420 // wait until a call on onLayout to start loading, because
1421 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1422 // TODO: can we do a measure/layout immediately?
1423 layout.setOnLayoutListener(new Runnable() {
1424 public void run() {
1425 // Load the widget previews
1426 int maxPreviewWidth = cellWidth;
1427 int maxPreviewHeight = cellHeight;
1428 if (layout.getChildCount() > 0) {
1429 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1430 int[] maxSize = w.getPreviewSize();
1431 maxPreviewWidth = maxSize[0];
1432 maxPreviewHeight = maxSize[1];
1433 }
1434 if (immediate) {
1435 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1436 maxPreviewWidth, maxPreviewHeight, null, null);
1437 loadWidgetPreviewsInBackground(null, data);
1438 onSyncWidgetPageItems(data);
1439 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001440 if (mInTransition) {
1441 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1442 } else {
1443 prepareLoadWidgetPreviewsTask(page, items,
1444 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1445 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001446 }
1447 }
1448 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001449 }
1450 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1451 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001452 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1453 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001454 if (task != null) {
1455 // Ensure that this task starts running at the correct priority
1456 task.syncThreadPriority();
1457 }
1458
1459 // Load each of the widget/shortcut previews
1460 ArrayList<Object> items = data.items;
1461 ArrayList<Bitmap> images = data.generatedImages;
1462 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001463 for (int i = 0; i < count; ++i) {
1464 if (task != null) {
1465 // Ensure we haven't been cancelled yet
1466 if (task.isCancelled()) break;
1467 // Before work on each item, ensure that this task is running at the correct
1468 // priority
1469 task.syncThreadPriority();
1470 }
1471
1472 Object rawInfo = items.get(i);
1473 if (rawInfo instanceof AppWidgetProviderInfo) {
1474 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001475 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001476
1477 int maxWidth = Math.min(data.maxImageWidth,
1478 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1479 int maxHeight = Math.min(data.maxImageHeight,
1480 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001481 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001482 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001483 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001484 } else if (rawInfo instanceof ResolveInfo) {
1485 // Fill in the shortcuts information
1486 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001487 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001488 }
1489 }
Winson Chungb44b5242011-06-13 11:32:14 -07001490 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001491
Winson Chungb44b5242011-06-13 11:32:14 -07001492 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001493 if (mInTransition) {
1494 mDeferredSyncWidgetPageItems.add(data);
1495 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001496 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001497 try {
1498 int page = data.page;
1499 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001500
Michael Jurka39e5d172012-03-12 18:36:12 -07001501 ArrayList<Object> items = data.items;
1502 int count = items.size();
1503 for (int i = 0; i < count; ++i) {
1504 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1505 if (widget != null) {
1506 Bitmap preview = data.generatedImages.get(i);
1507 widget.applyPreview(new FastBitmapDrawable(preview), i);
1508 }
1509 }
Winson Chung68e4c642011-11-10 15:48:25 -08001510
Michael Jurka39e5d172012-03-12 18:36:12 -07001511 layout.createHardwareLayer();
1512 invalidate();
1513
1514 // Update all thread priorities
1515 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1516 while (iter.hasNext()) {
1517 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1518 int pageIndex = task.page;
1519 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1520 }
1521 } finally {
1522 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001523 }
Winson Chungb44b5242011-06-13 11:32:14 -07001524 }
Winson Chung46af2e82011-05-09 16:00:53 -07001525
Winson Chung785d2eb2011-04-14 16:08:02 -07001526 @Override
1527 public void syncPages() {
1528 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001529 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001530
Adam Cohen0cd3b642011-10-14 14:58:00 -07001531 Context context = getContext();
1532 for (int j = 0; j < mNumWidgetPages; ++j) {
1533 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1534 mWidgetCountY);
1535 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001536 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001537 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001538 }
1539
Adam Cohen0cd3b642011-10-14 14:58:00 -07001540 for (int i = 0; i < mNumAppsPages; ++i) {
1541 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1542 setupPage(layout);
1543 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001544 }
1545 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001546
Winson Chung785d2eb2011-04-14 16:08:02 -07001547 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001548 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001549 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001550 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001551 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001552 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001553 }
1554 }
1555
Adam Cohen22f823d2011-09-01 17:22:18 -07001556 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1557 // it is in the z-order. This is important to insure touch events are handled correctly.
1558 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001559 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001560 }
1561
Adam Cohenae4f1552011-10-20 00:15:42 -07001562 @Override
1563 protected int indexToPage(int index) {
1564 return getChildCount() - index - 1;
1565 }
1566
Adam Cohen22f823d2011-09-01 17:22:18 -07001567 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1568 @Override
1569 protected void screenScrolled(int screenCenter) {
1570 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001571
1572 for (int i = 0; i < getChildCount(); i++) {
1573 View v = getPageAt(i);
1574 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001575 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001576
1577 float interpolatedProgress =
1578 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1579 float scale = (1 - interpolatedProgress) +
1580 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1581 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001582
Adam Cohen2591f6a2011-10-25 14:36:40 -07001583 float alpha;
1584
Winson Chungd167e2a2012-04-26 13:13:01 -07001585 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001586 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001587 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001588 } else {
1589 // On large screens we need to fade the page as it nears its leftmost position
1590 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1591 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001592
1593 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1594 int pageWidth = v.getMeasuredWidth();
1595 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001596
1597 if (PERFORM_OVERSCROLL_ROTATION) {
1598 if (i == 0 && scrollProgress < 0) {
1599 // Overscroll to the left
1600 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1601 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1602 scale = 1.0f;
1603 alpha = 1.0f;
1604 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001605 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001606 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1607 // Overscroll to the right
1608 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1609 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1610 scale = 1.0f;
1611 alpha = 1.0f;
1612 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001613 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001614 } else {
1615 v.setPivotY(pageHeight / 2.0f);
1616 v.setPivotX(pageWidth / 2.0f);
1617 v.setRotationY(0f);
1618 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001619 }
1620
1621 v.setTranslationX(translationX);
1622 v.setScaleX(scale);
1623 v.setScaleY(scale);
1624 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001625
1626 // If the view has 0 alpha, we set it to be invisible so as to prevent
1627 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001628 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001629 v.setVisibility(INVISIBLE);
1630 } else if (v.getVisibility() != VISIBLE) {
1631 v.setVisibility(VISIBLE);
1632 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001633 }
1634 }
1635 }
1636
1637 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001638 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001639 }
1640
Winson Chung785d2eb2011-04-14 16:08:02 -07001641 /**
1642 * Used by the parent to get the content width to set the tab bar to
1643 * @return
1644 */
1645 public int getPageContentWidth() {
1646 return mContentWidth;
1647 }
1648
Winson Chungb26f3d62011-06-02 10:49:29 -07001649 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001650 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001651 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001652 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001653 // We reset the save index when we change pages so that it will be recalculated on next
1654 // rotation
1655 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001656 }
1657
Winson Chung785d2eb2011-04-14 16:08:02 -07001658 /*
1659 * AllAppsView implementation
1660 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001661 public void setup(Launcher launcher, DragController dragController) {
1662 mLauncher = launcher;
1663 mDragController = dragController;
1664 }
Winson Chung9802ac92012-06-08 16:01:58 -07001665
1666 /**
1667 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1668 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1669 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1670 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1671 */
1672 private void invalidateOnDataChange() {
1673 if (!isDataReady()) {
1674 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1675 // request a layout to trigger the page data when ready.
1676 requestLayout();
1677 } else {
1678 cancelAllTasks();
1679 invalidatePageData();
1680 }
1681 }
1682
Winson Chung785d2eb2011-04-14 16:08:02 -07001683 public void setApps(ArrayList<ApplicationInfo> list) {
1684 mApps = list;
1685 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001686 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001687 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001688 }
1689 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1690 // We add it in place, in alphabetical order
1691 int count = list.size();
1692 for (int i = 0; i < count; ++i) {
1693 ApplicationInfo info = list.get(i);
1694 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1695 if (index < 0) {
1696 mApps.add(-(index + 1), info);
1697 }
1698 }
1699 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001700 public void addApps(ArrayList<ApplicationInfo> list) {
1701 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001702 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001703 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001704 }
1705 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1706 ComponentName removeComponent = item.intent.getComponent();
1707 int length = list.size();
1708 for (int i = 0; i < length; ++i) {
1709 ApplicationInfo info = list.get(i);
1710 if (info.intent.getComponent().equals(removeComponent)) {
1711 return i;
1712 }
1713 }
1714 return -1;
1715 }
Winson Chungcd810732012-06-18 16:45:43 -07001716 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1717 int length = list.size();
1718 for (int i = 0; i < length; ++i) {
1719 ApplicationInfo info = list.get(i);
1720 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1721 return i;
1722 }
1723 }
1724 return -1;
1725 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001726 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1727 // loop through all the apps and remove apps that have the same component
1728 int length = list.size();
1729 for (int i = 0; i < length; ++i) {
1730 ApplicationInfo info = list.get(i);
1731 int removeIndex = findAppByComponent(mApps, info);
1732 if (removeIndex > -1) {
1733 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001734 }
1735 }
1736 }
Winson Chungcd810732012-06-18 16:45:43 -07001737 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1738 // loop through all the package names and remove apps that have the same package name
1739 for (String pn : packageNames) {
1740 int removeIndex = findAppByPackage(mApps, pn);
1741 while (removeIndex > -1) {
1742 mApps.remove(removeIndex);
1743 removeIndex = findAppByPackage(mApps, pn);
1744 }
1745 }
1746 }
1747 public void removeApps(ArrayList<String> packageNames) {
1748 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001749 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001750 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001751 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001752 public void updateApps(ArrayList<ApplicationInfo> list) {
1753 // We remove and re-add the updated applications list because it's properties may have
1754 // changed (ie. the title), and this will ensure that the items will be in their proper
1755 // place in the list.
1756 removeAppsWithoutInvalidate(list);
1757 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001758 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001759 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001760 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001761
Winson Chung785d2eb2011-04-14 16:08:02 -07001762 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001763 // If we have reset, then we should not continue to restore the previous state
1764 mSaveInstanceStateItemIndex = -1;
1765
Adam Cohenb64d36e2011-10-17 21:48:02 -07001766 AppsCustomizeTabHost tabHost = getTabHost();
1767 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001768 if (tag != null) {
1769 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1770 tabHost.setCurrentTabFromContent(ContentType.Applications);
1771 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001772 }
Winson Chung649668f2012-01-10 13:07:16 -08001773
Adam Cohenb64d36e2011-10-17 21:48:02 -07001774 if (mCurrentPage != 0) {
1775 invalidatePageData(0);
1776 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001777 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001778
1779 private AppsCustomizeTabHost getTabHost() {
1780 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1781 }
1782
Winson Chung785d2eb2011-04-14 16:08:02 -07001783 public void dumpState() {
1784 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001785 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1786 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001787 }
Adam Cohen4e844012011-11-09 13:48:04 -08001788
Winson Chung785d2eb2011-04-14 16:08:02 -07001789 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001790 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001791 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001792 for (Object i: list) {
1793 if (i instanceof AppWidgetProviderInfo) {
1794 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1795 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1796 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1797 + " initialLayout=" + info.initialLayout
1798 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1799 } else if (i instanceof ResolveInfo) {
1800 ResolveInfo info = (ResolveInfo) i;
1801 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1802 + info.icon);
1803 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001804 }
1805 }
Adam Cohen4e844012011-11-09 13:48:04 -08001806
Winson Chung785d2eb2011-04-14 16:08:02 -07001807 public void surrender() {
1808 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1809 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001810
1811 // Stop all background tasks
1812 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001813 }
Winson Chung007c6982011-06-14 13:27:53 -07001814
Winson Chunge4e50662012-01-23 14:45:13 -08001815 @Override
1816 public void iconPressed(PagedViewIcon icon) {
1817 // Reset the previously pressed icon and store a reference to the pressed icon so that
1818 // we can reset it on return to Launcher (in Launcher.onResume())
1819 if (mPressedIcon != null) {
1820 mPressedIcon.resetDrawableState();
1821 }
1822 mPressedIcon = icon;
1823 }
1824
1825 public void resetDrawableState() {
1826 if (mPressedIcon != null) {
1827 mPressedIcon.resetDrawableState();
1828 mPressedIcon = null;
1829 }
1830 }
Winson Chung68e4c642011-11-10 15:48:25 -08001831
Winson Chungb44b5242011-06-13 11:32:14 -07001832 /*
1833 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1834 * widget previews in the background with the AsyncTasks.
1835 */
Winson Chung68e4c642011-11-10 15:48:25 -08001836 final static int sLookBehindPageCount = 2;
1837 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001838 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001839 final int count = getChildCount();
1840 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1841 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1842 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001843 }
1844 protected int getAssociatedUpperPageBound(int page) {
1845 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001846 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1847 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1848 count - 1);
1849 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001850 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001851
1852 @Override
1853 protected String getCurrentPageDescription() {
1854 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1855 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001856 int count = 0;
1857
Adam Cohen0cd3b642011-10-14 14:58:00 -07001858 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001859 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001860 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001861 } else {
1862 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001863 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001864 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001865 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001866
Michael Jurka8b805b12012-04-18 14:23:14 -07001867 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001868 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001869}