blob: 6493e973e2ca1d61f9aa476725c351949588a2bf [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
Winson Chung7f0acdd2011-09-19 18:34:19 -0700603 @Override
604 protected void onDetachedFromWindow() {
605 super.onDetachedFromWindow();
606
607 // Clean up all the async tasks
608 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
609 while (iter.hasNext()) {
610 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
611 task.cancel(false);
612 iter.remove();
613 }
614 }
615
Winson Chung785d2eb2011-04-14 16:08:02 -0700616 public void setContentType(ContentType type) {
617 mContentType = type;
Winson Chung87acb482011-08-23 17:28:05 -0700618 invalidatePageData(0, (type != ContentType.Applications));
Winson Chung785d2eb2011-04-14 16:08:02 -0700619 }
620
Winson Chungb44b5242011-06-13 11:32:14 -0700621 public boolean isContentType(ContentType type) {
622 return (mContentType == type);
623 }
624
Winson Chung5a808352011-06-27 19:08:49 -0700625 public void setCurrentPageToWidgets() {
626 invalidatePageData(0);
627 }
Winson Chung5a808352011-06-27 19:08:49 -0700628
Winson Chung785d2eb2011-04-14 16:08:02 -0700629 /*
630 * Apps PagedView implementation
631 */
Winson Chung63257c12011-05-05 17:06:13 -0700632 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
633 int childCount = layout.getChildCount();
634 for (int i = 0; i < childCount; ++i) {
635 layout.getChildAt(i).setVisibility(visibility);
636 }
637 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700638 private void setupPage(PagedViewCellLayout layout) {
639 layout.setCellCount(mCellCountX, mCellCountY);
640 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
641 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
642 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
643
Winson Chung63257c12011-05-05 17:06:13 -0700644 // Note: We force a measure here to get around the fact that when we do layout calculations
645 // immediately after syncing, we don't have a proper width. That said, we already know the
646 // expected page width, so we can actually optimize by hiding all the TextView-based
647 // children that are expensive to measure, and let that happen naturally later.
648 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700649 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700650 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700651 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700652 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700653 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700654 }
655 public void syncAppsPages() {
656 // Ensure that we have the right number of pages
657 Context context = getContext();
658 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
659 for (int i = 0; i < numPages; ++i) {
660 PagedViewCellLayout layout = new PagedViewCellLayout(context);
661 setupPage(layout);
662 addView(layout);
663 }
664 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700665 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700666 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700667 int numCells = mCellCountX * mCellCountY;
668 int startIndex = page * numCells;
669 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700670 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700671
Winson Chung785d2eb2011-04-14 16:08:02 -0700672 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700673 ArrayList<Object> items = new ArrayList<Object>();
674 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700675 for (int i = startIndex; i < endIndex; ++i) {
676 ApplicationInfo info = mApps.get(i);
677 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
678 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700679 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700680 icon.setOnClickListener(this);
681 icon.setOnLongClickListener(this);
682 icon.setOnTouchListener(this);
683
684 int index = i - startIndex;
685 int x = index % mCellCountX;
686 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700687 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700688
689 items.add(info);
690 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700691 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700692
Winson Chungf0ea4d32011-06-06 14:27:16 -0700693 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700694
Winson Chung54c725c2011-08-03 12:03:40 -0700695 if (mFadeInAdjacentScreens) {
696 prepareGenerateHoloOutlinesTask(page, items, images);
697 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700698 }
Winson Chungb44b5242011-06-13 11:32:14 -0700699
700 /**
701 * Return the appropriate thread priority for loading for a given page (we give the current
702 * page much higher priority)
703 */
704 private int getThreadPriorityForPage(int page) {
705 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
706 int pageDiff = Math.abs(page - mCurrentPage);
707 if (pageDiff <= 0) {
708 // return Process.THREAD_PRIORITY_DEFAULT;
709 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
710 } else if (pageDiff <= 1) {
711 // return Process.THREAD_PRIORITY_BACKGROUND;
712 return Process.THREAD_PRIORITY_DEFAULT;
713 } else {
714 // return Process.THREAD_PRIORITY_LOWEST;
715 return Process.THREAD_PRIORITY_DEFAULT;
716 }
717 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700718 private int getSleepForPage(int page) {
719 int pageDiff = Math.abs(page - mCurrentPage) - 1;
720 return Math.max(0, pageDiff * sPageSleepDelay);
721 }
Winson Chungb44b5242011-06-13 11:32:14 -0700722 /**
723 * Creates and executes a new AsyncTask to load a page of widget previews.
724 */
725 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700726 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700727 // Prune all tasks that are no longer needed
728 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
729 while (iter.hasNext()) {
730 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
731 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700732 if ((taskPage == page) ||
733 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700734 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700735 task.cancel(false);
736 iter.remove();
737 } else {
738 task.setThreadPriority(getThreadPriorityForPage(taskPage));
739 }
740 }
741
Winson Chungf314b0e2011-08-16 11:54:27 -0700742 // We introduce a slight delay to order the loading of side pages so that we don't thrash
743 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700744 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700745 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700746 @Override
747 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700748 try {
749 Thread.sleep(sleepMs);
750 } catch (Exception e) {}
751 loadWidgetPreviewsInBackground(task, data);
Winson Chungb44b5242011-06-13 11:32:14 -0700752 }
753 },
754 new AsyncTaskCallback() {
755 @Override
756 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
757 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700758 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700759 if (task.page > getPageCount()) return;
760 if (task.pageContentType != mContentType) return;
761 onSyncWidgetPageItems(data);
762 }
763 });
764
765 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700766 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
767 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700768 t.setThreadPriority(getThreadPriorityForPage(page));
769 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
770 mRunningTasks.add(t);
771 }
772 /**
773 * Creates and executes a new AsyncTask to load the outlines for a page of content.
774 */
775 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
776 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700777 // Prune old tasks for this page
778 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
779 while (iter.hasNext()) {
780 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
781 int taskPage = task.page;
782 if ((taskPage == page) &&
783 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
784 task.cancel(false);
785 iter.remove();
786 }
787 }
788
Winson Chungb44b5242011-06-13 11:32:14 -0700789 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
790 new AsyncTaskCallback() {
791 @Override
792 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
793 // Ensure that this task starts running at the correct priority
794 task.syncThreadPriority();
795
Winson Chung4e076542011-06-23 13:04:10 -0700796 ArrayList<Bitmap> images = data.generatedImages;
797 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700798 int count = srcImages.size();
799 Canvas c = new Canvas();
800 for (int i = 0; i < count && !task.isCancelled(); ++i) {
801 // Before work on each item, ensure that this task is running at the correct
802 // priority
803 task.syncThreadPriority();
804
805 Bitmap b = srcImages.get(i);
806 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
807 Bitmap.Config.ARGB_8888);
808
809 c.setBitmap(outline);
810 c.save();
811 c.drawBitmap(b, 0, 0, null);
812 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700813 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700814
815 images.add(outline);
816 }
817 }
818 },
819 new AsyncTaskCallback() {
820 @Override
821 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
822 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700823 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700824 if (task.page > getPageCount()) return;
825 if (task.pageContentType != mContentType) return;
826 onHolographicPageItemsLoaded(data);
827 }
828 });
829
830 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700831 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700832 new AppsCustomizeAsyncTask(page, mContentType,
833 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700834 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
835 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
836 mRunningTasks.add(t);
837 }
838
Winson Chung785d2eb2011-04-14 16:08:02 -0700839 /*
840 * Widgets PagedView implementation
841 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700842 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700843 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
844 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700845
846 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700847 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700848 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
849 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700851 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700852 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700853 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700854 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700855 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
856 }
857 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
858 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700859 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700860 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700861 c.scale(scaleX, scaleY);
862 Rect oldBounds = d.copyBounds();
863 d.setBounds(x, y, x + w, y + h);
864 d.draw(c);
865 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700866 if (multiplyColor != 0xFFFFFFFF) {
867 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
868 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700869 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700870 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700871 }
Winson Chungd2945262011-06-24 15:22:14 -0700872 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700873 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700874 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700875 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700876 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700877 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700878 }
Winson Chung4e076542011-06-23 13:04:10 -0700879 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700880 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700881
Winson Chung4b576be2011-04-27 17:40:20 -0700882 // Calculate the size of the drawable
883 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
884 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
885 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
886 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
887
888 // Scale down the bitmap to fit the space
889 float widgetPreviewScale = (float) cellWidth / expectedWidth;
890 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
891 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
892
893 // Load the preview image if possible
894 String packageName = info.provider.getPackageName();
895 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700896 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700897 if (info.previewImage != 0) {
898 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
899 if (drawable == null) {
900 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
901 + " for provider: " + info.provider);
902 } else {
903 // Scale down the preview to the dimensions we want
904 int imageWidth = drawable.getIntrinsicWidth();
905 int imageHeight = drawable.getIntrinsicHeight();
906 float aspect = (float) imageWidth / imageHeight;
907 int newWidth = imageWidth;
908 int newHeight = imageHeight;
909 if (aspect > 1f) {
910 newWidth = expectedWidth;
911 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
912 } else {
913 newHeight = expectedHeight;
914 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
915 }
916
Winson Chungb44b5242011-06-13 11:32:14 -0700917 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700918 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700919 }
920 }
921
922 // Generate a preview image if we couldn't load one
923 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700924 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700925 int bitmapWidth;
926 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700927
Winson Chung273c1022011-07-11 13:40:52 -0700928 // Specify the dimensions of the bitmap (since we are using a default preview bg with
929 // the full icon, we only imply the aspect ratio of the widget)
930 if (cellHSpan == cellVSpan) {
931 bitmapWidth = bitmapHeight = cellWidth;
932 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
933 } else if (cellHSpan >= cellVSpan) {
934 bitmapWidth = expectedWidth = cellWidth;
935 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700936 } else {
937 // Note that in vertical widgets, we might not have enough space due to the text
938 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700939 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
940 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700941 }
Winson Chung4b576be2011-04-27 17:40:20 -0700942
Winson Chung273c1022011-07-11 13:40:52 -0700943 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700944 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700945 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700946
947 // Draw the icon in the top left corner
948 try {
949 Drawable icon = null;
950 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
951 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
952
Winson Chungd2945262011-06-24 15:22:14 -0700953 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
954 renderDrawableToBitmap(icon, preview, offset, offset,
955 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700956 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700957 }
Winson Chungb44b5242011-06-13 11:32:14 -0700958 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700959 }
960 public void syncWidgetPages() {
961 // Ensure that we have the right number of pages
962 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700963 int numPages = (int) Math.ceil(mWidgets.size() /
964 (float) (mWidgetCountX * mWidgetCountY));
965 for (int j = 0; j < numPages; ++j) {
966 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
967 mWidgetCountY);
968 setupPage(layout);
969 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 }
971 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700972 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700973 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700974 int contentWidth = mWidgetSpacingLayout.getContentWidth();
975 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700976
Winson Chungd2945262011-06-24 15:22:14 -0700977 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700978 ArrayList<Object> items = new ArrayList<Object>();
979 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700980 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700981 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700982 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700983
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700984 int offset = page * numItemsPerPage;
985 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
986 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700987 }
988
Winson Chungf314b0e2011-08-16 11:54:27 -0700989 if (immediate) {
990 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
991 mWidgetCountX, null, null);
992 loadWidgetPreviewsInBackground(null, data);
993 onSyncWidgetPageItems(data);
994 } else {
995 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
996 }
Adam Cohen22f823d2011-09-01 17:22:18 -0700997 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700998 }
999 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1000 AsyncTaskPageData data) {
1001 if (task != null) {
1002 // Ensure that this task starts running at the correct priority
1003 task.syncThreadPriority();
1004 }
1005
1006 // Load each of the widget/shortcut previews
1007 ArrayList<Object> items = data.items;
1008 ArrayList<Bitmap> images = data.generatedImages;
1009 int count = items.size();
1010 int cellWidth = data.cellWidth;
1011 int cellHeight = data.cellHeight;
1012 for (int i = 0; i < count; ++i) {
1013 if (task != null) {
1014 // Ensure we haven't been cancelled yet
1015 if (task.isCancelled()) break;
1016 // Before work on each item, ensure that this task is running at the correct
1017 // priority
1018 task.syncThreadPriority();
1019 }
1020
1021 Object rawInfo = items.get(i);
1022 if (rawInfo instanceof AppWidgetProviderInfo) {
1023 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001024 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001025 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1026 cellWidth, cellHeight));
1027 } else if (rawInfo instanceof ResolveInfo) {
1028 // Fill in the shortcuts information
1029 ResolveInfo info = (ResolveInfo) rawInfo;
1030 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1031 }
1032 }
Winson Chungb44b5242011-06-13 11:32:14 -07001033 }
1034 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1035 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001036 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -07001037 // Only set the column count once we have items
1038 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -07001039
Winson Chungb44b5242011-06-13 11:32:14 -07001040 ArrayList<Object> items = data.items;
1041 int count = items.size();
1042 int cellWidth = data.cellWidth;
1043 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -07001044 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -07001045 for (int i = 0; i < count; ++i) {
1046 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001047 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -07001048 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1049 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -07001050 if (rawInfo instanceof AppWidgetProviderInfo) {
1051 // Fill in the widget information
1052 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001053 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen61dcf372011-09-13 15:01:58 -07001054 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chung4e076542011-06-23 13:04:10 -07001055 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -07001056 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -07001057 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001058 widget.setTag(createItemInfo);
1059 } else if (rawInfo instanceof ResolveInfo) {
1060 // Fill in the shortcuts information
1061 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkac9d95c52011-08-29 14:03:34 -07001062 createItemInfo = new PendingAddItemInfo();
Winson Chung1ed747a2011-05-03 16:18:34 -07001063 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1064 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1065 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -07001066 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001067 widget.applyFromResolveInfo(mPackageManager, info, preview,
1068 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001069 widget.setTag(createItemInfo);
1070 }
Winson Chung4b576be2011-04-27 17:40:20 -07001071 widget.setOnClickListener(this);
1072 widget.setOnLongClickListener(this);
1073 widget.setOnTouchListener(this);
1074
1075 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -07001076 int ix = i % cellCountX;
1077 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -07001078 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -07001079 GridLayout.spec(iy, GridLayout.LEFT),
1080 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -07001081 lp.width = cellWidth;
1082 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -07001083 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -07001084 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1085 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -07001086 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -07001087 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001088 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001089
1090 invalidate();
1091 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001092
1093 if (mFadeInAdjacentScreens) {
1094 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1095 }
Winson Chungb44b5242011-06-13 11:32:14 -07001096 }
1097 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1098 // Invalidate early to short-circuit children invalidates
1099 invalidate();
1100
1101 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001102 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001103 if (layout instanceof PagedViewCellLayout) {
1104 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1105 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001106 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001107 for (int i = 0; i < count; ++i) {
1108 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001109 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001110 }
1111 } else {
1112 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001113 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001114 for (int i = 0; i < count; ++i) {
1115 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001116 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001117 }
1118 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001119 }
Winson Chung46af2e82011-05-09 16:00:53 -07001120
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 @Override
1122 public void syncPages() {
1123 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001124
1125 // Remove all background asyc tasks if we are loading content anew
1126 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1127 while (iter.hasNext()) {
1128 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1129 task.cancel(false);
1130 iter.remove();
1131 }
1132
Winson Chung785d2eb2011-04-14 16:08:02 -07001133 switch (mContentType) {
1134 case Applications:
1135 syncAppsPages();
1136 break;
1137 case Widgets:
1138 syncWidgetPages();
1139 break;
1140 }
1141 }
1142 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001143 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 switch (mContentType) {
1145 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001146 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001147 break;
1148 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001149 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 break;
1151 }
1152 }
1153
Adam Cohen22f823d2011-09-01 17:22:18 -07001154 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1155 // it is in the z-order. This is important to insure touch events are handled correctly.
1156 View getPageAt(int index) {
1157 return getChildAt(getChildCount() - index - 1);
1158 }
1159
1160 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1161 @Override
1162 protected void screenScrolled(int screenCenter) {
1163 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001164
1165 for (int i = 0; i < getChildCount(); i++) {
1166 View v = getPageAt(i);
1167 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001168 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001169
1170 float interpolatedProgress =
1171 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1172 float scale = (1 - interpolatedProgress) +
1173 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1174 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001175
1176 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1177 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001178
1179 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1180 int pageWidth = v.getMeasuredWidth();
1181 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001182
1183 if (PERFORM_OVERSCROLL_ROTATION) {
1184 if (i == 0 && scrollProgress < 0) {
1185 // Overscroll to the left
1186 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1187 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1188 scale = 1.0f;
1189 alpha = 1.0f;
1190 // On the first page, we don't want the page to have any lateral motion
1191 translationX = getScrollX();
1192 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1193 // Overscroll to the right
1194 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1195 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1196 scale = 1.0f;
1197 alpha = 1.0f;
1198 // On the last page, we don't want the page to have any lateral motion.
1199 translationX = getScrollX() - mMaxScrollX;
1200 } else {
1201 v.setPivotY(pageHeight / 2.0f);
1202 v.setPivotX(pageWidth / 2.0f);
1203 v.setRotationY(0f);
1204 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001205 }
1206
1207 v.setTranslationX(translationX);
1208 v.setScaleX(scale);
1209 v.setScaleY(scale);
1210 v.setAlpha(alpha);
1211 }
1212 }
1213 }
1214
1215 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001216 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001217 }
1218
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 /**
1220 * Used by the parent to get the content width to set the tab bar to
1221 * @return
1222 */
1223 public int getPageContentWidth() {
1224 return mContentWidth;
1225 }
1226
Winson Chungb26f3d62011-06-02 10:49:29 -07001227 @Override
1228 protected void onPageBeginMoving() {
1229 /* TO BE ENABLED LATER
1230 setChildrenDrawnWithCacheEnabled(true);
1231 for (int i = 0; i < getChildCount(); ++i) {
1232 View v = getChildAt(i);
1233 if (v instanceof PagedViewCellLayout) {
1234 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1235 }
1236 }
1237 */
1238 super.onPageBeginMoving();
1239 }
1240
1241 @Override
1242 protected void onPageEndMoving() {
1243 /* TO BE ENABLED LATER
1244 for (int i = 0; i < getChildCount(); ++i) {
1245 View v = getChildAt(i);
1246 if (v instanceof PagedViewCellLayout) {
1247 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1248 }
1249 }
1250 setChildrenDrawnWithCacheEnabled(false);
1251 */
1252 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001253
1254 // We reset the save index when we change pages so that it will be recalculated on next
1255 // rotation
1256 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001257 }
1258
Winson Chung785d2eb2011-04-14 16:08:02 -07001259 /*
1260 * AllAppsView implementation
1261 */
1262 @Override
1263 public void setup(Launcher launcher, DragController dragController) {
1264 mLauncher = launcher;
1265 mDragController = dragController;
1266 }
1267 @Override
1268 public void zoom(float zoom, boolean animate) {
1269 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1270 }
1271 @Override
1272 public boolean isVisible() {
1273 return (getVisibility() == VISIBLE);
1274 }
1275 @Override
1276 public boolean isAnimating() {
1277 return false;
1278 }
1279 @Override
1280 public void setApps(ArrayList<ApplicationInfo> list) {
1281 mApps = list;
1282 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001283
Winson Chung875de7e2011-06-28 14:25:17 -07001284 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1285 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001286 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001287 }
1288 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1289 // We add it in place, in alphabetical order
1290 int count = list.size();
1291 for (int i = 0; i < count; ++i) {
1292 ApplicationInfo info = list.get(i);
1293 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1294 if (index < 0) {
1295 mApps.add(-(index + 1), info);
1296 }
1297 }
1298 }
1299 @Override
1300 public void addApps(ArrayList<ApplicationInfo> list) {
1301 addAppsWithoutInvalidate(list);
1302 invalidatePageData();
1303 }
1304 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1305 ComponentName removeComponent = item.intent.getComponent();
1306 int length = list.size();
1307 for (int i = 0; i < length; ++i) {
1308 ApplicationInfo info = list.get(i);
1309 if (info.intent.getComponent().equals(removeComponent)) {
1310 return i;
1311 }
1312 }
1313 return -1;
1314 }
1315 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1316 // loop through all the apps and remove apps that have the same component
1317 int length = list.size();
1318 for (int i = 0; i < length; ++i) {
1319 ApplicationInfo info = list.get(i);
1320 int removeIndex = findAppByComponent(mApps, info);
1321 if (removeIndex > -1) {
1322 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001323 }
1324 }
1325 }
1326 @Override
1327 public void removeApps(ArrayList<ApplicationInfo> list) {
1328 removeAppsWithoutInvalidate(list);
1329 invalidatePageData();
1330 }
1331 @Override
1332 public void updateApps(ArrayList<ApplicationInfo> list) {
1333 // We remove and re-add the updated applications list because it's properties may have
1334 // changed (ie. the title), and this will ensure that the items will be in their proper
1335 // place in the list.
1336 removeAppsWithoutInvalidate(list);
1337 addAppsWithoutInvalidate(list);
1338 invalidatePageData();
1339 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001340
Winson Chung785d2eb2011-04-14 16:08:02 -07001341 @Override
1342 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001343 if (mContentType != ContentType.Applications) {
1344 // Reset to the first page of the Apps pane
1345 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1346 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001347 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001348 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001349 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001350 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001351 }
1352 @Override
1353 public void dumpState() {
1354 // TODO: Dump information related to current list of Applications, Widgets, etc.
1355 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1356 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1357 }
1358 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001359 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001361 for (Object i: list) {
1362 if (i instanceof AppWidgetProviderInfo) {
1363 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1364 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1365 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1366 + " initialLayout=" + info.initialLayout
1367 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1368 } else if (i instanceof ResolveInfo) {
1369 ResolveInfo info = (ResolveInfo) i;
1370 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1371 + info.icon);
1372 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001373 }
1374 }
1375 @Override
1376 public void surrender() {
1377 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1378 // should stop this now.
1379 }
Winson Chung007c6982011-06-14 13:27:53 -07001380
Winson Chungb44b5242011-06-13 11:32:14 -07001381 /*
1382 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1383 * widget previews in the background with the AsyncTasks.
1384 */
1385 protected int getAssociatedLowerPageBound(int page) {
1386 return Math.max(0, page - 2);
1387 }
1388 protected int getAssociatedUpperPageBound(int page) {
1389 final int count = getChildCount();
1390 return Math.min(page + 2, count - 1);
1391 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001392
1393 @Override
1394 protected String getCurrentPageDescription() {
1395 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1396 int stringId = R.string.default_scroll_format;
1397 switch (mContentType) {
1398 case Applications:
1399 stringId = R.string.apps_customize_apps_scroll_format;
1400 break;
1401 case Widgets:
1402 stringId = R.string.apps_customize_widgets_scroll_format;
1403 break;
1404 }
1405 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1406 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001407}