blob: 949d8726647b2eea72890bb43e6d6c58c3b772f8 [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 Chungf0ea4d32011-06-06 14:27:16 -070034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070036import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.graphics.Rect;
38import 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;
Winson Chungfd3385f2011-06-15 19:51:24 -070049import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070051import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070052
53import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import com.android.launcher2.DropTarget.DragObject;
55
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Iterator;
59import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070060
Winson Chungb44b5242011-06-13 11:32:14 -070061/**
62 * A simple callback interface which also provides the results of the task.
63 */
64interface AsyncTaskCallback {
65 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
66}
Winson Chung4e076542011-06-23 13:04:10 -070067
Winson Chungb44b5242011-06-13 11:32:14 -070068/**
69 * The data needed to perform either of the custom AsyncTasks.
70 */
71class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070072 enum Type {
73 LoadWidgetPreviewData,
74 LoadHolographicIconsData
75 }
76
Winson Chungb44b5242011-06-13 11:32:14 -070077 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
78 AsyncTaskCallback postR) {
79 page = p;
80 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070081 sourceImages = si;
82 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070083 cellWidth = cellHeight = -1;
84 doInBackgroundCallback = bgR;
85 postExecuteCallback = postR;
86 }
Winson Chungd2945262011-06-24 15:22:14 -070087 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070088 AsyncTaskCallback postR) {
89 page = p;
90 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070091 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070092 cellWidth = cw;
93 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070094 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070095 doInBackgroundCallback = bgR;
96 postExecuteCallback = postR;
97 }
98 int page;
99 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700100 ArrayList<Bitmap> sourceImages;
101 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 int cellWidth;
103 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700104 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 AsyncTaskCallback doInBackgroundCallback;
106 AsyncTaskCallback postExecuteCallback;
107}
Winson Chung4e076542011-06-23 13:04:10 -0700108
Winson Chungb44b5242011-06-13 11:32:14 -0700109/**
110 * A generic template for an async task used in AppsCustomize.
111 */
112class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700113 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700114 page = p;
115 pageContentType = t;
116 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700117 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700118 }
119 @Override
120 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
121 if (params.length != 1) return null;
122 // Load each of the widget previews in the background
123 params[0].doInBackgroundCallback.run(this, params[0]);
124 return params[0];
125 }
126 @Override
127 protected void onPostExecute(AsyncTaskPageData result) {
128 // All the widget previews are loaded, so we can just callback to inflate the page
129 result.postExecuteCallback.run(this, result);
130 }
131
132 void setThreadPriority(int p) {
133 threadPriority = p;
134 }
135 void syncThreadPriority() {
136 Process.setThreadPriority(threadPriority);
137 }
138
139 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700140 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 int page;
142 AppsCustomizePagedView.ContentType pageContentType;
143 int threadPriority;
144}
Winson Chungb44b5242011-06-13 11:32:14 -0700145
146/**
147 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
148 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700149public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
150 AllAppsView, View.OnClickListener, DragSource {
151 static final String LOG_TAG = "AppsCustomizePagedView";
152
153 /**
154 * The different content types that this paged view can show.
155 */
156 public enum ContentType {
157 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700158 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700159 }
160
161 // Refs
162 private Launcher mLauncher;
163 private DragController mDragController;
164 private final LayoutInflater mLayoutInflater;
165 private final PackageManager mPackageManager;
166
Winson Chung5afbf7b2011-07-25 11:53:08 -0700167 // Save and Restore
168 private int mSaveInstanceStateItemIndex = -1;
169 private int mRestorePage = -1;
170
Winson Chung785d2eb2011-04-14 16:08:02 -0700171 // Content
172 private ContentType mContentType;
173 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700174 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700175
176 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700177 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700178 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700179 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700180 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700181
182 // Dimens
183 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700184 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700185 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700186 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700187 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700188 private final int mWidgetPreviewIconPaddedDimension;
189 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 private PagedViewCellLayout mWidgetSpacingLayout;
191
Winson Chungb44b5242011-06-13 11:32:14 -0700192 // Previews & outlines
193 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
194 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chungf314b0e2011-08-16 11:54:27 -0700195 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700196
Winson Chung785d2eb2011-04-14 16:08:02 -0700197 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
198 super(context, attrs);
199 mLayoutInflater = LayoutInflater.from(context);
200 mPackageManager = context.getPackageManager();
201 mContentType = ContentType.Applications;
202 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700203 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700204 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700205 mHolographicOutlineHelper = new HolographicOutlineHelper();
206 mCanvas = new Canvas();
207 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700208
209 // Save the default widget preview background
210 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700211 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700212 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
213 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700214
215 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700216 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700217 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
218 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
219 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700220 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700221 mWidgetWidthGap =
222 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
223 mWidgetHeightGap =
224 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700225 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
226 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
227 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700228 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700229
230 // The max widget span is the length N, such that NxN is the largest bounds that the widget
231 // preview can be before applying the widget scaling
232 mMinWidgetSpan = 1;
233 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700234
235 // The padding on the non-matched dimension for the default widget preview icons
236 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700237 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700238 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700239 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700240 }
241
242 @Override
243 protected void init() {
244 super.init();
245 mCenterPagesVertically = false;
246
247 Context context = getContext();
248 Resources r = context.getResources();
249 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
250 }
251
Winson Chungf0ea4d32011-06-06 14:27:16 -0700252 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700253 protected void onUnhandledTap(MotionEvent ev) {
254 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700255 // Dismiss AppsCustomize if we tap
256 mLauncher.showWorkspace(true);
257 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700258 }
259
Winson Chung5afbf7b2011-07-25 11:53:08 -0700260 /** Returns the item index of the center item on this page so that we can restore to this
261 * item index when we rotate. */
262 private int getMiddleComponentIndexOnCurrentPage() {
263 int i = -1;
264 if (getPageCount() > 0) {
265 int currentPage = getCurrentPage();
266 switch (mContentType) {
267 case Applications: {
268 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(currentPage);
269 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
270 int numItemsPerPage = mCellCountX * mCellCountY;
271 int childCount = childrenLayout.getChildCount();
272 if (childCount > 0) {
273 i = (currentPage * numItemsPerPage) + (childCount / 2);
274 }}
275 break;
276 case Widgets: {
277 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(currentPage);
278 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
279 int childCount = layout.getChildCount();
280 if (childCount > 0) {
281 i = (currentPage * numItemsPerPage) + (childCount / 2);
282 }}
283 break;
284 }
285 }
286 return i;
287 }
288
289 /** Get the index of the item to restore to if we need to restore the current page. */
290 int getSaveInstanceStateIndex() {
291 if (mSaveInstanceStateItemIndex == -1) {
292 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
293 }
294 return mSaveInstanceStateItemIndex;
295 }
296
297 /** Returns the page in the current orientation which is expected to contain the specified
298 * item index. */
299 int getPageForComponent(int index) {
300 switch (mContentType) {
301 case Applications: {
302 int numItemsPerPage = mCellCountX * mCellCountY;
303 return (index / numItemsPerPage);
304 }
305 case Widgets: {
306 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
307 return (index / numItemsPerPage);
308 }}
309 return -1;
310 }
311
Winson Chungf0ea4d32011-06-06 14:27:16 -0700312 /**
313 * This differs from isDataReady as this is the test done if isDataReady is not set.
314 */
315 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700316 // We only do this test once, and we default to the Applications page, so we only really
317 // have to wait for there to be apps.
318 return !mApps.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700319 }
320
Winson Chung5afbf7b2011-07-25 11:53:08 -0700321 /** Restores the page for an item at the specified index */
322 void restorePageForIndex(int index) {
323 if (index < 0) return;
324
325 int page = getPageForComponent(index);
326 if (page > -1) {
327 if (getChildCount() > 0) {
328 invalidatePageData(page);
329 } else {
330 mRestorePage = page;
331 }
332 }
333 }
334
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 protected void onDataReady(int width, int height) {
336 // Note that we transpose the counts in portrait so that we get a similar layout
337 boolean isLandscape = getResources().getConfiguration().orientation ==
338 Configuration.ORIENTATION_LANDSCAPE;
339 int maxCellCountX = Integer.MAX_VALUE;
340 int maxCellCountY = Integer.MAX_VALUE;
341 if (LauncherApplication.isScreenLarge()) {
342 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
343 LauncherModel.getCellCountY());
344 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
345 LauncherModel.getCellCountX());
346 }
347
348 // Now that the data is ready, we can calculate the content width, the number of cells to
349 // use for each page
350 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
351 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
352 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
353 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
354 mCellCountX = mWidgetSpacingLayout.getCellCountX();
355 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung5a808352011-06-27 19:08:49 -0700356
Winson Chungdb1138b2011-06-30 14:39:35 -0700357 // Force a measure to update recalculate the gaps
358 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
359 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
360 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700361 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700362 invalidatePageData(Math.max(0, mRestorePage));
363 mRestorePage = -1;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700364 }
365
366 @Override
367 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
368 int width = MeasureSpec.getSize(widthMeasureSpec);
369 int height = MeasureSpec.getSize(heightMeasureSpec);
370 if (!isDataReady()) {
371 if (testDataReady()) {
372 setDataIsReady();
373 setMeasuredDimension(width, height);
374 onDataReady(width, height);
375 }
376 }
377
378 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
379 }
380
Winson Chung34efdaf2011-05-24 14:19:56 -0700381 /** Removes and returns the ResolveInfo with the specified ComponentName */
382 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
383 ComponentName cn) {
384 Iterator<ResolveInfo> iter = list.iterator();
385 while (iter.hasNext()) {
386 ResolveInfo rinfo = iter.next();
387 ActivityInfo info = rinfo.activityInfo;
388 ComponentName c = new ComponentName(info.packageName, info.name);
389 if (c.equals(cn)) {
390 iter.remove();
391 return rinfo;
392 }
393 }
394 return null;
395 }
396
Winson Chung785d2eb2011-04-14 16:08:02 -0700397 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700398 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
399 // by a broadcast receiver, and in order for it to work correctly, we need to know that
400 // the AppWidgetService has already received and processed the same broadcast. Since there
401 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
402 // we should have a more precise way of ensuring the AppWidgetService is up to date.
403 postDelayed(new Runnable() {
404 public void run() {
405 updatePackages();
406 }
407 }, 500);
408 }
409
410 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700411 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700412 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700413 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700414 List<AppWidgetProviderInfo> widgets =
415 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700416 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
417 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700418 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700419 mWidgets.addAll(shortcuts);
420 Collections.sort(mWidgets,
421 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700422
Winson Chung875de7e2011-06-28 14:25:17 -0700423 if (wasEmpty) {
424 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
425 // a layout to do this test and invalidate the page data when ready.
426 if (testDataReady()) requestLayout();
427 } else {
428 invalidatePageData();
429 }
Winson Chung4b576be2011-04-27 17:40:20 -0700430 }
431
432 @Override
433 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700434 // When we have exited all apps or are in transition, disregard clicks
435 if (!mLauncher.isAllAppsCustomizeOpen() ||
436 mLauncher.getWorkspace().isSwitchingState()) return;
437
Winson Chung4b576be2011-04-27 17:40:20 -0700438 if (v instanceof PagedViewIcon) {
439 // Animate some feedback to the click
440 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
441 animateClickFeedback(v, new Runnable() {
442 @Override
443 public void run() {
444 mLauncher.startActivitySafely(appInfo.intent, appInfo);
445 }
446 });
447 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700448 // Let the user know that they have to long press to add a widget
449 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
450 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700451
Winson Chungd2e87b32011-06-02 10:53:07 -0700452 // Create a little animation to show that the widget can move
453 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
454 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
455 AnimatorSet bounce = new AnimatorSet();
456 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
457 tyuAnim.setDuration(125);
458 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
459 tydAnim.setDuration(100);
460 bounce.play(tyuAnim).before(tydAnim);
461 bounce.setInterpolator(new AccelerateInterpolator());
462 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700463 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 }
465
466 /*
467 * PagedViewWithDraggableItems implementation
468 */
469 @Override
470 protected void determineDraggingStart(android.view.MotionEvent ev) {
471 // Disable dragging by pulling an app down for now.
472 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700473
Winson Chung4b576be2011-04-27 17:40:20 -0700474 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700475 mLauncher.getWorkspace().onDragStartedWithItem(v);
476 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700477 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700478
Winson Chung4b576be2011-04-27 17:40:20 -0700479 private void beginDraggingWidget(View v) {
480 // Get the widget preview as the drag representation
481 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700482 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700483
484 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700485 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700486 Drawable preview = image.getDrawable();
487 int w = preview.getIntrinsicWidth();
488 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700489 if (createItemInfo instanceof PendingAddWidgetInfo) {
490 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
491 int[] spanXY = CellLayout.rectToCell(getResources(),
492 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
493 createItemInfo.spanX = spanXY[0];
494 createItemInfo.spanY = spanXY[1];
495
496 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung70fc4382011-08-08 15:31:33 -0700497 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700498 } else {
499 // Workaround for the fact that we don't keep the original ResolveInfo associated with
500 // the shortcut around. To get the icon, we just render the preview image (which has
501 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
502 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
503 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700504 mCanvas.setBitmap(b);
505 mCanvas.save();
506 preview.draw(mCanvas);
507 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700508 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700509 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700510 createItemInfo.spanX = createItemInfo.spanY = 1;
511 }
Winson Chung4b576be2011-04-27 17:40:20 -0700512
513 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700514 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700515 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
516 createItemInfo.spanY, b);
517 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700518 DragController.DRAG_ACTION_COPY, null);
519 b.recycle();
520 }
521 @Override
522 protected boolean beginDragging(View v) {
523 if (!super.beginDragging(v)) return false;
524
Winson Chungc07918d2011-07-01 15:35:26 -0700525 // Go into spring loaded mode (must happen before we startDrag())
526 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage();
527 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex);
528 mLauncher.enterSpringLoadedDragMode(currentPage);
Winson Chungfc79c802011-05-02 13:35:34 -0700529
Winson Chung4b576be2011-04-27 17:40:20 -0700530 if (v instanceof PagedViewIcon) {
531 beginDraggingApplication(v);
532 } else if (v instanceof PagedViewWidget) {
533 beginDraggingWidget(v);
534 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700535 return true;
536 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700537 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700538 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700539 if (!success || (target != mLauncher.getWorkspace() &&
540 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700541 // Exit spring loaded mode if we have not successfully dropped or have not handled the
542 // drop in Workspace
543 mLauncher.exitSpringLoadedDragMode();
544 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700545 mLauncher.unlockScreenOrientation();
Winson Chungb26f3d62011-06-02 10:49:29 -0700546
Winson Chung785d2eb2011-04-14 16:08:02 -0700547 }
548
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700550 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700551 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700552
553 // Display an error message if the drag failed due to there not being enough space on the
554 // target layout we were dropping on.
555 if (!success) {
556 boolean showOutOfSpaceMessage = false;
557 if (target instanceof Workspace) {
558 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
559 Workspace workspace = (Workspace) target;
560 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700561 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700562 if (layout != null) {
563 layout.calculateSpans(itemInfo);
564 showOutOfSpaceMessage =
565 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
566 }
567 }
568 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
569 if (showOutOfSpaceMessage) {
570 mLauncher.showOutOfSpaceMessage();
571 }
572 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700573 }
574
575 public void setContentType(ContentType type) {
576 mContentType = type;
Winson Chungf314b0e2011-08-16 11:54:27 -0700577 invalidatePageData(0, true);
Winson Chung785d2eb2011-04-14 16:08:02 -0700578 }
579
Winson Chungb44b5242011-06-13 11:32:14 -0700580 public boolean isContentType(ContentType type) {
581 return (mContentType == type);
582 }
583
Winson Chung5a808352011-06-27 19:08:49 -0700584 public void setCurrentPageToWidgets() {
585 invalidatePageData(0);
586 }
Winson Chung5a808352011-06-27 19:08:49 -0700587
Winson Chung785d2eb2011-04-14 16:08:02 -0700588 /*
589 * Apps PagedView implementation
590 */
Winson Chung63257c12011-05-05 17:06:13 -0700591 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
592 int childCount = layout.getChildCount();
593 for (int i = 0; i < childCount; ++i) {
594 layout.getChildAt(i).setVisibility(visibility);
595 }
596 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700597 private void setupPage(PagedViewCellLayout layout) {
598 layout.setCellCount(mCellCountX, mCellCountY);
599 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
600 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
601 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
602
Winson Chung63257c12011-05-05 17:06:13 -0700603 // Note: We force a measure here to get around the fact that when we do layout calculations
604 // immediately after syncing, we don't have a proper width. That said, we already know the
605 // expected page width, so we can actually optimize by hiding all the TextView-based
606 // children that are expensive to measure, and let that happen naturally later.
607 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700608 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700609 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700610 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700611 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700612 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700613 }
614 public void syncAppsPages() {
615 // Ensure that we have the right number of pages
616 Context context = getContext();
617 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
618 for (int i = 0; i < numPages; ++i) {
619 PagedViewCellLayout layout = new PagedViewCellLayout(context);
620 setupPage(layout);
621 addView(layout);
622 }
623 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700624 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700625 // ensure that we have the right number of items on the pages
626 int numPages = getPageCount();
627 int numCells = mCellCountX * mCellCountY;
628 int startIndex = page * numCells;
629 int endIndex = Math.min(startIndex + numCells, mApps.size());
630 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700631
Winson Chung785d2eb2011-04-14 16:08:02 -0700632 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700633 ArrayList<Object> items = new ArrayList<Object>();
634 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700635 for (int i = startIndex; i < endIndex; ++i) {
636 ApplicationInfo info = mApps.get(i);
637 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
638 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700639 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700640 icon.setOnClickListener(this);
641 icon.setOnLongClickListener(this);
642 icon.setOnTouchListener(this);
643
644 int index = i - startIndex;
645 int x = index % mCellCountX;
646 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700647 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700648
649 items.add(info);
650 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700651 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700652
653 // Create the hardware layers
654 layout.allowHardwareLayerCreation();
655 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700656
Winson Chung54c725c2011-08-03 12:03:40 -0700657 if (mFadeInAdjacentScreens) {
658 prepareGenerateHoloOutlinesTask(page, items, images);
659 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700660 }
Winson Chungb44b5242011-06-13 11:32:14 -0700661
662 /**
663 * Return the appropriate thread priority for loading for a given page (we give the current
664 * page much higher priority)
665 */
666 private int getThreadPriorityForPage(int page) {
667 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
668 int pageDiff = Math.abs(page - mCurrentPage);
669 if (pageDiff <= 0) {
670 // return Process.THREAD_PRIORITY_DEFAULT;
671 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
672 } else if (pageDiff <= 1) {
673 // return Process.THREAD_PRIORITY_BACKGROUND;
674 return Process.THREAD_PRIORITY_DEFAULT;
675 } else {
676 // return Process.THREAD_PRIORITY_LOWEST;
677 return Process.THREAD_PRIORITY_DEFAULT;
678 }
679 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700680 private int getSleepForPage(int page) {
681 int pageDiff = Math.abs(page - mCurrentPage) - 1;
682 return Math.max(0, pageDiff * sPageSleepDelay);
683 }
Winson Chungb44b5242011-06-13 11:32:14 -0700684 /**
685 * Creates and executes a new AsyncTask to load a page of widget previews.
686 */
687 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700688 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700689 // Prune all tasks that are no longer needed
690 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
691 while (iter.hasNext()) {
692 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
693 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700694 if ((taskPage == page) ||
695 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700696 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700697 task.cancel(false);
698 iter.remove();
699 } else {
700 task.setThreadPriority(getThreadPriorityForPage(taskPage));
701 }
702 }
703
Winson Chungf314b0e2011-08-16 11:54:27 -0700704 // We introduce a slight delay to order the loading of side pages so that we don't thrash
705 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700706 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700707 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700708 @Override
709 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700710 try {
711 Thread.sleep(sleepMs);
712 } catch (Exception e) {}
713 loadWidgetPreviewsInBackground(task, data);
Winson Chungb44b5242011-06-13 11:32:14 -0700714 }
715 },
716 new AsyncTaskCallback() {
717 @Override
718 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
719 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700720 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700721 if (task.page > getPageCount()) return;
722 if (task.pageContentType != mContentType) return;
723 onSyncWidgetPageItems(data);
724 }
725 });
726
727 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700728 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
729 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700730 t.setThreadPriority(getThreadPriorityForPage(page));
731 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
732 mRunningTasks.add(t);
733 }
734 /**
735 * Creates and executes a new AsyncTask to load the outlines for a page of content.
736 */
737 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
738 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700739 // Prune old tasks for this page
740 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
741 while (iter.hasNext()) {
742 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
743 int taskPage = task.page;
744 if ((taskPage == page) &&
745 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
746 task.cancel(false);
747 iter.remove();
748 }
749 }
750
Winson Chungb44b5242011-06-13 11:32:14 -0700751 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
752 new AsyncTaskCallback() {
753 @Override
754 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
755 // Ensure that this task starts running at the correct priority
756 task.syncThreadPriority();
757
Winson Chung4e076542011-06-23 13:04:10 -0700758 ArrayList<Bitmap> images = data.generatedImages;
759 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700760 int count = srcImages.size();
761 Canvas c = new Canvas();
762 for (int i = 0; i < count && !task.isCancelled(); ++i) {
763 // Before work on each item, ensure that this task is running at the correct
764 // priority
765 task.syncThreadPriority();
766
767 Bitmap b = srcImages.get(i);
768 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
769 Bitmap.Config.ARGB_8888);
770
771 c.setBitmap(outline);
772 c.save();
773 c.drawBitmap(b, 0, 0, null);
774 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700775 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700776
777 images.add(outline);
778 }
779 }
780 },
781 new AsyncTaskCallback() {
782 @Override
783 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
784 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700785 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700786 if (task.page > getPageCount()) return;
787 if (task.pageContentType != mContentType) return;
788 onHolographicPageItemsLoaded(data);
789 }
790 });
791
792 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700793 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700794 new AppsCustomizeAsyncTask(page, mContentType,
795 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700796 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
797 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
798 mRunningTasks.add(t);
799 }
800
Winson Chung785d2eb2011-04-14 16:08:02 -0700801 /*
802 * Widgets PagedView implementation
803 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700804 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700805 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
806 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700807
808 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700809 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700810 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
811 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700813 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700815 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700816 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700817 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
818 }
819 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
820 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700821 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700822 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700823 c.scale(scaleX, scaleY);
824 Rect oldBounds = d.copyBounds();
825 d.setBounds(x, y, x + w, y + h);
826 d.draw(c);
827 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700828 if (multiplyColor != 0xFFFFFFFF) {
829 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
830 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700831 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700832 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 }
Winson Chungd2945262011-06-24 15:22:14 -0700834 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700835 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700836 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700837 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700838 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700839 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700840 }
Winson Chung4e076542011-06-23 13:04:10 -0700841 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700842 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700843
Winson Chung4b576be2011-04-27 17:40:20 -0700844 // Calculate the size of the drawable
845 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
846 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
847 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
848 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
849
850 // Scale down the bitmap to fit the space
851 float widgetPreviewScale = (float) cellWidth / expectedWidth;
852 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
853 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
854
855 // Load the preview image if possible
856 String packageName = info.provider.getPackageName();
857 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700858 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700859 if (info.previewImage != 0) {
860 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
861 if (drawable == null) {
862 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
863 + " for provider: " + info.provider);
864 } else {
865 // Scale down the preview to the dimensions we want
866 int imageWidth = drawable.getIntrinsicWidth();
867 int imageHeight = drawable.getIntrinsicHeight();
868 float aspect = (float) imageWidth / imageHeight;
869 int newWidth = imageWidth;
870 int newHeight = imageHeight;
871 if (aspect > 1f) {
872 newWidth = expectedWidth;
873 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
874 } else {
875 newHeight = expectedHeight;
876 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
877 }
878
Winson Chungb44b5242011-06-13 11:32:14 -0700879 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700880 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700881 }
882 }
883
884 // Generate a preview image if we couldn't load one
885 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700886 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700887 int bitmapWidth;
888 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700889
Winson Chung273c1022011-07-11 13:40:52 -0700890 // Specify the dimensions of the bitmap (since we are using a default preview bg with
891 // the full icon, we only imply the aspect ratio of the widget)
892 if (cellHSpan == cellVSpan) {
893 bitmapWidth = bitmapHeight = cellWidth;
894 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
895 } else if (cellHSpan >= cellVSpan) {
896 bitmapWidth = expectedWidth = cellWidth;
897 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700898 } else {
899 // Note that in vertical widgets, we might not have enough space due to the text
900 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700901 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
902 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700903 }
Winson Chung4b576be2011-04-27 17:40:20 -0700904
Winson Chung273c1022011-07-11 13:40:52 -0700905 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700906 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700907 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700908
909 // Draw the icon in the top left corner
910 try {
911 Drawable icon = null;
912 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
913 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
914
Winson Chungd2945262011-06-24 15:22:14 -0700915 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
916 renderDrawableToBitmap(icon, preview, offset, offset,
917 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700918 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700919 }
Winson Chungb44b5242011-06-13 11:32:14 -0700920 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700921 }
922 public void syncWidgetPages() {
923 // Ensure that we have the right number of pages
924 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700925 int numPages = (int) Math.ceil(mWidgets.size() /
926 (float) (mWidgetCountX * mWidgetCountY));
927 for (int j = 0; j < numPages; ++j) {
928 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
929 mWidgetCountY);
930 setupPage(layout);
931 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700932 }
933 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700934 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700935 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700936 int contentWidth = mWidgetSpacingLayout.getContentWidth();
937 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700938
Winson Chungd2945262011-06-24 15:22:14 -0700939 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700940 ArrayList<Object> items = new ArrayList<Object>();
941 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700942 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700943 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700944 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700945
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700946 int offset = page * numItemsPerPage;
947 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
948 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700949 }
950
Winson Chungf314b0e2011-08-16 11:54:27 -0700951 if (immediate) {
952 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
953 mWidgetCountX, null, null);
954 loadWidgetPreviewsInBackground(null, data);
955 onSyncWidgetPageItems(data);
956 } else {
957 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
958 }
959 }
960 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
961 AsyncTaskPageData data) {
962 if (task != null) {
963 // Ensure that this task starts running at the correct priority
964 task.syncThreadPriority();
965 }
966
967 // Load each of the widget/shortcut previews
968 ArrayList<Object> items = data.items;
969 ArrayList<Bitmap> images = data.generatedImages;
970 int count = items.size();
971 int cellWidth = data.cellWidth;
972 int cellHeight = data.cellHeight;
973 for (int i = 0; i < count; ++i) {
974 if (task != null) {
975 // Ensure we haven't been cancelled yet
976 if (task.isCancelled()) break;
977 // Before work on each item, ensure that this task is running at the correct
978 // priority
979 task.syncThreadPriority();
980 }
981
982 Object rawInfo = items.get(i);
983 if (rawInfo instanceof AppWidgetProviderInfo) {
984 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
985 int[] cellSpans = CellLayout.rectToCell(getResources(),
986 info.minWidth, info.minHeight, null);
987 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
988 cellWidth, cellHeight));
989 } else if (rawInfo instanceof ResolveInfo) {
990 // Fill in the shortcuts information
991 ResolveInfo info = (ResolveInfo) rawInfo;
992 images.add(getShortcutPreview(info, cellWidth, cellHeight));
993 }
994 }
Winson Chungb44b5242011-06-13 11:32:14 -0700995 }
996 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
997 int page = data.page;
Winson Chung4e6a9762011-05-09 11:56:34 -0700998 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -0700999 // Only set the column count once we have items
1000 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -07001001
Winson Chungb44b5242011-06-13 11:32:14 -07001002 ArrayList<Object> items = data.items;
1003 int count = items.size();
1004 int cellWidth = data.cellWidth;
1005 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -07001006 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -07001007 for (int i = 0; i < count; ++i) {
1008 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001009 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -07001010 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1011 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -07001012 if (rawInfo instanceof AppWidgetProviderInfo) {
1013 // Fill in the widget information
1014 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1015 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungb44b5242011-06-13 11:32:14 -07001016 int[] cellSpans = CellLayout.rectToCell(getResources(),
1017 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -07001018 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -07001019 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -07001020 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001021 widget.setTag(createItemInfo);
1022 } else if (rawInfo instanceof ResolveInfo) {
1023 // Fill in the shortcuts information
1024 ResolveInfo info = (ResolveInfo) rawInfo;
1025 createItemInfo = new PendingAddItemInfo();
1026 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1027 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1028 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -07001029 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001030 widget.applyFromResolveInfo(mPackageManager, info, preview,
1031 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -07001032 widget.setTag(createItemInfo);
1033 }
Winson Chung4b576be2011-04-27 17:40:20 -07001034 widget.setOnClickListener(this);
1035 widget.setOnLongClickListener(this);
1036 widget.setOnTouchListener(this);
1037
1038 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -07001039 int ix = i % cellCountX;
1040 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -07001041 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -07001042 GridLayout.spec(iy, GridLayout.LEFT),
1043 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -07001044 lp.width = cellWidth;
1045 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -07001046 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -07001047 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1048 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -07001049 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -07001050 }
Winson Chungb44b5242011-06-13 11:32:14 -07001051
1052 invalidate();
1053 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001054
1055 if (mFadeInAdjacentScreens) {
1056 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1057 }
Winson Chungb44b5242011-06-13 11:32:14 -07001058 }
1059 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1060 // Invalidate early to short-circuit children invalidates
1061 invalidate();
1062
1063 int page = data.page;
1064 ViewGroup layout = (ViewGroup) getChildAt(page);
1065 if (layout instanceof PagedViewCellLayout) {
1066 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1067 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001068 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001069 for (int i = 0; i < count; ++i) {
1070 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001071 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001072 }
1073 } else {
1074 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001075 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001076 for (int i = 0; i < count; ++i) {
1077 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001078 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001079 }
1080 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001081 }
Winson Chung46af2e82011-05-09 16:00:53 -07001082
Winson Chung785d2eb2011-04-14 16:08:02 -07001083 @Override
1084 public void syncPages() {
1085 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -07001086
1087 // Remove all background asyc tasks if we are loading content anew
1088 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1089 while (iter.hasNext()) {
1090 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1091 task.cancel(false);
1092 iter.remove();
1093 }
1094
Winson Chung785d2eb2011-04-14 16:08:02 -07001095 switch (mContentType) {
1096 case Applications:
1097 syncAppsPages();
1098 break;
1099 case Widgets:
1100 syncWidgetPages();
1101 break;
1102 }
1103 }
1104 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001105 public void syncPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001106 switch (mContentType) {
1107 case Applications:
Winson Chungf314b0e2011-08-16 11:54:27 -07001108 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001109 break;
1110 case Widgets:
Winson Chungf314b0e2011-08-16 11:54:27 -07001111 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001112 break;
1113 }
1114 }
1115
1116 /**
1117 * Used by the parent to get the content width to set the tab bar to
1118 * @return
1119 */
1120 public int getPageContentWidth() {
1121 return mContentWidth;
1122 }
1123
Winson Chungb26f3d62011-06-02 10:49:29 -07001124 @Override
1125 protected void onPageBeginMoving() {
1126 /* TO BE ENABLED LATER
1127 setChildrenDrawnWithCacheEnabled(true);
1128 for (int i = 0; i < getChildCount(); ++i) {
1129 View v = getChildAt(i);
1130 if (v instanceof PagedViewCellLayout) {
1131 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1132 }
1133 }
1134 */
1135 super.onPageBeginMoving();
1136 }
1137
1138 @Override
1139 protected void onPageEndMoving() {
1140 /* TO BE ENABLED LATER
1141 for (int i = 0; i < getChildCount(); ++i) {
1142 View v = getChildAt(i);
1143 if (v instanceof PagedViewCellLayout) {
1144 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1145 }
1146 }
1147 setChildrenDrawnWithCacheEnabled(false);
1148 */
1149 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001150
1151 // We reset the save index when we change pages so that it will be recalculated on next
1152 // rotation
1153 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001154 }
1155
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 /*
1157 * AllAppsView implementation
1158 */
1159 @Override
1160 public void setup(Launcher launcher, DragController dragController) {
1161 mLauncher = launcher;
1162 mDragController = dragController;
1163 }
1164 @Override
1165 public void zoom(float zoom, boolean animate) {
1166 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1167 }
1168 @Override
1169 public boolean isVisible() {
1170 return (getVisibility() == VISIBLE);
1171 }
1172 @Override
1173 public boolean isAnimating() {
1174 return false;
1175 }
1176 @Override
1177 public void setApps(ArrayList<ApplicationInfo> list) {
1178 mApps = list;
1179 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001180
Winson Chung875de7e2011-06-28 14:25:17 -07001181 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1182 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001183 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001184 }
1185 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1186 // We add it in place, in alphabetical order
1187 int count = list.size();
1188 for (int i = 0; i < count; ++i) {
1189 ApplicationInfo info = list.get(i);
1190 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1191 if (index < 0) {
1192 mApps.add(-(index + 1), info);
1193 }
1194 }
1195 }
1196 @Override
1197 public void addApps(ArrayList<ApplicationInfo> list) {
1198 addAppsWithoutInvalidate(list);
1199 invalidatePageData();
1200 }
1201 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1202 ComponentName removeComponent = item.intent.getComponent();
1203 int length = list.size();
1204 for (int i = 0; i < length; ++i) {
1205 ApplicationInfo info = list.get(i);
1206 if (info.intent.getComponent().equals(removeComponent)) {
1207 return i;
1208 }
1209 }
1210 return -1;
1211 }
1212 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1213 // loop through all the apps and remove apps that have the same component
1214 int length = list.size();
1215 for (int i = 0; i < length; ++i) {
1216 ApplicationInfo info = list.get(i);
1217 int removeIndex = findAppByComponent(mApps, info);
1218 if (removeIndex > -1) {
1219 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 }
1221 }
1222 }
1223 @Override
1224 public void removeApps(ArrayList<ApplicationInfo> list) {
1225 removeAppsWithoutInvalidate(list);
1226 invalidatePageData();
1227 }
1228 @Override
1229 public void updateApps(ArrayList<ApplicationInfo> list) {
1230 // We remove and re-add the updated applications list because it's properties may have
1231 // changed (ie. the title), and this will ensure that the items will be in their proper
1232 // place in the list.
1233 removeAppsWithoutInvalidate(list);
1234 addAppsWithoutInvalidate(list);
1235 invalidatePageData();
1236 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001237
Winson Chung785d2eb2011-04-14 16:08:02 -07001238 @Override
1239 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001240 if (mContentType != ContentType.Applications) {
1241 // Reset to the first page of the Apps pane
1242 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1243 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001244 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001245 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001246 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001247 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001248 }
1249 @Override
1250 public void dumpState() {
1251 // TODO: Dump information related to current list of Applications, Widgets, etc.
1252 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1253 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1254 }
1255 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001256 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001258 for (Object i: list) {
1259 if (i instanceof AppWidgetProviderInfo) {
1260 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1261 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1262 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1263 + " initialLayout=" + info.initialLayout
1264 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1265 } else if (i instanceof ResolveInfo) {
1266 ResolveInfo info = (ResolveInfo) i;
1267 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1268 + info.icon);
1269 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 }
1271 }
1272 @Override
1273 public void surrender() {
1274 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1275 // should stop this now.
1276 }
Winson Chung007c6982011-06-14 13:27:53 -07001277
Winson Chungb44b5242011-06-13 11:32:14 -07001278 /*
1279 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1280 * widget previews in the background with the AsyncTasks.
1281 */
1282 protected int getAssociatedLowerPageBound(int page) {
1283 return Math.max(0, page - 2);
1284 }
1285 protected int getAssociatedUpperPageBound(int page) {
1286 final int count = getChildCount();
1287 return Math.min(page + 2, count - 1);
1288 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001289
1290 @Override
1291 protected String getCurrentPageDescription() {
1292 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1293 int stringId = R.string.default_scroll_format;
1294 switch (mContentType) {
1295 case Applications:
1296 stringId = R.string.apps_customize_apps_scroll_format;
1297 break;
1298 case Widgets:
1299 stringId = R.string.apps_customize_widgets_scroll_format;
1300 break;
1301 }
1302 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1303 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001304}