blob: 11ae8f239c44259ad948f71b83a1d52d7a0b4dfc [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 Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070036import android.graphics.ColorMatrix;
37import android.graphics.ColorMatrixColorFilter;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080039import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070040import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070041import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080043import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070044import android.graphics.Shader;
Peter Ng8db70002011-10-25 15:40:08 -070045import android.graphics.TableMaskFilter;
Michael Jurkadac85912012-05-18 15:04:49 -070046import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070048import android.os.AsyncTask;
49import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.util.AttributeSet;
51import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070052import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080053import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070054import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070055import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070056import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070057import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070058import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070059import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070060import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070061import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070062import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070063
64import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065import com.android.launcher2.DropTarget.DragObject;
66
Michael Jurka9bc8eba2012-05-21 20:36:44 -070067import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070068import java.util.ArrayList;
69import java.util.Collections;
70import java.util.Iterator;
71import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070072
Winson Chungb44b5242011-06-13 11:32:14 -070073/**
74 * A simple callback interface which also provides the results of the task.
75 */
76interface AsyncTaskCallback {
77 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
78}
Winson Chung4e076542011-06-23 13:04:10 -070079
Winson Chungb44b5242011-06-13 11:32:14 -070080/**
81 * The data needed to perform either of the custom AsyncTasks.
82 */
83class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070084 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080085 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070086 }
87
Winson Chungb44b5242011-06-13 11:32:14 -070088 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
89 AsyncTaskCallback postR) {
90 page = p;
91 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070092 sourceImages = si;
93 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070094 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070095 doInBackgroundCallback = bgR;
96 postExecuteCallback = postR;
97 }
Michael Jurka038f9d82011-11-03 13:50:45 -070098 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070099 AsyncTaskCallback postR) {
100 page = p;
101 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700102 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700103 maxImageWidth = cw;
104 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 doInBackgroundCallback = bgR;
106 postExecuteCallback = postR;
107 }
Winson Chung09945932011-09-20 14:22:40 -0700108 void cleanup(boolean cancelled) {
109 // Clean up any references to source/generated bitmaps
110 if (sourceImages != null) {
111 if (cancelled) {
112 for (Bitmap b : sourceImages) {
113 b.recycle();
114 }
115 }
116 sourceImages.clear();
117 }
118 if (generatedImages != null) {
119 if (cancelled) {
120 for (Bitmap b : generatedImages) {
121 b.recycle();
122 }
123 }
124 generatedImages.clear();
125 }
126 }
Winson Chungb44b5242011-06-13 11:32:14 -0700127 int page;
128 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700129 ArrayList<Bitmap> sourceImages;
130 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700131 int maxImageWidth;
132 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700133 AsyncTaskCallback doInBackgroundCallback;
134 AsyncTaskCallback postExecuteCallback;
135}
Winson Chung4e076542011-06-23 13:04:10 -0700136
Winson Chungb44b5242011-06-13 11:32:14 -0700137/**
138 * A generic template for an async task used in AppsCustomize.
139 */
140class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700141 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700142 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700143 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700144 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700145 }
146 @Override
147 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
148 if (params.length != 1) return null;
149 // Load each of the widget previews in the background
150 params[0].doInBackgroundCallback.run(this, params[0]);
151 return params[0];
152 }
153 @Override
154 protected void onPostExecute(AsyncTaskPageData result) {
155 // All the widget previews are loaded, so we can just callback to inflate the page
156 result.postExecuteCallback.run(this, result);
157 }
158
159 void setThreadPriority(int p) {
160 threadPriority = p;
161 }
162 void syncThreadPriority() {
163 Process.setThreadPriority(threadPriority);
164 }
165
166 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700167 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700168 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700169 int threadPriority;
170}
Winson Chungb44b5242011-06-13 11:32:14 -0700171
Michael Jurkadac85912012-05-18 15:04:49 -0700172abstract class WeakReferenceThreadLocal<T> {
173 private ThreadLocal<WeakReference<T>> mThreadLocal;
174 public WeakReferenceThreadLocal() {
175 mThreadLocal = new ThreadLocal<WeakReference<T>>();
176 }
177
178 abstract T initialValue();
179
180 public void set(T t) {
181 mThreadLocal.set(new WeakReference<T>(t));
182 }
183
184 public T get() {
185 WeakReference<T> reference = mThreadLocal.get();
186 T obj;
187 if (reference == null) {
188 obj = initialValue();
189 mThreadLocal.set(new WeakReference<T>(obj));
190 return obj;
191 } else {
192 obj = reference.get();
193 if (obj == null) {
194 obj = initialValue();
195 mThreadLocal.set(new WeakReference<T>(obj));
196 }
197 return obj;
198 }
199 }
200}
201
202class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
203 @Override
204 protected Canvas initialValue() {
205 return new Canvas();
206 }
207}
208
209class PaintCache extends WeakReferenceThreadLocal<Paint> {
210 @Override
211 protected Paint initialValue() {
212 return null;
213 }
214}
215
216class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
217 @Override
218 protected Bitmap initialValue() {
219 return null;
220 }
221}
222
223class RectCache extends WeakReferenceThreadLocal<Rect> {
224 @Override
225 protected Rect initialValue() {
226 return new Rect();
227 }
228}
229
Winson Chungb44b5242011-06-13 11:32:14 -0700230/**
231 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
232 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700233public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700234 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700235 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
236 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700237 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700238
239 /**
240 * The different content types that this paged view can show.
241 */
242 public enum ContentType {
243 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700244 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700245 }
246
247 // Refs
248 private Launcher mLauncher;
249 private DragController mDragController;
250 private final LayoutInflater mLayoutInflater;
251 private final PackageManager mPackageManager;
252
Winson Chung5afbf7b2011-07-25 11:53:08 -0700253 // Save and Restore
254 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800255 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700256
Winson Chung785d2eb2011-04-14 16:08:02 -0700257 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700258 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700259 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700260
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800262 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 private int mClingFocusedX;
264 private int mClingFocusedY;
265
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700267 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700268 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700269
270 // Dimens
271 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700272 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800273 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700274 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700275 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700276 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700277 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700278 private int mNumAppsPages;
279 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280
Adam Cohen22f823d2011-09-01 17:22:18 -0700281 // Relating to the scroll and overscroll effects
282 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700283 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700284 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700285 private static float TRANSITION_PIVOT = 0.65f;
286 private static float TRANSITION_MAX_ROTATION = 22;
287 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700288 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700289 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700290
Winson Chungb44b5242011-06-13 11:32:14 -0700291 // Previews & outlines
292 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800293 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700294
Adam Cohened66b2b2012-01-23 17:28:51 -0800295 private Runnable mInflateWidgetRunnable = null;
296 private Runnable mBindWidgetRunnable = null;
297 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700298 static final int WIDGET_PRELOAD_PENDING = 0;
299 static final int WIDGET_BOUND = 1;
300 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800301 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
302 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800303 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800304 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800305
Michael Jurka39e5d172012-03-12 18:36:12 -0700306 // Deferral of loading widget previews during launcher transitions
307 private boolean mInTransition;
308 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
309 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700310 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
311 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700312
Michael Jurkadac85912012-05-18 15:04:49 -0700313 // Used for drawing shortcut previews
314 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
315 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
316 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
317
318 // Used for drawing widget previews
319 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
320 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
321 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
322 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
323
Winson Chung785d2eb2011-04-14 16:08:02 -0700324 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
325 super(context, attrs);
326 mLayoutInflater = LayoutInflater.from(context);
327 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700328 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700329 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700330 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700331 mCanvas = new Canvas();
332 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700333
334 // Save the default widget preview background
335 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700336 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700337
Winson Chung6032e7e2011-11-08 15:47:17 -0800338 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
339 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
340 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700341 mWidgetWidthGap =
342 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
343 mWidgetHeightGap =
344 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700345 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
346 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700347 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
348 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700349 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700350 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700351
Winson Chung1ed747a2011-05-03 16:18:34 -0700352 // The padding on the non-matched dimension for the default widget preview icons
353 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700354 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700355
356 // Unless otherwise specified this view is important for accessibility.
357 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
358 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
359 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700360 }
361
362 @Override
363 protected void init() {
364 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700365 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700366
367 Context context = getContext();
368 Resources r = context.getResources();
369 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
370 }
371
Winson Chungf0ea4d32011-06-06 14:27:16 -0700372 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700373 protected void onUnhandledTap(MotionEvent ev) {
374 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700375 // Dismiss AppsCustomize if we tap
376 mLauncher.showWorkspace(true);
377 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
379
Winson Chung5afbf7b2011-07-25 11:53:08 -0700380 /** Returns the item index of the center item on this page so that we can restore to this
381 * item index when we rotate. */
382 private int getMiddleComponentIndexOnCurrentPage() {
383 int i = -1;
384 if (getPageCount() > 0) {
385 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700386 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700387 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700388 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
389 int numItemsPerPage = mCellCountX * mCellCountY;
390 int childCount = childrenLayout.getChildCount();
391 if (childCount > 0) {
392 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700393 }
394 } else {
395 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700396 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700397 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
398 int childCount = layout.getChildCount();
399 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700400 i = numApps +
401 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
402 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700403 }
404 }
405 return i;
406 }
407
408 /** Get the index of the item to restore to if we need to restore the current page. */
409 int getSaveInstanceStateIndex() {
410 if (mSaveInstanceStateItemIndex == -1) {
411 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
412 }
413 return mSaveInstanceStateItemIndex;
414 }
415
416 /** Returns the page in the current orientation which is expected to contain the specified
417 * item index. */
418 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700419 if (index < 0) return 0;
420
421 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700422 int numItemsPerPage = mCellCountX * mCellCountY;
423 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700424 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700425 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700426 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
427 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700428 }
429
Winson Chung5afbf7b2011-07-25 11:53:08 -0700430 /** Restores the page for an item at the specified index */
431 void restorePageForIndex(int index) {
432 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700433 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700434 }
435
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700436 private void updatePageCounts() {
437 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
438 (float) (mWidgetCountX * mWidgetCountY));
439 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
440 }
441
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 protected void onDataReady(int width, int height) {
443 // Note that we transpose the counts in portrait so that we get a similar layout
444 boolean isLandscape = getResources().getConfiguration().orientation ==
445 Configuration.ORIENTATION_LANDSCAPE;
446 int maxCellCountX = Integer.MAX_VALUE;
447 int maxCellCountY = Integer.MAX_VALUE;
448 if (LauncherApplication.isScreenLarge()) {
449 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
450 LauncherModel.getCellCountY());
451 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
452 LauncherModel.getCellCountX());
453 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800454 if (mMaxAppCellCountX > -1) {
455 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
456 }
457 if (mMaxAppCellCountY > -1) {
458 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
459 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700460
461 // Now that the data is ready, we can calculate the content width, the number of cells to
462 // use for each page
463 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
464 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
465 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
466 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
467 mCellCountX = mWidgetSpacingLayout.getCellCountX();
468 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700469 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700470
Winson Chungdb1138b2011-06-30 14:39:35 -0700471 // Force a measure to update recalculate the gaps
472 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
473 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
474 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700475 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700476
Michael Jurkae326f182011-11-21 14:05:46 -0800477 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
478 final boolean hostIsTransitioning = host.isTransitioning();
479
Adam Cohen0cd3b642011-10-14 14:58:00 -0700480 // Restore the page
481 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800482 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700483
Winson Chung3f4e1422011-11-17 14:58:51 -0800484 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
485 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
486 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800487 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800488 post(new Runnable() {
489 @Override
490 public void run() {
491 showAllAppsCling();
492 }
493 });
494 }
495 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700496
Winson Chung3f4e1422011-11-17 14:58:51 -0800497 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700498 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800499 mHasShownAllAppsCling = true;
500 // Calculate the position for the cling punch through
501 int[] offset = new int[2];
502 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
503 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
504 // PagedViews are centered horizontally but top aligned
505 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
506 offset[0];
507 pos[1] += offset[1];
508 mLauncher.showFirstRunAllAppsCling(pos);
509 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700510 }
511
512 @Override
513 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
514 int width = MeasureSpec.getSize(widthMeasureSpec);
515 int height = MeasureSpec.getSize(heightMeasureSpec);
516 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700517 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700518 setDataIsReady();
519 setMeasuredDimension(width, height);
520 onDataReady(width, height);
521 }
522 }
523
524 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
525 }
526
Winson Chung785d2eb2011-04-14 16:08:02 -0700527 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700528 // Get the list of widgets and shortcuts
529 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 List<AppWidgetProviderInfo> widgets =
531 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700532 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
533 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800534 for (AppWidgetProviderInfo widget : widgets) {
535 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700536 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700537 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
538 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700539 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
540 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700541 if (minSpanX <= LauncherModel.getCellCountX() &&
542 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700543 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700544 } else {
545 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
546 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700547 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800548 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700549 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800550 widget.minWidth + ", " + widget.minHeight + ")");
551 }
552 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700553 mWidgets.addAll(shortcuts);
554 Collections.sort(mWidgets,
555 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700556 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700557 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700558 }
559
560 @Override
561 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700562 // When we have exited all apps or are in transition, disregard clicks
563 if (!mLauncher.isAllAppsCustomizeOpen() ||
564 mLauncher.getWorkspace().isSwitchingState()) return;
565
Winson Chung4b576be2011-04-27 17:40:20 -0700566 if (v instanceof PagedViewIcon) {
567 // Animate some feedback to the click
568 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800569
570 // Lock the drawable state to pressed until we return to Launcher
571 if (mPressedIcon != null) {
572 mPressedIcon.lockDrawableState();
573 }
Winson Chungc7450e32012-04-17 17:34:08 -0700574
Winson Chung18f41f82012-05-09 13:28:10 -0700575 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
576 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
577 // when Launcher resumes and we are still in AllApps.
578 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700579 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
580
Winson Chung4b576be2011-04-27 17:40:20 -0700581 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700582 // Let the user know that they have to long press to add a widget
583 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
584 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700585
Winson Chungd2e87b32011-06-02 10:53:07 -0700586 // Create a little animation to show that the widget can move
587 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
588 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
589 AnimatorSet bounce = new AnimatorSet();
590 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
591 tyuAnim.setDuration(125);
592 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
593 tydAnim.setDuration(100);
594 bounce.play(tyuAnim).before(tydAnim);
595 bounce.setInterpolator(new AccelerateInterpolator());
596 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700597 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700598 }
599
Winson Chungc6f10b92011-11-14 11:39:07 -0800600 public boolean onKey(View v, int keyCode, KeyEvent event) {
601 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
602 }
603
Winson Chung785d2eb2011-04-14 16:08:02 -0700604 /*
605 * PagedViewWithDraggableItems implementation
606 */
607 @Override
608 protected void determineDraggingStart(android.view.MotionEvent ev) {
609 // Disable dragging by pulling an app down for now.
610 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700611
Winson Chung4b576be2011-04-27 17:40:20 -0700612 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700613 mLauncher.getWorkspace().onDragStartedWithItem(v);
614 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700615 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700616
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700617 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800618 final AppWidgetProviderInfo pInfo = info.info;
619 if (pInfo.configure != null) {
620 return;
621 }
622
Adam Cohen21a170b2012-05-30 15:17:06 -0700623 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800624 mBindWidgetRunnable = new Runnable() {
625 @Override
626 public void run() {
627 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700628 if (AppWidgetManager.getInstance(mLauncher)
629 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
630 mWidgetCleanupState = WIDGET_BOUND;
631 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800632 }
633 };
634 post(mBindWidgetRunnable);
635
636 mInflateWidgetRunnable = new Runnable() {
637 @Override
638 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700639 AppWidgetHostView hostView = mLauncher.
640 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800641 info.boundWidget = hostView;
642 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800643 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700644 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
645 info.spanY, info, false);
646
647 // We want the first widget layout to be the correct size. This will be important
648 // for width size reporting to the AppWidgetManager.
649 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
650 unScaledSize[1]);
651 lp.x = lp.y = 0;
652 lp.customPosition = true;
653 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800654 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800655 }
656 };
657 post(mInflateWidgetRunnable);
658 }
659
660 @Override
661 public void onShortPress(View v) {
662 // We are anticipating a long press, and we use this time to load bind and instantiate
663 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700664 if (mCreateWidgetInfo != null) {
665 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
666 cleanupWidgetPreloading(false);
667 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800668 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700669 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 }
671
Adam Cohen0e56cc92012-05-11 15:57:05 -0700672 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
673 if (!widgetWasAdded) {
674 // If the widget was not added, we may need to do further cleanup.
675 PendingAddWidgetInfo info = mCreateWidgetInfo;
676 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700677
678 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700679 // We never did any preloading, so just remove pending callbacks to do so
680 removeCallbacks(mBindWidgetRunnable);
681 removeCallbacks(mInflateWidgetRunnable);
682 } else if (mWidgetCleanupState == WIDGET_BOUND) {
683 // Delete the widget id which was allocated
684 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700685 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
686 }
687
688 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700689 removeCallbacks(mInflateWidgetRunnable);
690 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700691 // Delete the widget id which was allocated
692 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700693 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
694 }
695
Adam Cohen0e56cc92012-05-11 15:57:05 -0700696 // The widget was inflated and added to the DragLayer -- remove it.
697 AppWidgetHostView widget = info.boundWidget;
698 mLauncher.getDragLayer().removeView(widget);
699 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 }
701 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
702 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700703 mCreateWidgetInfo = null;
704 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800705 }
706
Adam Cohen7a326642012-02-22 12:03:22 -0800707 @Override
708 public void cleanUpShortPress(View v) {
709 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700710 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800711 }
712 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800713
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700714 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800715 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700716 // Get the widget preview as the drag representation
717 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700718 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700719
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700720 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
721 // we abort the drag.
722 if (image.getDrawable() == null) {
723 mDraggingWidget = false;
724 return false;
725 }
726
Winson Chung4b576be2011-04-27 17:40:20 -0700727 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800728 Bitmap preview;
729 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800730 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700731 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700732 // This can happen in some weird cases involving multi-touch. We can't start dragging
733 // the widget if this is null, so we break out.
734 if (mCreateWidgetInfo == null) {
735 return false;
736 }
737
Adam Cohen1b36dc32012-02-13 19:27:37 -0800738 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
739 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700740 int spanX = createItemInfo.spanX;
741 int spanY = createItemInfo.spanY;
742 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
743 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700744
Winson Chung72d59842012-02-22 13:51:36 -0800745 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
746 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700747 int maxWidth, maxHeight;
748 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
749 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800750 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700751 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800752
753 // Determine the image view drawable scale relative to the preview
754 float[] mv = new float[9];
755 Matrix m = new Matrix();
756 m.setRectToRect(
757 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
758 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
759 (float) previewDrawable.getIntrinsicHeight()),
760 Matrix.ScaleToFit.START);
761 m.getValues(mv);
762 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700763 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700764 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
765 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
766 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
767 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
768
Winson Chung1120e032011-11-22 16:11:31 -0800769 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700770 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700771 renderDrawableToBitmap(icon, preview, 0, 0,
772 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700773 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700774 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700775 createItemInfo.spanX = createItemInfo.spanY = 1;
776 }
Winson Chung4b576be2011-04-27 17:40:20 -0700777
Peter Ng8db70002011-10-25 15:40:08 -0700778 // We use a custom alpha clip table for the default widget previews
779 Paint alphaClipPaint = null;
780 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700781 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700782 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
783 alphaClipPaint = new Paint();
784 alphaClipPaint.setMaskFilter(alphaClipTable);
785 }
786 }
787
Winson Chung1120e032011-11-22 16:11:31 -0800788 // Save the preview for the outline generation, then dim the preview
789 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
790 false);
Winson Chung1120e032011-11-22 16:11:31 -0800791
Winson Chung4b576be2011-04-27 17:40:20 -0700792 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800793 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700794 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800795 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
796 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800797 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800798 outline.recycle();
799 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700800 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700801 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800802
Winson Chung4b576be2011-04-27 17:40:20 -0700803 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700805 if (!super.beginDragging(v)) return false;
806
807 if (v instanceof PagedViewIcon) {
808 beginDraggingApplication(v);
809 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700810 if (!beginDraggingWidget(v)) {
811 return false;
812 }
Winson Chung4b576be2011-04-27 17:40:20 -0700813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814
815 // We delay entering spring-loaded mode slightly to make sure the UI
816 // thready is free of any work.
817 postDelayed(new Runnable() {
818 @Override
819 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800820 // We don't enter spring-loaded mode if the drag has been cancelled
821 if (mLauncher.getDragController().isDragging()) {
822 // Dismiss the cling
823 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800824
Adam Cohen1b36dc32012-02-13 19:27:37 -0800825 // Reset the alpha on the dragged icon before we drag
826 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800827
Adam Cohen1b36dc32012-02-13 19:27:37 -0800828 // Go into spring loaded mode (must happen before we startDrag())
829 mLauncher.enterSpringLoadedDragMode();
830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 }
Winson Chung72d59842012-02-22 13:51:36 -0800832 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800833
Winson Chung785d2eb2011-04-14 16:08:02 -0700834 return true;
835 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800836
Winson Chunga48487a2012-03-20 16:19:37 -0700837 /**
838 * Clean up after dragging.
839 *
840 * @param target where the item was dragged to (can be null if the item was flung)
841 */
842 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700843 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700844 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700845 // Exit spring loaded mode if we have not successfully dropped or have not handled the
846 // drop in Workspace
847 mLauncher.exitSpringLoadedDragMode();
848 }
Winson Chung4b919f82012-05-01 10:44:08 -0700849 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 }
851
Winson Chung785d2eb2011-04-14 16:08:02 -0700852 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700853 public View getContent() {
854 return null;
855 }
856
857 @Override
858 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700859 mInTransition = true;
860 if (toWorkspace) {
861 cancelAllTasks();
862 }
863 }
864
865 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700866 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700867 }
868
869 @Override
870 public void onLauncherTransitionStep(Launcher l, float t) {
871 }
872
873 @Override
874 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
875 mInTransition = false;
876 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
877 onSyncWidgetPageItems(d);
878 }
879 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700880 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
881 r.run();
882 }
883 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700884 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700885 }
886
887 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700888 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
889 boolean success) {
890 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
891 if (isFlingToDelete) return;
892
893 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700894
895 // Display an error message if the drag failed due to there not being enough space on the
896 // target layout we were dropping on.
897 if (!success) {
898 boolean showOutOfSpaceMessage = false;
899 if (target instanceof Workspace) {
900 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
901 Workspace workspace = (Workspace) target;
902 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700903 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700904 if (layout != null) {
905 layout.calculateSpans(itemInfo);
906 showOutOfSpaceMessage =
907 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
908 }
909 }
Winson Chungfc79c802011-05-02 13:35:34 -0700910 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700911 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700912 }
Adam Cohen7a326642012-02-22 12:03:22 -0800913
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800914 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700915 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700916 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800917 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 }
919
Winson Chunga48487a2012-03-20 16:19:37 -0700920 @Override
921 public void onFlingToDeleteCompleted() {
922 // We just dismiss the drag when we fling, so cleanup here
923 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700924 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700925 mDraggingWidget = false;
926 }
927
928 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800929 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700930 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800931 }
932
Winson Chung7f0acdd2011-09-19 18:34:19 -0700933 @Override
934 protected void onDetachedFromWindow() {
935 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700936 cancelAllTasks();
937 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700938
Michael Jurkae326f182011-11-21 14:05:46 -0800939 public void clearAllWidgetPages() {
940 cancelAllTasks();
941 int count = getChildCount();
942 for (int i = 0; i < count; i++) {
943 View v = getPageAt(i);
944 if (v instanceof PagedViewGridLayout) {
945 ((PagedViewGridLayout) v).removeAllViewsOnPage();
946 mDirtyPageContent.set(i, true);
947 }
948 }
949 }
950
Adam Cohen0cd3b642011-10-14 14:58:00 -0700951 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700952 // Clean up all the async tasks
953 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
954 while (iter.hasNext()) {
955 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
956 task.cancel(false);
957 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700958 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700959
960 // We've already preallocated the views for the data to load into, so clear them as well
961 View v = getPageAt(task.page);
962 if (v instanceof PagedViewGridLayout) {
963 ((PagedViewGridLayout) v).removeAllViewsOnPage();
964 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700965 }
Winson Chung83687b12012-04-25 16:01:01 -0700966 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700967 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700968 }
969
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700971 if (type == ContentType.Widgets) {
972 invalidatePageData(mNumAppsPages, true);
973 } else if (type == ContentType.Applications) {
974 invalidatePageData(0, true);
975 }
Winson Chungb44b5242011-06-13 11:32:14 -0700976 }
977
Adam Cohen0cd3b642011-10-14 14:58:00 -0700978 protected void snapToPage(int whichPage, int delta, int duration) {
979 super.snapToPage(whichPage, delta, duration);
980 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800981
982 // Update the thread priorities given the direction lookahead
983 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
984 while (iter.hasNext()) {
985 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700986 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800987 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
988 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
989 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
990 } else {
991 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
992 }
993 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700994 }
995
996 private void updateCurrentTab(int currentPage) {
997 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800998 if (tabHost != null) {
999 String tag = tabHost.getCurrentTabTag();
1000 if (tag != null) {
1001 if (currentPage >= mNumAppsPages &&
1002 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1003 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1004 } else if (currentPage < mNumAppsPages &&
1005 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1006 tabHost.setCurrentTabFromContent(ContentType.Applications);
1007 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001008 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001009 }
1010 }
1011
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 /*
1013 * Apps PagedView implementation
1014 */
Winson Chung63257c12011-05-05 17:06:13 -07001015 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1016 int childCount = layout.getChildCount();
1017 for (int i = 0; i < childCount; ++i) {
1018 layout.getChildAt(i).setVisibility(visibility);
1019 }
1020 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001021 private void setupPage(PagedViewCellLayout layout) {
1022 layout.setCellCount(mCellCountX, mCellCountY);
1023 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1024 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1025 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1026
Winson Chung63257c12011-05-05 17:06:13 -07001027 // Note: We force a measure here to get around the fact that when we do layout calculations
1028 // immediately after syncing, we don't have a proper width. That said, we already know the
1029 // expected page width, so we can actually optimize by hiding all the TextView-based
1030 // children that are expensive to measure, and let that happen naturally later.
1031 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001032 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001033 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001034 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001036 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001038
Winson Chungf314b0e2011-08-16 11:54:27 -07001039 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001041 int numCells = mCellCountX * mCellCountY;
1042 int startIndex = page * numCells;
1043 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001044 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001045
Winson Chung785d2eb2011-04-14 16:08:02 -07001046 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001047 ArrayList<Object> items = new ArrayList<Object>();
1048 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001049 for (int i = startIndex; i < endIndex; ++i) {
1050 ApplicationInfo info = mApps.get(i);
1051 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1052 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001053 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001054 icon.setOnClickListener(this);
1055 icon.setOnLongClickListener(this);
1056 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001057 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001058
1059 int index = i - startIndex;
1060 int x = index % mCellCountX;
1061 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001062 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001063
1064 items.add(info);
1065 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001067
Winson Chungf0ea4d32011-06-06 14:27:16 -07001068 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001069 }
Winson Chungb44b5242011-06-13 11:32:14 -07001070
1071 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001072 * A helper to return the priority for loading of the specified widget page.
1073 */
1074 private int getWidgetPageLoadPriority(int page) {
1075 // If we are snapping to another page, use that index as the target page index
1076 int toPage = mCurrentPage;
1077 if (mNextPage > -1) {
1078 toPage = mNextPage;
1079 }
1080
1081 // We use the distance from the target page as an initial guess of priority, but if there
1082 // are no pages of higher priority than the page specified, then bump up the priority of
1083 // the specified page.
1084 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1085 int minPageDiff = Integer.MAX_VALUE;
1086 while (iter.hasNext()) {
1087 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001088 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001089 }
1090
1091 int rawPageDiff = Math.abs(page - toPage);
1092 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1093 }
1094 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001095 * Return the appropriate thread priority for loading for a given page (we give the current
1096 * page much higher priority)
1097 */
1098 private int getThreadPriorityForPage(int page) {
1099 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001100 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001101 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001102 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001103 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001104 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001105 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001106 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001107 }
1108 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001109 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001110 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001111 return Math.max(0, pageDiff * sPageSleepDelay);
1112 }
Winson Chungb44b5242011-06-13 11:32:14 -07001113 /**
1114 * Creates and executes a new AsyncTask to load a page of widget previews.
1115 */
1116 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001117 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001118
Winson Chungb44b5242011-06-13 11:32:14 -07001119 // Prune all tasks that are no longer needed
1120 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1121 while (iter.hasNext()) {
1122 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001123 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001124 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1125 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001126 task.cancel(false);
1127 iter.remove();
1128 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001129 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001130 }
1131 }
1132
Winson Chungf314b0e2011-08-16 11:54:27 -07001133 // 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 -07001134 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001135 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001136 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001137 @Override
1138 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001139 try {
Winson Chung09945932011-09-20 14:22:40 -07001140 try {
1141 Thread.sleep(sleepMs);
1142 } catch (Exception e) {}
1143 loadWidgetPreviewsInBackground(task, data);
1144 } finally {
1145 if (task.isCancelled()) {
1146 data.cleanup(true);
1147 }
1148 }
Winson Chungb44b5242011-06-13 11:32:14 -07001149 }
1150 },
1151 new AsyncTaskCallback() {
1152 @Override
1153 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001154 mRunningTasks.remove(task);
1155 if (task.isCancelled()) return;
1156 // do cleanup inside onSyncWidgetPageItems
1157 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001158 }
Winson Chung09945932011-09-20 14:22:40 -07001159 });
Winson Chungb44b5242011-06-13 11:32:14 -07001160
1161 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001162 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001163 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001164 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001165 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1166 mRunningTasks.add(t);
1167 }
Winson Chungb44b5242011-06-13 11:32:14 -07001168
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 /*
1170 * Widgets PagedView implementation
1171 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001172 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1174 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001175
1176 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001177 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001178 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1179 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001180 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001181 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001182 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001183
Winson Chung5fc72b32011-10-11 17:53:58 -07001184 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001185 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001186 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001187
Winson Chung5fc72b32011-10-11 17:53:58 -07001188 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001189 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001190 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001191 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001192 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001193 Rect oldBounds = d.copyBounds();
1194 d.setBounds(x, y, x + w, y + h);
1195 d.draw(c);
1196 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001197 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001198 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001199 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001200
Michael Jurkadac85912012-05-18 15:04:49 -07001201 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1202 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1203 final Canvas c = mCachedShortcutPreviewCanvas.get();
1204 if (tempBitmap == null ||
1205 tempBitmap.getWidth() != maxWidth ||
1206 tempBitmap.getHeight() != maxHeight) {
1207 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1208 mCachedShortcutPreviewBitmap.set(tempBitmap);
1209 } else {
1210 c.setBitmap(tempBitmap);
1211 c.drawColor(0, PorterDuff.Mode.CLEAR);
1212 c.setBitmap(null);
1213 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001214 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001215 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001216
1217 int paddingTop =
1218 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1219 int paddingLeft =
1220 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1221 int paddingRight =
1222 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1223
1224 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001225
1226 renderDrawableToBitmap(
1227 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1228
1229 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1230 c.setBitmap(preview);
1231 Paint p = mCachedShortcutPreviewPaint.get();
1232 if (p == null) {
1233 p = new Paint();
1234 ColorMatrix colorMatrix = new ColorMatrix();
1235 colorMatrix.setSaturation(0);
1236 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1237 p.setAlpha((int) (255 * 0.06f));
1238 //float density = 1f;
1239 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1240 mCachedShortcutPreviewPaint.set(p);
1241 }
1242 c.drawBitmap(tempBitmap, 0, 0, p);
1243 c.setBitmap(null);
1244
1245 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1246
Winson Chungb44b5242011-06-13 11:32:14 -07001247 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001248 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001249
Michael Jurkadac85912012-05-18 15:04:49 -07001250 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1251 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1252 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001253 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001254 String packageName = provider.getPackageName();
1255 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1256 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001257
Michael Jurka038f9d82011-11-03 13:50:45 -07001258 Drawable drawable = null;
1259 if (previewImage != 0) {
1260 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1261 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001262 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001263 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001264 }
1265 }
1266
Michael Jurka038f9d82011-11-03 13:50:45 -07001267 int bitmapWidth;
1268 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001269 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001270 boolean widgetPreviewExists = (drawable != null);
1271 if (widgetPreviewExists) {
1272 bitmapWidth = drawable.getIntrinsicWidth();
1273 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001274 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001275 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001276 if (cellHSpan < 1) cellHSpan = 1;
1277 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001278
1279 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1280 .getDrawable(R.drawable.widget_preview_tile);
1281 final int previewDrawableWidth = previewDrawable
1282 .getIntrinsicWidth();
1283 final int previewDrawableHeight = previewDrawable
1284 .getIntrinsicHeight();
1285 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1286 bitmapHeight = previewDrawableHeight * cellVSpan;
1287
1288 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1289 Config.ARGB_8888);
1290 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1291 c.setBitmap(defaultPreview);
1292 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1293 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1294 Shader.TileMode.REPEAT);
1295 previewDrawable.draw(c);
1296 c.setBitmap(null);
1297
1298 // Draw the icon in the top left corner
1299 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1300 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1301 float iconScale = Math.min((float) smallestSide
1302 / (mAppIconSize + 2 * minOffset), 1f);
1303
1304 try {
1305 Drawable icon = null;
1306 int hoffset =
1307 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1308 int yoffset =
1309 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1310 if (iconId > 0)
1311 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001312 if (icon != null) {
1313 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1314 yoffset, (int) (mAppIconSize * iconScale),
1315 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001316 }
Michael Jurkadac85912012-05-18 15:04:49 -07001317 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001318 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001319 }
1320
Michael Jurkadac85912012-05-18 15:04:49 -07001321 // Scale to fit width only - let the widget preview be clipped in the
1322 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001323 float scale = 1f;
1324 if (bitmapWidth > maxWidth) {
1325 scale = maxWidth / (float) bitmapWidth;
1326 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001327 if (scale != 1f) {
1328 bitmapWidth = (int) (scale * bitmapWidth);
1329 bitmapHeight = (int) (scale * bitmapHeight);
1330 }
1331
Michael Jurkadac85912012-05-18 15:04:49 -07001332 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1333 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001334
Michael Jurkadac85912012-05-18 15:04:49 -07001335 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001336 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001337 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1338 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001339 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001340 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1341 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1342 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1343 c.setBitmap(preview);
1344 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1345 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1346
1347 Paint p = mCachedAppWidgetPreviewPaint.get();
1348 if (p == null) {
1349 p = new Paint();
1350 p.setFilterBitmap(true);
1351 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001352 }
Michael Jurkadac85912012-05-18 15:04:49 -07001353 c.drawBitmap(defaultPreview, src, dest, p);
1354 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001355 }
Winson Chungb44b5242011-06-13 11:32:14 -07001356 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001357 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001358
Michael Jurka038f9d82011-11-03 13:50:45 -07001359 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001360 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001361
Winson Chungd2945262011-06-24 15:22:14 -07001362 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001363 final ArrayList<Object> items = new ArrayList<Object>();
1364 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1365 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001366 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001367 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1368 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001369 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001370
Winson Chunge4a647f2011-09-30 14:41:25 -07001371 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001372 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001373 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1374 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001375 }
1376
Winson Chunge4a647f2011-09-30 14:41:25 -07001377 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001378 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001379 layout.setColumnCount(layout.getCellCountX());
1380 for (int i = 0; i < items.size(); ++i) {
1381 Object rawInfo = items.get(i);
1382 PendingAddItemInfo createItemInfo = null;
1383 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1384 R.layout.apps_customize_widget, layout, false);
1385 if (rawInfo instanceof AppWidgetProviderInfo) {
1386 // Fill in the widget information
1387 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1388 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001389
1390 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001391 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001392 createItemInfo.spanX = spanXY[0];
1393 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001394 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001395 createItemInfo.minSpanX = minSpanXY[0];
1396 createItemInfo.minSpanY = minSpanXY[1];
1397
1398 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001399 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001400 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001401 } else if (rawInfo instanceof ResolveInfo) {
1402 // Fill in the shortcuts information
1403 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001404 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001405 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1406 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1407 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001408 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001409 widget.setTag(createItemInfo);
1410 }
1411 widget.setOnClickListener(this);
1412 widget.setOnLongClickListener(this);
1413 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001414 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001415
1416 // Layout each widget
1417 int ix = i % mWidgetCountX;
1418 int iy = i / mWidgetCountX;
1419 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1420 GridLayout.spec(iy, GridLayout.LEFT),
1421 GridLayout.spec(ix, GridLayout.TOP));
1422 lp.width = cellWidth;
1423 lp.height = cellHeight;
1424 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1425 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1426 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1427 layout.addView(widget, lp);
1428 }
1429
Michael Jurka038f9d82011-11-03 13:50:45 -07001430 // wait until a call on onLayout to start loading, because
1431 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1432 // TODO: can we do a measure/layout immediately?
1433 layout.setOnLayoutListener(new Runnable() {
1434 public void run() {
1435 // Load the widget previews
1436 int maxPreviewWidth = cellWidth;
1437 int maxPreviewHeight = cellHeight;
1438 if (layout.getChildCount() > 0) {
1439 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1440 int[] maxSize = w.getPreviewSize();
1441 maxPreviewWidth = maxSize[0];
1442 maxPreviewHeight = maxSize[1];
1443 }
1444 if (immediate) {
1445 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1446 maxPreviewWidth, maxPreviewHeight, null, null);
1447 loadWidgetPreviewsInBackground(null, data);
1448 onSyncWidgetPageItems(data);
1449 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001450 if (mInTransition) {
1451 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1452 } else {
1453 prepareLoadWidgetPreviewsTask(page, items,
1454 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1455 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001456 }
1457 }
1458 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001459 }
1460 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1461 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001462 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1463 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001464 if (task != null) {
1465 // Ensure that this task starts running at the correct priority
1466 task.syncThreadPriority();
1467 }
1468
1469 // Load each of the widget/shortcut previews
1470 ArrayList<Object> items = data.items;
1471 ArrayList<Bitmap> images = data.generatedImages;
1472 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001473 for (int i = 0; i < count; ++i) {
1474 if (task != null) {
1475 // Ensure we haven't been cancelled yet
1476 if (task.isCancelled()) break;
1477 // Before work on each item, ensure that this task is running at the correct
1478 // priority
1479 task.syncThreadPriority();
1480 }
1481
1482 Object rawInfo = items.get(i);
1483 if (rawInfo instanceof AppWidgetProviderInfo) {
1484 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001485 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001486
1487 int maxWidth = Math.min(data.maxImageWidth,
1488 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1489 int maxHeight = Math.min(data.maxImageHeight,
1490 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001491 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001492 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001493 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001494 } else if (rawInfo instanceof ResolveInfo) {
1495 // Fill in the shortcuts information
1496 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001497 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001498 }
1499 }
Winson Chungb44b5242011-06-13 11:32:14 -07001500 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001501
Winson Chungb44b5242011-06-13 11:32:14 -07001502 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001503 if (mInTransition) {
1504 mDeferredSyncWidgetPageItems.add(data);
1505 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001507 try {
1508 int page = data.page;
1509 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001510
Michael Jurka39e5d172012-03-12 18:36:12 -07001511 ArrayList<Object> items = data.items;
1512 int count = items.size();
1513 for (int i = 0; i < count; ++i) {
1514 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1515 if (widget != null) {
1516 Bitmap preview = data.generatedImages.get(i);
1517 widget.applyPreview(new FastBitmapDrawable(preview), i);
1518 }
1519 }
Winson Chung68e4c642011-11-10 15:48:25 -08001520
Michael Jurka39e5d172012-03-12 18:36:12 -07001521 layout.createHardwareLayer();
1522 invalidate();
1523
1524 // Update all thread priorities
1525 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1526 while (iter.hasNext()) {
1527 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1528 int pageIndex = task.page;
1529 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1530 }
1531 } finally {
1532 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001533 }
Winson Chungb44b5242011-06-13 11:32:14 -07001534 }
Winson Chung46af2e82011-05-09 16:00:53 -07001535
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 @Override
1537 public void syncPages() {
1538 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001539 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001540
Adam Cohen0cd3b642011-10-14 14:58:00 -07001541 Context context = getContext();
1542 for (int j = 0; j < mNumWidgetPages; ++j) {
1543 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1544 mWidgetCountY);
1545 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001546 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001547 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001548 }
1549
Adam Cohen0cd3b642011-10-14 14:58:00 -07001550 for (int i = 0; i < mNumAppsPages; ++i) {
1551 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1552 setupPage(layout);
1553 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001554 }
1555 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001556
Winson Chung785d2eb2011-04-14 16:08:02 -07001557 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001558 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001559 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001560 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001561 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001562 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001563 }
1564 }
1565
Adam Cohen22f823d2011-09-01 17:22:18 -07001566 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1567 // it is in the z-order. This is important to insure touch events are handled correctly.
1568 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001569 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001570 }
1571
Adam Cohenae4f1552011-10-20 00:15:42 -07001572 @Override
1573 protected int indexToPage(int index) {
1574 return getChildCount() - index - 1;
1575 }
1576
Adam Cohen22f823d2011-09-01 17:22:18 -07001577 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1578 @Override
1579 protected void screenScrolled(int screenCenter) {
1580 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001581
1582 for (int i = 0; i < getChildCount(); i++) {
1583 View v = getPageAt(i);
1584 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001585 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001586
1587 float interpolatedProgress =
1588 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1589 float scale = (1 - interpolatedProgress) +
1590 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1591 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001592
Adam Cohen2591f6a2011-10-25 14:36:40 -07001593 float alpha;
1594
Winson Chungd167e2a2012-04-26 13:13:01 -07001595 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001596 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001597 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001598 } else {
1599 // On large screens we need to fade the page as it nears its leftmost position
1600 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1601 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001602
1603 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1604 int pageWidth = v.getMeasuredWidth();
1605 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001606
1607 if (PERFORM_OVERSCROLL_ROTATION) {
1608 if (i == 0 && scrollProgress < 0) {
1609 // Overscroll to the left
1610 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1611 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1612 scale = 1.0f;
1613 alpha = 1.0f;
1614 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001615 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001616 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1617 // Overscroll to the right
1618 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1619 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1620 scale = 1.0f;
1621 alpha = 1.0f;
1622 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001623 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001624 } else {
1625 v.setPivotY(pageHeight / 2.0f);
1626 v.setPivotX(pageWidth / 2.0f);
1627 v.setRotationY(0f);
1628 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001629 }
1630
1631 v.setTranslationX(translationX);
1632 v.setScaleX(scale);
1633 v.setScaleY(scale);
1634 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001635
1636 // If the view has 0 alpha, we set it to be invisible so as to prevent
1637 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001638 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001639 v.setVisibility(INVISIBLE);
1640 } else if (v.getVisibility() != VISIBLE) {
1641 v.setVisibility(VISIBLE);
1642 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001643 }
1644 }
1645 }
1646
1647 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001648 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001649 }
1650
Winson Chung785d2eb2011-04-14 16:08:02 -07001651 /**
1652 * Used by the parent to get the content width to set the tab bar to
1653 * @return
1654 */
1655 public int getPageContentWidth() {
1656 return mContentWidth;
1657 }
1658
Winson Chungb26f3d62011-06-02 10:49:29 -07001659 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001660 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001661 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001662 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001663 // We reset the save index when we change pages so that it will be recalculated on next
1664 // rotation
1665 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001666 }
1667
Winson Chung785d2eb2011-04-14 16:08:02 -07001668 /*
1669 * AllAppsView implementation
1670 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001671 public void setup(Launcher launcher, DragController dragController) {
1672 mLauncher = launcher;
1673 mDragController = dragController;
1674 }
Winson Chung9802ac92012-06-08 16:01:58 -07001675
1676 /**
1677 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1678 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1679 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1680 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1681 */
1682 private void invalidateOnDataChange() {
1683 if (!isDataReady()) {
1684 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1685 // request a layout to trigger the page data when ready.
1686 requestLayout();
1687 } else {
1688 cancelAllTasks();
1689 invalidatePageData();
1690 }
1691 }
1692
Winson Chung785d2eb2011-04-14 16:08:02 -07001693 public void setApps(ArrayList<ApplicationInfo> list) {
1694 mApps = list;
1695 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001696 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001697 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001698 }
1699 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1700 // We add it in place, in alphabetical order
1701 int count = list.size();
1702 for (int i = 0; i < count; ++i) {
1703 ApplicationInfo info = list.get(i);
1704 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1705 if (index < 0) {
1706 mApps.add(-(index + 1), info);
1707 }
1708 }
1709 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001710 public void addApps(ArrayList<ApplicationInfo> list) {
1711 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001712 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001713 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001714 }
1715 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1716 ComponentName removeComponent = item.intent.getComponent();
1717 int length = list.size();
1718 for (int i = 0; i < length; ++i) {
1719 ApplicationInfo info = list.get(i);
1720 if (info.intent.getComponent().equals(removeComponent)) {
1721 return i;
1722 }
1723 }
1724 return -1;
1725 }
Winson Chungcd810732012-06-18 16:45:43 -07001726 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1727 int length = list.size();
1728 for (int i = 0; i < length; ++i) {
1729 ApplicationInfo info = list.get(i);
1730 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1731 return i;
1732 }
1733 }
1734 return -1;
1735 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001736 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1737 // loop through all the apps and remove apps that have the same component
1738 int length = list.size();
1739 for (int i = 0; i < length; ++i) {
1740 ApplicationInfo info = list.get(i);
1741 int removeIndex = findAppByComponent(mApps, info);
1742 if (removeIndex > -1) {
1743 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001744 }
1745 }
1746 }
Winson Chungcd810732012-06-18 16:45:43 -07001747 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1748 // loop through all the package names and remove apps that have the same package name
1749 for (String pn : packageNames) {
1750 int removeIndex = findAppByPackage(mApps, pn);
1751 while (removeIndex > -1) {
1752 mApps.remove(removeIndex);
1753 removeIndex = findAppByPackage(mApps, pn);
1754 }
1755 }
1756 }
1757 public void removeApps(ArrayList<String> packageNames) {
1758 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001759 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001760 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001761 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001762 public void updateApps(ArrayList<ApplicationInfo> list) {
1763 // We remove and re-add the updated applications list because it's properties may have
1764 // changed (ie. the title), and this will ensure that the items will be in their proper
1765 // place in the list.
1766 removeAppsWithoutInvalidate(list);
1767 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001768 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001769 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001770 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001771
Winson Chung785d2eb2011-04-14 16:08:02 -07001772 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001773 // If we have reset, then we should not continue to restore the previous state
1774 mSaveInstanceStateItemIndex = -1;
1775
Adam Cohenb64d36e2011-10-17 21:48:02 -07001776 AppsCustomizeTabHost tabHost = getTabHost();
1777 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001778 if (tag != null) {
1779 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1780 tabHost.setCurrentTabFromContent(ContentType.Applications);
1781 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001782 }
Winson Chung649668f2012-01-10 13:07:16 -08001783
Adam Cohenb64d36e2011-10-17 21:48:02 -07001784 if (mCurrentPage != 0) {
1785 invalidatePageData(0);
1786 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001787 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001788
1789 private AppsCustomizeTabHost getTabHost() {
1790 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1791 }
1792
Winson Chung785d2eb2011-04-14 16:08:02 -07001793 public void dumpState() {
1794 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001795 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1796 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001797 }
Adam Cohen4e844012011-11-09 13:48:04 -08001798
Winson Chung785d2eb2011-04-14 16:08:02 -07001799 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001800 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001801 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001802 for (Object i: list) {
1803 if (i instanceof AppWidgetProviderInfo) {
1804 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1805 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1806 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1807 + " initialLayout=" + info.initialLayout
1808 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1809 } else if (i instanceof ResolveInfo) {
1810 ResolveInfo info = (ResolveInfo) i;
1811 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1812 + info.icon);
1813 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001814 }
1815 }
Adam Cohen4e844012011-11-09 13:48:04 -08001816
Winson Chung785d2eb2011-04-14 16:08:02 -07001817 public void surrender() {
1818 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1819 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001820
1821 // Stop all background tasks
1822 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001823 }
Winson Chung007c6982011-06-14 13:27:53 -07001824
Winson Chunge4e50662012-01-23 14:45:13 -08001825 @Override
1826 public void iconPressed(PagedViewIcon icon) {
1827 // Reset the previously pressed icon and store a reference to the pressed icon so that
1828 // we can reset it on return to Launcher (in Launcher.onResume())
1829 if (mPressedIcon != null) {
1830 mPressedIcon.resetDrawableState();
1831 }
1832 mPressedIcon = icon;
1833 }
1834
1835 public void resetDrawableState() {
1836 if (mPressedIcon != null) {
1837 mPressedIcon.resetDrawableState();
1838 mPressedIcon = null;
1839 }
1840 }
Winson Chung68e4c642011-11-10 15:48:25 -08001841
Winson Chungb44b5242011-06-13 11:32:14 -07001842 /*
1843 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1844 * widget previews in the background with the AsyncTasks.
1845 */
Winson Chung68e4c642011-11-10 15:48:25 -08001846 final static int sLookBehindPageCount = 2;
1847 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001848 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001849 final int count = getChildCount();
1850 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1851 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1852 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001853 }
1854 protected int getAssociatedUpperPageBound(int page) {
1855 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001856 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1857 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1858 count - 1);
1859 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001860 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001861
1862 @Override
1863 protected String getCurrentPageDescription() {
1864 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1865 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001866 int count = 0;
1867
Adam Cohen0cd3b642011-10-14 14:58:00 -07001868 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001869 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001870 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001871 } else {
1872 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001873 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001874 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001875 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001876
Michael Jurka8b805b12012-04-18 14:23:14 -07001877 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001878 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001879}