blob: b71726165b7282e9ee88cfbf7b113ff5cc8352a9 [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;
Winson Chungde1af762011-07-21 16:44:07 -070054import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070055import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070056import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070057import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070058import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070059import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070060import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070061import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070062
63import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064import com.android.launcher2.DropTarget.DragObject;
65
Michael Jurka9bc8eba2012-05-21 20:36:44 -070066import java.lang.ref.WeakReference;
Adam Cohenc0dcf592011-06-01 15:30:43 -070067import java.util.ArrayList;
68import java.util.Collections;
69import java.util.Iterator;
70import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070071
Winson Chungb44b5242011-06-13 11:32:14 -070072/**
73 * A simple callback interface which also provides the results of the task.
74 */
75interface AsyncTaskCallback {
76 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
77}
Winson Chung4e076542011-06-23 13:04:10 -070078
Winson Chungb44b5242011-06-13 11:32:14 -070079/**
80 * The data needed to perform either of the custom AsyncTasks.
81 */
82class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070083 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080084 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070085 }
86
Winson Chungb44b5242011-06-13 11:32:14 -070087 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
88 AsyncTaskCallback postR) {
89 page = p;
90 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070091 sourceImages = si;
92 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070093 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070094 doInBackgroundCallback = bgR;
95 postExecuteCallback = postR;
96 }
Michael Jurka038f9d82011-11-03 13:50:45 -070097 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070098 AsyncTaskCallback postR) {
99 page = p;
100 items = l;
Winson Chung4e076542011-06-23 13:04:10 -0700101 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -0700102 maxImageWidth = cw;
103 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700104 doInBackgroundCallback = bgR;
105 postExecuteCallback = postR;
106 }
Winson Chung09945932011-09-20 14:22:40 -0700107 void cleanup(boolean cancelled) {
108 // Clean up any references to source/generated bitmaps
109 if (sourceImages != null) {
110 if (cancelled) {
111 for (Bitmap b : sourceImages) {
112 b.recycle();
113 }
114 }
115 sourceImages.clear();
116 }
117 if (generatedImages != null) {
118 if (cancelled) {
119 for (Bitmap b : generatedImages) {
120 b.recycle();
121 }
122 }
123 generatedImages.clear();
124 }
125 }
Winson Chungb44b5242011-06-13 11:32:14 -0700126 int page;
127 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700128 ArrayList<Bitmap> sourceImages;
129 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700130 int maxImageWidth;
131 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700132 AsyncTaskCallback doInBackgroundCallback;
133 AsyncTaskCallback postExecuteCallback;
134}
Winson Chung4e076542011-06-23 13:04:10 -0700135
Winson Chungb44b5242011-06-13 11:32:14 -0700136/**
137 * A generic template for an async task used in AppsCustomize.
138 */
139class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700140 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700141 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700143 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700144 }
145 @Override
146 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
147 if (params.length != 1) return null;
148 // Load each of the widget previews in the background
149 params[0].doInBackgroundCallback.run(this, params[0]);
150 return params[0];
151 }
152 @Override
153 protected void onPostExecute(AsyncTaskPageData result) {
154 // All the widget previews are loaded, so we can just callback to inflate the page
155 result.postExecuteCallback.run(this, result);
156 }
157
158 void setThreadPriority(int p) {
159 threadPriority = p;
160 }
161 void syncThreadPriority() {
162 Process.setThreadPriority(threadPriority);
163 }
164
165 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700166 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700167 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700168 int threadPriority;
169}
Winson Chungb44b5242011-06-13 11:32:14 -0700170
Michael Jurkadac85912012-05-18 15:04:49 -0700171abstract class WeakReferenceThreadLocal<T> {
172 private ThreadLocal<WeakReference<T>> mThreadLocal;
173 public WeakReferenceThreadLocal() {
174 mThreadLocal = new ThreadLocal<WeakReference<T>>();
175 }
176
177 abstract T initialValue();
178
179 public void set(T t) {
180 mThreadLocal.set(new WeakReference<T>(t));
181 }
182
183 public T get() {
184 WeakReference<T> reference = mThreadLocal.get();
185 T obj;
186 if (reference == null) {
187 obj = initialValue();
188 mThreadLocal.set(new WeakReference<T>(obj));
189 return obj;
190 } else {
191 obj = reference.get();
192 if (obj == null) {
193 obj = initialValue();
194 mThreadLocal.set(new WeakReference<T>(obj));
195 }
196 return obj;
197 }
198 }
199}
200
201class CanvasCache extends WeakReferenceThreadLocal<Canvas> {
202 @Override
203 protected Canvas initialValue() {
204 return new Canvas();
205 }
206}
207
208class PaintCache extends WeakReferenceThreadLocal<Paint> {
209 @Override
210 protected Paint initialValue() {
211 return null;
212 }
213}
214
215class BitmapCache extends WeakReferenceThreadLocal<Bitmap> {
216 @Override
217 protected Bitmap initialValue() {
218 return null;
219 }
220}
221
222class RectCache extends WeakReferenceThreadLocal<Rect> {
223 @Override
224 protected Rect initialValue() {
225 return new Rect();
226 }
227}
228
Winson Chungb44b5242011-06-13 11:32:14 -0700229/**
230 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
231 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700232public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700233 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700234 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
235 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700236 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700237
238 /**
239 * The different content types that this paged view can show.
240 */
241 public enum ContentType {
242 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700243 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700244 }
245
246 // Refs
247 private Launcher mLauncher;
248 private DragController mDragController;
249 private final LayoutInflater mLayoutInflater;
250 private final PackageManager mPackageManager;
251
Winson Chung5afbf7b2011-07-25 11:53:08 -0700252 // Save and Restore
253 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800254 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700255
Winson Chung785d2eb2011-04-14 16:08:02 -0700256 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700257 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700258 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700259
Winson Chung7d7541e2011-09-16 20:14:36 -0700260 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800261 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700262 private int mClingFocusedX;
263 private int mClingFocusedY;
264
Winson Chung1ed747a2011-05-03 16:18:34 -0700265 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700266 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700267 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700268
269 // Dimens
270 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700271 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800272 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700273 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700274 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700275 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700276 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700277 private int mNumAppsPages;
278 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700279
Adam Cohen22f823d2011-09-01 17:22:18 -0700280 // Relating to the scroll and overscroll effects
281 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700282 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700283 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700284 private static float TRANSITION_PIVOT = 0.65f;
285 private static float TRANSITION_MAX_ROTATION = 22;
286 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700288 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700289
Winson Chungb44b5242011-06-13 11:32:14 -0700290 // Previews & outlines
291 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800292 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700293
Adam Cohened66b2b2012-01-23 17:28:51 -0800294 private Runnable mInflateWidgetRunnable = null;
295 private Runnable mBindWidgetRunnable = null;
296 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700297 static final int WIDGET_PRELOAD_PENDING = 0;
298 static final int WIDGET_BOUND = 1;
299 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800300 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
301 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800302 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800303 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800304
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700305 private Toast mWidgetInstructionToast;
306
Michael Jurka39e5d172012-03-12 18:36:12 -0700307 // Deferral of loading widget previews during launcher transitions
308 private boolean mInTransition;
309 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
310 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700311 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
312 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700313
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700314 private Rect mTmpRect = new Rect();
315
Michael Jurkadac85912012-05-18 15:04:49 -0700316 // Used for drawing shortcut previews
317 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
318 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
319 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
320
321 // Used for drawing widget previews
322 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
323 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
324 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
325 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
326
Winson Chung785d2eb2011-04-14 16:08:02 -0700327 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
328 super(context, attrs);
329 mLayoutInflater = LayoutInflater.from(context);
330 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700331 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700332 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700333 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700334 mCanvas = new Canvas();
335 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700336
337 // Save the default widget preview background
338 Resources resources = context.getResources();
Winson Chung70fc4382011-08-08 15:31:33 -0700339 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700340
Winson Chung6032e7e2011-11-08 15:47:17 -0800341 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
342 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
343 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 mWidgetWidthGap =
345 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
346 mWidgetHeightGap =
347 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700348 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
349 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700350 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
351 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700352 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700354
Winson Chung1ed747a2011-05-03 16:18:34 -0700355 // The padding on the non-matched dimension for the default widget preview icons
356 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700357 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700358
359 // Unless otherwise specified this view is important for accessibility.
360 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
361 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
362 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700363 }
364
365 @Override
366 protected void init() {
367 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700368 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700369
370 Context context = getContext();
371 Resources r = context.getResources();
372 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
373 }
374
Winson Chung5afbf7b2011-07-25 11:53:08 -0700375 /** Returns the item index of the center item on this page so that we can restore to this
376 * item index when we rotate. */
377 private int getMiddleComponentIndexOnCurrentPage() {
378 int i = -1;
379 if (getPageCount() > 0) {
380 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700381 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700382 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700383 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
384 int numItemsPerPage = mCellCountX * mCellCountY;
385 int childCount = childrenLayout.getChildCount();
386 if (childCount > 0) {
387 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700388 }
389 } else {
390 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700391 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700392 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
393 int childCount = layout.getChildCount();
394 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700395 i = numApps +
396 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
397 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700398 }
399 }
400 return i;
401 }
402
403 /** Get the index of the item to restore to if we need to restore the current page. */
404 int getSaveInstanceStateIndex() {
405 if (mSaveInstanceStateItemIndex == -1) {
406 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
407 }
408 return mSaveInstanceStateItemIndex;
409 }
410
411 /** Returns the page in the current orientation which is expected to contain the specified
412 * item index. */
413 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700414 if (index < 0) return 0;
415
416 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700417 int numItemsPerPage = mCellCountX * mCellCountY;
418 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700419 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700420 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700421 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
422 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700423 }
424
Winson Chung5afbf7b2011-07-25 11:53:08 -0700425 /** Restores the page for an item at the specified index */
426 void restorePageForIndex(int index) {
427 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700428 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700429 }
430
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700431 private void updatePageCounts() {
432 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
433 (float) (mWidgetCountX * mWidgetCountY));
434 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
435 }
436
Winson Chungf0ea4d32011-06-06 14:27:16 -0700437 protected void onDataReady(int width, int height) {
438 // Note that we transpose the counts in portrait so that we get a similar layout
439 boolean isLandscape = getResources().getConfiguration().orientation ==
440 Configuration.ORIENTATION_LANDSCAPE;
441 int maxCellCountX = Integer.MAX_VALUE;
442 int maxCellCountY = Integer.MAX_VALUE;
443 if (LauncherApplication.isScreenLarge()) {
444 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
445 LauncherModel.getCellCountY());
446 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
447 LauncherModel.getCellCountX());
448 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800449 if (mMaxAppCellCountX > -1) {
450 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
451 }
452 if (mMaxAppCellCountY > -1) {
453 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
454 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700455
456 // Now that the data is ready, we can calculate the content width, the number of cells to
457 // use for each page
458 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
459 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
460 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
461 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
462 mCellCountX = mWidgetSpacingLayout.getCellCountX();
463 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700464 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700465
Winson Chungdb1138b2011-06-30 14:39:35 -0700466 // Force a measure to update recalculate the gaps
467 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
468 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
469 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700471
Michael Jurkae326f182011-11-21 14:05:46 -0800472 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
473 final boolean hostIsTransitioning = host.isTransitioning();
474
Adam Cohen0cd3b642011-10-14 14:58:00 -0700475 // Restore the page
476 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800477 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700478
Winson Chung3f4e1422011-11-17 14:58:51 -0800479 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
480 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
481 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800482 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800483 post(new Runnable() {
484 @Override
485 public void run() {
486 showAllAppsCling();
487 }
488 });
489 }
490 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700491
Winson Chung3f4e1422011-11-17 14:58:51 -0800492 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700493 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800494 mHasShownAllAppsCling = true;
495 // Calculate the position for the cling punch through
496 int[] offset = new int[2];
497 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
498 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
499 // PagedViews are centered horizontally but top aligned
500 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
501 offset[0];
502 pos[1] += offset[1];
503 mLauncher.showFirstRunAllAppsCling(pos);
504 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700505 }
506
507 @Override
508 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
509 int width = MeasureSpec.getSize(widthMeasureSpec);
510 int height = MeasureSpec.getSize(heightMeasureSpec);
511 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700512 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700513 setDataIsReady();
514 setMeasuredDimension(width, height);
515 onDataReady(width, height);
516 }
517 }
518
519 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
520 }
521
Winson Chung785d2eb2011-04-14 16:08:02 -0700522 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700523 // Get the list of widgets and shortcuts
524 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700525 List<AppWidgetProviderInfo> widgets =
526 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700527 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
528 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800529 for (AppWidgetProviderInfo widget : widgets) {
530 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700531 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700532 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
533 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700534 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
535 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700536 if (minSpanX <= LauncherModel.getCellCountX() &&
537 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700538 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700539 } else {
540 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
541 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700542 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800543 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700544 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800545 widget.minWidth + ", " + widget.minHeight + ")");
546 }
547 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700548 mWidgets.addAll(shortcuts);
549 Collections.sort(mWidgets,
550 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700551 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700552 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700553 }
554
555 @Override
556 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700557 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700558 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700559 mLauncher.getWorkspace().isSwitchingState()) return;
560
Winson Chung4b576be2011-04-27 17:40:20 -0700561 if (v instanceof PagedViewIcon) {
562 // Animate some feedback to the click
563 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800564
565 // Lock the drawable state to pressed until we return to Launcher
566 if (mPressedIcon != null) {
567 mPressedIcon.lockDrawableState();
568 }
Winson Chungc7450e32012-04-17 17:34:08 -0700569
Winson Chung18f41f82012-05-09 13:28:10 -0700570 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
571 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
572 // when Launcher resumes and we are still in AllApps.
573 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700574 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
575
Winson Chung4b576be2011-04-27 17:40:20 -0700576 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700577 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700578 if (mWidgetInstructionToast != null) {
579 mWidgetInstructionToast.cancel();
580 }
581 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
582 Toast.LENGTH_SHORT);
583 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700584
Winson Chungd2e87b32011-06-02 10:53:07 -0700585 // Create a little animation to show that the widget can move
586 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
587 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700588 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
589 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700590 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700591 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700592 tydAnim.setDuration(100);
593 bounce.play(tyuAnim).before(tydAnim);
594 bounce.setInterpolator(new AccelerateInterpolator());
595 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700596 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700597 }
598
Winson Chungc6f10b92011-11-14 11:39:07 -0800599 public boolean onKey(View v, int keyCode, KeyEvent event) {
600 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
601 }
602
Winson Chung785d2eb2011-04-14 16:08:02 -0700603 /*
604 * PagedViewWithDraggableItems implementation
605 */
606 @Override
607 protected void determineDraggingStart(android.view.MotionEvent ev) {
608 // Disable dragging by pulling an app down for now.
609 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700610
Winson Chung4b576be2011-04-27 17:40:20 -0700611 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700612 mLauncher.getWorkspace().onDragStartedWithItem(v);
613 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700614 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700615
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700616 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
617 Bundle options = null;
618 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
619 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700620 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
621 info.componentName, null);
622
623 float density = getResources().getDisplayMetrics().density;
624 int xPaddingDips = (int) ((padding.left + padding.right) / density);
625 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
626
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700627 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700628 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
629 mTmpRect.left - xPaddingDips);
630 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
631 mTmpRect.top - yPaddingDips);
632 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
633 mTmpRect.right - xPaddingDips);
634 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
635 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700636 }
637 return options;
638 }
639
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700640 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800641 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700642 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
643
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700645 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 return;
647 }
648
Adam Cohen21a170b2012-05-30 15:17:06 -0700649 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 mBindWidgetRunnable = new Runnable() {
651 @Override
652 public void run() {
653 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700654 // Options will be null for platforms with JB or lower, so this serves as an
655 // SDK level check.
656 if (options == null) {
657 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
658 mWidgetLoadingId, info.componentName)) {
659 mWidgetCleanupState = WIDGET_BOUND;
660 }
661 } else {
662 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
663 mWidgetLoadingId, info.componentName, options)) {
664 mWidgetCleanupState = WIDGET_BOUND;
665 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 }
668 };
669 post(mBindWidgetRunnable);
670
671 mInflateWidgetRunnable = new Runnable() {
672 @Override
673 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700674 if (mWidgetCleanupState != WIDGET_BOUND) {
675 return;
676 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 AppWidgetHostView hostView = mLauncher.
678 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 info.boundWidget = hostView;
680 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800681 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700682 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
683 info.spanY, info, false);
684
685 // We want the first widget layout to be the correct size. This will be important
686 // for width size reporting to the AppWidgetManager.
687 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
688 unScaledSize[1]);
689 lp.x = lp.y = 0;
690 lp.customPosition = true;
691 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800692 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 }
694 };
695 post(mInflateWidgetRunnable);
696 }
697
698 @Override
699 public void onShortPress(View v) {
700 // We are anticipating a long press, and we use this time to load bind and instantiate
701 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700702 if (mCreateWidgetInfo != null) {
703 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
704 cleanupWidgetPreloading(false);
705 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800706 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700707 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 }
709
Adam Cohen0e56cc92012-05-11 15:57:05 -0700710 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
711 if (!widgetWasAdded) {
712 // If the widget was not added, we may need to do further cleanup.
713 PendingAddWidgetInfo info = mCreateWidgetInfo;
714 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700715
716 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700717 // We never did any preloading, so just remove pending callbacks to do so
718 removeCallbacks(mBindWidgetRunnable);
719 removeCallbacks(mInflateWidgetRunnable);
720 } else if (mWidgetCleanupState == WIDGET_BOUND) {
721 // Delete the widget id which was allocated
722 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700723 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
724 }
725
726 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700727 removeCallbacks(mInflateWidgetRunnable);
728 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700729 // Delete the widget id which was allocated
730 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700731 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
732 }
733
Adam Cohen0e56cc92012-05-11 15:57:05 -0700734 // The widget was inflated and added to the DragLayer -- remove it.
735 AppWidgetHostView widget = info.boundWidget;
736 mLauncher.getDragLayer().removeView(widget);
737 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 }
739 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
740 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700741 mCreateWidgetInfo = null;
742 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
744
Adam Cohen7a326642012-02-22 12:03:22 -0800745 @Override
746 public void cleanUpShortPress(View v) {
747 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700748 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800749 }
750 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800751
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700752 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800753 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700754 // Get the widget preview as the drag representation
755 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700756 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700757
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700758 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
759 // we abort the drag.
760 if (image.getDrawable() == null) {
761 mDraggingWidget = false;
762 return false;
763 }
764
Winson Chung4b576be2011-04-27 17:40:20 -0700765 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800766 Bitmap preview;
767 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800768 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700769 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700770 // This can happen in some weird cases involving multi-touch. We can't start dragging
771 // the widget if this is null, so we break out.
772 if (mCreateWidgetInfo == null) {
773 return false;
774 }
775
Adam Cohen1b36dc32012-02-13 19:27:37 -0800776 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
777 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700778 int spanX = createItemInfo.spanX;
779 int spanY = createItemInfo.spanY;
780 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
781 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700782
Winson Chung72d59842012-02-22 13:51:36 -0800783 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
784 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700785 int maxWidth, maxHeight;
786 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
787 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800788 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurkadac85912012-05-18 15:04:49 -0700789 createWidgetInfo.icon, spanX, spanY, maxWidth, maxHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800790
791 // Determine the image view drawable scale relative to the preview
792 float[] mv = new float[9];
793 Matrix m = new Matrix();
794 m.setRectToRect(
795 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
796 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
797 (float) previewDrawable.getIntrinsicHeight()),
798 Matrix.ScaleToFit.START);
799 m.getValues(mv);
800 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700801 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700802 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
803 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
804 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
805 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
806
Winson Chung1120e032011-11-22 16:11:31 -0800807 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700808 mCanvas.save();
Michael Jurkadac85912012-05-18 15:04:49 -0700809 renderDrawableToBitmap(icon, preview, 0, 0,
810 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700811 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700812 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700813 createItemInfo.spanX = createItemInfo.spanY = 1;
814 }
Winson Chung4b576be2011-04-27 17:40:20 -0700815
Michael Jurka8c3339b2012-06-14 16:18:21 -0700816 // Don't clip alpha values for the drag outline if we're using the default widget preview
817 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
818 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700819
Winson Chung1120e032011-11-22 16:11:31 -0800820 // Save the preview for the outline generation, then dim the preview
821 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
822 false);
Winson Chung1120e032011-11-22 16:11:31 -0800823
Winson Chung4b576be2011-04-27 17:40:20 -0700824 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700825 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700826 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800827 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800828 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800829 outline.recycle();
830 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700831 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700832 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800833
Winson Chung4b576be2011-04-27 17:40:20 -0700834 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700836 if (!super.beginDragging(v)) return false;
837
838 if (v instanceof PagedViewIcon) {
839 beginDraggingApplication(v);
840 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700841 if (!beginDraggingWidget(v)) {
842 return false;
843 }
Winson Chung4b576be2011-04-27 17:40:20 -0700844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845
846 // We delay entering spring-loaded mode slightly to make sure the UI
847 // thready is free of any work.
848 postDelayed(new Runnable() {
849 @Override
850 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800851 // We don't enter spring-loaded mode if the drag has been cancelled
852 if (mLauncher.getDragController().isDragging()) {
853 // Dismiss the cling
854 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800855
Adam Cohen1b36dc32012-02-13 19:27:37 -0800856 // Reset the alpha on the dragged icon before we drag
857 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800858
Adam Cohen1b36dc32012-02-13 19:27:37 -0800859 // Go into spring loaded mode (must happen before we startDrag())
860 mLauncher.enterSpringLoadedDragMode();
861 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 }
Winson Chung72d59842012-02-22 13:51:36 -0800863 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800864
Winson Chung785d2eb2011-04-14 16:08:02 -0700865 return true;
866 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800867
Winson Chunga48487a2012-03-20 16:19:37 -0700868 /**
869 * Clean up after dragging.
870 *
871 * @param target where the item was dragged to (can be null if the item was flung)
872 */
873 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700874 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700875 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700876 // Exit spring loaded mode if we have not successfully dropped or have not handled the
877 // drop in Workspace
878 mLauncher.exitSpringLoadedDragMode();
879 }
Winson Chung4b919f82012-05-01 10:44:08 -0700880 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700881 }
882
Winson Chung785d2eb2011-04-14 16:08:02 -0700883 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700884 public View getContent() {
885 return null;
886 }
887
888 @Override
889 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700890 mInTransition = true;
891 if (toWorkspace) {
892 cancelAllTasks();
893 }
894 }
895
896 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700897 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700898 }
899
900 @Override
901 public void onLauncherTransitionStep(Launcher l, float t) {
902 }
903
904 @Override
905 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
906 mInTransition = false;
907 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
908 onSyncWidgetPageItems(d);
909 }
910 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700911 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
912 r.run();
913 }
914 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700915 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700916 }
917
918 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700919 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
920 boolean success) {
921 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
922 if (isFlingToDelete) return;
923
924 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700925
926 // Display an error message if the drag failed due to there not being enough space on the
927 // target layout we were dropping on.
928 if (!success) {
929 boolean showOutOfSpaceMessage = false;
930 if (target instanceof Workspace) {
931 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
932 Workspace workspace = (Workspace) target;
933 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700934 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700935 if (layout != null) {
936 layout.calculateSpans(itemInfo);
937 showOutOfSpaceMessage =
938 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
939 }
940 }
Winson Chungfc79c802011-05-02 13:35:34 -0700941 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700942 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700943 }
Adam Cohen7a326642012-02-22 12:03:22 -0800944
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800945 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700946 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700947 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800948 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700949 }
950
Winson Chunga48487a2012-03-20 16:19:37 -0700951 @Override
952 public void onFlingToDeleteCompleted() {
953 // We just dismiss the drag when we fling, so cleanup here
954 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700955 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700956 mDraggingWidget = false;
957 }
958
959 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800960 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700961 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800962 }
963
Winson Chung7f0acdd2011-09-19 18:34:19 -0700964 @Override
965 protected void onDetachedFromWindow() {
966 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700967 cancelAllTasks();
968 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700969
Michael Jurkae326f182011-11-21 14:05:46 -0800970 public void clearAllWidgetPages() {
971 cancelAllTasks();
972 int count = getChildCount();
973 for (int i = 0; i < count; i++) {
974 View v = getPageAt(i);
975 if (v instanceof PagedViewGridLayout) {
976 ((PagedViewGridLayout) v).removeAllViewsOnPage();
977 mDirtyPageContent.set(i, true);
978 }
979 }
980 }
981
Adam Cohen0cd3b642011-10-14 14:58:00 -0700982 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700983 // Clean up all the async tasks
984 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
985 while (iter.hasNext()) {
986 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
987 task.cancel(false);
988 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700989 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700990
991 // We've already preallocated the views for the data to load into, so clear them as well
992 View v = getPageAt(task.page);
993 if (v instanceof PagedViewGridLayout) {
994 ((PagedViewGridLayout) v).removeAllViewsOnPage();
995 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700996 }
Winson Chung83687b12012-04-25 16:01:01 -0700997 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700998 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700999 }
1000
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001002 if (type == ContentType.Widgets) {
1003 invalidatePageData(mNumAppsPages, true);
1004 } else if (type == ContentType.Applications) {
1005 invalidatePageData(0, true);
1006 }
Winson Chungb44b5242011-06-13 11:32:14 -07001007 }
1008
Adam Cohen0cd3b642011-10-14 14:58:00 -07001009 protected void snapToPage(int whichPage, int delta, int duration) {
1010 super.snapToPage(whichPage, delta, duration);
1011 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001012
1013 // Update the thread priorities given the direction lookahead
1014 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1015 while (iter.hasNext()) {
1016 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001017 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001018 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
1019 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
1020 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1021 } else {
1022 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1023 }
1024 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001025 }
1026
1027 private void updateCurrentTab(int currentPage) {
1028 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -08001029 if (tabHost != null) {
1030 String tag = tabHost.getCurrentTabTag();
1031 if (tag != null) {
1032 if (currentPage >= mNumAppsPages &&
1033 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
1034 tabHost.setCurrentTabFromContent(ContentType.Widgets);
1035 } else if (currentPage < mNumAppsPages &&
1036 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1037 tabHost.setCurrentTabFromContent(ContentType.Applications);
1038 }
Winson Chung6a8103c2011-10-21 11:08:32 -07001039 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001040 }
1041 }
1042
Winson Chung785d2eb2011-04-14 16:08:02 -07001043 /*
1044 * Apps PagedView implementation
1045 */
Winson Chung63257c12011-05-05 17:06:13 -07001046 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1047 int childCount = layout.getChildCount();
1048 for (int i = 0; i < childCount; ++i) {
1049 layout.getChildAt(i).setVisibility(visibility);
1050 }
1051 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001052 private void setupPage(PagedViewCellLayout layout) {
1053 layout.setCellCount(mCellCountX, mCellCountY);
1054 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1055 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1056 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1057
Winson Chung63257c12011-05-05 17:06:13 -07001058 // Note: We force a measure here to get around the fact that when we do layout calculations
1059 // immediately after syncing, we don't have a proper width. That said, we already know the
1060 // expected page width, so we can actually optimize by hiding all the TextView-based
1061 // children that are expensive to measure, and let that happen naturally later.
1062 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001063 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001064 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001065 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001067 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001068 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001069
Winson Chungf314b0e2011-08-16 11:54:27 -07001070 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001071 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -07001072 int numCells = mCellCountX * mCellCountY;
1073 int startIndex = page * numCells;
1074 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001075 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001076
Winson Chung785d2eb2011-04-14 16:08:02 -07001077 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001078 ArrayList<Object> items = new ArrayList<Object>();
1079 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001080 for (int i = startIndex; i < endIndex; ++i) {
1081 ApplicationInfo info = mApps.get(i);
1082 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1083 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001084 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001085 icon.setOnClickListener(this);
1086 icon.setOnLongClickListener(this);
1087 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001088 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001089
1090 int index = i - startIndex;
1091 int x = index % mCellCountX;
1092 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001093 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001094
1095 items.add(info);
1096 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001097 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001098
Winson Chungf0ea4d32011-06-06 14:27:16 -07001099 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001100 }
Winson Chungb44b5242011-06-13 11:32:14 -07001101
1102 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001103 * A helper to return the priority for loading of the specified widget page.
1104 */
1105 private int getWidgetPageLoadPriority(int page) {
1106 // If we are snapping to another page, use that index as the target page index
1107 int toPage = mCurrentPage;
1108 if (mNextPage > -1) {
1109 toPage = mNextPage;
1110 }
1111
1112 // We use the distance from the target page as an initial guess of priority, but if there
1113 // are no pages of higher priority than the page specified, then bump up the priority of
1114 // the specified page.
1115 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1116 int minPageDiff = Integer.MAX_VALUE;
1117 while (iter.hasNext()) {
1118 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001119 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001120 }
1121
1122 int rawPageDiff = Math.abs(page - toPage);
1123 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1124 }
1125 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001126 * Return the appropriate thread priority for loading for a given page (we give the current
1127 * page much higher priority)
1128 */
1129 private int getThreadPriorityForPage(int page) {
1130 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001131 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001132 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001133 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001134 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001135 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001136 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001137 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001138 }
1139 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001140 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001141 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001142 return Math.max(0, pageDiff * sPageSleepDelay);
1143 }
Winson Chungb44b5242011-06-13 11:32:14 -07001144 /**
1145 * Creates and executes a new AsyncTask to load a page of widget previews.
1146 */
1147 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001148 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001149
Winson Chungb44b5242011-06-13 11:32:14 -07001150 // Prune all tasks that are no longer needed
1151 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1152 while (iter.hasNext()) {
1153 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001154 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001155 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1156 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001157 task.cancel(false);
1158 iter.remove();
1159 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001160 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001161 }
1162 }
1163
Winson Chungf314b0e2011-08-16 11:54:27 -07001164 // 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 -07001165 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001166 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001167 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001168 @Override
1169 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001170 try {
Winson Chung09945932011-09-20 14:22:40 -07001171 try {
1172 Thread.sleep(sleepMs);
1173 } catch (Exception e) {}
1174 loadWidgetPreviewsInBackground(task, data);
1175 } finally {
1176 if (task.isCancelled()) {
1177 data.cleanup(true);
1178 }
1179 }
Winson Chungb44b5242011-06-13 11:32:14 -07001180 }
1181 },
1182 new AsyncTaskCallback() {
1183 @Override
1184 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001185 mRunningTasks.remove(task);
1186 if (task.isCancelled()) return;
1187 // do cleanup inside onSyncWidgetPageItems
1188 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001189 }
Winson Chung09945932011-09-20 14:22:40 -07001190 });
Winson Chungb44b5242011-06-13 11:32:14 -07001191
1192 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001193 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001194 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001195 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001196 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1197 mRunningTasks.add(t);
1198 }
Winson Chungb44b5242011-06-13 11:32:14 -07001199
Winson Chung785d2eb2011-04-14 16:08:02 -07001200 /*
1201 * Widgets PagedView implementation
1202 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001203 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001204 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1205 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001206
1207 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001208 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001209 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1210 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001212 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001213 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001214
Winson Chung5fc72b32011-10-11 17:53:58 -07001215 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Michael Jurkadac85912012-05-18 15:04:49 -07001216 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f);
Winson Chung70fc4382011-08-08 15:31:33 -07001217 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001218
Winson Chung5fc72b32011-10-11 17:53:58 -07001219 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Michael Jurkadac85912012-05-18 15:04:49 -07001220 float scale) {
Winson Chung201bc822011-06-20 15:41:53 -07001221 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001222 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001223 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001224 Rect oldBounds = d.copyBounds();
1225 d.setBounds(x, y, x + w, y + h);
1226 d.draw(c);
1227 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001228 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001229 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001230 }
Winson Chung5fc72b32011-10-11 17:53:58 -07001231
Michael Jurkadac85912012-05-18 15:04:49 -07001232 private Bitmap getShortcutPreview(ResolveInfo info, int maxWidth, int maxHeight) {
1233 Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get();
1234 final Canvas c = mCachedShortcutPreviewCanvas.get();
1235 if (tempBitmap == null ||
1236 tempBitmap.getWidth() != maxWidth ||
1237 tempBitmap.getHeight() != maxHeight) {
1238 tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1239 mCachedShortcutPreviewBitmap.set(tempBitmap);
1240 } else {
1241 c.setBitmap(tempBitmap);
1242 c.drawColor(0, PorterDuff.Mode.CLEAR);
1243 c.setBitmap(null);
1244 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001245 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001246 Drawable icon = mIconCache.getFullResIcon(info);
Michael Jurkadac85912012-05-18 15:04:49 -07001247
1248 int paddingTop =
1249 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top);
1250 int paddingLeft =
1251 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left);
1252 int paddingRight =
1253 getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right);
1254
1255 int scaledIconWidth = (maxWidth - paddingLeft - paddingRight);
Michael Jurkadac85912012-05-18 15:04:49 -07001256
1257 renderDrawableToBitmap(
1258 icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth);
1259
1260 Bitmap preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);
1261 c.setBitmap(preview);
1262 Paint p = mCachedShortcutPreviewPaint.get();
1263 if (p == null) {
1264 p = new Paint();
1265 ColorMatrix colorMatrix = new ColorMatrix();
1266 colorMatrix.setSaturation(0);
1267 p.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
1268 p.setAlpha((int) (255 * 0.06f));
1269 //float density = 1f;
1270 //p.setMaskFilter(new BlurMaskFilter(15*density, BlurMaskFilter.Blur.NORMAL));
1271 mCachedShortcutPreviewPaint.set(p);
1272 }
1273 c.drawBitmap(tempBitmap, 0, 0, p);
1274 c.setBitmap(null);
1275
1276 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize);
1277
Winson Chungb44b5242011-06-13 11:32:14 -07001278 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001279 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001280
Michael Jurkadac85912012-05-18 15:04:49 -07001281 private Bitmap getWidgetPreview(ComponentName provider, int previewImage,
1282 int iconId, int cellHSpan, int cellVSpan, int maxWidth,
1283 int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001284 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001285 String packageName = provider.getPackageName();
1286 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1287 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001288
Michael Jurka038f9d82011-11-03 13:50:45 -07001289 Drawable drawable = null;
1290 if (previewImage != 0) {
1291 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1292 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001293 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001294 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001295 }
1296 }
1297
Michael Jurka038f9d82011-11-03 13:50:45 -07001298 int bitmapWidth;
1299 int bitmapHeight;
Michael Jurkadac85912012-05-18 15:04:49 -07001300 Bitmap defaultPreview = null;
Michael Jurka038f9d82011-11-03 13:50:45 -07001301 boolean widgetPreviewExists = (drawable != null);
1302 if (widgetPreviewExists) {
1303 bitmapWidth = drawable.getIntrinsicWidth();
1304 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001305 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001306 // Generate a preview image if we couldn't load one
Michael Jurkac7e52f52012-03-26 06:20:31 -07001307 if (cellHSpan < 1) cellHSpan = 1;
1308 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurkadac85912012-05-18 15:04:49 -07001309
1310 BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
1311 .getDrawable(R.drawable.widget_preview_tile);
1312 final int previewDrawableWidth = previewDrawable
1313 .getIntrinsicWidth();
1314 final int previewDrawableHeight = previewDrawable
1315 .getIntrinsicHeight();
1316 bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
1317 bitmapHeight = previewDrawableHeight * cellVSpan;
1318
1319 defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1320 Config.ARGB_8888);
1321 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1322 c.setBitmap(defaultPreview);
1323 previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
1324 previewDrawable.setTileModeXY(Shader.TileMode.REPEAT,
1325 Shader.TileMode.REPEAT);
1326 previewDrawable.draw(c);
1327 c.setBitmap(null);
1328
1329 // Draw the icon in the top left corner
1330 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1331 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1332 float iconScale = Math.min((float) smallestSide
1333 / (mAppIconSize + 2 * minOffset), 1f);
1334
1335 try {
1336 Drawable icon = null;
1337 int hoffset =
1338 (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
1339 int yoffset =
1340 (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
1341 if (iconId > 0)
1342 icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurkadac85912012-05-18 15:04:49 -07001343 if (icon != null) {
1344 renderDrawableToBitmap(icon, defaultPreview, hoffset,
1345 yoffset, (int) (mAppIconSize * iconScale),
1346 (int) (mAppIconSize * iconScale));
Winson Chung5fc72b32011-10-11 17:53:58 -07001347 }
Michael Jurkadac85912012-05-18 15:04:49 -07001348 } catch (Resources.NotFoundException e) {
Winson Chung1ed747a2011-05-03 16:18:34 -07001349 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001350 }
1351
Michael Jurkadac85912012-05-18 15:04:49 -07001352 // Scale to fit width only - let the widget preview be clipped in the
1353 // vertical dimension
Michael Jurka038f9d82011-11-03 13:50:45 -07001354 float scale = 1f;
1355 if (bitmapWidth > maxWidth) {
1356 scale = maxWidth / (float) bitmapWidth;
1357 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001358 if (scale != 1f) {
1359 bitmapWidth = (int) (scale * bitmapWidth);
1360 bitmapHeight = (int) (scale * bitmapHeight);
1361 }
1362
Michael Jurkadac85912012-05-18 15:04:49 -07001363 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
1364 Config.ARGB_8888);
Michael Jurka038f9d82011-11-03 13:50:45 -07001365
Michael Jurkadac85912012-05-18 15:04:49 -07001366 // Draw the scaled preview into the final bitmap
Michael Jurka038f9d82011-11-03 13:50:45 -07001367 if (widgetPreviewExists) {
Michael Jurkadac85912012-05-18 15:04:49 -07001368 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth,
1369 bitmapHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001370 } else {
Michael Jurkadac85912012-05-18 15:04:49 -07001371 final Canvas c = mCachedAppWidgetPreviewCanvas.get();
1372 final Rect src = mCachedAppWidgetPreviewSrcRect.get();
1373 final Rect dest = mCachedAppWidgetPreviewDestRect.get();
1374 c.setBitmap(preview);
1375 src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
1376 dest.set(0, 0, preview.getWidth(), preview.getHeight());
1377
1378 Paint p = mCachedAppWidgetPreviewPaint.get();
1379 if (p == null) {
1380 p = new Paint();
1381 p.setFilterBitmap(true);
1382 mCachedAppWidgetPreviewPaint.set(p);
Peter Ng8db70002011-10-25 15:40:08 -07001383 }
Michael Jurkadac85912012-05-18 15:04:49 -07001384 c.drawBitmap(defaultPreview, src, dest, p);
1385 c.setBitmap(null);
Winson Chung4b576be2011-04-27 17:40:20 -07001386 }
Winson Chungb44b5242011-06-13 11:32:14 -07001387 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001388 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001389
Michael Jurka038f9d82011-11-03 13:50:45 -07001390 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001391 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001392
Winson Chungd2945262011-06-24 15:22:14 -07001393 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001394 final ArrayList<Object> items = new ArrayList<Object>();
1395 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1396 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001397 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001398 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1399 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001400 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001401
Winson Chunge4a647f2011-09-30 14:41:25 -07001402 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001403 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001404 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1405 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001406 }
1407
Winson Chunge4a647f2011-09-30 14:41:25 -07001408 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001409 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001410 layout.setColumnCount(layout.getCellCountX());
1411 for (int i = 0; i < items.size(); ++i) {
1412 Object rawInfo = items.get(i);
1413 PendingAddItemInfo createItemInfo = null;
1414 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1415 R.layout.apps_customize_widget, layout, false);
1416 if (rawInfo instanceof AppWidgetProviderInfo) {
1417 // Fill in the widget information
1418 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1419 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001420
1421 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001422 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001423 createItemInfo.spanX = spanXY[0];
1424 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001425 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001426 createItemInfo.minSpanX = minSpanXY[0];
1427 createItemInfo.minSpanY = minSpanXY[1];
1428
1429 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001430 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001431 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001432 } else if (rawInfo instanceof ResolveInfo) {
1433 // Fill in the shortcuts information
1434 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001435 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001436 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1437 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1438 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001439 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001440 widget.setTag(createItemInfo);
1441 }
1442 widget.setOnClickListener(this);
1443 widget.setOnLongClickListener(this);
1444 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001445 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001446
1447 // Layout each widget
1448 int ix = i % mWidgetCountX;
1449 int iy = i / mWidgetCountX;
1450 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1451 GridLayout.spec(iy, GridLayout.LEFT),
1452 GridLayout.spec(ix, GridLayout.TOP));
1453 lp.width = cellWidth;
1454 lp.height = cellHeight;
1455 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1456 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1457 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1458 layout.addView(widget, lp);
1459 }
1460
Michael Jurka038f9d82011-11-03 13:50:45 -07001461 // wait until a call on onLayout to start loading, because
1462 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1463 // TODO: can we do a measure/layout immediately?
1464 layout.setOnLayoutListener(new Runnable() {
1465 public void run() {
1466 // Load the widget previews
1467 int maxPreviewWidth = cellWidth;
1468 int maxPreviewHeight = cellHeight;
1469 if (layout.getChildCount() > 0) {
1470 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1471 int[] maxSize = w.getPreviewSize();
1472 maxPreviewWidth = maxSize[0];
1473 maxPreviewHeight = maxSize[1];
1474 }
1475 if (immediate) {
1476 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1477 maxPreviewWidth, maxPreviewHeight, null, null);
1478 loadWidgetPreviewsInBackground(null, data);
1479 onSyncWidgetPageItems(data);
1480 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001481 if (mInTransition) {
1482 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1483 } else {
1484 prepareLoadWidgetPreviewsTask(page, items,
1485 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1486 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001487 }
1488 }
1489 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001490 }
1491 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1492 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001493 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1494 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001495 if (task != null) {
1496 // Ensure that this task starts running at the correct priority
1497 task.syncThreadPriority();
1498 }
1499
1500 // Load each of the widget/shortcut previews
1501 ArrayList<Object> items = data.items;
1502 ArrayList<Bitmap> images = data.generatedImages;
1503 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001504 for (int i = 0; i < count; ++i) {
1505 if (task != null) {
1506 // Ensure we haven't been cancelled yet
1507 if (task.isCancelled()) break;
1508 // Before work on each item, ensure that this task is running at the correct
1509 // priority
1510 task.syncThreadPriority();
1511 }
1512
1513 Object rawInfo = items.get(i);
1514 if (rawInfo instanceof AppWidgetProviderInfo) {
1515 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001516 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001517
1518 int maxWidth = Math.min(data.maxImageWidth,
1519 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1520 int maxHeight = Math.min(data.maxImageHeight,
1521 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001522 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001523 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001524 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001525 } else if (rawInfo instanceof ResolveInfo) {
1526 // Fill in the shortcuts information
1527 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001528 images.add(getShortcutPreview(info, data.maxImageWidth, data.maxImageHeight));
Winson Chungf314b0e2011-08-16 11:54:27 -07001529 }
1530 }
Winson Chungb44b5242011-06-13 11:32:14 -07001531 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001532
Winson Chungb44b5242011-06-13 11:32:14 -07001533 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001534 if (mInTransition) {
1535 mDeferredSyncWidgetPageItems.add(data);
1536 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001537 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001538 try {
1539 int page = data.page;
1540 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001541
Michael Jurka39e5d172012-03-12 18:36:12 -07001542 ArrayList<Object> items = data.items;
1543 int count = items.size();
1544 for (int i = 0; i < count; ++i) {
1545 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1546 if (widget != null) {
1547 Bitmap preview = data.generatedImages.get(i);
1548 widget.applyPreview(new FastBitmapDrawable(preview), i);
1549 }
1550 }
Winson Chung68e4c642011-11-10 15:48:25 -08001551
Michael Jurka39e5d172012-03-12 18:36:12 -07001552 layout.createHardwareLayer();
1553 invalidate();
1554
1555 // Update all thread priorities
1556 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1557 while (iter.hasNext()) {
1558 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1559 int pageIndex = task.page;
1560 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1561 }
1562 } finally {
1563 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001564 }
Winson Chungb44b5242011-06-13 11:32:14 -07001565 }
Winson Chung46af2e82011-05-09 16:00:53 -07001566
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 @Override
1568 public void syncPages() {
1569 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001570 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001571
Adam Cohen0cd3b642011-10-14 14:58:00 -07001572 Context context = getContext();
1573 for (int j = 0; j < mNumWidgetPages; ++j) {
1574 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1575 mWidgetCountY);
1576 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001577 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001578 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001579 }
1580
Adam Cohen0cd3b642011-10-14 14:58:00 -07001581 for (int i = 0; i < mNumAppsPages; ++i) {
1582 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1583 setupPage(layout);
1584 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 }
1586 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001587
Winson Chung785d2eb2011-04-14 16:08:02 -07001588 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001589 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001590 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001591 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001592 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001593 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001594 }
1595 }
1596
Adam Cohen22f823d2011-09-01 17:22:18 -07001597 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1598 // it is in the z-order. This is important to insure touch events are handled correctly.
1599 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001600 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001601 }
1602
Adam Cohenae4f1552011-10-20 00:15:42 -07001603 @Override
1604 protected int indexToPage(int index) {
1605 return getChildCount() - index - 1;
1606 }
1607
Adam Cohen22f823d2011-09-01 17:22:18 -07001608 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1609 @Override
1610 protected void screenScrolled(int screenCenter) {
1611 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001612
1613 for (int i = 0; i < getChildCount(); i++) {
1614 View v = getPageAt(i);
1615 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001616 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001617
1618 float interpolatedProgress =
1619 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1620 float scale = (1 - interpolatedProgress) +
1621 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1622 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001623
Adam Cohen2591f6a2011-10-25 14:36:40 -07001624 float alpha;
1625
Winson Chungd167e2a2012-04-26 13:13:01 -07001626 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001627 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001628 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001629 } else {
1630 // On large screens we need to fade the page as it nears its leftmost position
1631 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1632 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001633
1634 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1635 int pageWidth = v.getMeasuredWidth();
1636 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001637
1638 if (PERFORM_OVERSCROLL_ROTATION) {
1639 if (i == 0 && scrollProgress < 0) {
1640 // Overscroll to the left
1641 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1642 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1643 scale = 1.0f;
1644 alpha = 1.0f;
1645 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001646 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001647 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1648 // Overscroll to the right
1649 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1650 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1651 scale = 1.0f;
1652 alpha = 1.0f;
1653 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001654 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001655 } else {
1656 v.setPivotY(pageHeight / 2.0f);
1657 v.setPivotX(pageWidth / 2.0f);
1658 v.setRotationY(0f);
1659 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001660 }
1661
1662 v.setTranslationX(translationX);
1663 v.setScaleX(scale);
1664 v.setScaleY(scale);
1665 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001666
1667 // If the view has 0 alpha, we set it to be invisible so as to prevent
1668 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001669 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001670 v.setVisibility(INVISIBLE);
1671 } else if (v.getVisibility() != VISIBLE) {
1672 v.setVisibility(VISIBLE);
1673 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001674 }
1675 }
1676 }
1677
1678 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001679 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001680 }
1681
Winson Chung785d2eb2011-04-14 16:08:02 -07001682 /**
1683 * Used by the parent to get the content width to set the tab bar to
1684 * @return
1685 */
1686 public int getPageContentWidth() {
1687 return mContentWidth;
1688 }
1689
Winson Chungb26f3d62011-06-02 10:49:29 -07001690 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001691 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001692 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001693 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001694 // We reset the save index when we change pages so that it will be recalculated on next
1695 // rotation
1696 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001697 }
1698
Winson Chung785d2eb2011-04-14 16:08:02 -07001699 /*
1700 * AllAppsView implementation
1701 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001702 public void setup(Launcher launcher, DragController dragController) {
1703 mLauncher = launcher;
1704 mDragController = dragController;
1705 }
Winson Chung9802ac92012-06-08 16:01:58 -07001706
1707 /**
1708 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1709 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1710 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1711 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1712 */
1713 private void invalidateOnDataChange() {
1714 if (!isDataReady()) {
1715 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1716 // request a layout to trigger the page data when ready.
1717 requestLayout();
1718 } else {
1719 cancelAllTasks();
1720 invalidatePageData();
1721 }
1722 }
1723
Winson Chung785d2eb2011-04-14 16:08:02 -07001724 public void setApps(ArrayList<ApplicationInfo> list) {
1725 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001726 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001727 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001728 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001729 }
1730 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1731 // We add it in place, in alphabetical order
1732 int count = list.size();
1733 for (int i = 0; i < count; ++i) {
1734 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001735 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001736 if (index < 0) {
1737 mApps.add(-(index + 1), info);
1738 }
1739 }
1740 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001741 public void addApps(ArrayList<ApplicationInfo> list) {
1742 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001743 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001744 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001745 }
1746 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1747 ComponentName removeComponent = item.intent.getComponent();
1748 int length = list.size();
1749 for (int i = 0; i < length; ++i) {
1750 ApplicationInfo info = list.get(i);
1751 if (info.intent.getComponent().equals(removeComponent)) {
1752 return i;
1753 }
1754 }
1755 return -1;
1756 }
Winson Chungcd810732012-06-18 16:45:43 -07001757 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1758 int length = list.size();
1759 for (int i = 0; i < length; ++i) {
1760 ApplicationInfo info = list.get(i);
1761 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1762 return i;
1763 }
1764 }
1765 return -1;
1766 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001767 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1768 // loop through all the apps and remove apps that have the same component
1769 int length = list.size();
1770 for (int i = 0; i < length; ++i) {
1771 ApplicationInfo info = list.get(i);
1772 int removeIndex = findAppByComponent(mApps, info);
1773 if (removeIndex > -1) {
1774 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001775 }
1776 }
1777 }
Winson Chungcd810732012-06-18 16:45:43 -07001778 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1779 // loop through all the package names and remove apps that have the same package name
1780 for (String pn : packageNames) {
1781 int removeIndex = findAppByPackage(mApps, pn);
1782 while (removeIndex > -1) {
1783 mApps.remove(removeIndex);
1784 removeIndex = findAppByPackage(mApps, pn);
1785 }
1786 }
1787 }
1788 public void removeApps(ArrayList<String> packageNames) {
1789 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001790 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001791 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001792 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001793 public void updateApps(ArrayList<ApplicationInfo> list) {
1794 // We remove and re-add the updated applications list because it's properties may have
1795 // changed (ie. the title), and this will ensure that the items will be in their proper
1796 // place in the list.
1797 removeAppsWithoutInvalidate(list);
1798 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001799 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001800 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001801 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001802
Winson Chung785d2eb2011-04-14 16:08:02 -07001803 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001804 // If we have reset, then we should not continue to restore the previous state
1805 mSaveInstanceStateItemIndex = -1;
1806
Adam Cohenb64d36e2011-10-17 21:48:02 -07001807 AppsCustomizeTabHost tabHost = getTabHost();
1808 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001809 if (tag != null) {
1810 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1811 tabHost.setCurrentTabFromContent(ContentType.Applications);
1812 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001813 }
Winson Chung649668f2012-01-10 13:07:16 -08001814
Adam Cohenb64d36e2011-10-17 21:48:02 -07001815 if (mCurrentPage != 0) {
1816 invalidatePageData(0);
1817 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001818 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001819
1820 private AppsCustomizeTabHost getTabHost() {
1821 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1822 }
1823
Winson Chung785d2eb2011-04-14 16:08:02 -07001824 public void dumpState() {
1825 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001826 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1827 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001828 }
Adam Cohen4e844012011-11-09 13:48:04 -08001829
Winson Chung785d2eb2011-04-14 16:08:02 -07001830 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001831 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001832 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001833 for (Object i: list) {
1834 if (i instanceof AppWidgetProviderInfo) {
1835 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1836 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1837 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1838 + " initialLayout=" + info.initialLayout
1839 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1840 } else if (i instanceof ResolveInfo) {
1841 ResolveInfo info = (ResolveInfo) i;
1842 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1843 + info.icon);
1844 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001845 }
1846 }
Adam Cohen4e844012011-11-09 13:48:04 -08001847
Winson Chung785d2eb2011-04-14 16:08:02 -07001848 public void surrender() {
1849 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1850 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001851
1852 // Stop all background tasks
1853 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001854 }
Winson Chung007c6982011-06-14 13:27:53 -07001855
Winson Chunge4e50662012-01-23 14:45:13 -08001856 @Override
1857 public void iconPressed(PagedViewIcon icon) {
1858 // Reset the previously pressed icon and store a reference to the pressed icon so that
1859 // we can reset it on return to Launcher (in Launcher.onResume())
1860 if (mPressedIcon != null) {
1861 mPressedIcon.resetDrawableState();
1862 }
1863 mPressedIcon = icon;
1864 }
1865
1866 public void resetDrawableState() {
1867 if (mPressedIcon != null) {
1868 mPressedIcon.resetDrawableState();
1869 mPressedIcon = null;
1870 }
1871 }
Winson Chung68e4c642011-11-10 15:48:25 -08001872
Winson Chungb44b5242011-06-13 11:32:14 -07001873 /*
1874 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1875 * widget previews in the background with the AsyncTasks.
1876 */
Winson Chung68e4c642011-11-10 15:48:25 -08001877 final static int sLookBehindPageCount = 2;
1878 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001879 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001880 final int count = getChildCount();
1881 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1882 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1883 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001884 }
1885 protected int getAssociatedUpperPageBound(int page) {
1886 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001887 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1888 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1889 count - 1);
1890 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001891 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001892
1893 @Override
1894 protected String getCurrentPageDescription() {
1895 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1896 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001897 int count = 0;
1898
Adam Cohen0cd3b642011-10-14 14:58:00 -07001899 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001900 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001901 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001902 } else {
1903 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001904 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001905 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001906 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001907
Michael Jurka8b805b12012-04-18 14:23:14 -07001908 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001909 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001910}