blob: 32b178760d717d2b1fd3157918604b4d5f9cdbaf [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 Chung5fc72b32011-10-11 17:53:58 -070034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070036import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.graphics.Rect;
Winson Chung5fc72b32011-10-11 17:53:58 -070038import android.graphics.RectF;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070040import android.os.AsyncTask;
41import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.util.AttributeSet;
43import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070044import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070046import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070048import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070049import android.view.animation.AccelerateInterpolator;
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();
Winson Chung5fc72b32011-10-11 17:53:58 -0700280 mCenterPagesVertically = true;
Winson Chung785d2eb2011-04-14 16:08:02 -0700281
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();
Winson Chung5fc72b32011-10-11 17:53:58 -0700534 RectF mTmpScaleRect = new RectF(0f,0f,1f,1f);
535 image.getImageMatrix().mapRect(mTmpScaleRect);
536 float scale = mTmpScaleRect.right;
537 int w = (int) (preview.getIntrinsicWidth() * scale);
538 int h = (int) (preview.getIntrinsicHeight() * scale);
Winson Chung1ed747a2011-05-03 16:18:34 -0700539 if (createItemInfo instanceof PendingAddWidgetInfo) {
540 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700541 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700542 createItemInfo.spanX = spanXY[0];
543 createItemInfo.spanY = spanXY[1];
544
545 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700546 renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700547 } else {
548 // Workaround for the fact that we don't keep the original ResolveInfo associated with
549 // the shortcut around. To get the icon, we just render the preview image (which has
550 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
551 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
552 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700553 mCanvas.setBitmap(b);
554 mCanvas.save();
555 preview.draw(mCanvas);
556 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700557 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700558 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700559 createItemInfo.spanX = createItemInfo.spanY = 1;
560 }
Winson Chung4b576be2011-04-27 17:40:20 -0700561
562 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700563 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700564 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
565 createItemInfo.spanY, b);
566 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700567 DragController.DRAG_ACTION_COPY, null);
568 b.recycle();
569 }
570 @Override
571 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700572 // Dismiss the cling
573 mLauncher.dismissAllAppsCling(null);
574
Winson Chung4b576be2011-04-27 17:40:20 -0700575 if (!super.beginDragging(v)) return false;
576
Winson Chungc07918d2011-07-01 15:35:26 -0700577 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700578 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700579
Winson Chung4b576be2011-04-27 17:40:20 -0700580 if (v instanceof PagedViewIcon) {
581 beginDraggingApplication(v);
582 } else if (v instanceof PagedViewWidget) {
583 beginDraggingWidget(v);
584 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700585 return true;
586 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700588 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700589 if (!success || (target != mLauncher.getWorkspace() &&
590 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700591 // Exit spring loaded mode if we have not successfully dropped or have not handled the
592 // drop in Workspace
593 mLauncher.exitSpringLoadedDragMode();
594 }
Adam Cohen446e9402011-09-15 18:21:21 -0700595 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700596
Winson Chung785d2eb2011-04-14 16:08:02 -0700597 }
598
Winson Chung785d2eb2011-04-14 16:08:02 -0700599 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700600 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700602
603 // Display an error message if the drag failed due to there not being enough space on the
604 // target layout we were dropping on.
605 if (!success) {
606 boolean showOutOfSpaceMessage = false;
607 if (target instanceof Workspace) {
608 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
609 Workspace workspace = (Workspace) target;
610 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700611 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700612 if (layout != null) {
613 layout.calculateSpans(itemInfo);
614 showOutOfSpaceMessage =
615 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
616 }
617 }
618 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
619 if (showOutOfSpaceMessage) {
620 mLauncher.showOutOfSpaceMessage();
621 }
622 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700623 }
624
Winson Chung7f0acdd2011-09-19 18:34:19 -0700625 @Override
626 protected void onDetachedFromWindow() {
627 super.onDetachedFromWindow();
628
629 // Clean up all the async tasks
630 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
631 while (iter.hasNext()) {
632 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
633 task.cancel(false);
634 iter.remove();
635 }
636 }
637
Winson Chung785d2eb2011-04-14 16:08:02 -0700638 public void setContentType(ContentType type) {
639 mContentType = type;
Winson Chung87acb482011-08-23 17:28:05 -0700640 invalidatePageData(0, (type != ContentType.Applications));
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 }
642
Winson Chungb44b5242011-06-13 11:32:14 -0700643 public boolean isContentType(ContentType type) {
644 return (mContentType == type);
645 }
646
Winson Chung5a808352011-06-27 19:08:49 -0700647 public void setCurrentPageToWidgets() {
648 invalidatePageData(0);
649 }
Winson Chung5a808352011-06-27 19:08:49 -0700650
Winson Chung785d2eb2011-04-14 16:08:02 -0700651 /*
652 * Apps PagedView implementation
653 */
Winson Chung63257c12011-05-05 17:06:13 -0700654 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
655 int childCount = layout.getChildCount();
656 for (int i = 0; i < childCount; ++i) {
657 layout.getChildAt(i).setVisibility(visibility);
658 }
659 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700660 private void setupPage(PagedViewCellLayout layout) {
661 layout.setCellCount(mCellCountX, mCellCountY);
662 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
663 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
664 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
665
Winson Chung63257c12011-05-05 17:06:13 -0700666 // Note: We force a measure here to get around the fact that when we do layout calculations
667 // immediately after syncing, we don't have a proper width. That said, we already know the
668 // expected page width, so we can actually optimize by hiding all the TextView-based
669 // children that are expensive to measure, and let that happen naturally later.
670 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700671 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700672 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700673 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700674 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700675 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 }
677 public void syncAppsPages() {
678 // Ensure that we have the right number of pages
679 Context context = getContext();
680 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
681 for (int i = 0; i < numPages; ++i) {
682 PagedViewCellLayout layout = new PagedViewCellLayout(context);
683 setupPage(layout);
684 addView(layout);
685 }
686 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700687 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700688 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700689 int numCells = mCellCountX * mCellCountY;
690 int startIndex = page * numCells;
691 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700692 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700693
Winson Chung785d2eb2011-04-14 16:08:02 -0700694 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700695 ArrayList<Object> items = new ArrayList<Object>();
696 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 for (int i = startIndex; i < endIndex; ++i) {
698 ApplicationInfo info = mApps.get(i);
699 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
700 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700701 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700702 icon.setOnClickListener(this);
703 icon.setOnLongClickListener(this);
704 icon.setOnTouchListener(this);
705
706 int index = i - startIndex;
707 int x = index % mCellCountX;
708 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700709 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700710
711 items.add(info);
712 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700714
Winson Chungf0ea4d32011-06-06 14:27:16 -0700715 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700716
Winson Chung8ff87132011-09-30 19:40:46 -0700717 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700718 if (mFadeInAdjacentScreens) {
719 prepareGenerateHoloOutlinesTask(page, items, images);
720 }
Winson Chung8ff87132011-09-30 19:40:46 -0700721 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700722 }
Winson Chungb44b5242011-06-13 11:32:14 -0700723
724 /**
725 * Return the appropriate thread priority for loading for a given page (we give the current
726 * page much higher priority)
727 */
728 private int getThreadPriorityForPage(int page) {
729 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
730 int pageDiff = Math.abs(page - mCurrentPage);
731 if (pageDiff <= 0) {
732 // return Process.THREAD_PRIORITY_DEFAULT;
733 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
734 } else if (pageDiff <= 1) {
735 // return Process.THREAD_PRIORITY_BACKGROUND;
736 return Process.THREAD_PRIORITY_DEFAULT;
737 } else {
738 // return Process.THREAD_PRIORITY_LOWEST;
739 return Process.THREAD_PRIORITY_DEFAULT;
740 }
741 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700742 private int getSleepForPage(int page) {
743 int pageDiff = Math.abs(page - mCurrentPage) - 1;
744 return Math.max(0, pageDiff * sPageSleepDelay);
745 }
Winson Chungb44b5242011-06-13 11:32:14 -0700746 /**
747 * Creates and executes a new AsyncTask to load a page of widget previews.
748 */
749 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700750 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700751 // Prune all tasks that are no longer needed
752 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
753 while (iter.hasNext()) {
754 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
755 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700756 if ((taskPage == page) ||
757 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700758 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700759 task.cancel(false);
760 iter.remove();
761 } else {
762 task.setThreadPriority(getThreadPriorityForPage(taskPage));
763 }
764 }
765
Winson Chungf314b0e2011-08-16 11:54:27 -0700766 // We introduce a slight delay to order the loading of side pages so that we don't thrash
767 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700768 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700769 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700770 @Override
771 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700772 try {
Winson Chung09945932011-09-20 14:22:40 -0700773 try {
774 Thread.sleep(sleepMs);
775 } catch (Exception e) {}
776 loadWidgetPreviewsInBackground(task, data);
777 } finally {
778 if (task.isCancelled()) {
779 data.cleanup(true);
780 }
781 }
Winson Chungb44b5242011-06-13 11:32:14 -0700782 }
783 },
784 new AsyncTaskCallback() {
785 @Override
786 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700787 try {
788 mRunningTasks.remove(task);
789 if (task.isCancelled()) return;
790 if (task.page > getPageCount()) return;
791 if (task.pageContentType != mContentType) return;
792 onSyncWidgetPageItems(data);
793 } finally {
794 data.cleanup(task.isCancelled());
795 }
Winson Chungb44b5242011-06-13 11:32:14 -0700796 }
Winson Chung09945932011-09-20 14:22:40 -0700797 });
Winson Chungb44b5242011-06-13 11:32:14 -0700798
799 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700800 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
801 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700802 t.setThreadPriority(getThreadPriorityForPage(page));
803 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
804 mRunningTasks.add(t);
805 }
806 /**
807 * Creates and executes a new AsyncTask to load the outlines for a page of content.
808 */
809 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
810 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700811 // Prune old tasks for this page
812 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
813 while (iter.hasNext()) {
814 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
815 int taskPage = task.page;
816 if ((taskPage == page) &&
817 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
818 task.cancel(false);
819 iter.remove();
820 }
821 }
822
Winson Chungb44b5242011-06-13 11:32:14 -0700823 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
824 new AsyncTaskCallback() {
825 @Override
826 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700827 try {
828 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700829 task.syncThreadPriority();
830
Winson Chung09945932011-09-20 14:22:40 -0700831 ArrayList<Bitmap> images = data.generatedImages;
832 ArrayList<Bitmap> srcImages = data.sourceImages;
833 int count = srcImages.size();
834 Canvas c = new Canvas();
835 for (int i = 0; i < count && !task.isCancelled(); ++i) {
836 // Before work on each item, ensure that this task is running at the correct
837 // priority
838 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700839
Winson Chung09945932011-09-20 14:22:40 -0700840 Bitmap b = srcImages.get(i);
841 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
842 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700843
Winson Chung09945932011-09-20 14:22:40 -0700844 c.setBitmap(outline);
845 c.save();
846 c.drawBitmap(b, 0, 0, null);
847 c.restore();
848 c.setBitmap(null);
849
850 images.add(outline);
851 }
852 } finally {
853 if (task.isCancelled()) {
854 data.cleanup(true);
855 }
Winson Chungb44b5242011-06-13 11:32:14 -0700856 }
857 }
858 },
859 new AsyncTaskCallback() {
860 @Override
861 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700862 try {
863 mRunningTasks.remove(task);
864 if (task.isCancelled()) return;
865 if (task.page > getPageCount()) return;
866 if (task.pageContentType != mContentType) return;
867 onHolographicPageItemsLoaded(data);
868 } finally {
869 data.cleanup(task.isCancelled());
870 }
Winson Chungb44b5242011-06-13 11:32:14 -0700871 }
872 });
873
874 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700875 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700876 new AppsCustomizeAsyncTask(page, mContentType,
877 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700878 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
879 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
880 mRunningTasks.add(t);
881 }
882
Winson Chung785d2eb2011-04-14 16:08:02 -0700883 /*
884 * Widgets PagedView implementation
885 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700886 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700887 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
888 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700889
890 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700891 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700892 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
893 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700894 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700895 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700896 }
Winson Chung5fc72b32011-10-11 17:53:58 -0700897 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
898 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700899 }
900 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung5fc72b32011-10-11 17:53:58 -0700901 float scale) {
902 renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
903 }
904 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
905 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700906 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700907 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700908 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700909 Rect oldBounds = d.copyBounds();
910 d.setBounds(x, y, x + w, y + h);
911 d.draw(c);
912 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700913 if (multiplyColor != 0xFFFFFFFF) {
914 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
915 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700916 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700917 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 }
Winson Chungd2945262011-06-24 15:22:14 -0700919 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700920 // Render the background
921 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
922 int bitmapSize = mAppIconSize + 2 * offset;
923 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
924 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapSize, bitmapSize);
925
Winson Chung1ed747a2011-05-03 16:18:34 -0700926 // Render the icon
Winson Chung4dbea792011-05-05 14:21:32 -0700927 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chung5fc72b32011-10-11 17:53:58 -0700928 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700929 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700930 }
Winson Chung4e076542011-06-23 13:04:10 -0700931 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700932 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700933
Winson Chung4b576be2011-04-27 17:40:20 -0700934 // 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 {
Winson Chung5fc72b32011-10-11 17:53:58 -0700944 // Scale down the preview to something that is closer to the cellWidth/Height
Winson Chung4b576be2011-04-27 17:40:20 -0700945 int imageWidth = drawable.getIntrinsicWidth();
946 int imageHeight = drawable.getIntrinsicHeight();
Winson Chung5fc72b32011-10-11 17:53:58 -0700947 int bitmapWidth = imageWidth;
948 int bitmapHeight = imageHeight;
949 if (imageWidth > imageHeight) {
950 bitmapWidth = cellWidth;
951 bitmapHeight = (int) (imageHeight * ((float) bitmapWidth / imageWidth));
Winson Chung4b576be2011-04-27 17:40:20 -0700952 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700953 bitmapHeight = cellHeight;
954 bitmapWidth = (int) (imageWidth * ((float) bitmapHeight / imageHeight));
Winson Chung4b576be2011-04-27 17:40:20 -0700955 }
956
Winson Chung5fc72b32011-10-11 17:53:58 -0700957 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
958 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
Winson Chung4b576be2011-04-27 17:40:20 -0700959 }
960 }
961
962 // Generate a preview image if we couldn't load one
963 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700964 Resources resources = mLauncher.getResources();
Winson Chung5fc72b32011-10-11 17:53:58 -0700965 // TODO: This actually uses the apps customize cell layout params, where as we make want
966 // the Workspace params for more accuracy.
967 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
968 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
969 int bitmapWidth = targetWidth;
970 int bitmapHeight = targetHeight;
971 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
972 float iconScale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700973
Winson Chung5fc72b32011-10-11 17:53:58 -0700974 // Determine the size of the bitmap we want to draw
Winson Chung273c1022011-07-11 13:40:52 -0700975 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700976 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
977 if (cellHSpan <= 1) {
978 bitmapWidth = bitmapHeight = mAppIconSize + 2 * offset;
979 } else {
980 bitmapWidth = bitmapHeight = mAppIconSize + 4 * offset;
981 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700982 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700983 // Otherwise, ensure that we are properly sized within the cellWidth/Height
984 if (targetWidth > targetHeight) {
985 bitmapWidth = Math.min(targetWidth, cellWidth);
986 bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth));
987 iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * offset), 1f);
988 } else {
989 bitmapHeight = Math.min(targetHeight, cellHeight);
990 bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight));
991 iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * offset), 1f);
992 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700993 }
Winson Chung273c1022011-07-11 13:40:52 -0700994 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700995 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
996 bitmapWidth);
Winson Chung4b576be2011-04-27 17:40:20 -0700997
998 // Draw the icon in the top left corner
999 try {
1000 Drawable icon = null;
1001 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
1002 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1003
Winson Chung5fc72b32011-10-11 17:53:58 -07001004 renderDrawableToBitmap(icon, preview, (int) (offset * iconScale),
1005 (int) (offset * iconScale), (int) (mAppIconSize * iconScale),
1006 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001007 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001008 }
Winson Chungb44b5242011-06-13 11:32:14 -07001009 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001010 }
1011 public void syncWidgetPages() {
1012 // Ensure that we have the right number of pages
1013 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001014 int numPages = (int) Math.ceil(mWidgets.size() /
1015 (float) (mWidgetCountX * mWidgetCountY));
1016 for (int j = 0; j < numPages; ++j) {
1017 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1018 mWidgetCountY);
1019 setupPage(layout);
Winson Chung5fc72b32011-10-11 17:53:58 -07001020 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1021 LayoutParams.MATCH_PARENT));
Winson Chung785d2eb2011-04-14 16:08:02 -07001022 }
1023 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001024 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001025 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001026 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1027 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -07001028
Winson Chungd2945262011-06-24 15:22:14 -07001029 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -07001030 ArrayList<Object> items = new ArrayList<Object>();
1031 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001032 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -07001033 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001034 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001035
Winson Chunge4a647f2011-09-30 14:41:25 -07001036 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001037 int offset = page * numItemsPerPage;
1038 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1039 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001040 }
1041
Winson Chunge4a647f2011-09-30 14:41:25 -07001042 // Prepopulate the pages with the other widget info, and fill in the previews later
1043 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1044 layout.setColumnCount(layout.getCellCountX());
1045 for (int i = 0; i < items.size(); ++i) {
1046 Object rawInfo = items.get(i);
1047 PendingAddItemInfo createItemInfo = null;
1048 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1049 R.layout.apps_customize_widget, layout, false);
1050 if (rawInfo instanceof AppWidgetProviderInfo) {
1051 // Fill in the widget information
1052 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1053 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1054 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1055 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1056 mHolographicOutlineHelper);
1057 widget.setTag(createItemInfo);
1058 } else if (rawInfo instanceof ResolveInfo) {
1059 // Fill in the shortcuts information
1060 ResolveInfo info = (ResolveInfo) rawInfo;
1061 createItemInfo = new PendingAddItemInfo();
1062 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1063 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1064 info.activityInfo.name);
1065 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1066 widget.setTag(createItemInfo);
1067 }
1068 widget.setOnClickListener(this);
1069 widget.setOnLongClickListener(this);
1070 widget.setOnTouchListener(this);
1071
1072 // Layout each widget
1073 int ix = i % mWidgetCountX;
1074 int iy = i / mWidgetCountX;
1075 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1076 GridLayout.spec(iy, GridLayout.LEFT),
1077 GridLayout.spec(ix, GridLayout.TOP));
1078 lp.width = cellWidth;
1079 lp.height = cellHeight;
1080 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1081 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1082 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1083 layout.addView(widget, lp);
1084 }
1085
1086 // Load the widget previews
Winson Chungf314b0e2011-08-16 11:54:27 -07001087 if (immediate) {
1088 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
1089 mWidgetCountX, null, null);
1090 loadWidgetPreviewsInBackground(null, data);
1091 onSyncWidgetPageItems(data);
1092 } else {
1093 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
1094 }
1095 }
1096 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1097 AsyncTaskPageData data) {
1098 if (task != null) {
1099 // Ensure that this task starts running at the correct priority
1100 task.syncThreadPriority();
1101 }
1102
1103 // Load each of the widget/shortcut previews
1104 ArrayList<Object> items = data.items;
1105 ArrayList<Bitmap> images = data.generatedImages;
1106 int count = items.size();
1107 int cellWidth = data.cellWidth;
1108 int cellHeight = data.cellHeight;
1109 for (int i = 0; i < count; ++i) {
1110 if (task != null) {
1111 // Ensure we haven't been cancelled yet
1112 if (task.isCancelled()) break;
1113 // Before work on each item, ensure that this task is running at the correct
1114 // priority
1115 task.syncThreadPriority();
1116 }
1117
1118 Object rawInfo = items.get(i);
1119 if (rawInfo instanceof AppWidgetProviderInfo) {
1120 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001121 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001122 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1123 cellWidth, cellHeight));
1124 } else if (rawInfo instanceof ResolveInfo) {
1125 // Fill in the shortcuts information
1126 ResolveInfo info = (ResolveInfo) rawInfo;
1127 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1128 }
1129 }
Winson Chungb44b5242011-06-13 11:32:14 -07001130 }
1131 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1132 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001133 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134
Winson Chungb44b5242011-06-13 11:32:14 -07001135 ArrayList<Object> items = data.items;
1136 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001137 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001138 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1139 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001140 Bitmap preview = data.generatedImages.get(i);
1141 boolean scale =
1142 (preview.getWidth() >= data.cellWidth ||
1143 preview.getHeight() >= data.cellHeight);
1144
1145 widget.applyPreview(new FastBitmapDrawable(preview), i, scale);
Winson Chung1ed747a2011-05-03 16:18:34 -07001146 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001147 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001148 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001149
1150 invalidate();
1151 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001152
Winson Chung8ff87132011-09-30 19:40:46 -07001153 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001154 if (mFadeInAdjacentScreens) {
1155 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1156 }
Winson Chung8ff87132011-09-30 19:40:46 -07001157 */
Winson Chungb44b5242011-06-13 11:32:14 -07001158 }
1159 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1160 // Invalidate early to short-circuit children invalidates
1161 invalidate();
1162
1163 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001164 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001165 if (layout instanceof PagedViewCellLayout) {
1166 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1167 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001168 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001169 for (int i = 0; i < count; ++i) {
1170 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001171 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001172 }
1173 } else {
1174 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001175 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001176 for (int i = 0; i < count; ++i) {
1177 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001178 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001179 }
1180 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001181 }
Winson Chung46af2e82011-05-09 16:00:53 -07001182
Winson Chung785d2eb2011-04-14 16:08:02 -07001183 @Override
1184 public void syncPages() {
1185 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001186
1187 // Remove all background asyc tasks if we are loading content anew
1188 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1189 while (iter.hasNext()) {
1190 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1191 task.cancel(false);
1192 iter.remove();
1193 }
1194
Winson Chung785d2eb2011-04-14 16:08:02 -07001195 switch (mContentType) {
1196 case Applications:
1197 syncAppsPages();
1198 break;
1199 case Widgets:
1200 syncWidgetPages();
1201 break;
1202 }
1203 }
1204 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001205 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 switch (mContentType) {
1207 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001208 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 break;
1210 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001211 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 break;
1213 }
1214 }
1215
Adam Cohen22f823d2011-09-01 17:22:18 -07001216 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1217 // it is in the z-order. This is important to insure touch events are handled correctly.
1218 View getPageAt(int index) {
1219 return getChildAt(getChildCount() - index - 1);
1220 }
1221
1222 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1223 @Override
1224 protected void screenScrolled(int screenCenter) {
1225 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001226
1227 for (int i = 0; i < getChildCount(); i++) {
1228 View v = getPageAt(i);
1229 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001230 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001231
1232 float interpolatedProgress =
1233 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1234 float scale = (1 - interpolatedProgress) +
1235 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1236 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001237
1238 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1239 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001240
1241 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1242 int pageWidth = v.getMeasuredWidth();
1243 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001244
1245 if (PERFORM_OVERSCROLL_ROTATION) {
1246 if (i == 0 && scrollProgress < 0) {
1247 // Overscroll to the left
1248 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1249 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1250 scale = 1.0f;
1251 alpha = 1.0f;
1252 // On the first page, we don't want the page to have any lateral motion
1253 translationX = getScrollX();
1254 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1255 // Overscroll to the right
1256 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1257 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1258 scale = 1.0f;
1259 alpha = 1.0f;
1260 // On the last page, we don't want the page to have any lateral motion.
1261 translationX = getScrollX() - mMaxScrollX;
1262 } else {
1263 v.setPivotY(pageHeight / 2.0f);
1264 v.setPivotX(pageWidth / 2.0f);
1265 v.setRotationY(0f);
1266 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001267 }
1268
1269 v.setTranslationX(translationX);
1270 v.setScaleX(scale);
1271 v.setScaleY(scale);
1272 v.setAlpha(alpha);
1273 }
1274 }
1275 }
1276
1277 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001278 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001279 }
1280
Winson Chung785d2eb2011-04-14 16:08:02 -07001281 /**
1282 * Used by the parent to get the content width to set the tab bar to
1283 * @return
1284 */
1285 public int getPageContentWidth() {
1286 return mContentWidth;
1287 }
1288
Winson Chungb26f3d62011-06-02 10:49:29 -07001289 @Override
1290 protected void onPageBeginMoving() {
1291 /* TO BE ENABLED LATER
1292 setChildrenDrawnWithCacheEnabled(true);
1293 for (int i = 0; i < getChildCount(); ++i) {
1294 View v = getChildAt(i);
1295 if (v instanceof PagedViewCellLayout) {
1296 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1297 }
1298 }
1299 */
1300 super.onPageBeginMoving();
1301 }
1302
1303 @Override
1304 protected void onPageEndMoving() {
1305 /* TO BE ENABLED LATER
1306 for (int i = 0; i < getChildCount(); ++i) {
1307 View v = getChildAt(i);
1308 if (v instanceof PagedViewCellLayout) {
1309 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1310 }
1311 }
1312 setChildrenDrawnWithCacheEnabled(false);
1313 */
1314 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001315
1316 // We reset the save index when we change pages so that it will be recalculated on next
1317 // rotation
1318 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001319 }
1320
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 /*
1322 * AllAppsView implementation
1323 */
1324 @Override
1325 public void setup(Launcher launcher, DragController dragController) {
1326 mLauncher = launcher;
1327 mDragController = dragController;
1328 }
1329 @Override
1330 public void zoom(float zoom, boolean animate) {
1331 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1332 }
1333 @Override
1334 public boolean isVisible() {
1335 return (getVisibility() == VISIBLE);
1336 }
1337 @Override
1338 public boolean isAnimating() {
1339 return false;
1340 }
1341 @Override
1342 public void setApps(ArrayList<ApplicationInfo> list) {
1343 mApps = list;
1344 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001345
Winson Chung875de7e2011-06-28 14:25:17 -07001346 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1347 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001348 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001349 }
1350 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1351 // We add it in place, in alphabetical order
1352 int count = list.size();
1353 for (int i = 0; i < count; ++i) {
1354 ApplicationInfo info = list.get(i);
1355 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1356 if (index < 0) {
1357 mApps.add(-(index + 1), info);
1358 }
1359 }
1360 }
1361 @Override
1362 public void addApps(ArrayList<ApplicationInfo> list) {
1363 addAppsWithoutInvalidate(list);
1364 invalidatePageData();
1365 }
1366 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1367 ComponentName removeComponent = item.intent.getComponent();
1368 int length = list.size();
1369 for (int i = 0; i < length; ++i) {
1370 ApplicationInfo info = list.get(i);
1371 if (info.intent.getComponent().equals(removeComponent)) {
1372 return i;
1373 }
1374 }
1375 return -1;
1376 }
1377 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1378 // loop through all the apps and remove apps that have the same component
1379 int length = list.size();
1380 for (int i = 0; i < length; ++i) {
1381 ApplicationInfo info = list.get(i);
1382 int removeIndex = findAppByComponent(mApps, info);
1383 if (removeIndex > -1) {
1384 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001385 }
1386 }
1387 }
1388 @Override
1389 public void removeApps(ArrayList<ApplicationInfo> list) {
1390 removeAppsWithoutInvalidate(list);
1391 invalidatePageData();
1392 }
1393 @Override
1394 public void updateApps(ArrayList<ApplicationInfo> list) {
1395 // We remove and re-add the updated applications list because it's properties may have
1396 // changed (ie. the title), and this will ensure that the items will be in their proper
1397 // place in the list.
1398 removeAppsWithoutInvalidate(list);
1399 addAppsWithoutInvalidate(list);
1400 invalidatePageData();
1401 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001402
Winson Chung785d2eb2011-04-14 16:08:02 -07001403 @Override
1404 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001405 if (mContentType != ContentType.Applications) {
1406 // Reset to the first page of the Apps pane
1407 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1408 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001409 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001410 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001411 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001412 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001413 }
1414 @Override
1415 public void dumpState() {
1416 // TODO: Dump information related to current list of Applications, Widgets, etc.
1417 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1418 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1419 }
1420 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001421 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001422 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001423 for (Object i: list) {
1424 if (i instanceof AppWidgetProviderInfo) {
1425 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1426 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1427 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1428 + " initialLayout=" + info.initialLayout
1429 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1430 } else if (i instanceof ResolveInfo) {
1431 ResolveInfo info = (ResolveInfo) i;
1432 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1433 + info.icon);
1434 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001435 }
1436 }
1437 @Override
1438 public void surrender() {
1439 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1440 // should stop this now.
1441 }
Winson Chung007c6982011-06-14 13:27:53 -07001442
Winson Chungb44b5242011-06-13 11:32:14 -07001443 /*
1444 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1445 * widget previews in the background with the AsyncTasks.
1446 */
1447 protected int getAssociatedLowerPageBound(int page) {
1448 return Math.max(0, page - 2);
1449 }
1450 protected int getAssociatedUpperPageBound(int page) {
1451 final int count = getChildCount();
1452 return Math.min(page + 2, count - 1);
1453 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001454
1455 @Override
1456 protected String getCurrentPageDescription() {
1457 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1458 int stringId = R.string.default_scroll_format;
1459 switch (mContentType) {
1460 case Applications:
1461 stringId = R.string.apps_customize_apps_scroll_format;
1462 break;
1463 case Widgets:
1464 stringId = R.string.apps_customize_widgets_scroll_format;
1465 break;
1466 }
1467 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1468 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001469}