blob: b208e884fd0f40b612231e8949647212edb2c630 [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;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070035import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.graphics.Rect;
Adam Cohenb5ba0972011-09-07 18:02:31 -070037import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070039import android.os.AsyncTask;
40import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070045import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070046import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohenb5ba0972011-09-07 18:02:31 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
54import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import com.android.launcher2.DropTarget.DragObject;
56
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
74 LoadWidgetPreviewData,
75 LoadHolographicIconsData
76 }
77
Winson Chungb44b5242011-06-13 11:32:14 -070078 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
79 AsyncTaskCallback postR) {
80 page = p;
81 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070082 sourceImages = si;
83 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070084 cellWidth = cellHeight = -1;
85 doInBackgroundCallback = bgR;
86 postExecuteCallback = postR;
87 }
Winson Chungd2945262011-06-24 15:22:14 -070088 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070089 AsyncTaskCallback postR) {
90 page = p;
91 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070092 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070093 cellWidth = cw;
94 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070095 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070096 doInBackgroundCallback = bgR;
97 postExecuteCallback = postR;
98 }
99 int page;
100 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700101 ArrayList<Bitmap> sourceImages;
102 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 int cellWidth;
104 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700105 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700106 AsyncTaskCallback doInBackgroundCallback;
107 AsyncTaskCallback postExecuteCallback;
108}
Winson Chung4e076542011-06-23 13:04:10 -0700109
Winson Chungb44b5242011-06-13 11:32:14 -0700110/**
111 * A generic template for an async task used in AppsCustomize.
112 */
113class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700114 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700115 page = p;
116 pageContentType = t;
117 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700118 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700119 }
120 @Override
121 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
122 if (params.length != 1) return null;
123 // Load each of the widget previews in the background
124 params[0].doInBackgroundCallback.run(this, params[0]);
125 return params[0];
126 }
127 @Override
128 protected void onPostExecute(AsyncTaskPageData result) {
129 // All the widget previews are loaded, so we can just callback to inflate the page
130 result.postExecuteCallback.run(this, result);
131 }
132
133 void setThreadPriority(int p) {
134 threadPriority = p;
135 }
136 void syncThreadPriority() {
137 Process.setThreadPriority(threadPriority);
138 }
139
140 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700141 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 int page;
143 AppsCustomizePagedView.ContentType pageContentType;
144 int threadPriority;
145}
Winson Chungb44b5242011-06-13 11:32:14 -0700146
147/**
148 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
149 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700150public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
151 AllAppsView, View.OnClickListener, DragSource {
152 static final String LOG_TAG = "AppsCustomizePagedView";
153
154 /**
155 * The different content types that this paged view can show.
156 */
157 public enum ContentType {
158 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700159 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700160 }
161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
170 private int mRestorePage = -1;
171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
173 private ContentType mContentType;
174 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700175 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700176
177 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700178 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700179 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700180 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700181 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700182
183 // Dimens
184 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700185 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700186 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700187 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700188 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700189 private final int mWidgetPreviewIconPaddedDimension;
190 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700191 private PagedViewCellLayout mWidgetSpacingLayout;
192
Adam Cohen22f823d2011-09-01 17:22:18 -0700193 // Relating to the scroll and overscroll effects
194 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700195 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700196 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700197 private static float TRANSITION_PIVOT = 0.65f;
198 private static float TRANSITION_MAX_ROTATION = 22;
199 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700200 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen22f823d2011-09-01 17:22:18 -0700201
Winson Chungb44b5242011-06-13 11:32:14 -0700202 // Previews & outlines
203 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
204 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chungf314b0e2011-08-16 11:54:27 -0700205 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700206
Winson Chung785d2eb2011-04-14 16:08:02 -0700207 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
208 super(context, attrs);
209 mLayoutInflater = LayoutInflater.from(context);
210 mPackageManager = context.getPackageManager();
211 mContentType = ContentType.Applications;
212 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700213 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700214 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700215 mHolographicOutlineHelper = new HolographicOutlineHelper();
216 mCanvas = new Canvas();
217 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700218
219 // Save the default widget preview background
220 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700221 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700222 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
223 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700224
225 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700226 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700227 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
228 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
229 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700230 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700231 mWidgetWidthGap =
232 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
233 mWidgetHeightGap =
234 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700235 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
236 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
237 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700238 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700239
240 // The max widget span is the length N, such that NxN is the largest bounds that the widget
241 // preview can be before applying the widget scaling
242 mMinWidgetSpan = 1;
243 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700244
245 // The padding on the non-matched dimension for the default widget preview icons
246 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700247 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700248 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700249 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700250 }
251
252 @Override
253 protected void init() {
254 super.init();
255 mCenterPagesVertically = false;
256
257 Context context = getContext();
258 Resources r = context.getResources();
259 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
260 }
261
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700263 protected void onUnhandledTap(MotionEvent ev) {
264 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700265 // Dismiss AppsCustomize if we tap
266 mLauncher.showWorkspace(true);
267 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700268 }
269
Winson Chung5afbf7b2011-07-25 11:53:08 -0700270 /** Returns the item index of the center item on this page so that we can restore to this
271 * item index when we rotate. */
272 private int getMiddleComponentIndexOnCurrentPage() {
273 int i = -1;
274 if (getPageCount() > 0) {
275 int currentPage = getCurrentPage();
276 switch (mContentType) {
277 case Applications: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700278 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700279 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
280 int numItemsPerPage = mCellCountX * mCellCountY;
281 int childCount = childrenLayout.getChildCount();
282 if (childCount > 0) {
283 i = (currentPage * numItemsPerPage) + (childCount / 2);
284 }}
285 break;
286 case Widgets: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700287 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700288 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
289 int childCount = layout.getChildCount();
290 if (childCount > 0) {
291 i = (currentPage * numItemsPerPage) + (childCount / 2);
292 }}
293 break;
294 }
295 }
296 return i;
297 }
298
299 /** Get the index of the item to restore to if we need to restore the current page. */
300 int getSaveInstanceStateIndex() {
301 if (mSaveInstanceStateItemIndex == -1) {
302 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
303 }
304 return mSaveInstanceStateItemIndex;
305 }
306
307 /** Returns the page in the current orientation which is expected to contain the specified
308 * item index. */
309 int getPageForComponent(int index) {
310 switch (mContentType) {
311 case Applications: {
312 int numItemsPerPage = mCellCountX * mCellCountY;
313 return (index / numItemsPerPage);
314 }
315 case Widgets: {
316 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
317 return (index / numItemsPerPage);
318 }}
319 return -1;
320 }
321
Winson Chungf0ea4d32011-06-06 14:27:16 -0700322 /**
323 * This differs from isDataReady as this is the test done if isDataReady is not set.
324 */
325 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700326 // We only do this test once, and we default to the Applications page, so we only really
327 // have to wait for there to be apps.
Winson Chung87acb482011-08-23 17:28:05 -0700328 if (mContentType == AppsCustomizePagedView.ContentType.Widgets) {
329 return !mApps.isEmpty() && !mWidgets.isEmpty();
330 } else {
331 return !mApps.isEmpty();
332 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700333 }
334
Winson Chung5afbf7b2011-07-25 11:53:08 -0700335 /** Restores the page for an item at the specified index */
336 void restorePageForIndex(int index) {
337 if (index < 0) return;
338
339 int page = getPageForComponent(index);
340 if (page > -1) {
341 if (getChildCount() > 0) {
342 invalidatePageData(page);
343 } else {
344 mRestorePage = page;
345 }
346 }
347 }
348
Winson Chungf0ea4d32011-06-06 14:27:16 -0700349 protected void onDataReady(int width, int height) {
350 // Note that we transpose the counts in portrait so that we get a similar layout
351 boolean isLandscape = getResources().getConfiguration().orientation ==
352 Configuration.ORIENTATION_LANDSCAPE;
353 int maxCellCountX = Integer.MAX_VALUE;
354 int maxCellCountY = Integer.MAX_VALUE;
355 if (LauncherApplication.isScreenLarge()) {
356 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
357 LauncherModel.getCellCountY());
358 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
359 LauncherModel.getCellCountX());
360 }
361
362 // Now that the data is ready, we can calculate the content width, the number of cells to
363 // use for each page
364 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
365 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
366 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
367 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
368 mCellCountX = mWidgetSpacingLayout.getCellCountX();
369 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung5a808352011-06-27 19:08:49 -0700370
Winson Chungdb1138b2011-06-30 14:39:35 -0700371 // Force a measure to update recalculate the gaps
372 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
373 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
374 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700376 invalidatePageData(Math.max(0, mRestorePage));
377 mRestorePage = -1;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
379
380 @Override
381 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
382 int width = MeasureSpec.getSize(widthMeasureSpec);
383 int height = MeasureSpec.getSize(heightMeasureSpec);
384 if (!isDataReady()) {
385 if (testDataReady()) {
386 setDataIsReady();
387 setMeasuredDimension(width, height);
388 onDataReady(width, height);
389 }
390 }
391
392 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
393 }
394
Winson Chung34efdaf2011-05-24 14:19:56 -0700395 /** Removes and returns the ResolveInfo with the specified ComponentName */
396 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
397 ComponentName cn) {
398 Iterator<ResolveInfo> iter = list.iterator();
399 while (iter.hasNext()) {
400 ResolveInfo rinfo = iter.next();
401 ActivityInfo info = rinfo.activityInfo;
402 ComponentName c = new ComponentName(info.packageName, info.name);
403 if (c.equals(cn)) {
404 iter.remove();
405 return rinfo;
406 }
407 }
408 return null;
409 }
410
Winson Chung785d2eb2011-04-14 16:08:02 -0700411 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700412 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
413 // by a broadcast receiver, and in order for it to work correctly, we need to know that
414 // the AppWidgetService has already received and processed the same broadcast. Since there
415 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
416 // we should have a more precise way of ensuring the AppWidgetService is up to date.
417 postDelayed(new Runnable() {
418 public void run() {
419 updatePackages();
420 }
421 }, 500);
422 }
423
424 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700425 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700426 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700427 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700428 List<AppWidgetProviderInfo> widgets =
429 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700430 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
431 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700432 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700433 mWidgets.addAll(shortcuts);
434 Collections.sort(mWidgets,
435 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700436
Winson Chung875de7e2011-06-28 14:25:17 -0700437 if (wasEmpty) {
438 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
439 // a layout to do this test and invalidate the page data when ready.
440 if (testDataReady()) requestLayout();
441 } else {
442 invalidatePageData();
443 }
Winson Chung4b576be2011-04-27 17:40:20 -0700444 }
445
446 @Override
447 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700448 // When we have exited all apps or are in transition, disregard clicks
449 if (!mLauncher.isAllAppsCustomizeOpen() ||
450 mLauncher.getWorkspace().isSwitchingState()) return;
451
Winson Chung4b576be2011-04-27 17:40:20 -0700452 if (v instanceof PagedViewIcon) {
453 // Animate some feedback to the click
454 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
455 animateClickFeedback(v, new Runnable() {
456 @Override
457 public void run() {
458 mLauncher.startActivitySafely(appInfo.intent, appInfo);
459 }
460 });
461 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700462 // Let the user know that they have to long press to add a widget
463 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
464 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700465
Winson Chungd2e87b32011-06-02 10:53:07 -0700466 // Create a little animation to show that the widget can move
467 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
468 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
469 AnimatorSet bounce = new AnimatorSet();
470 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
471 tyuAnim.setDuration(125);
472 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
473 tydAnim.setDuration(100);
474 bounce.play(tyuAnim).before(tydAnim);
475 bounce.setInterpolator(new AccelerateInterpolator());
476 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700477 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700478 }
479
480 /*
481 * PagedViewWithDraggableItems implementation
482 */
483 @Override
484 protected void determineDraggingStart(android.view.MotionEvent ev) {
485 // Disable dragging by pulling an app down for now.
486 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700487
Winson Chung4b576be2011-04-27 17:40:20 -0700488 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700489 mLauncher.getWorkspace().onDragStartedWithItem(v);
490 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700491 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700492
Winson Chung4b576be2011-04-27 17:40:20 -0700493 private void beginDraggingWidget(View v) {
494 // Get the widget preview as the drag representation
495 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700496 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700497
498 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700499 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700500 Drawable preview = image.getDrawable();
501 int w = preview.getIntrinsicWidth();
502 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700503 if (createItemInfo instanceof PendingAddWidgetInfo) {
504 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700505 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700506 createItemInfo.spanX = spanXY[0];
507 createItemInfo.spanY = spanXY[1];
508
509 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung70fc4382011-08-08 15:31:33 -0700510 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700511 } else {
512 // Workaround for the fact that we don't keep the original ResolveInfo associated with
513 // the shortcut around. To get the icon, we just render the preview image (which has
514 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
515 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
516 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700517 mCanvas.setBitmap(b);
518 mCanvas.save();
519 preview.draw(mCanvas);
520 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700521 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700522 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700523 createItemInfo.spanX = createItemInfo.spanY = 1;
524 }
Winson Chung4b576be2011-04-27 17:40:20 -0700525
526 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700527 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700528 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
529 createItemInfo.spanY, b);
530 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700531 DragController.DRAG_ACTION_COPY, null);
532 b.recycle();
533 }
534 @Override
535 protected boolean beginDragging(View v) {
536 if (!super.beginDragging(v)) return false;
537
Winson Chungc07918d2011-07-01 15:35:26 -0700538 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700539 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700540
Winson Chung4b576be2011-04-27 17:40:20 -0700541 if (v instanceof PagedViewIcon) {
542 beginDraggingApplication(v);
543 } else if (v instanceof PagedViewWidget) {
544 beginDraggingWidget(v);
545 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 return true;
547 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700548 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700550 if (!success || (target != mLauncher.getWorkspace() &&
551 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700552 // Exit spring loaded mode if we have not successfully dropped or have not handled the
553 // drop in Workspace
554 mLauncher.exitSpringLoadedDragMode();
555 }
Adam Cohen446e9402011-09-15 18:21:21 -0700556 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700557
Winson Chung785d2eb2011-04-14 16:08:02 -0700558 }
559
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700561 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700562 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700563
564 // Display an error message if the drag failed due to there not being enough space on the
565 // target layout we were dropping on.
566 if (!success) {
567 boolean showOutOfSpaceMessage = false;
568 if (target instanceof Workspace) {
569 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
570 Workspace workspace = (Workspace) target;
571 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700572 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700573 if (layout != null) {
574 layout.calculateSpans(itemInfo);
575 showOutOfSpaceMessage =
576 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
577 }
578 }
579 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
580 if (showOutOfSpaceMessage) {
581 mLauncher.showOutOfSpaceMessage();
582 }
583 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700584 }
585
586 public void setContentType(ContentType type) {
587 mContentType = type;
Winson Chung87acb482011-08-23 17:28:05 -0700588 invalidatePageData(0, (type != ContentType.Applications));
Winson Chung785d2eb2011-04-14 16:08:02 -0700589 }
590
Winson Chungb44b5242011-06-13 11:32:14 -0700591 public boolean isContentType(ContentType type) {
592 return (mContentType == type);
593 }
594
Winson Chung5a808352011-06-27 19:08:49 -0700595 public void setCurrentPageToWidgets() {
596 invalidatePageData(0);
597 }
Winson Chung5a808352011-06-27 19:08:49 -0700598
Winson Chung785d2eb2011-04-14 16:08:02 -0700599 /*
600 * Apps PagedView implementation
601 */
Winson Chung63257c12011-05-05 17:06:13 -0700602 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
603 int childCount = layout.getChildCount();
604 for (int i = 0; i < childCount; ++i) {
605 layout.getChildAt(i).setVisibility(visibility);
606 }
607 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700608 private void setupPage(PagedViewCellLayout layout) {
609 layout.setCellCount(mCellCountX, mCellCountY);
610 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
611 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
612 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
613
Winson Chung63257c12011-05-05 17:06:13 -0700614 // Note: We force a measure here to get around the fact that when we do layout calculations
615 // immediately after syncing, we don't have a proper width. That said, we already know the
616 // expected page width, so we can actually optimize by hiding all the TextView-based
617 // children that are expensive to measure, and let that happen naturally later.
618 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700619 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700620 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700621 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700622 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700623 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700624 }
625 public void syncAppsPages() {
626 // Ensure that we have the right number of pages
627 Context context = getContext();
628 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
629 for (int i = 0; i < numPages; ++i) {
630 PagedViewCellLayout layout = new PagedViewCellLayout(context);
631 setupPage(layout);
632 addView(layout);
633 }
634 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700635 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700636 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700637 int numCells = mCellCountX * mCellCountY;
638 int startIndex = page * numCells;
639 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700640 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700641
Winson Chung785d2eb2011-04-14 16:08:02 -0700642 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700643 ArrayList<Object> items = new ArrayList<Object>();
644 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700645 for (int i = startIndex; i < endIndex; ++i) {
646 ApplicationInfo info = mApps.get(i);
647 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
648 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700649 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700650 icon.setOnClickListener(this);
651 icon.setOnLongClickListener(this);
652 icon.setOnTouchListener(this);
653
654 int index = i - startIndex;
655 int x = index % mCellCountX;
656 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700657 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700658
659 items.add(info);
660 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700661 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700662
Winson Chungf0ea4d32011-06-06 14:27:16 -0700663 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700664
Winson Chung54c725c2011-08-03 12:03:40 -0700665 if (mFadeInAdjacentScreens) {
666 prepareGenerateHoloOutlinesTask(page, items, images);
667 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700668 }
Winson Chungb44b5242011-06-13 11:32:14 -0700669
670 /**
671 * Return the appropriate thread priority for loading for a given page (we give the current
672 * page much higher priority)
673 */
674 private int getThreadPriorityForPage(int page) {
675 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
676 int pageDiff = Math.abs(page - mCurrentPage);
677 if (pageDiff <= 0) {
678 // return Process.THREAD_PRIORITY_DEFAULT;
679 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
680 } else if (pageDiff <= 1) {
681 // return Process.THREAD_PRIORITY_BACKGROUND;
682 return Process.THREAD_PRIORITY_DEFAULT;
683 } else {
684 // return Process.THREAD_PRIORITY_LOWEST;
685 return Process.THREAD_PRIORITY_DEFAULT;
686 }
687 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700688 private int getSleepForPage(int page) {
689 int pageDiff = Math.abs(page - mCurrentPage) - 1;
690 return Math.max(0, pageDiff * sPageSleepDelay);
691 }
Winson Chungb44b5242011-06-13 11:32:14 -0700692 /**
693 * Creates and executes a new AsyncTask to load a page of widget previews.
694 */
695 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700696 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700697 // Prune all tasks that are no longer needed
698 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
699 while (iter.hasNext()) {
700 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
701 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700702 if ((taskPage == page) ||
703 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700704 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700705 task.cancel(false);
706 iter.remove();
707 } else {
708 task.setThreadPriority(getThreadPriorityForPage(taskPage));
709 }
710 }
711
Winson Chungf314b0e2011-08-16 11:54:27 -0700712 // We introduce a slight delay to order the loading of side pages so that we don't thrash
713 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700714 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700715 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700716 @Override
717 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700718 try {
719 Thread.sleep(sleepMs);
720 } catch (Exception e) {}
721 loadWidgetPreviewsInBackground(task, data);
Winson Chungb44b5242011-06-13 11:32:14 -0700722 }
723 },
724 new AsyncTaskCallback() {
725 @Override
726 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
727 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700728 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700729 if (task.page > getPageCount()) return;
730 if (task.pageContentType != mContentType) return;
731 onSyncWidgetPageItems(data);
732 }
733 });
734
735 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700736 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
737 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700738 t.setThreadPriority(getThreadPriorityForPage(page));
739 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
740 mRunningTasks.add(t);
741 }
742 /**
743 * Creates and executes a new AsyncTask to load the outlines for a page of content.
744 */
745 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
746 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700747 // Prune old tasks for this page
748 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
749 while (iter.hasNext()) {
750 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
751 int taskPage = task.page;
752 if ((taskPage == page) &&
753 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
754 task.cancel(false);
755 iter.remove();
756 }
757 }
758
Winson Chungb44b5242011-06-13 11:32:14 -0700759 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
760 new AsyncTaskCallback() {
761 @Override
762 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
763 // Ensure that this task starts running at the correct priority
764 task.syncThreadPriority();
765
Winson Chung4e076542011-06-23 13:04:10 -0700766 ArrayList<Bitmap> images = data.generatedImages;
767 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700768 int count = srcImages.size();
769 Canvas c = new Canvas();
770 for (int i = 0; i < count && !task.isCancelled(); ++i) {
771 // Before work on each item, ensure that this task is running at the correct
772 // priority
773 task.syncThreadPriority();
774
775 Bitmap b = srcImages.get(i);
776 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
777 Bitmap.Config.ARGB_8888);
778
779 c.setBitmap(outline);
780 c.save();
781 c.drawBitmap(b, 0, 0, null);
782 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700783 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700784
785 images.add(outline);
786 }
787 }
788 },
789 new AsyncTaskCallback() {
790 @Override
791 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
792 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700793 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700794 if (task.page > getPageCount()) return;
795 if (task.pageContentType != mContentType) return;
796 onHolographicPageItemsLoaded(data);
797 }
798 });
799
800 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700801 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700802 new AppsCustomizeAsyncTask(page, mContentType,
803 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700804 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
805 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
806 mRunningTasks.add(t);
807 }
808
Winson Chung785d2eb2011-04-14 16:08:02 -0700809 /*
810 * Widgets PagedView implementation
811 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700812 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
814 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700815
816 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700817 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700818 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
819 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700820 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700821 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700823 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700825 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
826 }
827 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
828 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700829 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700830 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700831 c.scale(scaleX, scaleY);
832 Rect oldBounds = d.copyBounds();
833 d.setBounds(x, y, x + w, y + h);
834 d.draw(c);
835 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700836 if (multiplyColor != 0xFFFFFFFF) {
837 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
838 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700839 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700840 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700841 }
Winson Chungd2945262011-06-24 15:22:14 -0700842 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700843 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700844 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700845 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700846 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700847 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700848 }
Winson Chung4e076542011-06-23 13:04:10 -0700849 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700850 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700851
Winson Chung4b576be2011-04-27 17:40:20 -0700852 // Calculate the size of the drawable
853 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
854 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
855 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
856 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
857
858 // Scale down the bitmap to fit the space
859 float widgetPreviewScale = (float) cellWidth / expectedWidth;
860 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
861 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
862
863 // Load the preview image if possible
864 String packageName = info.provider.getPackageName();
865 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700866 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700867 if (info.previewImage != 0) {
868 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
869 if (drawable == null) {
870 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
871 + " for provider: " + info.provider);
872 } else {
873 // Scale down the preview to the dimensions we want
874 int imageWidth = drawable.getIntrinsicWidth();
875 int imageHeight = drawable.getIntrinsicHeight();
876 float aspect = (float) imageWidth / imageHeight;
877 int newWidth = imageWidth;
878 int newHeight = imageHeight;
879 if (aspect > 1f) {
880 newWidth = expectedWidth;
881 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
882 } else {
883 newHeight = expectedHeight;
884 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
885 }
886
Winson Chungb44b5242011-06-13 11:32:14 -0700887 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700888 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700889 }
890 }
891
892 // Generate a preview image if we couldn't load one
893 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700894 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700895 int bitmapWidth;
896 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700897
Winson Chung273c1022011-07-11 13:40:52 -0700898 // Specify the dimensions of the bitmap (since we are using a default preview bg with
899 // the full icon, we only imply the aspect ratio of the widget)
900 if (cellHSpan == cellVSpan) {
901 bitmapWidth = bitmapHeight = cellWidth;
902 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
903 } else if (cellHSpan >= cellVSpan) {
904 bitmapWidth = expectedWidth = cellWidth;
905 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700906 } else {
907 // Note that in vertical widgets, we might not have enough space due to the text
908 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700909 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
910 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700911 }
Winson Chung4b576be2011-04-27 17:40:20 -0700912
Winson Chung273c1022011-07-11 13:40:52 -0700913 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700914 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700915 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700916
917 // Draw the icon in the top left corner
918 try {
919 Drawable icon = null;
920 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
921 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
922
Winson Chungd2945262011-06-24 15:22:14 -0700923 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
924 renderDrawableToBitmap(icon, preview, offset, offset,
925 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700926 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700927 }
Winson Chungb44b5242011-06-13 11:32:14 -0700928 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700929 }
930 public void syncWidgetPages() {
931 // Ensure that we have the right number of pages
932 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700933 int numPages = (int) Math.ceil(mWidgets.size() /
934 (float) (mWidgetCountX * mWidgetCountY));
935 for (int j = 0; j < numPages; ++j) {
936 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
937 mWidgetCountY);
938 setupPage(layout);
939 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700940 }
941 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700942 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700943 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700944 int contentWidth = mWidgetSpacingLayout.getContentWidth();
945 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700946
Winson Chungd2945262011-06-24 15:22:14 -0700947 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700948 ArrayList<Object> items = new ArrayList<Object>();
949 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700950 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700951 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700952 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700953
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700954 int offset = page * numItemsPerPage;
955 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
956 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700957 }
958
Winson Chungf314b0e2011-08-16 11:54:27 -0700959 if (immediate) {
960 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
961 mWidgetCountX, null, null);
962 loadWidgetPreviewsInBackground(null, data);
963 onSyncWidgetPageItems(data);
964 } else {
965 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
966 }
Adam Cohen22f823d2011-09-01 17:22:18 -0700967 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
968 layout.createHardwareLayer();
Winson Chungf314b0e2011-08-16 11:54:27 -0700969 }
970 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
971 AsyncTaskPageData data) {
972 if (task != null) {
973 // Ensure that this task starts running at the correct priority
974 task.syncThreadPriority();
975 }
976
977 // Load each of the widget/shortcut previews
978 ArrayList<Object> items = data.items;
979 ArrayList<Bitmap> images = data.generatedImages;
980 int count = items.size();
981 int cellWidth = data.cellWidth;
982 int cellHeight = data.cellHeight;
983 for (int i = 0; i < count; ++i) {
984 if (task != null) {
985 // Ensure we haven't been cancelled yet
986 if (task.isCancelled()) break;
987 // Before work on each item, ensure that this task is running at the correct
988 // priority
989 task.syncThreadPriority();
990 }
991
992 Object rawInfo = items.get(i);
993 if (rawInfo instanceof AppWidgetProviderInfo) {
994 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700995 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -0700996 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
997 cellWidth, cellHeight));
998 } else if (rawInfo instanceof ResolveInfo) {
999 // Fill in the shortcuts information
1000 ResolveInfo info = (ResolveInfo) rawInfo;
1001 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1002 }
1003 }
Winson Chungb44b5242011-06-13 11:32:14 -07001004 }
1005 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1006 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001007 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -07001008 // Only set the column count once we have items
1009 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -07001010
Winson Chungb44b5242011-06-13 11:32:14 -07001011 ArrayList<Object> items = data.items;
1012 int count = items.size();
1013 int cellWidth = data.cellWidth;
1014 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -07001015 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -07001016 for (int i = 0; i < count; ++i) {
1017 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001018 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -07001019 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1020 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -07001021 if (rawInfo instanceof AppWidgetProviderInfo) {
1022 // Fill in the widget information
1023 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen61dcf372011-09-13 15:01:58 -07001025 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chung4e076542011-06-23 13:04:10 -07001026 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -07001027 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -07001028 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001029 widget.setTag(createItemInfo);
1030 } else if (rawInfo instanceof ResolveInfo) {
1031 // Fill in the shortcuts information
1032 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 createItemInfo = new PendingAddItemInfo();
Winson Chung1ed747a2011-05-03 16:18:34 -07001034 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1035 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1036 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -07001037 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001038 widget.applyFromResolveInfo(mPackageManager, info, preview,
1039 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001040 widget.setTag(createItemInfo);
1041 }
Winson Chung4b576be2011-04-27 17:40:20 -07001042 widget.setOnClickListener(this);
1043 widget.setOnLongClickListener(this);
1044 widget.setOnTouchListener(this);
1045
1046 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -07001047 int ix = i % cellCountX;
1048 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -07001049 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -07001050 GridLayout.spec(iy, GridLayout.LEFT),
1051 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -07001052 lp.width = cellWidth;
1053 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -07001054 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -07001055 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1056 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -07001057 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -07001058 }
Winson Chungb44b5242011-06-13 11:32:14 -07001059
1060 invalidate();
1061 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001062
1063 if (mFadeInAdjacentScreens) {
1064 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1065 }
Winson Chungb44b5242011-06-13 11:32:14 -07001066 }
1067 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1068 // Invalidate early to short-circuit children invalidates
1069 invalidate();
1070
1071 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001072 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001073 if (layout instanceof PagedViewCellLayout) {
1074 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1075 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001076 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001077 for (int i = 0; i < count; ++i) {
1078 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001079 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001080 }
1081 } else {
1082 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001083 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001084 for (int i = 0; i < count; ++i) {
1085 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001086 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001087 }
1088 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001089 }
Winson Chung46af2e82011-05-09 16:00:53 -07001090
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 @Override
1092 public void syncPages() {
1093 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001094
1095 // Remove all background asyc tasks if we are loading content anew
1096 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1097 while (iter.hasNext()) {
1098 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1099 task.cancel(false);
1100 iter.remove();
1101 }
1102
Winson Chung785d2eb2011-04-14 16:08:02 -07001103 switch (mContentType) {
1104 case Applications:
1105 syncAppsPages();
1106 break;
1107 case Widgets:
1108 syncWidgetPages();
1109 break;
1110 }
1111 }
1112 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001113 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 switch (mContentType) {
1115 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001116 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 break;
1118 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001119 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001120 break;
1121 }
1122 }
1123
Adam Cohen22f823d2011-09-01 17:22:18 -07001124 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1125 // it is in the z-order. This is important to insure touch events are handled correctly.
1126 View getPageAt(int index) {
1127 return getChildAt(getChildCount() - index - 1);
1128 }
1129
1130 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1131 @Override
1132 protected void screenScrolled(int screenCenter) {
1133 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001134
1135 for (int i = 0; i < getChildCount(); i++) {
1136 View v = getPageAt(i);
1137 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001138 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001139
1140 float interpolatedProgress =
1141 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1142 float scale = (1 - interpolatedProgress) +
1143 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1144 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001145
1146 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1147 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001148
1149 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1150 int pageWidth = v.getMeasuredWidth();
1151 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001152
1153 if (PERFORM_OVERSCROLL_ROTATION) {
1154 if (i == 0 && scrollProgress < 0) {
1155 // Overscroll to the left
1156 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1157 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1158 scale = 1.0f;
1159 alpha = 1.0f;
1160 // On the first page, we don't want the page to have any lateral motion
1161 translationX = getScrollX();
1162 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1163 // Overscroll to the right
1164 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1165 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1166 scale = 1.0f;
1167 alpha = 1.0f;
1168 // On the last page, we don't want the page to have any lateral motion.
1169 translationX = getScrollX() - mMaxScrollX;
1170 } else {
1171 v.setPivotY(pageHeight / 2.0f);
1172 v.setPivotX(pageWidth / 2.0f);
1173 v.setRotationY(0f);
1174 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001175 }
1176
1177 v.setTranslationX(translationX);
1178 v.setScaleX(scale);
1179 v.setScaleY(scale);
1180 v.setAlpha(alpha);
1181 }
1182 }
1183 }
1184
1185 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001186 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001187 }
1188
Winson Chung785d2eb2011-04-14 16:08:02 -07001189 /**
1190 * Used by the parent to get the content width to set the tab bar to
1191 * @return
1192 */
1193 public int getPageContentWidth() {
1194 return mContentWidth;
1195 }
1196
Winson Chungb26f3d62011-06-02 10:49:29 -07001197 @Override
1198 protected void onPageBeginMoving() {
1199 /* TO BE ENABLED LATER
1200 setChildrenDrawnWithCacheEnabled(true);
1201 for (int i = 0; i < getChildCount(); ++i) {
1202 View v = getChildAt(i);
1203 if (v instanceof PagedViewCellLayout) {
1204 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1205 }
1206 }
1207 */
1208 super.onPageBeginMoving();
1209 }
1210
1211 @Override
1212 protected void onPageEndMoving() {
1213 /* TO BE ENABLED LATER
1214 for (int i = 0; i < getChildCount(); ++i) {
1215 View v = getChildAt(i);
1216 if (v instanceof PagedViewCellLayout) {
1217 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1218 }
1219 }
1220 setChildrenDrawnWithCacheEnabled(false);
1221 */
1222 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001223
1224 // We reset the save index when we change pages so that it will be recalculated on next
1225 // rotation
1226 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001227 }
1228
Winson Chung785d2eb2011-04-14 16:08:02 -07001229 /*
1230 * AllAppsView implementation
1231 */
1232 @Override
1233 public void setup(Launcher launcher, DragController dragController) {
1234 mLauncher = launcher;
1235 mDragController = dragController;
1236 }
1237 @Override
1238 public void zoom(float zoom, boolean animate) {
1239 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1240 }
1241 @Override
1242 public boolean isVisible() {
1243 return (getVisibility() == VISIBLE);
1244 }
1245 @Override
1246 public boolean isAnimating() {
1247 return false;
1248 }
1249 @Override
1250 public void setApps(ArrayList<ApplicationInfo> list) {
1251 mApps = list;
1252 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001253
Winson Chung875de7e2011-06-28 14:25:17 -07001254 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1255 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001256 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 }
1258 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1259 // We add it in place, in alphabetical order
1260 int count = list.size();
1261 for (int i = 0; i < count; ++i) {
1262 ApplicationInfo info = list.get(i);
1263 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1264 if (index < 0) {
1265 mApps.add(-(index + 1), info);
1266 }
1267 }
1268 }
1269 @Override
1270 public void addApps(ArrayList<ApplicationInfo> list) {
1271 addAppsWithoutInvalidate(list);
1272 invalidatePageData();
1273 }
1274 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1275 ComponentName removeComponent = item.intent.getComponent();
1276 int length = list.size();
1277 for (int i = 0; i < length; ++i) {
1278 ApplicationInfo info = list.get(i);
1279 if (info.intent.getComponent().equals(removeComponent)) {
1280 return i;
1281 }
1282 }
1283 return -1;
1284 }
1285 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1286 // loop through all the apps and remove apps that have the same component
1287 int length = list.size();
1288 for (int i = 0; i < length; ++i) {
1289 ApplicationInfo info = list.get(i);
1290 int removeIndex = findAppByComponent(mApps, info);
1291 if (removeIndex > -1) {
1292 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001293 }
1294 }
1295 }
1296 @Override
1297 public void removeApps(ArrayList<ApplicationInfo> list) {
1298 removeAppsWithoutInvalidate(list);
1299 invalidatePageData();
1300 }
1301 @Override
1302 public void updateApps(ArrayList<ApplicationInfo> list) {
1303 // We remove and re-add the updated applications list because it's properties may have
1304 // changed (ie. the title), and this will ensure that the items will be in their proper
1305 // place in the list.
1306 removeAppsWithoutInvalidate(list);
1307 addAppsWithoutInvalidate(list);
1308 invalidatePageData();
1309 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001310
Winson Chung785d2eb2011-04-14 16:08:02 -07001311 @Override
1312 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001313 if (mContentType != ContentType.Applications) {
1314 // Reset to the first page of the Apps pane
1315 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1316 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001317 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001318 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001319 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001320 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 }
1322 @Override
1323 public void dumpState() {
1324 // TODO: Dump information related to current list of Applications, Widgets, etc.
1325 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1326 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1327 }
1328 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001329 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001331 for (Object i: list) {
1332 if (i instanceof AppWidgetProviderInfo) {
1333 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1334 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1335 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1336 + " initialLayout=" + info.initialLayout
1337 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1338 } else if (i instanceof ResolveInfo) {
1339 ResolveInfo info = (ResolveInfo) i;
1340 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1341 + info.icon);
1342 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 }
1344 }
1345 @Override
1346 public void surrender() {
1347 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1348 // should stop this now.
1349 }
Winson Chung007c6982011-06-14 13:27:53 -07001350
Winson Chungb44b5242011-06-13 11:32:14 -07001351 /*
1352 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1353 * widget previews in the background with the AsyncTasks.
1354 */
1355 protected int getAssociatedLowerPageBound(int page) {
1356 return Math.max(0, page - 2);
1357 }
1358 protected int getAssociatedUpperPageBound(int page) {
1359 final int count = getChildCount();
1360 return Math.min(page + 2, count - 1);
1361 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001362
1363 @Override
1364 protected String getCurrentPageDescription() {
1365 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1366 int stringId = R.string.default_scroll_format;
1367 switch (mContentType) {
1368 case Applications:
1369 stringId = R.string.apps_customize_apps_scroll_format;
1370 break;
1371 case Widgets:
1372 stringId = R.string.apps_customize_widgets_scroll_format;
1373 break;
1374 }
1375 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1376 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001377}