blob: 1817c824f122d3b6e201a78ec4b6f9a9823aeff4 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080033import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Michael Jurkadac85912012-05-18 15:04:49 -070035import android.graphics.ColorMatrix;
36import android.graphics.ColorMatrixColorFilter;
Winson Chung72d59842012-02-22 13:51:36 -080037import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.Paint;
Michael Jurkadac85912012-05-18 15:04:49 -070039import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080041import android.graphics.RectF;
Michael Jurkadac85912012-05-18 15:04:49 -070042import android.graphics.Shader;
Michael Jurkadac85912012-05-18 15:04:49 -070043import android.graphics.drawable.BitmapDrawable;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070045import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070046import android.os.Build;
47import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070048import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.util.AttributeSet;
50import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070051import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080052import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070053import android.view.LayoutInflater;
54import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070055import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070056import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070057import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070058import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070059import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070060import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
62import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070063import com.android.launcher2.DropTarget.DragObject;
64
Michael Jurka9bc8eba2012-05-21 20:36:44 -070065import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066import java.util.ArrayList;
67import java.util.Collections;
68import java.util.Iterator;
69import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070070
Winson Chungb44b5242011-06-13 11:32:14 -070071/**
72 * A simple callback interface which also provides the results of the task.
73 */
74interface AsyncTaskCallback {
75 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
76}
Winson Chung4e076542011-06-23 13:04:10 -070077
Winson Chungb44b5242011-06-13 11:32:14 -070078/**
79 * The data needed to perform either of the custom AsyncTasks.
80 */
81class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070082 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080083 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070084 }
85
Winson Chungb44b5242011-06-13 11:32:14 -070086 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
87 AsyncTaskCallback postR) {
88 page = p;
89 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070090 sourceImages = si;
91 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070092 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070093 doInBackgroundCallback = bgR;
94 postExecuteCallback = postR;
95 }
Michael Jurka038f9d82011-11-03 13:50:45 -070096 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070097 AsyncTaskCallback postR) {
98 page = p;
99 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700100 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700101 maxImageWidth = cw;
102 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 doInBackgroundCallback = bgR;
104 postExecuteCallback = postR;
105 }
Winson Chung09945932011-09-20 14:22:40 -0700106 void cleanup(boolean cancelled) {
107 // Clean up any references to source/generated bitmaps
108 if (sourceImages != null) {
109 if (cancelled) {
110 for (Bitmap b : sourceImages) {
111 b.recycle();
112 }
113 }
114 sourceImages.clear();
115 }
116 if (generatedImages != null) {
117 if (cancelled) {
118 for (Bitmap b : generatedImages) {
119 b.recycle();
120 }
121 }
122 generatedImages.clear();
123 }
124 }
Winson Chungb44b5242011-06-13 11:32:14 -0700125 int page;
126 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700127 ArrayList<Bitmap> sourceImages;
128 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700129 int maxImageWidth;
130 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700131 AsyncTaskCallback doInBackgroundCallback;
132 AsyncTaskCallback postExecuteCallback;
133}
Winson Chung4e076542011-06-23 13:04:10 -0700134
Winson Chungb44b5242011-06-13 11:32:14 -0700135/**
136 * A generic template for an async task used in AppsCustomize.
137 */
138class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700139 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700140 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700142 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700143 }
144 @Override
145 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
146 if (params.length != 1) return null;
147 // Load each of the widget previews in the background
148 params[0].doInBackgroundCallback.run(this, params[0]);
149 return params[0];
150 }
151 @Override
152 protected void onPostExecute(AsyncTaskPageData result) {
153 // All the widget previews are loaded, so we can just callback to inflate the page
154 result.postExecuteCallback.run(this, result);
155 }
156
157 void setThreadPriority(int p) {
158 threadPriority = p;
159 }
160 void syncThreadPriority() {
161 Process.setThreadPriority(threadPriority);
162 }
163
164 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700165 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700166 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700167 int threadPriority;
168}
Winson Chungb44b5242011-06-13 11:32:14 -0700169
Michael Jurkadac85912012-05-18 15:04:49 -0700170abstract class WeakReferenceThreadLocal<T> {
171 private ThreadLocal<WeakReference<T>> mThreadLocal;
172 public WeakReferenceThreadLocal() {
173 mThreadLocal = new ThreadLocal<WeakReference<T>>();
174 }
175
176 abstract T initialValue();
177
178 public void set(T t) {
179 mThreadLocal.set(new WeakReference<T>(t));
180 }
181
182 public T get() {
183 WeakReference<T> reference = mThreadLocal.get();
184 T obj;
185 if (reference == null) {
186 obj = initialValue();
187 mThreadLocal.set(new WeakReference<T>(obj));
188 return obj;
189 } else {
190 obj = reference.get();
191 if (obj == null) {
192 obj = initialValue();
193 mThreadLocal.set(new WeakReference<T>(obj));
194 }
195 return obj;
196 }
197 }
198}
199
200class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
201 @Override
202 protected Canvas initialValue() {
203 return new Canvas();
204 }
205}
206
207class PaintCache extends WeakReferenceThreadLocal<Paint> {
208 @Override
209 protected Paint initialValue() {
210 return null;
211 }
212}
213
214class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
215 @Override
216 protected Bitmap initialValue() {
217 return null;
218 }
219}
220
221class RectCache extends WeakReferenceThreadLocal<Rect> {
222 @Override
223 protected Rect initialValue() {
224 return new Rect();
225 }
226}
227
Winson Chungb44b5242011-06-13 11:32:14 -0700228/**
229 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
230 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700231public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700232 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700233 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
234 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700235 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700236
237 /**
238 * The different content types that this paged view can show.
239 */
240 public enum ContentType {
241 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700242 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700243 }
244
245 // Refs
246 private Launcher mLauncher;
247 private DragController mDragController;
248 private final LayoutInflater mLayoutInflater;
249 private final PackageManager mPackageManager;
250
Winson Chung5afbf7b2011-07-25 11:53:08 -0700251 // Save and Restore
252 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800253 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700254
Winson Chung785d2eb2011-04-14 16:08:02 -0700255 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700256 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700257 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700258
Winson Chung7d7541e2011-09-16 20:14:36 -0700259 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800260 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 private int mClingFocusedX;
262 private int mClingFocusedY;
263
Winson Chung1ed747a2011-05-03 16:18:34 -0700264 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700265 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700266 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700267
268 // Dimens
269 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700270 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800271 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700272 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700273 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700274 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700275 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700276 private int mNumAppsPages;
277 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700278
Adam Cohen22f823d2011-09-01 17:22:18 -0700279 // Relating to the scroll and overscroll effects
280 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700281 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700282 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700283 private static float TRANSITION_PIVOT = 0.65f;
284 private static float TRANSITION_MAX_ROTATION = 22;
285 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700286 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700287 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700288
Winson Chungb44b5242011-06-13 11:32:14 -0700289 // Previews & outlines
290 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800291 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700292
Adam Cohened66b2b2012-01-23 17:28:51 -0800293 private Runnable mInflateWidgetRunnable = null;
294 private Runnable mBindWidgetRunnable = null;
295 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700296 static final int WIDGET_PRELOAD_PENDING = 0;
297 static final int WIDGET_BOUND = 1;
298 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800299 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
300 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800301 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800302 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800303
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700304 private Toast mWidgetInstructionToast;
305
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
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700313 private Rect mTmpRect = new Rect();
314
Michael Jurkadac85912012-05-18 15:04:49 -0700315 // Used for drawing shortcut previews
316 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
317 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
318 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
319
320 // Used for drawing widget previews
321 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
322 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
323 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
324 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
325
Winson Chung785d2eb2011-04-14 16:08:02 -0700326 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
327 super(context, attrs);
328 mLayoutInflater = LayoutInflater.from(context);
329 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700330 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700331 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700332 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700333 mCanvas = new Canvas();
334 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700335
336 // Save the default widget preview background
337 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700338 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700339
Winson Chung6032e7e2011-11-08 15:47:17 -0800340 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
341 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
342 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700343 mWidgetWidthGap =
344 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
345 mWidgetHeightGap =
346 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700347 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
348 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700349 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
350 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700351 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700352 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700353
Winson Chung1ed747a2011-05-03 16:18:34 -0700354 // The padding on the non-matched dimension for the default widget preview icons
355 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700356 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700357
358 // Unless otherwise specified this view is important for accessibility.
359 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
360 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
361 }
Winson 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);
372 }
373
Winson Chung5afbf7b2011-07-25 11:53:08 -0700374 /** Returns the item index of the center item on this page so that we can restore to this
375 * item index when we rotate. */
376 private int getMiddleComponentIndexOnCurrentPage() {
377 int i = -1;
378 if (getPageCount() > 0) {
379 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700380 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700381 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700382 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
383 int numItemsPerPage = mCellCountX * mCellCountY;
384 int childCount = childrenLayout.getChildCount();
385 if (childCount > 0) {
386 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700387 }
388 } else {
389 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700390 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700391 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
392 int childCount = layout.getChildCount();
393 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700394 i = numApps +
395 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
396 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700397 }
398 }
399 return i;
400 }
401
402 /** Get the index of the item to restore to if we need to restore the current page. */
403 int getSaveInstanceStateIndex() {
404 if (mSaveInstanceStateItemIndex == -1) {
405 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
406 }
407 return mSaveInstanceStateItemIndex;
408 }
409
410 /** Returns the page in the current orientation which is expected to contain the specified
411 * item index. */
412 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700413 if (index < 0) return 0;
414
415 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700416 int numItemsPerPage = mCellCountX * mCellCountY;
417 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700418 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700419 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700420 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
421 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700422 }
423
Winson Chung5afbf7b2011-07-25 11:53:08 -0700424 /** Restores the page for an item at the specified index */
425 void restorePageForIndex(int index) {
426 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700427 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700428 }
429
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700430 private void updatePageCounts() {
431 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
432 (float) (mWidgetCountX * mWidgetCountY));
433 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
434 }
435
Winson Chungf0ea4d32011-06-06 14:27:16 -0700436 protected void onDataReady(int width, int height) {
437 // Note that we transpose the counts in portrait so that we get a similar layout
438 boolean isLandscape = getResources().getConfiguration().orientation ==
439 Configuration.ORIENTATION_LANDSCAPE;
440 int maxCellCountX = Integer.MAX_VALUE;
441 int maxCellCountY = Integer.MAX_VALUE;
442 if (LauncherApplication.isScreenLarge()) {
443 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
444 LauncherModel.getCellCountY());
445 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
446 LauncherModel.getCellCountX());
447 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800448 if (mMaxAppCellCountX > -1) {
449 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
450 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200451 // Temp hack for now: only use the max cell count Y for widget layout
452 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800453 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200454 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800455 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700456
457 // Now that the data is ready, we can calculate the content width, the number of cells to
458 // use for each page
459 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
460 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
461 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
462 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
463 mCellCountX = mWidgetSpacingLayout.getCellCountX();
464 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700465 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700466
Winson Chungdb1138b2011-06-30 14:39:35 -0700467 // Force a measure to update recalculate the gaps
468 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
469 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200470 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700471 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700473
Michael Jurkae326f182011-11-21 14:05:46 -0800474 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
475 final boolean hostIsTransitioning = host.isTransitioning();
476
Adam Cohen0cd3b642011-10-14 14:58:00 -0700477 // Restore the page
478 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800479 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700480
Winson Chung3f4e1422011-11-17 14:58:51 -0800481 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
482 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
483 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800484 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800485 post(new Runnable() {
486 @Override
487 public void run() {
488 showAllAppsCling();
489 }
490 });
491 }
492 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700493
Winson Chung3f4e1422011-11-17 14:58:51 -0800494 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700495 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800496 mHasShownAllAppsCling = true;
497 // Calculate the position for the cling punch through
498 int[] offset = new int[2];
499 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
500 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
501 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800502 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800503 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
504 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800505 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800506 mLauncher.showFirstRunAllAppsCling(pos);
507 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700508 }
509
510 @Override
511 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
512 int width = MeasureSpec.getSize(widthMeasureSpec);
513 int height = MeasureSpec.getSize(heightMeasureSpec);
514 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700515 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700516 setDataIsReady();
517 setMeasuredDimension(width, height);
518 onDataReady(width, height);
519 }
520 }
521
522 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
523 }
524
Winson Chung785d2eb2011-04-14 16:08:02 -0700525 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700526 // Get the list of widgets and shortcuts
527 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700528 List<AppWidgetProviderInfo> widgets =
529 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
531 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800532 for (AppWidgetProviderInfo widget : widgets) {
533 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700534 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700535 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
536 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700537 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
538 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700539 if (minSpanX <= LauncherModel.getCellCountX() &&
540 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700541 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700542 } else {
543 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
544 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700545 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800546 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700547 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800548 widget.minWidth + ", " + widget.minHeight + ")");
549 }
550 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700551 mWidgets.addAll(shortcuts);
552 Collections.sort(mWidgets,
553 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700554 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700555 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700556 }
557
558 @Override
559 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700560 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700561 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700562 mLauncher.getWorkspace().isSwitchingState()) return;
563
Winson Chung4b576be2011-04-27 17:40:20 -0700564 if (v instanceof PagedViewIcon) {
565 // Animate some feedback to the click
566 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800567
568 // Lock the drawable state to pressed until we return to Launcher
569 if (mPressedIcon != null) {
570 mPressedIcon.lockDrawableState();
571 }
Winson Chungc7450e32012-04-17 17:34:08 -0700572
Winson Chung18f41f82012-05-09 13:28:10 -0700573 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
574 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
575 // when Launcher resumes and we are still in AllApps.
576 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700577 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
578
Winson Chung4b576be2011-04-27 17:40:20 -0700579 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700580 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700581 if (mWidgetInstructionToast != null) {
582 mWidgetInstructionToast.cancel();
583 }
584 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
585 Toast.LENGTH_SHORT);
586 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700587
Winson Chungd2e87b32011-06-02 10:53:07 -0700588 // Create a little animation to show that the widget can move
589 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
590 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700591 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
592 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700593 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700594 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700595 tydAnim.setDuration(100);
596 bounce.play(tyuAnim).before(tydAnim);
597 bounce.setInterpolator(new AccelerateInterpolator());
598 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700599 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700600 }
601
Winson Chungc6f10b92011-11-14 11:39:07 -0800602 public boolean onKey(View v, int keyCode, KeyEvent event) {
603 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
604 }
605
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 /*
607 * PagedViewWithDraggableItems implementation
608 */
609 @Override
610 protected void determineDraggingStart(android.view.MotionEvent ev) {
611 // Disable dragging by pulling an app down for now.
612 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700613
Winson Chung4b576be2011-04-27 17:40:20 -0700614 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700615 mLauncher.getWorkspace().onDragStartedWithItem(v);
616 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700617 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700618
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700619 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
620 Bundle options = null;
621 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
622 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700623 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
624 info.componentName, null);
625
626 float density = getResources().getDisplayMetrics().density;
627 int xPaddingDips = (int) ((padding.left + padding.right) / density);
628 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
629
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700630 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700631 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
632 mTmpRect.left - xPaddingDips);
633 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
634 mTmpRect.top - yPaddingDips);
635 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
636 mTmpRect.right - xPaddingDips);
637 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
638 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700639 }
640 return options;
641 }
642
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700643 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700645 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
646
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700648 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 return;
650 }
651
Adam Cohen21a170b2012-05-30 15:17:06 -0700652 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 mBindWidgetRunnable = new Runnable() {
654 @Override
655 public void run() {
656 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700657 // Options will be null for platforms with JB or lower, so this serves as an
658 // SDK level check.
659 if (options == null) {
660 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
661 mWidgetLoadingId, info.componentName)) {
662 mWidgetCleanupState = WIDGET_BOUND;
663 }
664 } else {
665 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
666 mWidgetLoadingId, info.componentName, options)) {
667 mWidgetCleanupState = WIDGET_BOUND;
668 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700669 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 }
671 };
672 post(mBindWidgetRunnable);
673
674 mInflateWidgetRunnable = new Runnable() {
675 @Override
676 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700677 if (mWidgetCleanupState != WIDGET_BOUND) {
678 return;
679 }
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
Michael Jurka8c3339b2012-06-14 16:18:21 -0700819 // Don't clip alpha values for the drag outline if we're using the default widget preview
820 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
821 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700822
Winson Chung1120e032011-11-22 16:11:31 -0800823 // Save the preview for the outline generation, then dim the preview
824 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
825 false);
Winson Chung1120e032011-11-22 16:11:31 -0800826
Winson Chung4b576be2011-04-27 17:40:20 -0700827 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700828 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700829 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800830 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800831 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800832 outline.recycle();
833 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700834 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836
Winson Chung4b576be2011-04-27 17:40:20 -0700837 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700839 if (!super.beginDragging(v)) return false;
840
841 if (v instanceof PagedViewIcon) {
842 beginDraggingApplication(v);
843 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700844 if (!beginDraggingWidget(v)) {
845 return false;
846 }
Winson Chung4b576be2011-04-27 17:40:20 -0700847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848
849 // We delay entering spring-loaded mode slightly to make sure the UI
850 // thready is free of any work.
851 postDelayed(new Runnable() {
852 @Override
853 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800854 // We don't enter spring-loaded mode if the drag has been cancelled
855 if (mLauncher.getDragController().isDragging()) {
856 // Dismiss the cling
857 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800858
Adam Cohen1b36dc32012-02-13 19:27:37 -0800859 // Reset the alpha on the dragged icon before we drag
860 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800861
Adam Cohen1b36dc32012-02-13 19:27:37 -0800862 // Go into spring loaded mode (must happen before we startDrag())
863 mLauncher.enterSpringLoadedDragMode();
864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 }
Winson Chung72d59842012-02-22 13:51:36 -0800866 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800867
Winson Chung785d2eb2011-04-14 16:08:02 -0700868 return true;
869 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800870
Winson Chunga48487a2012-03-20 16:19:37 -0700871 /**
872 * Clean up after dragging.
873 *
874 * @param target where the item was dragged to (can be null if the item was flung)
875 */
876 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700877 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700878 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700879 // Exit spring loaded mode if we have not successfully dropped or have not handled the
880 // drop in Workspace
881 mLauncher.exitSpringLoadedDragMode();
882 }
Winson Chung4b919f82012-05-01 10:44:08 -0700883 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700884 }
885
Winson Chung785d2eb2011-04-14 16:08:02 -0700886 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700887 public View getContent() {
888 return null;
889 }
890
891 @Override
892 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700893 mInTransition = true;
894 if (toWorkspace) {
895 cancelAllTasks();
896 }
897 }
898
899 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700900 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700901 }
902
903 @Override
904 public void onLauncherTransitionStep(Launcher l, float t) {
905 }
906
907 @Override
908 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
909 mInTransition = false;
910 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
911 onSyncWidgetPageItems(d);
912 }
913 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700914 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
915 r.run();
916 }
917 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700918 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700919 }
920
921 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700922 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
923 boolean success) {
924 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
925 if (isFlingToDelete) return;
926
927 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700928
929 // Display an error message if the drag failed due to there not being enough space on the
930 // target layout we were dropping on.
931 if (!success) {
932 boolean showOutOfSpaceMessage = false;
933 if (target instanceof Workspace) {
934 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
935 Workspace workspace = (Workspace) target;
936 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700937 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700938 if (layout != null) {
939 layout.calculateSpans(itemInfo);
940 showOutOfSpaceMessage =
941 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
942 }
943 }
Winson Chungfc79c802011-05-02 13:35:34 -0700944 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700945 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700946 }
Adam Cohen7a326642012-02-22 12:03:22 -0800947
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800948 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700949 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700950 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800951 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700952 }
953
Winson Chunga48487a2012-03-20 16:19:37 -0700954 @Override
955 public void onFlingToDeleteCompleted() {
956 // We just dismiss the drag when we fling, so cleanup here
957 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700958 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700959 mDraggingWidget = false;
960 }
961
962 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800963 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700964 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800965 }
966
Winson Chung7f0acdd2011-09-19 18:34:19 -0700967 @Override
968 protected void onDetachedFromWindow() {
969 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700970 cancelAllTasks();
971 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700972
Michael Jurkae326f182011-11-21 14:05:46 -0800973 public void clearAllWidgetPages() {
974 cancelAllTasks();
975 int count = getChildCount();
976 for (int i = 0; i < count; i++) {
977 View v = getPageAt(i);
978 if (v instanceof PagedViewGridLayout) {
979 ((PagedViewGridLayout) v).removeAllViewsOnPage();
980 mDirtyPageContent.set(i, true);
981 }
982 }
983 }
984
Adam Cohen0cd3b642011-10-14 14:58:00 -0700985 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700986 // Clean up all the async tasks
987 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
988 while (iter.hasNext()) {
989 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
990 task.cancel(false);
991 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700992 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700993
994 // We've already preallocated the views for the data to load into, so clear them as well
995 View v = getPageAt(task.page);
996 if (v instanceof PagedViewGridLayout) {
997 ((PagedViewGridLayout) v).removeAllViewsOnPage();
998 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700999 }
Winson Chung83687b12012-04-25 16:01:01 -07001000 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -07001001 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -07001002 }
1003
Winson Chung785d2eb2011-04-14 16:08:02 -07001004 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001005 if (type == ContentType.Widgets) {
1006 invalidatePageData(mNumAppsPages, true);
1007 } else if (type == ContentType.Applications) {
1008 invalidatePageData(0, true);
1009 }
Winson Chungb44b5242011-06-13 11:32:14 -07001010 }
1011
Adam Cohen0cd3b642011-10-14 14:58:00 -07001012 protected void snapToPage(int whichPage, int delta, int duration) {
1013 super.snapToPage(whichPage, delta, duration);
1014 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001015
1016 // Update the thread priorities given the direction lookahead
1017 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1018 while (iter.hasNext()) {
1019 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001020 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001021 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1022 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1023 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1024 } else {
1025 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1026 }
1027 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001028 }
1029
1030 private void updateCurrentTab(int currentPage) {
1031 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001032 if (tabHost != null) {
1033 String tag = tabHost.getCurrentTabTag();
1034 if (tag != null) {
1035 if (currentPage >= mNumAppsPages &&
1036 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1037 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1038 } else if (currentPage < mNumAppsPages &&
1039 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1040 tabHost.setCurrentTabFromContent(ContentType.Applications);
1041 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001042 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001043 }
1044 }
1045
Winson Chung785d2eb2011-04-14 16:08:02 -07001046 /*
1047 * Apps PagedView implementation
1048 */
Winson Chung63257c12011-05-05 17:06:13 -07001049 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1050 int childCount = layout.getChildCount();
1051 for (int i = 0; i < childCount; ++i) {
1052 layout.getChildAt(i).setVisibility(visibility);
1053 }
1054 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001055 private void setupPage(PagedViewCellLayout layout) {
1056 layout.setCellCount(mCellCountX, mCellCountY);
1057 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1058 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1059 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1060
Winson Chung63257c12011-05-05 17:06:13 -07001061 // Note: We force a measure here to get around the fact that when we do layout calculations
1062 // immediately after syncing, we don't have a proper width. That said, we already know the
1063 // expected page width, so we can actually optimize by hiding all the TextView-based
1064 // children that are expensive to measure, and let that happen naturally later.
1065 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001066 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001068 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001069 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001070 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001071 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001072
Winson Chungf314b0e2011-08-16 11:54:27 -07001073 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001074 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001075 int numCells = mCellCountX * mCellCountY;
1076 int startIndex = page * numCells;
1077 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001078 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001079
Winson Chung785d2eb2011-04-14 16:08:02 -07001080 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001081 ArrayList<Object> items = new ArrayList<Object>();
1082 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001083 for (int i = startIndex; i < endIndex; ++i) {
1084 ApplicationInfo info = mApps.get(i);
1085 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1086 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001087 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001088 icon.setOnClickListener(this);
1089 icon.setOnLongClickListener(this);
1090 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001091 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001092
1093 int index = i - startIndex;
1094 int x = index % mCellCountX;
1095 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001096 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001097
1098 items.add(info);
1099 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001100 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001101
Michael Jurka47639b92013-01-14 12:42:27 +01001102 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001103 }
Winson Chungb44b5242011-06-13 11:32:14 -07001104
1105 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001106 * A helper to return the priority for loading of the specified widget page.
1107 */
1108 private int getWidgetPageLoadPriority(int page) {
1109 // If we are snapping to another page, use that index as the target page index
1110 int toPage = mCurrentPage;
1111 if (mNextPage > -1) {
1112 toPage = mNextPage;
1113 }
1114
1115 // We use the distance from the target page as an initial guess of priority, but if there
1116 // are no pages of higher priority than the page specified, then bump up the priority of
1117 // the specified page.
1118 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1119 int minPageDiff = Integer.MAX_VALUE;
1120 while (iter.hasNext()) {
1121 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001122 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001123 }
1124
1125 int rawPageDiff = Math.abs(page - toPage);
1126 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1127 }
1128 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001129 * Return the appropriate thread priority for loading for a given page (we give the current
1130 * page much higher priority)
1131 */
1132 private int getThreadPriorityForPage(int page) {
1133 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001134 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001135 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001136 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001137 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001138 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001139 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001140 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001141 }
1142 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001143 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001144 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001145 return Math.max(0, pageDiff * sPageSleepDelay);
1146 }
Winson Chungb44b5242011-06-13 11:32:14 -07001147 /**
1148 * Creates and executes a new AsyncTask to load a page of widget previews.
1149 */
1150 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001151 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001152
Winson Chungb44b5242011-06-13 11:32:14 -07001153 // Prune all tasks that are no longer needed
1154 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1155 while (iter.hasNext()) {
1156 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001157 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001158 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1159 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001160 task.cancel(false);
1161 iter.remove();
1162 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001163 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001164 }
1165 }
1166
Winson Chungf314b0e2011-08-16 11:54:27 -07001167 // 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 -07001168 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001169 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001170 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001171 @Override
1172 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001173 try {
Winson Chung09945932011-09-20 14:22:40 -07001174 try {
1175 Thread.sleep(sleepMs);
1176 } catch (Exception e) {}
1177 loadWidgetPreviewsInBackground(task, data);
1178 } finally {
1179 if (task.isCancelled()) {
1180 data.cleanup(true);
1181 }
1182 }
Winson Chungb44b5242011-06-13 11:32:14 -07001183 }
1184 },
1185 new AsyncTaskCallback() {
1186 @Override
1187 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001188 mRunningTasks.remove(task);
1189 if (task.isCancelled()) return;
1190 // do cleanup inside onSyncWidgetPageItems
1191 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001192 }
Winson Chung09945932011-09-20 14:22:40 -07001193 });
Winson Chungb44b5242011-06-13 11:32:14 -07001194
1195 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001196 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001197 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001198 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001199 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1200 mRunningTasks.add(t);
1201 }
Winson Chungb44b5242011-06-13 11:32:14 -07001202
Winson Chung785d2eb2011-04-14 16:08:02 -07001203 /*
1204 * Widgets PagedView implementation
1205 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001206 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001207 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1208 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001209
1210 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001211 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001212 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1213 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001215 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001216 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001217
Winson Chung5fc72b32011-10-11 17:53:58 -07001218 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001219 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001220 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001221
Winson Chung5fc72b32011-10-11 17:53:58 -07001222 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001223 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001224 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001225 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001226 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001227 Rect oldBounds = d.copyBounds();
1228 d.setBounds(x, y, x + w, y + h);
1229 d.draw(c);
1230 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001231 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001232 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001233 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001234
Michael Jurkadac85912012-05-18 15:04:49 -07001235 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1236 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1237 final Canvas c = mCachedShortcutPreviewCanvas.get();
1238 if (tempBitmap == null ||
1239 tempBitmap.getWidth() != maxWidth ||
1240 tempBitmap.getHeight() != maxHeight) {
1241 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1242 mCachedShortcutPreviewBitmap.set(tempBitmap);
1243 } else {
1244 c.setBitmap(tempBitmap);
1245 c.drawColor(0, PorterDuff.Mode.CLEAR);
1246 c.setBitmap(null);
1247 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001248 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001249 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001250
1251 int paddingTop =
1252 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1253 int paddingLeft =
1254 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1255 int paddingRight =
1256 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1257
1258 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001259
1260 renderDrawableToBitmap(
1261 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1262
1263 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1264 c.setBitmap(preview);
1265 Paint p = mCachedShortcutPreviewPaint.get();
1266 if (p == null) {
1267 p = new Paint();
1268 ColorMatrix colorMatrix = new ColorMatrix();
1269 colorMatrix.setSaturation(0);
1270 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1271 p.setAlpha((int) (255 * 0.06f));
1272 //float density = 1f;
1273 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1274 mCachedShortcutPreviewPaint.set(p);
1275 }
1276 c.drawBitmap(tempBitmap, 0, 0, p);
1277 c.setBitmap(null);
1278
1279 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1280
Winson Chungb44b5242011-06-13 11:32:14 -07001281 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001282 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001283
Michael Jurkadac85912012-05-18 15:04:49 -07001284 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1285 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1286 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001287 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001288 String packageName = provider.getPackageName();
1289 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1290 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001291
Michael Jurka038f9d82011-11-03 13:50:45 -07001292 Drawable drawable = null;
1293 if (previewImage != 0) {
1294 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1295 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001296 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001297 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001298 }
1299 }
1300
Michael Jurka038f9d82011-11-03 13:50:45 -07001301 int bitmapWidth;
1302 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001303 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001304 boolean widgetPreviewExists = (drawable != null);
1305 if (widgetPreviewExists) {
1306 bitmapWidth = drawable.getIntrinsicWidth();
1307 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001308 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001309 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001310 if (cellHSpan < 1) cellHSpan = 1;
1311 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001312
1313 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1314 .getDrawable(R.drawable.widget_preview_tile);
1315 final int previewDrawableWidth = previewDrawable
1316 .getIntrinsicWidth();
1317 final int previewDrawableHeight = previewDrawable
1318 .getIntrinsicHeight();
1319 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1320 bitmapHeight = previewDrawableHeight * cellVSpan;
1321
1322 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1323 Config.ARGB_8888);
1324 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1325 c.setBitmap(defaultPreview);
1326 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1327 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1328 Shader.TileMode.REPEAT);
1329 previewDrawable.draw(c);
1330 c.setBitmap(null);
1331
1332 // Draw the icon in the top left corner
1333 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1334 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1335 float iconScale = Math.min((float) smallestSide
1336 / (mAppIconSize + 2 * minOffset), 1f);
1337
1338 try {
1339 Drawable icon = null;
1340 int hoffset =
1341 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1342 int yoffset =
1343 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1344 if (iconId > 0)
1345 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001346 if (icon != null) {
1347 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1348 yoffset, (int) (mAppIconSize * iconScale),
1349 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001350 }
Michael Jurkadac85912012-05-18 15:04:49 -07001351 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001352 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001353 }
1354
Michael Jurkadac85912012-05-18 15:04:49 -07001355 // Scale to fit width only - let the widget preview be clipped in the
1356 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001357 float scale = 1f;
1358 if (bitmapWidth > maxWidth) {
1359 scale = maxWidth / (float) bitmapWidth;
1360 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001361 if (scale != 1f) {
1362 bitmapWidth = (int) (scale * bitmapWidth);
1363 bitmapHeight = (int) (scale * bitmapHeight);
1364 }
1365
Michael Jurkadac85912012-05-18 15:04:49 -07001366 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1367 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001368
Michael Jurkadac85912012-05-18 15:04:49 -07001369 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001370 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001371 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1372 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001373 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001374 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1375 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1376 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1377 c.setBitmap(preview);
1378 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1379 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1380
1381 Paint p = mCachedAppWidgetPreviewPaint.get();
1382 if (p == null) {
1383 p = new Paint();
1384 p.setFilterBitmap(true);
1385 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001386 }
Michael Jurkadac85912012-05-18 15:04:49 -07001387 c.drawBitmap(defaultPreview, src, dest, p);
1388 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001389 }
Winson Chungb44b5242011-06-13 11:32:14 -07001390 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001391 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001392
Michael Jurka038f9d82011-11-03 13:50:45 -07001393 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001394 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001395
Winson Chungd2945262011-06-24 15:22:14 -07001396 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001397 final ArrayList<Object> items = new ArrayList<Object>();
1398 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1399 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001400 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001401 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1402 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001403 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001404
Winson Chunge4a647f2011-09-30 14:41:25 -07001405 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001406 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001407 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1408 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001409 }
1410
Winson Chunge4a647f2011-09-30 14:41:25 -07001411 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001412 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001413 layout.setColumnCount(layout.getCellCountX());
1414 for (int i = 0; i < items.size(); ++i) {
1415 Object rawInfo = items.get(i);
1416 PendingAddItemInfo createItemInfo = null;
1417 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1418 R.layout.apps_customize_widget, layout, false);
1419 if (rawInfo instanceof AppWidgetProviderInfo) {
1420 // Fill in the widget information
1421 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1422 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001423
1424 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001425 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001426 createItemInfo.spanX = spanXY[0];
1427 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001428 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001429 createItemInfo.minSpanX = minSpanXY[0];
1430 createItemInfo.minSpanY = minSpanXY[1];
1431
1432 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001433 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001434 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001435 } else if (rawInfo instanceof ResolveInfo) {
1436 // Fill in the shortcuts information
1437 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001438 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001439 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1440 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1441 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001442 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001443 widget.setTag(createItemInfo);
1444 }
1445 widget.setOnClickListener(this);
1446 widget.setOnLongClickListener(this);
1447 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001448 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001449
1450 // Layout each widget
1451 int ix = i % mWidgetCountX;
1452 int iy = i / mWidgetCountX;
1453 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001454 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001455 GridLayout.spec(ix, GridLayout.TOP));
1456 lp.width = cellWidth;
1457 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001458 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001459 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1460 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1461 layout.addView(widget, lp);
1462 }
1463
Michael Jurka038f9d82011-11-03 13:50:45 -07001464 // wait until a call on onLayout to start loading, because
1465 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1466 // TODO: can we do a measure/layout immediately?
1467 layout.setOnLayoutListener(new Runnable() {
1468 public void run() {
1469 // Load the widget previews
1470 int maxPreviewWidth = cellWidth;
1471 int maxPreviewHeight = cellHeight;
1472 if (layout.getChildCount() > 0) {
1473 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1474 int[] maxSize = w.getPreviewSize();
1475 maxPreviewWidth = maxSize[0];
1476 maxPreviewHeight = maxSize[1];
1477 }
1478 if (immediate) {
1479 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1480 maxPreviewWidth, maxPreviewHeight, null, null);
1481 loadWidgetPreviewsInBackground(null, data);
1482 onSyncWidgetPageItems(data);
1483 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001484 if (mInTransition) {
1485 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1486 } else {
1487 prepareLoadWidgetPreviewsTask(page, items,
1488 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1489 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001490 }
1491 }
1492 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001493 }
1494 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1495 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001496 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1497 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001498 if (task != null) {
1499 // Ensure that this task starts running at the correct priority
1500 task.syncThreadPriority();
1501 }
1502
1503 // Load each of the widget/shortcut previews
1504 ArrayList<Object> items = data.items;
1505 ArrayList<Bitmap> images = data.generatedImages;
1506 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001507 for (int i = 0; i < count; ++i) {
1508 if (task != null) {
1509 // Ensure we haven't been cancelled yet
1510 if (task.isCancelled()) break;
1511 // Before work on each item, ensure that this task is running at the correct
1512 // priority
1513 task.syncThreadPriority();
1514 }
1515
1516 Object rawInfo = items.get(i);
1517 if (rawInfo instanceof AppWidgetProviderInfo) {
1518 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001519 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001520
1521 int maxWidth = Math.min(data.maxImageWidth,
1522 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1523 int maxHeight = Math.min(data.maxImageHeight,
1524 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001525 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001526 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001527 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001528 } else if (rawInfo instanceof ResolveInfo) {
1529 // Fill in the shortcuts information
1530 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001531 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001532 }
1533 }
Winson Chungb44b5242011-06-13 11:32:14 -07001534 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001535
Winson Chungb44b5242011-06-13 11:32:14 -07001536 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001537 if (mInTransition) {
1538 mDeferredSyncWidgetPageItems.add(data);
1539 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001540 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001541 try {
1542 int page = data.page;
1543 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001544
Michael Jurka39e5d172012-03-12 18:36:12 -07001545 ArrayList<Object> items = data.items;
1546 int count = items.size();
1547 for (int i = 0; i < count; ++i) {
1548 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1549 if (widget != null) {
1550 Bitmap preview = data.generatedImages.get(i);
1551 widget.applyPreview(new FastBitmapDrawable(preview), i);
1552 }
1553 }
Winson Chung68e4c642011-11-10 15:48:25 -08001554
Michael Jurka47639b92013-01-14 12:42:27 +01001555 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001556
1557 // Update all thread priorities
1558 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1559 while (iter.hasNext()) {
1560 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1561 int pageIndex = task.page;
1562 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1563 }
1564 } finally {
1565 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001566 }
Winson Chungb44b5242011-06-13 11:32:14 -07001567 }
Winson Chung46af2e82011-05-09 16:00:53 -07001568
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 @Override
1570 public void syncPages() {
1571 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001572 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001573
Adam Cohen0cd3b642011-10-14 14:58:00 -07001574 Context context = getContext();
1575 for (int j = 0; j < mNumWidgetPages; ++j) {
1576 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1577 mWidgetCountY);
1578 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001579 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001580 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001581 }
1582
Adam Cohen0cd3b642011-10-14 14:58:00 -07001583 for (int i = 0; i < mNumAppsPages; ++i) {
1584 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1585 setupPage(layout);
1586 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001587 }
1588 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001589
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001591 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001592 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001593 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001594 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001595 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 }
1597 }
1598
Adam Cohen22f823d2011-09-01 17:22:18 -07001599 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1600 // it is in the z-order. This is important to insure touch events are handled correctly.
1601 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001602 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001603 }
1604
Adam Cohenae4f1552011-10-20 00:15:42 -07001605 @Override
1606 protected int indexToPage(int index) {
1607 return getChildCount() - index - 1;
1608 }
1609
Adam Cohen22f823d2011-09-01 17:22:18 -07001610 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1611 @Override
1612 protected void screenScrolled(int screenCenter) {
1613 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001614
1615 for (int i = 0; i < getChildCount(); i++) {
1616 View v = getPageAt(i);
1617 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001618 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001619
1620 float interpolatedProgress =
1621 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1622 float scale = (1 - interpolatedProgress) +
1623 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1624 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001625
Adam Cohen2591f6a2011-10-25 14:36:40 -07001626 float alpha;
1627
Winson Chungd167e2a2012-04-26 13:13:01 -07001628 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001629 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001630 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001631 } else {
1632 // On large screens we need to fade the page as it nears its leftmost position
1633 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1634 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001635
1636 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1637 int pageWidth = v.getMeasuredWidth();
1638 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001639
1640 if (PERFORM_OVERSCROLL_ROTATION) {
1641 if (i == 0 && scrollProgress < 0) {
1642 // Overscroll to the left
1643 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1644 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1645 scale = 1.0f;
1646 alpha = 1.0f;
1647 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001648 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001649 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1650 // Overscroll to the right
1651 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1652 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1653 scale = 1.0f;
1654 alpha = 1.0f;
1655 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001656 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001657 } else {
1658 v.setPivotY(pageHeight / 2.0f);
1659 v.setPivotX(pageWidth / 2.0f);
1660 v.setRotationY(0f);
1661 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001662 }
1663
1664 v.setTranslationX(translationX);
1665 v.setScaleX(scale);
1666 v.setScaleY(scale);
1667 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001668
1669 // If the view has 0 alpha, we set it to be invisible so as to prevent
1670 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001671 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001672 v.setVisibility(INVISIBLE);
1673 } else if (v.getVisibility() != VISIBLE) {
1674 v.setVisibility(VISIBLE);
1675 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001676 }
1677 }
Michael Jurka47639b92013-01-14 12:42:27 +01001678
1679 enableHwLayersOnVisiblePages();
1680 }
1681
1682 private void enableHwLayersOnVisiblePages() {
1683 final int screenCount = getChildCount();
1684
1685 getVisiblePages(mTempVisiblePagesRange);
1686 int leftScreen = mTempVisiblePagesRange[0];
1687 int rightScreen = mTempVisiblePagesRange[1];
1688 int forceDrawScreen = -1;
1689 if (leftScreen == rightScreen) {
1690 // make sure we're caching at least two pages always
1691 if (rightScreen < screenCount - 1) {
1692 rightScreen++;
1693 forceDrawScreen = rightScreen;
1694 } else if (leftScreen > 0) {
1695 leftScreen--;
1696 forceDrawScreen = leftScreen;
1697 }
1698 } else {
1699 forceDrawScreen = leftScreen + 1;
1700 }
1701
1702 for (int i = 0; i < screenCount; i++) {
1703 final View layout = (View) getPageAt(i);
1704 if (!(leftScreen <= i && i <= rightScreen &&
1705 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1706 layout.setLayerType(LAYER_TYPE_NONE, null);
1707 }
1708 }
1709
1710 int newLeft = -1;
1711 int newRight = -1;
1712 for (int i = 0; i < screenCount; i++) {
1713 final View layout = (View) getPageAt(i);
1714
1715 if (leftScreen <= i && i <= rightScreen &&
1716 (i == forceDrawScreen || shouldDrawChild(layout))) {
1717 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1718 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1719 }
1720 }
1721 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001722 }
1723
1724 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001725 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001726 }
1727
Winson Chung785d2eb2011-04-14 16:08:02 -07001728 /**
1729 * Used by the parent to get the content width to set the tab bar to
1730 * @return
1731 */
1732 public int getPageContentWidth() {
1733 return mContentWidth;
1734 }
1735
Winson Chungb26f3d62011-06-02 10:49:29 -07001736 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001737 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001738 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001739 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001740 // We reset the save index when we change pages so that it will be recalculated on next
1741 // rotation
1742 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001743 }
1744
Winson Chung785d2eb2011-04-14 16:08:02 -07001745 /*
1746 * AllAppsView implementation
1747 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001748 public void setup(Launcher launcher, DragController dragController) {
1749 mLauncher = launcher;
1750 mDragController = dragController;
1751 }
Winson Chung9802ac92012-06-08 16:01:58 -07001752
1753 /**
1754 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1755 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1756 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1757 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1758 */
1759 private void invalidateOnDataChange() {
1760 if (!isDataReady()) {
1761 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1762 // request a layout to trigger the page data when ready.
1763 requestLayout();
1764 } else {
1765 cancelAllTasks();
1766 invalidatePageData();
1767 }
1768 }
1769
Winson Chung785d2eb2011-04-14 16:08:02 -07001770 public void setApps(ArrayList<ApplicationInfo> list) {
1771 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001772 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001773 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001774 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001775 }
1776 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1777 // We add it in place, in alphabetical order
1778 int count = list.size();
1779 for (int i = 0; i < count; ++i) {
1780 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001781 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001782 if (index < 0) {
1783 mApps.add(-(index + 1), info);
1784 }
1785 }
1786 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001787 public void addApps(ArrayList<ApplicationInfo> list) {
1788 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001789 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001790 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001791 }
1792 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1793 ComponentName removeComponent = item.intent.getComponent();
1794 int length = list.size();
1795 for (int i = 0; i < length; ++i) {
1796 ApplicationInfo info = list.get(i);
1797 if (info.intent.getComponent().equals(removeComponent)) {
1798 return i;
1799 }
1800 }
1801 return -1;
1802 }
Winson Chungcd810732012-06-18 16:45:43 -07001803 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1804 int length = list.size();
1805 for (int i = 0; i < length; ++i) {
1806 ApplicationInfo info = list.get(i);
1807 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1808 return i;
1809 }
1810 }
1811 return -1;
1812 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001813 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1814 // loop through all the apps and remove apps that have the same component
1815 int length = list.size();
1816 for (int i = 0; i < length; ++i) {
1817 ApplicationInfo info = list.get(i);
1818 int removeIndex = findAppByComponent(mApps, info);
1819 if (removeIndex > -1) {
1820 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001821 }
1822 }
1823 }
Winson Chungcd810732012-06-18 16:45:43 -07001824 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1825 // loop through all the package names and remove apps that have the same package name
1826 for (String pn : packageNames) {
1827 int removeIndex = findAppByPackage(mApps, pn);
1828 while (removeIndex > -1) {
1829 mApps.remove(removeIndex);
1830 removeIndex = findAppByPackage(mApps, pn);
1831 }
1832 }
1833 }
1834 public void removeApps(ArrayList<String> packageNames) {
1835 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001836 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001837 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001838 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001839 public void updateApps(ArrayList<ApplicationInfo> list) {
1840 // We remove and re-add the updated applications list because it's properties may have
1841 // changed (ie. the title), and this will ensure that the items will be in their proper
1842 // place in the list.
1843 removeAppsWithoutInvalidate(list);
1844 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001845 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001846 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001847 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001848
Winson Chung785d2eb2011-04-14 16:08:02 -07001849 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001850 // If we have reset, then we should not continue to restore the previous state
1851 mSaveInstanceStateItemIndex = -1;
1852
Adam Cohenb64d36e2011-10-17 21:48:02 -07001853 AppsCustomizeTabHost tabHost = getTabHost();
1854 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001855 if (tag != null) {
1856 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1857 tabHost.setCurrentTabFromContent(ContentType.Applications);
1858 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001859 }
Winson Chung649668f2012-01-10 13:07:16 -08001860
Adam Cohenb64d36e2011-10-17 21:48:02 -07001861 if (mCurrentPage != 0) {
1862 invalidatePageData(0);
1863 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001864 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001865
1866 private AppsCustomizeTabHost getTabHost() {
1867 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1868 }
1869
Winson Chung785d2eb2011-04-14 16:08:02 -07001870 public void dumpState() {
1871 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001872 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1873 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001874 }
Adam Cohen4e844012011-11-09 13:48:04 -08001875
Winson Chung785d2eb2011-04-14 16:08:02 -07001876 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001877 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001878 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001879 for (Object i: list) {
1880 if (i instanceof AppWidgetProviderInfo) {
1881 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1882 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1883 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1884 + " initialLayout=" + info.initialLayout
1885 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1886 } else if (i instanceof ResolveInfo) {
1887 ResolveInfo info = (ResolveInfo) i;
1888 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1889 + info.icon);
1890 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001891 }
1892 }
Adam Cohen4e844012011-11-09 13:48:04 -08001893
Winson Chung785d2eb2011-04-14 16:08:02 -07001894 public void surrender() {
1895 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1896 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001897
1898 // Stop all background tasks
1899 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001900 }
Winson Chung007c6982011-06-14 13:27:53 -07001901
Winson Chunge4e50662012-01-23 14:45:13 -08001902 @Override
1903 public void iconPressed(PagedViewIcon icon) {
1904 // Reset the previously pressed icon and store a reference to the pressed icon so that
1905 // we can reset it on return to Launcher (in Launcher.onResume())
1906 if (mPressedIcon != null) {
1907 mPressedIcon.resetDrawableState();
1908 }
1909 mPressedIcon = icon;
1910 }
1911
1912 public void resetDrawableState() {
1913 if (mPressedIcon != null) {
1914 mPressedIcon.resetDrawableState();
1915 mPressedIcon = null;
1916 }
1917 }
Winson Chung68e4c642011-11-10 15:48:25 -08001918
Winson Chungb44b5242011-06-13 11:32:14 -07001919 /*
1920 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1921 * widget previews in the background with the AsyncTasks.
1922 */
Winson Chung68e4c642011-11-10 15:48:25 -08001923 final static int sLookBehindPageCount = 2;
1924 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001925 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001926 final int count = getChildCount();
1927 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1928 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1929 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001930 }
1931 protected int getAssociatedUpperPageBound(int page) {
1932 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001933 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1934 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1935 count - 1);
1936 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001937 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001938
1939 @Override
1940 protected String getCurrentPageDescription() {
1941 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1942 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001943 int count = 0;
1944
Adam Cohen0cd3b642011-10-14 14:58:00 -07001945 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001946 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001947 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001948 } else {
1949 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001950 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001951 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001952 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001953
Michael Jurka8b805b12012-04-18 14:23:14 -07001954 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001955 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001956}