blob: 0b8b268a90b652e3c0c80497556d93a2dd0e5d45 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080033import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070035import android.graphics.ColorMatrix;
36import android.graphics.ColorMatrixColorFilter;
Peter Ng8db70002011-10-25 15:40:08 -070037import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080038import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070039import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070040import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080042import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070043import android.graphics.Shader;
Michael Jurkadac85912012-05-18 15:04:49 -070044import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070046import android.os.AsyncTask;
47import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.util.AttributeSet;
49import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070050import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080051import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070052import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070053import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070054import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070055import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070056import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070057import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070058import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070059import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070060import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
62import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070063import com.android.launcher2.DropTarget.DragObject;
64
Michael Jurka9bc8eba2012-05-21 20:36:44 -070065import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066import java.util.ArrayList;
67import java.util.Collections;
68import java.util.Iterator;
69import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070070
Winson Chungb44b5242011-06-13 11:32:14 -070071/**
72 * A simple callback interface which also provides the results of the task.
73 */
74interface AsyncTaskCallback {
75 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
76}
Winson Chung4e076542011-06-23 13:04:10 -070077
Winson Chungb44b5242011-06-13 11:32:14 -070078/**
79 * The data needed to perform either of the custom AsyncTasks.
80 */
81class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070082 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080083 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070084 }
85
Winson Chungb44b5242011-06-13 11:32:14 -070086 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
87 AsyncTaskCallback postR) {
88 page = p;
89 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070090 sourceImages = si;
91 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070092 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070093 doInBackgroundCallback = bgR;
94 postExecuteCallback = postR;
95 }
Michael Jurka038f9d82011-11-03 13:50:45 -070096 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070097 AsyncTaskCallback postR) {
98 page = p;
99 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700100 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700101 maxImageWidth = cw;
102 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 doInBackgroundCallback = bgR;
104 postExecuteCallback = postR;
105 }
Winson Chung09945932011-09-20 14:22:40 -0700106 void cleanup(boolean cancelled) {
107 // Clean up any references to source/generated bitmaps
108 if (sourceImages != null) {
109 if (cancelled) {
110 for (Bitmap b : sourceImages) {
111 b.recycle();
112 }
113 }
114 sourceImages.clear();
115 }
116 if (generatedImages != null) {
117 if (cancelled) {
118 for (Bitmap b : generatedImages) {
119 b.recycle();
120 }
121 }
122 generatedImages.clear();
123 }
124 }
Winson Chungb44b5242011-06-13 11:32:14 -0700125 int page;
126 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700127 ArrayList<Bitmap> sourceImages;
128 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700129 int maxImageWidth;
130 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700131 AsyncTaskCallback doInBackgroundCallback;
132 AsyncTaskCallback postExecuteCallback;
133}
Winson Chung4e076542011-06-23 13:04:10 -0700134
Winson Chungb44b5242011-06-13 11:32:14 -0700135/**
136 * A generic template for an async task used in AppsCustomize.
137 */
138class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700139 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700140 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700142 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700143 }
144 @Override
145 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
146 if (params.length != 1) return null;
147 // Load each of the widget previews in the background
148 params[0].doInBackgroundCallback.run(this, params[0]);
149 return params[0];
150 }
151 @Override
152 protected void onPostExecute(AsyncTaskPageData result) {
153 // All the widget previews are loaded, so we can just callback to inflate the page
154 result.postExecuteCallback.run(this, result);
155 }
156
157 void setThreadPriority(int p) {
158 threadPriority = p;
159 }
160 void syncThreadPriority() {
161 Process.setThreadPriority(threadPriority);
162 }
163
164 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700165 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700166 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700167 int threadPriority;
168}
Winson Chungb44b5242011-06-13 11:32:14 -0700169
Michael Jurkadac85912012-05-18 15:04:49 -0700170abstract class WeakReferenceThreadLocal<T> {
171 private ThreadLocal<WeakReference<T>> mThreadLocal;
172 public WeakReferenceThreadLocal() {
173 mThreadLocal = new ThreadLocal<WeakReference<T>>();
174 }
175
176 abstract T initialValue();
177
178 public void set(T t) {
179 mThreadLocal.set(new WeakReference<T>(t));
180 }
181
182 public T get() {
183 WeakReference<T> reference = mThreadLocal.get();
184 T obj;
185 if (reference == null) {
186 obj = initialValue();
187 mThreadLocal.set(new WeakReference<T>(obj));
188 return obj;
189 } else {
190 obj = reference.get();
191 if (obj == null) {
192 obj = initialValue();
193 mThreadLocal.set(new WeakReference<T>(obj));
194 }
195 return obj;
196 }
197 }
198}
199
200class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
201 @Override
202 protected Canvas initialValue() {
203 return new Canvas();
204 }
205}
206
207class PaintCache extends WeakReferenceThreadLocal<Paint> {
208 @Override
209 protected Paint initialValue() {
210 return null;
211 }
212}
213
214class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
215 @Override
216 protected Bitmap initialValue() {
217 return null;
218 }
219}
220
221class RectCache extends WeakReferenceThreadLocal<Rect> {
222 @Override
223 protected Rect initialValue() {
224 return new Rect();
225 }
226}
227
Winson Chungb44b5242011-06-13 11:32:14 -0700228/**
229 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
230 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700231public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700232 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700233 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
234 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700235 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700236
237 /**
238 * The different content types that this paged view can show.
239 */
240 public enum ContentType {
241 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700242 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700243 }
244
245 // Refs
246 private Launcher mLauncher;
247 private DragController mDragController;
248 private final LayoutInflater mLayoutInflater;
249 private final PackageManager mPackageManager;
250
Winson Chung5afbf7b2011-07-25 11:53:08 -0700251 // Save and Restore
252 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800253 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700254
Winson Chung785d2eb2011-04-14 16:08:02 -0700255 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700256 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700257 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700258
Winson Chung7d7541e2011-09-16 20:14:36 -0700259 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800260 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 private int mClingFocusedX;
262 private int mClingFocusedY;
263
Winson Chung1ed747a2011-05-03 16:18:34 -0700264 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700265 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700266 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700267
268 // Dimens
269 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700270 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800271 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700272 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700273 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700274 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700275 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700276 private int mNumAppsPages;
277 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700278
Adam Cohen22f823d2011-09-01 17:22:18 -0700279 // Relating to the scroll and overscroll effects
280 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700281 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700282 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700283 private static float TRANSITION_PIVOT = 0.65f;
284 private static float TRANSITION_MAX_ROTATION = 22;
285 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700286 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700287 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700288
Winson Chungb44b5242011-06-13 11:32:14 -0700289 // Previews & outlines
290 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800291 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700292
Adam Cohened66b2b2012-01-23 17:28:51 -0800293 private Runnable mInflateWidgetRunnable = null;
294 private Runnable mBindWidgetRunnable = null;
295 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700296 static final int WIDGET_PRELOAD_PENDING = 0;
297 static final int WIDGET_BOUND = 1;
298 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800299 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
300 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800301 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800302 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800303
Michael Jurka39e5d172012-03-12 18:36:12 -0700304 // Deferral of loading widget previews during launcher transitions
305 private boolean mInTransition;
306 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
307 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700308 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
309 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700310
Michael Jurkadac85912012-05-18 15:04:49 -0700311 // Used for drawing shortcut previews
312 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
313 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
314 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
315
316 // Used for drawing widget previews
317 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
318 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
319 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
320 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
321
Winson Chung785d2eb2011-04-14 16:08:02 -0700322 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
323 super(context, attrs);
324 mLayoutInflater = LayoutInflater.from(context);
325 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700326 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700327 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700328 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700329 mCanvas = new Canvas();
330 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700331
332 // Save the default widget preview background
333 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700334 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700335
Winson Chung6032e7e2011-11-08 15:47:17 -0800336 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
337 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
338 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 mWidgetWidthGap =
340 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
341 mWidgetHeightGap =
342 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700343 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
344 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700345 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
346 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700347 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700349
Winson Chung1ed747a2011-05-03 16:18:34 -0700350 // The padding on the non-matched dimension for the default widget preview icons
351 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700352 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700353
354 // Unless otherwise specified this view is important for accessibility.
355 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
356 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
357 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700358 }
359
360 @Override
361 protected void init() {
362 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700363 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700364
365 Context context = getContext();
366 Resources r = context.getResources();
367 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
368 }
369
Winson Chungf0ea4d32011-06-06 14:27:16 -0700370 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700371 protected void onUnhandledTap(MotionEvent ev) {
372 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700373 // Dismiss AppsCustomize if we tap
374 mLauncher.showWorkspace(true);
375 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 }
377
Winson Chung5afbf7b2011-07-25 11:53:08 -0700378 /** Returns the item index of the center item on this page so that we can restore to this
379 * item index when we rotate. */
380 private int getMiddleComponentIndexOnCurrentPage() {
381 int i = -1;
382 if (getPageCount() > 0) {
383 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700384 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700385 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700386 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
387 int numItemsPerPage = mCellCountX * mCellCountY;
388 int childCount = childrenLayout.getChildCount();
389 if (childCount > 0) {
390 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700391 }
392 } else {
393 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700394 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700395 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
396 int childCount = layout.getChildCount();
397 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700398 i = numApps +
399 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
400 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700401 }
402 }
403 return i;
404 }
405
406 /** Get the index of the item to restore to if we need to restore the current page. */
407 int getSaveInstanceStateIndex() {
408 if (mSaveInstanceStateItemIndex == -1) {
409 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
410 }
411 return mSaveInstanceStateItemIndex;
412 }
413
414 /** Returns the page in the current orientation which is expected to contain the specified
415 * item index. */
416 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700417 if (index < 0) return 0;
418
419 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700420 int numItemsPerPage = mCellCountX * mCellCountY;
421 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700422 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700423 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700424 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
425 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700426 }
427
Winson Chung5afbf7b2011-07-25 11:53:08 -0700428 /** Restores the page for an item at the specified index */
429 void restorePageForIndex(int index) {
430 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700431 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700432 }
433
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700434 private void updatePageCounts() {
435 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
436 (float) (mWidgetCountX * mWidgetCountY));
437 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
438 }
439
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 protected void onDataReady(int width, int height) {
441 // Note that we transpose the counts in portrait so that we get a similar layout
442 boolean isLandscape = getResources().getConfiguration().orientation ==
443 Configuration.ORIENTATION_LANDSCAPE;
444 int maxCellCountX = Integer.MAX_VALUE;
445 int maxCellCountY = Integer.MAX_VALUE;
446 if (LauncherApplication.isScreenLarge()) {
447 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
448 LauncherModel.getCellCountY());
449 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
450 LauncherModel.getCellCountX());
451 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800452 if (mMaxAppCellCountX > -1) {
453 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
454 }
455 if (mMaxAppCellCountY > -1) {
456 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
457 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700458
459 // Now that the data is ready, we can calculate the content width, the number of cells to
460 // use for each page
461 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
462 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
463 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
464 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
465 mCellCountX = mWidgetSpacingLayout.getCellCountX();
466 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700467 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700468
Winson Chungdb1138b2011-06-30 14:39:35 -0700469 // Force a measure to update recalculate the gaps
470 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
471 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
472 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700473 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700474
Michael Jurkae326f182011-11-21 14:05:46 -0800475 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
476 final boolean hostIsTransitioning = host.isTransitioning();
477
Adam Cohen0cd3b642011-10-14 14:58:00 -0700478 // Restore the page
479 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800480 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700481
Winson Chung3f4e1422011-11-17 14:58:51 -0800482 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
483 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
484 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800485 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800486 post(new Runnable() {
487 @Override
488 public void run() {
489 showAllAppsCling();
490 }
491 });
492 }
493 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700494
Winson Chung3f4e1422011-11-17 14:58:51 -0800495 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700496 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800497 mHasShownAllAppsCling = true;
498 // Calculate the position for the cling punch through
499 int[] offset = new int[2];
500 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
501 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
502 // PagedViews are centered horizontally but top aligned
503 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
504 offset[0];
505 pos[1] += offset[1];
506 mLauncher.showFirstRunAllAppsCling(pos);
507 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700508 }
509
510 @Override
511 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
512 int width = MeasureSpec.getSize(widthMeasureSpec);
513 int height = MeasureSpec.getSize(heightMeasureSpec);
514 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700515 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700516 setDataIsReady();
517 setMeasuredDimension(width, height);
518 onDataReady(width, height);
519 }
520 }
521
522 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
523 }
524
Winson Chung785d2eb2011-04-14 16:08:02 -0700525 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700526 // Get the list of widgets and shortcuts
527 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700528 List<AppWidgetProviderInfo> widgets =
529 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
531 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800532 for (AppWidgetProviderInfo widget : widgets) {
533 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700534 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700535 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
536 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700537 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
538 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700539 if (minSpanX <= LauncherModel.getCellCountX() &&
540 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700541 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700542 } else {
543 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
544 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700545 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800546 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700547 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800548 widget.minWidth + ", " + widget.minHeight + ")");
549 }
550 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700551 mWidgets.addAll(shortcuts);
552 Collections.sort(mWidgets,
553 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700554 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700555 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700556 }
557
558 @Override
559 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700560 // When we have exited all apps or are in transition, disregard clicks
561 if (!mLauncher.isAllAppsCustomizeOpen() ||
562 mLauncher.getWorkspace().isSwitchingState()) return;
563
Winson Chung4b576be2011-04-27 17:40:20 -0700564 if (v instanceof PagedViewIcon) {
565 // Animate some feedback to the click
566 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800567
568 // Lock the drawable state to pressed until we return to Launcher
569 if (mPressedIcon != null) {
570 mPressedIcon.lockDrawableState();
571 }
Winson Chungc7450e32012-04-17 17:34:08 -0700572
Winson Chung18f41f82012-05-09 13:28:10 -0700573 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
574 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
575 // when Launcher resumes and we are still in AllApps.
576 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700577 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
578
Winson Chung4b576be2011-04-27 17:40:20 -0700579 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700580 // Let the user know that they have to long press to add a widget
581 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
582 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700583
Winson Chungd2e87b32011-06-02 10:53:07 -0700584 // Create a little animation to show that the widget can move
585 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
586 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700587 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
588 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700589 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700590 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700591 tydAnim.setDuration(100);
592 bounce.play(tyuAnim).before(tydAnim);
593 bounce.setInterpolator(new AccelerateInterpolator());
594 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700595 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700596 }
597
Winson Chungc6f10b92011-11-14 11:39:07 -0800598 public boolean onKey(View v, int keyCode, KeyEvent event) {
599 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
600 }
601
Winson Chung785d2eb2011-04-14 16:08:02 -0700602 /*
603 * PagedViewWithDraggableItems implementation
604 */
605 @Override
606 protected void determineDraggingStart(android.view.MotionEvent ev) {
607 // Disable dragging by pulling an app down for now.
608 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700609
Winson Chung4b576be2011-04-27 17:40:20 -0700610 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700611 mLauncher.getWorkspace().onDragStartedWithItem(v);
612 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700613 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700614
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700615 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 final AppWidgetProviderInfo pInfo = info.info;
617 if (pInfo.configure != null) {
618 return;
619 }
620
Adam Cohen21a170b2012-05-30 15:17:06 -0700621 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 mBindWidgetRunnable = new Runnable() {
623 @Override
624 public void run() {
625 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700626 if (AppWidgetManager.getInstance(mLauncher)
627 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
628 mWidgetCleanupState = WIDGET_BOUND;
629 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 }
631 };
632 post(mBindWidgetRunnable);
633
634 mInflateWidgetRunnable = new Runnable() {
635 @Override
636 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 AppWidgetHostView hostView = mLauncher.
638 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 info.boundWidget = hostView;
640 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800641 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700642 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
643 info.spanY, info, false);
644
645 // We want the first widget layout to be the correct size. This will be important
646 // for width size reporting to the AppWidgetManager.
647 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
648 unScaledSize[1]);
649 lp.x = lp.y = 0;
650 lp.customPosition = true;
651 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800652 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 }
654 };
655 post(mInflateWidgetRunnable);
656 }
657
658 @Override
659 public void onShortPress(View v) {
660 // We are anticipating a long press, and we use this time to load bind and instantiate
661 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700662 if (mCreateWidgetInfo != null) {
663 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
664 cleanupWidgetPreloading(false);
665 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800666 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700667 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 }
669
Adam Cohen0e56cc92012-05-11 15:57:05 -0700670 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
671 if (!widgetWasAdded) {
672 // If the widget was not added, we may need to do further cleanup.
673 PendingAddWidgetInfo info = mCreateWidgetInfo;
674 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700675
676 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700677 // We never did any preloading, so just remove pending callbacks to do so
678 removeCallbacks(mBindWidgetRunnable);
679 removeCallbacks(mInflateWidgetRunnable);
680 } else if (mWidgetCleanupState == WIDGET_BOUND) {
681 // Delete the widget id which was allocated
682 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700683 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
684 }
685
686 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700687 removeCallbacks(mInflateWidgetRunnable);
688 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700689 // Delete the widget id which was allocated
690 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700691 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
692 }
693
Adam Cohen0e56cc92012-05-11 15:57:05 -0700694 // The widget was inflated and added to the DragLayer -- remove it.
695 AppWidgetHostView widget = info.boundWidget;
696 mLauncher.getDragLayer().removeView(widget);
697 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 }
699 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
700 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700701 mCreateWidgetInfo = null;
702 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800703 }
704
Adam Cohen7a326642012-02-22 12:03:22 -0800705 @Override
706 public void cleanUpShortPress(View v) {
707 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700708 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800709 }
710 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800711
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700712 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800713 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700714 // Get the widget preview as the drag representation
715 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700716 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700717
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700718 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
719 // we abort the drag.
720 if (image.getDrawable() == null) {
721 mDraggingWidget = false;
722 return false;
723 }
724
Winson Chung4b576be2011-04-27 17:40:20 -0700725 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800726 Bitmap preview;
727 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800728 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700729 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700730 // This can happen in some weird cases involving multi-touch. We can't start dragging
731 // the widget if this is null, so we break out.
732 if (mCreateWidgetInfo == null) {
733 return false;
734 }
735
Adam Cohen1b36dc32012-02-13 19:27:37 -0800736 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
737 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700738 int spanX = createItemInfo.spanX;
739 int spanY = createItemInfo.spanY;
740 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
741 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700742
Winson Chung72d59842012-02-22 13:51:36 -0800743 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
744 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700745 int maxWidth, maxHeight;
746 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
747 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800748 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700749 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800750
751 // Determine the image view drawable scale relative to the preview
752 float[] mv = new float[9];
753 Matrix m = new Matrix();
754 m.setRectToRect(
755 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
756 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
757 (float) previewDrawable.getIntrinsicHeight()),
758 Matrix.ScaleToFit.START);
759 m.getValues(mv);
760 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700761 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700762 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
763 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
764 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
765 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
766
Winson Chung1120e032011-11-22 16:11:31 -0800767 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700768 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700769 renderDrawableToBitmap(icon, preview, 0, 0,
770 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700771 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700772 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700773 createItemInfo.spanX = createItemInfo.spanY = 1;
774 }
Winson Chung4b576be2011-04-27 17:40:20 -0700775
Michael Jurka8c3339b2012-06-14 16:18:21 -0700776 // Don't clip alpha values for the drag outline if we're using the default widget preview
777 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
778 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700779
Winson Chung1120e032011-11-22 16:11:31 -0800780 // Save the preview for the outline generation, then dim the preview
781 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
782 false);
Winson Chung1120e032011-11-22 16:11:31 -0800783
Winson Chung4b576be2011-04-27 17:40:20 -0700784 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700785 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700786 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800787 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800788 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800789 outline.recycle();
790 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700791 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700792 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800793
Winson Chung4b576be2011-04-27 17:40:20 -0700794 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700796 if (!super.beginDragging(v)) return false;
797
798 if (v instanceof PagedViewIcon) {
799 beginDraggingApplication(v);
800 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700801 if (!beginDraggingWidget(v)) {
802 return false;
803 }
Winson Chung4b576be2011-04-27 17:40:20 -0700804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805
806 // We delay entering spring-loaded mode slightly to make sure the UI
807 // thready is free of any work.
808 postDelayed(new Runnable() {
809 @Override
810 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800811 // We don't enter spring-loaded mode if the drag has been cancelled
812 if (mLauncher.getDragController().isDragging()) {
813 // Dismiss the cling
814 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800815
Adam Cohen1b36dc32012-02-13 19:27:37 -0800816 // Reset the alpha on the dragged icon before we drag
817 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800818
Adam Cohen1b36dc32012-02-13 19:27:37 -0800819 // Go into spring loaded mode (must happen before we startDrag())
820 mLauncher.enterSpringLoadedDragMode();
821 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 }
Winson Chung72d59842012-02-22 13:51:36 -0800823 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 return true;
826 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800827
Winson Chunga48487a2012-03-20 16:19:37 -0700828 /**
829 * Clean up after dragging.
830 *
831 * @param target where the item was dragged to (can be null if the item was flung)
832 */
833 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700834 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700835 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700836 // Exit spring loaded mode if we have not successfully dropped or have not handled the
837 // drop in Workspace
838 mLauncher.exitSpringLoadedDragMode();
839 }
Winson Chung4b919f82012-05-01 10:44:08 -0700840 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700841 }
842
Winson Chung785d2eb2011-04-14 16:08:02 -0700843 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700844 public View getContent() {
845 return null;
846 }
847
848 @Override
849 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700850 mInTransition = true;
851 if (toWorkspace) {
852 cancelAllTasks();
853 }
854 }
855
856 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700857 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700858 }
859
860 @Override
861 public void onLauncherTransitionStep(Launcher l, float t) {
862 }
863
864 @Override
865 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
866 mInTransition = false;
867 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
868 onSyncWidgetPageItems(d);
869 }
870 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700871 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
872 r.run();
873 }
874 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700875 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700876 }
877
878 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700879 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
880 boolean success) {
881 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
882 if (isFlingToDelete) return;
883
884 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700885
886 // Display an error message if the drag failed due to there not being enough space on the
887 // target layout we were dropping on.
888 if (!success) {
889 boolean showOutOfSpaceMessage = false;
890 if (target instanceof Workspace) {
891 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
892 Workspace workspace = (Workspace) target;
893 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700894 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700895 if (layout != null) {
896 layout.calculateSpans(itemInfo);
897 showOutOfSpaceMessage =
898 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
899 }
900 }
Winson Chungfc79c802011-05-02 13:35:34 -0700901 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700902 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700903 }
Adam Cohen7a326642012-02-22 12:03:22 -0800904
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800905 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700906 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700907 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800908 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700909 }
910
Winson Chunga48487a2012-03-20 16:19:37 -0700911 @Override
912 public void onFlingToDeleteCompleted() {
913 // We just dismiss the drag when we fling, so cleanup here
914 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700915 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700916 mDraggingWidget = false;
917 }
918
919 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800920 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700921 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800922 }
923
Winson Chung7f0acdd2011-09-19 18:34:19 -0700924 @Override
925 protected void onDetachedFromWindow() {
926 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700927 cancelAllTasks();
928 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700929
Michael Jurkae326f182011-11-21 14:05:46 -0800930 public void clearAllWidgetPages() {
931 cancelAllTasks();
932 int count = getChildCount();
933 for (int i = 0; i < count; i++) {
934 View v = getPageAt(i);
935 if (v instanceof PagedViewGridLayout) {
936 ((PagedViewGridLayout) v).removeAllViewsOnPage();
937 mDirtyPageContent.set(i, true);
938 }
939 }
940 }
941
Adam Cohen0cd3b642011-10-14 14:58:00 -0700942 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700943 // Clean up all the async tasks
944 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
945 while (iter.hasNext()) {
946 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
947 task.cancel(false);
948 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700949 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700950
951 // We've already preallocated the views for the data to load into, so clear them as well
952 View v = getPageAt(task.page);
953 if (v instanceof PagedViewGridLayout) {
954 ((PagedViewGridLayout) v).removeAllViewsOnPage();
955 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700956 }
Winson Chung83687b12012-04-25 16:01:01 -0700957 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700958 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700959 }
960
Winson Chung785d2eb2011-04-14 16:08:02 -0700961 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700962 if (type == ContentType.Widgets) {
963 invalidatePageData(mNumAppsPages, true);
964 } else if (type == ContentType.Applications) {
965 invalidatePageData(0, true);
966 }
Winson Chungb44b5242011-06-13 11:32:14 -0700967 }
968
Adam Cohen0cd3b642011-10-14 14:58:00 -0700969 protected void snapToPage(int whichPage, int delta, int duration) {
970 super.snapToPage(whichPage, delta, duration);
971 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800972
973 // Update the thread priorities given the direction lookahead
974 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
975 while (iter.hasNext()) {
976 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700977 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800978 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
979 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
980 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
981 } else {
982 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
983 }
984 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700985 }
986
987 private void updateCurrentTab(int currentPage) {
988 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800989 if (tabHost != null) {
990 String tag = tabHost.getCurrentTabTag();
991 if (tag != null) {
992 if (currentPage >= mNumAppsPages &&
993 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
994 tabHost.setCurrentTabFromContent(ContentType.Widgets);
995 } else if (currentPage < mNumAppsPages &&
996 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
997 tabHost.setCurrentTabFromContent(ContentType.Applications);
998 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700999 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001000 }
1001 }
1002
Winson Chung785d2eb2011-04-14 16:08:02 -07001003 /*
1004 * Apps PagedView implementation
1005 */
Winson Chung63257c12011-05-05 17:06:13 -07001006 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1007 int childCount = layout.getChildCount();
1008 for (int i = 0; i < childCount; ++i) {
1009 layout.getChildAt(i).setVisibility(visibility);
1010 }
1011 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 private void setupPage(PagedViewCellLayout layout) {
1013 layout.setCellCount(mCellCountX, mCellCountY);
1014 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1015 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1016 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1017
Winson Chung63257c12011-05-05 17:06:13 -07001018 // Note: We force a measure here to get around the fact that when we do layout calculations
1019 // immediately after syncing, we don't have a proper width. That said, we already know the
1020 // expected page width, so we can actually optimize by hiding all the TextView-based
1021 // children that are expensive to measure, and let that happen naturally later.
1022 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001023 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001024 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001025 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001026 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001027 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001028 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001029
Winson Chungf314b0e2011-08-16 11:54:27 -07001030 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001031 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 int numCells = mCellCountX * mCellCountY;
1033 int startIndex = page * numCells;
1034 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001035 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001036
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001038 ArrayList<Object> items = new ArrayList<Object>();
1039 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 for (int i = startIndex; i < endIndex; ++i) {
1041 ApplicationInfo info = mApps.get(i);
1042 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1043 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001044 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001045 icon.setOnClickListener(this);
1046 icon.setOnLongClickListener(this);
1047 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001048 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001049
1050 int index = i - startIndex;
1051 int x = index % mCellCountX;
1052 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001053 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001054
1055 items.add(info);
1056 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001057 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001058
Winson Chungf0ea4d32011-06-06 14:27:16 -07001059 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 }
Winson Chungb44b5242011-06-13 11:32:14 -07001061
1062 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001063 * A helper to return the priority for loading of the specified widget page.
1064 */
1065 private int getWidgetPageLoadPriority(int page) {
1066 // If we are snapping to another page, use that index as the target page index
1067 int toPage = mCurrentPage;
1068 if (mNextPage > -1) {
1069 toPage = mNextPage;
1070 }
1071
1072 // We use the distance from the target page as an initial guess of priority, but if there
1073 // are no pages of higher priority than the page specified, then bump up the priority of
1074 // the specified page.
1075 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1076 int minPageDiff = Integer.MAX_VALUE;
1077 while (iter.hasNext()) {
1078 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001079 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001080 }
1081
1082 int rawPageDiff = Math.abs(page - toPage);
1083 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1084 }
1085 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001086 * Return the appropriate thread priority for loading for a given page (we give the current
1087 * page much higher priority)
1088 */
1089 private int getThreadPriorityForPage(int page) {
1090 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001091 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001092 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001093 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001094 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001095 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001096 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001097 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001098 }
1099 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001100 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001101 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001102 return Math.max(0, pageDiff * sPageSleepDelay);
1103 }
Winson Chungb44b5242011-06-13 11:32:14 -07001104 /**
1105 * Creates and executes a new AsyncTask to load a page of widget previews.
1106 */
1107 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001108 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001109
Winson Chungb44b5242011-06-13 11:32:14 -07001110 // Prune all tasks that are no longer needed
1111 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1112 while (iter.hasNext()) {
1113 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001114 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001115 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1116 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001117 task.cancel(false);
1118 iter.remove();
1119 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001120 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001121 }
1122 }
1123
Winson Chungf314b0e2011-08-16 11:54:27 -07001124 // 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 -07001125 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001126 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001127 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001128 @Override
1129 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001130 try {
Winson Chung09945932011-09-20 14:22:40 -07001131 try {
1132 Thread.sleep(sleepMs);
1133 } catch (Exception e) {}
1134 loadWidgetPreviewsInBackground(task, data);
1135 } finally {
1136 if (task.isCancelled()) {
1137 data.cleanup(true);
1138 }
1139 }
Winson Chungb44b5242011-06-13 11:32:14 -07001140 }
1141 },
1142 new AsyncTaskCallback() {
1143 @Override
1144 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001145 mRunningTasks.remove(task);
1146 if (task.isCancelled()) return;
1147 // do cleanup inside onSyncWidgetPageItems
1148 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001149 }
Winson Chung09945932011-09-20 14:22:40 -07001150 });
Winson Chungb44b5242011-06-13 11:32:14 -07001151
1152 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001153 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001154 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001155 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001156 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1157 mRunningTasks.add(t);
1158 }
Winson Chungb44b5242011-06-13 11:32:14 -07001159
Winson Chung785d2eb2011-04-14 16:08:02 -07001160 /*
1161 * Widgets PagedView implementation
1162 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001163 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001164 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1165 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001166
1167 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001168 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001169 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1170 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001171 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001172 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001174
Winson Chung5fc72b32011-10-11 17:53:58 -07001175 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001176 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001177 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001178
Winson Chung5fc72b32011-10-11 17:53:58 -07001179 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001180 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001181 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001182 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001183 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001184 Rect oldBounds = d.copyBounds();
1185 d.setBounds(x, y, x + w, y + h);
1186 d.draw(c);
1187 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001188 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001189 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001190 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001191
Michael Jurkadac85912012-05-18 15:04:49 -07001192 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1193 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1194 final Canvas c = mCachedShortcutPreviewCanvas.get();
1195 if (tempBitmap == null ||
1196 tempBitmap.getWidth() != maxWidth ||
1197 tempBitmap.getHeight() != maxHeight) {
1198 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1199 mCachedShortcutPreviewBitmap.set(tempBitmap);
1200 } else {
1201 c.setBitmap(tempBitmap);
1202 c.drawColor(0, PorterDuff.Mode.CLEAR);
1203 c.setBitmap(null);
1204 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001205 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001206 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001207
1208 int paddingTop =
1209 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1210 int paddingLeft =
1211 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1212 int paddingRight =
1213 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1214
1215 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001216
1217 renderDrawableToBitmap(
1218 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1219
1220 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1221 c.setBitmap(preview);
1222 Paint p = mCachedShortcutPreviewPaint.get();
1223 if (p == null) {
1224 p = new Paint();
1225 ColorMatrix colorMatrix = new ColorMatrix();
1226 colorMatrix.setSaturation(0);
1227 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1228 p.setAlpha((int) (255 * 0.06f));
1229 //float density = 1f;
1230 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1231 mCachedShortcutPreviewPaint.set(p);
1232 }
1233 c.drawBitmap(tempBitmap, 0, 0, p);
1234 c.setBitmap(null);
1235
1236 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1237
Winson Chungb44b5242011-06-13 11:32:14 -07001238 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001239 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001240
Michael Jurkadac85912012-05-18 15:04:49 -07001241 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1242 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1243 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001244 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001245 String packageName = provider.getPackageName();
1246 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1247 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001248
Michael Jurka038f9d82011-11-03 13:50:45 -07001249 Drawable drawable = null;
1250 if (previewImage != 0) {
1251 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1252 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001253 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001254 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001255 }
1256 }
1257
Michael Jurka038f9d82011-11-03 13:50:45 -07001258 int bitmapWidth;
1259 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001260 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001261 boolean widgetPreviewExists = (drawable != null);
1262 if (widgetPreviewExists) {
1263 bitmapWidth = drawable.getIntrinsicWidth();
1264 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001265 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001266 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001267 if (cellHSpan < 1) cellHSpan = 1;
1268 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001269
1270 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1271 .getDrawable(R.drawable.widget_preview_tile);
1272 final int previewDrawableWidth = previewDrawable
1273 .getIntrinsicWidth();
1274 final int previewDrawableHeight = previewDrawable
1275 .getIntrinsicHeight();
1276 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1277 bitmapHeight = previewDrawableHeight * cellVSpan;
1278
1279 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1280 Config.ARGB_8888);
1281 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1282 c.setBitmap(defaultPreview);
1283 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1284 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1285 Shader.TileMode.REPEAT);
1286 previewDrawable.draw(c);
1287 c.setBitmap(null);
1288
1289 // Draw the icon in the top left corner
1290 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1291 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1292 float iconScale = Math.min((float) smallestSide
1293 / (mAppIconSize + 2 * minOffset), 1f);
1294
1295 try {
1296 Drawable icon = null;
1297 int hoffset =
1298 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1299 int yoffset =
1300 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1301 if (iconId > 0)
1302 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001303 if (icon != null) {
1304 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1305 yoffset, (int) (mAppIconSize * iconScale),
1306 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001307 }
Michael Jurkadac85912012-05-18 15:04:49 -07001308 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001309 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001310 }
1311
Michael Jurkadac85912012-05-18 15:04:49 -07001312 // Scale to fit width only - let the widget preview be clipped in the
1313 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001314 float scale = 1f;
1315 if (bitmapWidth > maxWidth) {
1316 scale = maxWidth / (float) bitmapWidth;
1317 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001318 if (scale != 1f) {
1319 bitmapWidth = (int) (scale * bitmapWidth);
1320 bitmapHeight = (int) (scale * bitmapHeight);
1321 }
1322
Michael Jurkadac85912012-05-18 15:04:49 -07001323 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1324 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001325
Michael Jurkadac85912012-05-18 15:04:49 -07001326 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001327 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001328 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1329 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001330 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001331 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1332 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1333 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1334 c.setBitmap(preview);
1335 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1336 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1337
1338 Paint p = mCachedAppWidgetPreviewPaint.get();
1339 if (p == null) {
1340 p = new Paint();
1341 p.setFilterBitmap(true);
1342 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001343 }
Michael Jurkadac85912012-05-18 15:04:49 -07001344 c.drawBitmap(defaultPreview, src, dest, p);
1345 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001346 }
Winson Chungb44b5242011-06-13 11:32:14 -07001347 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001348 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001349
Michael Jurka038f9d82011-11-03 13:50:45 -07001350 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001351 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001352
Winson Chungd2945262011-06-24 15:22:14 -07001353 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001354 final ArrayList<Object> items = new ArrayList<Object>();
1355 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1356 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001357 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001358 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1359 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001360 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001361
Winson Chunge4a647f2011-09-30 14:41:25 -07001362 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001363 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001364 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1365 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001366 }
1367
Winson Chunge4a647f2011-09-30 14:41:25 -07001368 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001369 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001370 layout.setColumnCount(layout.getCellCountX());
1371 for (int i = 0; i < items.size(); ++i) {
1372 Object rawInfo = items.get(i);
1373 PendingAddItemInfo createItemInfo = null;
1374 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1375 R.layout.apps_customize_widget, layout, false);
1376 if (rawInfo instanceof AppWidgetProviderInfo) {
1377 // Fill in the widget information
1378 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1379 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001380
1381 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001382 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001383 createItemInfo.spanX = spanXY[0];
1384 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001385 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001386 createItemInfo.minSpanX = minSpanXY[0];
1387 createItemInfo.minSpanY = minSpanXY[1];
1388
1389 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001390 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001391 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001392 } else if (rawInfo instanceof ResolveInfo) {
1393 // Fill in the shortcuts information
1394 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001395 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001396 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1397 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1398 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001399 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001400 widget.setTag(createItemInfo);
1401 }
1402 widget.setOnClickListener(this);
1403 widget.setOnLongClickListener(this);
1404 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001405 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001406
1407 // Layout each widget
1408 int ix = i % mWidgetCountX;
1409 int iy = i / mWidgetCountX;
1410 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1411 GridLayout.spec(iy, GridLayout.LEFT),
1412 GridLayout.spec(ix, GridLayout.TOP));
1413 lp.width = cellWidth;
1414 lp.height = cellHeight;
1415 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1416 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1417 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1418 layout.addView(widget, lp);
1419 }
1420
Michael Jurka038f9d82011-11-03 13:50:45 -07001421 // wait until a call on onLayout to start loading, because
1422 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1423 // TODO: can we do a measure/layout immediately?
1424 layout.setOnLayoutListener(new Runnable() {
1425 public void run() {
1426 // Load the widget previews
1427 int maxPreviewWidth = cellWidth;
1428 int maxPreviewHeight = cellHeight;
1429 if (layout.getChildCount() > 0) {
1430 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1431 int[] maxSize = w.getPreviewSize();
1432 maxPreviewWidth = maxSize[0];
1433 maxPreviewHeight = maxSize[1];
1434 }
1435 if (immediate) {
1436 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1437 maxPreviewWidth, maxPreviewHeight, null, null);
1438 loadWidgetPreviewsInBackground(null, data);
1439 onSyncWidgetPageItems(data);
1440 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001441 if (mInTransition) {
1442 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1443 } else {
1444 prepareLoadWidgetPreviewsTask(page, items,
1445 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1446 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001447 }
1448 }
1449 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001450 }
1451 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1452 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001453 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1454 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001455 if (task != null) {
1456 // Ensure that this task starts running at the correct priority
1457 task.syncThreadPriority();
1458 }
1459
1460 // Load each of the widget/shortcut previews
1461 ArrayList<Object> items = data.items;
1462 ArrayList<Bitmap> images = data.generatedImages;
1463 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001464 for (int i = 0; i < count; ++i) {
1465 if (task != null) {
1466 // Ensure we haven't been cancelled yet
1467 if (task.isCancelled()) break;
1468 // Before work on each item, ensure that this task is running at the correct
1469 // priority
1470 task.syncThreadPriority();
1471 }
1472
1473 Object rawInfo = items.get(i);
1474 if (rawInfo instanceof AppWidgetProviderInfo) {
1475 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001476 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001477
1478 int maxWidth = Math.min(data.maxImageWidth,
1479 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1480 int maxHeight = Math.min(data.maxImageHeight,
1481 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001482 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001483 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001484 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001485 } else if (rawInfo instanceof ResolveInfo) {
1486 // Fill in the shortcuts information
1487 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001488 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001489 }
1490 }
Winson Chungb44b5242011-06-13 11:32:14 -07001491 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001492
Winson Chungb44b5242011-06-13 11:32:14 -07001493 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001494 if (mInTransition) {
1495 mDeferredSyncWidgetPageItems.add(data);
1496 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001497 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001498 try {
1499 int page = data.page;
1500 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001501
Michael Jurka39e5d172012-03-12 18:36:12 -07001502 ArrayList<Object> items = data.items;
1503 int count = items.size();
1504 for (int i = 0; i < count; ++i) {
1505 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1506 if (widget != null) {
1507 Bitmap preview = data.generatedImages.get(i);
1508 widget.applyPreview(new FastBitmapDrawable(preview), i);
1509 }
1510 }
Winson Chung68e4c642011-11-10 15:48:25 -08001511
Michael Jurka39e5d172012-03-12 18:36:12 -07001512 layout.createHardwareLayer();
1513 invalidate();
1514
1515 // Update all thread priorities
1516 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1517 while (iter.hasNext()) {
1518 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1519 int pageIndex = task.page;
1520 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1521 }
1522 } finally {
1523 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001524 }
Winson Chungb44b5242011-06-13 11:32:14 -07001525 }
Winson Chung46af2e82011-05-09 16:00:53 -07001526
Winson Chung785d2eb2011-04-14 16:08:02 -07001527 @Override
1528 public void syncPages() {
1529 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001530 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001531
Adam Cohen0cd3b642011-10-14 14:58:00 -07001532 Context context = getContext();
1533 for (int j = 0; j < mNumWidgetPages; ++j) {
1534 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1535 mWidgetCountY);
1536 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001537 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001538 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001539 }
1540
Adam Cohen0cd3b642011-10-14 14:58:00 -07001541 for (int i = 0; i < mNumAppsPages; ++i) {
1542 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1543 setupPage(layout);
1544 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001545 }
1546 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001547
Winson Chung785d2eb2011-04-14 16:08:02 -07001548 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001549 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001550 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001551 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001552 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001553 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001554 }
1555 }
1556
Adam Cohen22f823d2011-09-01 17:22:18 -07001557 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1558 // it is in the z-order. This is important to insure touch events are handled correctly.
1559 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001560 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001561 }
1562
Adam Cohenae4f1552011-10-20 00:15:42 -07001563 @Override
1564 protected int indexToPage(int index) {
1565 return getChildCount() - index - 1;
1566 }
1567
Adam Cohen22f823d2011-09-01 17:22:18 -07001568 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1569 @Override
1570 protected void screenScrolled(int screenCenter) {
1571 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001572
1573 for (int i = 0; i < getChildCount(); i++) {
1574 View v = getPageAt(i);
1575 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001576 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001577
1578 float interpolatedProgress =
1579 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1580 float scale = (1 - interpolatedProgress) +
1581 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1582 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001583
Adam Cohen2591f6a2011-10-25 14:36:40 -07001584 float alpha;
1585
Winson Chungd167e2a2012-04-26 13:13:01 -07001586 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001587 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001588 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001589 } else {
1590 // On large screens we need to fade the page as it nears its leftmost position
1591 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1592 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001593
1594 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1595 int pageWidth = v.getMeasuredWidth();
1596 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001597
1598 if (PERFORM_OVERSCROLL_ROTATION) {
1599 if (i == 0 && scrollProgress < 0) {
1600 // Overscroll to the left
1601 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1602 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1603 scale = 1.0f;
1604 alpha = 1.0f;
1605 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001606 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001607 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1608 // Overscroll to the right
1609 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1610 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1611 scale = 1.0f;
1612 alpha = 1.0f;
1613 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001614 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001615 } else {
1616 v.setPivotY(pageHeight / 2.0f);
1617 v.setPivotX(pageWidth / 2.0f);
1618 v.setRotationY(0f);
1619 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001620 }
1621
1622 v.setTranslationX(translationX);
1623 v.setScaleX(scale);
1624 v.setScaleY(scale);
1625 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001626
1627 // If the view has 0 alpha, we set it to be invisible so as to prevent
1628 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001629 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001630 v.setVisibility(INVISIBLE);
1631 } else if (v.getVisibility() != VISIBLE) {
1632 v.setVisibility(VISIBLE);
1633 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001634 }
1635 }
1636 }
1637
1638 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001639 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001640 }
1641
Winson Chung785d2eb2011-04-14 16:08:02 -07001642 /**
1643 * Used by the parent to get the content width to set the tab bar to
1644 * @return
1645 */
1646 public int getPageContentWidth() {
1647 return mContentWidth;
1648 }
1649
Winson Chungb26f3d62011-06-02 10:49:29 -07001650 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001651 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001652 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001653 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001654 // We reset the save index when we change pages so that it will be recalculated on next
1655 // rotation
1656 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001657 }
1658
Winson Chung785d2eb2011-04-14 16:08:02 -07001659 /*
1660 * AllAppsView implementation
1661 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001662 public void setup(Launcher launcher, DragController dragController) {
1663 mLauncher = launcher;
1664 mDragController = dragController;
1665 }
Winson Chung9802ac92012-06-08 16:01:58 -07001666
1667 /**
1668 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1669 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1670 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1671 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1672 */
1673 private void invalidateOnDataChange() {
1674 if (!isDataReady()) {
1675 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1676 // request a layout to trigger the page data when ready.
1677 requestLayout();
1678 } else {
1679 cancelAllTasks();
1680 invalidatePageData();
1681 }
1682 }
1683
Winson Chung785d2eb2011-04-14 16:08:02 -07001684 public void setApps(ArrayList<ApplicationInfo> list) {
1685 mApps = list;
1686 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001687 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001688 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001689 }
1690 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1691 // We add it in place, in alphabetical order
1692 int count = list.size();
1693 for (int i = 0; i < count; ++i) {
1694 ApplicationInfo info = list.get(i);
1695 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1696 if (index < 0) {
1697 mApps.add(-(index + 1), info);
1698 }
1699 }
1700 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001701 public void addApps(ArrayList<ApplicationInfo> list) {
1702 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001703 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001704 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001705 }
1706 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1707 ComponentName removeComponent = item.intent.getComponent();
1708 int length = list.size();
1709 for (int i = 0; i < length; ++i) {
1710 ApplicationInfo info = list.get(i);
1711 if (info.intent.getComponent().equals(removeComponent)) {
1712 return i;
1713 }
1714 }
1715 return -1;
1716 }
Winson Chungcd810732012-06-18 16:45:43 -07001717 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1718 int length = list.size();
1719 for (int i = 0; i < length; ++i) {
1720 ApplicationInfo info = list.get(i);
1721 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1722 return i;
1723 }
1724 }
1725 return -1;
1726 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001727 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1728 // loop through all the apps and remove apps that have the same component
1729 int length = list.size();
1730 for (int i = 0; i < length; ++i) {
1731 ApplicationInfo info = list.get(i);
1732 int removeIndex = findAppByComponent(mApps, info);
1733 if (removeIndex > -1) {
1734 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001735 }
1736 }
1737 }
Winson Chungcd810732012-06-18 16:45:43 -07001738 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1739 // loop through all the package names and remove apps that have the same package name
1740 for (String pn : packageNames) {
1741 int removeIndex = findAppByPackage(mApps, pn);
1742 while (removeIndex > -1) {
1743 mApps.remove(removeIndex);
1744 removeIndex = findAppByPackage(mApps, pn);
1745 }
1746 }
1747 }
1748 public void removeApps(ArrayList<String> packageNames) {
1749 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001750 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001751 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001752 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001753 public void updateApps(ArrayList<ApplicationInfo> list) {
1754 // We remove and re-add the updated applications list because it's properties may have
1755 // changed (ie. the title), and this will ensure that the items will be in their proper
1756 // place in the list.
1757 removeAppsWithoutInvalidate(list);
1758 addAppsWithoutInvalidate(list);
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 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001762
Winson Chung785d2eb2011-04-14 16:08:02 -07001763 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001764 // If we have reset, then we should not continue to restore the previous state
1765 mSaveInstanceStateItemIndex = -1;
1766
Adam Cohenb64d36e2011-10-17 21:48:02 -07001767 AppsCustomizeTabHost tabHost = getTabHost();
1768 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001769 if (tag != null) {
1770 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1771 tabHost.setCurrentTabFromContent(ContentType.Applications);
1772 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001773 }
Winson Chung649668f2012-01-10 13:07:16 -08001774
Adam Cohenb64d36e2011-10-17 21:48:02 -07001775 if (mCurrentPage != 0) {
1776 invalidatePageData(0);
1777 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001778 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001779
1780 private AppsCustomizeTabHost getTabHost() {
1781 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1782 }
1783
Winson Chung785d2eb2011-04-14 16:08:02 -07001784 public void dumpState() {
1785 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001786 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1787 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001788 }
Adam Cohen4e844012011-11-09 13:48:04 -08001789
Winson Chung785d2eb2011-04-14 16:08:02 -07001790 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001791 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001792 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001793 for (Object i: list) {
1794 if (i instanceof AppWidgetProviderInfo) {
1795 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1796 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1797 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1798 + " initialLayout=" + info.initialLayout
1799 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1800 } else if (i instanceof ResolveInfo) {
1801 ResolveInfo info = (ResolveInfo) i;
1802 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1803 + info.icon);
1804 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001805 }
1806 }
Adam Cohen4e844012011-11-09 13:48:04 -08001807
Winson Chung785d2eb2011-04-14 16:08:02 -07001808 public void surrender() {
1809 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1810 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001811
1812 // Stop all background tasks
1813 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001814 }
Winson Chung007c6982011-06-14 13:27:53 -07001815
Winson Chunge4e50662012-01-23 14:45:13 -08001816 @Override
1817 public void iconPressed(PagedViewIcon icon) {
1818 // Reset the previously pressed icon and store a reference to the pressed icon so that
1819 // we can reset it on return to Launcher (in Launcher.onResume())
1820 if (mPressedIcon != null) {
1821 mPressedIcon.resetDrawableState();
1822 }
1823 mPressedIcon = icon;
1824 }
1825
1826 public void resetDrawableState() {
1827 if (mPressedIcon != null) {
1828 mPressedIcon.resetDrawableState();
1829 mPressedIcon = null;
1830 }
1831 }
Winson Chung68e4c642011-11-10 15:48:25 -08001832
Winson Chungb44b5242011-06-13 11:32:14 -07001833 /*
1834 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1835 * widget previews in the background with the AsyncTasks.
1836 */
Winson Chung68e4c642011-11-10 15:48:25 -08001837 final static int sLookBehindPageCount = 2;
1838 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001839 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001840 final int count = getChildCount();
1841 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1842 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1843 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001844 }
1845 protected int getAssociatedUpperPageBound(int page) {
1846 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001847 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1848 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1849 count - 1);
1850 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001851 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001852
1853 @Override
1854 protected String getCurrentPageDescription() {
1855 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1856 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001857 int count = 0;
1858
Adam Cohen0cd3b642011-10-14 14:58:00 -07001859 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001860 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001861 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001862 } else {
1863 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001864 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001865 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001866 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001867
Michael Jurka8b805b12012-04-18 14:23:14 -07001868 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001869 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001870}