blob: 3855c4369975fa954861cbaf487d17b9f398b130 [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;
Winson Chung46af2e82011-05-09 16:00:53 -070027import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import 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;
43import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.util.AttributeSet;
45import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070046import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080047import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070049import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080051import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070052import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070053import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070054import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070055import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070056import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070057import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
59import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import com.android.launcher2.DropTarget.DragObject;
61
62import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Iterator;
65import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070066
Winson Chungb44b5242011-06-13 11:32:14 -070067/**
68 * A simple callback interface which also provides the results of the task.
69 */
70interface AsyncTaskCallback {
71 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
72}
Winson Chung4e076542011-06-23 13:04:10 -070073
Winson Chungb44b5242011-06-13 11:32:14 -070074/**
75 * The data needed to perform either of the custom AsyncTasks.
76 */
77class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070078 enum Type {
79 LoadWidgetPreviewData,
80 LoadHolographicIconsData
81 }
82
Winson Chungb44b5242011-06-13 11:32:14 -070083 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
84 AsyncTaskCallback postR) {
85 page = p;
86 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070087 sourceImages = si;
88 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070089 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070090 doInBackgroundCallback = bgR;
91 postExecuteCallback = postR;
92 }
Michael Jurka038f9d82011-11-03 13:50:45 -070093 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070094 AsyncTaskCallback postR) {
95 page = p;
96 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070097 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070098 maxImageWidth = cw;
99 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700100 doInBackgroundCallback = bgR;
101 postExecuteCallback = postR;
102 }
Winson Chung09945932011-09-20 14:22:40 -0700103 void cleanup(boolean cancelled) {
104 // Clean up any references to source/generated bitmaps
105 if (sourceImages != null) {
106 if (cancelled) {
107 for (Bitmap b : sourceImages) {
108 b.recycle();
109 }
110 }
111 sourceImages.clear();
112 }
113 if (generatedImages != null) {
114 if (cancelled) {
115 for (Bitmap b : generatedImages) {
116 b.recycle();
117 }
118 }
119 generatedImages.clear();
120 }
121 }
Winson Chungb44b5242011-06-13 11:32:14 -0700122 int page;
123 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700124 ArrayList<Bitmap> sourceImages;
125 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700126 int maxImageWidth;
127 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700128 AsyncTaskCallback doInBackgroundCallback;
129 AsyncTaskCallback postExecuteCallback;
130}
Winson Chung4e076542011-06-23 13:04:10 -0700131
Winson Chungb44b5242011-06-13 11:32:14 -0700132/**
133 * A generic template for an async task used in AppsCustomize.
134 */
135class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700136 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700137 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700139 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 }
141 @Override
142 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
143 if (params.length != 1) return null;
144 // Load each of the widget previews in the background
145 params[0].doInBackgroundCallback.run(this, params[0]);
146 return params[0];
147 }
148 @Override
149 protected void onPostExecute(AsyncTaskPageData result) {
150 // All the widget previews are loaded, so we can just callback to inflate the page
151 result.postExecuteCallback.run(this, result);
152 }
153
154 void setThreadPriority(int p) {
155 threadPriority = p;
156 }
157 void syncThreadPriority() {
158 Process.setThreadPriority(threadPriority);
159 }
160
161 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700162 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700164 int threadPriority;
165}
Winson Chungb44b5242011-06-13 11:32:14 -0700166
167/**
168 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
169 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungc6f10b92011-11-14 11:39:07 -0800171 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource {
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 static final String LOG_TAG = "AppsCustomizePagedView";
173
174 /**
175 * The different content types that this paged view can show.
176 */
177 public enum ContentType {
178 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700179 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700180 }
181
182 // Refs
183 private Launcher mLauncher;
184 private DragController mDragController;
185 private final LayoutInflater mLayoutInflater;
186 private final PackageManager mPackageManager;
187
Winson Chung5afbf7b2011-07-25 11:53:08 -0700188 // Save and Restore
189 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700190
Winson Chung785d2eb2011-04-14 16:08:02 -0700191 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700192 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700193 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700194
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800196 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700197 private int mClingFocusedX;
198 private int mClingFocusedY;
199
Winson Chung1ed747a2011-05-03 16:18:34 -0700200 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700201 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700202 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700203 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700204 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700205
206 // Dimens
207 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700208 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800209 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700210 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700211 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700212 private final int mWidgetPreviewIconPaddedDimension;
213 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700214 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700215 private int mNumAppsPages;
216 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700217
Adam Cohen22f823d2011-09-01 17:22:18 -0700218 // Relating to the scroll and overscroll effects
219 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700220 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700221 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700222 private static float TRANSITION_PIVOT = 0.65f;
223 private static float TRANSITION_MAX_ROTATION = 22;
224 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700225 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700226 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700227
Winson Chungb44b5242011-06-13 11:32:14 -0700228 // Previews & outlines
229 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
230 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung68e4c642011-11-10 15:48:25 -0800231 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700232
Winson Chung785d2eb2011-04-14 16:08:02 -0700233 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
234 super(context, attrs);
235 mLayoutInflater = LayoutInflater.from(context);
236 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700237 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700238 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700239 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700240 mHolographicOutlineHelper = new HolographicOutlineHelper();
241 mCanvas = new Canvas();
242 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700243
244 // Save the default widget preview background
245 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700246 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700247 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
248 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700249
Winson Chung6032e7e2011-11-08 15:47:17 -0800250 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
251 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
252 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700253 mWidgetWidthGap =
254 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
255 mWidgetHeightGap =
256 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700257 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
258 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700259 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
260 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700261 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700263
Winson Chung1ed747a2011-05-03 16:18:34 -0700264 // The padding on the non-matched dimension for the default widget preview icons
265 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700267 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700268 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700269 }
270
271 @Override
272 protected void init() {
273 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700274 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700275
276 Context context = getContext();
277 Resources r = context.getResources();
278 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
279 }
280
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700282 protected void onUnhandledTap(MotionEvent ev) {
283 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700284 // Dismiss AppsCustomize if we tap
285 mLauncher.showWorkspace(true);
286 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700287 }
288
Winson Chung5afbf7b2011-07-25 11:53:08 -0700289 /** Returns the item index of the center item on this page so that we can restore to this
290 * item index when we rotate. */
291 private int getMiddleComponentIndexOnCurrentPage() {
292 int i = -1;
293 if (getPageCount() > 0) {
294 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700295 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700296 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700297 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
298 int numItemsPerPage = mCellCountX * mCellCountY;
299 int childCount = childrenLayout.getChildCount();
300 if (childCount > 0) {
301 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700302 }
303 } else {
304 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700305 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700306 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
307 int childCount = layout.getChildCount();
308 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 i = numApps +
310 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
311 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700312 }
313 }
314 return i;
315 }
316
317 /** Get the index of the item to restore to if we need to restore the current page. */
318 int getSaveInstanceStateIndex() {
319 if (mSaveInstanceStateItemIndex == -1) {
320 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
321 }
322 return mSaveInstanceStateItemIndex;
323 }
324
325 /** Returns the page in the current orientation which is expected to contain the specified
326 * item index. */
327 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700328 if (index < 0) return 0;
329
330 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700331 int numItemsPerPage = mCellCountX * mCellCountY;
332 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700333 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700334 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
336 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700337 }
338
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 /**
340 * This differs from isDataReady as this is the test done if isDataReady is not set.
341 */
342 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700343 // We only do this test once, and we default to the Applications page, so we only really
344 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700345 // TODO: What if one of them is validly empty
346 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700347 }
348
Winson Chung5afbf7b2011-07-25 11:53:08 -0700349 /** Restores the page for an item at the specified index */
350 void restorePageForIndex(int index) {
351 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700352 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700353 }
354
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700355 private void updatePageCounts() {
356 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
357 (float) (mWidgetCountX * mWidgetCountY));
358 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
359 }
360
Winson Chungf0ea4d32011-06-06 14:27:16 -0700361 protected void onDataReady(int width, int height) {
362 // Note that we transpose the counts in portrait so that we get a similar layout
363 boolean isLandscape = getResources().getConfiguration().orientation ==
364 Configuration.ORIENTATION_LANDSCAPE;
365 int maxCellCountX = Integer.MAX_VALUE;
366 int maxCellCountY = Integer.MAX_VALUE;
367 if (LauncherApplication.isScreenLarge()) {
368 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
369 LauncherModel.getCellCountY());
370 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
371 LauncherModel.getCellCountX());
372 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800373 if (mMaxAppCellCountX > -1) {
374 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
375 }
376 if (mMaxAppCellCountY > -1) {
377 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
378 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700379
380 // Now that the data is ready, we can calculate the content width, the number of cells to
381 // use for each page
382 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
383 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
384 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
385 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
386 mCellCountX = mWidgetSpacingLayout.getCellCountX();
387 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700388 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700389
Winson Chungdb1138b2011-06-30 14:39:35 -0700390 // Force a measure to update recalculate the gaps
391 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
392 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
393 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700394 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700395
396 // Restore the page
397 int page = getPageForComponent(mSaveInstanceStateItemIndex);
398 invalidatePageData(Math.max(0, page));
Winson Chung7d7541e2011-09-16 20:14:36 -0700399
Winson Chung3f4e1422011-11-17 14:58:51 -0800400 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
401 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
402 // returned while animating)
403 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
404 if (!host.isTransitioning()) {
405 post(new Runnable() {
406 @Override
407 public void run() {
408 showAllAppsCling();
409 }
410 });
411 }
412 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700413
Winson Chung3f4e1422011-11-17 14:58:51 -0800414 void showAllAppsCling() {
415 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
416 mHasShownAllAppsCling = true;
417 // Calculate the position for the cling punch through
418 int[] offset = new int[2];
419 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
420 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
421 // PagedViews are centered horizontally but top aligned
422 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
423 offset[0];
424 pos[1] += offset[1];
425 mLauncher.showFirstRunAllAppsCling(pos);
426 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700427 }
428
429 @Override
430 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
431 int width = MeasureSpec.getSize(widthMeasureSpec);
432 int height = MeasureSpec.getSize(heightMeasureSpec);
433 if (!isDataReady()) {
434 if (testDataReady()) {
435 setDataIsReady();
436 setMeasuredDimension(width, height);
437 onDataReady(width, height);
438 }
439 }
440
441 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
442 }
443
Winson Chung34efdaf2011-05-24 14:19:56 -0700444 /** Removes and returns the ResolveInfo with the specified ComponentName */
445 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
446 ComponentName cn) {
447 Iterator<ResolveInfo> iter = list.iterator();
448 while (iter.hasNext()) {
449 ResolveInfo rinfo = iter.next();
450 ActivityInfo info = rinfo.activityInfo;
451 ComponentName c = new ComponentName(info.packageName, info.name);
452 if (c.equals(cn)) {
453 iter.remove();
454 return rinfo;
455 }
456 }
457 return null;
458 }
459
Winson Chung785d2eb2011-04-14 16:08:02 -0700460 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700461 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
462 // by a broadcast receiver, and in order for it to work correctly, we need to know that
463 // the AppWidgetService has already received and processed the same broadcast. Since there
464 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
465 // we should have a more precise way of ensuring the AppWidgetService is up to date.
466 postDelayed(new Runnable() {
467 public void run() {
468 updatePackages();
469 }
470 }, 500);
471 }
472
473 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700474 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700475 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700476 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700477 List<AppWidgetProviderInfo> widgets =
478 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700479 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
480 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800481 for (AppWidgetProviderInfo widget : widgets) {
482 if (widget.minWidth > 0 && widget.minHeight > 0) {
483 mWidgets.add(widget);
484 } else {
485 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
486 widget.minWidth + ", " + widget.minHeight + ")");
487 }
488 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700489 mWidgets.addAll(shortcuts);
490 Collections.sort(mWidgets,
491 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700492 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700493
Winson Chung875de7e2011-06-28 14:25:17 -0700494 if (wasEmpty) {
495 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
496 // a layout to do this test and invalidate the page data when ready.
497 if (testDataReady()) requestLayout();
498 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700499 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700500 invalidatePageData();
501 }
Winson Chung4b576be2011-04-27 17:40:20 -0700502 }
503
504 @Override
505 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700506 // When we have exited all apps or are in transition, disregard clicks
507 if (!mLauncher.isAllAppsCustomizeOpen() ||
508 mLauncher.getWorkspace().isSwitchingState()) return;
509
Winson Chung4b576be2011-04-27 17:40:20 -0700510 if (v instanceof PagedViewIcon) {
511 // Animate some feedback to the click
512 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
513 animateClickFeedback(v, new Runnable() {
514 @Override
515 public void run() {
516 mLauncher.startActivitySafely(appInfo.intent, appInfo);
517 }
518 });
519 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700520 // Let the user know that they have to long press to add a widget
521 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
522 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700523
Winson Chungd2e87b32011-06-02 10:53:07 -0700524 // Create a little animation to show that the widget can move
525 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
526 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
527 AnimatorSet bounce = new AnimatorSet();
528 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
529 tyuAnim.setDuration(125);
530 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
531 tydAnim.setDuration(100);
532 bounce.play(tyuAnim).before(tydAnim);
533 bounce.setInterpolator(new AccelerateInterpolator());
534 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700535 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700536 }
537
Winson Chungc6f10b92011-11-14 11:39:07 -0800538 public boolean onKey(View v, int keyCode, KeyEvent event) {
539 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
540 }
541
Winson Chung785d2eb2011-04-14 16:08:02 -0700542 /*
543 * PagedViewWithDraggableItems implementation
544 */
545 @Override
546 protected void determineDraggingStart(android.view.MotionEvent ev) {
547 // Disable dragging by pulling an app down for now.
548 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700549
Winson Chung4b576be2011-04-27 17:40:20 -0700550 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700551 mLauncher.getWorkspace().onDragStartedWithItem(v);
552 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700553 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700554
Winson Chung4b576be2011-04-27 17:40:20 -0700555 private void beginDraggingWidget(View v) {
556 // Get the widget preview as the drag representation
557 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700558 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700559
560 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800561 Bitmap preview;
562 Bitmap outline;
Winson Chung1ed747a2011-05-03 16:18:34 -0700563 if (createItemInfo instanceof PendingAddWidgetInfo) {
564 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700565 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700566 createItemInfo.spanX = spanXY[0];
567 createItemInfo.spanY = spanXY[1];
568
Michael Jurka038f9d82011-11-03 13:50:45 -0700569 int[] maxSize = mLauncher.getWorkspace().estimateItemSize(spanXY[0], spanXY[1],
570 createWidgetInfo, true);
Winson Chung1120e032011-11-22 16:11:31 -0800571 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurka038f9d82011-11-03 13:50:45 -0700572 createWidgetInfo.icon, spanXY[0], spanXY[1], maxSize[0], maxSize[1]);
Winson Chung1ed747a2011-05-03 16:18:34 -0700573 } else {
574 // Workaround for the fact that we don't keep the original ResolveInfo associated with
575 // the shortcut around. To get the icon, we just render the preview image (which has
576 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800577 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700578 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800579 Drawable d = image.getDrawable();
580 mCanvas.setBitmap(preview);
581 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700582 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700583 createItemInfo.spanX = createItemInfo.spanY = 1;
584 }
Winson Chung4b576be2011-04-27 17:40:20 -0700585
Peter Ng8db70002011-10-25 15:40:08 -0700586 // We use a custom alpha clip table for the default widget previews
587 Paint alphaClipPaint = null;
588 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700589 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700590 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
591 alphaClipPaint = new Paint();
592 alphaClipPaint.setMaskFilter(alphaClipTable);
593 }
594 }
595
Winson Chung1120e032011-11-22 16:11:31 -0800596 // Save the preview for the outline generation, then dim the preview
597 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
598 false);
599 mCanvas.setBitmap(preview);
600 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
601 mCanvas.setBitmap(null);
602
Winson Chung4b576be2011-04-27 17:40:20 -0700603 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800604 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700605 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800606 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
607 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700608 DragController.DRAG_ACTION_COPY, null);
Winson Chung1120e032011-11-22 16:11:31 -0800609 outline.recycle();
610 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700611 }
612 @Override
613 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700614 // Dismiss the cling
615 mLauncher.dismissAllAppsCling(null);
616
Winson Chung4b576be2011-04-27 17:40:20 -0700617 if (!super.beginDragging(v)) return false;
618
Winson Chungc07918d2011-07-01 15:35:26 -0700619 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700620 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700621
Winson Chung4b576be2011-04-27 17:40:20 -0700622 if (v instanceof PagedViewIcon) {
623 beginDraggingApplication(v);
624 } else if (v instanceof PagedViewWidget) {
625 beginDraggingWidget(v);
626 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700627 return true;
628 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700629 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700630 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700631 if (!success || (target != mLauncher.getWorkspace() &&
632 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700633 // Exit spring loaded mode if we have not successfully dropped or have not handled the
634 // drop in Workspace
635 mLauncher.exitSpringLoadedDragMode();
636 }
Adam Cohen446e9402011-09-15 18:21:21 -0700637 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700638
Winson Chung785d2eb2011-04-14 16:08:02 -0700639 }
640
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700642 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700643 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700644
645 // Display an error message if the drag failed due to there not being enough space on the
646 // target layout we were dropping on.
647 if (!success) {
648 boolean showOutOfSpaceMessage = false;
649 if (target instanceof Workspace) {
650 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
651 Workspace workspace = (Workspace) target;
652 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700653 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700654 if (layout != null) {
655 layout.calculateSpans(itemInfo);
656 showOutOfSpaceMessage =
657 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
658 }
659 }
Winson Chungfc79c802011-05-02 13:35:34 -0700660 if (showOutOfSpaceMessage) {
661 mLauncher.showOutOfSpaceMessage();
662 }
663 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700664 }
665
Winson Chung7f0acdd2011-09-19 18:34:19 -0700666 @Override
667 protected void onDetachedFromWindow() {
668 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700669 cancelAllTasks();
670 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700671
Adam Cohen0cd3b642011-10-14 14:58:00 -0700672 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700673 // Clean up all the async tasks
674 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
675 while (iter.hasNext()) {
676 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
677 task.cancel(false);
678 iter.remove();
679 }
680 }
681
Winson Chung785d2eb2011-04-14 16:08:02 -0700682 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700683 if (type == ContentType.Widgets) {
684 invalidatePageData(mNumAppsPages, true);
685 } else if (type == ContentType.Applications) {
686 invalidatePageData(0, true);
687 }
Winson Chungb44b5242011-06-13 11:32:14 -0700688 }
689
Adam Cohen0cd3b642011-10-14 14:58:00 -0700690 protected void snapToPage(int whichPage, int delta, int duration) {
691 super.snapToPage(whichPage, delta, duration);
692 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800693
694 // Update the thread priorities given the direction lookahead
695 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
696 while (iter.hasNext()) {
697 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
698 int pageIndex = task.page + mNumAppsPages;
699 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
700 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
701 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
702 } else {
703 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
704 }
705 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700706 }
707
708 private void updateCurrentTab(int currentPage) {
709 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800710 if (tabHost != null) {
711 String tag = tabHost.getCurrentTabTag();
712 if (tag != null) {
713 if (currentPage >= mNumAppsPages &&
714 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
715 tabHost.setCurrentTabFromContent(ContentType.Widgets);
716 } else if (currentPage < mNumAppsPages &&
717 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
718 tabHost.setCurrentTabFromContent(ContentType.Applications);
719 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700720 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700721 }
722 }
723
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 /*
725 * Apps PagedView implementation
726 */
Winson Chung63257c12011-05-05 17:06:13 -0700727 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
728 int childCount = layout.getChildCount();
729 for (int i = 0; i < childCount; ++i) {
730 layout.getChildAt(i).setVisibility(visibility);
731 }
732 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700733 private void setupPage(PagedViewCellLayout layout) {
734 layout.setCellCount(mCellCountX, mCellCountY);
735 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
736 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
737 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
738
Winson Chung63257c12011-05-05 17:06:13 -0700739 // Note: We force a measure here to get around the fact that when we do layout calculations
740 // immediately after syncing, we don't have a proper width. That said, we already know the
741 // expected page width, so we can actually optimize by hiding all the TextView-based
742 // children that are expensive to measure, and let that happen naturally later.
743 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700744 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700745 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700746 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700747 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700748 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700750
Winson Chungf314b0e2011-08-16 11:54:27 -0700751 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700752 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700753 int numCells = mCellCountX * mCellCountY;
754 int startIndex = page * numCells;
755 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700756 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700757
Winson Chung785d2eb2011-04-14 16:08:02 -0700758 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700759 ArrayList<Object> items = new ArrayList<Object>();
760 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700761 for (int i = startIndex; i < endIndex; ++i) {
762 ApplicationInfo info = mApps.get(i);
763 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
764 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700765 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700766 icon.setOnClickListener(this);
767 icon.setOnLongClickListener(this);
768 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800769 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700770
771 int index = i - startIndex;
772 int x = index % mCellCountX;
773 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700774 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700775
776 items.add(info);
777 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700778 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700779
Winson Chungf0ea4d32011-06-06 14:27:16 -0700780 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700781
Winson Chung8ff87132011-09-30 19:40:46 -0700782 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700783 if (mFadeInAdjacentScreens) {
784 prepareGenerateHoloOutlinesTask(page, items, images);
785 }
Winson Chung8ff87132011-09-30 19:40:46 -0700786 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700787 }
Winson Chungb44b5242011-06-13 11:32:14 -0700788
789 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800790 * A helper to return the priority for loading of the specified widget page.
791 */
792 private int getWidgetPageLoadPriority(int page) {
793 // If we are snapping to another page, use that index as the target page index
794 int toPage = mCurrentPage;
795 if (mNextPage > -1) {
796 toPage = mNextPage;
797 }
798
799 // We use the distance from the target page as an initial guess of priority, but if there
800 // are no pages of higher priority than the page specified, then bump up the priority of
801 // the specified page.
802 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
803 int minPageDiff = Integer.MAX_VALUE;
804 while (iter.hasNext()) {
805 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
806 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
807 }
808
809 int rawPageDiff = Math.abs(page - toPage);
810 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
811 }
812 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700813 * Return the appropriate thread priority for loading for a given page (we give the current
814 * page much higher priority)
815 */
816 private int getThreadPriorityForPage(int page) {
817 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800818 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700819 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800820 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700821 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800822 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700823 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800824 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700825 }
826 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700827 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800828 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700829 return Math.max(0, pageDiff * sPageSleepDelay);
830 }
Winson Chungb44b5242011-06-13 11:32:14 -0700831 /**
832 * Creates and executes a new AsyncTask to load a page of widget previews.
833 */
834 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700835 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800836
Winson Chungb44b5242011-06-13 11:32:14 -0700837 // Prune all tasks that are no longer needed
838 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
839 while (iter.hasNext()) {
840 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800841 int taskPage = task.page + mNumAppsPages;
842 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
843 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700844 task.cancel(false);
845 iter.remove();
846 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800847 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700848 }
849 }
850
Winson Chungf314b0e2011-08-16 11:54:27 -0700851 // 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 -0700852 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700853 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700854 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700855 @Override
856 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700857 try {
Winson Chung09945932011-09-20 14:22:40 -0700858 try {
859 Thread.sleep(sleepMs);
860 } catch (Exception e) {}
861 loadWidgetPreviewsInBackground(task, data);
862 } finally {
863 if (task.isCancelled()) {
864 data.cleanup(true);
865 }
866 }
Winson Chungb44b5242011-06-13 11:32:14 -0700867 }
868 },
869 new AsyncTaskCallback() {
870 @Override
871 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700872 try {
873 mRunningTasks.remove(task);
874 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700875 onSyncWidgetPageItems(data);
876 } finally {
877 data.cleanup(task.isCancelled());
878 }
Winson Chungb44b5242011-06-13 11:32:14 -0700879 }
Winson Chung09945932011-09-20 14:22:40 -0700880 });
Winson Chungb44b5242011-06-13 11:32:14 -0700881
882 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700883 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700884 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800885 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700886 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
887 mRunningTasks.add(t);
888 }
889 /**
890 * Creates and executes a new AsyncTask to load the outlines for a page of content.
891 */
892 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
893 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700894 // Prune old tasks for this page
895 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
896 while (iter.hasNext()) {
897 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
898 int taskPage = task.page;
899 if ((taskPage == page) &&
900 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
901 task.cancel(false);
902 iter.remove();
903 }
904 }
905
Winson Chungb44b5242011-06-13 11:32:14 -0700906 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
907 new AsyncTaskCallback() {
908 @Override
909 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700910 try {
911 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700912 task.syncThreadPriority();
913
Winson Chung09945932011-09-20 14:22:40 -0700914 ArrayList<Bitmap> images = data.generatedImages;
915 ArrayList<Bitmap> srcImages = data.sourceImages;
916 int count = srcImages.size();
917 Canvas c = new Canvas();
918 for (int i = 0; i < count && !task.isCancelled(); ++i) {
919 // Before work on each item, ensure that this task is running at the correct
920 // priority
921 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700922
Winson Chung09945932011-09-20 14:22:40 -0700923 Bitmap b = srcImages.get(i);
924 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
925 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700926
Winson Chung09945932011-09-20 14:22:40 -0700927 c.setBitmap(outline);
928 c.save();
929 c.drawBitmap(b, 0, 0, null);
930 c.restore();
931 c.setBitmap(null);
932
933 images.add(outline);
934 }
935 } finally {
936 if (task.isCancelled()) {
937 data.cleanup(true);
938 }
Winson Chungb44b5242011-06-13 11:32:14 -0700939 }
940 }
941 },
942 new AsyncTaskCallback() {
943 @Override
944 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700945 try {
946 mRunningTasks.remove(task);
947 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700948 onHolographicPageItemsLoaded(data);
949 } finally {
950 data.cleanup(task.isCancelled());
951 }
Winson Chungb44b5242011-06-13 11:32:14 -0700952 }
953 });
954
955 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700956 AppsCustomizeAsyncTask t =
Adam Cohen0cd3b642011-10-14 14:58:00 -0700957 new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700958 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
959 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
960 mRunningTasks.add(t);
961 }
962
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 /*
964 * Widgets PagedView implementation
965 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700966 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700967 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
968 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700969
970 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700971 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700972 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
973 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700975 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700977
Winson Chung5fc72b32011-10-11 17:53:58 -0700978 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
979 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700980 }
981 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung5fc72b32011-10-11 17:53:58 -0700982 float scale) {
983 renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
984 }
985 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
986 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700987 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700988 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700989 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700990 Rect oldBounds = d.copyBounds();
991 d.setBounds(x, y, x + w, y + h);
992 d.draw(c);
993 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700994 if (multiplyColor != 0xFFFFFFFF) {
995 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
996 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700997 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700998 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700999 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001000 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001001 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001002 int offset = 0;
1003 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001004 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001005
Winson Chung1ed747a2011-05-03 16:18:34 -07001006 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001007 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001008 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001009 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001010 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001011
Michael Jurka038f9d82011-11-03 13:50:45 -07001012 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1013 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001014 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001015 String packageName = provider.getPackageName();
1016 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1017 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001018
Michael Jurka038f9d82011-11-03 13:50:45 -07001019 Drawable drawable = null;
1020 if (previewImage != 0) {
1021 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1022 if (drawable == null) {
1023 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1024 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001025 }
1026 }
1027
Michael Jurka038f9d82011-11-03 13:50:45 -07001028 int bitmapWidth;
1029 int bitmapHeight;
1030 boolean widgetPreviewExists = (drawable != null);
1031 if (widgetPreviewExists) {
1032 bitmapWidth = drawable.getIntrinsicWidth();
1033 bitmapHeight = drawable.getIntrinsicHeight();
1034
1035 // Cap the size so widget previews don't appear larger than the actual widget
1036 maxWidth = Math.min(maxWidth, mWidgetSpacingLayout.estimateCellWidth(cellHSpan));
1037 maxHeight = Math.min(maxHeight, mWidgetSpacingLayout.estimateCellHeight(cellVSpan));
1038 } else {
1039 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001040 // TODO: This actually uses the apps customize cell layout params, where as we make want
1041 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001042 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1043 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001044 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001045 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001046 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001047 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001048 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001049 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001050 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001051 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001052 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001053 }
1054
1055 float scale = 1f;
1056 if (bitmapWidth > maxWidth) {
1057 scale = maxWidth / (float) bitmapWidth;
1058 }
1059 if (bitmapHeight * scale > maxHeight) {
1060 scale = maxHeight / (float) bitmapHeight;
1061 }
1062 if (scale != 1f) {
1063 bitmapWidth = (int) (scale * bitmapWidth);
1064 bitmapHeight = (int) (scale * bitmapHeight);
1065 }
1066
1067 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1068
1069 if (widgetPreviewExists) {
1070 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1071 } else {
1072 // Generate a preview image if we couldn't load one
1073 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1074 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1075 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001076 if (cellHSpan != 1 || cellVSpan != 1) {
1077 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1078 bitmapHeight);
1079 }
Winson Chung4b576be2011-04-27 17:40:20 -07001080
1081 // Draw the icon in the top left corner
1082 try {
1083 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001084 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1085 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001086 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001087 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001088 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1089
Peter Ng8db70002011-10-25 15:40:08 -07001090 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1091 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001092 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001093 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001094 }
Winson Chungb44b5242011-06-13 11:32:14 -07001095 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001096 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001097
Michael Jurka038f9d82011-11-03 13:50:45 -07001098 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001099 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001100
Winson Chungd2945262011-06-24 15:22:14 -07001101 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001102 final ArrayList<Object> items = new ArrayList<Object>();
1103 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1104 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001105 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001106 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1107 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001108 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001109
Winson Chunge4a647f2011-09-30 14:41:25 -07001110 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001111 int offset = page * numItemsPerPage;
1112 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1113 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001114 }
1115
Winson Chunge4a647f2011-09-30 14:41:25 -07001116 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka038f9d82011-11-03 13:50:45 -07001117 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001118 layout.setColumnCount(layout.getCellCountX());
1119 for (int i = 0; i < items.size(); ++i) {
1120 Object rawInfo = items.get(i);
1121 PendingAddItemInfo createItemInfo = null;
1122 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1123 R.layout.apps_customize_widget, layout, false);
1124 if (rawInfo instanceof AppWidgetProviderInfo) {
1125 // Fill in the widget information
1126 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1127 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1128 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1129 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1130 mHolographicOutlineHelper);
1131 widget.setTag(createItemInfo);
1132 } else if (rawInfo instanceof ResolveInfo) {
1133 // Fill in the shortcuts information
1134 ResolveInfo info = (ResolveInfo) rawInfo;
1135 createItemInfo = new PendingAddItemInfo();
1136 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1137 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1138 info.activityInfo.name);
1139 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1140 widget.setTag(createItemInfo);
1141 }
1142 widget.setOnClickListener(this);
1143 widget.setOnLongClickListener(this);
1144 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001145 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001146
1147 // Layout each widget
1148 int ix = i % mWidgetCountX;
1149 int iy = i / mWidgetCountX;
1150 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1151 GridLayout.spec(iy, GridLayout.LEFT),
1152 GridLayout.spec(ix, GridLayout.TOP));
1153 lp.width = cellWidth;
1154 lp.height = cellHeight;
1155 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1156 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1157 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1158 layout.addView(widget, lp);
1159 }
1160
Michael Jurka038f9d82011-11-03 13:50:45 -07001161 // wait until a call on onLayout to start loading, because
1162 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1163 // TODO: can we do a measure/layout immediately?
1164 layout.setOnLayoutListener(new Runnable() {
1165 public void run() {
1166 // Load the widget previews
1167 int maxPreviewWidth = cellWidth;
1168 int maxPreviewHeight = cellHeight;
1169 if (layout.getChildCount() > 0) {
1170 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1171 int[] maxSize = w.getPreviewSize();
1172 maxPreviewWidth = maxSize[0];
1173 maxPreviewHeight = maxSize[1];
1174 }
1175 if (immediate) {
1176 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1177 maxPreviewWidth, maxPreviewHeight, null, null);
1178 loadWidgetPreviewsInBackground(null, data);
1179 onSyncWidgetPageItems(data);
1180 } else {
1181 prepareLoadWidgetPreviewsTask(page, items,
1182 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1183 }
1184 }
1185 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001186 }
1187 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1188 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001189 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1190 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001191 if (task != null) {
1192 // Ensure that this task starts running at the correct priority
1193 task.syncThreadPriority();
1194 }
1195
1196 // Load each of the widget/shortcut previews
1197 ArrayList<Object> items = data.items;
1198 ArrayList<Bitmap> images = data.generatedImages;
1199 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001200 for (int i = 0; i < count; ++i) {
1201 if (task != null) {
1202 // Ensure we haven't been cancelled yet
1203 if (task.isCancelled()) break;
1204 // Before work on each item, ensure that this task is running at the correct
1205 // priority
1206 task.syncThreadPriority();
1207 }
1208
1209 Object rawInfo = items.get(i);
1210 if (rawInfo instanceof AppWidgetProviderInfo) {
1211 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001212 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001213 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
1214 cellSpans[0], cellSpans[1], data.maxImageWidth, data.maxImageHeight);
1215 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001216 } else if (rawInfo instanceof ResolveInfo) {
1217 // Fill in the shortcuts information
1218 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001219 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001220 }
1221 }
Winson Chungb44b5242011-06-13 11:32:14 -07001222 }
1223 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1224 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001225 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001226
Winson Chungb44b5242011-06-13 11:32:14 -07001227 ArrayList<Object> items = data.items;
1228 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001229 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001230 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1231 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001232 Bitmap preview = data.generatedImages.get(i);
Michael Jurka038f9d82011-11-03 13:50:45 -07001233 widget.applyPreview(new FastBitmapDrawable(preview), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001234 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001235 }
Winson Chungb44b5242011-06-13 11:32:14 -07001236
Winson Chung68e4c642011-11-10 15:48:25 -08001237 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001238 invalidate();
Winson Chung68e4c642011-11-10 15:48:25 -08001239
Winson Chung8ff87132011-09-30 19:40:46 -07001240 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001241 if (mFadeInAdjacentScreens) {
1242 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1243 }
Winson Chung8ff87132011-09-30 19:40:46 -07001244 */
Winson Chung68e4c642011-11-10 15:48:25 -08001245
1246 // Update all thread priorities
1247 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1248 while (iter.hasNext()) {
1249 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1250 int pageIndex = task.page + mNumAppsPages;
1251 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1252 }
Winson Chungb44b5242011-06-13 11:32:14 -07001253 }
1254 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1255 // Invalidate early to short-circuit children invalidates
1256 invalidate();
1257
1258 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001259 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001260 if (layout instanceof PagedViewCellLayout) {
1261 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1262 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001263 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001264 for (int i = 0; i < count; ++i) {
1265 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001266 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001267 }
1268 } else {
1269 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001270 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001271 for (int i = 0; i < count; ++i) {
1272 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001273 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001274 }
1275 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001276 }
Winson Chung46af2e82011-05-09 16:00:53 -07001277
Winson Chung785d2eb2011-04-14 16:08:02 -07001278 @Override
1279 public void syncPages() {
1280 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001281 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001282
Adam Cohen0cd3b642011-10-14 14:58:00 -07001283 Context context = getContext();
1284 for (int j = 0; j < mNumWidgetPages; ++j) {
1285 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1286 mWidgetCountY);
1287 setupPage(layout);
1288 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1289 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001290 }
1291
Adam Cohen0cd3b642011-10-14 14:58:00 -07001292 for (int i = 0; i < mNumAppsPages; ++i) {
1293 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1294 setupPage(layout);
1295 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001296 }
1297 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001298
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001300 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001301 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001302 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001303 } else {
1304 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 }
1306 }
1307
Adam Cohen22f823d2011-09-01 17:22:18 -07001308 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1309 // it is in the z-order. This is important to insure touch events are handled correctly.
1310 View getPageAt(int index) {
1311 return getChildAt(getChildCount() - index - 1);
1312 }
1313
Adam Cohenae4f1552011-10-20 00:15:42 -07001314 @Override
1315 protected int indexToPage(int index) {
1316 return getChildCount() - index - 1;
1317 }
1318
Adam Cohen22f823d2011-09-01 17:22:18 -07001319 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1320 @Override
1321 protected void screenScrolled(int screenCenter) {
1322 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001323
1324 for (int i = 0; i < getChildCount(); i++) {
1325 View v = getPageAt(i);
1326 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001327 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001328
1329 float interpolatedProgress =
1330 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1331 float scale = (1 - interpolatedProgress) +
1332 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1333 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001334
Adam Cohen2591f6a2011-10-25 14:36:40 -07001335 float alpha;
1336
1337 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1338 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001339 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001340 } else {
1341 // On large screens we need to fade the page as it nears its leftmost position
1342 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1343 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001344
1345 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1346 int pageWidth = v.getMeasuredWidth();
1347 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001348
1349 if (PERFORM_OVERSCROLL_ROTATION) {
1350 if (i == 0 && scrollProgress < 0) {
1351 // Overscroll to the left
1352 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1353 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1354 scale = 1.0f;
1355 alpha = 1.0f;
1356 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001357 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001358 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1359 // Overscroll to the right
1360 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1361 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1362 scale = 1.0f;
1363 alpha = 1.0f;
1364 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001365 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001366 } else {
1367 v.setPivotY(pageHeight / 2.0f);
1368 v.setPivotX(pageWidth / 2.0f);
1369 v.setRotationY(0f);
1370 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001371 }
1372
1373 v.setTranslationX(translationX);
1374 v.setScaleX(scale);
1375 v.setScaleY(scale);
1376 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001377
1378 // If the view has 0 alpha, we set it to be invisible so as to prevent
1379 // it from accepting touches
1380 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1381 v.setVisibility(INVISIBLE);
1382 } else if (v.getVisibility() != VISIBLE) {
1383 v.setVisibility(VISIBLE);
1384 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001385 }
1386 }
1387 }
1388
1389 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001390 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001391 }
1392
Winson Chung785d2eb2011-04-14 16:08:02 -07001393 /**
1394 * Used by the parent to get the content width to set the tab bar to
1395 * @return
1396 */
1397 public int getPageContentWidth() {
1398 return mContentWidth;
1399 }
1400
Winson Chungb26f3d62011-06-02 10:49:29 -07001401 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001402 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001403 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001404
1405 // We reset the save index when we change pages so that it will be recalculated on next
1406 // rotation
1407 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001408 }
1409
Winson Chung785d2eb2011-04-14 16:08:02 -07001410 /*
1411 * AllAppsView implementation
1412 */
1413 @Override
1414 public void setup(Launcher launcher, DragController dragController) {
1415 mLauncher = launcher;
1416 mDragController = dragController;
1417 }
1418 @Override
1419 public void zoom(float zoom, boolean animate) {
1420 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1421 }
1422 @Override
1423 public boolean isVisible() {
1424 return (getVisibility() == VISIBLE);
1425 }
1426 @Override
1427 public boolean isAnimating() {
1428 return false;
1429 }
1430 @Override
1431 public void setApps(ArrayList<ApplicationInfo> list) {
1432 mApps = list;
1433 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001434 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001435
Winson Chung875de7e2011-06-28 14:25:17 -07001436 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1437 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001438 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001439 }
1440 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1441 // We add it in place, in alphabetical order
1442 int count = list.size();
1443 for (int i = 0; i < count; ++i) {
1444 ApplicationInfo info = list.get(i);
1445 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1446 if (index < 0) {
1447 mApps.add(-(index + 1), info);
1448 }
1449 }
1450 }
1451 @Override
1452 public void addApps(ArrayList<ApplicationInfo> list) {
1453 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001454 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 invalidatePageData();
1456 }
1457 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1458 ComponentName removeComponent = item.intent.getComponent();
1459 int length = list.size();
1460 for (int i = 0; i < length; ++i) {
1461 ApplicationInfo info = list.get(i);
1462 if (info.intent.getComponent().equals(removeComponent)) {
1463 return i;
1464 }
1465 }
1466 return -1;
1467 }
1468 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1469 // loop through all the apps and remove apps that have the same component
1470 int length = list.size();
1471 for (int i = 0; i < length; ++i) {
1472 ApplicationInfo info = list.get(i);
1473 int removeIndex = findAppByComponent(mApps, info);
1474 if (removeIndex > -1) {
1475 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001476 }
1477 }
1478 }
1479 @Override
1480 public void removeApps(ArrayList<ApplicationInfo> list) {
1481 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001482 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001483 invalidatePageData();
1484 }
1485 @Override
1486 public void updateApps(ArrayList<ApplicationInfo> list) {
1487 // We remove and re-add the updated applications list because it's properties may have
1488 // changed (ie. the title), and this will ensure that the items will be in their proper
1489 // place in the list.
1490 removeAppsWithoutInvalidate(list);
1491 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001492 updatePageCounts();
1493
Winson Chung785d2eb2011-04-14 16:08:02 -07001494 invalidatePageData();
1495 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001496
Winson Chung785d2eb2011-04-14 16:08:02 -07001497 @Override
1498 public void reset() {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001499 AppsCustomizeTabHost tabHost = getTabHost();
1500 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001501 if (tag != null) {
1502 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1503 tabHost.setCurrentTabFromContent(ContentType.Applications);
1504 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001505 }
1506 if (mCurrentPage != 0) {
1507 invalidatePageData(0);
1508 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001510
1511 private AppsCustomizeTabHost getTabHost() {
1512 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1513 }
1514
Winson Chung785d2eb2011-04-14 16:08:02 -07001515 @Override
1516 public void dumpState() {
1517 // TODO: Dump information related to current list of Applications, Widgets, etc.
1518 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1519 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1520 }
Adam Cohen4e844012011-11-09 13:48:04 -08001521
Winson Chung785d2eb2011-04-14 16:08:02 -07001522 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001523 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001524 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001525 for (Object i: list) {
1526 if (i instanceof AppWidgetProviderInfo) {
1527 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1528 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1529 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1530 + " initialLayout=" + info.initialLayout
1531 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1532 } else if (i instanceof ResolveInfo) {
1533 ResolveInfo info = (ResolveInfo) i;
1534 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1535 + info.icon);
1536 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001537 }
1538 }
Adam Cohen4e844012011-11-09 13:48:04 -08001539
Winson Chung785d2eb2011-04-14 16:08:02 -07001540 @Override
1541 public void surrender() {
1542 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1543 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001544
1545 // Stop all background tasks
1546 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001547 }
Winson Chung007c6982011-06-14 13:27:53 -07001548
Winson Chung68e4c642011-11-10 15:48:25 -08001549
Winson Chungb44b5242011-06-13 11:32:14 -07001550 /*
1551 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1552 * widget previews in the background with the AsyncTasks.
1553 */
Winson Chung68e4c642011-11-10 15:48:25 -08001554 final static int sLookBehindPageCount = 2;
1555 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001556 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001557 final int count = getChildCount();
1558 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1559 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1560 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001561 }
1562 protected int getAssociatedUpperPageBound(int page) {
1563 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001564 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1565 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1566 count - 1);
1567 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001568 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001569
1570 @Override
1571 protected String getCurrentPageDescription() {
1572 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1573 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001574 int count = 0;
1575
Adam Cohen0cd3b642011-10-14 14:58:00 -07001576 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001577 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001578 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001579 } else {
1580 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001581 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001582 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001583 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001584
Adam Cohend3357b12011-10-18 14:58:11 -07001585 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001586 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001587}