blob: f12da7d0539dcf7f08f4e0c32cb5625feeeaedc3 [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>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700313 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
314 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700315
Michael Jurkadac85912012-05-18 15:04:49 -0700316 // Used for drawing shortcut previews
317 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
318 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
319 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
320
321 // Used for drawing widget previews
322 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
323 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
324 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
325 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
326
Winson Chung785d2eb2011-04-14 16:08:02 -0700327 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
328 super(context, attrs);
329 mLayoutInflater = LayoutInflater.from(context);
330 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700331 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700332 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700333 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700334 mCanvas = new Canvas();
335 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700336
337 // Save the default widget preview background
338 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700339 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700340 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700341
Winson Chung6032e7e2011-11-08 15:47:17 -0800342 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
343 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
344 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700345 mWidgetWidthGap =
346 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
347 mWidgetHeightGap =
348 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700349 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
350 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700351 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
352 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700353 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700354 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700355
Winson Chung1ed747a2011-05-03 16:18:34 -0700356 // The padding on the non-matched dimension for the default widget preview icons
357 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700358 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700359
360 // Unless otherwise specified this view is important for accessibility.
361 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
362 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
363 }
Winson Chung7c7a22d2012-06-01 13:46:48 -0700364
365 Log.d(TAG, "6549598 ctor mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700366 }
367
368 @Override
369 protected void init() {
370 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700371 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700372
373 Context context = getContext();
374 Resources r = context.getResources();
375 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
Winson Chung7c7a22d2012-06-01 13:46:48 -0700376 Log.d(TAG, "6549598 init mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700377 }
378
Winson Chungf0ea4d32011-06-06 14:27:16 -0700379 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700380 protected void onUnhandledTap(MotionEvent ev) {
381 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700382 // Dismiss AppsCustomize if we tap
383 mLauncher.showWorkspace(true);
384 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385 }
386
Winson Chung5afbf7b2011-07-25 11:53:08 -0700387 /** Returns the item index of the center item on this page so that we can restore to this
388 * item index when we rotate. */
389 private int getMiddleComponentIndexOnCurrentPage() {
390 int i = -1;
391 if (getPageCount() > 0) {
392 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700393 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700394 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700395 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
396 int numItemsPerPage = mCellCountX * mCellCountY;
397 int childCount = childrenLayout.getChildCount();
398 if (childCount > 0) {
399 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700400 }
401 } else {
402 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700403 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700404 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
405 int childCount = layout.getChildCount();
406 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700407 i = numApps +
408 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
409 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700410 }
411 }
412 return i;
413 }
414
415 /** Get the index of the item to restore to if we need to restore the current page. */
416 int getSaveInstanceStateIndex() {
417 if (mSaveInstanceStateItemIndex == -1) {
418 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
419 }
420 return mSaveInstanceStateItemIndex;
421 }
422
423 /** Returns the page in the current orientation which is expected to contain the specified
424 * item index. */
425 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700426 if (index < 0) return 0;
427
428 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700429 int numItemsPerPage = mCellCountX * mCellCountY;
430 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700431 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700432 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700433 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
434 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700435 }
436
Winson Chungf0ea4d32011-06-06 14:27:16 -0700437 /**
438 * This differs from isDataReady as this is the test done if isDataReady is not set.
439 */
440 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700441 // We only do this test once, and we default to the Applications page, so we only really
442 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700443 // TODO: What if one of them is validly empty
444 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700445 }
446
Winson Chung5afbf7b2011-07-25 11:53:08 -0700447 /** Restores the page for an item at the specified index */
448 void restorePageForIndex(int index) {
449 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700450 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700451 }
452
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700453 private void updatePageCounts() {
454 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
455 (float) (mWidgetCountX * mWidgetCountY));
456 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
Winson Chung7c7a22d2012-06-01 13:46:48 -0700457 Log.d(TAG, "6549598 updatePageCounts mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
458 Log.d(TAG, "6549598 mApps.size(): " + mApps.size() + " mWidgets.size(): " + mWidgets.size() + " mCellCountX: " + mCellCountX + " mCellCountY: " + mCellCountY);
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700459 }
460
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 protected void onDataReady(int width, int height) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700462 Log.d(TAG, "6549598 onDataReady");
Winson Chungf0ea4d32011-06-06 14:27:16 -0700463 // Note that we transpose the counts in portrait so that we get a similar layout
464 boolean isLandscape = getResources().getConfiguration().orientation ==
465 Configuration.ORIENTATION_LANDSCAPE;
466 int maxCellCountX = Integer.MAX_VALUE;
467 int maxCellCountY = Integer.MAX_VALUE;
468 if (LauncherApplication.isScreenLarge()) {
469 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
470 LauncherModel.getCellCountY());
471 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
472 LauncherModel.getCellCountX());
473 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800474 if (mMaxAppCellCountX > -1) {
475 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
476 }
477 if (mMaxAppCellCountY > -1) {
478 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
479 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700480
481 // Now that the data is ready, we can calculate the content width, the number of cells to
482 // use for each page
483 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
484 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
485 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
486 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
487 mCellCountX = mWidgetSpacingLayout.getCellCountX();
488 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700489 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700490
Winson Chungdb1138b2011-06-30 14:39:35 -0700491 // Force a measure to update recalculate the gaps
492 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
493 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
494 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700495 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700496
Michael Jurkae326f182011-11-21 14:05:46 -0800497 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
498 final boolean hostIsTransitioning = host.isTransitioning();
499
Adam Cohen0cd3b642011-10-14 14:58:00 -0700500 // Restore the page
501 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800502 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700503
Winson Chung3f4e1422011-11-17 14:58:51 -0800504 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
505 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
506 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800507 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800508 post(new Runnable() {
509 @Override
510 public void run() {
511 showAllAppsCling();
512 }
513 });
514 }
515 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700516
Winson Chung3f4e1422011-11-17 14:58:51 -0800517 void showAllAppsCling() {
518 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
519 mHasShownAllAppsCling = true;
520 // Calculate the position for the cling punch through
521 int[] offset = new int[2];
522 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
523 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
524 // PagedViews are centered horizontally but top aligned
525 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
526 offset[0];
527 pos[1] += offset[1];
528 mLauncher.showFirstRunAllAppsCling(pos);
529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
531
532 @Override
533 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
534 int width = MeasureSpec.getSize(widthMeasureSpec);
535 int height = MeasureSpec.getSize(heightMeasureSpec);
536 if (!isDataReady()) {
537 if (testDataReady()) {
538 setDataIsReady();
539 setMeasuredDimension(width, height);
540 onDataReady(width, height);
541 }
542 }
543
544 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
545 }
546
Winson Chung785d2eb2011-04-14 16:08:02 -0700547 public void onPackagesUpdated() {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700548 Log.d(TAG, "6549598 onPackagesUpdated");
Adam Cohenb0fa3522011-08-17 16:10:46 -0700549 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
550 // by a broadcast receiver, and in order for it to work correctly, we need to know that
551 // the AppWidgetService has already received and processed the same broadcast. Since there
552 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
553 // we should have a more precise way of ensuring the AppWidgetService is up to date.
554 postDelayed(new Runnable() {
555 public void run() {
556 updatePackages();
557 }
558 }, 500);
559 }
560
561 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700562 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700563 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700564 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 List<AppWidgetProviderInfo> widgets =
566 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
568 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800569 for (AppWidgetProviderInfo widget : widgets) {
570 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700571 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700572 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
573 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700574 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
575 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700576 if (minSpanX <= LauncherModel.getCellCountX() &&
577 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700578 mWidgets.add(widget);
579 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800580 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700581 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800582 widget.minWidth + ", " + widget.minHeight + ")");
583 }
584 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700585 mWidgets.addAll(shortcuts);
586 Collections.sort(mWidgets,
587 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung7c7a22d2012-06-01 13:46:48 -0700588 Log.d(TAG, "6549598 updatePackages mWidgets.size(): " + mWidgets.size() + " wasEmpty: " + wasEmpty);
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700589 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700590
Winson Chung875de7e2011-06-28 14:25:17 -0700591 if (wasEmpty) {
592 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
593 // a layout to do this test and invalidate the page data when ready.
594 if (testDataReady()) requestLayout();
595 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700596 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700597 invalidatePageData();
598 }
Winson Chung4b576be2011-04-27 17:40:20 -0700599 }
600
601 @Override
602 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700603 // When we have exited all apps or are in transition, disregard clicks
604 if (!mLauncher.isAllAppsCustomizeOpen() ||
605 mLauncher.getWorkspace().isSwitchingState()) return;
606
Winson Chung4b576be2011-04-27 17:40:20 -0700607 if (v instanceof PagedViewIcon) {
608 // Animate some feedback to the click
609 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800610
611 // Lock the drawable state to pressed until we return to Launcher
612 if (mPressedIcon != null) {
613 mPressedIcon.lockDrawableState();
614 }
Winson Chungc7450e32012-04-17 17:34:08 -0700615
Winson Chung18f41f82012-05-09 13:28:10 -0700616 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
617 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
618 // when Launcher resumes and we are still in AllApps.
619 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700620 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
621
Winson Chung4b576be2011-04-27 17:40:20 -0700622 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700623 // Let the user know that they have to long press to add a widget
624 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
625 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700626
Winson Chungd2e87b32011-06-02 10:53:07 -0700627 // Create a little animation to show that the widget can move
628 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
629 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
630 AnimatorSet bounce = new AnimatorSet();
631 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
632 tyuAnim.setDuration(125);
633 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
634 tydAnim.setDuration(100);
635 bounce.play(tyuAnim).before(tydAnim);
636 bounce.setInterpolator(new AccelerateInterpolator());
637 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700638 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700639 }
640
Winson Chungc6f10b92011-11-14 11:39:07 -0800641 public boolean onKey(View v, int keyCode, KeyEvent event) {
642 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
643 }
644
Winson Chung785d2eb2011-04-14 16:08:02 -0700645 /*
646 * PagedViewWithDraggableItems implementation
647 */
648 @Override
649 protected void determineDraggingStart(android.view.MotionEvent ev) {
650 // Disable dragging by pulling an app down for now.
651 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700652
Winson Chung4b576be2011-04-27 17:40:20 -0700653 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700654 mLauncher.getWorkspace().onDragStartedWithItem(v);
655 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700656 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700657
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700658 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700659 Log.d(TAG, "6557954 Preload widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800660 final AppWidgetProviderInfo pInfo = info.info;
661 if (pInfo.configure != null) {
662 return;
663 }
664
Adam Cohen21a170b2012-05-30 15:17:06 -0700665 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 mBindWidgetRunnable = new Runnable() {
667 @Override
668 public void run() {
Adam Cohen68f681b2012-05-28 15:01:16 -0700669 Log.d(TAG, " 6557954 Preload, bind widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700671 if (AppWidgetManager.getInstance(mLauncher)
672 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
673 mWidgetCleanupState = WIDGET_BOUND;
674 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 }
676 };
677 post(mBindWidgetRunnable);
678
679 mInflateWidgetRunnable = new Runnable() {
680 @Override
681 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700682 AppWidgetHostView hostView = mLauncher.
683 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 info.boundWidget = hostView;
Adam Cohen68f681b2012-05-28 15:01:16 -0700685 Log.d(TAG, " 6557954 Preload, inflate widget: " + info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800687 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700688 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
689 info.spanY, info, false);
690
691 // We want the first widget layout to be the correct size. This will be important
692 // for width size reporting to the AppWidgetManager.
693 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
694 unScaledSize[1]);
695 lp.x = lp.y = 0;
696 lp.customPosition = true;
697 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800698 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800699 }
700 };
701 post(mInflateWidgetRunnable);
702 }
703
704 @Override
705 public void onShortPress(View v) {
706 // We are anticipating a long press, and we use this time to load bind and instantiate
707 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700708 if (mCreateWidgetInfo != null) {
709 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
Adam Cohen68f681b2012-05-28 15:01:16 -0700710 Log.d(TAG, "**** 6557954 Previous shortpress not cleaned up, cleaning up now: " + mCreateWidgetInfo.info);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700711 cleanupWidgetPreloading(false);
712 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800713 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohen68f681b2012-05-28 15:01:16 -0700714 Log.d(TAG, "6557954 Short press triggered for view: " + v + ", widget info: " + mCreateWidgetInfo.info);
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700715 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 }
717
Adam Cohen0e56cc92012-05-11 15:57:05 -0700718 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700719 Log.d(TAG, "6557954 Cleaning up widget, was added: " + widgetWasAdded);
720 if (mCreateWidgetInfo != null) {
721 Log.d(TAG, " 6557954 Cleaning up widget, widget info: " + mCreateWidgetInfo.info);
722 }
723
Adam Cohen0e56cc92012-05-11 15:57:05 -0700724 if (!widgetWasAdded) {
725 // If the widget was not added, we may need to do further cleanup.
726 PendingAddWidgetInfo info = mCreateWidgetInfo;
727 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700728
729 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
730 Log.d(TAG, " 6557954 Cleaning up widget, remove preload callbacks");
731 // We never did any preloading, so just remove pending callbacks to do so
732 removeCallbacks(mBindWidgetRunnable);
733 removeCallbacks(mInflateWidgetRunnable);
734 } else if (mWidgetCleanupState == WIDGET_BOUND) {
735 // Delete the widget id which was allocated
736 if (mWidgetLoadingId != -1) {
737 Log.d(TAG, " 6557954 Cleaning up widget, delete widget id");
738 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
739 }
740
741 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen68f681b2012-05-28 15:01:16 -0700742 Log.d(TAG, " 6557954 Cleaning up widget, remove callbacks");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700743 removeCallbacks(mInflateWidgetRunnable);
744 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700745 // Delete the widget id which was allocated
746 if (mWidgetLoadingId != -1) {
747 Log.d(TAG, " 6557954 Cleaning up widget, delete widget id");
748 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
749 }
750
Adam Cohen0e56cc92012-05-11 15:57:05 -0700751 // The widget was inflated and added to the DragLayer -- remove it.
Adam Cohen68f681b2012-05-28 15:01:16 -0700752 Log.d(TAG, " 6557954 Cleaning up widget, remove inflated widget from draglayer");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700753 AppWidgetHostView widget = info.boundWidget;
754 mLauncher.getDragLayer().removeView(widget);
755 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 }
757 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
758 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700759 mCreateWidgetInfo = null;
760 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 }
762
Adam Cohen7a326642012-02-22 12:03:22 -0800763 @Override
764 public void cleanUpShortPress(View v) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700765 Log.d(TAG, "6557954 Cleanup shortpress");
Adam Cohen7a326642012-02-22 12:03:22 -0800766 if (!mDraggingWidget) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700767 Log.d(TAG, " 6557954 Cleanup shortpress, cleanup cleanup preloading");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700768 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800769 }
770 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800771
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700772 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800773 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700774 // Get the widget preview as the drag representation
775 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700776 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700777
Adam Cohen68f681b2012-05-28 15:01:16 -0700778 if (createItemInfo instanceof PendingAddWidgetInfo) {
779 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
780 Log.d(TAG, "6557954 Begin dragging widget, view: " + v + ", widget info: " + createWidgetInfo.info);
781 }
782
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700783 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
784 // we abort the drag.
785 if (image.getDrawable() == null) {
Adam Cohen68f681b2012-05-28 15:01:16 -0700786 Log.d(TAG, " 6557954 Begin dragging widget, abort, no drawable set");
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700787 mDraggingWidget = false;
788 return false;
789 }
790
Winson Chung4b576be2011-04-27 17:40:20 -0700791 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800792 Bitmap preview;
793 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800794 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700795 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700796 // This can happen in some weird cases involving multi-touch. We can't start dragging
797 // the widget if this is null, so we break out.
798 if (mCreateWidgetInfo == null) {
799 return false;
800 }
801
Adam Cohen1b36dc32012-02-13 19:27:37 -0800802 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
803 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700804 int spanX = createItemInfo.spanX;
805 int spanY = createItemInfo.spanY;
806 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
807 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700808
Winson Chung72d59842012-02-22 13:51:36 -0800809 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
810 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700811 int maxWidth, maxHeight;
812 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
813 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800814 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700815 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800816
817 // Determine the image view drawable scale relative to the preview
818 float[] mv = new float[9];
819 Matrix m = new Matrix();
820 m.setRectToRect(
821 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
822 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
823 (float) previewDrawable.getIntrinsicHeight()),
824 Matrix.ScaleToFit.START);
825 m.getValues(mv);
826 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700827 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700828 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
829 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
830 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
831 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
832
Winson Chung1120e032011-11-22 16:11:31 -0800833 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700834 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700835 renderDrawableToBitmap(icon, preview, 0, 0,
836 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700837 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700838 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700839 createItemInfo.spanX = createItemInfo.spanY = 1;
840 }
Winson Chung4b576be2011-04-27 17:40:20 -0700841
Peter Ng8db70002011-10-25 15:40:08 -0700842 // We use a custom alpha clip table for the default widget previews
843 Paint alphaClipPaint = null;
844 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700845 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700846 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
847 alphaClipPaint = new Paint();
848 alphaClipPaint.setMaskFilter(alphaClipTable);
849 }
850 }
851
Winson Chung1120e032011-11-22 16:11:31 -0800852 // Save the preview for the outline generation, then dim the preview
853 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
854 false);
Winson Chung1120e032011-11-22 16:11:31 -0800855
Winson Chung4b576be2011-04-27 17:40:20 -0700856 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800857 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700858 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800859 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
860 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800861 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800862 outline.recycle();
863 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700864 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866
Winson Chung4b576be2011-04-27 17:40:20 -0700867 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700869 if (!super.beginDragging(v)) return false;
870
871 if (v instanceof PagedViewIcon) {
872 beginDraggingApplication(v);
873 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700874 if (!beginDraggingWidget(v)) {
875 return false;
876 }
Winson Chung4b576be2011-04-27 17:40:20 -0700877 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800878
879 // We delay entering spring-loaded mode slightly to make sure the UI
880 // thready is free of any work.
881 postDelayed(new Runnable() {
882 @Override
883 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800884 // We don't enter spring-loaded mode if the drag has been cancelled
885 if (mLauncher.getDragController().isDragging()) {
886 // Dismiss the cling
887 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800888
Adam Cohen1b36dc32012-02-13 19:27:37 -0800889 // Reset the alpha on the dragged icon before we drag
890 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800891
Adam Cohen1b36dc32012-02-13 19:27:37 -0800892 // Go into spring loaded mode (must happen before we startDrag())
893 mLauncher.enterSpringLoadedDragMode();
894 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 }
Winson Chung72d59842012-02-22 13:51:36 -0800896 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800897
Winson Chung785d2eb2011-04-14 16:08:02 -0700898 return true;
899 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800900
Winson Chunga48487a2012-03-20 16:19:37 -0700901 /**
902 * Clean up after dragging.
903 *
904 * @param target where the item was dragged to (can be null if the item was flung)
905 */
906 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700907 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700908 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700909 // Exit spring loaded mode if we have not successfully dropped or have not handled the
910 // drop in Workspace
911 mLauncher.exitSpringLoadedDragMode();
912 }
Winson Chung4b919f82012-05-01 10:44:08 -0700913 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700914 }
915
Winson Chung785d2eb2011-04-14 16:08:02 -0700916 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700917 public View getContent() {
918 return null;
919 }
920
921 @Override
922 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700923 mInTransition = true;
924 if (toWorkspace) {
925 cancelAllTasks();
926 }
927 }
928
929 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700930 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700931 }
932
933 @Override
934 public void onLauncherTransitionStep(Launcher l, float t) {
935 }
936
937 @Override
938 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700939 Log.d(TAG, "6549598 onLauncherTransitionEnd");
Michael Jurka39e5d172012-03-12 18:36:12 -0700940 mInTransition = false;
941 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
942 onSyncWidgetPageItems(d);
943 }
944 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700945 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
946 r.run();
947 }
948 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700949 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700950 }
951
952 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700953 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
954 boolean success) {
955 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
956 if (isFlingToDelete) return;
957
958 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700959
960 // Display an error message if the drag failed due to there not being enough space on the
961 // target layout we were dropping on.
962 if (!success) {
963 boolean showOutOfSpaceMessage = false;
964 if (target instanceof Workspace) {
965 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
966 Workspace workspace = (Workspace) target;
967 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700968 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700969 if (layout != null) {
970 layout.calculateSpans(itemInfo);
971 showOutOfSpaceMessage =
972 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
973 }
974 }
Winson Chungfc79c802011-05-02 13:35:34 -0700975 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700976 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700977 }
Adam Cohen7a326642012-02-22 12:03:22 -0800978
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800979 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700980 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700981 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800982 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 }
984
Winson Chunga48487a2012-03-20 16:19:37 -0700985 @Override
986 public void onFlingToDeleteCompleted() {
987 // We just dismiss the drag when we fling, so cleanup here
988 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700989 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700990 mDraggingWidget = false;
991 }
992
993 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800994 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700995 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800996 }
997
Winson Chung7f0acdd2011-09-19 18:34:19 -0700998 @Override
999 protected void onDetachedFromWindow() {
1000 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001001 cancelAllTasks();
1002 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001003
Michael Jurkae326f182011-11-21 14:05:46 -08001004 public void clearAllWidgetPages() {
1005 cancelAllTasks();
1006 int count = getChildCount();
1007 for (int i = 0; i < count; i++) {
1008 View v = getPageAt(i);
1009 if (v instanceof PagedViewGridLayout) {
1010 ((PagedViewGridLayout) v).removeAllViewsOnPage();
1011 mDirtyPageContent.set(i, true);
1012 }
1013 }
1014 }
1015
Adam Cohen0cd3b642011-10-14 14:58:00 -07001016 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -07001017 // Clean up all the async tasks
1018 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1019 while (iter.hasNext()) {
1020 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1021 task.cancel(false);
1022 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -07001023 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -07001024
1025 // We've already preallocated the views for the data to load into, so clear them as well
1026 View v = getPageAt(task.page);
1027 if (v instanceof PagedViewGridLayout) {
1028 ((PagedViewGridLayout) v).removeAllViewsOnPage();
1029 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001030 }
Winson Chung83687b12012-04-25 16:01:01 -07001031 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -07001032 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -07001033 }
1034
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 public void setContentType(ContentType type) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001036 Log.d(TAG, "6549598 setContentType mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001037 if (type == ContentType.Widgets) {
1038 invalidatePageData(mNumAppsPages, true);
1039 } else if (type == ContentType.Applications) {
1040 invalidatePageData(0, true);
1041 }
Winson Chungb44b5242011-06-13 11:32:14 -07001042 }
1043
Adam Cohen0cd3b642011-10-14 14:58:00 -07001044 protected void snapToPage(int whichPage, int delta, int duration) {
1045 super.snapToPage(whichPage, delta, duration);
1046 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001047
1048 // Update the thread priorities given the direction lookahead
1049 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1050 while (iter.hasNext()) {
1051 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001052 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001053 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1054 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1055 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1056 } else {
1057 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1058 }
1059 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001060 }
1061
1062 private void updateCurrentTab(int currentPage) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001063 Log.d(TAG, "6549598 updateCurrentTab mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001064 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001065 if (tabHost != null) {
1066 String tag = tabHost.getCurrentTabTag();
1067 if (tag != null) {
1068 if (currentPage >= mNumAppsPages &&
1069 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1070 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1071 } else if (currentPage < mNumAppsPages &&
1072 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1073 tabHost.setCurrentTabFromContent(ContentType.Applications);
1074 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001075 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001076 }
1077 }
1078
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 /*
1080 * Apps PagedView implementation
1081 */
Winson Chung63257c12011-05-05 17:06:13 -07001082 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1083 int childCount = layout.getChildCount();
1084 for (int i = 0; i < childCount; ++i) {
1085 layout.getChildAt(i).setVisibility(visibility);
1086 }
1087 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001088 private void setupPage(PagedViewCellLayout layout) {
1089 layout.setCellCount(mCellCountX, mCellCountY);
1090 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1091 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1092 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1093
Winson Chung63257c12011-05-05 17:06:13 -07001094 // Note: We force a measure here to get around the fact that when we do layout calculations
1095 // immediately after syncing, we don't have a proper width. That said, we already know the
1096 // expected page width, so we can actually optimize by hiding all the TextView-based
1097 // children that are expensive to measure, and let that happen naturally later.
1098 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001099 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001100 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001101 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001102 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001103 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001104 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001105
Winson Chungf314b0e2011-08-16 11:54:27 -07001106 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001107 Log.d(TAG, "6549598 syncAppsPageItems page: " + page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001108 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001109 int numCells = mCellCountX * mCellCountY;
1110 int startIndex = page * numCells;
1111 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001112 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001113
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001115 ArrayList<Object> items = new ArrayList<Object>();
1116 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 for (int i = startIndex; i < endIndex; ++i) {
1118 ApplicationInfo info = mApps.get(i);
1119 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1120 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001121 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001122 icon.setOnClickListener(this);
1123 icon.setOnLongClickListener(this);
1124 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001125 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001126
1127 int index = i - startIndex;
1128 int x = index % mCellCountX;
1129 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001130 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001131
1132 items.add(info);
1133 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001135
Winson Chungf0ea4d32011-06-06 14:27:16 -07001136 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 }
Winson Chungb44b5242011-06-13 11:32:14 -07001138
1139 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001140 * A helper to return the priority for loading of the specified widget page.
1141 */
1142 private int getWidgetPageLoadPriority(int page) {
1143 // If we are snapping to another page, use that index as the target page index
1144 int toPage = mCurrentPage;
1145 if (mNextPage > -1) {
1146 toPage = mNextPage;
1147 }
1148
1149 // We use the distance from the target page as an initial guess of priority, but if there
1150 // are no pages of higher priority than the page specified, then bump up the priority of
1151 // the specified page.
1152 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1153 int minPageDiff = Integer.MAX_VALUE;
1154 while (iter.hasNext()) {
1155 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001156 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001157 }
1158
1159 int rawPageDiff = Math.abs(page - toPage);
1160 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1161 }
1162 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001163 * Return the appropriate thread priority for loading for a given page (we give the current
1164 * page much higher priority)
1165 */
1166 private int getThreadPriorityForPage(int page) {
1167 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001168 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001169 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001170 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001171 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001172 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001173 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001174 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001175 }
1176 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001177 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001178 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001179 return Math.max(0, pageDiff * sPageSleepDelay);
1180 }
Winson Chungb44b5242011-06-13 11:32:14 -07001181 /**
1182 * Creates and executes a new AsyncTask to load a page of widget previews.
1183 */
1184 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001185 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001186
Winson Chungb44b5242011-06-13 11:32:14 -07001187 // Prune all tasks that are no longer needed
1188 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1189 while (iter.hasNext()) {
1190 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001191 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001192 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1193 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001194 task.cancel(false);
1195 iter.remove();
1196 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001197 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001198 }
1199 }
1200
Winson Chungf314b0e2011-08-16 11:54:27 -07001201 // 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 -07001202 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001203 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001204 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001205 @Override
1206 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001207 try {
Winson Chung09945932011-09-20 14:22:40 -07001208 try {
1209 Thread.sleep(sleepMs);
1210 } catch (Exception e) {}
1211 loadWidgetPreviewsInBackground(task, data);
1212 } finally {
1213 if (task.isCancelled()) {
1214 data.cleanup(true);
1215 }
1216 }
Winson Chungb44b5242011-06-13 11:32:14 -07001217 }
1218 },
1219 new AsyncTaskCallback() {
1220 @Override
1221 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001222 mRunningTasks.remove(task);
1223 if (task.isCancelled()) return;
1224 // do cleanup inside onSyncWidgetPageItems
1225 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001226 }
Winson Chung09945932011-09-20 14:22:40 -07001227 });
Winson Chungb44b5242011-06-13 11:32:14 -07001228
1229 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001230 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001231 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001232 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001233 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1234 mRunningTasks.add(t);
1235 }
Winson Chungb44b5242011-06-13 11:32:14 -07001236
Winson Chung785d2eb2011-04-14 16:08:02 -07001237 /*
1238 * Widgets PagedView implementation
1239 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001240 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001241 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1242 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001243
1244 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001245 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001246 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1247 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001248 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001249 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001250 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001251
Winson Chung5fc72b32011-10-11 17:53:58 -07001252 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001253 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001254 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001255
Winson Chung5fc72b32011-10-11 17:53:58 -07001256 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001257 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001258 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001259 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001260 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001261 Rect oldBounds = d.copyBounds();
1262 d.setBounds(x, y, x + w, y + h);
1263 d.draw(c);
1264 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001265 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001266 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001267 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001268
Michael Jurkadac85912012-05-18 15:04:49 -07001269 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1270 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1271 final Canvas c = mCachedShortcutPreviewCanvas.get();
1272 if (tempBitmap == null ||
1273 tempBitmap.getWidth() != maxWidth ||
1274 tempBitmap.getHeight() != maxHeight) {
1275 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1276 mCachedShortcutPreviewBitmap.set(tempBitmap);
1277 } else {
1278 c.setBitmap(tempBitmap);
1279 c.drawColor(0, PorterDuff.Mode.CLEAR);
1280 c.setBitmap(null);
1281 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001282 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001283 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001284
1285 int paddingTop =
1286 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1287 int paddingLeft =
1288 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1289 int paddingRight =
1290 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1291
1292 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
1293 float scaleSize = scaledIconWidth / (float) mAppIconSize;
1294
1295 renderDrawableToBitmap(
1296 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1297
1298 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1299 c.setBitmap(preview);
1300 Paint p = mCachedShortcutPreviewPaint.get();
1301 if (p == null) {
1302 p = new Paint();
1303 ColorMatrix colorMatrix = new ColorMatrix();
1304 colorMatrix.setSaturation(0);
1305 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1306 p.setAlpha((int) (255 * 0.06f));
1307 //float density = 1f;
1308 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1309 mCachedShortcutPreviewPaint.set(p);
1310 }
1311 c.drawBitmap(tempBitmap, 0, 0, p);
1312 c.setBitmap(null);
1313
1314 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1315
Winson Chungb44b5242011-06-13 11:32:14 -07001316 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001317 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001318
Michael Jurkadac85912012-05-18 15:04:49 -07001319 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1320 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1321 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001322 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001323 String packageName = provider.getPackageName();
1324 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1325 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001326
Michael Jurka038f9d82011-11-03 13:50:45 -07001327 Drawable drawable = null;
1328 if (previewImage != 0) {
1329 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1330 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001331 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001332 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001333 }
1334 }
1335
Michael Jurka038f9d82011-11-03 13:50:45 -07001336 int bitmapWidth;
1337 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001338 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001339 boolean widgetPreviewExists = (drawable != null);
1340 if (widgetPreviewExists) {
1341 bitmapWidth = drawable.getIntrinsicWidth();
1342 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001343 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001344 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001345 if (cellHSpan < 1) cellHSpan = 1;
1346 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001347
1348 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1349 .getDrawable(R.drawable.widget_preview_tile);
1350 final int previewDrawableWidth = previewDrawable
1351 .getIntrinsicWidth();
1352 final int previewDrawableHeight = previewDrawable
1353 .getIntrinsicHeight();
1354 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1355 bitmapHeight = previewDrawableHeight * cellVSpan;
1356
1357 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1358 Config.ARGB_8888);
1359 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1360 c.setBitmap(defaultPreview);
1361 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1362 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1363 Shader.TileMode.REPEAT);
1364 previewDrawable.draw(c);
1365 c.setBitmap(null);
1366
1367 // Draw the icon in the top left corner
1368 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1369 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1370 float iconScale = Math.min((float) smallestSide
1371 / (mAppIconSize + 2 * minOffset), 1f);
1372
1373 try {
1374 Drawable icon = null;
1375 int hoffset =
1376 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1377 int yoffset =
1378 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1379 if (iconId > 0)
1380 icon = mIconCache.getFullResIcon(packageName, iconId);
1381 Resources resources = mLauncher.getResources();
1382 if (icon != null) {
1383 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1384 yoffset, (int) (mAppIconSize * iconScale),
1385 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001386 }
Michael Jurkadac85912012-05-18 15:04:49 -07001387 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001388 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001389 }
1390
Michael Jurkadac85912012-05-18 15:04:49 -07001391 // Scale to fit width only - let the widget preview be clipped in the
1392 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001393 float scale = 1f;
1394 if (bitmapWidth > maxWidth) {
1395 scale = maxWidth / (float) bitmapWidth;
1396 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001397 if (scale != 1f) {
1398 bitmapWidth = (int) (scale * bitmapWidth);
1399 bitmapHeight = (int) (scale * bitmapHeight);
1400 }
1401
Michael Jurkadac85912012-05-18 15:04:49 -07001402 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1403 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001404
Michael Jurkadac85912012-05-18 15:04:49 -07001405 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001406 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001407 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1408 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001409 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001410 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1411 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1412 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1413 c.setBitmap(preview);
1414 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1415 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1416
1417 Paint p = mCachedAppWidgetPreviewPaint.get();
1418 if (p == null) {
1419 p = new Paint();
1420 p.setFilterBitmap(true);
1421 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001422 }
Michael Jurkadac85912012-05-18 15:04:49 -07001423 c.drawBitmap(defaultPreview, src, dest, p);
1424 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001425 }
Winson Chungb44b5242011-06-13 11:32:14 -07001426 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001428
Michael Jurka038f9d82011-11-03 13:50:45 -07001429 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001430 Log.d(TAG, "6549598 syncWidgetPageItems page: " + page);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001431 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001432
Winson Chungd2945262011-06-24 15:22:14 -07001433 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001434 final ArrayList<Object> items = new ArrayList<Object>();
1435 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1436 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001437 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001438 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1439 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001440 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001441
Winson Chunge4a647f2011-09-30 14:41:25 -07001442 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001443 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001444 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1445 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001446 }
1447
Winson Chunge4a647f2011-09-30 14:41:25 -07001448 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001449 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001450 layout.setColumnCount(layout.getCellCountX());
1451 for (int i = 0; i < items.size(); ++i) {
1452 Object rawInfo = items.get(i);
1453 PendingAddItemInfo createItemInfo = null;
1454 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1455 R.layout.apps_customize_widget, layout, false);
1456 if (rawInfo instanceof AppWidgetProviderInfo) {
1457 // Fill in the widget information
1458 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1459 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001460
1461 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001462 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001463 createItemInfo.spanX = spanXY[0];
1464 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001465 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001466 createItemInfo.minSpanX = minSpanXY[0];
1467 createItemInfo.minSpanY = minSpanXY[1];
1468
1469 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001470 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001471 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001472 } else if (rawInfo instanceof ResolveInfo) {
1473 // Fill in the shortcuts information
1474 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001475 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001476 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1477 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1478 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001479 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001480 widget.setTag(createItemInfo);
1481 }
1482 widget.setOnClickListener(this);
1483 widget.setOnLongClickListener(this);
1484 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001485 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001486
1487 // Layout each widget
1488 int ix = i % mWidgetCountX;
1489 int iy = i / mWidgetCountX;
1490 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1491 GridLayout.spec(iy, GridLayout.LEFT),
1492 GridLayout.spec(ix, GridLayout.TOP));
1493 lp.width = cellWidth;
1494 lp.height = cellHeight;
1495 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1496 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1497 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1498 layout.addView(widget, lp);
1499 }
1500
Michael Jurka038f9d82011-11-03 13:50:45 -07001501 // wait until a call on onLayout to start loading, because
1502 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1503 // TODO: can we do a measure/layout immediately?
1504 layout.setOnLayoutListener(new Runnable() {
1505 public void run() {
1506 // Load the widget previews
1507 int maxPreviewWidth = cellWidth;
1508 int maxPreviewHeight = cellHeight;
1509 if (layout.getChildCount() > 0) {
1510 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1511 int[] maxSize = w.getPreviewSize();
1512 maxPreviewWidth = maxSize[0];
1513 maxPreviewHeight = maxSize[1];
1514 }
1515 if (immediate) {
1516 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1517 maxPreviewWidth, maxPreviewHeight, null, null);
1518 loadWidgetPreviewsInBackground(null, data);
1519 onSyncWidgetPageItems(data);
1520 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001521 if (mInTransition) {
1522 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1523 } else {
1524 prepareLoadWidgetPreviewsTask(page, items,
1525 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1526 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001527 }
1528 }
1529 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001530 }
1531 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1532 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001533 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1534 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001535 if (task != null) {
1536 // Ensure that this task starts running at the correct priority
1537 task.syncThreadPriority();
1538 }
1539
1540 // Load each of the widget/shortcut previews
1541 ArrayList<Object> items = data.items;
1542 ArrayList<Bitmap> images = data.generatedImages;
1543 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001544 for (int i = 0; i < count; ++i) {
1545 if (task != null) {
1546 // Ensure we haven't been cancelled yet
1547 if (task.isCancelled()) break;
1548 // Before work on each item, ensure that this task is running at the correct
1549 // priority
1550 task.syncThreadPriority();
1551 }
1552
1553 Object rawInfo = items.get(i);
1554 if (rawInfo instanceof AppWidgetProviderInfo) {
1555 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001556 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001557
1558 int maxWidth = Math.min(data.maxImageWidth,
1559 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1560 int maxHeight = Math.min(data.maxImageHeight,
1561 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001562 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001563 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001564 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001565 } else if (rawInfo instanceof ResolveInfo) {
1566 // Fill in the shortcuts information
1567 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001568 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001569 }
1570 }
Winson Chungb44b5242011-06-13 11:32:14 -07001571 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001572
Winson Chungb44b5242011-06-13 11:32:14 -07001573 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001574 if (mInTransition) {
1575 mDeferredSyncWidgetPageItems.add(data);
1576 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001577 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001578 try {
1579 int page = data.page;
1580 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001581
Michael Jurka39e5d172012-03-12 18:36:12 -07001582 ArrayList<Object> items = data.items;
1583 int count = items.size();
1584 for (int i = 0; i < count; ++i) {
1585 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1586 if (widget != null) {
1587 Bitmap preview = data.generatedImages.get(i);
1588 widget.applyPreview(new FastBitmapDrawable(preview), i);
1589 }
1590 }
Winson Chung68e4c642011-11-10 15:48:25 -08001591
Michael Jurka39e5d172012-03-12 18:36:12 -07001592 layout.createHardwareLayer();
1593 invalidate();
1594
1595 // Update all thread priorities
1596 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1597 while (iter.hasNext()) {
1598 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1599 int pageIndex = task.page;
1600 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1601 }
1602 } finally {
1603 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001604 }
Winson Chungb44b5242011-06-13 11:32:14 -07001605 }
Winson Chung46af2e82011-05-09 16:00:53 -07001606
Winson Chung785d2eb2011-04-14 16:08:02 -07001607 @Override
1608 public void syncPages() {
1609 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001610 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001611
Adam Cohen0cd3b642011-10-14 14:58:00 -07001612 Context context = getContext();
1613 for (int j = 0; j < mNumWidgetPages; ++j) {
1614 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1615 mWidgetCountY);
1616 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001617 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001618 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001619 }
1620
Adam Cohen0cd3b642011-10-14 14:58:00 -07001621 for (int i = 0; i < mNumAppsPages; ++i) {
1622 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1623 setupPage(layout);
1624 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001625 }
Winson Chung7c7a22d2012-06-01 13:46:48 -07001626
1627 Log.d(TAG, "6549598 syncPages mNumAppsPages: " + mNumAppsPages + " mNumWidgetPages: " + mNumWidgetPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001629
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001631 public void syncPageItems(int page, boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001632 Log.d(TAG, "6549598 syncPageItems page: " + page + " immediate: " + immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001633 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001634 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001635 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001636 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001637 }
1638 }
1639
Adam Cohen22f823d2011-09-01 17:22:18 -07001640 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1641 // it is in the z-order. This is important to insure touch events are handled correctly.
1642 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001643 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001644 }
1645
Adam Cohenae4f1552011-10-20 00:15:42 -07001646 @Override
1647 protected int indexToPage(int index) {
1648 return getChildCount() - index - 1;
1649 }
1650
Adam Cohen22f823d2011-09-01 17:22:18 -07001651 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1652 @Override
1653 protected void screenScrolled(int screenCenter) {
1654 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001655
1656 for (int i = 0; i < getChildCount(); i++) {
1657 View v = getPageAt(i);
1658 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001659 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001660
1661 float interpolatedProgress =
1662 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1663 float scale = (1 - interpolatedProgress) +
1664 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1665 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001666
Adam Cohen2591f6a2011-10-25 14:36:40 -07001667 float alpha;
1668
Winson Chungd167e2a2012-04-26 13:13:01 -07001669 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001670 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001671 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001672 } else {
1673 // On large screens we need to fade the page as it nears its leftmost position
1674 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1675 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001676
1677 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1678 int pageWidth = v.getMeasuredWidth();
1679 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001680
1681 if (PERFORM_OVERSCROLL_ROTATION) {
1682 if (i == 0 && scrollProgress < 0) {
1683 // Overscroll to the left
1684 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1685 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1686 scale = 1.0f;
1687 alpha = 1.0f;
1688 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001689 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001690 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1691 // Overscroll to the right
1692 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1693 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1694 scale = 1.0f;
1695 alpha = 1.0f;
1696 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001697 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001698 } else {
1699 v.setPivotY(pageHeight / 2.0f);
1700 v.setPivotX(pageWidth / 2.0f);
1701 v.setRotationY(0f);
1702 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001703 }
1704
1705 v.setTranslationX(translationX);
1706 v.setScaleX(scale);
1707 v.setScaleY(scale);
1708 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001709
1710 // If the view has 0 alpha, we set it to be invisible so as to prevent
1711 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001712 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001713 v.setVisibility(INVISIBLE);
1714 } else if (v.getVisibility() != VISIBLE) {
1715 v.setVisibility(VISIBLE);
1716 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001717 }
1718 }
1719 }
1720
1721 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001722 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001723 }
1724
Winson Chung785d2eb2011-04-14 16:08:02 -07001725 /**
1726 * Used by the parent to get the content width to set the tab bar to
1727 * @return
1728 */
1729 public int getPageContentWidth() {
1730 return mContentWidth;
1731 }
1732
Winson Chungb26f3d62011-06-02 10:49:29 -07001733 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001734 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001735 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001736 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001737 // We reset the save index when we change pages so that it will be recalculated on next
1738 // rotation
1739 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001740 }
1741
Winson Chung785d2eb2011-04-14 16:08:02 -07001742 /*
1743 * AllAppsView implementation
1744 */
1745 @Override
1746 public void setup(Launcher launcher, DragController dragController) {
1747 mLauncher = launcher;
1748 mDragController = dragController;
1749 }
1750 @Override
1751 public void zoom(float zoom, boolean animate) {
1752 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1753 }
1754 @Override
1755 public boolean isVisible() {
1756 return (getVisibility() == VISIBLE);
1757 }
1758 @Override
1759 public boolean isAnimating() {
1760 return false;
1761 }
1762 @Override
1763 public void setApps(ArrayList<ApplicationInfo> list) {
1764 mApps = list;
1765 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001766 Log.d(TAG, "6549598 setApps mApps.size(): " + mApps.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001767 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001768
Winson Chung875de7e2011-06-28 14:25:17 -07001769 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1770 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001771 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001772 }
1773 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1774 // We add it in place, in alphabetical order
1775 int count = list.size();
1776 for (int i = 0; i < count; ++i) {
1777 ApplicationInfo info = list.get(i);
1778 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1779 if (index < 0) {
1780 mApps.add(-(index + 1), info);
1781 }
1782 }
1783 }
1784 @Override
1785 public void addApps(ArrayList<ApplicationInfo> list) {
1786 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001787 Log.d(TAG, "6549598 addApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001788 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001789 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001790 Log.d(TAG, "6549598 addApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001791 }
1792 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1793 ComponentName removeComponent = item.intent.getComponent();
1794 int length = list.size();
1795 for (int i = 0; i < length; ++i) {
1796 ApplicationInfo info = list.get(i);
1797 if (info.intent.getComponent().equals(removeComponent)) {
1798 return i;
1799 }
1800 }
1801 return -1;
1802 }
1803 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1804 // loop through all the apps and remove apps that have the same component
1805 int length = list.size();
1806 for (int i = 0; i < length; ++i) {
1807 ApplicationInfo info = list.get(i);
1808 int removeIndex = findAppByComponent(mApps, info);
1809 if (removeIndex > -1) {
1810 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001811 }
1812 }
1813 }
1814 @Override
1815 public void removeApps(ArrayList<ApplicationInfo> list) {
1816 removeAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001817 Log.d(TAG, "6549598 removeApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001818 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001819 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001820 Log.d(TAG, "6549598 removeApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001821 }
1822 @Override
1823 public void updateApps(ArrayList<ApplicationInfo> list) {
1824 // We remove and re-add the updated applications list because it's properties may have
1825 // changed (ie. the title), and this will ensure that the items will be in their proper
1826 // place in the list.
1827 removeAppsWithoutInvalidate(list);
1828 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001829 Log.d(TAG, "6549598 updateApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001830 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001831 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001832 Log.d(TAG, "6549598 updateApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001833 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001834
Winson Chung785d2eb2011-04-14 16:08:02 -07001835 @Override
1836 public void reset() {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001837 Log.d(TAG, "6549598 reset");
Winson Chung649668f2012-01-10 13:07:16 -08001838 // If we have reset, then we should not continue to restore the previous state
1839 mSaveInstanceStateItemIndex = -1;
1840
Adam Cohenb64d36e2011-10-17 21:48:02 -07001841 AppsCustomizeTabHost tabHost = getTabHost();
1842 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001843 if (tag != null) {
1844 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1845 tabHost.setCurrentTabFromContent(ContentType.Applications);
1846 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001847 }
Winson Chung649668f2012-01-10 13:07:16 -08001848
Adam Cohenb64d36e2011-10-17 21:48:02 -07001849 if (mCurrentPage != 0) {
1850 invalidatePageData(0);
1851 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001852 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001853
1854 private AppsCustomizeTabHost getTabHost() {
1855 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1856 }
1857
Winson Chung785d2eb2011-04-14 16:08:02 -07001858 @Override
1859 public void dumpState() {
1860 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001861 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1862 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001863 }
Adam Cohen4e844012011-11-09 13:48:04 -08001864
Winson Chung785d2eb2011-04-14 16:08:02 -07001865 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001866 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001867 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001868 for (Object i: list) {
1869 if (i instanceof AppWidgetProviderInfo) {
1870 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1871 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1872 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1873 + " initialLayout=" + info.initialLayout
1874 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1875 } else if (i instanceof ResolveInfo) {
1876 ResolveInfo info = (ResolveInfo) i;
1877 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1878 + info.icon);
1879 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001880 }
1881 }
Adam Cohen4e844012011-11-09 13:48:04 -08001882
Winson Chung785d2eb2011-04-14 16:08:02 -07001883 @Override
1884 public void surrender() {
1885 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1886 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001887
1888 // Stop all background tasks
1889 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001890 }
Winson Chung007c6982011-06-14 13:27:53 -07001891
Winson Chunge4e50662012-01-23 14:45:13 -08001892 @Override
1893 public void iconPressed(PagedViewIcon icon) {
1894 // Reset the previously pressed icon and store a reference to the pressed icon so that
1895 // we can reset it on return to Launcher (in Launcher.onResume())
1896 if (mPressedIcon != null) {
1897 mPressedIcon.resetDrawableState();
1898 }
1899 mPressedIcon = icon;
1900 }
1901
1902 public void resetDrawableState() {
1903 if (mPressedIcon != null) {
1904 mPressedIcon.resetDrawableState();
1905 mPressedIcon = null;
1906 }
1907 }
Winson Chung68e4c642011-11-10 15:48:25 -08001908
Winson Chungb44b5242011-06-13 11:32:14 -07001909 /*
1910 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1911 * widget previews in the background with the AsyncTasks.
1912 */
Winson Chung68e4c642011-11-10 15:48:25 -08001913 final static int sLookBehindPageCount = 2;
1914 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001915 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001916 final int count = getChildCount();
1917 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1918 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1919 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001920 }
1921 protected int getAssociatedUpperPageBound(int page) {
1922 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001923 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1924 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1925 count - 1);
1926 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001927 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001928
1929 @Override
1930 protected String getCurrentPageDescription() {
1931 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1932 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001933 int count = 0;
1934
Adam Cohen0cd3b642011-10-14 14:58:00 -07001935 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001936 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001937 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001938 } else {
1939 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001940 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001941 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001942 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001943
Michael Jurka8b805b12012-04-18 14:23:14 -07001944 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001945 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001946}