blob: 7f0edde75ae97a5a5aa7a8efccd17f8143a0ad71 [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;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080033import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070035import android.graphics.MaskFilter;
36import android.graphics.Paint;
Winson Chung70fc4382011-08-08 15:31:33 -070037import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.graphics.Rect;
Peter Ng8db70002011-10-25 15:40:08 -070039import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070041import android.os.AsyncTask;
42import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.util.AttributeSet;
44import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070045import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080046import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070048import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080050import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070051import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070053import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070054import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070055import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070056import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070057
58import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059import com.android.launcher2.DropTarget.DragObject;
60
61import java.util.ArrayList;
62import java.util.Collections;
63import java.util.Iterator;
64import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * A simple callback interface which also provides the results of the task.
68 */
69interface AsyncTaskCallback {
70 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
71}
Winson Chung4e076542011-06-23 13:04:10 -070072
Winson Chungb44b5242011-06-13 11:32:14 -070073/**
74 * The data needed to perform either of the custom AsyncTasks.
75 */
76class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070077 enum Type {
78 LoadWidgetPreviewData,
79 LoadHolographicIconsData
80 }
81
Winson Chungb44b5242011-06-13 11:32:14 -070082 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
83 AsyncTaskCallback postR) {
84 page = p;
85 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070086 sourceImages = si;
87 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070088 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070089 doInBackgroundCallback = bgR;
90 postExecuteCallback = postR;
91 }
Michael Jurka038f9d82011-11-03 13:50:45 -070092 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070093 AsyncTaskCallback postR) {
94 page = p;
95 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070096 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070097 maxImageWidth = cw;
98 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070099 doInBackgroundCallback = bgR;
100 postExecuteCallback = postR;
101 }
Winson Chung09945932011-09-20 14:22:40 -0700102 void cleanup(boolean cancelled) {
103 // Clean up any references to source/generated bitmaps
104 if (sourceImages != null) {
105 if (cancelled) {
106 for (Bitmap b : sourceImages) {
107 b.recycle();
108 }
109 }
110 sourceImages.clear();
111 }
112 if (generatedImages != null) {
113 if (cancelled) {
114 for (Bitmap b : generatedImages) {
115 b.recycle();
116 }
117 }
118 generatedImages.clear();
119 }
120 }
Winson Chungb44b5242011-06-13 11:32:14 -0700121 int page;
122 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700123 ArrayList<Bitmap> sourceImages;
124 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700125 int maxImageWidth;
126 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700127 AsyncTaskCallback doInBackgroundCallback;
128 AsyncTaskCallback postExecuteCallback;
129}
Winson Chung4e076542011-06-23 13:04:10 -0700130
Winson Chungb44b5242011-06-13 11:32:14 -0700131/**
132 * A generic template for an async task used in AppsCustomize.
133 */
134class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700135 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700136 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700137 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700138 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 }
140 @Override
141 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
142 if (params.length != 1) return null;
143 // Load each of the widget previews in the background
144 params[0].doInBackgroundCallback.run(this, params[0]);
145 return params[0];
146 }
147 @Override
148 protected void onPostExecute(AsyncTaskPageData result) {
149 // All the widget previews are loaded, so we can just callback to inflate the page
150 result.postExecuteCallback.run(this, result);
151 }
152
153 void setThreadPriority(int p) {
154 threadPriority = p;
155 }
156 void syncThreadPriority() {
157 Process.setThreadPriority(threadPriority);
158 }
159
160 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700161 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700162 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int threadPriority;
164}
Winson Chungb44b5242011-06-13 11:32:14 -0700165
166/**
167 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
168 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700169public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungc6f10b92011-11-14 11:39:07 -0800170 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource {
Winson Chung785d2eb2011-04-14 16:08:02 -0700171 static final String LOG_TAG = "AppsCustomizePagedView";
172
173 /**
174 * The different content types that this paged view can show.
175 */
176 public enum ContentType {
177 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700178 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700179 }
180
181 // Refs
182 private Launcher mLauncher;
183 private DragController mDragController;
184 private final LayoutInflater mLayoutInflater;
185 private final PackageManager mPackageManager;
186
Winson Chung5afbf7b2011-07-25 11:53:08 -0700187 // Save and Restore
188 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700189
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700191 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700192 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700193
Winson Chung7d7541e2011-09-16 20:14:36 -0700194 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800195 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 private int mClingFocusedX;
197 private int mClingFocusedY;
198
Winson Chung1ed747a2011-05-03 16:18:34 -0700199 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700200 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700201 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700202 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700203 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700204
205 // Dimens
206 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700207 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800208 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700209 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700210 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700211 private final int mWidgetPreviewIconPaddedDimension;
212 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700213 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700214 private int mNumAppsPages;
215 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700216
Adam Cohen22f823d2011-09-01 17:22:18 -0700217 // Relating to the scroll and overscroll effects
218 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700219 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700220 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700221 private static float TRANSITION_PIVOT = 0.65f;
222 private static float TRANSITION_MAX_ROTATION = 22;
223 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700224 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700225 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700226
Winson Chungb44b5242011-06-13 11:32:14 -0700227 // Previews & outlines
228 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
229 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung68e4c642011-11-10 15:48:25 -0800230 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700231
Winson Chung785d2eb2011-04-14 16:08:02 -0700232 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
233 super(context, attrs);
234 mLayoutInflater = LayoutInflater.from(context);
235 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700236 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700237 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700238 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700239 mHolographicOutlineHelper = new HolographicOutlineHelper();
240 mCanvas = new Canvas();
241 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700242
243 // Save the default widget preview background
244 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700245 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700246 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
247 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700248
Winson Chung6032e7e2011-11-08 15:47:17 -0800249 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
250 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
251 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700252 mWidgetWidthGap =
253 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
254 mWidgetHeightGap =
255 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700256 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
257 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700258 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
259 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700260 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700261 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700262
Winson Chung1ed747a2011-05-03 16:18:34 -0700263 // The padding on the non-matched dimension for the default widget preview icons
264 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700265 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700266 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700267 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700268 }
269
270 @Override
271 protected void init() {
272 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700273 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700274
275 Context context = getContext();
276 Resources r = context.getResources();
277 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
278 }
279
Winson Chungf0ea4d32011-06-06 14:27:16 -0700280 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700281 protected void onUnhandledTap(MotionEvent ev) {
282 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700283 // Dismiss AppsCustomize if we tap
284 mLauncher.showWorkspace(true);
285 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700286 }
287
Winson Chung5afbf7b2011-07-25 11:53:08 -0700288 /** Returns the item index of the center item on this page so that we can restore to this
289 * item index when we rotate. */
290 private int getMiddleComponentIndexOnCurrentPage() {
291 int i = -1;
292 if (getPageCount() > 0) {
293 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700294 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700295 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700296 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
297 int numItemsPerPage = mCellCountX * mCellCountY;
298 int childCount = childrenLayout.getChildCount();
299 if (childCount > 0) {
300 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700301 }
302 } else {
303 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700304 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700305 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
306 int childCount = layout.getChildCount();
307 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700308 i = numApps +
309 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
310 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 }
312 }
313 return i;
314 }
315
316 /** Get the index of the item to restore to if we need to restore the current page. */
317 int getSaveInstanceStateIndex() {
318 if (mSaveInstanceStateItemIndex == -1) {
319 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
320 }
321 return mSaveInstanceStateItemIndex;
322 }
323
324 /** Returns the page in the current orientation which is expected to contain the specified
325 * item index. */
326 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700327 if (index < 0) return 0;
328
329 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 int numItemsPerPage = mCellCountX * mCellCountY;
331 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700334 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
335 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 }
337
Winson Chungf0ea4d32011-06-06 14:27:16 -0700338 /**
339 * This differs from isDataReady as this is the test done if isDataReady is not set.
340 */
341 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700342 // We only do this test once, and we default to the Applications page, so we only really
343 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 // TODO: What if one of them is validly empty
345 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700346 }
347
Winson Chung5afbf7b2011-07-25 11:53:08 -0700348 /** Restores the page for an item at the specified index */
349 void restorePageForIndex(int index) {
350 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700351 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700352 }
353
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700354 private void updatePageCounts() {
355 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
356 (float) (mWidgetCountX * mWidgetCountY));
357 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
358 }
359
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 protected void onDataReady(int width, int height) {
361 // Note that we transpose the counts in portrait so that we get a similar layout
362 boolean isLandscape = getResources().getConfiguration().orientation ==
363 Configuration.ORIENTATION_LANDSCAPE;
364 int maxCellCountX = Integer.MAX_VALUE;
365 int maxCellCountY = Integer.MAX_VALUE;
366 if (LauncherApplication.isScreenLarge()) {
367 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
368 LauncherModel.getCellCountY());
369 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
370 LauncherModel.getCellCountX());
371 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800372 if (mMaxAppCellCountX > -1) {
373 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
374 }
375 if (mMaxAppCellCountY > -1) {
376 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
377 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378
379 // Now that the data is ready, we can calculate the content width, the number of cells to
380 // use for each page
381 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
382 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
383 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
384 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
385 mCellCountX = mWidgetSpacingLayout.getCellCountX();
386 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700387 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700388
Winson Chungdb1138b2011-06-30 14:39:35 -0700389 // Force a measure to update recalculate the gaps
390 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
391 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
392 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700394
Michael Jurkae326f182011-11-21 14:05:46 -0800395 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
396 final boolean hostIsTransitioning = host.isTransitioning();
397
Adam Cohen0cd3b642011-10-14 14:58:00 -0700398 // Restore the page
399 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800400 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700401
Winson Chung3f4e1422011-11-17 14:58:51 -0800402 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
403 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
404 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800405 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800406 post(new Runnable() {
407 @Override
408 public void run() {
409 showAllAppsCling();
410 }
411 });
412 }
413 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700414
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 void showAllAppsCling() {
416 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
417 mHasShownAllAppsCling = true;
418 // Calculate the position for the cling punch through
419 int[] offset = new int[2];
420 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
421 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
422 // PagedViews are centered horizontally but top aligned
423 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
424 offset[0];
425 pos[1] += offset[1];
426 mLauncher.showFirstRunAllAppsCling(pos);
427 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700428 }
429
430 @Override
431 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
432 int width = MeasureSpec.getSize(widthMeasureSpec);
433 int height = MeasureSpec.getSize(heightMeasureSpec);
434 if (!isDataReady()) {
435 if (testDataReady()) {
436 setDataIsReady();
437 setMeasuredDimension(width, height);
438 onDataReady(width, height);
439 }
440 }
441
442 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
443 }
444
Winson Chung785d2eb2011-04-14 16:08:02 -0700445 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700446 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
447 // by a broadcast receiver, and in order for it to work correctly, we need to know that
448 // the AppWidgetService has already received and processed the same broadcast. Since there
449 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
450 // we should have a more precise way of ensuring the AppWidgetService is up to date.
451 postDelayed(new Runnable() {
452 public void run() {
453 updatePackages();
454 }
455 }, 500);
456 }
457
458 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700459 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700460 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700461 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700462 List<AppWidgetProviderInfo> widgets =
463 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700464 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
465 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800466 for (AppWidgetProviderInfo widget : widgets) {
467 if (widget.minWidth > 0 && widget.minHeight > 0) {
468 mWidgets.add(widget);
469 } else {
470 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
471 widget.minWidth + ", " + widget.minHeight + ")");
472 }
473 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700474 mWidgets.addAll(shortcuts);
475 Collections.sort(mWidgets,
476 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700477 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700478
Winson Chung875de7e2011-06-28 14:25:17 -0700479 if (wasEmpty) {
480 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
481 // a layout to do this test and invalidate the page data when ready.
482 if (testDataReady()) requestLayout();
483 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700484 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700485 invalidatePageData();
486 }
Winson Chung4b576be2011-04-27 17:40:20 -0700487 }
488
489 @Override
490 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700491 // When we have exited all apps or are in transition, disregard clicks
492 if (!mLauncher.isAllAppsCustomizeOpen() ||
493 mLauncher.getWorkspace().isSwitchingState()) return;
494
Winson Chung4b576be2011-04-27 17:40:20 -0700495 if (v instanceof PagedViewIcon) {
496 // Animate some feedback to the click
497 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
498 animateClickFeedback(v, new Runnable() {
499 @Override
500 public void run() {
501 mLauncher.startActivitySafely(appInfo.intent, appInfo);
502 }
503 });
504 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700505 // Let the user know that they have to long press to add a widget
506 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
507 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700508
Winson Chungd2e87b32011-06-02 10:53:07 -0700509 // Create a little animation to show that the widget can move
510 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
511 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
512 AnimatorSet bounce = new AnimatorSet();
513 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
514 tyuAnim.setDuration(125);
515 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
516 tydAnim.setDuration(100);
517 bounce.play(tyuAnim).before(tydAnim);
518 bounce.setInterpolator(new AccelerateInterpolator());
519 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700520 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700521 }
522
Winson Chungc6f10b92011-11-14 11:39:07 -0800523 public boolean onKey(View v, int keyCode, KeyEvent event) {
524 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
525 }
526
Winson Chung785d2eb2011-04-14 16:08:02 -0700527 /*
528 * PagedViewWithDraggableItems implementation
529 */
530 @Override
531 protected void determineDraggingStart(android.view.MotionEvent ev) {
532 // Disable dragging by pulling an app down for now.
533 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700534
Winson Chung4b576be2011-04-27 17:40:20 -0700535 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700536 mLauncher.getWorkspace().onDragStartedWithItem(v);
537 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700538 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700539
Winson Chung4b576be2011-04-27 17:40:20 -0700540 private void beginDraggingWidget(View v) {
541 // Get the widget preview as the drag representation
542 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700543 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700544
545 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800546 Bitmap preview;
547 Bitmap outline;
Winson Chung1ed747a2011-05-03 16:18:34 -0700548 if (createItemInfo instanceof PendingAddWidgetInfo) {
549 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700550 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700551 createItemInfo.spanX = spanXY[0];
552 createItemInfo.spanY = spanXY[1];
553
Michael Jurka038f9d82011-11-03 13:50:45 -0700554 int[] maxSize = mLauncher.getWorkspace().estimateItemSize(spanXY[0], spanXY[1],
555 createWidgetInfo, true);
Winson Chung1120e032011-11-22 16:11:31 -0800556 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurka038f9d82011-11-03 13:50:45 -0700557 createWidgetInfo.icon, spanXY[0], spanXY[1], maxSize[0], maxSize[1]);
Winson Chung1ed747a2011-05-03 16:18:34 -0700558 } else {
559 // Workaround for the fact that we don't keep the original ResolveInfo associated with
560 // the shortcut around. To get the icon, we just render the preview image (which has
561 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800562 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700563 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800564 Drawable d = image.getDrawable();
565 mCanvas.setBitmap(preview);
566 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700567 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700568 createItemInfo.spanX = createItemInfo.spanY = 1;
569 }
Winson Chung4b576be2011-04-27 17:40:20 -0700570
Peter Ng8db70002011-10-25 15:40:08 -0700571 // We use a custom alpha clip table for the default widget previews
572 Paint alphaClipPaint = null;
573 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700574 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700575 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
576 alphaClipPaint = new Paint();
577 alphaClipPaint.setMaskFilter(alphaClipTable);
578 }
579 }
580
Winson Chung1120e032011-11-22 16:11:31 -0800581 // Save the preview for the outline generation, then dim the preview
582 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
583 false);
584 mCanvas.setBitmap(preview);
585 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
586 mCanvas.setBitmap(null);
587
Winson Chung4b576be2011-04-27 17:40:20 -0700588 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800589 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700590 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800591 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
592 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700593 DragController.DRAG_ACTION_COPY, null);
Winson Chung1120e032011-11-22 16:11:31 -0800594 outline.recycle();
595 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700596 }
597 @Override
598 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700599 // Dismiss the cling
600 mLauncher.dismissAllAppsCling(null);
601
Winson Chung4b576be2011-04-27 17:40:20 -0700602 if (!super.beginDragging(v)) return false;
603
Winson Chungc07918d2011-07-01 15:35:26 -0700604 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700605 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700606
Winson Chung4b576be2011-04-27 17:40:20 -0700607 if (v instanceof PagedViewIcon) {
608 beginDraggingApplication(v);
609 } else if (v instanceof PagedViewWidget) {
610 beginDraggingWidget(v);
611 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700612 return true;
613 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700614 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700615 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700616 if (!success || (target != mLauncher.getWorkspace() &&
617 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700618 // Exit spring loaded mode if we have not successfully dropped or have not handled the
619 // drop in Workspace
620 mLauncher.exitSpringLoadedDragMode();
621 }
Adam Cohen446e9402011-09-15 18:21:21 -0700622 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700623
Winson Chung785d2eb2011-04-14 16:08:02 -0700624 }
625
Winson Chung785d2eb2011-04-14 16:08:02 -0700626 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700627 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700628 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700629
630 // Display an error message if the drag failed due to there not being enough space on the
631 // target layout we were dropping on.
632 if (!success) {
633 boolean showOutOfSpaceMessage = false;
634 if (target instanceof Workspace) {
635 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
636 Workspace workspace = (Workspace) target;
637 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700638 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700639 if (layout != null) {
640 layout.calculateSpans(itemInfo);
641 showOutOfSpaceMessage =
642 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
643 }
644 }
Winson Chungfc79c802011-05-02 13:35:34 -0700645 if (showOutOfSpaceMessage) {
646 mLauncher.showOutOfSpaceMessage();
647 }
648 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700649 }
650
Winson Chung7f0acdd2011-09-19 18:34:19 -0700651 @Override
652 protected void onDetachedFromWindow() {
653 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700654 cancelAllTasks();
655 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700656
Michael Jurkae326f182011-11-21 14:05:46 -0800657 public void clearAllWidgetPages() {
658 cancelAllTasks();
659 int count = getChildCount();
660 for (int i = 0; i < count; i++) {
661 View v = getPageAt(i);
662 if (v instanceof PagedViewGridLayout) {
663 ((PagedViewGridLayout) v).removeAllViewsOnPage();
664 mDirtyPageContent.set(i, true);
665 }
666 }
667 }
668
Adam Cohen0cd3b642011-10-14 14:58:00 -0700669 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700670 // Clean up all the async tasks
671 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
672 while (iter.hasNext()) {
673 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
674 task.cancel(false);
675 iter.remove();
676 }
677 }
678
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700680 if (type == ContentType.Widgets) {
681 invalidatePageData(mNumAppsPages, true);
682 } else if (type == ContentType.Applications) {
683 invalidatePageData(0, true);
684 }
Winson Chungb44b5242011-06-13 11:32:14 -0700685 }
686
Adam Cohen0cd3b642011-10-14 14:58:00 -0700687 protected void snapToPage(int whichPage, int delta, int duration) {
688 super.snapToPage(whichPage, delta, duration);
689 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800690
691 // Update the thread priorities given the direction lookahead
692 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
693 while (iter.hasNext()) {
694 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
695 int pageIndex = task.page + mNumAppsPages;
696 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
697 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
698 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
699 } else {
700 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
701 }
702 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700703 }
704
705 private void updateCurrentTab(int currentPage) {
706 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800707 if (tabHost != null) {
708 String tag = tabHost.getCurrentTabTag();
709 if (tag != null) {
710 if (currentPage >= mNumAppsPages &&
711 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
712 tabHost.setCurrentTabFromContent(ContentType.Widgets);
713 } else if (currentPage < mNumAppsPages &&
714 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
715 tabHost.setCurrentTabFromContent(ContentType.Applications);
716 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700717 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700718 }
719 }
720
Winson Chung785d2eb2011-04-14 16:08:02 -0700721 /*
722 * Apps PagedView implementation
723 */
Winson Chung63257c12011-05-05 17:06:13 -0700724 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
725 int childCount = layout.getChildCount();
726 for (int i = 0; i < childCount; ++i) {
727 layout.getChildAt(i).setVisibility(visibility);
728 }
729 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700730 private void setupPage(PagedViewCellLayout layout) {
731 layout.setCellCount(mCellCountX, mCellCountY);
732 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
733 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
734 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
735
Winson Chung63257c12011-05-05 17:06:13 -0700736 // Note: We force a measure here to get around the fact that when we do layout calculations
737 // immediately after syncing, we don't have a proper width. That said, we already know the
738 // expected page width, so we can actually optimize by hiding all the TextView-based
739 // children that are expensive to measure, and let that happen naturally later.
740 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700741 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700742 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700743 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700744 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700745 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700746 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700747
Winson Chungf314b0e2011-08-16 11:54:27 -0700748 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700750 int numCells = mCellCountX * mCellCountY;
751 int startIndex = page * numCells;
752 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700753 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700754
Winson Chung785d2eb2011-04-14 16:08:02 -0700755 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700756 ArrayList<Object> items = new ArrayList<Object>();
757 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700758 for (int i = startIndex; i < endIndex; ++i) {
759 ApplicationInfo info = mApps.get(i);
760 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
761 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700762 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700763 icon.setOnClickListener(this);
764 icon.setOnLongClickListener(this);
765 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800766 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700767
768 int index = i - startIndex;
769 int x = index % mCellCountX;
770 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700771 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700772
773 items.add(info);
774 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700776
Winson Chungf0ea4d32011-06-06 14:27:16 -0700777 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700778
Winson Chung8ff87132011-09-30 19:40:46 -0700779 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700780 if (mFadeInAdjacentScreens) {
781 prepareGenerateHoloOutlinesTask(page, items, images);
782 }
Winson Chung8ff87132011-09-30 19:40:46 -0700783 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700784 }
Winson Chungb44b5242011-06-13 11:32:14 -0700785
786 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800787 * A helper to return the priority for loading of the specified widget page.
788 */
789 private int getWidgetPageLoadPriority(int page) {
790 // If we are snapping to another page, use that index as the target page index
791 int toPage = mCurrentPage;
792 if (mNextPage > -1) {
793 toPage = mNextPage;
794 }
795
796 // We use the distance from the target page as an initial guess of priority, but if there
797 // are no pages of higher priority than the page specified, then bump up the priority of
798 // the specified page.
799 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
800 int minPageDiff = Integer.MAX_VALUE;
801 while (iter.hasNext()) {
802 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
803 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
804 }
805
806 int rawPageDiff = Math.abs(page - toPage);
807 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
808 }
809 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700810 * Return the appropriate thread priority for loading for a given page (we give the current
811 * page much higher priority)
812 */
813 private int getThreadPriorityForPage(int page) {
814 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800815 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700816 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800817 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700818 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800819 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700820 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800821 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700822 }
823 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700824 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800825 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700826 return Math.max(0, pageDiff * sPageSleepDelay);
827 }
Winson Chungb44b5242011-06-13 11:32:14 -0700828 /**
829 * Creates and executes a new AsyncTask to load a page of widget previews.
830 */
831 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700832 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800833
Winson Chungb44b5242011-06-13 11:32:14 -0700834 // Prune all tasks that are no longer needed
835 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
836 while (iter.hasNext()) {
837 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800838 int taskPage = task.page + mNumAppsPages;
839 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
840 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700841 task.cancel(false);
842 iter.remove();
843 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800844 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700845 }
846 }
847
Winson Chungf314b0e2011-08-16 11:54:27 -0700848 // 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 -0700849 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700850 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700851 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700852 @Override
853 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700854 try {
Winson Chung09945932011-09-20 14:22:40 -0700855 try {
856 Thread.sleep(sleepMs);
857 } catch (Exception e) {}
858 loadWidgetPreviewsInBackground(task, data);
859 } finally {
860 if (task.isCancelled()) {
861 data.cleanup(true);
862 }
863 }
Winson Chungb44b5242011-06-13 11:32:14 -0700864 }
865 },
866 new AsyncTaskCallback() {
867 @Override
868 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700869 try {
870 mRunningTasks.remove(task);
871 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700872 onSyncWidgetPageItems(data);
873 } finally {
874 data.cleanup(task.isCancelled());
875 }
Winson Chungb44b5242011-06-13 11:32:14 -0700876 }
Winson Chung09945932011-09-20 14:22:40 -0700877 });
Winson Chungb44b5242011-06-13 11:32:14 -0700878
879 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700880 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700881 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800882 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700883 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
884 mRunningTasks.add(t);
885 }
886 /**
887 * Creates and executes a new AsyncTask to load the outlines for a page of content.
888 */
889 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
890 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700891 // Prune old tasks for this page
892 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
893 while (iter.hasNext()) {
894 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
895 int taskPage = task.page;
896 if ((taskPage == page) &&
897 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
898 task.cancel(false);
899 iter.remove();
900 }
901 }
902
Winson Chungb44b5242011-06-13 11:32:14 -0700903 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
904 new AsyncTaskCallback() {
905 @Override
906 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700907 try {
908 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700909 task.syncThreadPriority();
910
Winson Chung09945932011-09-20 14:22:40 -0700911 ArrayList<Bitmap> images = data.generatedImages;
912 ArrayList<Bitmap> srcImages = data.sourceImages;
913 int count = srcImages.size();
914 Canvas c = new Canvas();
915 for (int i = 0; i < count && !task.isCancelled(); ++i) {
916 // Before work on each item, ensure that this task is running at the correct
917 // priority
918 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700919
Winson Chung09945932011-09-20 14:22:40 -0700920 Bitmap b = srcImages.get(i);
921 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
922 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700923
Winson Chung09945932011-09-20 14:22:40 -0700924 c.setBitmap(outline);
925 c.save();
926 c.drawBitmap(b, 0, 0, null);
927 c.restore();
928 c.setBitmap(null);
929
930 images.add(outline);
931 }
932 } finally {
933 if (task.isCancelled()) {
934 data.cleanup(true);
935 }
Winson Chungb44b5242011-06-13 11:32:14 -0700936 }
937 }
938 },
939 new AsyncTaskCallback() {
940 @Override
941 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700942 try {
943 mRunningTasks.remove(task);
944 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700945 onHolographicPageItemsLoaded(data);
946 } finally {
947 data.cleanup(task.isCancelled());
948 }
Winson Chungb44b5242011-06-13 11:32:14 -0700949 }
950 });
951
952 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700953 AppsCustomizeAsyncTask t =
Adam Cohen0cd3b642011-10-14 14:58:00 -0700954 new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700955 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
956 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
957 mRunningTasks.add(t);
958 }
959
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 /*
961 * Widgets PagedView implementation
962 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700963 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
965 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700966
967 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700968 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700969 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
970 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700972 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700974
Winson Chung5fc72b32011-10-11 17:53:58 -0700975 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
976 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700977 }
Michael Jurka92f3d462011-11-22 21:02:29 -0800978
Winson Chung5fc72b32011-10-11 17:53:58 -0700979 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
980 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700981 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700982 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700983 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700984 Rect oldBounds = d.copyBounds();
985 d.setBounds(x, y, x + w, y + h);
986 d.draw(c);
987 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700988 if (multiplyColor != 0xFFFFFFFF) {
989 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
990 }
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);
1123 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1124 mHolographicOutlineHelper);
1125 widget.setTag(createItemInfo);
1126 } 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);
1133 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1134 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 Chung8ff87132011-09-30 19:40:46 -07001234 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001235 if (mFadeInAdjacentScreens) {
1236 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1237 }
Winson Chung8ff87132011-09-30 19:40:46 -07001238 */
Winson Chung68e4c642011-11-10 15:48:25 -08001239
1240 // Update all thread priorities
1241 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1242 while (iter.hasNext()) {
1243 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1244 int pageIndex = task.page + mNumAppsPages;
1245 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1246 }
Winson Chungb44b5242011-06-13 11:32:14 -07001247 }
1248 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1249 // Invalidate early to short-circuit children invalidates
1250 invalidate();
1251
1252 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001253 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001254 if (layout instanceof PagedViewCellLayout) {
1255 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1256 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001257 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001258 for (int i = 0; i < count; ++i) {
1259 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001260 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001261 }
1262 } else {
1263 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001264 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001265 for (int i = 0; i < count; ++i) {
1266 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001267 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001268 }
1269 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 }
Winson Chung46af2e82011-05-09 16:00:53 -07001271
Winson Chung785d2eb2011-04-14 16:08:02 -07001272 @Override
1273 public void syncPages() {
1274 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001275 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001276
Adam Cohen0cd3b642011-10-14 14:58:00 -07001277 Context context = getContext();
1278 for (int j = 0; j < mNumWidgetPages; ++j) {
1279 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1280 mWidgetCountY);
1281 setupPage(layout);
1282 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1283 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001284 }
1285
Adam Cohen0cd3b642011-10-14 14:58:00 -07001286 for (int i = 0; i < mNumAppsPages; ++i) {
1287 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1288 setupPage(layout);
1289 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001290 }
1291 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001292
Winson Chung785d2eb2011-04-14 16:08:02 -07001293 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001294 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001295 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001296 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001297 } else {
1298 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 }
1300 }
1301
Adam Cohen22f823d2011-09-01 17:22:18 -07001302 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1303 // it is in the z-order. This is important to insure touch events are handled correctly.
1304 View getPageAt(int index) {
1305 return getChildAt(getChildCount() - index - 1);
1306 }
1307
Adam Cohenae4f1552011-10-20 00:15:42 -07001308 @Override
1309 protected int indexToPage(int index) {
1310 return getChildCount() - index - 1;
1311 }
1312
Adam Cohen22f823d2011-09-01 17:22:18 -07001313 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1314 @Override
1315 protected void screenScrolled(int screenCenter) {
1316 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001317
1318 for (int i = 0; i < getChildCount(); i++) {
1319 View v = getPageAt(i);
1320 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001321 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001322
1323 float interpolatedProgress =
1324 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1325 float scale = (1 - interpolatedProgress) +
1326 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1327 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001328
Adam Cohen2591f6a2011-10-25 14:36:40 -07001329 float alpha;
1330
1331 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1332 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001333 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001334 } else {
1335 // On large screens we need to fade the page as it nears its leftmost position
1336 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1337 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001338
1339 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1340 int pageWidth = v.getMeasuredWidth();
1341 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001342
1343 if (PERFORM_OVERSCROLL_ROTATION) {
1344 if (i == 0 && scrollProgress < 0) {
1345 // Overscroll to the left
1346 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1347 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1348 scale = 1.0f;
1349 alpha = 1.0f;
1350 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001351 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001352 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1353 // Overscroll to the right
1354 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1355 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1356 scale = 1.0f;
1357 alpha = 1.0f;
1358 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001359 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001360 } else {
1361 v.setPivotY(pageHeight / 2.0f);
1362 v.setPivotX(pageWidth / 2.0f);
1363 v.setRotationY(0f);
1364 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001365 }
1366
1367 v.setTranslationX(translationX);
1368 v.setScaleX(scale);
1369 v.setScaleY(scale);
1370 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001371
1372 // If the view has 0 alpha, we set it to be invisible so as to prevent
1373 // it from accepting touches
1374 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1375 v.setVisibility(INVISIBLE);
1376 } else if (v.getVisibility() != VISIBLE) {
1377 v.setVisibility(VISIBLE);
1378 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001379 }
1380 }
1381 }
1382
1383 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001384 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001385 }
1386
Winson Chung785d2eb2011-04-14 16:08:02 -07001387 /**
1388 * Used by the parent to get the content width to set the tab bar to
1389 * @return
1390 */
1391 public int getPageContentWidth() {
1392 return mContentWidth;
1393 }
1394
Winson Chungb26f3d62011-06-02 10:49:29 -07001395 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001396 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001397 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001398
1399 // We reset the save index when we change pages so that it will be recalculated on next
1400 // rotation
1401 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001402 }
1403
Winson Chung785d2eb2011-04-14 16:08:02 -07001404 /*
1405 * AllAppsView implementation
1406 */
1407 @Override
1408 public void setup(Launcher launcher, DragController dragController) {
1409 mLauncher = launcher;
1410 mDragController = dragController;
1411 }
1412 @Override
1413 public void zoom(float zoom, boolean animate) {
1414 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1415 }
1416 @Override
1417 public boolean isVisible() {
1418 return (getVisibility() == VISIBLE);
1419 }
1420 @Override
1421 public boolean isAnimating() {
1422 return false;
1423 }
1424 @Override
1425 public void setApps(ArrayList<ApplicationInfo> list) {
1426 mApps = list;
1427 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001428 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001429
Winson Chung875de7e2011-06-28 14:25:17 -07001430 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1431 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001432 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001433 }
1434 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1435 // We add it in place, in alphabetical order
1436 int count = list.size();
1437 for (int i = 0; i < count; ++i) {
1438 ApplicationInfo info = list.get(i);
1439 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1440 if (index < 0) {
1441 mApps.add(-(index + 1), info);
1442 }
1443 }
1444 }
1445 @Override
1446 public void addApps(ArrayList<ApplicationInfo> list) {
1447 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001448 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001449 invalidatePageData();
1450 }
1451 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1452 ComponentName removeComponent = item.intent.getComponent();
1453 int length = list.size();
1454 for (int i = 0; i < length; ++i) {
1455 ApplicationInfo info = list.get(i);
1456 if (info.intent.getComponent().equals(removeComponent)) {
1457 return i;
1458 }
1459 }
1460 return -1;
1461 }
1462 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1463 // loop through all the apps and remove apps that have the same component
1464 int length = list.size();
1465 for (int i = 0; i < length; ++i) {
1466 ApplicationInfo info = list.get(i);
1467 int removeIndex = findAppByComponent(mApps, info);
1468 if (removeIndex > -1) {
1469 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001470 }
1471 }
1472 }
1473 @Override
1474 public void removeApps(ArrayList<ApplicationInfo> list) {
1475 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001476 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001477 invalidatePageData();
1478 }
1479 @Override
1480 public void updateApps(ArrayList<ApplicationInfo> list) {
1481 // We remove and re-add the updated applications list because it's properties may have
1482 // changed (ie. the title), and this will ensure that the items will be in their proper
1483 // place in the list.
1484 removeAppsWithoutInvalidate(list);
1485 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001486 updatePageCounts();
1487
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 invalidatePageData();
1489 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001490
Winson Chung785d2eb2011-04-14 16:08:02 -07001491 @Override
1492 public void reset() {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001493 AppsCustomizeTabHost tabHost = getTabHost();
1494 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001495 if (tag != null) {
1496 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1497 tabHost.setCurrentTabFromContent(ContentType.Applications);
1498 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001499 }
1500 if (mCurrentPage != 0) {
1501 invalidatePageData(0);
1502 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001503 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001504
1505 private AppsCustomizeTabHost getTabHost() {
1506 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1507 }
1508
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 @Override
1510 public void dumpState() {
1511 // TODO: Dump information related to current list of Applications, Widgets, etc.
1512 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1513 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1514 }
Adam Cohen4e844012011-11-09 13:48:04 -08001515
Winson Chung785d2eb2011-04-14 16:08:02 -07001516 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001517 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001518 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001519 for (Object i: list) {
1520 if (i instanceof AppWidgetProviderInfo) {
1521 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1522 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1523 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1524 + " initialLayout=" + info.initialLayout
1525 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1526 } else if (i instanceof ResolveInfo) {
1527 ResolveInfo info = (ResolveInfo) i;
1528 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1529 + info.icon);
1530 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001531 }
1532 }
Adam Cohen4e844012011-11-09 13:48:04 -08001533
Winson Chung785d2eb2011-04-14 16:08:02 -07001534 @Override
1535 public void surrender() {
1536 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1537 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001538
1539 // Stop all background tasks
1540 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001541 }
Winson Chung007c6982011-06-14 13:27:53 -07001542
Winson Chung68e4c642011-11-10 15:48:25 -08001543
Winson Chungb44b5242011-06-13 11:32:14 -07001544 /*
1545 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1546 * widget previews in the background with the AsyncTasks.
1547 */
Winson Chung68e4c642011-11-10 15:48:25 -08001548 final static int sLookBehindPageCount = 2;
1549 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001550 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001551 final int count = getChildCount();
1552 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1553 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1554 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001555 }
1556 protected int getAssociatedUpperPageBound(int page) {
1557 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001558 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1559 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1560 count - 1);
1561 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001562 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001563
1564 @Override
1565 protected String getCurrentPageDescription() {
1566 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1567 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001568 int count = 0;
1569
Adam Cohen0cd3b642011-10-14 14:58:00 -07001570 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001571 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001572 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001573 } else {
1574 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001575 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001576 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001577 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001578
Adam Cohend3357b12011-10-18 14:58:11 -07001579 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001580 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001581}