blob: ca4d614483860bb03938766f084c5a516e751e7e [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 }
Winson Chung09945932011-09-20 14:22:40 -070099 void cleanup(boolean cancelled) {
100 // Clean up any references to source/generated bitmaps
101 if (sourceImages != null) {
102 if (cancelled) {
103 for (Bitmap b : sourceImages) {
104 b.recycle();
105 }
106 }
107 sourceImages.clear();
108 }
109 if (generatedImages != null) {
110 if (cancelled) {
111 for (Bitmap b : generatedImages) {
112 b.recycle();
113 }
114 }
115 generatedImages.clear();
116 }
117 }
Winson Chungb44b5242011-06-13 11:32:14 -0700118 int page;
119 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700120 ArrayList<Bitmap> sourceImages;
121 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700122 int cellWidth;
123 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700124 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700125 AsyncTaskCallback doInBackgroundCallback;
126 AsyncTaskCallback postExecuteCallback;
127}
Winson Chung4e076542011-06-23 13:04:10 -0700128
Winson Chungb44b5242011-06-13 11:32:14 -0700129/**
130 * A generic template for an async task used in AppsCustomize.
131 */
132class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700133 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700134 page = p;
135 pageContentType = t;
136 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700137 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 }
139 @Override
140 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
141 if (params.length != 1) return null;
142 // Load each of the widget previews in the background
143 params[0].doInBackgroundCallback.run(this, params[0]);
144 return params[0];
145 }
146 @Override
147 protected void onPostExecute(AsyncTaskPageData result) {
148 // All the widget previews are loaded, so we can just callback to inflate the page
149 result.postExecuteCallback.run(this, result);
150 }
151
152 void setThreadPriority(int p) {
153 threadPriority = p;
154 }
155 void syncThreadPriority() {
156 Process.setThreadPriority(threadPriority);
157 }
158
159 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700160 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700161 int page;
162 AppsCustomizePagedView.ContentType pageContentType;
163 int threadPriority;
164}
Winson Chungb44b5242011-06-13 11:32:14 -0700165
166/**
167 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
168 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700169public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
170 AllAppsView, View.OnClickListener, DragSource {
171 static final String LOG_TAG = "AppsCustomizePagedView";
172
173 /**
174 * The different content types that this paged view can show.
175 */
176 public enum ContentType {
177 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700178 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700179 }
180
181 // Refs
182 private Launcher mLauncher;
183 private DragController mDragController;
184 private final LayoutInflater mLayoutInflater;
185 private final PackageManager mPackageManager;
186
Winson Chung5afbf7b2011-07-25 11:53:08 -0700187 // Save and Restore
188 private int mSaveInstanceStateItemIndex = -1;
189 private int mRestorePage = -1;
190
Winson Chung785d2eb2011-04-14 16:08:02 -0700191 // Content
192 private ContentType mContentType;
193 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700194 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700195
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 // Cling
197 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 Chung4b576be2011-04-27 17:40:20 -0700209 private int mMaxWidgetSpan, mMinWidgetSpan;
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;
215
Adam Cohen22f823d2011-09-01 17:22:18 -0700216 // Relating to the scroll and overscroll effects
217 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700218 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700219 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700220 private static float TRANSITION_PIVOT = 0.65f;
221 private static float TRANSITION_MAX_ROTATION = 22;
222 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700223 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen22f823d2011-09-01 17:22:18 -0700224
Winson Chungb44b5242011-06-13 11:32:14 -0700225 // Previews & outlines
226 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
227 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chungf314b0e2011-08-16 11:54:27 -0700228 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700229
Winson Chung785d2eb2011-04-14 16:08:02 -0700230 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
231 super(context, attrs);
232 mLayoutInflater = LayoutInflater.from(context);
233 mPackageManager = context.getPackageManager();
234 mContentType = ContentType.Applications;
235 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700236 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700237 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700238 mHolographicOutlineHelper = new HolographicOutlineHelper();
239 mCanvas = new Canvas();
240 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700241
242 // Save the default widget preview background
243 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700244 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700245 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
246 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700247
248 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700250 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
251 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
252 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700253 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700254 mWidgetWidthGap =
255 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
256 mWidgetHeightGap =
257 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700258 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
259 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700260 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
261 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700262 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700263 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700264
265 // The max widget span is the length N, such that NxN is the largest bounds that the widget
266 // preview can be before applying the widget scaling
267 mMinWidgetSpan = 1;
268 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700269
270 // The padding on the non-matched dimension for the default widget preview icons
271 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700272 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700273 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700274 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700275 }
276
277 @Override
278 protected void init() {
279 super.init();
280 mCenterPagesVertically = false;
281
282 Context context = getContext();
283 Resources r = context.getResources();
284 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
285 }
286
Winson Chungf0ea4d32011-06-06 14:27:16 -0700287 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700288 protected void onUnhandledTap(MotionEvent ev) {
289 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700290 // Dismiss AppsCustomize if we tap
291 mLauncher.showWorkspace(true);
292 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700293 }
294
Winson Chung5afbf7b2011-07-25 11:53:08 -0700295 /** Returns the item index of the center item on this page so that we can restore to this
296 * item index when we rotate. */
297 private int getMiddleComponentIndexOnCurrentPage() {
298 int i = -1;
299 if (getPageCount() > 0) {
300 int currentPage = getCurrentPage();
301 switch (mContentType) {
302 case Applications: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700303 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700304 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
305 int numItemsPerPage = mCellCountX * mCellCountY;
306 int childCount = childrenLayout.getChildCount();
307 if (childCount > 0) {
308 i = (currentPage * numItemsPerPage) + (childCount / 2);
309 }}
310 break;
311 case Widgets: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700312 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700313 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
314 int childCount = layout.getChildCount();
315 if (childCount > 0) {
316 i = (currentPage * numItemsPerPage) + (childCount / 2);
317 }}
318 break;
319 }
320 }
321 return i;
322 }
323
324 /** Get the index of the item to restore to if we need to restore the current page. */
325 int getSaveInstanceStateIndex() {
326 if (mSaveInstanceStateItemIndex == -1) {
327 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
328 }
329 return mSaveInstanceStateItemIndex;
330 }
331
332 /** Returns the page in the current orientation which is expected to contain the specified
333 * item index. */
334 int getPageForComponent(int index) {
335 switch (mContentType) {
336 case Applications: {
337 int numItemsPerPage = mCellCountX * mCellCountY;
338 return (index / numItemsPerPage);
339 }
340 case Widgets: {
341 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
342 return (index / numItemsPerPage);
343 }}
344 return -1;
345 }
346
Winson Chungf0ea4d32011-06-06 14:27:16 -0700347 /**
348 * This differs from isDataReady as this is the test done if isDataReady is not set.
349 */
350 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700351 // We only do this test once, and we default to the Applications page, so we only really
352 // have to wait for there to be apps.
Winson Chung87acb482011-08-23 17:28:05 -0700353 if (mContentType == AppsCustomizePagedView.ContentType.Widgets) {
354 return !mApps.isEmpty() && !mWidgets.isEmpty();
355 } else {
356 return !mApps.isEmpty();
357 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700358 }
359
Winson Chung5afbf7b2011-07-25 11:53:08 -0700360 /** Restores the page for an item at the specified index */
361 void restorePageForIndex(int index) {
362 if (index < 0) return;
363
364 int page = getPageForComponent(index);
365 if (page > -1) {
366 if (getChildCount() > 0) {
367 invalidatePageData(page);
368 } else {
369 mRestorePage = page;
370 }
371 }
372 }
373
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 protected void onDataReady(int width, int height) {
375 // Note that we transpose the counts in portrait so that we get a similar layout
376 boolean isLandscape = getResources().getConfiguration().orientation ==
377 Configuration.ORIENTATION_LANDSCAPE;
378 int maxCellCountX = Integer.MAX_VALUE;
379 int maxCellCountY = Integer.MAX_VALUE;
380 if (LauncherApplication.isScreenLarge()) {
381 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
382 LauncherModel.getCellCountY());
383 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
384 LauncherModel.getCellCountX());
385 }
386
387 // Now that the data is ready, we can calculate the content width, the number of cells to
388 // use for each page
389 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
390 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
391 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
392 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
393 mCellCountX = mWidgetSpacingLayout.getCellCountX();
394 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung5a808352011-06-27 19:08:49 -0700395
Winson Chungdb1138b2011-06-30 14:39:35 -0700396 // Force a measure to update recalculate the gaps
397 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
398 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
399 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700400 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700401 invalidatePageData(Math.max(0, mRestorePage));
402 mRestorePage = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700403
404 int[] offset = new int[2];
405 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
406 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
407 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + offset[0];
408 pos[1] += (getMeasuredHeight() - mWidgetSpacingLayout.getMeasuredHeight()) / 2 + offset[1];
409 mLauncher.showFirstRunAllAppsCling(pos);
410
Winson Chungf0ea4d32011-06-06 14:27:16 -0700411 }
412
413 @Override
414 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
415 int width = MeasureSpec.getSize(widthMeasureSpec);
416 int height = MeasureSpec.getSize(heightMeasureSpec);
417 if (!isDataReady()) {
418 if (testDataReady()) {
419 setDataIsReady();
420 setMeasuredDimension(width, height);
421 onDataReady(width, height);
422 }
423 }
424
425 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
426 }
427
Winson Chung34efdaf2011-05-24 14:19:56 -0700428 /** Removes and returns the ResolveInfo with the specified ComponentName */
429 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
430 ComponentName cn) {
431 Iterator<ResolveInfo> iter = list.iterator();
432 while (iter.hasNext()) {
433 ResolveInfo rinfo = iter.next();
434 ActivityInfo info = rinfo.activityInfo;
435 ComponentName c = new ComponentName(info.packageName, info.name);
436 if (c.equals(cn)) {
437 iter.remove();
438 return rinfo;
439 }
440 }
441 return null;
442 }
443
Winson Chung785d2eb2011-04-14 16:08:02 -0700444 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700445 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
446 // by a broadcast receiver, and in order for it to work correctly, we need to know that
447 // the AppWidgetService has already received and processed the same broadcast. Since there
448 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
449 // we should have a more precise way of ensuring the AppWidgetService is up to date.
450 postDelayed(new Runnable() {
451 public void run() {
452 updatePackages();
453 }
454 }, 500);
455 }
456
457 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700458 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700459 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700460 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 List<AppWidgetProviderInfo> widgets =
462 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700463 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
464 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700465 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700466 mWidgets.addAll(shortcuts);
467 Collections.sort(mWidgets,
468 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700469
Winson Chung875de7e2011-06-28 14:25:17 -0700470 if (wasEmpty) {
471 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
472 // a layout to do this test and invalidate the page data when ready.
473 if (testDataReady()) requestLayout();
474 } else {
475 invalidatePageData();
476 }
Winson Chung4b576be2011-04-27 17:40:20 -0700477 }
478
479 @Override
480 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700481 // When we have exited all apps or are in transition, disregard clicks
482 if (!mLauncher.isAllAppsCustomizeOpen() ||
483 mLauncher.getWorkspace().isSwitchingState()) return;
484
Winson Chung4b576be2011-04-27 17:40:20 -0700485 if (v instanceof PagedViewIcon) {
486 // Animate some feedback to the click
487 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
488 animateClickFeedback(v, new Runnable() {
489 @Override
490 public void run() {
491 mLauncher.startActivitySafely(appInfo.intent, appInfo);
492 }
493 });
494 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700495 // Let the user know that they have to long press to add a widget
496 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
497 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700498
Winson Chungd2e87b32011-06-02 10:53:07 -0700499 // Create a little animation to show that the widget can move
500 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
501 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
502 AnimatorSet bounce = new AnimatorSet();
503 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
504 tyuAnim.setDuration(125);
505 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
506 tydAnim.setDuration(100);
507 bounce.play(tyuAnim).before(tydAnim);
508 bounce.setInterpolator(new AccelerateInterpolator());
509 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700510 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700511 }
512
513 /*
514 * PagedViewWithDraggableItems implementation
515 */
516 @Override
517 protected void determineDraggingStart(android.view.MotionEvent ev) {
518 // Disable dragging by pulling an app down for now.
519 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700520
Winson Chung4b576be2011-04-27 17:40:20 -0700521 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700522 mLauncher.getWorkspace().onDragStartedWithItem(v);
523 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700524 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700525
Winson Chung4b576be2011-04-27 17:40:20 -0700526 private void beginDraggingWidget(View v) {
527 // Get the widget preview as the drag representation
528 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700529 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700530
531 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700532 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700533 Drawable preview = image.getDrawable();
534 int w = preview.getIntrinsicWidth();
535 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700536 if (createItemInfo instanceof PendingAddWidgetInfo) {
537 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700538 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700539 createItemInfo.spanX = spanXY[0];
540 createItemInfo.spanY = spanXY[1];
541
542 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung70fc4382011-08-08 15:31:33 -0700543 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700544 } else {
545 // Workaround for the fact that we don't keep the original ResolveInfo associated with
546 // the shortcut around. To get the icon, we just render the preview image (which has
547 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
548 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
549 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700550 mCanvas.setBitmap(b);
551 mCanvas.save();
552 preview.draw(mCanvas);
553 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700554 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700555 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700556 createItemInfo.spanX = createItemInfo.spanY = 1;
557 }
Winson Chung4b576be2011-04-27 17:40:20 -0700558
559 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700560 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700561 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
562 createItemInfo.spanY, b);
563 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700564 DragController.DRAG_ACTION_COPY, null);
565 b.recycle();
566 }
567 @Override
568 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700569 // Dismiss the cling
570 mLauncher.dismissAllAppsCling(null);
571
Winson Chung4b576be2011-04-27 17:40:20 -0700572 if (!super.beginDragging(v)) return false;
573
Winson Chungc07918d2011-07-01 15:35:26 -0700574 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700575 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700576
Winson Chung4b576be2011-04-27 17:40:20 -0700577 if (v instanceof PagedViewIcon) {
578 beginDraggingApplication(v);
579 } else if (v instanceof PagedViewWidget) {
580 beginDraggingWidget(v);
581 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700582 return true;
583 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700584 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700585 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700586 if (!success || (target != mLauncher.getWorkspace() &&
587 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700588 // Exit spring loaded mode if we have not successfully dropped or have not handled the
589 // drop in Workspace
590 mLauncher.exitSpringLoadedDragMode();
591 }
Adam Cohen446e9402011-09-15 18:21:21 -0700592 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700593
Winson Chung785d2eb2011-04-14 16:08:02 -0700594 }
595
Winson Chung785d2eb2011-04-14 16:08:02 -0700596 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700597 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700598 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700599
600 // Display an error message if the drag failed due to there not being enough space on the
601 // target layout we were dropping on.
602 if (!success) {
603 boolean showOutOfSpaceMessage = false;
604 if (target instanceof Workspace) {
605 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
606 Workspace workspace = (Workspace) target;
607 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700608 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700609 if (layout != null) {
610 layout.calculateSpans(itemInfo);
611 showOutOfSpaceMessage =
612 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
613 }
614 }
615 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
616 if (showOutOfSpaceMessage) {
617 mLauncher.showOutOfSpaceMessage();
618 }
619 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700620 }
621
Winson Chung7f0acdd2011-09-19 18:34:19 -0700622 @Override
623 protected void onDetachedFromWindow() {
624 super.onDetachedFromWindow();
625
626 // Clean up all the async tasks
627 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
628 while (iter.hasNext()) {
629 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
630 task.cancel(false);
631 iter.remove();
632 }
633 }
634
Winson Chung785d2eb2011-04-14 16:08:02 -0700635 public void setContentType(ContentType type) {
636 mContentType = type;
Winson Chung87acb482011-08-23 17:28:05 -0700637 invalidatePageData(0, (type != ContentType.Applications));
Winson Chung785d2eb2011-04-14 16:08:02 -0700638 }
639
Winson Chungb44b5242011-06-13 11:32:14 -0700640 public boolean isContentType(ContentType type) {
641 return (mContentType == type);
642 }
643
Winson Chung5a808352011-06-27 19:08:49 -0700644 public void setCurrentPageToWidgets() {
645 invalidatePageData(0);
646 }
Winson Chung5a808352011-06-27 19:08:49 -0700647
Winson Chung785d2eb2011-04-14 16:08:02 -0700648 /*
649 * Apps PagedView implementation
650 */
Winson Chung63257c12011-05-05 17:06:13 -0700651 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
652 int childCount = layout.getChildCount();
653 for (int i = 0; i < childCount; ++i) {
654 layout.getChildAt(i).setVisibility(visibility);
655 }
656 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700657 private void setupPage(PagedViewCellLayout layout) {
658 layout.setCellCount(mCellCountX, mCellCountY);
659 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
660 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
661 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
662
Winson Chung63257c12011-05-05 17:06:13 -0700663 // Note: We force a measure here to get around the fact that when we do layout calculations
664 // immediately after syncing, we don't have a proper width. That said, we already know the
665 // expected page width, so we can actually optimize by hiding all the TextView-based
666 // children that are expensive to measure, and let that happen naturally later.
667 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700668 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700669 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700670 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700671 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700672 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700673 }
674 public void syncAppsPages() {
675 // Ensure that we have the right number of pages
676 Context context = getContext();
677 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
678 for (int i = 0; i < numPages; ++i) {
679 PagedViewCellLayout layout = new PagedViewCellLayout(context);
680 setupPage(layout);
681 addView(layout);
682 }
683 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700684 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700685 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700686 int numCells = mCellCountX * mCellCountY;
687 int startIndex = page * numCells;
688 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700689 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700690
Winson Chung785d2eb2011-04-14 16:08:02 -0700691 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700692 ArrayList<Object> items = new ArrayList<Object>();
693 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700694 for (int i = startIndex; i < endIndex; ++i) {
695 ApplicationInfo info = mApps.get(i);
696 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
697 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700698 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700699 icon.setOnClickListener(this);
700 icon.setOnLongClickListener(this);
701 icon.setOnTouchListener(this);
702
703 int index = i - startIndex;
704 int x = index % mCellCountX;
705 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700706 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700707
708 items.add(info);
709 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700710 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700711
Winson Chungf0ea4d32011-06-06 14:27:16 -0700712 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700713
Winson Chung8ff87132011-09-30 19:40:46 -0700714 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700715 if (mFadeInAdjacentScreens) {
716 prepareGenerateHoloOutlinesTask(page, items, images);
717 }
Winson Chung8ff87132011-09-30 19:40:46 -0700718 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700719 }
Winson Chungb44b5242011-06-13 11:32:14 -0700720
721 /**
722 * Return the appropriate thread priority for loading for a given page (we give the current
723 * page much higher priority)
724 */
725 private int getThreadPriorityForPage(int page) {
726 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
727 int pageDiff = Math.abs(page - mCurrentPage);
728 if (pageDiff <= 0) {
729 // return Process.THREAD_PRIORITY_DEFAULT;
730 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
731 } else if (pageDiff <= 1) {
732 // return Process.THREAD_PRIORITY_BACKGROUND;
733 return Process.THREAD_PRIORITY_DEFAULT;
734 } else {
735 // return Process.THREAD_PRIORITY_LOWEST;
736 return Process.THREAD_PRIORITY_DEFAULT;
737 }
738 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700739 private int getSleepForPage(int page) {
740 int pageDiff = Math.abs(page - mCurrentPage) - 1;
741 return Math.max(0, pageDiff * sPageSleepDelay);
742 }
Winson Chungb44b5242011-06-13 11:32:14 -0700743 /**
744 * Creates and executes a new AsyncTask to load a page of widget previews.
745 */
746 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700747 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700748 // Prune all tasks that are no longer needed
749 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
750 while (iter.hasNext()) {
751 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
752 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700753 if ((taskPage == page) ||
754 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700755 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700756 task.cancel(false);
757 iter.remove();
758 } else {
759 task.setThreadPriority(getThreadPriorityForPage(taskPage));
760 }
761 }
762
Winson Chungf314b0e2011-08-16 11:54:27 -0700763 // We introduce a slight delay to order the loading of side pages so that we don't thrash
764 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700765 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700766 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700767 @Override
768 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700769 try {
Winson Chung09945932011-09-20 14:22:40 -0700770 try {
771 Thread.sleep(sleepMs);
772 } catch (Exception e) {}
773 loadWidgetPreviewsInBackground(task, data);
774 } finally {
775 if (task.isCancelled()) {
776 data.cleanup(true);
777 }
778 }
Winson Chungb44b5242011-06-13 11:32:14 -0700779 }
780 },
781 new AsyncTaskCallback() {
782 @Override
783 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700784 try {
785 mRunningTasks.remove(task);
786 if (task.isCancelled()) return;
787 if (task.page > getPageCount()) return;
788 if (task.pageContentType != mContentType) return;
789 onSyncWidgetPageItems(data);
790 } finally {
791 data.cleanup(task.isCancelled());
792 }
Winson Chungb44b5242011-06-13 11:32:14 -0700793 }
Winson Chung09945932011-09-20 14:22:40 -0700794 });
Winson Chungb44b5242011-06-13 11:32:14 -0700795
796 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700797 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
798 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700799 t.setThreadPriority(getThreadPriorityForPage(page));
800 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
801 mRunningTasks.add(t);
802 }
803 /**
804 * Creates and executes a new AsyncTask to load the outlines for a page of content.
805 */
806 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
807 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700808 // Prune old tasks for this page
809 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
810 while (iter.hasNext()) {
811 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
812 int taskPage = task.page;
813 if ((taskPage == page) &&
814 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
815 task.cancel(false);
816 iter.remove();
817 }
818 }
819
Winson Chungb44b5242011-06-13 11:32:14 -0700820 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
821 new AsyncTaskCallback() {
822 @Override
823 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700824 try {
825 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700826 task.syncThreadPriority();
827
Winson Chung09945932011-09-20 14:22:40 -0700828 ArrayList<Bitmap> images = data.generatedImages;
829 ArrayList<Bitmap> srcImages = data.sourceImages;
830 int count = srcImages.size();
831 Canvas c = new Canvas();
832 for (int i = 0; i < count && !task.isCancelled(); ++i) {
833 // Before work on each item, ensure that this task is running at the correct
834 // priority
835 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700836
Winson Chung09945932011-09-20 14:22:40 -0700837 Bitmap b = srcImages.get(i);
838 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
839 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700840
Winson Chung09945932011-09-20 14:22:40 -0700841 c.setBitmap(outline);
842 c.save();
843 c.drawBitmap(b, 0, 0, null);
844 c.restore();
845 c.setBitmap(null);
846
847 images.add(outline);
848 }
849 } finally {
850 if (task.isCancelled()) {
851 data.cleanup(true);
852 }
Winson Chungb44b5242011-06-13 11:32:14 -0700853 }
854 }
855 },
856 new AsyncTaskCallback() {
857 @Override
858 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700859 try {
860 mRunningTasks.remove(task);
861 if (task.isCancelled()) return;
862 if (task.page > getPageCount()) return;
863 if (task.pageContentType != mContentType) return;
864 onHolographicPageItemsLoaded(data);
865 } finally {
866 data.cleanup(task.isCancelled());
867 }
Winson Chungb44b5242011-06-13 11:32:14 -0700868 }
869 });
870
871 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700872 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700873 new AppsCustomizeAsyncTask(page, mContentType,
874 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700875 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
876 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
877 mRunningTasks.add(t);
878 }
879
Winson Chung785d2eb2011-04-14 16:08:02 -0700880 /*
881 * Widgets PagedView implementation
882 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700883 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700884 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
885 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700886
887 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700888 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700889 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
890 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700891 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700892 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700893 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700894 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700895 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700896 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
897 }
898 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
899 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700900 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700901 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700902 c.scale(scaleX, scaleY);
903 Rect oldBounds = d.copyBounds();
904 d.setBounds(x, y, x + w, y + h);
905 d.draw(c);
906 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700907 if (multiplyColor != 0xFFFFFFFF) {
908 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
909 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700910 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700911 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700912 }
Winson Chungd2945262011-06-24 15:22:14 -0700913 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700914 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700915 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700916 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700917 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700918 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700919 }
Winson Chung4e076542011-06-23 13:04:10 -0700920 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700921 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700922
Winson Chung4b576be2011-04-27 17:40:20 -0700923 // Calculate the size of the drawable
924 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
925 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
926 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
927 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
928
929 // Scale down the bitmap to fit the space
930 float widgetPreviewScale = (float) cellWidth / expectedWidth;
931 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
932 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
933
934 // Load the preview image if possible
935 String packageName = info.provider.getPackageName();
936 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700937 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700938 if (info.previewImage != 0) {
939 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
940 if (drawable == null) {
941 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
942 + " for provider: " + info.provider);
943 } else {
944 // Scale down the preview to the dimensions we want
945 int imageWidth = drawable.getIntrinsicWidth();
946 int imageHeight = drawable.getIntrinsicHeight();
947 float aspect = (float) imageWidth / imageHeight;
948 int newWidth = imageWidth;
949 int newHeight = imageHeight;
950 if (aspect > 1f) {
951 newWidth = expectedWidth;
952 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
953 } else {
954 newHeight = expectedHeight;
955 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
956 }
957
Winson Chungb44b5242011-06-13 11:32:14 -0700958 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700959 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700960 }
961 }
962
963 // Generate a preview image if we couldn't load one
964 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700965 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700966 int bitmapWidth;
967 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700968
Winson Chung273c1022011-07-11 13:40:52 -0700969 // Specify the dimensions of the bitmap (since we are using a default preview bg with
970 // the full icon, we only imply the aspect ratio of the widget)
971 if (cellHSpan == cellVSpan) {
972 bitmapWidth = bitmapHeight = cellWidth;
973 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
974 } else if (cellHSpan >= cellVSpan) {
975 bitmapWidth = expectedWidth = cellWidth;
976 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700977 } else {
978 // Note that in vertical widgets, we might not have enough space due to the text
979 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700980 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
981 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700982 }
Winson Chung4b576be2011-04-27 17:40:20 -0700983
Winson Chung273c1022011-07-11 13:40:52 -0700984 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700985 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700986 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700987
988 // Draw the icon in the top left corner
989 try {
990 Drawable icon = null;
991 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
992 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
993
Winson Chungd2945262011-06-24 15:22:14 -0700994 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
995 renderDrawableToBitmap(icon, preview, offset, offset,
996 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700997 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700998 }
Winson Chungb44b5242011-06-13 11:32:14 -0700999 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 }
1001 public void syncWidgetPages() {
1002 // Ensure that we have the right number of pages
1003 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001004 int numPages = (int) Math.ceil(mWidgets.size() /
1005 (float) (mWidgetCountX * mWidgetCountY));
1006 for (int j = 0; j < numPages; ++j) {
1007 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1008 mWidgetCountY);
1009 setupPage(layout);
1010 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001011 }
1012 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001013 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001014 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001015 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1016 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -07001017
Winson Chungd2945262011-06-24 15:22:14 -07001018 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -07001019 ArrayList<Object> items = new ArrayList<Object>();
1020 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001021 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -07001022 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001023 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001024
Winson Chunge4a647f2011-09-30 14:41:25 -07001025 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001026 int offset = page * numItemsPerPage;
1027 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1028 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001029 }
1030
Winson Chunge4a647f2011-09-30 14:41:25 -07001031 // Prepopulate the pages with the other widget info, and fill in the previews later
1032 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1033 layout.setColumnCount(layout.getCellCountX());
1034 for (int i = 0; i < items.size(); ++i) {
1035 Object rawInfo = items.get(i);
1036 PendingAddItemInfo createItemInfo = null;
1037 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1038 R.layout.apps_customize_widget, layout, false);
1039 if (rawInfo instanceof AppWidgetProviderInfo) {
1040 // Fill in the widget information
1041 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1042 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1043 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1044 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1045 mHolographicOutlineHelper);
1046 widget.setTag(createItemInfo);
1047 } else if (rawInfo instanceof ResolveInfo) {
1048 // Fill in the shortcuts information
1049 ResolveInfo info = (ResolveInfo) rawInfo;
1050 createItemInfo = new PendingAddItemInfo();
1051 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1052 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1053 info.activityInfo.name);
1054 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1055 widget.setTag(createItemInfo);
1056 }
1057 widget.setOnClickListener(this);
1058 widget.setOnLongClickListener(this);
1059 widget.setOnTouchListener(this);
1060
1061 // Layout each widget
1062 int ix = i % mWidgetCountX;
1063 int iy = i / mWidgetCountX;
1064 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1065 GridLayout.spec(iy, GridLayout.LEFT),
1066 GridLayout.spec(ix, GridLayout.TOP));
1067 lp.width = cellWidth;
1068 lp.height = cellHeight;
1069 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1070 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1071 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1072 layout.addView(widget, lp);
1073 }
1074
1075 // Load the widget previews
Winson Chungf314b0e2011-08-16 11:54:27 -07001076 if (immediate) {
1077 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
1078 mWidgetCountX, null, null);
1079 loadWidgetPreviewsInBackground(null, data);
1080 onSyncWidgetPageItems(data);
1081 } else {
1082 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
1083 }
1084 }
1085 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1086 AsyncTaskPageData data) {
1087 if (task != null) {
1088 // Ensure that this task starts running at the correct priority
1089 task.syncThreadPriority();
1090 }
1091
1092 // Load each of the widget/shortcut previews
1093 ArrayList<Object> items = data.items;
1094 ArrayList<Bitmap> images = data.generatedImages;
1095 int count = items.size();
1096 int cellWidth = data.cellWidth;
1097 int cellHeight = data.cellHeight;
1098 for (int i = 0; i < count; ++i) {
1099 if (task != null) {
1100 // Ensure we haven't been cancelled yet
1101 if (task.isCancelled()) break;
1102 // Before work on each item, ensure that this task is running at the correct
1103 // priority
1104 task.syncThreadPriority();
1105 }
1106
1107 Object rawInfo = items.get(i);
1108 if (rawInfo instanceof AppWidgetProviderInfo) {
1109 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001110 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001111 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1112 cellWidth, cellHeight));
1113 } else if (rawInfo instanceof ResolveInfo) {
1114 // Fill in the shortcuts information
1115 ResolveInfo info = (ResolveInfo) rawInfo;
1116 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1117 }
1118 }
Winson Chungb44b5242011-06-13 11:32:14 -07001119 }
1120 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1121 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001122 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001123
Winson Chungb44b5242011-06-13 11:32:14 -07001124 ArrayList<Object> items = data.items;
1125 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001126 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001127 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1128 if (widget != null) {
1129 widget.applyPreview(new FastBitmapDrawable(data.generatedImages.get(i)), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001130 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001131 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001132 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001133
1134 invalidate();
1135 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001136
Winson Chung8ff87132011-09-30 19:40:46 -07001137 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001138 if (mFadeInAdjacentScreens) {
1139 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1140 }
Winson Chung8ff87132011-09-30 19:40:46 -07001141 */
Winson Chungb44b5242011-06-13 11:32:14 -07001142 }
1143 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1144 // Invalidate early to short-circuit children invalidates
1145 invalidate();
1146
1147 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001148 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001149 if (layout instanceof PagedViewCellLayout) {
1150 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1151 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001152 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001153 for (int i = 0; i < count; ++i) {
1154 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001155 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001156 }
1157 } else {
1158 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001159 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001160 for (int i = 0; i < count; ++i) {
1161 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001162 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001163 }
1164 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001165 }
Winson Chung46af2e82011-05-09 16:00:53 -07001166
Winson Chung785d2eb2011-04-14 16:08:02 -07001167 @Override
1168 public void syncPages() {
1169 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001170
1171 // Remove all background asyc tasks if we are loading content anew
1172 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1173 while (iter.hasNext()) {
1174 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1175 task.cancel(false);
1176 iter.remove();
1177 }
1178
Winson Chung785d2eb2011-04-14 16:08:02 -07001179 switch (mContentType) {
1180 case Applications:
1181 syncAppsPages();
1182 break;
1183 case Widgets:
1184 syncWidgetPages();
1185 break;
1186 }
1187 }
1188 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001189 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001190 switch (mContentType) {
1191 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001192 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001193 break;
1194 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001195 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001196 break;
1197 }
1198 }
1199
Adam Cohen22f823d2011-09-01 17:22:18 -07001200 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1201 // it is in the z-order. This is important to insure touch events are handled correctly.
1202 View getPageAt(int index) {
1203 return getChildAt(getChildCount() - index - 1);
1204 }
1205
1206 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1207 @Override
1208 protected void screenScrolled(int screenCenter) {
1209 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001210
1211 for (int i = 0; i < getChildCount(); i++) {
1212 View v = getPageAt(i);
1213 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001214 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001215
1216 float interpolatedProgress =
1217 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1218 float scale = (1 - interpolatedProgress) +
1219 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1220 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001221
1222 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1223 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001224
1225 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1226 int pageWidth = v.getMeasuredWidth();
1227 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001228
1229 if (PERFORM_OVERSCROLL_ROTATION) {
1230 if (i == 0 && scrollProgress < 0) {
1231 // Overscroll to the left
1232 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1233 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1234 scale = 1.0f;
1235 alpha = 1.0f;
1236 // On the first page, we don't want the page to have any lateral motion
1237 translationX = getScrollX();
1238 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1239 // Overscroll to the right
1240 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1241 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1242 scale = 1.0f;
1243 alpha = 1.0f;
1244 // On the last page, we don't want the page to have any lateral motion.
1245 translationX = getScrollX() - mMaxScrollX;
1246 } else {
1247 v.setPivotY(pageHeight / 2.0f);
1248 v.setPivotX(pageWidth / 2.0f);
1249 v.setRotationY(0f);
1250 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001251 }
1252
1253 v.setTranslationX(translationX);
1254 v.setScaleX(scale);
1255 v.setScaleY(scale);
1256 v.setAlpha(alpha);
1257 }
1258 }
1259 }
1260
1261 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001262 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001263 }
1264
Winson Chung785d2eb2011-04-14 16:08:02 -07001265 /**
1266 * Used by the parent to get the content width to set the tab bar to
1267 * @return
1268 */
1269 public int getPageContentWidth() {
1270 return mContentWidth;
1271 }
1272
Winson Chungb26f3d62011-06-02 10:49:29 -07001273 @Override
1274 protected void onPageBeginMoving() {
1275 /* TO BE ENABLED LATER
1276 setChildrenDrawnWithCacheEnabled(true);
1277 for (int i = 0; i < getChildCount(); ++i) {
1278 View v = getChildAt(i);
1279 if (v instanceof PagedViewCellLayout) {
1280 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1281 }
1282 }
1283 */
1284 super.onPageBeginMoving();
1285 }
1286
1287 @Override
1288 protected void onPageEndMoving() {
1289 /* TO BE ENABLED LATER
1290 for (int i = 0; i < getChildCount(); ++i) {
1291 View v = getChildAt(i);
1292 if (v instanceof PagedViewCellLayout) {
1293 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1294 }
1295 }
1296 setChildrenDrawnWithCacheEnabled(false);
1297 */
1298 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001299
1300 // We reset the save index when we change pages so that it will be recalculated on next
1301 // rotation
1302 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001303 }
1304
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 /*
1306 * AllAppsView implementation
1307 */
1308 @Override
1309 public void setup(Launcher launcher, DragController dragController) {
1310 mLauncher = launcher;
1311 mDragController = dragController;
1312 }
1313 @Override
1314 public void zoom(float zoom, boolean animate) {
1315 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1316 }
1317 @Override
1318 public boolean isVisible() {
1319 return (getVisibility() == VISIBLE);
1320 }
1321 @Override
1322 public boolean isAnimating() {
1323 return false;
1324 }
1325 @Override
1326 public void setApps(ArrayList<ApplicationInfo> list) {
1327 mApps = list;
1328 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001329
Winson Chung875de7e2011-06-28 14:25:17 -07001330 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1331 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001332 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001333 }
1334 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1335 // We add it in place, in alphabetical order
1336 int count = list.size();
1337 for (int i = 0; i < count; ++i) {
1338 ApplicationInfo info = list.get(i);
1339 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1340 if (index < 0) {
1341 mApps.add(-(index + 1), info);
1342 }
1343 }
1344 }
1345 @Override
1346 public void addApps(ArrayList<ApplicationInfo> list) {
1347 addAppsWithoutInvalidate(list);
1348 invalidatePageData();
1349 }
1350 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1351 ComponentName removeComponent = item.intent.getComponent();
1352 int length = list.size();
1353 for (int i = 0; i < length; ++i) {
1354 ApplicationInfo info = list.get(i);
1355 if (info.intent.getComponent().equals(removeComponent)) {
1356 return i;
1357 }
1358 }
1359 return -1;
1360 }
1361 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1362 // loop through all the apps and remove apps that have the same component
1363 int length = list.size();
1364 for (int i = 0; i < length; ++i) {
1365 ApplicationInfo info = list.get(i);
1366 int removeIndex = findAppByComponent(mApps, info);
1367 if (removeIndex > -1) {
1368 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001369 }
1370 }
1371 }
1372 @Override
1373 public void removeApps(ArrayList<ApplicationInfo> list) {
1374 removeAppsWithoutInvalidate(list);
1375 invalidatePageData();
1376 }
1377 @Override
1378 public void updateApps(ArrayList<ApplicationInfo> list) {
1379 // We remove and re-add the updated applications list because it's properties may have
1380 // changed (ie. the title), and this will ensure that the items will be in their proper
1381 // place in the list.
1382 removeAppsWithoutInvalidate(list);
1383 addAppsWithoutInvalidate(list);
1384 invalidatePageData();
1385 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001386
Winson Chung785d2eb2011-04-14 16:08:02 -07001387 @Override
1388 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001389 if (mContentType != ContentType.Applications) {
1390 // Reset to the first page of the Apps pane
1391 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1392 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001393 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001394 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001395 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001396 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001397 }
1398 @Override
1399 public void dumpState() {
1400 // TODO: Dump information related to current list of Applications, Widgets, etc.
1401 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1402 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1403 }
1404 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001405 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001406 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001407 for (Object i: list) {
1408 if (i instanceof AppWidgetProviderInfo) {
1409 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1410 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1411 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1412 + " initialLayout=" + info.initialLayout
1413 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1414 } else if (i instanceof ResolveInfo) {
1415 ResolveInfo info = (ResolveInfo) i;
1416 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1417 + info.icon);
1418 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001419 }
1420 }
1421 @Override
1422 public void surrender() {
1423 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1424 // should stop this now.
1425 }
Winson Chung007c6982011-06-14 13:27:53 -07001426
Winson Chungb44b5242011-06-13 11:32:14 -07001427 /*
1428 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1429 * widget previews in the background with the AsyncTasks.
1430 */
1431 protected int getAssociatedLowerPageBound(int page) {
1432 return Math.max(0, page - 2);
1433 }
1434 protected int getAssociatedUpperPageBound(int page) {
1435 final int count = getChildCount();
1436 return Math.min(page + 2, count - 1);
1437 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001438
1439 @Override
1440 protected String getCurrentPageDescription() {
1441 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1442 int stringId = R.string.default_scroll_format;
1443 switch (mContentType) {
1444 case Applications:
1445 stringId = R.string.apps_customize_apps_scroll_format;
1446 break;
1447 case Widgets:
1448 stringId = R.string.apps_customize_widgets_scroll_format;
1449 break;
1450 }
1451 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1452 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001453}