blob: 30d3cf8b08519e6346b213c640a28e7ef0477dc5 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070036import android.graphics.ColorMatrix;
37import android.graphics.ColorMatrixColorFilter;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080039import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070040import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070041import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080043import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070044import android.graphics.Shader;
Peter Ng8db70002011-10-25 15:40:08 -070045import android.graphics.TableMaskFilter;
Michael Jurkadac85912012-05-18 15:04:49 -070046import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070048import android.os.AsyncTask;
49import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.util.AttributeSet;
51import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070052import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080053import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070054import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070055import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070056import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070057import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070058import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070059import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070060import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070061import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070062import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070063
64import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065import com.android.launcher2.DropTarget.DragObject;
66
Michael Jurka9bc8eba2012-05-21 20:36:44 -070067import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070068import java.util.ArrayList;
69import java.util.Collections;
70import java.util.Iterator;
71import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070072
Winson Chungb44b5242011-06-13 11:32:14 -070073/**
74 * A simple callback interface which also provides the results of the task.
75 */
76interface AsyncTaskCallback {
77 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
78}
Winson Chung4e076542011-06-23 13:04:10 -070079
Winson Chungb44b5242011-06-13 11:32:14 -070080/**
81 * The data needed to perform either of the custom AsyncTasks.
82 */
83class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070084 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080085 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070086 }
87
Winson Chungb44b5242011-06-13 11:32:14 -070088 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
89 AsyncTaskCallback postR) {
90 page = p;
91 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070092 sourceImages = si;
93 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070094 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070095 doInBackgroundCallback = bgR;
96 postExecuteCallback = postR;
97 }
Michael Jurka038f9d82011-11-03 13:50:45 -070098 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070099 AsyncTaskCallback postR) {
100 page = p;
101 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700102 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700103 maxImageWidth = cw;
104 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 doInBackgroundCallback = bgR;
106 postExecuteCallback = postR;
107 }
Winson Chung09945932011-09-20 14:22:40 -0700108 void cleanup(boolean cancelled) {
109 // Clean up any references to source/generated bitmaps
110 if (sourceImages != null) {
111 if (cancelled) {
112 for (Bitmap b : sourceImages) {
113 b.recycle();
114 }
115 }
116 sourceImages.clear();
117 }
118 if (generatedImages != null) {
119 if (cancelled) {
120 for (Bitmap b : generatedImages) {
121 b.recycle();
122 }
123 }
124 generatedImages.clear();
125 }
126 }
Winson Chungb44b5242011-06-13 11:32:14 -0700127 int page;
128 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700129 ArrayList<Bitmap> sourceImages;
130 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700131 int maxImageWidth;
132 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700133 AsyncTaskCallback doInBackgroundCallback;
134 AsyncTaskCallback postExecuteCallback;
135}
Winson Chung4e076542011-06-23 13:04:10 -0700136
Winson Chungb44b5242011-06-13 11:32:14 -0700137/**
138 * A generic template for an async task used in AppsCustomize.
139 */
140class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700141 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700142 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700143 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700144 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700145 }
146 @Override
147 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
148 if (params.length != 1) return null;
149 // Load each of the widget previews in the background
150 params[0].doInBackgroundCallback.run(this, params[0]);
151 return params[0];
152 }
153 @Override
154 protected void onPostExecute(AsyncTaskPageData result) {
155 // All the widget previews are loaded, so we can just callback to inflate the page
156 result.postExecuteCallback.run(this, result);
157 }
158
159 void setThreadPriority(int p) {
160 threadPriority = p;
161 }
162 void syncThreadPriority() {
163 Process.setThreadPriority(threadPriority);
164 }
165
166 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700167 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700168 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700169 int threadPriority;
170}
Winson Chungb44b5242011-06-13 11:32:14 -0700171
Michael Jurkadac85912012-05-18 15:04:49 -0700172abstract class WeakReferenceThreadLocal<T> {
173 private ThreadLocal<WeakReference<T>> mThreadLocal;
174 public WeakReferenceThreadLocal() {
175 mThreadLocal = new ThreadLocal<WeakReference<T>>();
176 }
177
178 abstract T initialValue();
179
180 public void set(T t) {
181 mThreadLocal.set(new WeakReference<T>(t));
182 }
183
184 public T get() {
185 WeakReference<T> reference = mThreadLocal.get();
186 T obj;
187 if (reference == null) {
188 obj = initialValue();
189 mThreadLocal.set(new WeakReference<T>(obj));
190 return obj;
191 } else {
192 obj = reference.get();
193 if (obj == null) {
194 obj = initialValue();
195 mThreadLocal.set(new WeakReference<T>(obj));
196 }
197 return obj;
198 }
199 }
200}
201
202class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
203 @Override
204 protected Canvas initialValue() {
205 return new Canvas();
206 }
207}
208
209class PaintCache extends WeakReferenceThreadLocal<Paint> {
210 @Override
211 protected Paint initialValue() {
212 return null;
213 }
214}
215
216class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
217 @Override
218 protected Bitmap initialValue() {
219 return null;
220 }
221}
222
223class RectCache extends WeakReferenceThreadLocal<Rect> {
224 @Override
225 protected Rect initialValue() {
226 return new Rect();
227 }
228}
229
Winson Chungb44b5242011-06-13 11:32:14 -0700230/**
231 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
232 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700233public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800234 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700235 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
236 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700237 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700238
239 /**
240 * The different content types that this paged view can show.
241 */
242 public enum ContentType {
243 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700244 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700245 }
246
247 // Refs
248 private Launcher mLauncher;
249 private DragController mDragController;
250 private final LayoutInflater mLayoutInflater;
251 private final PackageManager mPackageManager;
252
Winson Chung5afbf7b2011-07-25 11:53:08 -0700253 // Save and Restore
254 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800255 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700256
Winson Chung785d2eb2011-04-14 16:08:02 -0700257 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700258 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700259 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700260
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800262 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 private int mClingFocusedX;
264 private int mClingFocusedY;
265
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700267 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700268 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700269
270 // Dimens
271 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700272 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800273 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700274 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700275 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700276 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700277 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700278 private int mNumAppsPages;
279 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280
Adam Cohen22f823d2011-09-01 17:22:18 -0700281 // Relating to the scroll and overscroll effects
282 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700283 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700284 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700285 private static float TRANSITION_PIVOT = 0.65f;
286 private static float TRANSITION_MAX_ROTATION = 22;
287 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700288 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700289 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700290
Winson Chungb44b5242011-06-13 11:32:14 -0700291 // Previews & outlines
292 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800293 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700294
Adam Cohened66b2b2012-01-23 17:28:51 -0800295 private Runnable mInflateWidgetRunnable = null;
296 private Runnable mBindWidgetRunnable = null;
297 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700298 static final int WIDGET_PRELOAD_PENDING = 0;
299 static final int WIDGET_BOUND = 1;
300 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800301 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
302 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800303 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800304 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800305
Michael Jurka39e5d172012-03-12 18:36:12 -0700306 // Deferral of loading widget previews during launcher transitions
307 private boolean mInTransition;
308 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
309 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700310 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
311 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700312
Michael Jurkadac85912012-05-18 15:04:49 -0700313 // Used for drawing shortcut previews
314 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
315 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
316 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
317
318 // Used for drawing widget previews
319 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
320 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
321 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
322 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
323
Winson Chung785d2eb2011-04-14 16:08:02 -0700324 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
325 super(context, attrs);
326 mLayoutInflater = LayoutInflater.from(context);
327 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700328 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700329 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700330 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700331 mCanvas = new Canvas();
332 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700333
334 // Save the default widget preview background
335 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700336 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700337
Winson Chung6032e7e2011-11-08 15:47:17 -0800338 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
339 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
340 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700341 mWidgetWidthGap =
342 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
343 mWidgetHeightGap =
344 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700345 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
346 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700347 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
348 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700349 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700350 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700351
Winson Chung1ed747a2011-05-03 16:18:34 -0700352 // The padding on the non-matched dimension for the default widget preview icons
353 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700354 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700355
356 // Unless otherwise specified this view is important for accessibility.
357 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
358 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
359 }
Winson Chung7c7a22d2012-06-01 13:46:48 -0700360
361 Log.d(TAG, "6549598 ctor mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700362 }
363
364 @Override
365 protected void init() {
366 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700367 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700368
369 Context context = getContext();
370 Resources r = context.getResources();
371 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
Winson Chung7c7a22d2012-06-01 13:46:48 -0700372 Log.d(TAG, "6549598 init mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -0700373 }
374
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700376 protected void onUnhandledTap(MotionEvent ev) {
377 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700378 // Dismiss AppsCustomize if we tap
379 mLauncher.showWorkspace(true);
380 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700381 }
382
Winson Chung5afbf7b2011-07-25 11:53:08 -0700383 /** Returns the item index of the center item on this page so that we can restore to this
384 * item index when we rotate. */
385 private int getMiddleComponentIndexOnCurrentPage() {
386 int i = -1;
387 if (getPageCount() > 0) {
388 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700389 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700390 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700391 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
392 int numItemsPerPage = mCellCountX * mCellCountY;
393 int childCount = childrenLayout.getChildCount();
394 if (childCount > 0) {
395 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700396 }
397 } else {
398 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700399 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700400 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
401 int childCount = layout.getChildCount();
402 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700403 i = numApps +
404 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
405 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700406 }
407 }
408 return i;
409 }
410
411 /** Get the index of the item to restore to if we need to restore the current page. */
412 int getSaveInstanceStateIndex() {
413 if (mSaveInstanceStateItemIndex == -1) {
414 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
415 }
416 return mSaveInstanceStateItemIndex;
417 }
418
419 /** Returns the page in the current orientation which is expected to contain the specified
420 * item index. */
421 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700422 if (index < 0) return 0;
423
424 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700425 int numItemsPerPage = mCellCountX * mCellCountY;
426 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700427 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700428 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700429 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
430 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700431 }
432
Winson Chungf0ea4d32011-06-06 14:27:16 -0700433 /**
434 * This differs from isDataReady as this is the test done if isDataReady is not set.
435 */
436 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700437 // We only do this test once, and we default to the Applications page, so we only really
438 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700439 // TODO: What if one of them is validly empty
440 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700441 }
442
Winson Chung5afbf7b2011-07-25 11:53:08 -0700443 /** Restores the page for an item at the specified index */
444 void restorePageForIndex(int index) {
445 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700446 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700447 }
448
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700449 private void updatePageCounts() {
450 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
451 (float) (mWidgetCountX * mWidgetCountY));
452 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
Winson Chung7c7a22d2012-06-01 13:46:48 -0700453 Log.d(TAG, "6549598 updatePageCounts mNumWidgetPages: " + mNumWidgetPages + " mNumAppsPages: " + mNumAppsPages);
454 Log.d(TAG, "6549598 mApps.size(): " + mApps.size() + " mWidgets.size(): " + mWidgets.size() + " mCellCountX: " + mCellCountX + " mCellCountY: " + mCellCountY);
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700455 }
456
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 protected void onDataReady(int width, int height) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700458 Log.d(TAG, "6549598 onDataReady");
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 // Note that we transpose the counts in portrait so that we get a similar layout
460 boolean isLandscape = getResources().getConfiguration().orientation ==
461 Configuration.ORIENTATION_LANDSCAPE;
462 int maxCellCountX = Integer.MAX_VALUE;
463 int maxCellCountY = Integer.MAX_VALUE;
464 if (LauncherApplication.isScreenLarge()) {
465 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
466 LauncherModel.getCellCountY());
467 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
468 LauncherModel.getCellCountX());
469 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800470 if (mMaxAppCellCountX > -1) {
471 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
472 }
473 if (mMaxAppCellCountY > -1) {
474 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
475 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700476
477 // Now that the data is ready, we can calculate the content width, the number of cells to
478 // use for each page
479 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
480 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
481 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
482 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
483 mCellCountX = mWidgetSpacingLayout.getCellCountX();
484 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700485 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700486
Winson Chungdb1138b2011-06-30 14:39:35 -0700487 // Force a measure to update recalculate the gaps
488 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
489 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
490 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700491 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700492
Michael Jurkae326f182011-11-21 14:05:46 -0800493 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
494 final boolean hostIsTransitioning = host.isTransitioning();
495
Adam Cohen0cd3b642011-10-14 14:58:00 -0700496 // Restore the page
497 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800498 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700499
Winson Chung3f4e1422011-11-17 14:58:51 -0800500 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
501 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
502 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800503 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800504 post(new Runnable() {
505 @Override
506 public void run() {
507 showAllAppsCling();
508 }
509 });
510 }
511 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700512
Winson Chung3f4e1422011-11-17 14:58:51 -0800513 void showAllAppsCling() {
514 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
515 mHasShownAllAppsCling = true;
516 // Calculate the position for the cling punch through
517 int[] offset = new int[2];
518 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
519 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
520 // PagedViews are centered horizontally but top aligned
521 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
522 offset[0];
523 pos[1] += offset[1];
524 mLauncher.showFirstRunAllAppsCling(pos);
525 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 }
527
528 @Override
529 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
530 int width = MeasureSpec.getSize(widthMeasureSpec);
531 int height = MeasureSpec.getSize(heightMeasureSpec);
532 if (!isDataReady()) {
533 if (testDataReady()) {
534 setDataIsReady();
535 setMeasuredDimension(width, height);
536 onDataReady(width, height);
537 }
538 }
539
540 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
541 }
542
Winson Chung785d2eb2011-04-14 16:08:02 -0700543 public void onPackagesUpdated() {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700544 Log.d(TAG, "6549598 onPackagesUpdated");
Adam Cohenb0fa3522011-08-17 16:10:46 -0700545 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
546 // by a broadcast receiver, and in order for it to work correctly, we need to know that
547 // the AppWidgetService has already received and processed the same broadcast. Since there
Winson Chungfd39d8e2012-06-05 10:12:48 -0700548 // is no guarantee about ordering of broadcast receipt, we just delay here. This is a
549 // workaround until we add a callback from AppWidgetService to AppWidgetHost when widget
550 // packages are added, updated or removed.
Adam Cohenb0fa3522011-08-17 16:10:46 -0700551 postDelayed(new Runnable() {
552 public void run() {
553 updatePackages();
554 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700555 }, 1500);
Adam Cohenb0fa3522011-08-17 16:10:46 -0700556 }
557
558 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700559 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700560 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700561 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700562 List<AppWidgetProviderInfo> widgets =
563 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700564 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
565 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800566 for (AppWidgetProviderInfo widget : widgets) {
567 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700568 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700569 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
570 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700571 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
572 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700573 if (minSpanX <= LauncherModel.getCellCountX() &&
574 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700575 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700576 } else {
577 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
578 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700579 }
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 Cohened66b2b2012-01-23 17:28:51 -0800659 final AppWidgetProviderInfo pInfo = info.info;
660 if (pInfo.configure != null) {
661 return;
662 }
663
Adam Cohen21a170b2012-05-30 15:17:06 -0700664 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 mBindWidgetRunnable = new Runnable() {
666 @Override
667 public void run() {
668 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700669 if (AppWidgetManager.getInstance(mLauncher)
670 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
671 mWidgetCleanupState = WIDGET_BOUND;
672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 }
674 };
675 post(mBindWidgetRunnable);
676
677 mInflateWidgetRunnable = new Runnable() {
678 @Override
679 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 AppWidgetHostView hostView = mLauncher.
681 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 info.boundWidget = hostView;
683 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800684 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700685 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
686 info.spanY, info, false);
687
688 // We want the first widget layout to be the correct size. This will be important
689 // for width size reporting to the AppWidgetManager.
690 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
691 unScaledSize[1]);
692 lp.x = lp.y = 0;
693 lp.customPosition = true;
694 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800695 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 }
697 };
698 post(mInflateWidgetRunnable);
699 }
700
701 @Override
702 public void onShortPress(View v) {
703 // We are anticipating a long press, and we use this time to load bind and instantiate
704 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700705 if (mCreateWidgetInfo != null) {
706 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
707 cleanupWidgetPreloading(false);
708 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800709 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700710 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 }
712
Adam Cohen0e56cc92012-05-11 15:57:05 -0700713 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
714 if (!widgetWasAdded) {
715 // If the widget was not added, we may need to do further cleanup.
716 PendingAddWidgetInfo info = mCreateWidgetInfo;
717 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700718
719 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700720 // We never did any preloading, so just remove pending callbacks to do so
721 removeCallbacks(mBindWidgetRunnable);
722 removeCallbacks(mInflateWidgetRunnable);
723 } else if (mWidgetCleanupState == WIDGET_BOUND) {
724 // Delete the widget id which was allocated
725 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700726 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
727 }
728
729 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700730 removeCallbacks(mInflateWidgetRunnable);
731 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700732 // Delete the widget id which was allocated
733 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700734 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
735 }
736
Adam Cohen0e56cc92012-05-11 15:57:05 -0700737 // The widget was inflated and added to the DragLayer -- remove it.
738 AppWidgetHostView widget = info.boundWidget;
739 mLauncher.getDragLayer().removeView(widget);
740 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 }
742 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
743 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700744 mCreateWidgetInfo = null;
745 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 }
747
Adam Cohen7a326642012-02-22 12:03:22 -0800748 @Override
749 public void cleanUpShortPress(View v) {
750 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700751 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800752 }
753 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800754
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700755 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800756 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700757 // Get the widget preview as the drag representation
758 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700759 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700760
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700761 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
762 // we abort the drag.
763 if (image.getDrawable() == null) {
764 mDraggingWidget = false;
765 return false;
766 }
767
Winson Chung4b576be2011-04-27 17:40:20 -0700768 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800769 Bitmap preview;
770 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800771 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700772 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700773 // This can happen in some weird cases involving multi-touch. We can't start dragging
774 // the widget if this is null, so we break out.
775 if (mCreateWidgetInfo == null) {
776 return false;
777 }
778
Adam Cohen1b36dc32012-02-13 19:27:37 -0800779 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
780 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700781 int spanX = createItemInfo.spanX;
782 int spanY = createItemInfo.spanY;
783 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
784 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700785
Winson Chung72d59842012-02-22 13:51:36 -0800786 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
787 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700788 int maxWidth, maxHeight;
789 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
790 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800791 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700792 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800793
794 // Determine the image view drawable scale relative to the preview
795 float[] mv = new float[9];
796 Matrix m = new Matrix();
797 m.setRectToRect(
798 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
799 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
800 (float) previewDrawable.getIntrinsicHeight()),
801 Matrix.ScaleToFit.START);
802 m.getValues(mv);
803 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700804 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700805 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
806 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
807 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
808 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
809
Winson Chung1120e032011-11-22 16:11:31 -0800810 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700811 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700812 renderDrawableToBitmap(icon, preview, 0, 0,
813 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700814 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700815 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700816 createItemInfo.spanX = createItemInfo.spanY = 1;
817 }
Winson Chung4b576be2011-04-27 17:40:20 -0700818
Peter Ng8db70002011-10-25 15:40:08 -0700819 // We use a custom alpha clip table for the default widget previews
820 Paint alphaClipPaint = null;
821 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700822 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700823 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
824 alphaClipPaint = new Paint();
825 alphaClipPaint.setMaskFilter(alphaClipTable);
826 }
827 }
828
Winson Chung1120e032011-11-22 16:11:31 -0800829 // Save the preview for the outline generation, then dim the preview
830 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
831 false);
Winson Chung1120e032011-11-22 16:11:31 -0800832
Winson Chung4b576be2011-04-27 17:40:20 -0700833 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800834 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700835 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800836 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
837 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800838 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800839 outline.recycle();
840 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700841 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843
Winson Chung4b576be2011-04-27 17:40:20 -0700844 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700846 if (!super.beginDragging(v)) return false;
847
848 if (v instanceof PagedViewIcon) {
849 beginDraggingApplication(v);
850 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700851 if (!beginDraggingWidget(v)) {
852 return false;
853 }
Winson Chung4b576be2011-04-27 17:40:20 -0700854 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800855
856 // We delay entering spring-loaded mode slightly to make sure the UI
857 // thready is free of any work.
858 postDelayed(new Runnable() {
859 @Override
860 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800861 // We don't enter spring-loaded mode if the drag has been cancelled
862 if (mLauncher.getDragController().isDragging()) {
863 // Dismiss the cling
864 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800865
Adam Cohen1b36dc32012-02-13 19:27:37 -0800866 // Reset the alpha on the dragged icon before we drag
867 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800868
Adam Cohen1b36dc32012-02-13 19:27:37 -0800869 // Go into spring loaded mode (must happen before we startDrag())
870 mLauncher.enterSpringLoadedDragMode();
871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 }
Winson Chung72d59842012-02-22 13:51:36 -0800873 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800874
Winson Chung785d2eb2011-04-14 16:08:02 -0700875 return true;
876 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800877
Winson Chunga48487a2012-03-20 16:19:37 -0700878 /**
879 * Clean up after dragging.
880 *
881 * @param target where the item was dragged to (can be null if the item was flung)
882 */
883 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700884 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700885 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700886 // Exit spring loaded mode if we have not successfully dropped or have not handled the
887 // drop in Workspace
888 mLauncher.exitSpringLoadedDragMode();
889 }
Winson Chung4b919f82012-05-01 10:44:08 -0700890 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700891 }
892
Winson Chung785d2eb2011-04-14 16:08:02 -0700893 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700894 public View getContent() {
895 return null;
896 }
897
898 @Override
899 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700900 mInTransition = true;
901 if (toWorkspace) {
902 cancelAllTasks();
903 }
904 }
905
906 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700907 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700908 }
909
910 @Override
911 public void onLauncherTransitionStep(Launcher l, float t) {
912 }
913
914 @Override
915 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Winson Chungbfeac062012-06-06 15:56:08 -0700916 Log.d(TAG, "6549598 onLauncherTransitionEnd mDeferredSyncWidgetPageItems.size(): " + mDeferredSyncWidgetPageItems.size());
Michael Jurka39e5d172012-03-12 18:36:12 -0700917 mInTransition = false;
918 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
919 onSyncWidgetPageItems(d);
920 }
921 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700922 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
923 r.run();
924 }
925 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700926 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700927 }
928
929 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700930 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
931 boolean success) {
932 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
933 if (isFlingToDelete) return;
934
935 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700936
937 // Display an error message if the drag failed due to there not being enough space on the
938 // target layout we were dropping on.
939 if (!success) {
940 boolean showOutOfSpaceMessage = false;
941 if (target instanceof Workspace) {
942 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
943 Workspace workspace = (Workspace) target;
944 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700945 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700946 if (layout != null) {
947 layout.calculateSpans(itemInfo);
948 showOutOfSpaceMessage =
949 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
950 }
951 }
Winson Chungfc79c802011-05-02 13:35:34 -0700952 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700953 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700954 }
Adam Cohen7a326642012-02-22 12:03:22 -0800955
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800956 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700957 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700958 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800959 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 }
961
Winson Chunga48487a2012-03-20 16:19:37 -0700962 @Override
963 public void onFlingToDeleteCompleted() {
964 // We just dismiss the drag when we fling, so cleanup here
965 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700966 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700967 mDraggingWidget = false;
968 }
969
970 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800971 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700972 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800973 }
974
Winson Chung7f0acdd2011-09-19 18:34:19 -0700975 @Override
976 protected void onDetachedFromWindow() {
977 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700978 cancelAllTasks();
979 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700980
Michael Jurkae326f182011-11-21 14:05:46 -0800981 public void clearAllWidgetPages() {
982 cancelAllTasks();
983 int count = getChildCount();
984 for (int i = 0; i < count; i++) {
985 View v = getPageAt(i);
986 if (v instanceof PagedViewGridLayout) {
987 ((PagedViewGridLayout) v).removeAllViewsOnPage();
988 mDirtyPageContent.set(i, true);
989 }
990 }
991 }
992
Adam Cohen0cd3b642011-10-14 14:58:00 -0700993 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700994 // Clean up all the async tasks
995 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
996 while (iter.hasNext()) {
997 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
998 task.cancel(false);
999 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -07001000 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -07001001
1002 // We've already preallocated the views for the data to load into, so clear them as well
1003 View v = getPageAt(task.page);
1004 if (v instanceof PagedViewGridLayout) {
1005 ((PagedViewGridLayout) v).removeAllViewsOnPage();
1006 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001007 }
Winson Chung83687b12012-04-25 16:01:01 -07001008 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -07001009 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -07001010 }
1011
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 public void setContentType(ContentType type) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001013 Log.d(TAG, "6549598 setContentType mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001014 if (type == ContentType.Widgets) {
1015 invalidatePageData(mNumAppsPages, true);
1016 } else if (type == ContentType.Applications) {
1017 invalidatePageData(0, true);
1018 }
Winson Chungb44b5242011-06-13 11:32:14 -07001019 }
1020
Adam Cohen0cd3b642011-10-14 14:58:00 -07001021 protected void snapToPage(int whichPage, int delta, int duration) {
1022 super.snapToPage(whichPage, delta, duration);
1023 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001024
1025 // Update the thread priorities given the direction lookahead
1026 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1027 while (iter.hasNext()) {
1028 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001029 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001030 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1031 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1032 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1033 } else {
1034 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1035 }
1036 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001037 }
1038
1039 private void updateCurrentTab(int currentPage) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001040 Log.d(TAG, "6549598 updateCurrentTab mNumAppsPages: " + mNumAppsPages);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001041 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001042 if (tabHost != null) {
1043 String tag = tabHost.getCurrentTabTag();
1044 if (tag != null) {
1045 if (currentPage >= mNumAppsPages &&
1046 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1047 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1048 } else if (currentPage < mNumAppsPages &&
1049 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1050 tabHost.setCurrentTabFromContent(ContentType.Applications);
1051 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001052 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001053 }
1054 }
1055
Winson Chung785d2eb2011-04-14 16:08:02 -07001056 /*
1057 * Apps PagedView implementation
1058 */
Winson Chung63257c12011-05-05 17:06:13 -07001059 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1060 int childCount = layout.getChildCount();
1061 for (int i = 0; i < childCount; ++i) {
1062 layout.getChildAt(i).setVisibility(visibility);
1063 }
1064 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001065 private void setupPage(PagedViewCellLayout layout) {
1066 layout.setCellCount(mCellCountX, mCellCountY);
1067 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1068 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1069 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1070
Winson Chung63257c12011-05-05 17:06:13 -07001071 // Note: We force a measure here to get around the fact that when we do layout calculations
1072 // immediately after syncing, we don't have a proper width. That said, we already know the
1073 // expected page width, so we can actually optimize by hiding all the TextView-based
1074 // children that are expensive to measure, and let that happen naturally later.
1075 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001076 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001077 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001078 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001080 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001081 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001082
Winson Chungf314b0e2011-08-16 11:54:27 -07001083 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chungbfeac062012-06-06 15:56:08 -07001084 Log.d(TAG, "6549598 syncAppsPageItems page: " + page + " mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001085 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001086 int numCells = mCellCountX * mCellCountY;
1087 int startIndex = page * numCells;
1088 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001089 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001090
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001092 ArrayList<Object> items = new ArrayList<Object>();
1093 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001094 for (int i = startIndex; i < endIndex; ++i) {
1095 ApplicationInfo info = mApps.get(i);
1096 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1097 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001098 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001099 icon.setOnClickListener(this);
1100 icon.setOnLongClickListener(this);
1101 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001102 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001103
1104 int index = i - startIndex;
1105 int x = index % mCellCountX;
1106 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001107 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001108
1109 items.add(info);
1110 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001111 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001112
Winson Chungf0ea4d32011-06-06 14:27:16 -07001113 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 }
Winson Chungb44b5242011-06-13 11:32:14 -07001115
1116 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001117 * A helper to return the priority for loading of the specified widget page.
1118 */
1119 private int getWidgetPageLoadPriority(int page) {
1120 // If we are snapping to another page, use that index as the target page index
1121 int toPage = mCurrentPage;
1122 if (mNextPage > -1) {
1123 toPage = mNextPage;
1124 }
1125
1126 // We use the distance from the target page as an initial guess of priority, but if there
1127 // are no pages of higher priority than the page specified, then bump up the priority of
1128 // the specified page.
1129 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1130 int minPageDiff = Integer.MAX_VALUE;
1131 while (iter.hasNext()) {
1132 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001133 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001134 }
1135
1136 int rawPageDiff = Math.abs(page - toPage);
1137 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1138 }
1139 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001140 * Return the appropriate thread priority for loading for a given page (we give the current
1141 * page much higher priority)
1142 */
1143 private int getThreadPriorityForPage(int page) {
1144 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001145 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001146 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001147 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001148 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001149 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001150 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001151 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001152 }
1153 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001154 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001155 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001156 return Math.max(0, pageDiff * sPageSleepDelay);
1157 }
Winson Chungb44b5242011-06-13 11:32:14 -07001158 /**
1159 * Creates and executes a new AsyncTask to load a page of widget previews.
1160 */
1161 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001162 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001163
Winson Chungb44b5242011-06-13 11:32:14 -07001164 // Prune all tasks that are no longer needed
1165 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1166 while (iter.hasNext()) {
1167 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001168 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001169 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1170 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001171 task.cancel(false);
1172 iter.remove();
1173 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001174 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001175 }
1176 }
1177
Winson Chungf314b0e2011-08-16 11:54:27 -07001178 // 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 -07001179 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001180 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001182 @Override
1183 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001184 try {
Winson Chung09945932011-09-20 14:22:40 -07001185 try {
1186 Thread.sleep(sleepMs);
1187 } catch (Exception e) {}
1188 loadWidgetPreviewsInBackground(task, data);
1189 } finally {
1190 if (task.isCancelled()) {
1191 data.cleanup(true);
1192 }
1193 }
Winson Chungb44b5242011-06-13 11:32:14 -07001194 }
1195 },
1196 new AsyncTaskCallback() {
1197 @Override
1198 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001199 mRunningTasks.remove(task);
1200 if (task.isCancelled()) return;
1201 // do cleanup inside onSyncWidgetPageItems
1202 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001203 }
Winson Chung09945932011-09-20 14:22:40 -07001204 });
Winson Chungb44b5242011-06-13 11:32:14 -07001205
1206 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001207 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001208 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001209 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001210 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1211 mRunningTasks.add(t);
1212 }
Winson Chungb44b5242011-06-13 11:32:14 -07001213
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 /*
1215 * Widgets PagedView implementation
1216 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001217 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001218 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1219 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001220
1221 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001222 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001223 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1224 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001225 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001226 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001227 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001228
Winson Chung5fc72b32011-10-11 17:53:58 -07001229 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001230 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001231 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001232
Winson Chung5fc72b32011-10-11 17:53:58 -07001233 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001234 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001235 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001236 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001237 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001238 Rect oldBounds = d.copyBounds();
1239 d.setBounds(x, y, x + w, y + h);
1240 d.draw(c);
1241 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001242 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001243 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001244 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001245
Michael Jurkadac85912012-05-18 15:04:49 -07001246 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1247 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1248 final Canvas c = mCachedShortcutPreviewCanvas.get();
1249 if (tempBitmap == null ||
1250 tempBitmap.getWidth() != maxWidth ||
1251 tempBitmap.getHeight() != maxHeight) {
1252 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1253 mCachedShortcutPreviewBitmap.set(tempBitmap);
1254 } else {
1255 c.setBitmap(tempBitmap);
1256 c.drawColor(0, PorterDuff.Mode.CLEAR);
1257 c.setBitmap(null);
1258 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001259 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001260 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001261
1262 int paddingTop =
1263 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1264 int paddingLeft =
1265 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1266 int paddingRight =
1267 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1268
1269 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001270
1271 renderDrawableToBitmap(
1272 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1273
1274 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1275 c.setBitmap(preview);
1276 Paint p = mCachedShortcutPreviewPaint.get();
1277 if (p == null) {
1278 p = new Paint();
1279 ColorMatrix colorMatrix = new ColorMatrix();
1280 colorMatrix.setSaturation(0);
1281 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1282 p.setAlpha((int) (255 * 0.06f));
1283 //float density = 1f;
1284 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1285 mCachedShortcutPreviewPaint.set(p);
1286 }
1287 c.drawBitmap(tempBitmap, 0, 0, p);
1288 c.setBitmap(null);
1289
1290 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1291
Winson Chungb44b5242011-06-13 11:32:14 -07001292 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001293 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001294
Michael Jurkadac85912012-05-18 15:04:49 -07001295 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1296 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1297 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001298 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001299 String packageName = provider.getPackageName();
1300 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1301 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001302
Michael Jurka038f9d82011-11-03 13:50:45 -07001303 Drawable drawable = null;
1304 if (previewImage != 0) {
1305 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1306 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001307 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001308 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001309 }
1310 }
1311
Michael Jurka038f9d82011-11-03 13:50:45 -07001312 int bitmapWidth;
1313 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001314 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001315 boolean widgetPreviewExists = (drawable != null);
1316 if (widgetPreviewExists) {
1317 bitmapWidth = drawable.getIntrinsicWidth();
1318 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001319 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001320 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001321 if (cellHSpan < 1) cellHSpan = 1;
1322 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001323
1324 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1325 .getDrawable(R.drawable.widget_preview_tile);
1326 final int previewDrawableWidth = previewDrawable
1327 .getIntrinsicWidth();
1328 final int previewDrawableHeight = previewDrawable
1329 .getIntrinsicHeight();
1330 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1331 bitmapHeight = previewDrawableHeight * cellVSpan;
1332
1333 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1334 Config.ARGB_8888);
1335 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1336 c.setBitmap(defaultPreview);
1337 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1338 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1339 Shader.TileMode.REPEAT);
1340 previewDrawable.draw(c);
1341 c.setBitmap(null);
1342
1343 // Draw the icon in the top left corner
1344 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1345 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1346 float iconScale = Math.min((float) smallestSide
1347 / (mAppIconSize + 2 * minOffset), 1f);
1348
1349 try {
1350 Drawable icon = null;
1351 int hoffset =
1352 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1353 int yoffset =
1354 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1355 if (iconId > 0)
1356 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001357 if (icon != null) {
1358 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1359 yoffset, (int) (mAppIconSize * iconScale),
1360 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001361 }
Michael Jurkadac85912012-05-18 15:04:49 -07001362 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001363 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001364 }
1365
Michael Jurkadac85912012-05-18 15:04:49 -07001366 // Scale to fit width only - let the widget preview be clipped in the
1367 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001368 float scale = 1f;
1369 if (bitmapWidth > maxWidth) {
1370 scale = maxWidth / (float) bitmapWidth;
1371 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001372 if (scale != 1f) {
1373 bitmapWidth = (int) (scale * bitmapWidth);
1374 bitmapHeight = (int) (scale * bitmapHeight);
1375 }
1376
Michael Jurkadac85912012-05-18 15:04:49 -07001377 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1378 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001379
Michael Jurkadac85912012-05-18 15:04:49 -07001380 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001381 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001382 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1383 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001384 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001385 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1386 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1387 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1388 c.setBitmap(preview);
1389 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1390 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1391
1392 Paint p = mCachedAppWidgetPreviewPaint.get();
1393 if (p == null) {
1394 p = new Paint();
1395 p.setFilterBitmap(true);
1396 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001397 }
Michael Jurkadac85912012-05-18 15:04:49 -07001398 c.drawBitmap(defaultPreview, src, dest, p);
1399 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001400 }
Winson Chungb44b5242011-06-13 11:32:14 -07001401 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001402 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001403
Michael Jurka038f9d82011-11-03 13:50:45 -07001404 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001405 Log.d(TAG, "6549598 syncWidgetPageItems page: " + page);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001406 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001407
Winson Chungd2945262011-06-24 15:22:14 -07001408 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001409 final ArrayList<Object> items = new ArrayList<Object>();
1410 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1411 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001412 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001413 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1414 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001415 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001416
Winson Chunge4a647f2011-09-30 14:41:25 -07001417 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001418 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001419 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1420 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001421 }
1422
Winson Chunge4a647f2011-09-30 14:41:25 -07001423 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001424 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001425 layout.setColumnCount(layout.getCellCountX());
1426 for (int i = 0; i < items.size(); ++i) {
1427 Object rawInfo = items.get(i);
1428 PendingAddItemInfo createItemInfo = null;
1429 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1430 R.layout.apps_customize_widget, layout, false);
1431 if (rawInfo instanceof AppWidgetProviderInfo) {
1432 // Fill in the widget information
1433 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1434 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001435
1436 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001437 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001438 createItemInfo.spanX = spanXY[0];
1439 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001440 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001441 createItemInfo.minSpanX = minSpanXY[0];
1442 createItemInfo.minSpanY = minSpanXY[1];
1443
1444 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001445 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001446 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001447 } else if (rawInfo instanceof ResolveInfo) {
1448 // Fill in the shortcuts information
1449 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001450 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001451 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1452 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1453 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001454 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001455 widget.setTag(createItemInfo);
1456 }
1457 widget.setOnClickListener(this);
1458 widget.setOnLongClickListener(this);
1459 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001460 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001461
1462 // Layout each widget
1463 int ix = i % mWidgetCountX;
1464 int iy = i / mWidgetCountX;
1465 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1466 GridLayout.spec(iy, GridLayout.LEFT),
1467 GridLayout.spec(ix, GridLayout.TOP));
1468 lp.width = cellWidth;
1469 lp.height = cellHeight;
1470 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1471 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1472 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1473 layout.addView(widget, lp);
1474 }
1475
Michael Jurka038f9d82011-11-03 13:50:45 -07001476 // wait until a call on onLayout to start loading, because
1477 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1478 // TODO: can we do a measure/layout immediately?
1479 layout.setOnLayoutListener(new Runnable() {
1480 public void run() {
1481 // Load the widget previews
1482 int maxPreviewWidth = cellWidth;
1483 int maxPreviewHeight = cellHeight;
1484 if (layout.getChildCount() > 0) {
1485 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1486 int[] maxSize = w.getPreviewSize();
1487 maxPreviewWidth = maxSize[0];
1488 maxPreviewHeight = maxSize[1];
1489 }
1490 if (immediate) {
1491 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1492 maxPreviewWidth, maxPreviewHeight, null, null);
1493 loadWidgetPreviewsInBackground(null, data);
1494 onSyncWidgetPageItems(data);
1495 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001496 if (mInTransition) {
1497 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1498 } else {
1499 prepareLoadWidgetPreviewsTask(page, items,
1500 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1501 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001502 }
1503 }
1504 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001505 }
1506 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1507 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001508 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1509 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001510 if (task != null) {
1511 // Ensure that this task starts running at the correct priority
1512 task.syncThreadPriority();
1513 }
1514
1515 // Load each of the widget/shortcut previews
1516 ArrayList<Object> items = data.items;
1517 ArrayList<Bitmap> images = data.generatedImages;
1518 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001519 for (int i = 0; i < count; ++i) {
1520 if (task != null) {
1521 // Ensure we haven't been cancelled yet
1522 if (task.isCancelled()) break;
1523 // Before work on each item, ensure that this task is running at the correct
1524 // priority
1525 task.syncThreadPriority();
1526 }
1527
1528 Object rawInfo = items.get(i);
1529 if (rawInfo instanceof AppWidgetProviderInfo) {
1530 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001531 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001532
1533 int maxWidth = Math.min(data.maxImageWidth,
1534 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1535 int maxHeight = Math.min(data.maxImageHeight,
1536 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001537 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001538 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001539 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001540 } else if (rawInfo instanceof ResolveInfo) {
1541 // Fill in the shortcuts information
1542 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001543 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001544 }
1545 }
Winson Chungb44b5242011-06-13 11:32:14 -07001546 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001547
Winson Chungb44b5242011-06-13 11:32:14 -07001548 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001549 if (mInTransition) {
1550 mDeferredSyncWidgetPageItems.add(data);
1551 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001552 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001553 try {
1554 int page = data.page;
1555 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001556
Michael Jurka39e5d172012-03-12 18:36:12 -07001557 ArrayList<Object> items = data.items;
1558 int count = items.size();
1559 for (int i = 0; i < count; ++i) {
1560 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1561 if (widget != null) {
1562 Bitmap preview = data.generatedImages.get(i);
1563 widget.applyPreview(new FastBitmapDrawable(preview), i);
1564 }
1565 }
Winson Chung68e4c642011-11-10 15:48:25 -08001566
Michael Jurka39e5d172012-03-12 18:36:12 -07001567 layout.createHardwareLayer();
1568 invalidate();
1569
1570 // Update all thread priorities
1571 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1572 while (iter.hasNext()) {
1573 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1574 int pageIndex = task.page;
1575 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1576 }
1577 } finally {
1578 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001579 }
Winson Chungb44b5242011-06-13 11:32:14 -07001580 }
Winson Chung46af2e82011-05-09 16:00:53 -07001581
Winson Chung785d2eb2011-04-14 16:08:02 -07001582 @Override
1583 public void syncPages() {
1584 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001585 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001586
Adam Cohen0cd3b642011-10-14 14:58:00 -07001587 Context context = getContext();
1588 for (int j = 0; j < mNumWidgetPages; ++j) {
1589 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1590 mWidgetCountY);
1591 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001592 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001593 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001594 }
1595
Adam Cohen0cd3b642011-10-14 14:58:00 -07001596 for (int i = 0; i < mNumAppsPages; ++i) {
1597 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1598 setupPage(layout);
1599 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 }
Winson Chung7c7a22d2012-06-01 13:46:48 -07001601
1602 Log.d(TAG, "6549598 syncPages mNumAppsPages: " + mNumAppsPages + " mNumWidgetPages: " + mNumWidgetPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001603 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001604
Winson Chung785d2eb2011-04-14 16:08:02 -07001605 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001606 public void syncPageItems(int page, boolean immediate) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001607 Log.d(TAG, "6549598 syncPageItems page: " + page + " immediate: " + immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001608 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001609 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001610 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001611 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001612 }
1613 }
1614
Adam Cohen22f823d2011-09-01 17:22:18 -07001615 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1616 // it is in the z-order. This is important to insure touch events are handled correctly.
1617 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001618 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001619 }
1620
Adam Cohenae4f1552011-10-20 00:15:42 -07001621 @Override
1622 protected int indexToPage(int index) {
1623 return getChildCount() - index - 1;
1624 }
1625
Adam Cohen22f823d2011-09-01 17:22:18 -07001626 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1627 @Override
1628 protected void screenScrolled(int screenCenter) {
1629 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001630
1631 for (int i = 0; i < getChildCount(); i++) {
1632 View v = getPageAt(i);
1633 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001634 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001635
1636 float interpolatedProgress =
1637 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1638 float scale = (1 - interpolatedProgress) +
1639 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1640 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001641
Adam Cohen2591f6a2011-10-25 14:36:40 -07001642 float alpha;
1643
Winson Chungd167e2a2012-04-26 13:13:01 -07001644 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001645 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001646 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001647 } else {
1648 // On large screens we need to fade the page as it nears its leftmost position
1649 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1650 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001651
1652 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1653 int pageWidth = v.getMeasuredWidth();
1654 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001655
1656 if (PERFORM_OVERSCROLL_ROTATION) {
1657 if (i == 0 && scrollProgress < 0) {
1658 // Overscroll to the left
1659 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1660 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1661 scale = 1.0f;
1662 alpha = 1.0f;
1663 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001664 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001665 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1666 // Overscroll to the right
1667 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1668 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1669 scale = 1.0f;
1670 alpha = 1.0f;
1671 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001672 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001673 } else {
1674 v.setPivotY(pageHeight / 2.0f);
1675 v.setPivotX(pageWidth / 2.0f);
1676 v.setRotationY(0f);
1677 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001678 }
1679
1680 v.setTranslationX(translationX);
1681 v.setScaleX(scale);
1682 v.setScaleY(scale);
1683 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001684
1685 // If the view has 0 alpha, we set it to be invisible so as to prevent
1686 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001687 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001688 v.setVisibility(INVISIBLE);
1689 } else if (v.getVisibility() != VISIBLE) {
1690 v.setVisibility(VISIBLE);
1691 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001692 }
1693 }
1694 }
1695
1696 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001697 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001698 }
1699
Winson Chung785d2eb2011-04-14 16:08:02 -07001700 /**
1701 * Used by the parent to get the content width to set the tab bar to
1702 * @return
1703 */
1704 public int getPageContentWidth() {
1705 return mContentWidth;
1706 }
1707
Winson Chungb26f3d62011-06-02 10:49:29 -07001708 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001709 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001710 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001711 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001712 // We reset the save index when we change pages so that it will be recalculated on next
1713 // rotation
1714 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001715 }
1716
Winson Chung785d2eb2011-04-14 16:08:02 -07001717 /*
1718 * AllAppsView implementation
1719 */
1720 @Override
1721 public void setup(Launcher launcher, DragController dragController) {
1722 mLauncher = launcher;
1723 mDragController = dragController;
1724 }
1725 @Override
1726 public void zoom(float zoom, boolean animate) {
1727 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1728 }
1729 @Override
1730 public boolean isVisible() {
1731 return (getVisibility() == VISIBLE);
1732 }
1733 @Override
1734 public boolean isAnimating() {
1735 return false;
1736 }
1737 @Override
1738 public void setApps(ArrayList<ApplicationInfo> list) {
1739 mApps = list;
1740 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001741 Log.d(TAG, "6549598 setApps mApps.size(): " + mApps.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001742 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001743
Winson Chung875de7e2011-06-28 14:25:17 -07001744 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1745 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001746 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001747 }
1748 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1749 // We add it in place, in alphabetical order
1750 int count = list.size();
1751 for (int i = 0; i < count; ++i) {
1752 ApplicationInfo info = list.get(i);
1753 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1754 if (index < 0) {
1755 mApps.add(-(index + 1), info);
1756 }
1757 }
1758 }
1759 @Override
1760 public void addApps(ArrayList<ApplicationInfo> list) {
1761 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001762 Log.d(TAG, "6549598 addApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001763 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001764 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001765 Log.d(TAG, "6549598 addApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001766 }
1767 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1768 ComponentName removeComponent = item.intent.getComponent();
1769 int length = list.size();
1770 for (int i = 0; i < length; ++i) {
1771 ApplicationInfo info = list.get(i);
1772 if (info.intent.getComponent().equals(removeComponent)) {
1773 return i;
1774 }
1775 }
1776 return -1;
1777 }
1778 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1779 // loop through all the apps and remove apps that have the same component
1780 int length = list.size();
1781 for (int i = 0; i < length; ++i) {
1782 ApplicationInfo info = list.get(i);
1783 int removeIndex = findAppByComponent(mApps, info);
1784 if (removeIndex > -1) {
1785 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001786 }
1787 }
1788 }
1789 @Override
1790 public void removeApps(ArrayList<ApplicationInfo> list) {
1791 removeAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001792 Log.d(TAG, "6549598 removeApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001793 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001794 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001795 Log.d(TAG, "6549598 removeApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001796 }
1797 @Override
1798 public void updateApps(ArrayList<ApplicationInfo> list) {
1799 // We remove and re-add the updated applications list because it's properties may have
1800 // changed (ie. the title), and this will ensure that the items will be in their proper
1801 // place in the list.
1802 removeAppsWithoutInvalidate(list);
1803 addAppsWithoutInvalidate(list);
Winson Chung7c7a22d2012-06-01 13:46:48 -07001804 Log.d(TAG, "6549598 updateApps mApps.size(): " + mApps.size() + " list.size(): " + list.size());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001805 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001806 invalidatePageData();
Winson Chung7c7a22d2012-06-01 13:46:48 -07001807 Log.d(TAG, "6549598 updateApps mNumAppsPages: " + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001808 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001809
Winson Chung785d2eb2011-04-14 16:08:02 -07001810 @Override
1811 public void reset() {
Winson Chung7c7a22d2012-06-01 13:46:48 -07001812 Log.d(TAG, "6549598 reset");
Winson Chung649668f2012-01-10 13:07:16 -08001813 // If we have reset, then we should not continue to restore the previous state
1814 mSaveInstanceStateItemIndex = -1;
1815
Adam Cohenb64d36e2011-10-17 21:48:02 -07001816 AppsCustomizeTabHost tabHost = getTabHost();
1817 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001818 if (tag != null) {
1819 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1820 tabHost.setCurrentTabFromContent(ContentType.Applications);
1821 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001822 }
Winson Chung649668f2012-01-10 13:07:16 -08001823
Adam Cohenb64d36e2011-10-17 21:48:02 -07001824 if (mCurrentPage != 0) {
1825 invalidatePageData(0);
1826 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001827 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001828
1829 private AppsCustomizeTabHost getTabHost() {
1830 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1831 }
1832
Winson Chung785d2eb2011-04-14 16:08:02 -07001833 @Override
1834 public void dumpState() {
1835 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001836 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1837 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001838 }
Adam Cohen4e844012011-11-09 13:48:04 -08001839
Winson Chung785d2eb2011-04-14 16:08:02 -07001840 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001841 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001842 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001843 for (Object i: list) {
1844 if (i instanceof AppWidgetProviderInfo) {
1845 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1846 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1847 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1848 + " initialLayout=" + info.initialLayout
1849 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1850 } else if (i instanceof ResolveInfo) {
1851 ResolveInfo info = (ResolveInfo) i;
1852 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1853 + info.icon);
1854 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001855 }
1856 }
Adam Cohen4e844012011-11-09 13:48:04 -08001857
Winson Chung785d2eb2011-04-14 16:08:02 -07001858 @Override
1859 public void surrender() {
1860 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1861 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001862
1863 // Stop all background tasks
1864 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001865 }
Winson Chung007c6982011-06-14 13:27:53 -07001866
Winson Chunge4e50662012-01-23 14:45:13 -08001867 @Override
1868 public void iconPressed(PagedViewIcon icon) {
1869 // Reset the previously pressed icon and store a reference to the pressed icon so that
1870 // we can reset it on return to Launcher (in Launcher.onResume())
1871 if (mPressedIcon != null) {
1872 mPressedIcon.resetDrawableState();
1873 }
1874 mPressedIcon = icon;
1875 }
1876
1877 public void resetDrawableState() {
1878 if (mPressedIcon != null) {
1879 mPressedIcon.resetDrawableState();
1880 mPressedIcon = null;
1881 }
1882 }
Winson Chung68e4c642011-11-10 15:48:25 -08001883
Winson Chungb44b5242011-06-13 11:32:14 -07001884 /*
1885 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1886 * widget previews in the background with the AsyncTasks.
1887 */
Winson Chung68e4c642011-11-10 15:48:25 -08001888 final static int sLookBehindPageCount = 2;
1889 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001890 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001891 final int count = getChildCount();
1892 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1893 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1894 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001895 }
1896 protected int getAssociatedUpperPageBound(int page) {
1897 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001898 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1899 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1900 count - 1);
1901 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001902 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001903
1904 @Override
1905 protected String getCurrentPageDescription() {
1906 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1907 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001908 int count = 0;
1909
Adam Cohen0cd3b642011-10-14 14:58:00 -07001910 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001911 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001912 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001913 } else {
1914 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001915 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001916 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001917 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001918
Michael Jurka8b805b12012-04-18 14:23:14 -07001919 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001920 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001921}