blob: f10e07ebfbff64df79534a39145e9af81a6ae387 [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;
241
Winson Chung785d2eb2011-04-14 16:08:02 -0700242 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
243 super(context, attrs);
244 mLayoutInflater = LayoutInflater.from(context);
245 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700246 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700247 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700248 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700249 mCanvas = new Canvas();
250 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700251
252 // Save the default widget preview background
253 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700254 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700255 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700256
Winson Chung6032e7e2011-11-08 15:47:17 -0800257 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
258 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
259 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700260 mWidgetWidthGap =
261 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
262 mWidgetHeightGap =
263 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700264 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
265 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700266 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
267 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700268 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700270
Winson Chung1ed747a2011-05-03 16:18:34 -0700271 // The padding on the non-matched dimension for the default widget preview icons
272 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700273 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700274 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700275 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700276 }
277
278 @Override
279 protected void init() {
280 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700281 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700282
283 Context context = getContext();
284 Resources r = context.getResources();
285 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
286 }
287
Winson Chungf0ea4d32011-06-06 14:27:16 -0700288 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700289 protected void onUnhandledTap(MotionEvent ev) {
290 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700291 // Dismiss AppsCustomize if we tap
292 mLauncher.showWorkspace(true);
293 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700294 }
295
Winson Chung5afbf7b2011-07-25 11:53:08 -0700296 /** Returns the item index of the center item on this page so that we can restore to this
297 * item index when we rotate. */
298 private int getMiddleComponentIndexOnCurrentPage() {
299 int i = -1;
300 if (getPageCount() > 0) {
301 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700302 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700303 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700304 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
305 int numItemsPerPage = mCellCountX * mCellCountY;
306 int childCount = childrenLayout.getChildCount();
307 if (childCount > 0) {
308 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 }
310 } else {
311 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700312 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700313 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
314 int childCount = layout.getChildCount();
315 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 i = numApps +
317 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
318 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700319 }
320 }
321 return i;
322 }
323
324 /** Get the index of the item to restore to if we need to restore the current page. */
325 int getSaveInstanceStateIndex() {
326 if (mSaveInstanceStateItemIndex == -1) {
327 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
328 }
329 return mSaveInstanceStateItemIndex;
330 }
331
332 /** Returns the page in the current orientation which is expected to contain the specified
333 * item index. */
334 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 if (index < 0) return 0;
336
337 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700338 int numItemsPerPage = mCellCountX * mCellCountY;
339 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700340 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
343 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700344 }
345
Winson Chungf0ea4d32011-06-06 14:27:16 -0700346 /**
347 * This differs from isDataReady as this is the test done if isDataReady is not set.
348 */
349 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700350 // We only do this test once, and we default to the Applications page, so we only really
351 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700352 // TODO: What if one of them is validly empty
353 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700354 }
355
Winson Chung5afbf7b2011-07-25 11:53:08 -0700356 /** Restores the page for an item at the specified index */
357 void restorePageForIndex(int index) {
358 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700359 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700360 }
361
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700362 private void updatePageCounts() {
363 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
364 (float) (mWidgetCountX * mWidgetCountY));
365 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
366 }
367
Winson Chungf0ea4d32011-06-06 14:27:16 -0700368 protected void onDataReady(int width, int height) {
369 // Note that we transpose the counts in portrait so that we get a similar layout
370 boolean isLandscape = getResources().getConfiguration().orientation ==
371 Configuration.ORIENTATION_LANDSCAPE;
372 int maxCellCountX = Integer.MAX_VALUE;
373 int maxCellCountY = Integer.MAX_VALUE;
374 if (LauncherApplication.isScreenLarge()) {
375 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
376 LauncherModel.getCellCountY());
377 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
378 LauncherModel.getCellCountX());
379 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800380 if (mMaxAppCellCountX > -1) {
381 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
382 }
383 if (mMaxAppCellCountY > -1) {
384 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
385 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700386
387 // Now that the data is ready, we can calculate the content width, the number of cells to
388 // use for each page
389 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
390 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
391 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
392 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
393 mCellCountX = mWidgetSpacingLayout.getCellCountX();
394 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700395 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700396
Winson Chungdb1138b2011-06-30 14:39:35 -0700397 // Force a measure to update recalculate the gaps
398 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
399 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
400 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700401 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700402
Michael Jurkae326f182011-11-21 14:05:46 -0800403 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
404 final boolean hostIsTransitioning = host.isTransitioning();
405
Adam Cohen0cd3b642011-10-14 14:58:00 -0700406 // Restore the page
407 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800408 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700409
Winson Chung3f4e1422011-11-17 14:58:51 -0800410 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
411 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
412 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800413 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800414 post(new Runnable() {
415 @Override
416 public void run() {
417 showAllAppsCling();
418 }
419 });
420 }
421 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700422
Winson Chung3f4e1422011-11-17 14:58:51 -0800423 void showAllAppsCling() {
424 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
425 mHasShownAllAppsCling = true;
426 // Calculate the position for the cling punch through
427 int[] offset = new int[2];
428 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
429 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
430 // PagedViews are centered horizontally but top aligned
431 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
432 offset[0];
433 pos[1] += offset[1];
434 mLauncher.showFirstRunAllAppsCling(pos);
435 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700436 }
437
438 @Override
439 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
440 int width = MeasureSpec.getSize(widthMeasureSpec);
441 int height = MeasureSpec.getSize(heightMeasureSpec);
442 if (!isDataReady()) {
443 if (testDataReady()) {
444 setDataIsReady();
445 setMeasuredDimension(width, height);
446 onDataReady(width, height);
447 }
448 }
449
450 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
451 }
452
Winson Chung785d2eb2011-04-14 16:08:02 -0700453 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700454 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
455 // by a broadcast receiver, and in order for it to work correctly, we need to know that
456 // the AppWidgetService has already received and processed the same broadcast. Since there
457 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
458 // we should have a more precise way of ensuring the AppWidgetService is up to date.
459 postDelayed(new Runnable() {
460 public void run() {
461 updatePackages();
462 }
463 }, 500);
464 }
465
466 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700467 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700468 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700469 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700470 List<AppWidgetProviderInfo> widgets =
471 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
473 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800474 for (AppWidgetProviderInfo widget : widgets) {
475 if (widget.minWidth > 0 && widget.minHeight > 0) {
476 mWidgets.add(widget);
477 } else {
478 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
479 widget.minWidth + ", " + widget.minHeight + ")");
480 }
481 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700482 mWidgets.addAll(shortcuts);
483 Collections.sort(mWidgets,
484 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700485 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700486
Winson Chung875de7e2011-06-28 14:25:17 -0700487 if (wasEmpty) {
488 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
489 // a layout to do this test and invalidate the page data when ready.
490 if (testDataReady()) requestLayout();
491 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700492 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700493 invalidatePageData();
494 }
Winson Chung4b576be2011-04-27 17:40:20 -0700495 }
496
497 @Override
498 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700499 // When we have exited all apps or are in transition, disregard clicks
500 if (!mLauncher.isAllAppsCustomizeOpen() ||
501 mLauncher.getWorkspace().isSwitchingState()) return;
502
Winson Chung4b576be2011-04-27 17:40:20 -0700503 if (v instanceof PagedViewIcon) {
504 // Animate some feedback to the click
505 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Michael Jurkae3517d02012-01-12 07:46:24 -0800506 mLauncher.startActivitySafely(appInfo.intent, appInfo);
Winson Chung3b187b82012-01-30 15:11:08 -0800507
508 // Lock the drawable state to pressed until we return to Launcher
509 if (mPressedIcon != null) {
510 mPressedIcon.lockDrawableState();
511 }
Winson Chung4b576be2011-04-27 17:40:20 -0700512 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700513 // Let the user know that they have to long press to add a widget
514 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
515 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700516
Winson Chungd2e87b32011-06-02 10:53:07 -0700517 // Create a little animation to show that the widget can move
518 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
519 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
520 AnimatorSet bounce = new AnimatorSet();
521 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
522 tyuAnim.setDuration(125);
523 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
524 tydAnim.setDuration(100);
525 bounce.play(tyuAnim).before(tydAnim);
526 bounce.setInterpolator(new AccelerateInterpolator());
527 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700528 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700529 }
530
Winson Chungc6f10b92011-11-14 11:39:07 -0800531 public boolean onKey(View v, int keyCode, KeyEvent event) {
532 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
533 }
534
Winson Chung785d2eb2011-04-14 16:08:02 -0700535 /*
536 * PagedViewWithDraggableItems implementation
537 */
538 @Override
539 protected void determineDraggingStart(android.view.MotionEvent ev) {
540 // Disable dragging by pulling an app down for now.
541 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700542
Winson Chung4b576be2011-04-27 17:40:20 -0700543 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700544 mLauncher.getWorkspace().onDragStartedWithItem(v);
545 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700546 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700547
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 private void loadWidgetInBackground(final PendingAddWidgetInfo info) {
549 final AppWidgetProviderInfo pInfo = info.info;
550 if (pInfo.configure != null) {
551 return;
552 }
553
554 mBindWidgetRunnable = new Runnable() {
555 @Override
556 public void run() {
557 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
558 AppWidgetManager.getInstance(mLauncher).bindAppWidgetId(mWidgetLoadingId,
559 info.componentName);
560 mWidgetCleanupState = WIDGET_BOUND;
561 }
562 };
563 post(mBindWidgetRunnable);
564
565 mInflateWidgetRunnable = new Runnable() {
566 @Override
567 public void run() {
568 AppWidgetHostView hostView =
569 mLauncher.getAppWidgetHost().createView(mContext, mWidgetLoadingId, pInfo);
570 info.boundWidget = hostView;
571 mWidgetCleanupState = WIDGET_INFLATED;
572 }
573 };
574 post(mInflateWidgetRunnable);
575 }
576
577 @Override
578 public void onShortPress(View v) {
579 // We are anticipating a long press, and we use this time to load bind and instantiate
580 // the widget. This will need to be cleaned up if it turns out no long press occurs.
581 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
582 loadWidgetInBackground(createWidgetInfo);
583 }
584
585 @Override
586 public void cleanUpShortPress(View v) {
587 PendingAddWidgetInfo info = (PendingAddWidgetInfo) v.getTag();
588 if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
589 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
590 }
591 if (mWidgetCleanupState == WIDGET_BOUND) {
592 removeCallbacks(mInflateWidgetRunnable);
593 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
594 AppWidgetHostView widget = info.boundWidget;
595 int widgetId = widget.getAppWidgetId();
596 mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId);
597 }
598 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
599 mWidgetLoadingId = -1;
600 }
601
Winson Chung4b576be2011-04-27 17:40:20 -0700602 private void beginDraggingWidget(View v) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
604 mWidgetLoadingId = -1;
605
Winson Chung4b576be2011-04-27 17:40:20 -0700606 // Get the widget preview as the drag representation
607 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700608 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700609
610 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800611 Bitmap preview;
612 Bitmap outline;
Winson Chung1ed747a2011-05-03 16:18:34 -0700613 if (createItemInfo instanceof PendingAddWidgetInfo) {
614 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700615 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 int[] size = mLauncher.getWorkspace().estimateItemSize(spanXY[0],
617 spanXY[1], createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700618 createItemInfo.spanX = spanXY[0];
619 createItemInfo.spanY = spanXY[1];
620
Winson Chung1120e032011-11-22 16:11:31 -0800621 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 createWidgetInfo.icon, spanXY[0], spanXY[1], size[0], size[1]);
Winson Chung1ed747a2011-05-03 16:18:34 -0700623 } else {
624 // Workaround for the fact that we don't keep the original ResolveInfo associated with
625 // the shortcut around. To get the icon, we just render the preview image (which has
626 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800627 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700628 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800629 Drawable d = image.getDrawable();
630 mCanvas.setBitmap(preview);
631 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700632 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700633 createItemInfo.spanX = createItemInfo.spanY = 1;
634 }
Winson Chung4b576be2011-04-27 17:40:20 -0700635
Peter Ng8db70002011-10-25 15:40:08 -0700636 // We use a custom alpha clip table for the default widget previews
637 Paint alphaClipPaint = null;
638 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700639 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700640 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
641 alphaClipPaint = new Paint();
642 alphaClipPaint.setMaskFilter(alphaClipTable);
643 }
644 }
645
Winson Chung1120e032011-11-22 16:11:31 -0800646 // Save the preview for the outline generation, then dim the preview
647 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
648 false);
Winson Chung1120e032011-11-22 16:11:31 -0800649
Winson Chung4b576be2011-04-27 17:40:20 -0700650 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800651 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700652 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800653 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
654 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700655 DragController.DRAG_ACTION_COPY, null);
Winson Chung1120e032011-11-22 16:11:31 -0800656 outline.recycle();
657 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700658 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800659
Winson Chung4b576be2011-04-27 17:40:20 -0700660 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700662 if (!super.beginDragging(v)) return false;
663
664 if (v instanceof PagedViewIcon) {
665 beginDraggingApplication(v);
666 } else if (v instanceof PagedViewWidget) {
667 beginDraggingWidget(v);
668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669
670 // We delay entering spring-loaded mode slightly to make sure the UI
671 // thready is free of any work.
672 postDelayed(new Runnable() {
673 @Override
674 public void run() {
675 // Dismiss the cling
676 mLauncher.dismissAllAppsCling(null);
677
678 // Reset the alpha on the dragged icon before we drag
679 resetDrawableState();
680
681 // Go into spring loaded mode (must happen before we startDrag())
682 mLauncher.enterSpringLoadedDragMode();
683 }
684 },150);
685
Winson Chung785d2eb2011-04-14 16:08:02 -0700686 return true;
687 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700688 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700689 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700690 if (!success || (target != mLauncher.getWorkspace() &&
691 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 // Exit spring loaded mode if we have not successfully dropped or have not handled the
693 // drop in Workspace
694 mLauncher.exitSpringLoadedDragMode();
695 }
Adam Cohen446e9402011-09-15 18:21:21 -0700696 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700697
Winson Chung785d2eb2011-04-14 16:08:02 -0700698 }
699
Winson Chung785d2eb2011-04-14 16:08:02 -0700700 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700701 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700702 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700703
704 // Display an error message if the drag failed due to there not being enough space on the
705 // target layout we were dropping on.
706 if (!success) {
707 boolean showOutOfSpaceMessage = false;
708 if (target instanceof Workspace) {
709 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
710 Workspace workspace = (Workspace) target;
711 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700712 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700713 if (layout != null) {
714 layout.calculateSpans(itemInfo);
715 showOutOfSpaceMessage =
716 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
717 }
718 }
Winson Chungfc79c802011-05-02 13:35:34 -0700719 if (showOutOfSpaceMessage) {
720 mLauncher.showOutOfSpaceMessage();
721 }
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800722 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700723 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 }
725
Winson Chung7f0acdd2011-09-19 18:34:19 -0700726 @Override
727 protected void onDetachedFromWindow() {
728 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700729 cancelAllTasks();
730 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700731
Michael Jurkae326f182011-11-21 14:05:46 -0800732 public void clearAllWidgetPages() {
733 cancelAllTasks();
734 int count = getChildCount();
735 for (int i = 0; i < count; i++) {
736 View v = getPageAt(i);
737 if (v instanceof PagedViewGridLayout) {
738 ((PagedViewGridLayout) v).removeAllViewsOnPage();
739 mDirtyPageContent.set(i, true);
740 }
741 }
742 }
743
Adam Cohen0cd3b642011-10-14 14:58:00 -0700744 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700745 // Clean up all the async tasks
746 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
747 while (iter.hasNext()) {
748 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
749 task.cancel(false);
750 iter.remove();
751 }
752 }
753
Winson Chung785d2eb2011-04-14 16:08:02 -0700754 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700755 if (type == ContentType.Widgets) {
756 invalidatePageData(mNumAppsPages, true);
757 } else if (type == ContentType.Applications) {
758 invalidatePageData(0, true);
759 }
Winson Chungb44b5242011-06-13 11:32:14 -0700760 }
761
Adam Cohen0cd3b642011-10-14 14:58:00 -0700762 protected void snapToPage(int whichPage, int delta, int duration) {
763 super.snapToPage(whichPage, delta, duration);
764 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800765
766 // Update the thread priorities given the direction lookahead
767 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
768 while (iter.hasNext()) {
769 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
770 int pageIndex = task.page + mNumAppsPages;
771 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
772 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
773 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
774 } else {
775 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
776 }
777 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700778 }
779
780 private void updateCurrentTab(int currentPage) {
781 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800782 if (tabHost != null) {
783 String tag = tabHost.getCurrentTabTag();
784 if (tag != null) {
785 if (currentPage >= mNumAppsPages &&
786 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
787 tabHost.setCurrentTabFromContent(ContentType.Widgets);
788 } else if (currentPage < mNumAppsPages &&
789 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
790 tabHost.setCurrentTabFromContent(ContentType.Applications);
791 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700792 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700793 }
794 }
795
Winson Chung785d2eb2011-04-14 16:08:02 -0700796 /*
797 * Apps PagedView implementation
798 */
Winson Chung63257c12011-05-05 17:06:13 -0700799 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
800 int childCount = layout.getChildCount();
801 for (int i = 0; i < childCount; ++i) {
802 layout.getChildAt(i).setVisibility(visibility);
803 }
804 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700805 private void setupPage(PagedViewCellLayout layout) {
806 layout.setCellCount(mCellCountX, mCellCountY);
807 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
808 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
809 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
810
Winson Chung63257c12011-05-05 17:06:13 -0700811 // Note: We force a measure here to get around the fact that when we do layout calculations
812 // immediately after syncing, we don't have a proper width. That said, we already know the
813 // expected page width, so we can actually optimize by hiding all the TextView-based
814 // children that are expensive to measure, and let that happen naturally later.
815 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700816 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700817 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700818 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700819 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700820 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700822
Winson Chungf314b0e2011-08-16 11:54:27 -0700823 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 int numCells = mCellCountX * mCellCountY;
826 int startIndex = page * numCells;
827 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700828 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700829
Winson Chung785d2eb2011-04-14 16:08:02 -0700830 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700831 ArrayList<Object> items = new ArrayList<Object>();
832 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 for (int i = startIndex; i < endIndex; ++i) {
834 ApplicationInfo info = mApps.get(i);
835 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
836 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800837 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700838 icon.setOnClickListener(this);
839 icon.setOnLongClickListener(this);
840 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800841 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700842
843 int index = i - startIndex;
844 int x = index % mCellCountX;
845 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700846 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700847
848 items.add(info);
849 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700851
Winson Chungf0ea4d32011-06-06 14:27:16 -0700852 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700853 }
Winson Chungb44b5242011-06-13 11:32:14 -0700854
855 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800856 * A helper to return the priority for loading of the specified widget page.
857 */
858 private int getWidgetPageLoadPriority(int page) {
859 // If we are snapping to another page, use that index as the target page index
860 int toPage = mCurrentPage;
861 if (mNextPage > -1) {
862 toPage = mNextPage;
863 }
864
865 // We use the distance from the target page as an initial guess of priority, but if there
866 // are no pages of higher priority than the page specified, then bump up the priority of
867 // the specified page.
868 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
869 int minPageDiff = Integer.MAX_VALUE;
870 while (iter.hasNext()) {
871 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
872 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
873 }
874
875 int rawPageDiff = Math.abs(page - toPage);
876 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
877 }
878 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700879 * Return the appropriate thread priority for loading for a given page (we give the current
880 * page much higher priority)
881 */
882 private int getThreadPriorityForPage(int page) {
883 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800884 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700885 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800886 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700887 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800888 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700889 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800890 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700891 }
892 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700893 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800894 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700895 return Math.max(0, pageDiff * sPageSleepDelay);
896 }
Winson Chungb44b5242011-06-13 11:32:14 -0700897 /**
898 * Creates and executes a new AsyncTask to load a page of widget previews.
899 */
900 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700901 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800902
Winson Chungb44b5242011-06-13 11:32:14 -0700903 // Prune all tasks that are no longer needed
904 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
905 while (iter.hasNext()) {
906 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800907 int taskPage = task.page + mNumAppsPages;
908 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
909 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700910 task.cancel(false);
911 iter.remove();
912 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800913 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700914 }
915 }
916
Winson Chungf314b0e2011-08-16 11:54:27 -0700917 // 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 -0700918 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700919 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700920 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700921 @Override
922 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700923 try {
Winson Chung09945932011-09-20 14:22:40 -0700924 try {
925 Thread.sleep(sleepMs);
926 } catch (Exception e) {}
927 loadWidgetPreviewsInBackground(task, data);
928 } finally {
929 if (task.isCancelled()) {
930 data.cleanup(true);
931 }
932 }
Winson Chungb44b5242011-06-13 11:32:14 -0700933 }
934 },
935 new AsyncTaskCallback() {
936 @Override
937 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700938 try {
939 mRunningTasks.remove(task);
940 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700941 onSyncWidgetPageItems(data);
942 } finally {
943 data.cleanup(task.isCancelled());
944 }
Winson Chungb44b5242011-06-13 11:32:14 -0700945 }
Winson Chung09945932011-09-20 14:22:40 -0700946 });
Winson Chungb44b5242011-06-13 11:32:14 -0700947
948 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700949 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700950 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800951 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700952 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
953 mRunningTasks.add(t);
954 }
Winson Chungb44b5242011-06-13 11:32:14 -0700955
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 /*
957 * Widgets PagedView implementation
958 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700959 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
961 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700962
963 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700964 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700965 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
966 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700967 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700968 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700970
Winson Chung5fc72b32011-10-11 17:53:58 -0700971 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
972 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700973 }
Michael Jurka92f3d462011-11-22 21:02:29 -0800974
Winson Chung5fc72b32011-10-11 17:53:58 -0700975 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
976 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700977 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700978 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700979 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700980 Rect oldBounds = d.copyBounds();
981 d.setBounds(x, y, x + w, y + h);
982 d.draw(c);
983 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -0700984 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700985 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 }
Michael Jurka038f9d82011-11-03 13:50:45 -0700987 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700988 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -0700989 int offset = 0;
990 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -0700991 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700992
Winson Chung1ed747a2011-05-03 16:18:34 -0700993 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -0700994 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -0700995 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700996 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700997 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700998
Michael Jurka038f9d82011-11-03 13:50:45 -0700999 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1000 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001001 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001002 String packageName = provider.getPackageName();
1003 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1004 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001005
Michael Jurka038f9d82011-11-03 13:50:45 -07001006 Drawable drawable = null;
1007 if (previewImage != 0) {
1008 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1009 if (drawable == null) {
1010 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1011 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001012 }
1013 }
1014
Michael Jurka038f9d82011-11-03 13:50:45 -07001015 int bitmapWidth;
1016 int bitmapHeight;
1017 boolean widgetPreviewExists = (drawable != null);
1018 if (widgetPreviewExists) {
1019 bitmapWidth = drawable.getIntrinsicWidth();
1020 bitmapHeight = drawable.getIntrinsicHeight();
1021
1022 // Cap the size so widget previews don't appear larger than the actual widget
1023 maxWidth = Math.min(maxWidth, mWidgetSpacingLayout.estimateCellWidth(cellHSpan));
1024 maxHeight = Math.min(maxHeight, mWidgetSpacingLayout.estimateCellHeight(cellVSpan));
1025 } else {
1026 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001027 // TODO: This actually uses the apps customize cell layout params, where as we make want
1028 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001029 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1030 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001031 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001032 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001033 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001034 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001035 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001036 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001037 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001038 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001039 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001040 }
1041
1042 float scale = 1f;
1043 if (bitmapWidth > maxWidth) {
1044 scale = maxWidth / (float) bitmapWidth;
1045 }
1046 if (bitmapHeight * scale > maxHeight) {
1047 scale = maxHeight / (float) bitmapHeight;
1048 }
1049 if (scale != 1f) {
1050 bitmapWidth = (int) (scale * bitmapWidth);
1051 bitmapHeight = (int) (scale * bitmapHeight);
1052 }
1053
1054 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1055
1056 if (widgetPreviewExists) {
1057 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1058 } else {
1059 // Generate a preview image if we couldn't load one
1060 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1061 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1062 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001063 if (cellHSpan != 1 || cellVSpan != 1) {
1064 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1065 bitmapHeight);
1066 }
Winson Chung4b576be2011-04-27 17:40:20 -07001067
1068 // Draw the icon in the top left corner
1069 try {
1070 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001071 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1072 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001073 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001074 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001075 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1076
Peter Ng8db70002011-10-25 15:40:08 -07001077 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1078 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001079 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001080 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001081 }
Winson Chungb44b5242011-06-13 11:32:14 -07001082 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001083 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001084
Michael Jurka038f9d82011-11-03 13:50:45 -07001085 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001086 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001087
Winson Chungd2945262011-06-24 15:22:14 -07001088 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001089 final ArrayList<Object> items = new ArrayList<Object>();
1090 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1091 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001092 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001093 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1094 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001095 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001096
Winson Chunge4a647f2011-09-30 14:41:25 -07001097 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001098 int offset = page * numItemsPerPage;
1099 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1100 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001101 }
1102
Winson Chunge4a647f2011-09-30 14:41:25 -07001103 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka038f9d82011-11-03 13:50:45 -07001104 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001105 layout.setColumnCount(layout.getCellCountX());
1106 for (int i = 0; i < items.size(); ++i) {
1107 Object rawInfo = items.get(i);
1108 PendingAddItemInfo createItemInfo = null;
1109 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1110 R.layout.apps_customize_widget, layout, false);
1111 if (rawInfo instanceof AppWidgetProviderInfo) {
1112 // Fill in the widget information
1113 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1114 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1115 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka82369a12012-01-12 08:08:38 -08001116 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans);
Winson Chunge4a647f2011-09-30 14:41:25 -07001117 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001118 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001119 } else if (rawInfo instanceof ResolveInfo) {
1120 // Fill in the shortcuts information
1121 ResolveInfo info = (ResolveInfo) rawInfo;
1122 createItemInfo = new PendingAddItemInfo();
1123 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1124 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1125 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001126 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001127 widget.setTag(createItemInfo);
1128 }
1129 widget.setOnClickListener(this);
1130 widget.setOnLongClickListener(this);
1131 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001132 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001133
1134 // Layout each widget
1135 int ix = i % mWidgetCountX;
1136 int iy = i / mWidgetCountX;
1137 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1138 GridLayout.spec(iy, GridLayout.LEFT),
1139 GridLayout.spec(ix, GridLayout.TOP));
1140 lp.width = cellWidth;
1141 lp.height = cellHeight;
1142 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1143 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1144 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1145 layout.addView(widget, lp);
1146 }
1147
Michael Jurka038f9d82011-11-03 13:50:45 -07001148 // wait until a call on onLayout to start loading, because
1149 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1150 // TODO: can we do a measure/layout immediately?
1151 layout.setOnLayoutListener(new Runnable() {
1152 public void run() {
1153 // Load the widget previews
1154 int maxPreviewWidth = cellWidth;
1155 int maxPreviewHeight = cellHeight;
1156 if (layout.getChildCount() > 0) {
1157 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1158 int[] maxSize = w.getPreviewSize();
1159 maxPreviewWidth = maxSize[0];
1160 maxPreviewHeight = maxSize[1];
1161 }
1162 if (immediate) {
1163 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1164 maxPreviewWidth, maxPreviewHeight, null, null);
1165 loadWidgetPreviewsInBackground(null, data);
1166 onSyncWidgetPageItems(data);
1167 } else {
1168 prepareLoadWidgetPreviewsTask(page, items,
1169 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1170 }
1171 }
1172 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001173 }
1174 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1175 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001176 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1177 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001178 if (task != null) {
1179 // Ensure that this task starts running at the correct priority
1180 task.syncThreadPriority();
1181 }
1182
1183 // Load each of the widget/shortcut previews
1184 ArrayList<Object> items = data.items;
1185 ArrayList<Bitmap> images = data.generatedImages;
1186 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001187 for (int i = 0; i < count; ++i) {
1188 if (task != null) {
1189 // Ensure we haven't been cancelled yet
1190 if (task.isCancelled()) break;
1191 // Before work on each item, ensure that this task is running at the correct
1192 // priority
1193 task.syncThreadPriority();
1194 }
1195
1196 Object rawInfo = items.get(i);
1197 if (rawInfo instanceof AppWidgetProviderInfo) {
1198 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001199 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001200 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
1201 cellSpans[0], cellSpans[1], data.maxImageWidth, data.maxImageHeight);
1202 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001203 } else if (rawInfo instanceof ResolveInfo) {
1204 // Fill in the shortcuts information
1205 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001206 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001207 }
1208 }
Winson Chungb44b5242011-06-13 11:32:14 -07001209 }
1210 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1211 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001212 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001213
Winson Chungb44b5242011-06-13 11:32:14 -07001214 ArrayList<Object> items = data.items;
1215 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001216 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001217 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1218 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001219 Bitmap preview = data.generatedImages.get(i);
Michael Jurka038f9d82011-11-03 13:50:45 -07001220 widget.applyPreview(new FastBitmapDrawable(preview), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001221 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001222 }
Winson Chungb44b5242011-06-13 11:32:14 -07001223
Winson Chung68e4c642011-11-10 15:48:25 -08001224 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001225 invalidate();
Winson Chung68e4c642011-11-10 15:48:25 -08001226
Winson Chung68e4c642011-11-10 15:48:25 -08001227 // Update all thread priorities
1228 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1229 while (iter.hasNext()) {
1230 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1231 int pageIndex = task.page + mNumAppsPages;
1232 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1233 }
Winson Chungb44b5242011-06-13 11:32:14 -07001234 }
Winson Chung46af2e82011-05-09 16:00:53 -07001235
Winson Chung785d2eb2011-04-14 16:08:02 -07001236 @Override
1237 public void syncPages() {
1238 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001239 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001240
Adam Cohen0cd3b642011-10-14 14:58:00 -07001241 Context context = getContext();
1242 for (int j = 0; j < mNumWidgetPages; ++j) {
1243 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1244 mWidgetCountY);
1245 setupPage(layout);
1246 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1247 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001248 }
1249
Adam Cohen0cd3b642011-10-14 14:58:00 -07001250 for (int i = 0; i < mNumAppsPages; ++i) {
1251 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1252 setupPage(layout);
1253 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001254 }
1255 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001256
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001258 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001259 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001260 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001261 } else {
1262 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 }
1264 }
1265
Adam Cohen22f823d2011-09-01 17:22:18 -07001266 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1267 // it is in the z-order. This is important to insure touch events are handled correctly.
1268 View getPageAt(int index) {
1269 return getChildAt(getChildCount() - index - 1);
1270 }
1271
Adam Cohenae4f1552011-10-20 00:15:42 -07001272 @Override
1273 protected int indexToPage(int index) {
1274 return getChildCount() - index - 1;
1275 }
1276
Adam Cohen22f823d2011-09-01 17:22:18 -07001277 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1278 @Override
1279 protected void screenScrolled(int screenCenter) {
1280 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001281
1282 for (int i = 0; i < getChildCount(); i++) {
1283 View v = getPageAt(i);
1284 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001285 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001286
1287 float interpolatedProgress =
1288 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1289 float scale = (1 - interpolatedProgress) +
1290 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1291 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001292
Adam Cohen2591f6a2011-10-25 14:36:40 -07001293 float alpha;
1294
1295 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1296 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001297 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001298 } else {
1299 // On large screens we need to fade the page as it nears its leftmost position
1300 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1301 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001302
1303 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1304 int pageWidth = v.getMeasuredWidth();
1305 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001306
1307 if (PERFORM_OVERSCROLL_ROTATION) {
1308 if (i == 0 && scrollProgress < 0) {
1309 // Overscroll to the left
1310 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1311 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1312 scale = 1.0f;
1313 alpha = 1.0f;
1314 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001315 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001316 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1317 // Overscroll to the right
1318 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1319 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1320 scale = 1.0f;
1321 alpha = 1.0f;
1322 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001323 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001324 } else {
1325 v.setPivotY(pageHeight / 2.0f);
1326 v.setPivotX(pageWidth / 2.0f);
1327 v.setRotationY(0f);
1328 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001329 }
1330
1331 v.setTranslationX(translationX);
1332 v.setScaleX(scale);
1333 v.setScaleY(scale);
1334 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001335
1336 // If the view has 0 alpha, we set it to be invisible so as to prevent
1337 // it from accepting touches
1338 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1339 v.setVisibility(INVISIBLE);
1340 } else if (v.getVisibility() != VISIBLE) {
1341 v.setVisibility(VISIBLE);
1342 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001343 }
1344 }
1345 }
1346
1347 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001348 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001349 }
1350
Winson Chung785d2eb2011-04-14 16:08:02 -07001351 /**
1352 * Used by the parent to get the content width to set the tab bar to
1353 * @return
1354 */
1355 public int getPageContentWidth() {
1356 return mContentWidth;
1357 }
1358
Winson Chungb26f3d62011-06-02 10:49:29 -07001359 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001360 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001361 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001362
1363 // We reset the save index when we change pages so that it will be recalculated on next
1364 // rotation
1365 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001366 }
1367
Winson Chung785d2eb2011-04-14 16:08:02 -07001368 /*
1369 * AllAppsView implementation
1370 */
1371 @Override
1372 public void setup(Launcher launcher, DragController dragController) {
1373 mLauncher = launcher;
1374 mDragController = dragController;
1375 }
1376 @Override
1377 public void zoom(float zoom, boolean animate) {
1378 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1379 }
1380 @Override
1381 public boolean isVisible() {
1382 return (getVisibility() == VISIBLE);
1383 }
1384 @Override
1385 public boolean isAnimating() {
1386 return false;
1387 }
1388 @Override
1389 public void setApps(ArrayList<ApplicationInfo> list) {
1390 mApps = list;
1391 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001392 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001393
Winson Chung875de7e2011-06-28 14:25:17 -07001394 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1395 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001396 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001397 }
1398 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1399 // We add it in place, in alphabetical order
1400 int count = list.size();
1401 for (int i = 0; i < count; ++i) {
1402 ApplicationInfo info = list.get(i);
1403 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1404 if (index < 0) {
1405 mApps.add(-(index + 1), info);
1406 }
1407 }
1408 }
1409 @Override
1410 public void addApps(ArrayList<ApplicationInfo> list) {
1411 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001412 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001413 invalidatePageData();
1414 }
1415 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1416 ComponentName removeComponent = item.intent.getComponent();
1417 int length = list.size();
1418 for (int i = 0; i < length; ++i) {
1419 ApplicationInfo info = list.get(i);
1420 if (info.intent.getComponent().equals(removeComponent)) {
1421 return i;
1422 }
1423 }
1424 return -1;
1425 }
1426 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1427 // loop through all the apps and remove apps that have the same component
1428 int length = list.size();
1429 for (int i = 0; i < length; ++i) {
1430 ApplicationInfo info = list.get(i);
1431 int removeIndex = findAppByComponent(mApps, info);
1432 if (removeIndex > -1) {
1433 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001434 }
1435 }
1436 }
1437 @Override
1438 public void removeApps(ArrayList<ApplicationInfo> list) {
1439 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001440 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001441 invalidatePageData();
1442 }
1443 @Override
1444 public void updateApps(ArrayList<ApplicationInfo> list) {
1445 // We remove and re-add the updated applications list because it's properties may have
1446 // changed (ie. the title), and this will ensure that the items will be in their proper
1447 // place in the list.
1448 removeAppsWithoutInvalidate(list);
1449 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001450 updatePageCounts();
1451
Winson Chung785d2eb2011-04-14 16:08:02 -07001452 invalidatePageData();
1453 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001454
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 @Override
1456 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001457 // If we have reset, then we should not continue to restore the previous state
1458 mSaveInstanceStateItemIndex = -1;
1459
Adam Cohenb64d36e2011-10-17 21:48:02 -07001460 AppsCustomizeTabHost tabHost = getTabHost();
1461 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001462 if (tag != null) {
1463 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1464 tabHost.setCurrentTabFromContent(ContentType.Applications);
1465 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001466 }
Winson Chung649668f2012-01-10 13:07:16 -08001467
Adam Cohenb64d36e2011-10-17 21:48:02 -07001468 if (mCurrentPage != 0) {
1469 invalidatePageData(0);
1470 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001471 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001472
1473 private AppsCustomizeTabHost getTabHost() {
1474 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1475 }
1476
Winson Chung785d2eb2011-04-14 16:08:02 -07001477 @Override
1478 public void dumpState() {
1479 // TODO: Dump information related to current list of Applications, Widgets, etc.
1480 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1481 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1482 }
Adam Cohen4e844012011-11-09 13:48:04 -08001483
Winson Chung785d2eb2011-04-14 16:08:02 -07001484 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001485 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001487 for (Object i: list) {
1488 if (i instanceof AppWidgetProviderInfo) {
1489 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1490 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1491 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1492 + " initialLayout=" + info.initialLayout
1493 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1494 } else if (i instanceof ResolveInfo) {
1495 ResolveInfo info = (ResolveInfo) i;
1496 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1497 + info.icon);
1498 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001499 }
1500 }
Adam Cohen4e844012011-11-09 13:48:04 -08001501
Winson Chung785d2eb2011-04-14 16:08:02 -07001502 @Override
1503 public void surrender() {
1504 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1505 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001506
1507 // Stop all background tasks
1508 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 }
Winson Chung007c6982011-06-14 13:27:53 -07001510
Winson Chunge4e50662012-01-23 14:45:13 -08001511 @Override
1512 public void iconPressed(PagedViewIcon icon) {
1513 // Reset the previously pressed icon and store a reference to the pressed icon so that
1514 // we can reset it on return to Launcher (in Launcher.onResume())
1515 if (mPressedIcon != null) {
1516 mPressedIcon.resetDrawableState();
1517 }
1518 mPressedIcon = icon;
1519 }
1520
1521 public void resetDrawableState() {
1522 if (mPressedIcon != null) {
1523 mPressedIcon.resetDrawableState();
1524 mPressedIcon = null;
1525 }
1526 }
Winson Chung68e4c642011-11-10 15:48:25 -08001527
Winson Chungb44b5242011-06-13 11:32:14 -07001528 /*
1529 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1530 * widget previews in the background with the AsyncTasks.
1531 */
Winson Chung68e4c642011-11-10 15:48:25 -08001532 final static int sLookBehindPageCount = 2;
1533 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001534 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001535 final int count = getChildCount();
1536 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1537 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1538 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001539 }
1540 protected int getAssociatedUpperPageBound(int page) {
1541 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001542 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1543 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1544 count - 1);
1545 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001546 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001547
1548 @Override
1549 protected String getCurrentPageDescription() {
1550 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1551 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001552 int count = 0;
1553
Adam Cohen0cd3b642011-10-14 14:58:00 -07001554 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001555 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001556 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001557 } else {
1558 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001559 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001560 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001561 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001562
Adam Cohend3357b12011-10-18 14:58:11 -07001563 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001564 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001565}