blob: 96859d371cf6b5bfa6756f7e7f1077d95cf7aa6a [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
Winson Chung46af2e82011-05-09 16:00:53 -070027import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070035import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.graphics.Rect;
Adam Cohenb5ba0972011-09-07 18:02:31 -070037import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070039import android.os.AsyncTask;
40import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070045import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070046import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohenb5ba0972011-09-07 18:02:31 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
54import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import com.android.launcher2.DropTarget.DragObject;
56
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
74 LoadWidgetPreviewData,
75 LoadHolographicIconsData
76 }
77
Winson Chungb44b5242011-06-13 11:32:14 -070078 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
79 AsyncTaskCallback postR) {
80 page = p;
81 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070082 sourceImages = si;
83 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070084 cellWidth = cellHeight = -1;
85 doInBackgroundCallback = bgR;
86 postExecuteCallback = postR;
87 }
Winson Chungd2945262011-06-24 15:22:14 -070088 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070089 AsyncTaskCallback postR) {
90 page = p;
91 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070092 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070093 cellWidth = cw;
94 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070095 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070096 doInBackgroundCallback = bgR;
97 postExecuteCallback = postR;
98 }
99 int page;
100 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700101 ArrayList<Bitmap> sourceImages;
102 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 int cellWidth;
104 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700105 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700106 AsyncTaskCallback doInBackgroundCallback;
107 AsyncTaskCallback postExecuteCallback;
108}
Winson Chung4e076542011-06-23 13:04:10 -0700109
Winson Chungb44b5242011-06-13 11:32:14 -0700110/**
111 * A generic template for an async task used in AppsCustomize.
112 */
113class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700114 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700115 page = p;
116 pageContentType = t;
117 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700118 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700119 }
120 @Override
121 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
122 if (params.length != 1) return null;
123 // Load each of the widget previews in the background
124 params[0].doInBackgroundCallback.run(this, params[0]);
125 return params[0];
126 }
127 @Override
128 protected void onPostExecute(AsyncTaskPageData result) {
129 // All the widget previews are loaded, so we can just callback to inflate the page
130 result.postExecuteCallback.run(this, result);
131 }
132
133 void setThreadPriority(int p) {
134 threadPriority = p;
135 }
136 void syncThreadPriority() {
137 Process.setThreadPriority(threadPriority);
138 }
139
140 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700141 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 int page;
143 AppsCustomizePagedView.ContentType pageContentType;
144 int threadPriority;
145}
Winson Chungb44b5242011-06-13 11:32:14 -0700146
147/**
148 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
149 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700150public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
151 AllAppsView, View.OnClickListener, DragSource {
152 static final String LOG_TAG = "AppsCustomizePagedView";
153
154 /**
155 * The different content types that this paged view can show.
156 */
157 public enum ContentType {
158 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700159 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700160 }
161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
170 private int mRestorePage = -1;
171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
173 private ContentType mContentType;
174 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700175 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700176
Winson Chung7d7541e2011-09-16 20:14:36 -0700177 // Cling
178 private int mClingFocusedX;
179 private int mClingFocusedY;
180
Winson Chung1ed747a2011-05-03 16:18:34 -0700181 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700182 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700183 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700184 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700185 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700186
187 // Dimens
188 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700189 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700190 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700191 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700192 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700193 private final int mWidgetPreviewIconPaddedDimension;
194 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700195 private PagedViewCellLayout mWidgetSpacingLayout;
196
Adam Cohen22f823d2011-09-01 17:22:18 -0700197 // Relating to the scroll and overscroll effects
198 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700199 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700200 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700201 private static float TRANSITION_PIVOT = 0.65f;
202 private static float TRANSITION_MAX_ROTATION = 22;
203 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700204 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen22f823d2011-09-01 17:22:18 -0700205
Winson Chungb44b5242011-06-13 11:32:14 -0700206 // Previews & outlines
207 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
208 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chungf314b0e2011-08-16 11:54:27 -0700209 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700210
Winson Chung785d2eb2011-04-14 16:08:02 -0700211 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
212 super(context, attrs);
213 mLayoutInflater = LayoutInflater.from(context);
214 mPackageManager = context.getPackageManager();
215 mContentType = ContentType.Applications;
216 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700217 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700218 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700219 mHolographicOutlineHelper = new HolographicOutlineHelper();
220 mCanvas = new Canvas();
221 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700222
223 // Save the default widget preview background
224 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700225 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700226 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
227 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700228
229 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700230 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700231 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
232 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
233 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700234 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700235 mWidgetWidthGap =
236 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
237 mWidgetHeightGap =
238 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700239 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
240 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700241 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
242 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700243 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700244 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700245
246 // The max widget span is the length N, such that NxN is the largest bounds that the widget
247 // preview can be before applying the widget scaling
248 mMinWidgetSpan = 1;
249 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700250
251 // The padding on the non-matched dimension for the default widget preview icons
252 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700253 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700254 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700255 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700256 }
257
258 @Override
259 protected void init() {
260 super.init();
261 mCenterPagesVertically = false;
262
263 Context context = getContext();
264 Resources r = context.getResources();
265 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
266 }
267
Winson Chungf0ea4d32011-06-06 14:27:16 -0700268 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700269 protected void onUnhandledTap(MotionEvent ev) {
270 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700271 // Dismiss AppsCustomize if we tap
272 mLauncher.showWorkspace(true);
273 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 }
275
Winson Chung5afbf7b2011-07-25 11:53:08 -0700276 /** Returns the item index of the center item on this page so that we can restore to this
277 * item index when we rotate. */
278 private int getMiddleComponentIndexOnCurrentPage() {
279 int i = -1;
280 if (getPageCount() > 0) {
281 int currentPage = getCurrentPage();
282 switch (mContentType) {
283 case Applications: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700284 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700285 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
286 int numItemsPerPage = mCellCountX * mCellCountY;
287 int childCount = childrenLayout.getChildCount();
288 if (childCount > 0) {
289 i = (currentPage * numItemsPerPage) + (childCount / 2);
290 }}
291 break;
292 case Widgets: {
Adam Cohen22f823d2011-09-01 17:22:18 -0700293 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700294 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
295 int childCount = layout.getChildCount();
296 if (childCount > 0) {
297 i = (currentPage * numItemsPerPage) + (childCount / 2);
298 }}
299 break;
300 }
301 }
302 return i;
303 }
304
305 /** Get the index of the item to restore to if we need to restore the current page. */
306 int getSaveInstanceStateIndex() {
307 if (mSaveInstanceStateItemIndex == -1) {
308 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
309 }
310 return mSaveInstanceStateItemIndex;
311 }
312
313 /** Returns the page in the current orientation which is expected to contain the specified
314 * item index. */
315 int getPageForComponent(int index) {
316 switch (mContentType) {
317 case Applications: {
318 int numItemsPerPage = mCellCountX * mCellCountY;
319 return (index / numItemsPerPage);
320 }
321 case Widgets: {
322 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
323 return (index / numItemsPerPage);
324 }}
325 return -1;
326 }
327
Winson Chungf0ea4d32011-06-06 14:27:16 -0700328 /**
329 * This differs from isDataReady as this is the test done if isDataReady is not set.
330 */
331 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700332 // We only do this test once, and we default to the Applications page, so we only really
333 // have to wait for there to be apps.
Winson Chung87acb482011-08-23 17:28:05 -0700334 if (mContentType == AppsCustomizePagedView.ContentType.Widgets) {
335 return !mApps.isEmpty() && !mWidgets.isEmpty();
336 } else {
337 return !mApps.isEmpty();
338 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 }
340
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 /** Restores the page for an item at the specified index */
342 void restorePageForIndex(int index) {
343 if (index < 0) return;
344
345 int page = getPageForComponent(index);
346 if (page > -1) {
347 if (getChildCount() > 0) {
348 invalidatePageData(page);
349 } else {
350 mRestorePage = page;
351 }
352 }
353 }
354
Winson Chungf0ea4d32011-06-06 14:27:16 -0700355 protected void onDataReady(int width, int height) {
356 // Note that we transpose the counts in portrait so that we get a similar layout
357 boolean isLandscape = getResources().getConfiguration().orientation ==
358 Configuration.ORIENTATION_LANDSCAPE;
359 int maxCellCountX = Integer.MAX_VALUE;
360 int maxCellCountY = Integer.MAX_VALUE;
361 if (LauncherApplication.isScreenLarge()) {
362 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
363 LauncherModel.getCellCountY());
364 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
365 LauncherModel.getCellCountX());
366 }
367
368 // Now that the data is ready, we can calculate the content width, the number of cells to
369 // use for each page
370 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
371 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
372 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
373 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
374 mCellCountX = mWidgetSpacingLayout.getCellCountX();
375 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung5a808352011-06-27 19:08:49 -0700376
Winson Chungdb1138b2011-06-30 14:39:35 -0700377 // Force a measure to update recalculate the gaps
378 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
379 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
380 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700381 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700382 invalidatePageData(Math.max(0, mRestorePage));
383 mRestorePage = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700384
385 int[] offset = new int[2];
386 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
387 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
388 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + offset[0];
389 pos[1] += (getMeasuredHeight() - mWidgetSpacingLayout.getMeasuredHeight()) / 2 + offset[1];
390 mLauncher.showFirstRunAllAppsCling(pos);
391
Winson Chungf0ea4d32011-06-06 14:27:16 -0700392 }
393
394 @Override
395 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
396 int width = MeasureSpec.getSize(widthMeasureSpec);
397 int height = MeasureSpec.getSize(heightMeasureSpec);
398 if (!isDataReady()) {
399 if (testDataReady()) {
400 setDataIsReady();
401 setMeasuredDimension(width, height);
402 onDataReady(width, height);
403 }
404 }
405
406 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
407 }
408
Winson Chung34efdaf2011-05-24 14:19:56 -0700409 /** Removes and returns the ResolveInfo with the specified ComponentName */
410 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
411 ComponentName cn) {
412 Iterator<ResolveInfo> iter = list.iterator();
413 while (iter.hasNext()) {
414 ResolveInfo rinfo = iter.next();
415 ActivityInfo info = rinfo.activityInfo;
416 ComponentName c = new ComponentName(info.packageName, info.name);
417 if (c.equals(cn)) {
418 iter.remove();
419 return rinfo;
420 }
421 }
422 return null;
423 }
424
Winson Chung785d2eb2011-04-14 16:08:02 -0700425 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700426 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
427 // by a broadcast receiver, and in order for it to work correctly, we need to know that
428 // the AppWidgetService has already received and processed the same broadcast. Since there
429 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
430 // we should have a more precise way of ensuring the AppWidgetService is up to date.
431 postDelayed(new Runnable() {
432 public void run() {
433 updatePackages();
434 }
435 }, 500);
436 }
437
438 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700439 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700440 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700441 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 List<AppWidgetProviderInfo> widgets =
443 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
445 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700447 mWidgets.addAll(shortcuts);
448 Collections.sort(mWidgets,
449 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700450
Winson Chung875de7e2011-06-28 14:25:17 -0700451 if (wasEmpty) {
452 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
453 // a layout to do this test and invalidate the page data when ready.
454 if (testDataReady()) requestLayout();
455 } else {
456 invalidatePageData();
457 }
Winson Chung4b576be2011-04-27 17:40:20 -0700458 }
459
460 @Override
461 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700462 // When we have exited all apps or are in transition, disregard clicks
463 if (!mLauncher.isAllAppsCustomizeOpen() ||
464 mLauncher.getWorkspace().isSwitchingState()) return;
465
Winson Chung4b576be2011-04-27 17:40:20 -0700466 if (v instanceof PagedViewIcon) {
467 // Animate some feedback to the click
468 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
469 animateClickFeedback(v, new Runnable() {
470 @Override
471 public void run() {
472 mLauncher.startActivitySafely(appInfo.intent, appInfo);
473 }
474 });
475 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700476 // Let the user know that they have to long press to add a widget
477 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
478 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700479
Winson Chungd2e87b32011-06-02 10:53:07 -0700480 // Create a little animation to show that the widget can move
481 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
482 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
483 AnimatorSet bounce = new AnimatorSet();
484 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
485 tyuAnim.setDuration(125);
486 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
487 tydAnim.setDuration(100);
488 bounce.play(tyuAnim).before(tydAnim);
489 bounce.setInterpolator(new AccelerateInterpolator());
490 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700491 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700492 }
493
494 /*
495 * PagedViewWithDraggableItems implementation
496 */
497 @Override
498 protected void determineDraggingStart(android.view.MotionEvent ev) {
499 // Disable dragging by pulling an app down for now.
500 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700501
Winson Chung4b576be2011-04-27 17:40:20 -0700502 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700503 mLauncher.getWorkspace().onDragStartedWithItem(v);
504 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700505 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700506
Winson Chung4b576be2011-04-27 17:40:20 -0700507 private void beginDraggingWidget(View v) {
508 // Get the widget preview as the drag representation
509 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700510 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700511
512 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700513 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700514 Drawable preview = image.getDrawable();
515 int w = preview.getIntrinsicWidth();
516 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700517 if (createItemInfo instanceof PendingAddWidgetInfo) {
518 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700519 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700520 createItemInfo.spanX = spanXY[0];
521 createItemInfo.spanY = spanXY[1];
522
523 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung70fc4382011-08-08 15:31:33 -0700524 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700525 } else {
526 // Workaround for the fact that we don't keep the original ResolveInfo associated with
527 // the shortcut around. To get the icon, we just render the preview image (which has
528 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
529 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
530 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700531 mCanvas.setBitmap(b);
532 mCanvas.save();
533 preview.draw(mCanvas);
534 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700535 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700536 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700537 createItemInfo.spanX = createItemInfo.spanY = 1;
538 }
Winson Chung4b576be2011-04-27 17:40:20 -0700539
540 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700541 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700542 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
543 createItemInfo.spanY, b);
544 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700545 DragController.DRAG_ACTION_COPY, null);
546 b.recycle();
547 }
548 @Override
549 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700550 // Dismiss the cling
551 mLauncher.dismissAllAppsCling(null);
552
Winson Chung4b576be2011-04-27 17:40:20 -0700553 if (!super.beginDragging(v)) return false;
554
Winson Chungc07918d2011-07-01 15:35:26 -0700555 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700556 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700557
Winson Chung4b576be2011-04-27 17:40:20 -0700558 if (v instanceof PagedViewIcon) {
559 beginDraggingApplication(v);
560 } else if (v instanceof PagedViewWidget) {
561 beginDraggingWidget(v);
562 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700563 return true;
564 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700565 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700566 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700567 if (!success || (target != mLauncher.getWorkspace() &&
568 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700569 // Exit spring loaded mode if we have not successfully dropped or have not handled the
570 // drop in Workspace
571 mLauncher.exitSpringLoadedDragMode();
572 }
Adam Cohen446e9402011-09-15 18:21:21 -0700573 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700574
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 }
576
Winson Chung785d2eb2011-04-14 16:08:02 -0700577 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700578 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700579 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700580
581 // Display an error message if the drag failed due to there not being enough space on the
582 // target layout we were dropping on.
583 if (!success) {
584 boolean showOutOfSpaceMessage = false;
585 if (target instanceof Workspace) {
586 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
587 Workspace workspace = (Workspace) target;
588 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700589 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700590 if (layout != null) {
591 layout.calculateSpans(itemInfo);
592 showOutOfSpaceMessage =
593 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
594 }
595 }
596 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
597 if (showOutOfSpaceMessage) {
598 mLauncher.showOutOfSpaceMessage();
599 }
600 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700601 }
602
603 public void setContentType(ContentType type) {
604 mContentType = type;
Winson Chung87acb482011-08-23 17:28:05 -0700605 invalidatePageData(0, (type != ContentType.Applications));
Winson Chung785d2eb2011-04-14 16:08:02 -0700606 }
607
Winson Chungb44b5242011-06-13 11:32:14 -0700608 public boolean isContentType(ContentType type) {
609 return (mContentType == type);
610 }
611
Winson Chung5a808352011-06-27 19:08:49 -0700612 public void setCurrentPageToWidgets() {
613 invalidatePageData(0);
614 }
Winson Chung5a808352011-06-27 19:08:49 -0700615
Winson Chung785d2eb2011-04-14 16:08:02 -0700616 /*
617 * Apps PagedView implementation
618 */
Winson Chung63257c12011-05-05 17:06:13 -0700619 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
620 int childCount = layout.getChildCount();
621 for (int i = 0; i < childCount; ++i) {
622 layout.getChildAt(i).setVisibility(visibility);
623 }
624 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700625 private void setupPage(PagedViewCellLayout layout) {
626 layout.setCellCount(mCellCountX, mCellCountY);
627 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
628 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
629 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
630
Winson Chung63257c12011-05-05 17:06:13 -0700631 // Note: We force a measure here to get around the fact that when we do layout calculations
632 // immediately after syncing, we don't have a proper width. That said, we already know the
633 // expected page width, so we can actually optimize by hiding all the TextView-based
634 // children that are expensive to measure, and let that happen naturally later.
635 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700636 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700637 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700638 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700639 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700640 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 }
642 public void syncAppsPages() {
643 // Ensure that we have the right number of pages
644 Context context = getContext();
645 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
646 for (int i = 0; i < numPages; ++i) {
647 PagedViewCellLayout layout = new PagedViewCellLayout(context);
648 setupPage(layout);
649 addView(layout);
650 }
651 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700652 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700653 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700654 int numCells = mCellCountX * mCellCountY;
655 int startIndex = page * numCells;
656 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700657 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700658
Winson Chung785d2eb2011-04-14 16:08:02 -0700659 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700660 ArrayList<Object> items = new ArrayList<Object>();
661 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700662 for (int i = startIndex; i < endIndex; ++i) {
663 ApplicationInfo info = mApps.get(i);
664 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
665 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700666 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700667 icon.setOnClickListener(this);
668 icon.setOnLongClickListener(this);
669 icon.setOnTouchListener(this);
670
671 int index = i - startIndex;
672 int x = index % mCellCountX;
673 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700674 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700675
676 items.add(info);
677 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700679
Winson Chungf0ea4d32011-06-06 14:27:16 -0700680 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700681
Winson Chung54c725c2011-08-03 12:03:40 -0700682 if (mFadeInAdjacentScreens) {
683 prepareGenerateHoloOutlinesTask(page, items, images);
684 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700685 }
Winson Chungb44b5242011-06-13 11:32:14 -0700686
687 /**
688 * Return the appropriate thread priority for loading for a given page (we give the current
689 * page much higher priority)
690 */
691 private int getThreadPriorityForPage(int page) {
692 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
693 int pageDiff = Math.abs(page - mCurrentPage);
694 if (pageDiff <= 0) {
695 // return Process.THREAD_PRIORITY_DEFAULT;
696 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
697 } else if (pageDiff <= 1) {
698 // return Process.THREAD_PRIORITY_BACKGROUND;
699 return Process.THREAD_PRIORITY_DEFAULT;
700 } else {
701 // return Process.THREAD_PRIORITY_LOWEST;
702 return Process.THREAD_PRIORITY_DEFAULT;
703 }
704 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700705 private int getSleepForPage(int page) {
706 int pageDiff = Math.abs(page - mCurrentPage) - 1;
707 return Math.max(0, pageDiff * sPageSleepDelay);
708 }
Winson Chungb44b5242011-06-13 11:32:14 -0700709 /**
710 * Creates and executes a new AsyncTask to load a page of widget previews.
711 */
712 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700713 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700714 // Prune all tasks that are no longer needed
715 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
716 while (iter.hasNext()) {
717 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
718 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700719 if ((taskPage == page) ||
720 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700721 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700722 task.cancel(false);
723 iter.remove();
724 } else {
725 task.setThreadPriority(getThreadPriorityForPage(taskPage));
726 }
727 }
728
Winson Chungf314b0e2011-08-16 11:54:27 -0700729 // We introduce a slight delay to order the loading of side pages so that we don't thrash
730 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700731 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700732 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700733 @Override
734 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700735 try {
736 Thread.sleep(sleepMs);
737 } catch (Exception e) {}
738 loadWidgetPreviewsInBackground(task, data);
Winson Chungb44b5242011-06-13 11:32:14 -0700739 }
740 },
741 new AsyncTaskCallback() {
742 @Override
743 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
744 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700745 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700746 if (task.page > getPageCount()) return;
747 if (task.pageContentType != mContentType) return;
748 onSyncWidgetPageItems(data);
749 }
750 });
751
752 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700753 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
754 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700755 t.setThreadPriority(getThreadPriorityForPage(page));
756 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
757 mRunningTasks.add(t);
758 }
759 /**
760 * Creates and executes a new AsyncTask to load the outlines for a page of content.
761 */
762 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
763 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700764 // Prune old tasks for this page
765 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
766 while (iter.hasNext()) {
767 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
768 int taskPage = task.page;
769 if ((taskPage == page) &&
770 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
771 task.cancel(false);
772 iter.remove();
773 }
774 }
775
Winson Chungb44b5242011-06-13 11:32:14 -0700776 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
777 new AsyncTaskCallback() {
778 @Override
779 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
780 // Ensure that this task starts running at the correct priority
781 task.syncThreadPriority();
782
Winson Chung4e076542011-06-23 13:04:10 -0700783 ArrayList<Bitmap> images = data.generatedImages;
784 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700785 int count = srcImages.size();
786 Canvas c = new Canvas();
787 for (int i = 0; i < count && !task.isCancelled(); ++i) {
788 // Before work on each item, ensure that this task is running at the correct
789 // priority
790 task.syncThreadPriority();
791
792 Bitmap b = srcImages.get(i);
793 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
794 Bitmap.Config.ARGB_8888);
795
796 c.setBitmap(outline);
797 c.save();
798 c.drawBitmap(b, 0, 0, null);
799 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700800 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700801
802 images.add(outline);
803 }
804 }
805 },
806 new AsyncTaskCallback() {
807 @Override
808 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
809 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700810 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700811 if (task.page > getPageCount()) return;
812 if (task.pageContentType != mContentType) return;
813 onHolographicPageItemsLoaded(data);
814 }
815 });
816
817 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700818 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700819 new AppsCustomizeAsyncTask(page, mContentType,
820 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700821 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
822 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
823 mRunningTasks.add(t);
824 }
825
Winson Chung785d2eb2011-04-14 16:08:02 -0700826 /*
827 * Widgets PagedView implementation
828 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700829 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700830 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
831 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700832
833 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700834 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700835 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
836 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700838 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700839 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700840 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700841 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700842 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
843 }
844 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
845 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700846 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700847 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700848 c.scale(scaleX, scaleY);
849 Rect oldBounds = d.copyBounds();
850 d.setBounds(x, y, x + w, y + h);
851 d.draw(c);
852 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700853 if (multiplyColor != 0xFFFFFFFF) {
854 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
855 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700856 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700857 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700858 }
Winson Chungd2945262011-06-24 15:22:14 -0700859 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700860 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700861 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700862 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700863 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700864 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700865 }
Winson Chung4e076542011-06-23 13:04:10 -0700866 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700867 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700868
Winson Chung4b576be2011-04-27 17:40:20 -0700869 // Calculate the size of the drawable
870 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
871 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
872 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
873 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
874
875 // Scale down the bitmap to fit the space
876 float widgetPreviewScale = (float) cellWidth / expectedWidth;
877 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
878 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
879
880 // Load the preview image if possible
881 String packageName = info.provider.getPackageName();
882 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700883 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700884 if (info.previewImage != 0) {
885 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
886 if (drawable == null) {
887 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
888 + " for provider: " + info.provider);
889 } else {
890 // Scale down the preview to the dimensions we want
891 int imageWidth = drawable.getIntrinsicWidth();
892 int imageHeight = drawable.getIntrinsicHeight();
893 float aspect = (float) imageWidth / imageHeight;
894 int newWidth = imageWidth;
895 int newHeight = imageHeight;
896 if (aspect > 1f) {
897 newWidth = expectedWidth;
898 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
899 } else {
900 newHeight = expectedHeight;
901 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
902 }
903
Winson Chungb44b5242011-06-13 11:32:14 -0700904 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700905 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700906 }
907 }
908
909 // Generate a preview image if we couldn't load one
910 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700911 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700912 int bitmapWidth;
913 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700914
Winson Chung273c1022011-07-11 13:40:52 -0700915 // Specify the dimensions of the bitmap (since we are using a default preview bg with
916 // the full icon, we only imply the aspect ratio of the widget)
917 if (cellHSpan == cellVSpan) {
918 bitmapWidth = bitmapHeight = cellWidth;
919 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
920 } else if (cellHSpan >= cellVSpan) {
921 bitmapWidth = expectedWidth = cellWidth;
922 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700923 } else {
924 // Note that in vertical widgets, we might not have enough space due to the text
925 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700926 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
927 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700928 }
Winson Chung4b576be2011-04-27 17:40:20 -0700929
Winson Chung273c1022011-07-11 13:40:52 -0700930 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700931 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700932 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700933
934 // Draw the icon in the top left corner
935 try {
936 Drawable icon = null;
937 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
938 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
939
Winson Chungd2945262011-06-24 15:22:14 -0700940 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
941 renderDrawableToBitmap(icon, preview, offset, offset,
942 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700943 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700944 }
Winson Chungb44b5242011-06-13 11:32:14 -0700945 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700946 }
947 public void syncWidgetPages() {
948 // Ensure that we have the right number of pages
949 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700950 int numPages = (int) Math.ceil(mWidgets.size() /
951 (float) (mWidgetCountX * mWidgetCountY));
952 for (int j = 0; j < numPages; ++j) {
953 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
954 mWidgetCountY);
955 setupPage(layout);
956 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700957 }
958 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700959 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700960 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700961 int contentWidth = mWidgetSpacingLayout.getContentWidth();
962 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700963
Winson Chungd2945262011-06-24 15:22:14 -0700964 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700965 ArrayList<Object> items = new ArrayList<Object>();
966 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700967 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700968 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700969 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700970
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700971 int offset = page * numItemsPerPage;
972 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
973 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700974 }
975
Winson Chungf314b0e2011-08-16 11:54:27 -0700976 if (immediate) {
977 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
978 mWidgetCountX, null, null);
979 loadWidgetPreviewsInBackground(null, data);
980 onSyncWidgetPageItems(data);
981 } else {
982 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
983 }
Adam Cohen22f823d2011-09-01 17:22:18 -0700984 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
985 layout.createHardwareLayer();
Winson Chungf314b0e2011-08-16 11:54:27 -0700986 }
987 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
988 AsyncTaskPageData data) {
989 if (task != null) {
990 // Ensure that this task starts running at the correct priority
991 task.syncThreadPriority();
992 }
993
994 // Load each of the widget/shortcut previews
995 ArrayList<Object> items = data.items;
996 ArrayList<Bitmap> images = data.generatedImages;
997 int count = items.size();
998 int cellWidth = data.cellWidth;
999 int cellHeight = data.cellHeight;
1000 for (int i = 0; i < count; ++i) {
1001 if (task != null) {
1002 // Ensure we haven't been cancelled yet
1003 if (task.isCancelled()) break;
1004 // Before work on each item, ensure that this task is running at the correct
1005 // priority
1006 task.syncThreadPriority();
1007 }
1008
1009 Object rawInfo = items.get(i);
1010 if (rawInfo instanceof AppWidgetProviderInfo) {
1011 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001012 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001013 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1014 cellWidth, cellHeight));
1015 } else if (rawInfo instanceof ResolveInfo) {
1016 // Fill in the shortcuts information
1017 ResolveInfo info = (ResolveInfo) rawInfo;
1018 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1019 }
1020 }
Winson Chungb44b5242011-06-13 11:32:14 -07001021 }
1022 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1023 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001024 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -07001025 // Only set the column count once we have items
1026 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -07001027
Winson Chungb44b5242011-06-13 11:32:14 -07001028 ArrayList<Object> items = data.items;
1029 int count = items.size();
1030 int cellWidth = data.cellWidth;
1031 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -07001032 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -07001033 for (int i = 0; i < count; ++i) {
1034 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001035 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -07001036 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1037 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -07001038 if (rawInfo instanceof AppWidgetProviderInfo) {
1039 // Fill in the widget information
1040 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001041 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen61dcf372011-09-13 15:01:58 -07001042 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chung4e076542011-06-23 13:04:10 -07001043 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -07001044 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -07001045 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001046 widget.setTag(createItemInfo);
1047 } else if (rawInfo instanceof ResolveInfo) {
1048 // Fill in the shortcuts information
1049 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001050 createItemInfo = new PendingAddItemInfo();
Winson Chung1ed747a2011-05-03 16:18:34 -07001051 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1052 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1053 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -07001054 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001055 widget.applyFromResolveInfo(mPackageManager, info, preview,
1056 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001057 widget.setTag(createItemInfo);
1058 }
Winson Chung4b576be2011-04-27 17:40:20 -07001059 widget.setOnClickListener(this);
1060 widget.setOnLongClickListener(this);
1061 widget.setOnTouchListener(this);
1062
1063 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -07001064 int ix = i % cellCountX;
1065 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -07001066 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -07001067 GridLayout.spec(iy, GridLayout.LEFT),
1068 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -07001069 lp.width = cellWidth;
1070 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -07001071 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -07001072 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1073 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -07001074 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -07001075 }
Winson Chungb44b5242011-06-13 11:32:14 -07001076
1077 invalidate();
1078 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001079
1080 if (mFadeInAdjacentScreens) {
1081 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1082 }
Winson Chungb44b5242011-06-13 11:32:14 -07001083 }
1084 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1085 // Invalidate early to short-circuit children invalidates
1086 invalidate();
1087
1088 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001089 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001090 if (layout instanceof PagedViewCellLayout) {
1091 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1092 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001093 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001094 for (int i = 0; i < count; ++i) {
1095 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001096 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001097 }
1098 } else {
1099 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001100 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001101 for (int i = 0; i < count; ++i) {
1102 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001103 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001104 }
1105 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001106 }
Winson Chung46af2e82011-05-09 16:00:53 -07001107
Winson Chung785d2eb2011-04-14 16:08:02 -07001108 @Override
1109 public void syncPages() {
1110 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001111
1112 // Remove all background asyc tasks if we are loading content anew
1113 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1114 while (iter.hasNext()) {
1115 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1116 task.cancel(false);
1117 iter.remove();
1118 }
1119
Winson Chung785d2eb2011-04-14 16:08:02 -07001120 switch (mContentType) {
1121 case Applications:
1122 syncAppsPages();
1123 break;
1124 case Widgets:
1125 syncWidgetPages();
1126 break;
1127 }
1128 }
1129 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001130 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001131 switch (mContentType) {
1132 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001133 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 break;
1135 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001136 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 break;
1138 }
1139 }
1140
Adam Cohen22f823d2011-09-01 17:22:18 -07001141 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1142 // it is in the z-order. This is important to insure touch events are handled correctly.
1143 View getPageAt(int index) {
1144 return getChildAt(getChildCount() - index - 1);
1145 }
1146
1147 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1148 @Override
1149 protected void screenScrolled(int screenCenter) {
1150 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001151
1152 for (int i = 0; i < getChildCount(); i++) {
1153 View v = getPageAt(i);
1154 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001155 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001156
1157 float interpolatedProgress =
1158 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1159 float scale = (1 - interpolatedProgress) +
1160 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1161 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001162
1163 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1164 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001165
1166 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1167 int pageWidth = v.getMeasuredWidth();
1168 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001169
1170 if (PERFORM_OVERSCROLL_ROTATION) {
1171 if (i == 0 && scrollProgress < 0) {
1172 // Overscroll to the left
1173 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1174 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1175 scale = 1.0f;
1176 alpha = 1.0f;
1177 // On the first page, we don't want the page to have any lateral motion
1178 translationX = getScrollX();
1179 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1180 // Overscroll to the right
1181 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1182 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1183 scale = 1.0f;
1184 alpha = 1.0f;
1185 // On the last page, we don't want the page to have any lateral motion.
1186 translationX = getScrollX() - mMaxScrollX;
1187 } else {
1188 v.setPivotY(pageHeight / 2.0f);
1189 v.setPivotX(pageWidth / 2.0f);
1190 v.setRotationY(0f);
1191 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001192 }
1193
1194 v.setTranslationX(translationX);
1195 v.setScaleX(scale);
1196 v.setScaleY(scale);
1197 v.setAlpha(alpha);
1198 }
1199 }
1200 }
1201
1202 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001203 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001204 }
1205
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 /**
1207 * Used by the parent to get the content width to set the tab bar to
1208 * @return
1209 */
1210 public int getPageContentWidth() {
1211 return mContentWidth;
1212 }
1213
Winson Chungb26f3d62011-06-02 10:49:29 -07001214 @Override
1215 protected void onPageBeginMoving() {
1216 /* TO BE ENABLED LATER
1217 setChildrenDrawnWithCacheEnabled(true);
1218 for (int i = 0; i < getChildCount(); ++i) {
1219 View v = getChildAt(i);
1220 if (v instanceof PagedViewCellLayout) {
1221 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1222 }
1223 }
1224 */
1225 super.onPageBeginMoving();
1226 }
1227
1228 @Override
1229 protected void onPageEndMoving() {
1230 /* TO BE ENABLED LATER
1231 for (int i = 0; i < getChildCount(); ++i) {
1232 View v = getChildAt(i);
1233 if (v instanceof PagedViewCellLayout) {
1234 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1235 }
1236 }
1237 setChildrenDrawnWithCacheEnabled(false);
1238 */
1239 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001240
1241 // We reset the save index when we change pages so that it will be recalculated on next
1242 // rotation
1243 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001244 }
1245
Winson Chung785d2eb2011-04-14 16:08:02 -07001246 /*
1247 * AllAppsView implementation
1248 */
1249 @Override
1250 public void setup(Launcher launcher, DragController dragController) {
1251 mLauncher = launcher;
1252 mDragController = dragController;
1253 }
1254 @Override
1255 public void zoom(float zoom, boolean animate) {
1256 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1257 }
1258 @Override
1259 public boolean isVisible() {
1260 return (getVisibility() == VISIBLE);
1261 }
1262 @Override
1263 public boolean isAnimating() {
1264 return false;
1265 }
1266 @Override
1267 public void setApps(ArrayList<ApplicationInfo> list) {
1268 mApps = list;
1269 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001270
Winson Chung875de7e2011-06-28 14:25:17 -07001271 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1272 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001273 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001274 }
1275 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1276 // We add it in place, in alphabetical order
1277 int count = list.size();
1278 for (int i = 0; i < count; ++i) {
1279 ApplicationInfo info = list.get(i);
1280 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1281 if (index < 0) {
1282 mApps.add(-(index + 1), info);
1283 }
1284 }
1285 }
1286 @Override
1287 public void addApps(ArrayList<ApplicationInfo> list) {
1288 addAppsWithoutInvalidate(list);
1289 invalidatePageData();
1290 }
1291 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1292 ComponentName removeComponent = item.intent.getComponent();
1293 int length = list.size();
1294 for (int i = 0; i < length; ++i) {
1295 ApplicationInfo info = list.get(i);
1296 if (info.intent.getComponent().equals(removeComponent)) {
1297 return i;
1298 }
1299 }
1300 return -1;
1301 }
1302 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1303 // loop through all the apps and remove apps that have the same component
1304 int length = list.size();
1305 for (int i = 0; i < length; ++i) {
1306 ApplicationInfo info = list.get(i);
1307 int removeIndex = findAppByComponent(mApps, info);
1308 if (removeIndex > -1) {
1309 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 }
1311 }
1312 }
1313 @Override
1314 public void removeApps(ArrayList<ApplicationInfo> list) {
1315 removeAppsWithoutInvalidate(list);
1316 invalidatePageData();
1317 }
1318 @Override
1319 public void updateApps(ArrayList<ApplicationInfo> list) {
1320 // We remove and re-add the updated applications list because it's properties may have
1321 // changed (ie. the title), and this will ensure that the items will be in their proper
1322 // place in the list.
1323 removeAppsWithoutInvalidate(list);
1324 addAppsWithoutInvalidate(list);
1325 invalidatePageData();
1326 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001327
Winson Chung785d2eb2011-04-14 16:08:02 -07001328 @Override
1329 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001330 if (mContentType != ContentType.Applications) {
1331 // Reset to the first page of the Apps pane
1332 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1333 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001334 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001335 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001336 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001337 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 }
1339 @Override
1340 public void dumpState() {
1341 // TODO: Dump information related to current list of Applications, Widgets, etc.
1342 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1343 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1344 }
1345 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001346 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001348 for (Object i: list) {
1349 if (i instanceof AppWidgetProviderInfo) {
1350 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1351 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1352 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1353 + " initialLayout=" + info.initialLayout
1354 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1355 } else if (i instanceof ResolveInfo) {
1356 ResolveInfo info = (ResolveInfo) i;
1357 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1358 + info.icon);
1359 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 }
1361 }
1362 @Override
1363 public void surrender() {
1364 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1365 // should stop this now.
1366 }
Winson Chung007c6982011-06-14 13:27:53 -07001367
Winson Chungb44b5242011-06-13 11:32:14 -07001368 /*
1369 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1370 * widget previews in the background with the AsyncTasks.
1371 */
1372 protected int getAssociatedLowerPageBound(int page) {
1373 return Math.max(0, page - 2);
1374 }
1375 protected int getAssociatedUpperPageBound(int page) {
1376 final int count = getChildCount();
1377 return Math.min(page + 2, count - 1);
1378 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001379
1380 @Override
1381 protected String getCurrentPageDescription() {
1382 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1383 int stringId = R.string.default_scroll_format;
1384 switch (mContentType) {
1385 case Applications:
1386 stringId = R.string.apps_customize_apps_scroll_format;
1387 break;
1388 case Widgets:
1389 stringId = R.string.apps_customize_widgets_scroll_format;
1390 break;
1391 }
1392 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1393 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001394}