blob: 2f8d128c0b79c498399a2655e725533add534cb9 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070036import android.graphics.MaskFilter;
37import android.graphics.Paint;
Winson Chung70fc4382011-08-08 15:31:33 -070038import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.Rect;
Peter Ng8db70002011-10-25 15:40:08 -070040import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070042import android.os.AsyncTask;
Adam Cohened66b2b2012-01-23 17:28:51 -080043import android.os.Handler;
44import android.os.HandlerThread;
Winson Chungb44b5242011-06-13 11:32:14 -070045import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070046import android.util.AttributeSet;
47import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070048import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080049import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070051import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070052import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080053import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070054import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070055import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070056import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070057import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070058import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070059import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070060
61import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import com.android.launcher2.DropTarget.DragObject;
63
64import java.util.ArrayList;
65import java.util.Collections;
66import java.util.Iterator;
67import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * A simple callback interface which also provides the results of the task.
71 */
72interface AsyncTaskCallback {
73 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
74}
Winson Chung4e076542011-06-23 13:04:10 -070075
Winson Chungb44b5242011-06-13 11:32:14 -070076/**
77 * The data needed to perform either of the custom AsyncTasks.
78 */
79class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070080 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080081 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070082 }
83
Winson Chungb44b5242011-06-13 11:32:14 -070084 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
85 AsyncTaskCallback postR) {
86 page = p;
87 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070088 sourceImages = si;
89 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070090 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070091 doInBackgroundCallback = bgR;
92 postExecuteCallback = postR;
93 }
Michael Jurka038f9d82011-11-03 13:50:45 -070094 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070095 AsyncTaskCallback postR) {
96 page = p;
97 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070098 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070099 maxImageWidth = cw;
100 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700101 doInBackgroundCallback = bgR;
102 postExecuteCallback = postR;
103 }
Winson Chung09945932011-09-20 14:22:40 -0700104 void cleanup(boolean cancelled) {
105 // Clean up any references to source/generated bitmaps
106 if (sourceImages != null) {
107 if (cancelled) {
108 for (Bitmap b : sourceImages) {
109 b.recycle();
110 }
111 }
112 sourceImages.clear();
113 }
114 if (generatedImages != null) {
115 if (cancelled) {
116 for (Bitmap b : generatedImages) {
117 b.recycle();
118 }
119 }
120 generatedImages.clear();
121 }
122 }
Winson Chungb44b5242011-06-13 11:32:14 -0700123 int page;
124 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700125 ArrayList<Bitmap> sourceImages;
126 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700127 int maxImageWidth;
128 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700129 AsyncTaskCallback doInBackgroundCallback;
130 AsyncTaskCallback postExecuteCallback;
131}
Winson Chung4e076542011-06-23 13:04:10 -0700132
Winson Chungb44b5242011-06-13 11:32:14 -0700133/**
134 * A generic template for an async task used in AppsCustomize.
135 */
136class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700137 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700138 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700140 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 }
142 @Override
143 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
144 if (params.length != 1) return null;
145 // Load each of the widget previews in the background
146 params[0].doInBackgroundCallback.run(this, params[0]);
147 return params[0];
148 }
149 @Override
150 protected void onPostExecute(AsyncTaskPageData result) {
151 // All the widget previews are loaded, so we can just callback to inflate the page
152 result.postExecuteCallback.run(this, result);
153 }
154
155 void setThreadPriority(int p) {
156 threadPriority = p;
157 }
158 void syncThreadPriority() {
159 Process.setThreadPriority(threadPriority);
160 }
161
162 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700163 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700164 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700165 int threadPriority;
166}
Winson Chungb44b5242011-06-13 11:32:14 -0700167
168/**
169 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
170 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700171public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800172 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Adam Cohened66b2b2012-01-23 17:28:51 -0800173 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener {
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 static final String LOG_TAG = "AppsCustomizePagedView";
175
176 /**
177 * The different content types that this paged view can show.
178 */
179 public enum ContentType {
180 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700181 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700182 }
183
184 // Refs
185 private Launcher mLauncher;
186 private DragController mDragController;
187 private final LayoutInflater mLayoutInflater;
188 private final PackageManager mPackageManager;
189
Winson Chung5afbf7b2011-07-25 11:53:08 -0700190 // Save and Restore
191 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800192 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700193
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700195 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700196 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700197
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800199 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700200 private int mClingFocusedX;
201 private int mClingFocusedY;
202
Winson Chung1ed747a2011-05-03 16:18:34 -0700203 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700204 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700205 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700206 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700207
208 // Dimens
209 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700210 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800211 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700212 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700213 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700214 private final int mWidgetPreviewIconPaddedDimension;
215 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700216 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700217 private int mNumAppsPages;
218 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700219
Adam Cohen22f823d2011-09-01 17:22:18 -0700220 // Relating to the scroll and overscroll effects
221 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700222 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700223 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700224 private static float TRANSITION_PIVOT = 0.65f;
225 private static float TRANSITION_MAX_ROTATION = 22;
226 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700227 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700228 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700229
Winson Chungb44b5242011-06-13 11:32:14 -0700230 // Previews & outlines
231 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800232 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700233
Adam Cohened66b2b2012-01-23 17:28:51 -0800234 private Runnable mInflateWidgetRunnable = null;
235 private Runnable mBindWidgetRunnable = null;
236 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
237 static final int WIDGET_BOUND = 0;
238 static final int WIDGET_INFLATED = 1;
239 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
240 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800241 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800242
Winson Chung785d2eb2011-04-14 16:08:02 -0700243 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
244 super(context, attrs);
245 mLayoutInflater = LayoutInflater.from(context);
246 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700247 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700248 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700249 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700250 mCanvas = new Canvas();
251 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700252
253 // Save the default widget preview background
254 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700255 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700256 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700257
Winson Chung6032e7e2011-11-08 15:47:17 -0800258 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
259 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
260 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700261 mWidgetWidthGap =
262 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
263 mWidgetHeightGap =
264 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700265 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
266 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700267 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
268 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700269 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700270 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700271
Winson Chung1ed747a2011-05-03 16:18:34 -0700272 // The padding on the non-matched dimension for the default widget preview icons
273 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700274 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700275 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700276 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700277 }
278
279 @Override
280 protected void init() {
281 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700282 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700283
284 Context context = getContext();
285 Resources r = context.getResources();
286 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
287 }
288
Winson Chungf0ea4d32011-06-06 14:27:16 -0700289 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700290 protected void onUnhandledTap(MotionEvent ev) {
291 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700292 // Dismiss AppsCustomize if we tap
293 mLauncher.showWorkspace(true);
294 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700295 }
296
Winson Chung5afbf7b2011-07-25 11:53:08 -0700297 /** Returns the item index of the center item on this page so that we can restore to this
298 * item index when we rotate. */
299 private int getMiddleComponentIndexOnCurrentPage() {
300 int i = -1;
301 if (getPageCount() > 0) {
302 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700303 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700304 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700305 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
306 int numItemsPerPage = mCellCountX * mCellCountY;
307 int childCount = childrenLayout.getChildCount();
308 if (childCount > 0) {
309 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700310 }
311 } else {
312 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700313 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700314 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
315 int childCount = layout.getChildCount();
316 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700317 i = numApps +
318 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
319 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700320 }
321 }
322 return i;
323 }
324
325 /** Get the index of the item to restore to if we need to restore the current page. */
326 int getSaveInstanceStateIndex() {
327 if (mSaveInstanceStateItemIndex == -1) {
328 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
329 }
330 return mSaveInstanceStateItemIndex;
331 }
332
333 /** Returns the page in the current orientation which is expected to contain the specified
334 * item index. */
335 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700336 if (index < 0) return 0;
337
338 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700339 int numItemsPerPage = mCellCountX * mCellCountY;
340 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700341 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700342 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700343 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
344 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 }
346
Winson Chungf0ea4d32011-06-06 14:27:16 -0700347 /**
348 * This differs from isDataReady as this is the test done if isDataReady is not set.
349 */
350 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700351 // We only do this test once, and we default to the Applications page, so we only really
352 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353 // TODO: What if one of them is validly empty
354 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700355 }
356
Winson Chung5afbf7b2011-07-25 11:53:08 -0700357 /** Restores the page for an item at the specified index */
358 void restorePageForIndex(int index) {
359 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700360 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700361 }
362
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700363 private void updatePageCounts() {
364 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
365 (float) (mWidgetCountX * mWidgetCountY));
366 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
367 }
368
Winson Chungf0ea4d32011-06-06 14:27:16 -0700369 protected void onDataReady(int width, int height) {
370 // Note that we transpose the counts in portrait so that we get a similar layout
371 boolean isLandscape = getResources().getConfiguration().orientation ==
372 Configuration.ORIENTATION_LANDSCAPE;
373 int maxCellCountX = Integer.MAX_VALUE;
374 int maxCellCountY = Integer.MAX_VALUE;
375 if (LauncherApplication.isScreenLarge()) {
376 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
377 LauncherModel.getCellCountY());
378 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
379 LauncherModel.getCellCountX());
380 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800381 if (mMaxAppCellCountX > -1) {
382 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
383 }
384 if (mMaxAppCellCountY > -1) {
385 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
386 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700387
388 // Now that the data is ready, we can calculate the content width, the number of cells to
389 // use for each page
390 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
391 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
392 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
393 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
394 mCellCountX = mWidgetSpacingLayout.getCellCountX();
395 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700396 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700397
Winson Chungdb1138b2011-06-30 14:39:35 -0700398 // Force a measure to update recalculate the gaps
399 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
400 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
401 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700402 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700403
Michael Jurkae326f182011-11-21 14:05:46 -0800404 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
405 final boolean hostIsTransitioning = host.isTransitioning();
406
Adam Cohen0cd3b642011-10-14 14:58:00 -0700407 // Restore the page
408 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800409 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700410
Winson Chung3f4e1422011-11-17 14:58:51 -0800411 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
412 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
413 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800414 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 post(new Runnable() {
416 @Override
417 public void run() {
418 showAllAppsCling();
419 }
420 });
421 }
422 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700423
Winson Chung3f4e1422011-11-17 14:58:51 -0800424 void showAllAppsCling() {
425 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
426 mHasShownAllAppsCling = true;
427 // Calculate the position for the cling punch through
428 int[] offset = new int[2];
429 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
430 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
431 // PagedViews are centered horizontally but top aligned
432 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
433 offset[0];
434 pos[1] += offset[1];
435 mLauncher.showFirstRunAllAppsCling(pos);
436 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700437 }
438
439 @Override
440 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
441 int width = MeasureSpec.getSize(widthMeasureSpec);
442 int height = MeasureSpec.getSize(heightMeasureSpec);
443 if (!isDataReady()) {
444 if (testDataReady()) {
445 setDataIsReady();
446 setMeasuredDimension(width, height);
447 onDataReady(width, height);
448 }
449 }
450
451 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
452 }
453
Winson Chung785d2eb2011-04-14 16:08:02 -0700454 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700455 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
456 // by a broadcast receiver, and in order for it to work correctly, we need to know that
457 // the AppWidgetService has already received and processed the same broadcast. Since there
458 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
459 // we should have a more precise way of ensuring the AppWidgetService is up to date.
460 postDelayed(new Runnable() {
461 public void run() {
462 updatePackages();
463 }
464 }, 500);
465 }
466
467 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700468 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700469 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700470 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700471 List<AppWidgetProviderInfo> widgets =
472 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700473 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
474 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800475 for (AppWidgetProviderInfo widget : widgets) {
476 if (widget.minWidth > 0 && widget.minHeight > 0) {
477 mWidgets.add(widget);
478 } else {
479 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
480 widget.minWidth + ", " + widget.minHeight + ")");
481 }
482 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700483 mWidgets.addAll(shortcuts);
484 Collections.sort(mWidgets,
485 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700486 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700487
Winson Chung875de7e2011-06-28 14:25:17 -0700488 if (wasEmpty) {
489 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
490 // a layout to do this test and invalidate the page data when ready.
491 if (testDataReady()) requestLayout();
492 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700493 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700494 invalidatePageData();
495 }
Winson Chung4b576be2011-04-27 17:40:20 -0700496 }
497
498 @Override
499 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700500 // When we have exited all apps or are in transition, disregard clicks
501 if (!mLauncher.isAllAppsCustomizeOpen() ||
502 mLauncher.getWorkspace().isSwitchingState()) return;
503
Winson Chung4b576be2011-04-27 17:40:20 -0700504 if (v instanceof PagedViewIcon) {
505 // Animate some feedback to the click
506 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Michael Jurkae3517d02012-01-12 07:46:24 -0800507 mLauncher.startActivitySafely(appInfo.intent, appInfo);
Winson Chung3b187b82012-01-30 15:11:08 -0800508
509 // Lock the drawable state to pressed until we return to Launcher
510 if (mPressedIcon != null) {
511 mPressedIcon.lockDrawableState();
512 }
Winson Chung4b576be2011-04-27 17:40:20 -0700513 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700514 // Let the user know that they have to long press to add a widget
515 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
516 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700517
Winson Chungd2e87b32011-06-02 10:53:07 -0700518 // Create a little animation to show that the widget can move
519 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
520 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
521 AnimatorSet bounce = new AnimatorSet();
522 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
523 tyuAnim.setDuration(125);
524 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
525 tydAnim.setDuration(100);
526 bounce.play(tyuAnim).before(tydAnim);
527 bounce.setInterpolator(new AccelerateInterpolator());
528 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700529 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700530 }
531
Winson Chungc6f10b92011-11-14 11:39:07 -0800532 public boolean onKey(View v, int keyCode, KeyEvent event) {
533 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
534 }
535
Winson Chung785d2eb2011-04-14 16:08:02 -0700536 /*
537 * PagedViewWithDraggableItems implementation
538 */
539 @Override
540 protected void determineDraggingStart(android.view.MotionEvent ev) {
541 // Disable dragging by pulling an app down for now.
542 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700543
Winson Chung4b576be2011-04-27 17:40:20 -0700544 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700545 mLauncher.getWorkspace().onDragStartedWithItem(v);
546 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700547 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700548
Adam Cohened66b2b2012-01-23 17:28:51 -0800549 private void loadWidgetInBackground(final PendingAddWidgetInfo info) {
550 final AppWidgetProviderInfo pInfo = info.info;
551 if (pInfo.configure != null) {
552 return;
553 }
554
555 mBindWidgetRunnable = new Runnable() {
556 @Override
557 public void run() {
558 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
559 AppWidgetManager.getInstance(mLauncher).bindAppWidgetId(mWidgetLoadingId,
560 info.componentName);
561 mWidgetCleanupState = WIDGET_BOUND;
562 }
563 };
564 post(mBindWidgetRunnable);
565
566 mInflateWidgetRunnable = new Runnable() {
567 @Override
568 public void run() {
569 AppWidgetHostView hostView =
570 mLauncher.getAppWidgetHost().createView(mContext, mWidgetLoadingId, pInfo);
571 info.boundWidget = hostView;
572 mWidgetCleanupState = WIDGET_INFLATED;
573 }
574 };
575 post(mInflateWidgetRunnable);
576 }
577
578 @Override
579 public void onShortPress(View v) {
580 // We are anticipating a long press, and we use this time to load bind and instantiate
581 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen1b36dc32012-02-13 19:27:37 -0800582 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
583 loadWidgetInBackground(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800584 }
585
586 @Override
587 public void cleanUpShortPress(View v) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800588 PendingAddWidgetInfo info = mCreateWidgetInfo;
589 mCreateWidgetInfo = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
591 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
592 }
593 if (mWidgetCleanupState == WIDGET_BOUND) {
594 removeCallbacks(mInflateWidgetRunnable);
595 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
596 AppWidgetHostView widget = info.boundWidget;
597 int widgetId = widget.getAppWidgetId();
598 mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId);
599 }
600 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
601 mWidgetLoadingId = -1;
602 }
603
Winson Chung4b576be2011-04-27 17:40:20 -0700604 private void beginDraggingWidget(View v) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
606 mWidgetLoadingId = -1;
607
Winson Chung4b576be2011-04-27 17:40:20 -0700608 // Get the widget preview as the drag representation
609 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700610 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700611
612 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800613 Bitmap preview;
614 Bitmap outline;
Winson Chung1ed747a2011-05-03 16:18:34 -0700615 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800616 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
617 createItemInfo = createWidgetInfo;
618 mCreateWidgetInfo = null;
Adam Cohenf814aa02011-09-01 13:48:05 -0700619 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800620 int[] size = mLauncher.getWorkspace().estimateItemSize(spanXY[0],
621 spanXY[1], createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700622 createItemInfo.spanX = spanXY[0];
623 createItemInfo.spanY = spanXY[1];
624
Winson Chung1120e032011-11-22 16:11:31 -0800625 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohened66b2b2012-01-23 17:28:51 -0800626 createWidgetInfo.icon, spanXY[0], spanXY[1], size[0], size[1]);
Winson Chung1ed747a2011-05-03 16:18:34 -0700627 } else {
628 // Workaround for the fact that we don't keep the original ResolveInfo associated with
629 // the shortcut around. To get the icon, we just render the preview image (which has
630 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800631 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700632 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800633 Drawable d = image.getDrawable();
634 mCanvas.setBitmap(preview);
635 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700636 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700637 createItemInfo.spanX = createItemInfo.spanY = 1;
638 }
Winson Chung4b576be2011-04-27 17:40:20 -0700639
Peter Ng8db70002011-10-25 15:40:08 -0700640 // We use a custom alpha clip table for the default widget previews
641 Paint alphaClipPaint = null;
642 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700643 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700644 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
645 alphaClipPaint = new Paint();
646 alphaClipPaint.setMaskFilter(alphaClipTable);
647 }
648 }
649
Winson Chung1120e032011-11-22 16:11:31 -0800650 // Save the preview for the outline generation, then dim the preview
651 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
652 false);
Winson Chung1120e032011-11-22 16:11:31 -0800653
Winson Chung4b576be2011-04-27 17:40:20 -0700654 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800655 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700656 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800657 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
658 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700659 DragController.DRAG_ACTION_COPY, null);
Winson Chung1120e032011-11-22 16:11:31 -0800660 outline.recycle();
661 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700662 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800663
Winson Chung4b576be2011-04-27 17:40:20 -0700664 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700666 if (!super.beginDragging(v)) return false;
667
668 if (v instanceof PagedViewIcon) {
669 beginDraggingApplication(v);
670 } else if (v instanceof PagedViewWidget) {
671 beginDraggingWidget(v);
672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673
674 // We delay entering spring-loaded mode slightly to make sure the UI
675 // thready is free of any work.
676 postDelayed(new Runnable() {
677 @Override
678 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800679 // We don't enter spring-loaded mode if the drag has been cancelled
680 if (mLauncher.getDragController().isDragging()) {
681 // Dismiss the cling
682 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800683
Adam Cohen1b36dc32012-02-13 19:27:37 -0800684 // Reset the alpha on the dragged icon before we drag
685 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800686
Adam Cohen1b36dc32012-02-13 19:27:37 -0800687 // Go into spring loaded mode (must happen before we startDrag())
688 mLauncher.enterSpringLoadedDragMode();
689 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 }
691 },150);
692
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 return true;
694 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800695
Winson Chung557d6ed2011-07-08 15:34:52 -0700696 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700698 if (!success || (target != mLauncher.getWorkspace() &&
699 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700700 // Exit spring loaded mode if we have not successfully dropped or have not handled the
701 // drop in Workspace
702 mLauncher.exitSpringLoadedDragMode();
703 }
Adam Cohen446e9402011-09-15 18:21:21 -0700704 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 }
706
Winson Chung785d2eb2011-04-14 16:08:02 -0700707 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700708 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700709 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700710
711 // Display an error message if the drag failed due to there not being enough space on the
712 // target layout we were dropping on.
713 if (!success) {
714 boolean showOutOfSpaceMessage = false;
715 if (target instanceof Workspace) {
716 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
717 Workspace workspace = (Workspace) target;
718 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700719 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700720 if (layout != null) {
721 layout.calculateSpans(itemInfo);
722 showOutOfSpaceMessage =
723 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
724 }
725 }
Winson Chungfc79c802011-05-02 13:35:34 -0700726 if (showOutOfSpaceMessage) {
727 mLauncher.showOutOfSpaceMessage();
728 }
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800729 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700730 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700731 }
732
Winson Chung7f0acdd2011-09-19 18:34:19 -0700733 @Override
734 protected void onDetachedFromWindow() {
735 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700736 cancelAllTasks();
737 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700738
Michael Jurkae326f182011-11-21 14:05:46 -0800739 public void clearAllWidgetPages() {
740 cancelAllTasks();
741 int count = getChildCount();
742 for (int i = 0; i < count; i++) {
743 View v = getPageAt(i);
744 if (v instanceof PagedViewGridLayout) {
745 ((PagedViewGridLayout) v).removeAllViewsOnPage();
746 mDirtyPageContent.set(i, true);
747 }
748 }
749 }
750
Adam Cohen0cd3b642011-10-14 14:58:00 -0700751 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700752 // Clean up all the async tasks
753 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
754 while (iter.hasNext()) {
755 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
756 task.cancel(false);
757 iter.remove();
758 }
759 }
760
Winson Chung785d2eb2011-04-14 16:08:02 -0700761 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700762 if (type == ContentType.Widgets) {
763 invalidatePageData(mNumAppsPages, true);
764 } else if (type == ContentType.Applications) {
765 invalidatePageData(0, true);
766 }
Winson Chungb44b5242011-06-13 11:32:14 -0700767 }
768
Adam Cohen0cd3b642011-10-14 14:58:00 -0700769 protected void snapToPage(int whichPage, int delta, int duration) {
770 super.snapToPage(whichPage, delta, duration);
771 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800772
773 // Update the thread priorities given the direction lookahead
774 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
775 while (iter.hasNext()) {
776 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
777 int pageIndex = task.page + mNumAppsPages;
778 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
779 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
780 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
781 } else {
782 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
783 }
784 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700785 }
786
787 private void updateCurrentTab(int currentPage) {
788 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800789 if (tabHost != null) {
790 String tag = tabHost.getCurrentTabTag();
791 if (tag != null) {
792 if (currentPage >= mNumAppsPages &&
793 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
794 tabHost.setCurrentTabFromContent(ContentType.Widgets);
795 } else if (currentPage < mNumAppsPages &&
796 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
797 tabHost.setCurrentTabFromContent(ContentType.Applications);
798 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700799 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700800 }
801 }
802
Winson Chung785d2eb2011-04-14 16:08:02 -0700803 /*
804 * Apps PagedView implementation
805 */
Winson Chung63257c12011-05-05 17:06:13 -0700806 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
807 int childCount = layout.getChildCount();
808 for (int i = 0; i < childCount; ++i) {
809 layout.getChildAt(i).setVisibility(visibility);
810 }
811 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 private void setupPage(PagedViewCellLayout layout) {
813 layout.setCellCount(mCellCountX, mCellCountY);
814 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
815 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
816 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
817
Winson Chung63257c12011-05-05 17:06:13 -0700818 // Note: We force a measure here to get around the fact that when we do layout calculations
819 // immediately after syncing, we don't have a proper width. That said, we already know the
820 // expected page width, so we can actually optimize by hiding all the TextView-based
821 // children that are expensive to measure, and let that happen naturally later.
822 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700823 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700825 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700826 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700827 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700828 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700829
Winson Chungf314b0e2011-08-16 11:54:27 -0700830 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700831 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700832 int numCells = mCellCountX * mCellCountY;
833 int startIndex = page * numCells;
834 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700835 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700836
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700838 ArrayList<Object> items = new ArrayList<Object>();
839 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700840 for (int i = startIndex; i < endIndex; ++i) {
841 ApplicationInfo info = mApps.get(i);
842 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
843 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800844 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700845 icon.setOnClickListener(this);
846 icon.setOnLongClickListener(this);
847 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800848 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700849
850 int index = i - startIndex;
851 int x = index % mCellCountX;
852 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700853 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700854
855 items.add(info);
856 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700857 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700858
Winson Chungf0ea4d32011-06-06 14:27:16 -0700859 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700860 }
Winson Chungb44b5242011-06-13 11:32:14 -0700861
862 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800863 * A helper to return the priority for loading of the specified widget page.
864 */
865 private int getWidgetPageLoadPriority(int page) {
866 // If we are snapping to another page, use that index as the target page index
867 int toPage = mCurrentPage;
868 if (mNextPage > -1) {
869 toPage = mNextPage;
870 }
871
872 // We use the distance from the target page as an initial guess of priority, but if there
873 // are no pages of higher priority than the page specified, then bump up the priority of
874 // the specified page.
875 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
876 int minPageDiff = Integer.MAX_VALUE;
877 while (iter.hasNext()) {
878 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
879 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
880 }
881
882 int rawPageDiff = Math.abs(page - toPage);
883 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
884 }
885 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700886 * Return the appropriate thread priority for loading for a given page (we give the current
887 * page much higher priority)
888 */
889 private int getThreadPriorityForPage(int page) {
890 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800891 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700892 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800893 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700894 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800895 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700896 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800897 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700898 }
899 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700900 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800901 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700902 return Math.max(0, pageDiff * sPageSleepDelay);
903 }
Winson Chungb44b5242011-06-13 11:32:14 -0700904 /**
905 * Creates and executes a new AsyncTask to load a page of widget previews.
906 */
907 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700908 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800909
Winson Chungb44b5242011-06-13 11:32:14 -0700910 // Prune all tasks that are no longer needed
911 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
912 while (iter.hasNext()) {
913 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800914 int taskPage = task.page + mNumAppsPages;
915 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
916 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700917 task.cancel(false);
918 iter.remove();
919 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800920 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700921 }
922 }
923
Winson Chungf314b0e2011-08-16 11:54:27 -0700924 // We introduce a slight delay to order the loading of side pages so that we don't thrash
Adam Cohen0cd3b642011-10-14 14:58:00 -0700925 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700926 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700927 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700928 @Override
929 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700930 try {
Winson Chung09945932011-09-20 14:22:40 -0700931 try {
932 Thread.sleep(sleepMs);
933 } catch (Exception e) {}
934 loadWidgetPreviewsInBackground(task, data);
935 } finally {
936 if (task.isCancelled()) {
937 data.cleanup(true);
938 }
939 }
Winson Chungb44b5242011-06-13 11:32:14 -0700940 }
941 },
942 new AsyncTaskCallback() {
943 @Override
944 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700945 try {
946 mRunningTasks.remove(task);
947 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700948 onSyncWidgetPageItems(data);
949 } finally {
950 data.cleanup(task.isCancelled());
951 }
Winson Chungb44b5242011-06-13 11:32:14 -0700952 }
Winson Chung09945932011-09-20 14:22:40 -0700953 });
Winson Chungb44b5242011-06-13 11:32:14 -0700954
955 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700956 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700957 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800958 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700959 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
960 mRunningTasks.add(t);
961 }
Winson Chungb44b5242011-06-13 11:32:14 -0700962
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 /*
964 * Widgets PagedView implementation
965 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700966 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700967 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
968 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700969
970 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700971 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700972 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
973 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700975 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700977
Winson Chung5fc72b32011-10-11 17:53:58 -0700978 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
979 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700980 }
Michael Jurka92f3d462011-11-22 21:02:29 -0800981
Winson Chung5fc72b32011-10-11 17:53:58 -0700982 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
983 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700984 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700985 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700986 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700987 Rect oldBounds = d.copyBounds();
988 d.setBounds(x, y, x + w, y + h);
989 d.draw(c);
990 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -0700991 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700992 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700993 }
Michael Jurka038f9d82011-11-03 13:50:45 -0700994 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700995 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -0700996 int offset = 0;
997 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -0700998 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700999
Winson Chung1ed747a2011-05-03 16:18:34 -07001000 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001001 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001002 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001003 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001004 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001005
Michael Jurka038f9d82011-11-03 13:50:45 -07001006 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1007 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001008 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001009 String packageName = provider.getPackageName();
1010 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1011 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001012
Michael Jurka038f9d82011-11-03 13:50:45 -07001013 Drawable drawable = null;
1014 if (previewImage != 0) {
1015 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1016 if (drawable == null) {
1017 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1018 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001019 }
1020 }
1021
Michael Jurka038f9d82011-11-03 13:50:45 -07001022 int bitmapWidth;
1023 int bitmapHeight;
1024 boolean widgetPreviewExists = (drawable != null);
1025 if (widgetPreviewExists) {
1026 bitmapWidth = drawable.getIntrinsicWidth();
1027 bitmapHeight = drawable.getIntrinsicHeight();
1028
1029 // Cap the size so widget previews don't appear larger than the actual widget
1030 maxWidth = Math.min(maxWidth, mWidgetSpacingLayout.estimateCellWidth(cellHSpan));
1031 maxHeight = Math.min(maxHeight, mWidgetSpacingLayout.estimateCellHeight(cellVSpan));
1032 } else {
1033 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001034 // TODO: This actually uses the apps customize cell layout params, where as we make want
1035 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001036 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1037 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001038 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001039 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001040 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001041 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001042 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001043 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001044 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001045 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001046 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001047 }
1048
1049 float scale = 1f;
1050 if (bitmapWidth > maxWidth) {
1051 scale = maxWidth / (float) bitmapWidth;
1052 }
1053 if (bitmapHeight * scale > maxHeight) {
1054 scale = maxHeight / (float) bitmapHeight;
1055 }
1056 if (scale != 1f) {
1057 bitmapWidth = (int) (scale * bitmapWidth);
1058 bitmapHeight = (int) (scale * bitmapHeight);
1059 }
1060
1061 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1062
1063 if (widgetPreviewExists) {
1064 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1065 } else {
1066 // Generate a preview image if we couldn't load one
1067 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1068 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1069 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001070 if (cellHSpan != 1 || cellVSpan != 1) {
1071 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1072 bitmapHeight);
1073 }
Winson Chung4b576be2011-04-27 17:40:20 -07001074
1075 // Draw the icon in the top left corner
1076 try {
1077 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001078 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1079 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001080 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001081 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001082 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1083
Peter Ng8db70002011-10-25 15:40:08 -07001084 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1085 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001086 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001087 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001088 }
Winson Chungb44b5242011-06-13 11:32:14 -07001089 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001091
Michael Jurka038f9d82011-11-03 13:50:45 -07001092 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001093 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001094
Winson Chungd2945262011-06-24 15:22:14 -07001095 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001096 final ArrayList<Object> items = new ArrayList<Object>();
1097 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1098 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001099 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001100 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1101 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001102 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001103
Winson Chunge4a647f2011-09-30 14:41:25 -07001104 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001105 int offset = page * numItemsPerPage;
1106 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1107 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001108 }
1109
Winson Chunge4a647f2011-09-30 14:41:25 -07001110 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka038f9d82011-11-03 13:50:45 -07001111 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001112 layout.setColumnCount(layout.getCellCountX());
1113 for (int i = 0; i < items.size(); ++i) {
1114 Object rawInfo = items.get(i);
1115 PendingAddItemInfo createItemInfo = null;
1116 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1117 R.layout.apps_customize_widget, layout, false);
1118 if (rawInfo instanceof AppWidgetProviderInfo) {
1119 // Fill in the widget information
1120 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1121 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1122 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka82369a12012-01-12 08:08:38 -08001123 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans);
Winson Chunge4a647f2011-09-30 14:41:25 -07001124 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001125 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001126 } else if (rawInfo instanceof ResolveInfo) {
1127 // Fill in the shortcuts information
1128 ResolveInfo info = (ResolveInfo) rawInfo;
1129 createItemInfo = new PendingAddItemInfo();
1130 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1131 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1132 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001133 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001134 widget.setTag(createItemInfo);
1135 }
1136 widget.setOnClickListener(this);
1137 widget.setOnLongClickListener(this);
1138 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001139 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001140
1141 // Layout each widget
1142 int ix = i % mWidgetCountX;
1143 int iy = i / mWidgetCountX;
1144 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1145 GridLayout.spec(iy, GridLayout.LEFT),
1146 GridLayout.spec(ix, GridLayout.TOP));
1147 lp.width = cellWidth;
1148 lp.height = cellHeight;
1149 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1150 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1151 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1152 layout.addView(widget, lp);
1153 }
1154
Michael Jurka038f9d82011-11-03 13:50:45 -07001155 // wait until a call on onLayout to start loading, because
1156 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1157 // TODO: can we do a measure/layout immediately?
1158 layout.setOnLayoutListener(new Runnable() {
1159 public void run() {
1160 // Load the widget previews
1161 int maxPreviewWidth = cellWidth;
1162 int maxPreviewHeight = cellHeight;
1163 if (layout.getChildCount() > 0) {
1164 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1165 int[] maxSize = w.getPreviewSize();
1166 maxPreviewWidth = maxSize[0];
1167 maxPreviewHeight = maxSize[1];
1168 }
1169 if (immediate) {
1170 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1171 maxPreviewWidth, maxPreviewHeight, null, null);
1172 loadWidgetPreviewsInBackground(null, data);
1173 onSyncWidgetPageItems(data);
1174 } else {
1175 prepareLoadWidgetPreviewsTask(page, items,
1176 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1177 }
1178 }
1179 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001180 }
1181 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1182 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001183 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1184 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001185 if (task != null) {
1186 // Ensure that this task starts running at the correct priority
1187 task.syncThreadPriority();
1188 }
1189
1190 // Load each of the widget/shortcut previews
1191 ArrayList<Object> items = data.items;
1192 ArrayList<Bitmap> images = data.generatedImages;
1193 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001194 for (int i = 0; i < count; ++i) {
1195 if (task != null) {
1196 // Ensure we haven't been cancelled yet
1197 if (task.isCancelled()) break;
1198 // Before work on each item, ensure that this task is running at the correct
1199 // priority
1200 task.syncThreadPriority();
1201 }
1202
1203 Object rawInfo = items.get(i);
1204 if (rawInfo instanceof AppWidgetProviderInfo) {
1205 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001206 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001207 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
1208 cellSpans[0], cellSpans[1], data.maxImageWidth, data.maxImageHeight);
1209 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001210 } else if (rawInfo instanceof ResolveInfo) {
1211 // Fill in the shortcuts information
1212 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001213 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001214 }
1215 }
Winson Chungb44b5242011-06-13 11:32:14 -07001216 }
1217 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1218 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001219 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001220
Winson Chungb44b5242011-06-13 11:32:14 -07001221 ArrayList<Object> items = data.items;
1222 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001223 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001224 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1225 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001226 Bitmap preview = data.generatedImages.get(i);
Michael Jurka038f9d82011-11-03 13:50:45 -07001227 widget.applyPreview(new FastBitmapDrawable(preview), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001228 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001229 }
Winson Chungb44b5242011-06-13 11:32:14 -07001230
Winson Chung68e4c642011-11-10 15:48:25 -08001231 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001232 invalidate();
Winson Chung68e4c642011-11-10 15:48:25 -08001233
Winson Chung68e4c642011-11-10 15:48:25 -08001234 // Update all thread priorities
1235 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1236 while (iter.hasNext()) {
1237 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1238 int pageIndex = task.page + mNumAppsPages;
1239 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1240 }
Winson Chungb44b5242011-06-13 11:32:14 -07001241 }
Winson Chung46af2e82011-05-09 16:00:53 -07001242
Winson Chung785d2eb2011-04-14 16:08:02 -07001243 @Override
1244 public void syncPages() {
1245 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001246 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001247
Adam Cohen0cd3b642011-10-14 14:58:00 -07001248 Context context = getContext();
1249 for (int j = 0; j < mNumWidgetPages; ++j) {
1250 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1251 mWidgetCountY);
1252 setupPage(layout);
1253 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1254 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001255 }
1256
Adam Cohen0cd3b642011-10-14 14:58:00 -07001257 for (int i = 0; i < mNumAppsPages; ++i) {
1258 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1259 setupPage(layout);
1260 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001261 }
1262 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001263
Winson Chung785d2eb2011-04-14 16:08:02 -07001264 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001265 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001266 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001267 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001268 } else {
1269 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 }
1271 }
1272
Adam Cohen22f823d2011-09-01 17:22:18 -07001273 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1274 // it is in the z-order. This is important to insure touch events are handled correctly.
1275 View getPageAt(int index) {
1276 return getChildAt(getChildCount() - index - 1);
1277 }
1278
Adam Cohenae4f1552011-10-20 00:15:42 -07001279 @Override
1280 protected int indexToPage(int index) {
1281 return getChildCount() - index - 1;
1282 }
1283
Adam Cohen22f823d2011-09-01 17:22:18 -07001284 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1285 @Override
1286 protected void screenScrolled(int screenCenter) {
1287 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001288
1289 for (int i = 0; i < getChildCount(); i++) {
1290 View v = getPageAt(i);
1291 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001292 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001293
1294 float interpolatedProgress =
1295 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1296 float scale = (1 - interpolatedProgress) +
1297 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1298 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001299
Adam Cohen2591f6a2011-10-25 14:36:40 -07001300 float alpha;
1301
1302 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1303 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001304 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001305 } else {
1306 // On large screens we need to fade the page as it nears its leftmost position
1307 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1308 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001309
1310 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1311 int pageWidth = v.getMeasuredWidth();
1312 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001313
1314 if (PERFORM_OVERSCROLL_ROTATION) {
1315 if (i == 0 && scrollProgress < 0) {
1316 // Overscroll to the left
1317 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1318 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1319 scale = 1.0f;
1320 alpha = 1.0f;
1321 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001322 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001323 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1324 // Overscroll to the right
1325 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1326 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1327 scale = 1.0f;
1328 alpha = 1.0f;
1329 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001330 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001331 } else {
1332 v.setPivotY(pageHeight / 2.0f);
1333 v.setPivotX(pageWidth / 2.0f);
1334 v.setRotationY(0f);
1335 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001336 }
1337
1338 v.setTranslationX(translationX);
1339 v.setScaleX(scale);
1340 v.setScaleY(scale);
1341 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001342
1343 // If the view has 0 alpha, we set it to be invisible so as to prevent
1344 // it from accepting touches
1345 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1346 v.setVisibility(INVISIBLE);
1347 } else if (v.getVisibility() != VISIBLE) {
1348 v.setVisibility(VISIBLE);
1349 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001350 }
1351 }
1352 }
1353
1354 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001355 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 }
1357
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 /**
1359 * Used by the parent to get the content width to set the tab bar to
1360 * @return
1361 */
1362 public int getPageContentWidth() {
1363 return mContentWidth;
1364 }
1365
Winson Chungb26f3d62011-06-02 10:49:29 -07001366 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001367 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001368 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001369
1370 // We reset the save index when we change pages so that it will be recalculated on next
1371 // rotation
1372 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001373 }
1374
Winson Chung785d2eb2011-04-14 16:08:02 -07001375 /*
1376 * AllAppsView implementation
1377 */
1378 @Override
1379 public void setup(Launcher launcher, DragController dragController) {
1380 mLauncher = launcher;
1381 mDragController = dragController;
1382 }
1383 @Override
1384 public void zoom(float zoom, boolean animate) {
1385 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1386 }
1387 @Override
1388 public boolean isVisible() {
1389 return (getVisibility() == VISIBLE);
1390 }
1391 @Override
1392 public boolean isAnimating() {
1393 return false;
1394 }
1395 @Override
1396 public void setApps(ArrayList<ApplicationInfo> list) {
1397 mApps = list;
1398 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001399 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001400
Winson Chung875de7e2011-06-28 14:25:17 -07001401 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1402 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001403 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001404 }
1405 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1406 // We add it in place, in alphabetical order
1407 int count = list.size();
1408 for (int i = 0; i < count; ++i) {
1409 ApplicationInfo info = list.get(i);
1410 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1411 if (index < 0) {
1412 mApps.add(-(index + 1), info);
1413 }
1414 }
1415 }
1416 @Override
1417 public void addApps(ArrayList<ApplicationInfo> list) {
1418 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001419 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001420 invalidatePageData();
1421 }
1422 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1423 ComponentName removeComponent = item.intent.getComponent();
1424 int length = list.size();
1425 for (int i = 0; i < length; ++i) {
1426 ApplicationInfo info = list.get(i);
1427 if (info.intent.getComponent().equals(removeComponent)) {
1428 return i;
1429 }
1430 }
1431 return -1;
1432 }
1433 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1434 // loop through all the apps and remove apps that have the same component
1435 int length = list.size();
1436 for (int i = 0; i < length; ++i) {
1437 ApplicationInfo info = list.get(i);
1438 int removeIndex = findAppByComponent(mApps, info);
1439 if (removeIndex > -1) {
1440 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001441 }
1442 }
1443 }
1444 @Override
1445 public void removeApps(ArrayList<ApplicationInfo> list) {
1446 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001447 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001448 invalidatePageData();
1449 }
1450 @Override
1451 public void updateApps(ArrayList<ApplicationInfo> list) {
1452 // We remove and re-add the updated applications list because it's properties may have
1453 // changed (ie. the title), and this will ensure that the items will be in their proper
1454 // place in the list.
1455 removeAppsWithoutInvalidate(list);
1456 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001457 updatePageCounts();
1458
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 invalidatePageData();
1460 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001461
Winson Chung785d2eb2011-04-14 16:08:02 -07001462 @Override
1463 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001464 // If we have reset, then we should not continue to restore the previous state
1465 mSaveInstanceStateItemIndex = -1;
1466
Adam Cohenb64d36e2011-10-17 21:48:02 -07001467 AppsCustomizeTabHost tabHost = getTabHost();
1468 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001469 if (tag != null) {
1470 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1471 tabHost.setCurrentTabFromContent(ContentType.Applications);
1472 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001473 }
Winson Chung649668f2012-01-10 13:07:16 -08001474
Adam Cohenb64d36e2011-10-17 21:48:02 -07001475 if (mCurrentPage != 0) {
1476 invalidatePageData(0);
1477 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001478 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001479
1480 private AppsCustomizeTabHost getTabHost() {
1481 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1482 }
1483
Winson Chung785d2eb2011-04-14 16:08:02 -07001484 @Override
1485 public void dumpState() {
1486 // TODO: Dump information related to current list of Applications, Widgets, etc.
1487 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1488 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1489 }
Adam Cohen4e844012011-11-09 13:48:04 -08001490
Winson Chung785d2eb2011-04-14 16:08:02 -07001491 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001492 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001493 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001494 for (Object i: list) {
1495 if (i instanceof AppWidgetProviderInfo) {
1496 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1497 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1498 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1499 + " initialLayout=" + info.initialLayout
1500 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1501 } else if (i instanceof ResolveInfo) {
1502 ResolveInfo info = (ResolveInfo) i;
1503 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1504 + info.icon);
1505 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 }
1507 }
Adam Cohen4e844012011-11-09 13:48:04 -08001508
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 @Override
1510 public void surrender() {
1511 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1512 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001513
1514 // Stop all background tasks
1515 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001516 }
Winson Chung007c6982011-06-14 13:27:53 -07001517
Winson Chunge4e50662012-01-23 14:45:13 -08001518 @Override
1519 public void iconPressed(PagedViewIcon icon) {
1520 // Reset the previously pressed icon and store a reference to the pressed icon so that
1521 // we can reset it on return to Launcher (in Launcher.onResume())
1522 if (mPressedIcon != null) {
1523 mPressedIcon.resetDrawableState();
1524 }
1525 mPressedIcon = icon;
1526 }
1527
1528 public void resetDrawableState() {
1529 if (mPressedIcon != null) {
1530 mPressedIcon.resetDrawableState();
1531 mPressedIcon = null;
1532 }
1533 }
Winson Chung68e4c642011-11-10 15:48:25 -08001534
Winson Chungb44b5242011-06-13 11:32:14 -07001535 /*
1536 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1537 * widget previews in the background with the AsyncTasks.
1538 */
Winson Chung68e4c642011-11-10 15:48:25 -08001539 final static int sLookBehindPageCount = 2;
1540 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001541 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001542 final int count = getChildCount();
1543 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1544 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1545 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001546 }
1547 protected int getAssociatedUpperPageBound(int page) {
1548 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001549 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1550 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1551 count - 1);
1552 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001553 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001554
1555 @Override
1556 protected String getCurrentPageDescription() {
1557 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1558 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001559 int count = 0;
1560
Adam Cohen0cd3b642011-10-14 14:58:00 -07001561 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001562 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001563 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001564 } else {
1565 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001566 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001567 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001568 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001569
Adam Cohend3357b12011-10-18 14:58:11 -07001570 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001571 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001572}