blob: 6a959daffd5e8a8b2ac3431face482852614f6f3 [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
502 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
503 offset[0];
504 pos[1] += offset[1];
505 mLauncher.showFirstRunAllAppsCling(pos);
506 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700507 }
508
509 @Override
510 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
511 int width = MeasureSpec.getSize(widthMeasureSpec);
512 int height = MeasureSpec.getSize(heightMeasureSpec);
513 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700514 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700515 setDataIsReady();
516 setMeasuredDimension(width, height);
517 onDataReady(width, height);
518 }
519 }
520
521 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
522 }
523
Winson Chung785d2eb2011-04-14 16:08:02 -0700524 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700525 // Get the list of widgets and shortcuts
526 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700527 List<AppWidgetProviderInfo> widgets =
528 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
530 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800531 for (AppWidgetProviderInfo widget : widgets) {
532 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700533 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700534 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
535 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700536 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
537 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700538 if (minSpanX <= LauncherModel.getCellCountX() &&
539 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700540 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700541 } else {
542 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
543 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700544 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800545 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700546 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800547 widget.minWidth + ", " + widget.minHeight + ")");
548 }
549 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700550 mWidgets.addAll(shortcuts);
551 Collections.sort(mWidgets,
552 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700553 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700554 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700555 }
556
557 @Override
558 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700559 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700560 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700561 mLauncher.getWorkspace().isSwitchingState()) return;
562
Winson Chung4b576be2011-04-27 17:40:20 -0700563 if (v instanceof PagedViewIcon) {
564 // Animate some feedback to the click
565 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800566
567 // Lock the drawable state to pressed until we return to Launcher
568 if (mPressedIcon != null) {
569 mPressedIcon.lockDrawableState();
570 }
Winson Chungc7450e32012-04-17 17:34:08 -0700571
Winson Chung18f41f82012-05-09 13:28:10 -0700572 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
573 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
574 // when Launcher resumes and we are still in AllApps.
575 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700576 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
577
Winson Chung4b576be2011-04-27 17:40:20 -0700578 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700579 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700580 if (mWidgetInstructionToast != null) {
581 mWidgetInstructionToast.cancel();
582 }
583 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
584 Toast.LENGTH_SHORT);
585 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700586
Winson Chungd2e87b32011-06-02 10:53:07 -0700587 // Create a little animation to show that the widget can move
588 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
589 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700590 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
591 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700592 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700593 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700594 tydAnim.setDuration(100);
595 bounce.play(tyuAnim).before(tydAnim);
596 bounce.setInterpolator(new AccelerateInterpolator());
597 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700598 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700599 }
600
Winson Chungc6f10b92011-11-14 11:39:07 -0800601 public boolean onKey(View v, int keyCode, KeyEvent event) {
602 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
603 }
604
Winson Chung785d2eb2011-04-14 16:08:02 -0700605 /*
606 * PagedViewWithDraggableItems implementation
607 */
608 @Override
609 protected void determineDraggingStart(android.view.MotionEvent ev) {
610 // Disable dragging by pulling an app down for now.
611 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700612
Winson Chung4b576be2011-04-27 17:40:20 -0700613 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700614 mLauncher.getWorkspace().onDragStartedWithItem(v);
615 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700616 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700617
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700618 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
619 Bundle options = null;
620 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
621 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700622 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
623 info.componentName, null);
624
625 float density = getResources().getDisplayMetrics().density;
626 int xPaddingDips = (int) ((padding.left + padding.right) / density);
627 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
628
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700629 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700630 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
631 mTmpRect.left - xPaddingDips);
632 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
633 mTmpRect.top - yPaddingDips);
634 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
635 mTmpRect.right - xPaddingDips);
636 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
637 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700638 }
639 return options;
640 }
641
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700642 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800643 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700644 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
645
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700647 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 return;
649 }
650
Adam Cohen21a170b2012-05-30 15:17:06 -0700651 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 mBindWidgetRunnable = new Runnable() {
653 @Override
654 public void run() {
655 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700656 // Options will be null for platforms with JB or lower, so this serves as an
657 // SDK level check.
658 if (options == null) {
659 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
660 mWidgetLoadingId, info.componentName)) {
661 mWidgetCleanupState = WIDGET_BOUND;
662 }
663 } else {
664 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
665 mWidgetLoadingId, info.componentName, options)) {
666 mWidgetCleanupState = WIDGET_BOUND;
667 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 }
670 };
671 post(mBindWidgetRunnable);
672
673 mInflateWidgetRunnable = new Runnable() {
674 @Override
675 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700676 if (mWidgetCleanupState != WIDGET_BOUND) {
677 return;
678 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 AppWidgetHostView hostView = mLauncher.
680 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 info.boundWidget = hostView;
682 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800683 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700684 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
685 info.spanY, info, false);
686
687 // We want the first widget layout to be the correct size. This will be important
688 // for width size reporting to the AppWidgetManager.
689 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
690 unScaledSize[1]);
691 lp.x = lp.y = 0;
692 lp.customPosition = true;
693 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800694 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 }
696 };
697 post(mInflateWidgetRunnable);
698 }
699
700 @Override
701 public void onShortPress(View v) {
702 // We are anticipating a long press, and we use this time to load bind and instantiate
703 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700704 if (mCreateWidgetInfo != null) {
705 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
706 cleanupWidgetPreloading(false);
707 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800708 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700709 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800710 }
711
Adam Cohen0e56cc92012-05-11 15:57:05 -0700712 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
713 if (!widgetWasAdded) {
714 // If the widget was not added, we may need to do further cleanup.
715 PendingAddWidgetInfo info = mCreateWidgetInfo;
716 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700717
718 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700719 // We never did any preloading, so just remove pending callbacks to do so
720 removeCallbacks(mBindWidgetRunnable);
721 removeCallbacks(mInflateWidgetRunnable);
722 } else if (mWidgetCleanupState == WIDGET_BOUND) {
723 // Delete the widget id which was allocated
724 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700725 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
726 }
727
728 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700729 removeCallbacks(mInflateWidgetRunnable);
730 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700731 // Delete the widget id which was allocated
732 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700733 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
734 }
735
Adam Cohen0e56cc92012-05-11 15:57:05 -0700736 // The widget was inflated and added to the DragLayer -- remove it.
737 AppWidgetHostView widget = info.boundWidget;
738 mLauncher.getDragLayer().removeView(widget);
739 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 }
741 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
742 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700743 mCreateWidgetInfo = null;
744 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 }
746
Adam Cohen7a326642012-02-22 12:03:22 -0800747 @Override
748 public void cleanUpShortPress(View v) {
749 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700750 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800751 }
752 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800753
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700754 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800755 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700756 // Get the widget preview as the drag representation
757 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700758 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700759
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700760 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
761 // we abort the drag.
762 if (image.getDrawable() == null) {
763 mDraggingWidget = false;
764 return false;
765 }
766
Winson Chung4b576be2011-04-27 17:40:20 -0700767 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800768 Bitmap preview;
769 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800770 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700771 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700772 // This can happen in some weird cases involving multi-touch. We can't start dragging
773 // the widget if this is null, so we break out.
774 if (mCreateWidgetInfo == null) {
775 return false;
776 }
777
Adam Cohen1b36dc32012-02-13 19:27:37 -0800778 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
779 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700780 int spanX = createItemInfo.spanX;
781 int spanY = createItemInfo.spanY;
782 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
783 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700784
Winson Chung72d59842012-02-22 13:51:36 -0800785 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
786 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700787 int maxWidth, maxHeight;
788 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
789 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800790 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700791 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800792
793 // Determine the image view drawable scale relative to the preview
794 float[] mv = new float[9];
795 Matrix m = new Matrix();
796 m.setRectToRect(
797 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
798 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
799 (float) previewDrawable.getIntrinsicHeight()),
800 Matrix.ScaleToFit.START);
801 m.getValues(mv);
802 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700803 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700804 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
805 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
806 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
807 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
808
Winson Chung1120e032011-11-22 16:11:31 -0800809 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700810 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700811 renderDrawableToBitmap(icon, preview, 0, 0,
812 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700813 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700814 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700815 createItemInfo.spanX = createItemInfo.spanY = 1;
816 }
Winson Chung4b576be2011-04-27 17:40:20 -0700817
Michael Jurka8c3339b2012-06-14 16:18:21 -0700818 // Don't clip alpha values for the drag outline if we're using the default widget preview
819 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
820 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700821
Winson Chung1120e032011-11-22 16:11:31 -0800822 // Save the preview for the outline generation, then dim the preview
823 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
824 false);
Winson Chung1120e032011-11-22 16:11:31 -0800825
Winson Chung4b576be2011-04-27 17:40:20 -0700826 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700827 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700828 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800829 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800830 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800831 outline.recycle();
832 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700833 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835
Winson Chung4b576be2011-04-27 17:40:20 -0700836 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700838 if (!super.beginDragging(v)) return false;
839
840 if (v instanceof PagedViewIcon) {
841 beginDraggingApplication(v);
842 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700843 if (!beginDraggingWidget(v)) {
844 return false;
845 }
Winson Chung4b576be2011-04-27 17:40:20 -0700846 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800847
848 // We delay entering spring-loaded mode slightly to make sure the UI
849 // thready is free of any work.
850 postDelayed(new Runnable() {
851 @Override
852 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800853 // We don't enter spring-loaded mode if the drag has been cancelled
854 if (mLauncher.getDragController().isDragging()) {
855 // Dismiss the cling
856 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800857
Adam Cohen1b36dc32012-02-13 19:27:37 -0800858 // Reset the alpha on the dragged icon before we drag
859 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800860
Adam Cohen1b36dc32012-02-13 19:27:37 -0800861 // Go into spring loaded mode (must happen before we startDrag())
862 mLauncher.enterSpringLoadedDragMode();
863 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 }
Winson Chung72d59842012-02-22 13:51:36 -0800865 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800866
Winson Chung785d2eb2011-04-14 16:08:02 -0700867 return true;
868 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800869
Winson Chunga48487a2012-03-20 16:19:37 -0700870 /**
871 * Clean up after dragging.
872 *
873 * @param target where the item was dragged to (can be null if the item was flung)
874 */
875 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700876 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700877 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700878 // Exit spring loaded mode if we have not successfully dropped or have not handled the
879 // drop in Workspace
880 mLauncher.exitSpringLoadedDragMode();
881 }
Winson Chung4b919f82012-05-01 10:44:08 -0700882 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700883 }
884
Winson Chung785d2eb2011-04-14 16:08:02 -0700885 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700886 public View getContent() {
887 return null;
888 }
889
890 @Override
891 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700892 mInTransition = true;
893 if (toWorkspace) {
894 cancelAllTasks();
895 }
896 }
897
898 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700899 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700900 }
901
902 @Override
903 public void onLauncherTransitionStep(Launcher l, float t) {
904 }
905
906 @Override
907 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
908 mInTransition = false;
909 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
910 onSyncWidgetPageItems(d);
911 }
912 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700913 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
914 r.run();
915 }
916 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700917 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700918 }
919
920 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700921 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
922 boolean success) {
923 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
924 if (isFlingToDelete) return;
925
926 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700927
928 // Display an error message if the drag failed due to there not being enough space on the
929 // target layout we were dropping on.
930 if (!success) {
931 boolean showOutOfSpaceMessage = false;
932 if (target instanceof Workspace) {
933 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
934 Workspace workspace = (Workspace) target;
935 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700936 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700937 if (layout != null) {
938 layout.calculateSpans(itemInfo);
939 showOutOfSpaceMessage =
940 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
941 }
942 }
Winson Chungfc79c802011-05-02 13:35:34 -0700943 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700944 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700945 }
Adam Cohen7a326642012-02-22 12:03:22 -0800946
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800947 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700948 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700949 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800950 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700951 }
952
Winson Chunga48487a2012-03-20 16:19:37 -0700953 @Override
954 public void onFlingToDeleteCompleted() {
955 // We just dismiss the drag when we fling, so cleanup here
956 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700957 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700958 mDraggingWidget = false;
959 }
960
961 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800962 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700963 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800964 }
965
Winson Chung7f0acdd2011-09-19 18:34:19 -0700966 @Override
967 protected void onDetachedFromWindow() {
968 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700969 cancelAllTasks();
970 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700971
Michael Jurkae326f182011-11-21 14:05:46 -0800972 public void clearAllWidgetPages() {
973 cancelAllTasks();
974 int count = getChildCount();
975 for (int i = 0; i < count; i++) {
976 View v = getPageAt(i);
977 if (v instanceof PagedViewGridLayout) {
978 ((PagedViewGridLayout) v).removeAllViewsOnPage();
979 mDirtyPageContent.set(i, true);
980 }
981 }
982 }
983
Adam Cohen0cd3b642011-10-14 14:58:00 -0700984 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700985 // Clean up all the async tasks
986 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
987 while (iter.hasNext()) {
988 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
989 task.cancel(false);
990 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700991 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700992
993 // We've already preallocated the views for the data to load into, so clear them as well
994 View v = getPageAt(task.page);
995 if (v instanceof PagedViewGridLayout) {
996 ((PagedViewGridLayout) v).removeAllViewsOnPage();
997 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700998 }
Winson Chung83687b12012-04-25 16:01:01 -0700999 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -07001000 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -07001001 }
1002
Winson Chung785d2eb2011-04-14 16:08:02 -07001003 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001004 if (type == ContentType.Widgets) {
1005 invalidatePageData(mNumAppsPages, true);
1006 } else if (type == ContentType.Applications) {
1007 invalidatePageData(0, true);
1008 }
Winson Chungb44b5242011-06-13 11:32:14 -07001009 }
1010
Adam Cohen0cd3b642011-10-14 14:58:00 -07001011 protected void snapToPage(int whichPage, int delta, int duration) {
1012 super.snapToPage(whichPage, delta, duration);
1013 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001014
1015 // Update the thread priorities given the direction lookahead
1016 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1017 while (iter.hasNext()) {
1018 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001019 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001020 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1021 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1022 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1023 } else {
1024 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1025 }
1026 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001027 }
1028
1029 private void updateCurrentTab(int currentPage) {
1030 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001031 if (tabHost != null) {
1032 String tag = tabHost.getCurrentTabTag();
1033 if (tag != null) {
1034 if (currentPage >= mNumAppsPages &&
1035 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1036 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1037 } else if (currentPage < mNumAppsPages &&
1038 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1039 tabHost.setCurrentTabFromContent(ContentType.Applications);
1040 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001041 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001042 }
1043 }
1044
Winson Chung785d2eb2011-04-14 16:08:02 -07001045 /*
1046 * Apps PagedView implementation
1047 */
Winson Chung63257c12011-05-05 17:06:13 -07001048 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1049 int childCount = layout.getChildCount();
1050 for (int i = 0; i < childCount; ++i) {
1051 layout.getChildAt(i).setVisibility(visibility);
1052 }
1053 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001054 private void setupPage(PagedViewCellLayout layout) {
1055 layout.setCellCount(mCellCountX, mCellCountY);
1056 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1057 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1058 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1059
Winson Chung63257c12011-05-05 17:06:13 -07001060 // Note: We force a measure here to get around the fact that when we do layout calculations
1061 // immediately after syncing, we don't have a proper width. That said, we already know the
1062 // expected page width, so we can actually optimize by hiding all the TextView-based
1063 // children that are expensive to measure, and let that happen naturally later.
1064 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001065 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001067 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001068 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001069 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001070 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001071
Winson Chungf314b0e2011-08-16 11:54:27 -07001072 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001073 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001074 int numCells = mCellCountX * mCellCountY;
1075 int startIndex = page * numCells;
1076 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001077 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001078
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001080 ArrayList<Object> items = new ArrayList<Object>();
1081 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001082 for (int i = startIndex; i < endIndex; ++i) {
1083 ApplicationInfo info = mApps.get(i);
1084 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1085 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001086 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001087 icon.setOnClickListener(this);
1088 icon.setOnLongClickListener(this);
1089 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001090 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001091
1092 int index = i - startIndex;
1093 int x = index % mCellCountX;
1094 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001095 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001096
1097 items.add(info);
1098 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001099 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001100
Winson Chungf0ea4d32011-06-06 14:27:16 -07001101 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001102 }
Winson Chungb44b5242011-06-13 11:32:14 -07001103
1104 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001105 * A helper to return the priority for loading of the specified widget page.
1106 */
1107 private int getWidgetPageLoadPriority(int page) {
1108 // If we are snapping to another page, use that index as the target page index
1109 int toPage = mCurrentPage;
1110 if (mNextPage > -1) {
1111 toPage = mNextPage;
1112 }
1113
1114 // We use the distance from the target page as an initial guess of priority, but if there
1115 // are no pages of higher priority than the page specified, then bump up the priority of
1116 // the specified page.
1117 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1118 int minPageDiff = Integer.MAX_VALUE;
1119 while (iter.hasNext()) {
1120 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001121 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001122 }
1123
1124 int rawPageDiff = Math.abs(page - toPage);
1125 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1126 }
1127 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001128 * Return the appropriate thread priority for loading for a given page (we give the current
1129 * page much higher priority)
1130 */
1131 private int getThreadPriorityForPage(int page) {
1132 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001133 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001134 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001135 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001136 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001137 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001138 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001139 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001140 }
1141 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001142 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001143 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001144 return Math.max(0, pageDiff * sPageSleepDelay);
1145 }
Winson Chungb44b5242011-06-13 11:32:14 -07001146 /**
1147 * Creates and executes a new AsyncTask to load a page of widget previews.
1148 */
1149 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001150 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001151
Winson Chungb44b5242011-06-13 11:32:14 -07001152 // Prune all tasks that are no longer needed
1153 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1154 while (iter.hasNext()) {
1155 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001156 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001157 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1158 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001159 task.cancel(false);
1160 iter.remove();
1161 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001162 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001163 }
1164 }
1165
Winson Chungf314b0e2011-08-16 11:54:27 -07001166 // 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 -07001167 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001168 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001169 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001170 @Override
1171 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001172 try {
Winson Chung09945932011-09-20 14:22:40 -07001173 try {
1174 Thread.sleep(sleepMs);
1175 } catch (Exception e) {}
1176 loadWidgetPreviewsInBackground(task, data);
1177 } finally {
1178 if (task.isCancelled()) {
1179 data.cleanup(true);
1180 }
1181 }
Winson Chungb44b5242011-06-13 11:32:14 -07001182 }
1183 },
1184 new AsyncTaskCallback() {
1185 @Override
1186 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001187 mRunningTasks.remove(task);
1188 if (task.isCancelled()) return;
1189 // do cleanup inside onSyncWidgetPageItems
1190 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001191 }
Winson Chung09945932011-09-20 14:22:40 -07001192 });
Winson Chungb44b5242011-06-13 11:32:14 -07001193
1194 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001195 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001196 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001197 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001198 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1199 mRunningTasks.add(t);
1200 }
Winson Chungb44b5242011-06-13 11:32:14 -07001201
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 /*
1203 * Widgets PagedView implementation
1204 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001205 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1207 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001208
1209 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001210 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001211 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1212 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001213 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001214 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001216
Winson Chung5fc72b32011-10-11 17:53:58 -07001217 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001218 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001219 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001220
Winson Chung5fc72b32011-10-11 17:53:58 -07001221 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001222 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001223 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001224 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001225 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001226 Rect oldBounds = d.copyBounds();
1227 d.setBounds(x, y, x + w, y + h);
1228 d.draw(c);
1229 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001230 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001231 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001232 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001233
Michael Jurkadac85912012-05-18 15:04:49 -07001234 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1235 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1236 final Canvas c = mCachedShortcutPreviewCanvas.get();
1237 if (tempBitmap == null ||
1238 tempBitmap.getWidth() != maxWidth ||
1239 tempBitmap.getHeight() != maxHeight) {
1240 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1241 mCachedShortcutPreviewBitmap.set(tempBitmap);
1242 } else {
1243 c.setBitmap(tempBitmap);
1244 c.drawColor(0, PorterDuff.Mode.CLEAR);
1245 c.setBitmap(null);
1246 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001247 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001248 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001249
1250 int paddingTop =
1251 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1252 int paddingLeft =
1253 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1254 int paddingRight =
1255 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1256
1257 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001258
1259 renderDrawableToBitmap(
1260 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1261
1262 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1263 c.setBitmap(preview);
1264 Paint p = mCachedShortcutPreviewPaint.get();
1265 if (p == null) {
1266 p = new Paint();
1267 ColorMatrix colorMatrix = new ColorMatrix();
1268 colorMatrix.setSaturation(0);
1269 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1270 p.setAlpha((int) (255 * 0.06f));
1271 //float density = 1f;
1272 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1273 mCachedShortcutPreviewPaint.set(p);
1274 }
1275 c.drawBitmap(tempBitmap, 0, 0, p);
1276 c.setBitmap(null);
1277
1278 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1279
Winson Chungb44b5242011-06-13 11:32:14 -07001280 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001281 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001282
Michael Jurkadac85912012-05-18 15:04:49 -07001283 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1284 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1285 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001286 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001287 String packageName = provider.getPackageName();
1288 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1289 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001290
Michael Jurka038f9d82011-11-03 13:50:45 -07001291 Drawable drawable = null;
1292 if (previewImage != 0) {
1293 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1294 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001295 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001296 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001297 }
1298 }
1299
Michael Jurka038f9d82011-11-03 13:50:45 -07001300 int bitmapWidth;
1301 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001302 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001303 boolean widgetPreviewExists = (drawable != null);
1304 if (widgetPreviewExists) {
1305 bitmapWidth = drawable.getIntrinsicWidth();
1306 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001307 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001308 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001309 if (cellHSpan < 1) cellHSpan = 1;
1310 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001311
1312 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1313 .getDrawable(R.drawable.widget_preview_tile);
1314 final int previewDrawableWidth = previewDrawable
1315 .getIntrinsicWidth();
1316 final int previewDrawableHeight = previewDrawable
1317 .getIntrinsicHeight();
1318 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1319 bitmapHeight = previewDrawableHeight * cellVSpan;
1320
1321 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1322 Config.ARGB_8888);
1323 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1324 c.setBitmap(defaultPreview);
1325 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1326 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1327 Shader.TileMode.REPEAT);
1328 previewDrawable.draw(c);
1329 c.setBitmap(null);
1330
1331 // Draw the icon in the top left corner
1332 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1333 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1334 float iconScale = Math.min((float) smallestSide
1335 / (mAppIconSize + 2 * minOffset), 1f);
1336
1337 try {
1338 Drawable icon = null;
1339 int hoffset =
1340 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1341 int yoffset =
1342 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1343 if (iconId > 0)
1344 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001345 if (icon != null) {
1346 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1347 yoffset, (int) (mAppIconSize * iconScale),
1348 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001349 }
Michael Jurkadac85912012-05-18 15:04:49 -07001350 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001351 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001352 }
1353
Michael Jurkadac85912012-05-18 15:04:49 -07001354 // Scale to fit width only - let the widget preview be clipped in the
1355 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001356 float scale = 1f;
1357 if (bitmapWidth > maxWidth) {
1358 scale = maxWidth / (float) bitmapWidth;
1359 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001360 if (scale != 1f) {
1361 bitmapWidth = (int) (scale * bitmapWidth);
1362 bitmapHeight = (int) (scale * bitmapHeight);
1363 }
1364
Michael Jurkadac85912012-05-18 15:04:49 -07001365 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1366 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001367
Michael Jurkadac85912012-05-18 15:04:49 -07001368 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001369 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001370 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1371 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001372 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001373 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1374 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1375 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1376 c.setBitmap(preview);
1377 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1378 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1379
1380 Paint p = mCachedAppWidgetPreviewPaint.get();
1381 if (p == null) {
1382 p = new Paint();
1383 p.setFilterBitmap(true);
1384 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001385 }
Michael Jurkadac85912012-05-18 15:04:49 -07001386 c.drawBitmap(defaultPreview, src, dest, p);
1387 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001388 }
Winson Chungb44b5242011-06-13 11:32:14 -07001389 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001390 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001391
Michael Jurka038f9d82011-11-03 13:50:45 -07001392 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001393 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001394
Winson Chungd2945262011-06-24 15:22:14 -07001395 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001396 final ArrayList<Object> items = new ArrayList<Object>();
1397 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1398 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001399 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001400 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1401 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001402 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001403
Winson Chunge4a647f2011-09-30 14:41:25 -07001404 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001405 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001406 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1407 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001408 }
1409
Winson Chunge4a647f2011-09-30 14:41:25 -07001410 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001411 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001412 layout.setColumnCount(layout.getCellCountX());
1413 for (int i = 0; i < items.size(); ++i) {
1414 Object rawInfo = items.get(i);
1415 PendingAddItemInfo createItemInfo = null;
1416 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1417 R.layout.apps_customize_widget, layout, false);
1418 if (rawInfo instanceof AppWidgetProviderInfo) {
1419 // Fill in the widget information
1420 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1421 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001422
1423 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001424 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001425 createItemInfo.spanX = spanXY[0];
1426 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001427 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001428 createItemInfo.minSpanX = minSpanXY[0];
1429 createItemInfo.minSpanY = minSpanXY[1];
1430
1431 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001432 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001433 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001434 } else if (rawInfo instanceof ResolveInfo) {
1435 // Fill in the shortcuts information
1436 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001437 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001438 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1439 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1440 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001441 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001442 widget.setTag(createItemInfo);
1443 }
1444 widget.setOnClickListener(this);
1445 widget.setOnLongClickListener(this);
1446 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001447 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001448
1449 // Layout each widget
1450 int ix = i % mWidgetCountX;
1451 int iy = i / mWidgetCountX;
1452 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1453 GridLayout.spec(iy, GridLayout.LEFT),
1454 GridLayout.spec(ix, GridLayout.TOP));
1455 lp.width = cellWidth;
1456 lp.height = cellHeight;
1457 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1458 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1459 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1460 layout.addView(widget, lp);
1461 }
1462
Michael Jurka038f9d82011-11-03 13:50:45 -07001463 // wait until a call on onLayout to start loading, because
1464 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1465 // TODO: can we do a measure/layout immediately?
1466 layout.setOnLayoutListener(new Runnable() {
1467 public void run() {
1468 // Load the widget previews
1469 int maxPreviewWidth = cellWidth;
1470 int maxPreviewHeight = cellHeight;
1471 if (layout.getChildCount() > 0) {
1472 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1473 int[] maxSize = w.getPreviewSize();
1474 maxPreviewWidth = maxSize[0];
1475 maxPreviewHeight = maxSize[1];
1476 }
1477 if (immediate) {
1478 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1479 maxPreviewWidth, maxPreviewHeight, null, null);
1480 loadWidgetPreviewsInBackground(null, data);
1481 onSyncWidgetPageItems(data);
1482 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001483 if (mInTransition) {
1484 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1485 } else {
1486 prepareLoadWidgetPreviewsTask(page, items,
1487 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1488 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001489 }
1490 }
1491 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001492 }
1493 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1494 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001495 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1496 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001497 if (task != null) {
1498 // Ensure that this task starts running at the correct priority
1499 task.syncThreadPriority();
1500 }
1501
1502 // Load each of the widget/shortcut previews
1503 ArrayList<Object> items = data.items;
1504 ArrayList<Bitmap> images = data.generatedImages;
1505 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001506 for (int i = 0; i < count; ++i) {
1507 if (task != null) {
1508 // Ensure we haven't been cancelled yet
1509 if (task.isCancelled()) break;
1510 // Before work on each item, ensure that this task is running at the correct
1511 // priority
1512 task.syncThreadPriority();
1513 }
1514
1515 Object rawInfo = items.get(i);
1516 if (rawInfo instanceof AppWidgetProviderInfo) {
1517 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001518 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001519
1520 int maxWidth = Math.min(data.maxImageWidth,
1521 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1522 int maxHeight = Math.min(data.maxImageHeight,
1523 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001524 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001525 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001526 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001527 } else if (rawInfo instanceof ResolveInfo) {
1528 // Fill in the shortcuts information
1529 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001530 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001531 }
1532 }
Winson Chungb44b5242011-06-13 11:32:14 -07001533 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001534
Winson Chungb44b5242011-06-13 11:32:14 -07001535 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001536 if (mInTransition) {
1537 mDeferredSyncWidgetPageItems.add(data);
1538 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001539 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001540 try {
1541 int page = data.page;
1542 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001543
Michael Jurka39e5d172012-03-12 18:36:12 -07001544 ArrayList<Object> items = data.items;
1545 int count = items.size();
1546 for (int i = 0; i < count; ++i) {
1547 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1548 if (widget != null) {
1549 Bitmap preview = data.generatedImages.get(i);
1550 widget.applyPreview(new FastBitmapDrawable(preview), i);
1551 }
1552 }
Winson Chung68e4c642011-11-10 15:48:25 -08001553
Michael Jurka39e5d172012-03-12 18:36:12 -07001554 layout.createHardwareLayer();
1555 invalidate();
1556
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 }
1678 }
1679
1680 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001681 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001682 }
1683
Winson Chung785d2eb2011-04-14 16:08:02 -07001684 /**
1685 * Used by the parent to get the content width to set the tab bar to
1686 * @return
1687 */
1688 public int getPageContentWidth() {
1689 return mContentWidth;
1690 }
1691
Winson Chungb26f3d62011-06-02 10:49:29 -07001692 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001693 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001694 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001695 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001696 // We reset the save index when we change pages so that it will be recalculated on next
1697 // rotation
1698 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001699 }
1700
Winson Chung785d2eb2011-04-14 16:08:02 -07001701 /*
1702 * AllAppsView implementation
1703 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001704 public void setup(Launcher launcher, DragController dragController) {
1705 mLauncher = launcher;
1706 mDragController = dragController;
1707 }
Winson Chung9802ac92012-06-08 16:01:58 -07001708
1709 /**
1710 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1711 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1712 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1713 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1714 */
1715 private void invalidateOnDataChange() {
1716 if (!isDataReady()) {
1717 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1718 // request a layout to trigger the page data when ready.
1719 requestLayout();
1720 } else {
1721 cancelAllTasks();
1722 invalidatePageData();
1723 }
1724 }
1725
Winson Chung785d2eb2011-04-14 16:08:02 -07001726 public void setApps(ArrayList<ApplicationInfo> list) {
1727 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001728 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001729 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001730 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001731 }
1732 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1733 // We add it in place, in alphabetical order
1734 int count = list.size();
1735 for (int i = 0; i < count; ++i) {
1736 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001737 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001738 if (index < 0) {
1739 mApps.add(-(index + 1), info);
1740 }
1741 }
1742 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001743 public void addApps(ArrayList<ApplicationInfo> list) {
1744 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001745 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001746 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001747 }
1748 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1749 ComponentName removeComponent = item.intent.getComponent();
1750 int length = list.size();
1751 for (int i = 0; i < length; ++i) {
1752 ApplicationInfo info = list.get(i);
1753 if (info.intent.getComponent().equals(removeComponent)) {
1754 return i;
1755 }
1756 }
1757 return -1;
1758 }
Winson Chungcd810732012-06-18 16:45:43 -07001759 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1760 int length = list.size();
1761 for (int i = 0; i < length; ++i) {
1762 ApplicationInfo info = list.get(i);
1763 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1764 return i;
1765 }
1766 }
1767 return -1;
1768 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001769 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1770 // loop through all the apps and remove apps that have the same component
1771 int length = list.size();
1772 for (int i = 0; i < length; ++i) {
1773 ApplicationInfo info = list.get(i);
1774 int removeIndex = findAppByComponent(mApps, info);
1775 if (removeIndex > -1) {
1776 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001777 }
1778 }
1779 }
Winson Chungcd810732012-06-18 16:45:43 -07001780 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1781 // loop through all the package names and remove apps that have the same package name
1782 for (String pn : packageNames) {
1783 int removeIndex = findAppByPackage(mApps, pn);
1784 while (removeIndex > -1) {
1785 mApps.remove(removeIndex);
1786 removeIndex = findAppByPackage(mApps, pn);
1787 }
1788 }
1789 }
1790 public void removeApps(ArrayList<String> packageNames) {
1791 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001792 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001793 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001794 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001795 public void updateApps(ArrayList<ApplicationInfo> list) {
1796 // We remove and re-add the updated applications list because it's properties may have
1797 // changed (ie. the title), and this will ensure that the items will be in their proper
1798 // place in the list.
1799 removeAppsWithoutInvalidate(list);
1800 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001801 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001802 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001803 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001804
Winson Chung785d2eb2011-04-14 16:08:02 -07001805 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001806 // If we have reset, then we should not continue to restore the previous state
1807 mSaveInstanceStateItemIndex = -1;
1808
Adam Cohenb64d36e2011-10-17 21:48:02 -07001809 AppsCustomizeTabHost tabHost = getTabHost();
1810 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001811 if (tag != null) {
1812 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1813 tabHost.setCurrentTabFromContent(ContentType.Applications);
1814 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001815 }
Winson Chung649668f2012-01-10 13:07:16 -08001816
Adam Cohenb64d36e2011-10-17 21:48:02 -07001817 if (mCurrentPage != 0) {
1818 invalidatePageData(0);
1819 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001820 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001821
1822 private AppsCustomizeTabHost getTabHost() {
1823 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1824 }
1825
Winson Chung785d2eb2011-04-14 16:08:02 -07001826 public void dumpState() {
1827 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001828 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1829 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001830 }
Adam Cohen4e844012011-11-09 13:48:04 -08001831
Winson Chung785d2eb2011-04-14 16:08:02 -07001832 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001833 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001834 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001835 for (Object i: list) {
1836 if (i instanceof AppWidgetProviderInfo) {
1837 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1838 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1839 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1840 + " initialLayout=" + info.initialLayout
1841 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1842 } else if (i instanceof ResolveInfo) {
1843 ResolveInfo info = (ResolveInfo) i;
1844 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1845 + info.icon);
1846 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001847 }
1848 }
Adam Cohen4e844012011-11-09 13:48:04 -08001849
Winson Chung785d2eb2011-04-14 16:08:02 -07001850 public void surrender() {
1851 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1852 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001853
1854 // Stop all background tasks
1855 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001856 }
Winson Chung007c6982011-06-14 13:27:53 -07001857
Winson Chunge4e50662012-01-23 14:45:13 -08001858 @Override
1859 public void iconPressed(PagedViewIcon icon) {
1860 // Reset the previously pressed icon and store a reference to the pressed icon so that
1861 // we can reset it on return to Launcher (in Launcher.onResume())
1862 if (mPressedIcon != null) {
1863 mPressedIcon.resetDrawableState();
1864 }
1865 mPressedIcon = icon;
1866 }
1867
1868 public void resetDrawableState() {
1869 if (mPressedIcon != null) {
1870 mPressedIcon.resetDrawableState();
1871 mPressedIcon = null;
1872 }
1873 }
Winson Chung68e4c642011-11-10 15:48:25 -08001874
Winson Chungb44b5242011-06-13 11:32:14 -07001875 /*
1876 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1877 * widget previews in the background with the AsyncTasks.
1878 */
Winson Chung68e4c642011-11-10 15:48:25 -08001879 final static int sLookBehindPageCount = 2;
1880 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001881 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001882 final int count = getChildCount();
1883 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1884 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1885 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001886 }
1887 protected int getAssociatedUpperPageBound(int page) {
1888 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001889 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1890 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1891 count - 1);
1892 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001893 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001894
1895 @Override
1896 protected String getCurrentPageDescription() {
1897 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1898 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001899 int count = 0;
1900
Adam Cohen0cd3b642011-10-14 14:58:00 -07001901 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001902 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001903 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001904 } else {
1905 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001906 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001907 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001908 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001909
Michael Jurka8b805b12012-04-18 14:23:14 -07001910 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001911 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001912}