blob: 45bb08ac9def067bc36ad2a39fcff6660e5ab8d0 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070036import android.graphics.ColorMatrix;
37import android.graphics.ColorMatrixColorFilter;
38import android.graphics.Insets;
Peter Ng8db70002011-10-25 15:40:08 -070039import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080040import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070041import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070042import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080044import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070045import android.graphics.Shader;
Peter Ng8db70002011-10-25 15:40:08 -070046import android.graphics.TableMaskFilter;
Michael Jurkadac85912012-05-18 15:04:49 -070047import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070049import android.os.AsyncTask;
50import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.util.AttributeSet;
52import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070053import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080054import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070055import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070056import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070057import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070058import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070059import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070060import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070061import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070062import android.widget.ImageView;
Michael Jurkadac85912012-05-18 15:04:49 -070063import android.widget.LinearLayout;
Winson Chung55b65502011-05-26 12:03:43 -070064import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070065
66import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070067import com.android.launcher2.DropTarget.DragObject;
68
69import java.util.ArrayList;
70import java.util.Collections;
71import java.util.Iterator;
72import java.util.List;
Michael Jurkadac85912012-05-18 15:04:49 -070073import java.lang.ref.WeakReference;
Winson Chung785d2eb2011-04-14 16:08:02 -070074
Winson Chungb44b5242011-06-13 11:32:14 -070075/**
76 * A simple callback interface which also provides the results of the task.
77 */
78interface AsyncTaskCallback {
79 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
80}
Winson Chung4e076542011-06-23 13:04:10 -070081
Winson Chungb44b5242011-06-13 11:32:14 -070082/**
83 * The data needed to perform either of the custom AsyncTasks.
84 */
85class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070086 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080087 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070088 }
89
Winson Chungb44b5242011-06-13 11:32:14 -070090 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
91 AsyncTaskCallback postR) {
92 page = p;
93 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070094 sourceImages = si;
95 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070096 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070097 doInBackgroundCallback = bgR;
98 postExecuteCallback = postR;
99 }
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -0700101 AsyncTaskCallback postR) {
102 page = p;
103 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700104 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700105 maxImageWidth = cw;
106 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700107 doInBackgroundCallback = bgR;
108 postExecuteCallback = postR;
109 }
Winson Chung09945932011-09-20 14:22:40 -0700110 void cleanup(boolean cancelled) {
111 // Clean up any references to source/generated bitmaps
112 if (sourceImages != null) {
113 if (cancelled) {
114 for (Bitmap b : sourceImages) {
115 b.recycle();
116 }
117 }
118 sourceImages.clear();
119 }
120 if (generatedImages != null) {
121 if (cancelled) {
122 for (Bitmap b : generatedImages) {
123 b.recycle();
124 }
125 }
126 generatedImages.clear();
127 }
128 }
Winson Chungb44b5242011-06-13 11:32:14 -0700129 int page;
130 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700131 ArrayList<Bitmap> sourceImages;
132 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700133 int maxImageWidth;
134 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700135 AsyncTaskCallback doInBackgroundCallback;
136 AsyncTaskCallback postExecuteCallback;
137}
Winson Chung4e076542011-06-23 13:04:10 -0700138
Winson Chungb44b5242011-06-13 11:32:14 -0700139/**
140 * A generic template for an async task used in AppsCustomize.
141 */
142class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700143 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700144 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700145 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700146 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700147 }
148 @Override
149 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
150 if (params.length != 1) return null;
151 // Load each of the widget previews in the background
152 params[0].doInBackgroundCallback.run(this, params[0]);
153 return params[0];
154 }
155 @Override
156 protected void onPostExecute(AsyncTaskPageData result) {
157 // All the widget previews are loaded, so we can just callback to inflate the page
158 result.postExecuteCallback.run(this, result);
159 }
160
161 void setThreadPriority(int p) {
162 threadPriority = p;
163 }
164 void syncThreadPriority() {
165 Process.setThreadPriority(threadPriority);
166 }
167
168 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700169 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700170 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700171 int threadPriority;
172}
Winson Chungb44b5242011-06-13 11:32:14 -0700173
Michael Jurkadac85912012-05-18 15:04:49 -0700174abstract class WeakReferenceThreadLocal<T> {
175 private ThreadLocal<WeakReference<T>> mThreadLocal;
176 public WeakReferenceThreadLocal() {
177 mThreadLocal = new ThreadLocal<WeakReference<T>>();
178 }
179
180 abstract T initialValue();
181
182 public void set(T t) {
183 mThreadLocal.set(new WeakReference<T>(t));
184 }
185
186 public T get() {
187 WeakReference<T> reference = mThreadLocal.get();
188 T obj;
189 if (reference == null) {
190 obj = initialValue();
191 mThreadLocal.set(new WeakReference<T>(obj));
192 return obj;
193 } else {
194 obj = reference.get();
195 if (obj == null) {
196 obj = initialValue();
197 mThreadLocal.set(new WeakReference<T>(obj));
198 }
199 return obj;
200 }
201 }
202}
203
204class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
205 @Override
206 protected Canvas initialValue() {
207 return new Canvas();
208 }
209}
210
211class PaintCache extends WeakReferenceThreadLocal<Paint> {
212 @Override
213 protected Paint initialValue() {
214 return null;
215 }
216}
217
218class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
219 @Override
220 protected Bitmap initialValue() {
221 return null;
222 }
223}
224
225class RectCache extends WeakReferenceThreadLocal<Rect> {
226 @Override
227 protected Rect initialValue() {
228 return new Rect();
229 }
230}
231
Winson Chungb44b5242011-06-13 11:32:14 -0700232/**
233 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
234 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700235public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800236 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700237 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
238 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700239 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700240
241 /**
242 * The different content types that this paged view can show.
243 */
244 public enum ContentType {
245 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700246 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700247 }
248
249 // Refs
250 private Launcher mLauncher;
251 private DragController mDragController;
252 private final LayoutInflater mLayoutInflater;
253 private final PackageManager mPackageManager;
254
Winson Chung5afbf7b2011-07-25 11:53:08 -0700255 // Save and Restore
256 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800257 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700258
Winson Chung785d2eb2011-04-14 16:08:02 -0700259 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700260 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700261 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700262
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800264 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700265 private int mClingFocusedX;
266 private int mClingFocusedY;
267
Winson Chung1ed747a2011-05-03 16:18:34 -0700268 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700269 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700270 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700271 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700272
273 // Dimens
274 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700275 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800276 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700277 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700278 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700279 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700281 private int mNumAppsPages;
282 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700283
Adam Cohen22f823d2011-09-01 17:22:18 -0700284 // Relating to the scroll and overscroll effects
285 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700286 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700288 private static float TRANSITION_PIVOT = 0.65f;
289 private static float TRANSITION_MAX_ROTATION = 22;
290 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700291 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700292 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700293
Winson Chungb44b5242011-06-13 11:32:14 -0700294 // Previews & outlines
295 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800296 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700297
Adam Cohened66b2b2012-01-23 17:28:51 -0800298 private Runnable mInflateWidgetRunnable = null;
299 private Runnable mBindWidgetRunnable = null;
300 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700301 static final int WIDGET_PRELOAD_PENDING = 0;
302 static final int WIDGET_BOUND = 1;
303 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800304 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
305 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800306 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800307 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800308
Michael Jurka39e5d172012-03-12 18:36:12 -0700309 // Deferral of loading widget previews during launcher transitions
310 private boolean mInTransition;
311 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
312 new ArrayList<AsyncTaskPageData>();
313
Michael Jurkadac85912012-05-18 15:04:49 -0700314 // Used for drawing shortcut previews
315 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
316 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
317 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
318
319 // Used for drawing widget previews
320 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
321 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
322 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
323 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
324
Winson Chung785d2eb2011-04-14 16:08:02 -0700325 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
326 super(context, attrs);
327 mLayoutInflater = LayoutInflater.from(context);
328 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700329 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700330 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700331 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700332 mCanvas = new Canvas();
333 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700334
335 // Save the default widget preview background
336 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700337 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700338 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700339
Winson Chung6032e7e2011-11-08 15:47:17 -0800340 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
341 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
342 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700343 mWidgetWidthGap =
344 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
345 mWidgetHeightGap =
346 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700347 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
348 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700349 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
350 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700351 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700352 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700353
Winson Chung1ed747a2011-05-03 16:18:34 -0700354 // The padding on the non-matched dimension for the default widget preview icons
355 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700356 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700357
358 // Unless otherwise specified this view is important for accessibility.
359 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
360 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
361 }
Winson Chung7c7a22d2012-06-01 13:46:48 -0700362
363 Log.d(TAG, "6549598 ctor mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700364 }
365
366 @Override
367 protected void init() {
368 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700369 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700370
371 Context context = getContext();
372 Resources r = context.getResources();
373 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
Winson Chung7c7a22d2012-06-01 13:46:48 -0700374 Log.d(TAG, "6549598 init mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700375 }
376
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700378 protected void onUnhandledTap(MotionEvent ev) {
379 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700380 // Dismiss AppsCustomize if we tap
381 mLauncher.showWorkspace(true);
382 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700383 }
384
Winson Chung5afbf7b2011-07-25 11:53:08 -0700385 /** Returns the item index of the center item on this page so that we can restore to this
386 * item index when we rotate. */
387 private int getMiddleComponentIndexOnCurrentPage() {
388 int i = -1;
389 if (getPageCount() > 0) {
390 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700391 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700392 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700393 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
394 int numItemsPerPage = mCellCountX * mCellCountY;
395 int childCount = childrenLayout.getChildCount();
396 if (childCount > 0) {
397 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700398 }
399 } else {
400 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700401 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700402 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
403 int childCount = layout.getChildCount();
404 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700405 i = numApps +
406 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
407 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700408 }
409 }
410 return i;
411 }
412
413 /** Get the index of the item to restore to if we need to restore the current page. */
414 int getSaveInstanceStateIndex() {
415 if (mSaveInstanceStateItemIndex == -1) {
416 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
417 }
418 return mSaveInstanceStateItemIndex;
419 }
420
421 /** Returns the page in the current orientation which is expected to contain the specified
422 * item index. */
423 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700424 if (index < 0) return 0;
425
426 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700427 int numItemsPerPage = mCellCountX * mCellCountY;
428 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700429 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700430 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700431 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
432 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700433 }
434
Winson Chungf0ea4d32011-06-06 14:27:16 -0700435 /**
436 * This differs from isDataReady as this is the test done if isDataReady is not set.
437 */
438 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700439 // We only do this test once, and we default to the Applications page, so we only really
440 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700441 // TODO: What if one of them is validly empty
442 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
444
Winson Chung5afbf7b2011-07-25 11:53:08 -0700445 /** Restores the page for an item at the specified index */
446 void restorePageForIndex(int index) {
447 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700448 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700449 }
450
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700451 private void updatePageCounts() {
452 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
453 (float) (mWidgetCountX * mWidgetCountY));
454 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
Winson Chung7c7a22d2012-06-01 13:46:48 -0700455 Log.d(TAG, "6549598 updatePageCounts mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
456 Log.d(TAG, "6549598 mApps.size(): " + mApps.size() + " mWidgets.size(): " + mWidgets.size() + " mCellCountX: " + mCellCountX + " mCellCountY: " + mCellCountY);
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700457 }
458
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 protected void onDataReady(int width, int height) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700460 Log.d(TAG, "6549598 onDataReady");
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 // Note that we transpose the counts in portrait so that we get a similar layout
462 boolean isLandscape = getResources().getConfiguration().orientation ==
463 Configuration.ORIENTATION_LANDSCAPE;
464 int maxCellCountX = Integer.MAX_VALUE;
465 int maxCellCountY = Integer.MAX_VALUE;
466 if (LauncherApplication.isScreenLarge()) {
467 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
468 LauncherModel.getCellCountY());
469 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
470 LauncherModel.getCellCountX());
471 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800472 if (mMaxAppCellCountX > -1) {
473 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
474 }
475 if (mMaxAppCellCountY > -1) {
476 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
477 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700478
479 // Now that the data is ready, we can calculate the content width, the number of cells to
480 // use for each page
481 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
482 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
483 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
484 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
485 mCellCountX = mWidgetSpacingLayout.getCellCountX();
486 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700487 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700488
Winson Chungdb1138b2011-06-30 14:39:35 -0700489 // Force a measure to update recalculate the gaps
490 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
491 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
492 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700493 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700494
Michael Jurkae326f182011-11-21 14:05:46 -0800495 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
496 final boolean hostIsTransitioning = host.isTransitioning();
497
Adam Cohen0cd3b642011-10-14 14:58:00 -0700498 // Restore the page
499 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800500 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700501
Winson Chung3f4e1422011-11-17 14:58:51 -0800502 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
503 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
504 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800505 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800506 post(new Runnable() {
507 @Override
508 public void run() {
509 showAllAppsCling();
510 }
511 });
512 }
513 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700514
Winson Chung3f4e1422011-11-17 14:58:51 -0800515 void showAllAppsCling() {
516 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
517 mHasShownAllAppsCling = true;
518 // Calculate the position for the cling punch through
519 int[] offset = new int[2];
520 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
521 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
522 // PagedViews are centered horizontally but top aligned
523 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
524 offset[0];
525 pos[1] += offset[1];
526 mLauncher.showFirstRunAllAppsCling(pos);
527 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700528 }
529
530 @Override
531 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
532 int width = MeasureSpec.getSize(widthMeasureSpec);
533 int height = MeasureSpec.getSize(heightMeasureSpec);
534 if (!isDataReady()) {
535 if (testDataReady()) {
536 setDataIsReady();
537 setMeasuredDimension(width, height);
538 onDataReady(width, height);
539 }
540 }
541
542 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
543 }
544
Winson Chung785d2eb2011-04-14 16:08:02 -0700545 public void onPackagesUpdated() {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700546 Log.d(TAG, "6549598 onPackagesUpdated");
Adam Cohenb0fa3522011-08-17 16:10:46 -0700547 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
548 // by a broadcast receiver, and in order for it to work correctly, we need to know that
549 // the AppWidgetService has already received and processed the same broadcast. Since there
Winson Chungfd39d8e2012-06-05 10:12:48 -0700550 // is no guarantee about ordering of broadcast receipt, we just delay here. This is a
551 // workaround until we add a callback from AppWidgetService to AppWidgetHost when widget
552 // packages are added, updated or removed.
Adam Cohenb0fa3522011-08-17 16:10:46 -0700553 postDelayed(new Runnable() {
554 public void run() {
555 updatePackages();
556 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700557 }, 1500);
Adam Cohenb0fa3522011-08-17 16:10:46 -0700558 }
559
560 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700561 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700562 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700563 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700564 List<AppWidgetProviderInfo> widgets =
565 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700566 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
567 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800568 for (AppWidgetProviderInfo widget : widgets) {
569 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700570 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700571 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
572 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700573 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
574 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700575 if (minSpanX <= LauncherModel.getCellCountX() &&
576 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700577 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700578 } else {
579 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
580 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700581 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800582 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700583 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800584 widget.minWidth + ", " + widget.minHeight + ")");
585 }
586 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700587 mWidgets.addAll(shortcuts);
588 Collections.sort(mWidgets,
589 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung7c7a22d2012-06-01 13:46:48 -0700590 Log.d(TAG, "6549598 updatePackages mWidgets.size(): " + mWidgets.size() + " wasEmpty: " + wasEmpty);
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700591 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700592
Winson Chung875de7e2011-06-28 14:25:17 -0700593 if (wasEmpty) {
594 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
595 // a layout to do this test and invalidate the page data when ready.
596 if (testDataReady()) requestLayout();
597 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700598 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700599 invalidatePageData();
600 }
Winson Chung4b576be2011-04-27 17:40:20 -0700601 }
602
603 @Override
604 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700605 // When we have exited all apps or are in transition, disregard clicks
606 if (!mLauncher.isAllAppsCustomizeOpen() ||
607 mLauncher.getWorkspace().isSwitchingState()) return;
608
Winson Chung4b576be2011-04-27 17:40:20 -0700609 if (v instanceof PagedViewIcon) {
610 // Animate some feedback to the click
611 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800612
613 // Lock the drawable state to pressed until we return to Launcher
614 if (mPressedIcon != null) {
615 mPressedIcon.lockDrawableState();
616 }
Winson Chungc7450e32012-04-17 17:34:08 -0700617
Winson Chung18f41f82012-05-09 13:28:10 -0700618 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
619 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
620 // when Launcher resumes and we are still in AllApps.
621 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700622 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
623
Winson Chung4b576be2011-04-27 17:40:20 -0700624 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700625 // Let the user know that they have to long press to add a widget
626 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
627 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700628
Winson Chungd2e87b32011-06-02 10:53:07 -0700629 // Create a little animation to show that the widget can move
630 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
631 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
632 AnimatorSet bounce = new AnimatorSet();
633 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
634 tyuAnim.setDuration(125);
635 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
636 tydAnim.setDuration(100);
637 bounce.play(tyuAnim).before(tydAnim);
638 bounce.setInterpolator(new AccelerateInterpolator());
639 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700640 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 }
642
Winson Chungc6f10b92011-11-14 11:39:07 -0800643 public boolean onKey(View v, int keyCode, KeyEvent event) {
644 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
645 }
646
Winson Chung785d2eb2011-04-14 16:08:02 -0700647 /*
648 * PagedViewWithDraggableItems implementation
649 */
650 @Override
651 protected void determineDraggingStart(android.view.MotionEvent ev) {
652 // Disable dragging by pulling an app down for now.
653 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700654
Winson Chung4b576be2011-04-27 17:40:20 -0700655 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700656 mLauncher.getWorkspace().onDragStartedWithItem(v);
657 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700658 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700659
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700660 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700661 Log.d(TAG, "6557954 Preload widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 final AppWidgetProviderInfo pInfo = info.info;
663 if (pInfo.configure != null) {
664 return;
665 }
666
Adam Cohen21a170b2012-05-30 15:17:06 -0700667 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 mBindWidgetRunnable = new Runnable() {
669 @Override
670 public void run() {
Adam Cohen68f681b2012-05-28 15:01:16 -0700671 Log.d(TAG, " 6557954 Preload, bind widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 if (AppWidgetManager.getInstance(mLauncher)
674 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
675 mWidgetCleanupState = WIDGET_BOUND;
676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 }
678 };
679 post(mBindWidgetRunnable);
680
681 mInflateWidgetRunnable = new Runnable() {
682 @Override
683 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700684 AppWidgetHostView hostView = mLauncher.
685 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 info.boundWidget = hostView;
Adam Cohen68f681b2012-05-28 15:01:16 -0700687 Log.d(TAG, " 6557954 Preload, inflate widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800689 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700690 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
691 info.spanY, info, false);
692
693 // We want the first widget layout to be the correct size. This will be important
694 // for width size reporting to the AppWidgetManager.
695 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
696 unScaledSize[1]);
697 lp.x = lp.y = 0;
698 lp.customPosition = true;
699 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800700 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800701 }
702 };
703 post(mInflateWidgetRunnable);
704 }
705
706 @Override
707 public void onShortPress(View v) {
708 // We are anticipating a long press, and we use this time to load bind and instantiate
709 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700710 if (mCreateWidgetInfo != null) {
711 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
Adam Cohen68f681b2012-05-28 15:01:16 -0700712 Log.d(TAG, "**** 6557954 Previous shortpress not cleaned up, cleaning up now: " + mCreateWidgetInfo.info);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700713 cleanupWidgetPreloading(false);
714 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800715 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohen68f681b2012-05-28 15:01:16 -0700716 Log.d(TAG, "6557954 Short press triggered for view: " + v + ", widget info: " + mCreateWidgetInfo.info);
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700717 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 }
719
Adam Cohen0e56cc92012-05-11 15:57:05 -0700720 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700721 Log.d(TAG, "6557954 Cleaning up widget, was added: " + widgetWasAdded);
722 if (mCreateWidgetInfo != null) {
723 Log.d(TAG, " 6557954 Cleaning up widget, widget info: " + mCreateWidgetInfo.info);
724 }
725
Adam Cohen0e56cc92012-05-11 15:57:05 -0700726 if (!widgetWasAdded) {
727 // If the widget was not added, we may need to do further cleanup.
728 PendingAddWidgetInfo info = mCreateWidgetInfo;
729 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700730
731 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
732 Log.d(TAG, " 6557954 Cleaning up widget, remove preload callbacks");
733 // We never did any preloading, so just remove pending callbacks to do so
734 removeCallbacks(mBindWidgetRunnable);
735 removeCallbacks(mInflateWidgetRunnable);
736 } else if (mWidgetCleanupState == WIDGET_BOUND) {
737 // Delete the widget id which was allocated
738 if (mWidgetLoadingId != -1) {
739 Log.d(TAG, " 6557954 Cleaning up widget, delete widget id");
740 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
741 }
742
743 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen68f681b2012-05-28 15:01:16 -0700744 Log.d(TAG, " 6557954 Cleaning up widget, remove callbacks");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700745 removeCallbacks(mInflateWidgetRunnable);
746 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700747 // Delete the widget id which was allocated
748 if (mWidgetLoadingId != -1) {
749 Log.d(TAG, " 6557954 Cleaning up widget, delete widget id");
750 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
751 }
752
Adam Cohen0e56cc92012-05-11 15:57:05 -0700753 // The widget was inflated and added to the DragLayer -- remove it.
Adam Cohen68f681b2012-05-28 15:01:16 -0700754 Log.d(TAG, " 6557954 Cleaning up widget, remove inflated widget from draglayer");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700755 AppWidgetHostView widget = info.boundWidget;
756 mLauncher.getDragLayer().removeView(widget);
757 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 }
759 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
760 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700761 mCreateWidgetInfo = null;
762 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 }
764
Adam Cohen7a326642012-02-22 12:03:22 -0800765 @Override
766 public void cleanUpShortPress(View v) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700767 Log.d(TAG, "6557954 Cleanup shortpress");
Adam Cohen7a326642012-02-22 12:03:22 -0800768 if (!mDraggingWidget) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700769 Log.d(TAG, " 6557954 Cleanup shortpress, cleanup cleanup preloading");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700770 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800771 }
772 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800773
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700774 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800775 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700776 // Get the widget preview as the drag representation
777 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700778 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700779
Adam Cohen68f681b2012-05-28 15:01:16 -0700780 if (createItemInfo instanceof PendingAddWidgetInfo) {
781 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
782 Log.d(TAG, "6557954 Begin dragging widget, view: " + v + ", widget info: " + createWidgetInfo.info);
783 }
784
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700785 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
786 // we abort the drag.
787 if (image.getDrawable() == null) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700788 Log.d(TAG, " 6557954 Begin dragging widget, abort, no drawable set");
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700789 mDraggingWidget = false;
790 return false;
791 }
792
Winson Chung4b576be2011-04-27 17:40:20 -0700793 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800794 Bitmap preview;
795 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800796 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700797 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700798 // This can happen in some weird cases involving multi-touch. We can't start dragging
799 // the widget if this is null, so we break out.
800 if (mCreateWidgetInfo == null) {
801 return false;
802 }
803
Adam Cohen1b36dc32012-02-13 19:27:37 -0800804 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
805 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700806 int spanX = createItemInfo.spanX;
807 int spanY = createItemInfo.spanY;
808 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
809 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700810
Winson Chung72d59842012-02-22 13:51:36 -0800811 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
812 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700813 int maxWidth, maxHeight;
814 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
815 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800816 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700817 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800818
819 // Determine the image view drawable scale relative to the preview
820 float[] mv = new float[9];
821 Matrix m = new Matrix();
822 m.setRectToRect(
823 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
824 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
825 (float) previewDrawable.getIntrinsicHeight()),
826 Matrix.ScaleToFit.START);
827 m.getValues(mv);
828 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700829 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700830 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
831 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
832 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
833 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
834
Winson Chung1120e032011-11-22 16:11:31 -0800835 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700836 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700837 renderDrawableToBitmap(icon, preview, 0, 0,
838 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700839 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700840 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700841 createItemInfo.spanX = createItemInfo.spanY = 1;
842 }
Winson Chung4b576be2011-04-27 17:40:20 -0700843
Peter Ng8db70002011-10-25 15:40:08 -0700844 // We use a custom alpha clip table for the default widget previews
845 Paint alphaClipPaint = null;
846 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700847 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700848 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
849 alphaClipPaint = new Paint();
850 alphaClipPaint.setMaskFilter(alphaClipTable);
851 }
852 }
853
Winson Chung1120e032011-11-22 16:11:31 -0800854 // Save the preview for the outline generation, then dim the preview
855 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
856 false);
Winson Chung1120e032011-11-22 16:11:31 -0800857
Winson Chung4b576be2011-04-27 17:40:20 -0700858 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800859 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700860 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800861 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
862 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800863 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800864 outline.recycle();
865 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700866 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700867 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800868
Winson Chung4b576be2011-04-27 17:40:20 -0700869 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700871 if (!super.beginDragging(v)) return false;
872
873 if (v instanceof PagedViewIcon) {
874 beginDraggingApplication(v);
875 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700876 if (!beginDraggingWidget(v)) {
877 return false;
878 }
Winson Chung4b576be2011-04-27 17:40:20 -0700879 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800880
881 // We delay entering spring-loaded mode slightly to make sure the UI
882 // thready is free of any work.
883 postDelayed(new Runnable() {
884 @Override
885 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800886 // We don't enter spring-loaded mode if the drag has been cancelled
887 if (mLauncher.getDragController().isDragging()) {
888 // Dismiss the cling
889 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800890
Adam Cohen1b36dc32012-02-13 19:27:37 -0800891 // Reset the alpha on the dragged icon before we drag
892 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800893
Adam Cohen1b36dc32012-02-13 19:27:37 -0800894 // Go into spring loaded mode (must happen before we startDrag())
895 mLauncher.enterSpringLoadedDragMode();
896 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
Winson Chung72d59842012-02-22 13:51:36 -0800898 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800899
Winson Chung785d2eb2011-04-14 16:08:02 -0700900 return true;
901 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800902
Winson Chunga48487a2012-03-20 16:19:37 -0700903 /**
904 * Clean up after dragging.
905 *
906 * @param target where the item was dragged to (can be null if the item was flung)
907 */
908 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700909 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700910 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700911 // Exit spring loaded mode if we have not successfully dropped or have not handled the
912 // drop in Workspace
913 mLauncher.exitSpringLoadedDragMode();
914 }
Winson Chung4b919f82012-05-01 10:44:08 -0700915 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700916 }
917
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700919 public View getContent() {
920 return null;
921 }
922
923 @Override
924 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700925 mInTransition = true;
926 if (toWorkspace) {
927 cancelAllTasks();
928 }
929 }
930
931 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700932 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700933 }
934
935 @Override
936 public void onLauncherTransitionStep(Launcher l, float t) {
937 }
938
939 @Override
940 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700941 Log.d(TAG, "6549598 onLauncherTransitionEnd");
Michael Jurka39e5d172012-03-12 18:36:12 -0700942 mInTransition = false;
943 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
944 onSyncWidgetPageItems(d);
945 }
946 mDeferredSyncWidgetPageItems.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700947 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700948 }
949
950 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700951 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
952 boolean success) {
953 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
954 if (isFlingToDelete) return;
955
956 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700957
958 // Display an error message if the drag failed due to there not being enough space on the
959 // target layout we were dropping on.
960 if (!success) {
961 boolean showOutOfSpaceMessage = false;
962 if (target instanceof Workspace) {
963 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
964 Workspace workspace = (Workspace) target;
965 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700966 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700967 if (layout != null) {
968 layout.calculateSpans(itemInfo);
969 showOutOfSpaceMessage =
970 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
971 }
972 }
Winson Chungfc79c802011-05-02 13:35:34 -0700973 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700974 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700975 }
Adam Cohen7a326642012-02-22 12:03:22 -0800976
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800977 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700978 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700979 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800980 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700981 }
982
Winson Chunga48487a2012-03-20 16:19:37 -0700983 @Override
984 public void onFlingToDeleteCompleted() {
985 // We just dismiss the drag when we fling, so cleanup here
986 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700987 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700988 mDraggingWidget = false;
989 }
990
991 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800992 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700993 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800994 }
995
Winson Chung7f0acdd2011-09-19 18:34:19 -0700996 @Override
997 protected void onDetachedFromWindow() {
998 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700999 cancelAllTasks();
1000 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001001
Michael Jurkae326f182011-11-21 14:05:46 -08001002 public void clearAllWidgetPages() {
1003 cancelAllTasks();
1004 int count = getChildCount();
1005 for (int i = 0; i < count; i++) {
1006 View v = getPageAt(i);
1007 if (v instanceof PagedViewGridLayout) {
1008 ((PagedViewGridLayout) v).removeAllViewsOnPage();
1009 mDirtyPageContent.set(i, true);
1010 }
1011 }
1012 }
1013
Adam Cohen0cd3b642011-10-14 14:58:00 -07001014 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -07001015 // Clean up all the async tasks
1016 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1017 while (iter.hasNext()) {
1018 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1019 task.cancel(false);
1020 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -07001021 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -07001022
1023 // We've already preallocated the views for the data to load into, so clear them as well
1024 View v = getPageAt(task.page);
1025 if (v instanceof PagedViewGridLayout) {
1026 ((PagedViewGridLayout) v).removeAllViewsOnPage();
1027 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001028 }
Winson Chung83687b12012-04-25 16:01:01 -07001029 mDeferredSyncWidgetPageItems.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -07001030 }
1031
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 public void setContentType(ContentType type) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001033 Log.d(TAG, "6549598 setContentType mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001034 if (type == ContentType.Widgets) {
1035 invalidatePageData(mNumAppsPages, true);
1036 } else if (type == ContentType.Applications) {
1037 invalidatePageData(0, true);
1038 }
Winson Chungb44b5242011-06-13 11:32:14 -07001039 }
1040
Adam Cohen0cd3b642011-10-14 14:58:00 -07001041 protected void snapToPage(int whichPage, int delta, int duration) {
1042 super.snapToPage(whichPage, delta, duration);
1043 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001044
1045 // Update the thread priorities given the direction lookahead
1046 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1047 while (iter.hasNext()) {
1048 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001049 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001050 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1051 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1052 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1053 } else {
1054 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1055 }
1056 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001057 }
1058
1059 private void updateCurrentTab(int currentPage) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001060 Log.d(TAG, "6549598 updateCurrentTab mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001061 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001062 if (tabHost != null) {
1063 String tag = tabHost.getCurrentTabTag();
1064 if (tag != null) {
1065 if (currentPage >= mNumAppsPages &&
1066 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1067 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1068 } else if (currentPage < mNumAppsPages &&
1069 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1070 tabHost.setCurrentTabFromContent(ContentType.Applications);
1071 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001072 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001073 }
1074 }
1075
Winson Chung785d2eb2011-04-14 16:08:02 -07001076 /*
1077 * Apps PagedView implementation
1078 */
Winson Chung63257c12011-05-05 17:06:13 -07001079 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1080 int childCount = layout.getChildCount();
1081 for (int i = 0; i < childCount; ++i) {
1082 layout.getChildAt(i).setVisibility(visibility);
1083 }
1084 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001085 private void setupPage(PagedViewCellLayout layout) {
1086 layout.setCellCount(mCellCountX, mCellCountY);
1087 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1088 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1089 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1090
Winson Chung63257c12011-05-05 17:06:13 -07001091 // Note: We force a measure here to get around the fact that when we do layout calculations
1092 // immediately after syncing, we don't have a proper width. That said, we already know the
1093 // expected page width, so we can actually optimize by hiding all the TextView-based
1094 // children that are expensive to measure, and let that happen naturally later.
1095 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001096 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001097 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001098 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001099 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001100 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001101 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001102
Winson Chungf314b0e2011-08-16 11:54:27 -07001103 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001104 Log.d(TAG, "6549598 syncAppsPageItems page: " + page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001105 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001106 int numCells = mCellCountX * mCellCountY;
1107 int startIndex = page * numCells;
1108 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001109 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001110
Winson Chung785d2eb2011-04-14 16:08:02 -07001111 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001112 ArrayList<Object> items = new ArrayList<Object>();
1113 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 for (int i = startIndex; i < endIndex; ++i) {
1115 ApplicationInfo info = mApps.get(i);
1116 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1117 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001118 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001119 icon.setOnClickListener(this);
1120 icon.setOnLongClickListener(this);
1121 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001122 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001123
1124 int index = i - startIndex;
1125 int x = index % mCellCountX;
1126 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001127 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001128
1129 items.add(info);
1130 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001131 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001132
Winson Chungf0ea4d32011-06-06 14:27:16 -07001133 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 }
Winson Chungb44b5242011-06-13 11:32:14 -07001135
1136 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001137 * A helper to return the priority for loading of the specified widget page.
1138 */
1139 private int getWidgetPageLoadPriority(int page) {
1140 // If we are snapping to another page, use that index as the target page index
1141 int toPage = mCurrentPage;
1142 if (mNextPage > -1) {
1143 toPage = mNextPage;
1144 }
1145
1146 // We use the distance from the target page as an initial guess of priority, but if there
1147 // are no pages of higher priority than the page specified, then bump up the priority of
1148 // the specified page.
1149 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1150 int minPageDiff = Integer.MAX_VALUE;
1151 while (iter.hasNext()) {
1152 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001153 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001154 }
1155
1156 int rawPageDiff = Math.abs(page - toPage);
1157 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1158 }
1159 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001160 * Return the appropriate thread priority for loading for a given page (we give the current
1161 * page much higher priority)
1162 */
1163 private int getThreadPriorityForPage(int page) {
1164 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001165 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001166 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001167 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001168 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001169 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001170 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001171 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001172 }
1173 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001174 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001175 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001176 return Math.max(0, pageDiff * sPageSleepDelay);
1177 }
Winson Chungb44b5242011-06-13 11:32:14 -07001178 /**
1179 * Creates and executes a new AsyncTask to load a page of widget previews.
1180 */
1181 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001182 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001183
Winson Chungb44b5242011-06-13 11:32:14 -07001184 // Prune all tasks that are no longer needed
1185 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1186 while (iter.hasNext()) {
1187 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001188 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001189 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1190 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001191 task.cancel(false);
1192 iter.remove();
1193 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001194 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001195 }
1196 }
1197
Winson Chungf314b0e2011-08-16 11:54:27 -07001198 // 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 -07001199 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001200 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001201 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001202 @Override
1203 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001204 try {
Winson Chung09945932011-09-20 14:22:40 -07001205 try {
1206 Thread.sleep(sleepMs);
1207 } catch (Exception e) {}
1208 loadWidgetPreviewsInBackground(task, data);
1209 } finally {
1210 if (task.isCancelled()) {
1211 data.cleanup(true);
1212 }
1213 }
Winson Chungb44b5242011-06-13 11:32:14 -07001214 }
1215 },
1216 new AsyncTaskCallback() {
1217 @Override
1218 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001219 mRunningTasks.remove(task);
1220 if (task.isCancelled()) return;
1221 // do cleanup inside onSyncWidgetPageItems
1222 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001223 }
Winson Chung09945932011-09-20 14:22:40 -07001224 });
Winson Chungb44b5242011-06-13 11:32:14 -07001225
1226 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001227 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001228 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001229 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001230 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1231 mRunningTasks.add(t);
1232 }
Winson Chungb44b5242011-06-13 11:32:14 -07001233
Winson Chung785d2eb2011-04-14 16:08:02 -07001234 /*
1235 * Widgets PagedView implementation
1236 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001237 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001238 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1239 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001240
1241 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001242 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001243 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1244 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001246 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001247 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001248
Winson Chung5fc72b32011-10-11 17:53:58 -07001249 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001250 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001251 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001252
Winson Chung5fc72b32011-10-11 17:53:58 -07001253 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001254 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001255 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001256 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001257 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001258 Rect oldBounds = d.copyBounds();
1259 d.setBounds(x, y, x + w, y + h);
1260 d.draw(c);
1261 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001262 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001263 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001264 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001265
Michael Jurkadac85912012-05-18 15:04:49 -07001266 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1267 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1268 final Canvas c = mCachedShortcutPreviewCanvas.get();
1269 if (tempBitmap == null ||
1270 tempBitmap.getWidth() != maxWidth ||
1271 tempBitmap.getHeight() != maxHeight) {
1272 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1273 mCachedShortcutPreviewBitmap.set(tempBitmap);
1274 } else {
1275 c.setBitmap(tempBitmap);
1276 c.drawColor(0, PorterDuff.Mode.CLEAR);
1277 c.setBitmap(null);
1278 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001279 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001280 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001281
1282 int paddingTop =
1283 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1284 int paddingLeft =
1285 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1286 int paddingRight =
1287 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1288
1289 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
1290 float scaleSize = scaledIconWidth / (float) mAppIconSize;
1291
1292 renderDrawableToBitmap(
1293 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1294
1295 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1296 c.setBitmap(preview);
1297 Paint p = mCachedShortcutPreviewPaint.get();
1298 if (p == null) {
1299 p = new Paint();
1300 ColorMatrix colorMatrix = new ColorMatrix();
1301 colorMatrix.setSaturation(0);
1302 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1303 p.setAlpha((int) (255 * 0.06f));
1304 //float density = 1f;
1305 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1306 mCachedShortcutPreviewPaint.set(p);
1307 }
1308 c.drawBitmap(tempBitmap, 0, 0, p);
1309 c.setBitmap(null);
1310
1311 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1312
Winson Chungb44b5242011-06-13 11:32:14 -07001313 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001314 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001315
Michael Jurkadac85912012-05-18 15:04:49 -07001316 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1317 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1318 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001319 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001320 String packageName = provider.getPackageName();
1321 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1322 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001323
Michael Jurka038f9d82011-11-03 13:50:45 -07001324 Drawable drawable = null;
1325 if (previewImage != 0) {
1326 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1327 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001328 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001329 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001330 }
1331 }
1332
Michael Jurka038f9d82011-11-03 13:50:45 -07001333 int bitmapWidth;
1334 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001335 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001336 boolean widgetPreviewExists = (drawable != null);
1337 if (widgetPreviewExists) {
1338 bitmapWidth = drawable.getIntrinsicWidth();
1339 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001340 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001341 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001342 if (cellHSpan < 1) cellHSpan = 1;
1343 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001344
1345 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1346 .getDrawable(R.drawable.widget_preview_tile);
1347 final int previewDrawableWidth = previewDrawable
1348 .getIntrinsicWidth();
1349 final int previewDrawableHeight = previewDrawable
1350 .getIntrinsicHeight();
1351 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1352 bitmapHeight = previewDrawableHeight * cellVSpan;
1353
1354 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1355 Config.ARGB_8888);
1356 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1357 c.setBitmap(defaultPreview);
1358 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1359 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1360 Shader.TileMode.REPEAT);
1361 previewDrawable.draw(c);
1362 c.setBitmap(null);
1363
1364 // Draw the icon in the top left corner
1365 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1366 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1367 float iconScale = Math.min((float) smallestSide
1368 / (mAppIconSize + 2 * minOffset), 1f);
1369
1370 try {
1371 Drawable icon = null;
1372 int hoffset =
1373 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1374 int yoffset =
1375 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1376 if (iconId > 0)
1377 icon = mIconCache.getFullResIcon(packageName, iconId);
1378 Resources resources = mLauncher.getResources();
1379 if (icon != null) {
1380 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1381 yoffset, (int) (mAppIconSize * iconScale),
1382 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001383 }
Michael Jurkadac85912012-05-18 15:04:49 -07001384 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001385 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001386 }
1387
Michael Jurkadac85912012-05-18 15:04:49 -07001388 // Scale to fit width only - let the widget preview be clipped in the
1389 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001390 float scale = 1f;
1391 if (bitmapWidth > maxWidth) {
1392 scale = maxWidth / (float) bitmapWidth;
1393 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001394 if (scale != 1f) {
1395 bitmapWidth = (int) (scale * bitmapWidth);
1396 bitmapHeight = (int) (scale * bitmapHeight);
1397 }
1398
Michael Jurkadac85912012-05-18 15:04:49 -07001399 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1400 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001401
Michael Jurkadac85912012-05-18 15:04:49 -07001402 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001403 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001404 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1405 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001406 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001407 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1408 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1409 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1410 c.setBitmap(preview);
1411 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1412 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1413
1414 Paint p = mCachedAppWidgetPreviewPaint.get();
1415 if (p == null) {
1416 p = new Paint();
1417 p.setFilterBitmap(true);
1418 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001419 }
Michael Jurkadac85912012-05-18 15:04:49 -07001420 c.drawBitmap(defaultPreview, src, dest, p);
1421 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001422 }
Winson Chungb44b5242011-06-13 11:32:14 -07001423 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001424 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001425
Michael Jurka038f9d82011-11-03 13:50:45 -07001426 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001427 Log.d(TAG, "6549598 syncWidgetPageItems page: " + page);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001428 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001429
Winson Chungd2945262011-06-24 15:22:14 -07001430 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001431 final ArrayList<Object> items = new ArrayList<Object>();
1432 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1433 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001434 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001435 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1436 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001437 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001438
Winson Chunge4a647f2011-09-30 14:41:25 -07001439 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001440 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001441 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1442 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001443 }
1444
Winson Chunge4a647f2011-09-30 14:41:25 -07001445 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001446 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001447 layout.setColumnCount(layout.getCellCountX());
1448 for (int i = 0; i < items.size(); ++i) {
1449 Object rawInfo = items.get(i);
1450 PendingAddItemInfo createItemInfo = null;
1451 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1452 R.layout.apps_customize_widget, layout, false);
1453 if (rawInfo instanceof AppWidgetProviderInfo) {
1454 // Fill in the widget information
1455 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1456 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001457
1458 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001459 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001460 createItemInfo.spanX = spanXY[0];
1461 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001462 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001463 createItemInfo.minSpanX = minSpanXY[0];
1464 createItemInfo.minSpanY = minSpanXY[1];
1465
1466 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001467 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001468 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001469 } else if (rawInfo instanceof ResolveInfo) {
1470 // Fill in the shortcuts information
1471 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001472 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001473 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1474 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1475 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001476 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001477 widget.setTag(createItemInfo);
1478 }
1479 widget.setOnClickListener(this);
1480 widget.setOnLongClickListener(this);
1481 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001482 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001483
1484 // Layout each widget
1485 int ix = i % mWidgetCountX;
1486 int iy = i / mWidgetCountX;
1487 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1488 GridLayout.spec(iy, GridLayout.LEFT),
1489 GridLayout.spec(ix, GridLayout.TOP));
1490 lp.width = cellWidth;
1491 lp.height = cellHeight;
1492 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1493 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1494 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1495 layout.addView(widget, lp);
1496 }
1497
Michael Jurka038f9d82011-11-03 13:50:45 -07001498 // wait until a call on onLayout to start loading, because
1499 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1500 // TODO: can we do a measure/layout immediately?
1501 layout.setOnLayoutListener(new Runnable() {
1502 public void run() {
1503 // Load the widget previews
1504 int maxPreviewWidth = cellWidth;
1505 int maxPreviewHeight = cellHeight;
1506 if (layout.getChildCount() > 0) {
1507 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1508 int[] maxSize = w.getPreviewSize();
1509 maxPreviewWidth = maxSize[0];
1510 maxPreviewHeight = maxSize[1];
1511 }
1512 if (immediate) {
1513 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1514 maxPreviewWidth, maxPreviewHeight, null, null);
1515 loadWidgetPreviewsInBackground(null, data);
1516 onSyncWidgetPageItems(data);
1517 } else {
1518 prepareLoadWidgetPreviewsTask(page, items,
1519 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1520 }
1521 }
1522 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001523 }
1524 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1525 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001526 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1527 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001528 if (task != null) {
1529 // Ensure that this task starts running at the correct priority
1530 task.syncThreadPriority();
1531 }
1532
1533 // Load each of the widget/shortcut previews
1534 ArrayList<Object> items = data.items;
1535 ArrayList<Bitmap> images = data.generatedImages;
1536 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001537 for (int i = 0; i < count; ++i) {
1538 if (task != null) {
1539 // Ensure we haven't been cancelled yet
1540 if (task.isCancelled()) break;
1541 // Before work on each item, ensure that this task is running at the correct
1542 // priority
1543 task.syncThreadPriority();
1544 }
1545
1546 Object rawInfo = items.get(i);
1547 if (rawInfo instanceof AppWidgetProviderInfo) {
1548 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001549 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001550
1551 int maxWidth = Math.min(data.maxImageWidth,
1552 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1553 int maxHeight = Math.min(data.maxImageHeight,
1554 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001555 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001556 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001557 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001558 } else if (rawInfo instanceof ResolveInfo) {
1559 // Fill in the shortcuts information
1560 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001561 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001562 }
1563 }
Winson Chungb44b5242011-06-13 11:32:14 -07001564 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001565
Winson Chungb44b5242011-06-13 11:32:14 -07001566 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001567 if (mInTransition) {
1568 mDeferredSyncWidgetPageItems.add(data);
1569 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001571 try {
1572 int page = data.page;
1573 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001574
Michael Jurka39e5d172012-03-12 18:36:12 -07001575 ArrayList<Object> items = data.items;
1576 int count = items.size();
1577 for (int i = 0; i < count; ++i) {
1578 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1579 if (widget != null) {
1580 Bitmap preview = data.generatedImages.get(i);
1581 widget.applyPreview(new FastBitmapDrawable(preview), i);
1582 }
1583 }
Winson Chung68e4c642011-11-10 15:48:25 -08001584
Michael Jurka39e5d172012-03-12 18:36:12 -07001585 layout.createHardwareLayer();
1586 invalidate();
1587
1588 // Update all thread priorities
1589 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1590 while (iter.hasNext()) {
1591 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1592 int pageIndex = task.page;
1593 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1594 }
1595 } finally {
1596 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001597 }
Winson Chungb44b5242011-06-13 11:32:14 -07001598 }
Winson Chung46af2e82011-05-09 16:00:53 -07001599
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 @Override
1601 public void syncPages() {
1602 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001603 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001604
Adam Cohen0cd3b642011-10-14 14:58:00 -07001605 Context context = getContext();
1606 for (int j = 0; j < mNumWidgetPages; ++j) {
1607 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1608 mWidgetCountY);
1609 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001610 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001611 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001612 }
1613
Adam Cohen0cd3b642011-10-14 14:58:00 -07001614 for (int i = 0; i < mNumAppsPages; ++i) {
1615 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1616 setupPage(layout);
1617 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 }
Winson Chung7c7a22d2012-06-01 13:46:48 -07001619
1620 Log.d(TAG, "6549598 syncPages mNumAppsPages: " + mNumAppsPages + " mNumWidgetPages: " + mNumWidgetPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001621 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001622
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001624 public void syncPageItems(int page, boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001625 Log.d(TAG, "6549598 syncPageItems page: " + page + " immediate: " + immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001626 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001627 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001628 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001629 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 }
1631 }
1632
Adam Cohen22f823d2011-09-01 17:22:18 -07001633 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1634 // it is in the z-order. This is important to insure touch events are handled correctly.
1635 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001636 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001637 }
1638
Adam Cohenae4f1552011-10-20 00:15:42 -07001639 @Override
1640 protected int indexToPage(int index) {
1641 return getChildCount() - index - 1;
1642 }
1643
Adam Cohen22f823d2011-09-01 17:22:18 -07001644 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1645 @Override
1646 protected void screenScrolled(int screenCenter) {
1647 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001648
1649 for (int i = 0; i < getChildCount(); i++) {
1650 View v = getPageAt(i);
1651 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001652 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001653
1654 float interpolatedProgress =
1655 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1656 float scale = (1 - interpolatedProgress) +
1657 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1658 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001659
Adam Cohen2591f6a2011-10-25 14:36:40 -07001660 float alpha;
1661
Winson Chungd167e2a2012-04-26 13:13:01 -07001662 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001663 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001664 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001665 } else {
1666 // On large screens we need to fade the page as it nears its leftmost position
1667 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1668 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001669
1670 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1671 int pageWidth = v.getMeasuredWidth();
1672 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001673
1674 if (PERFORM_OVERSCROLL_ROTATION) {
1675 if (i == 0 && scrollProgress < 0) {
1676 // Overscroll to the left
1677 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1678 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1679 scale = 1.0f;
1680 alpha = 1.0f;
1681 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001682 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001683 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1684 // Overscroll to the right
1685 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1686 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1687 scale = 1.0f;
1688 alpha = 1.0f;
1689 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001690 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001691 } else {
1692 v.setPivotY(pageHeight / 2.0f);
1693 v.setPivotX(pageWidth / 2.0f);
1694 v.setRotationY(0f);
1695 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001696 }
1697
1698 v.setTranslationX(translationX);
1699 v.setScaleX(scale);
1700 v.setScaleY(scale);
1701 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001702
1703 // If the view has 0 alpha, we set it to be invisible so as to prevent
1704 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001705 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001706 v.setVisibility(INVISIBLE);
1707 } else if (v.getVisibility() != VISIBLE) {
1708 v.setVisibility(VISIBLE);
1709 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001710 }
1711 }
1712 }
1713
1714 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001715 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001716 }
1717
Winson Chung785d2eb2011-04-14 16:08:02 -07001718 /**
1719 * Used by the parent to get the content width to set the tab bar to
1720 * @return
1721 */
1722 public int getPageContentWidth() {
1723 return mContentWidth;
1724 }
1725
Winson Chungb26f3d62011-06-02 10:49:29 -07001726 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001727 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001728 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001729 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001730 // We reset the save index when we change pages so that it will be recalculated on next
1731 // rotation
1732 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001733 }
1734
Winson Chung785d2eb2011-04-14 16:08:02 -07001735 /*
1736 * AllAppsView implementation
1737 */
1738 @Override
1739 public void setup(Launcher launcher, DragController dragController) {
1740 mLauncher = launcher;
1741 mDragController = dragController;
1742 }
1743 @Override
1744 public void zoom(float zoom, boolean animate) {
1745 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1746 }
1747 @Override
1748 public boolean isVisible() {
1749 return (getVisibility() == VISIBLE);
1750 }
1751 @Override
1752 public boolean isAnimating() {
1753 return false;
1754 }
1755 @Override
1756 public void setApps(ArrayList<ApplicationInfo> list) {
1757 mApps = list;
1758 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001759 Log.d(TAG, "6549598 setApps mApps.size(): " + mApps.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001760 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001761
Winson Chung875de7e2011-06-28 14:25:17 -07001762 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1763 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001764 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001765 }
1766 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1767 // We add it in place, in alphabetical order
1768 int count = list.size();
1769 for (int i = 0; i < count; ++i) {
1770 ApplicationInfo info = list.get(i);
1771 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1772 if (index < 0) {
1773 mApps.add(-(index + 1), info);
1774 }
1775 }
1776 }
1777 @Override
1778 public void addApps(ArrayList<ApplicationInfo> list) {
1779 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001780 Log.d(TAG, "6549598 addApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001781 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001782 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001783 Log.d(TAG, "6549598 addApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001784 }
1785 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1786 ComponentName removeComponent = item.intent.getComponent();
1787 int length = list.size();
1788 for (int i = 0; i < length; ++i) {
1789 ApplicationInfo info = list.get(i);
1790 if (info.intent.getComponent().equals(removeComponent)) {
1791 return i;
1792 }
1793 }
1794 return -1;
1795 }
1796 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1797 // loop through all the apps and remove apps that have the same component
1798 int length = list.size();
1799 for (int i = 0; i < length; ++i) {
1800 ApplicationInfo info = list.get(i);
1801 int removeIndex = findAppByComponent(mApps, info);
1802 if (removeIndex > -1) {
1803 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001804 }
1805 }
1806 }
1807 @Override
1808 public void removeApps(ArrayList<ApplicationInfo> list) {
1809 removeAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001810 Log.d(TAG, "6549598 removeApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001811 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001812 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001813 Log.d(TAG, "6549598 removeApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001814 }
1815 @Override
1816 public void updateApps(ArrayList<ApplicationInfo> list) {
1817 // We remove and re-add the updated applications list because it's properties may have
1818 // changed (ie. the title), and this will ensure that the items will be in their proper
1819 // place in the list.
1820 removeAppsWithoutInvalidate(list);
1821 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001822 Log.d(TAG, "6549598 updateApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001823 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001824 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001825 Log.d(TAG, "6549598 updateApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001826 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001827
Winson Chung785d2eb2011-04-14 16:08:02 -07001828 @Override
1829 public void reset() {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001830 Log.d(TAG, "6549598 reset");
Winson Chung649668f2012-01-10 13:07:16 -08001831 // If we have reset, then we should not continue to restore the previous state
1832 mSaveInstanceStateItemIndex = -1;
1833
Adam Cohenb64d36e2011-10-17 21:48:02 -07001834 AppsCustomizeTabHost tabHost = getTabHost();
1835 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001836 if (tag != null) {
1837 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1838 tabHost.setCurrentTabFromContent(ContentType.Applications);
1839 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001840 }
Winson Chung649668f2012-01-10 13:07:16 -08001841
Adam Cohenb64d36e2011-10-17 21:48:02 -07001842 if (mCurrentPage != 0) {
1843 invalidatePageData(0);
1844 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001845 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001846
1847 private AppsCustomizeTabHost getTabHost() {
1848 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1849 }
1850
Winson Chung785d2eb2011-04-14 16:08:02 -07001851 @Override
1852 public void dumpState() {
1853 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001854 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1855 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001856 }
Adam Cohen4e844012011-11-09 13:48:04 -08001857
Winson Chung785d2eb2011-04-14 16:08:02 -07001858 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001859 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001860 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001861 for (Object i: list) {
1862 if (i instanceof AppWidgetProviderInfo) {
1863 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1864 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1865 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1866 + " initialLayout=" + info.initialLayout
1867 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1868 } else if (i instanceof ResolveInfo) {
1869 ResolveInfo info = (ResolveInfo) i;
1870 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1871 + info.icon);
1872 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001873 }
1874 }
Adam Cohen4e844012011-11-09 13:48:04 -08001875
Winson Chung785d2eb2011-04-14 16:08:02 -07001876 @Override
1877 public void surrender() {
1878 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1879 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001880
1881 // Stop all background tasks
1882 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001883 }
Winson Chung007c6982011-06-14 13:27:53 -07001884
Winson Chunge4e50662012-01-23 14:45:13 -08001885 @Override
1886 public void iconPressed(PagedViewIcon icon) {
1887 // Reset the previously pressed icon and store a reference to the pressed icon so that
1888 // we can reset it on return to Launcher (in Launcher.onResume())
1889 if (mPressedIcon != null) {
1890 mPressedIcon.resetDrawableState();
1891 }
1892 mPressedIcon = icon;
1893 }
1894
1895 public void resetDrawableState() {
1896 if (mPressedIcon != null) {
1897 mPressedIcon.resetDrawableState();
1898 mPressedIcon = null;
1899 }
1900 }
Winson Chung68e4c642011-11-10 15:48:25 -08001901
Winson Chungb44b5242011-06-13 11:32:14 -07001902 /*
1903 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1904 * widget previews in the background with the AsyncTasks.
1905 */
Winson Chung68e4c642011-11-10 15:48:25 -08001906 final static int sLookBehindPageCount = 2;
1907 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001908 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001909 final int count = getChildCount();
1910 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1911 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1912 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001913 }
1914 protected int getAssociatedUpperPageBound(int page) {
1915 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001916 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1917 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1918 count - 1);
1919 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001920 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001921
1922 @Override
1923 protected String getCurrentPageDescription() {
1924 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1925 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001926 int count = 0;
1927
Adam Cohen0cd3b642011-10-14 14:58:00 -07001928 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001929 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001930 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001931 } else {
1932 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001933 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001934 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001935 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001936
Michael Jurka8b805b12012-04-18 14:23:14 -07001937 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001938 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001939}