blob: ed9d0cffa3c74b7fbeb68f4b5e0adf9e21197df3 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
Winson Chung46af2e82011-05-09 16:00:53 -070027import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Winson Chung5fc72b32011-10-11 17:53:58 -070034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070036import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.graphics.Rect;
Winson Chung5fc72b32011-10-11 17:53:58 -070038import android.graphics.RectF;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070040import android.os.AsyncTask;
41import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.util.AttributeSet;
43import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070044import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070046import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070048import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070049import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
54import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import com.android.launcher2.DropTarget.DragObject;
56
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
74 LoadWidgetPreviewData,
75 LoadHolographicIconsData
76 }
77
Winson Chungb44b5242011-06-13 11:32:14 -070078 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
79 AsyncTaskCallback postR) {
80 page = p;
81 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070082 sourceImages = si;
83 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070084 cellWidth = cellHeight = -1;
85 doInBackgroundCallback = bgR;
86 postExecuteCallback = postR;
87 }
Winson Chungd2945262011-06-24 15:22:14 -070088 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070089 AsyncTaskCallback postR) {
90 page = p;
91 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070092 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070093 cellWidth = cw;
94 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070095 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070096 doInBackgroundCallback = bgR;
97 postExecuteCallback = postR;
98 }
Winson Chung09945932011-09-20 14:22:40 -070099 void cleanup(boolean cancelled) {
100 // Clean up any references to source/generated bitmaps
101 if (sourceImages != null) {
102 if (cancelled) {
103 for (Bitmap b : sourceImages) {
104 b.recycle();
105 }
106 }
107 sourceImages.clear();
108 }
109 if (generatedImages != null) {
110 if (cancelled) {
111 for (Bitmap b : generatedImages) {
112 b.recycle();
113 }
114 }
115 generatedImages.clear();
116 }
117 }
Winson Chungb44b5242011-06-13 11:32:14 -0700118 int page;
119 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700120 ArrayList<Bitmap> sourceImages;
121 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700122 int cellWidth;
123 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700124 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700125 AsyncTaskCallback doInBackgroundCallback;
126 AsyncTaskCallback postExecuteCallback;
127}
Winson Chung4e076542011-06-23 13:04:10 -0700128
Winson Chungb44b5242011-06-13 11:32:14 -0700129/**
130 * A generic template for an async task used in AppsCustomize.
131 */
132class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700133 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700134 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700135 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700136 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700137 }
138 @Override
139 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
140 if (params.length != 1) return null;
141 // Load each of the widget previews in the background
142 params[0].doInBackgroundCallback.run(this, params[0]);
143 return params[0];
144 }
145 @Override
146 protected void onPostExecute(AsyncTaskPageData result) {
147 // All the widget previews are loaded, so we can just callback to inflate the page
148 result.postExecuteCallback.run(this, result);
149 }
150
151 void setThreadPriority(int p) {
152 threadPriority = p;
153 }
154 void syncThreadPriority() {
155 Process.setThreadPriority(threadPriority);
156 }
157
158 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700159 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700160 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700161 int threadPriority;
162}
Winson Chungb44b5242011-06-13 11:32:14 -0700163
164/**
165 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
166 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700167public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
168 AllAppsView, View.OnClickListener, DragSource {
169 static final String LOG_TAG = "AppsCustomizePagedView";
170
171 /**
172 * The different content types that this paged view can show.
173 */
174 public enum ContentType {
175 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700176 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700177 }
178
179 // Refs
180 private Launcher mLauncher;
181 private DragController mDragController;
182 private final LayoutInflater mLayoutInflater;
183 private final PackageManager mPackageManager;
184
Winson Chung5afbf7b2011-07-25 11:53:08 -0700185 // Save and Restore
186 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700187
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700189 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700190 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700191
Winson Chung7d7541e2011-09-16 20:14:36 -0700192 // Cling
193 private int mClingFocusedX;
194 private int mClingFocusedY;
195
Winson Chung1ed747a2011-05-03 16:18:34 -0700196 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700197 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700198 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700199 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700200 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700201
202 // Dimens
203 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700204 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700205 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700206 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700207 private final int mWidgetPreviewIconPaddedDimension;
208 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700209 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700210 private int mNumAppsPages;
211 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700212
Adam Cohen22f823d2011-09-01 17:22:18 -0700213 // Relating to the scroll and overscroll effects
214 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700215 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700216 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700217 private static float TRANSITION_PIVOT = 0.65f;
218 private static float TRANSITION_MAX_ROTATION = 22;
219 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700220 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen22f823d2011-09-01 17:22:18 -0700221
Winson Chungb44b5242011-06-13 11:32:14 -0700222 // Previews & outlines
223 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
224 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700225 private static final int sPageSleepDelay = 150;
Winson Chung4b576be2011-04-27 17:40:20 -0700226
Winson Chung785d2eb2011-04-14 16:08:02 -0700227 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
228 super(context, attrs);
229 mLayoutInflater = LayoutInflater.from(context);
230 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700231 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700232 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700233 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700234 mHolographicOutlineHelper = new HolographicOutlineHelper();
235 mCanvas = new Canvas();
236 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700237
238 // Save the default widget preview background
239 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700240 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700241 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
242 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700243
244 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700245 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700246 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
247 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
248 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700249 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700250 mWidgetWidthGap =
251 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
252 mWidgetHeightGap =
253 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700254 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
255 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700256 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
257 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700258 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700260
Winson Chung1ed747a2011-05-03 16:18:34 -0700261 // The padding on the non-matched dimension for the default widget preview icons
262 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700263 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700264 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700265 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700266 }
267
268 @Override
269 protected void init() {
270 super.init();
Winson Chung5fc72b32011-10-11 17:53:58 -0700271 mCenterPagesVertically = true;
Winson Chung785d2eb2011-04-14 16:08:02 -0700272
273 Context context = getContext();
274 Resources r = context.getResources();
275 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
276 }
277
Winson Chungf0ea4d32011-06-06 14:27:16 -0700278 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700279 protected void onUnhandledTap(MotionEvent ev) {
280 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700281 // Dismiss AppsCustomize if we tap
282 mLauncher.showWorkspace(true);
283 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700284 }
285
Winson Chung5afbf7b2011-07-25 11:53:08 -0700286 /** Returns the item index of the center item on this page so that we can restore to this
287 * item index when we rotate. */
288 private int getMiddleComponentIndexOnCurrentPage() {
289 int i = -1;
290 if (getPageCount() > 0) {
291 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700292 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700293 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700294 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
295 int numItemsPerPage = mCellCountX * mCellCountY;
296 int childCount = childrenLayout.getChildCount();
297 if (childCount > 0) {
298 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700299 }
300 } else {
301 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700302 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700303 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
304 int childCount = layout.getChildCount();
305 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700306 i = numApps +
307 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
308 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700309 }
310 }
311 return i;
312 }
313
314 /** Get the index of the item to restore to if we need to restore the current page. */
315 int getSaveInstanceStateIndex() {
316 if (mSaveInstanceStateItemIndex == -1) {
317 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
318 }
319 return mSaveInstanceStateItemIndex;
320 }
321
322 /** Returns the page in the current orientation which is expected to contain the specified
323 * item index. */
324 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700325 if (index < 0) return 0;
326
327 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700328 int numItemsPerPage = mCellCountX * mCellCountY;
329 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700331 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
333 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700334 }
335
Winson Chungf0ea4d32011-06-06 14:27:16 -0700336 /**
337 * This differs from isDataReady as this is the test done if isDataReady is not set.
338 */
339 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700340 // We only do this test once, and we default to the Applications page, so we only really
341 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 // TODO: What if one of them is validly empty
343 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 }
345
Winson Chung5afbf7b2011-07-25 11:53:08 -0700346 /** Restores the page for an item at the specified index */
347 void restorePageForIndex(int index) {
348 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700349 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700350 }
351
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700352 private void updatePageCounts() {
353 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
354 (float) (mWidgetCountX * mWidgetCountY));
355 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
356 }
357
Winson Chungf0ea4d32011-06-06 14:27:16 -0700358 protected void onDataReady(int width, int height) {
359 // Note that we transpose the counts in portrait so that we get a similar layout
360 boolean isLandscape = getResources().getConfiguration().orientation ==
361 Configuration.ORIENTATION_LANDSCAPE;
362 int maxCellCountX = Integer.MAX_VALUE;
363 int maxCellCountY = Integer.MAX_VALUE;
364 if (LauncherApplication.isScreenLarge()) {
365 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
366 LauncherModel.getCellCountY());
367 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
368 LauncherModel.getCellCountX());
369 }
370
371 // Now that the data is ready, we can calculate the content width, the number of cells to
372 // use for each page
373 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
374 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
375 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
376 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
377 mCellCountX = mWidgetSpacingLayout.getCellCountX();
378 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700379 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700380
Winson Chungdb1138b2011-06-30 14:39:35 -0700381 // Force a measure to update recalculate the gaps
382 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
383 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
384 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700386
387 // Restore the page
388 int page = getPageForComponent(mSaveInstanceStateItemIndex);
389 invalidatePageData(Math.max(0, page));
Winson Chung7d7541e2011-09-16 20:14:36 -0700390
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700391 // Calculate the position for the cling punch through
Winson Chung7d7541e2011-09-16 20:14:36 -0700392 int[] offset = new int[2];
393 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
394 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
395 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + offset[0];
396 pos[1] += (getMeasuredHeight() - mWidgetSpacingLayout.getMeasuredHeight()) / 2 + offset[1];
397 mLauncher.showFirstRunAllAppsCling(pos);
398
Winson Chungf0ea4d32011-06-06 14:27:16 -0700399 }
400
401 @Override
402 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
403 int width = MeasureSpec.getSize(widthMeasureSpec);
404 int height = MeasureSpec.getSize(heightMeasureSpec);
405 if (!isDataReady()) {
406 if (testDataReady()) {
407 setDataIsReady();
408 setMeasuredDimension(width, height);
409 onDataReady(width, height);
410 }
411 }
412
413 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
414 }
415
Winson Chung34efdaf2011-05-24 14:19:56 -0700416 /** Removes and returns the ResolveInfo with the specified ComponentName */
417 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
418 ComponentName cn) {
419 Iterator<ResolveInfo> iter = list.iterator();
420 while (iter.hasNext()) {
421 ResolveInfo rinfo = iter.next();
422 ActivityInfo info = rinfo.activityInfo;
423 ComponentName c = new ComponentName(info.packageName, info.name);
424 if (c.equals(cn)) {
425 iter.remove();
426 return rinfo;
427 }
428 }
429 return null;
430 }
431
Winson Chung785d2eb2011-04-14 16:08:02 -0700432 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700433 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
434 // by a broadcast receiver, and in order for it to work correctly, we need to know that
435 // the AppWidgetService has already received and processed the same broadcast. Since there
436 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
437 // we should have a more precise way of ensuring the AppWidgetService is up to date.
438 postDelayed(new Runnable() {
439 public void run() {
440 updatePackages();
441 }
442 }, 500);
443 }
444
445 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700446 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700447 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700448 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 List<AppWidgetProviderInfo> widgets =
450 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
452 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700454 mWidgets.addAll(shortcuts);
455 Collections.sort(mWidgets,
456 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700457 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700458
Winson Chung875de7e2011-06-28 14:25:17 -0700459 if (wasEmpty) {
460 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
461 // a layout to do this test and invalidate the page data when ready.
462 if (testDataReady()) requestLayout();
463 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700464 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700465 invalidatePageData();
466 }
Winson Chung4b576be2011-04-27 17:40:20 -0700467 }
468
469 @Override
470 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700471 // When we have exited all apps or are in transition, disregard clicks
472 if (!mLauncher.isAllAppsCustomizeOpen() ||
473 mLauncher.getWorkspace().isSwitchingState()) return;
474
Winson Chung4b576be2011-04-27 17:40:20 -0700475 if (v instanceof PagedViewIcon) {
476 // Animate some feedback to the click
477 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
478 animateClickFeedback(v, new Runnable() {
479 @Override
480 public void run() {
481 mLauncher.startActivitySafely(appInfo.intent, appInfo);
482 }
483 });
484 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700485 // Let the user know that they have to long press to add a widget
486 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
487 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700488
Winson Chungd2e87b32011-06-02 10:53:07 -0700489 // Create a little animation to show that the widget can move
490 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
491 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
492 AnimatorSet bounce = new AnimatorSet();
493 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
494 tyuAnim.setDuration(125);
495 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
496 tydAnim.setDuration(100);
497 bounce.play(tyuAnim).before(tydAnim);
498 bounce.setInterpolator(new AccelerateInterpolator());
499 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700500 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700501 }
502
503 /*
504 * PagedViewWithDraggableItems implementation
505 */
506 @Override
507 protected void determineDraggingStart(android.view.MotionEvent ev) {
508 // Disable dragging by pulling an app down for now.
509 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700510
Winson Chung4b576be2011-04-27 17:40:20 -0700511 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700512 mLauncher.getWorkspace().onDragStartedWithItem(v);
513 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700514 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700515
Winson Chung4b576be2011-04-27 17:40:20 -0700516 private void beginDraggingWidget(View v) {
517 // Get the widget preview as the drag representation
518 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700519 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700520
521 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700522 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700523 Drawable preview = image.getDrawable();
Winson Chung5fc72b32011-10-11 17:53:58 -0700524 RectF mTmpScaleRect = new RectF(0f,0f,1f,1f);
525 image.getImageMatrix().mapRect(mTmpScaleRect);
526 float scale = mTmpScaleRect.right;
527 int w = (int) (preview.getIntrinsicWidth() * scale);
528 int h = (int) (preview.getIntrinsicHeight() * scale);
Winson Chung1ed747a2011-05-03 16:18:34 -0700529 if (createItemInfo instanceof PendingAddWidgetInfo) {
530 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700531 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700532 createItemInfo.spanX = spanXY[0];
533 createItemInfo.spanY = spanXY[1];
534
535 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700536 renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700537 } else {
538 // Workaround for the fact that we don't keep the original ResolveInfo associated with
539 // the shortcut around. To get the icon, we just render the preview image (which has
540 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
541 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
542 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700543 mCanvas.setBitmap(b);
544 mCanvas.save();
545 preview.draw(mCanvas);
546 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700547 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700548 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700549 createItemInfo.spanX = createItemInfo.spanY = 1;
550 }
Winson Chung4b576be2011-04-27 17:40:20 -0700551
552 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700553 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700554 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
555 createItemInfo.spanY, b);
556 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700557 DragController.DRAG_ACTION_COPY, null);
558 b.recycle();
559 }
560 @Override
561 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700562 // Dismiss the cling
563 mLauncher.dismissAllAppsCling(null);
564
Winson Chung4b576be2011-04-27 17:40:20 -0700565 if (!super.beginDragging(v)) return false;
566
Winson Chungc07918d2011-07-01 15:35:26 -0700567 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700568 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700569
Winson Chung4b576be2011-04-27 17:40:20 -0700570 if (v instanceof PagedViewIcon) {
571 beginDraggingApplication(v);
572 } else if (v instanceof PagedViewWidget) {
573 beginDraggingWidget(v);
574 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 return true;
576 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700577 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700578 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700579 if (!success || (target != mLauncher.getWorkspace() &&
580 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700581 // Exit spring loaded mode if we have not successfully dropped or have not handled the
582 // drop in Workspace
583 mLauncher.exitSpringLoadedDragMode();
584 }
Adam Cohen446e9402011-09-15 18:21:21 -0700585 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700586
Winson Chung785d2eb2011-04-14 16:08:02 -0700587 }
588
Winson Chung785d2eb2011-04-14 16:08:02 -0700589 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700590 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700591 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700592
593 // Display an error message if the drag failed due to there not being enough space on the
594 // target layout we were dropping on.
595 if (!success) {
596 boolean showOutOfSpaceMessage = false;
597 if (target instanceof Workspace) {
598 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
599 Workspace workspace = (Workspace) target;
600 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700601 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700602 if (layout != null) {
603 layout.calculateSpans(itemInfo);
604 showOutOfSpaceMessage =
605 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
606 }
607 }
Winson Chungfc79c802011-05-02 13:35:34 -0700608 if (showOutOfSpaceMessage) {
609 mLauncher.showOutOfSpaceMessage();
610 }
611 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700612 }
613
Winson Chung7f0acdd2011-09-19 18:34:19 -0700614 @Override
615 protected void onDetachedFromWindow() {
616 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700617 cancelAllTasks();
618 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700619
Adam Cohen0cd3b642011-10-14 14:58:00 -0700620 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700621 // Clean up all the async tasks
622 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
623 while (iter.hasNext()) {
624 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
625 task.cancel(false);
626 iter.remove();
627 }
628 }
629
Winson Chung785d2eb2011-04-14 16:08:02 -0700630 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700631 if (type == ContentType.Widgets) {
632 invalidatePageData(mNumAppsPages, true);
633 } else if (type == ContentType.Applications) {
634 invalidatePageData(0, true);
635 }
Winson Chungb44b5242011-06-13 11:32:14 -0700636 }
637
Adam Cohen0cd3b642011-10-14 14:58:00 -0700638 protected void snapToPage(int whichPage, int delta, int duration) {
639 super.snapToPage(whichPage, delta, duration);
640 updateCurrentTab(whichPage);
641 }
642
643 private void updateCurrentTab(int currentPage) {
644 AppsCustomizeTabHost tabHost = getTabHost();
645 String tag = tabHost.getCurrentTabTag();
646 if (currentPage >= mNumAppsPages &&
647 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
648 tabHost.setCurrentTabFromContent(ContentType.Widgets);
649 } else if (currentPage < mNumAppsPages &&
650 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
651 tabHost.setCurrentTabFromContent(ContentType.Applications);
652 }
653 }
654
Winson Chung785d2eb2011-04-14 16:08:02 -0700655 /*
656 * Apps PagedView implementation
657 */
Winson Chung63257c12011-05-05 17:06:13 -0700658 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
659 int childCount = layout.getChildCount();
660 for (int i = 0; i < childCount; ++i) {
661 layout.getChildAt(i).setVisibility(visibility);
662 }
663 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700664 private void setupPage(PagedViewCellLayout layout) {
665 layout.setCellCount(mCellCountX, mCellCountY);
666 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
667 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
668 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
669
Winson Chung63257c12011-05-05 17:06:13 -0700670 // Note: We force a measure here to get around the fact that when we do layout calculations
671 // immediately after syncing, we don't have a proper width. That said, we already know the
672 // expected page width, so we can actually optimize by hiding all the TextView-based
673 // children that are expensive to measure, and let that happen naturally later.
674 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700675 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700677 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700679 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700680 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700681
Winson Chungf314b0e2011-08-16 11:54:27 -0700682 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 int numCells = mCellCountX * mCellCountY;
685 int startIndex = page * numCells;
686 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700687 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700688
Winson Chung785d2eb2011-04-14 16:08:02 -0700689 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700690 ArrayList<Object> items = new ArrayList<Object>();
691 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700692 for (int i = startIndex; i < endIndex; ++i) {
693 ApplicationInfo info = mApps.get(i);
694 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
695 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700696 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 icon.setOnClickListener(this);
698 icon.setOnLongClickListener(this);
699 icon.setOnTouchListener(this);
700
701 int index = i - startIndex;
702 int x = index % mCellCountX;
703 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700704 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700705
706 items.add(info);
707 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700708 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700709
Winson Chungf0ea4d32011-06-06 14:27:16 -0700710 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700711
Winson Chung8ff87132011-09-30 19:40:46 -0700712 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700713 if (mFadeInAdjacentScreens) {
714 prepareGenerateHoloOutlinesTask(page, items, images);
715 }
Winson Chung8ff87132011-09-30 19:40:46 -0700716 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700717 }
Winson Chungb44b5242011-06-13 11:32:14 -0700718
719 /**
720 * Return the appropriate thread priority for loading for a given page (we give the current
721 * page much higher priority)
722 */
723 private int getThreadPriorityForPage(int page) {
724 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
725 int pageDiff = Math.abs(page - mCurrentPage);
726 if (pageDiff <= 0) {
727 // return Process.THREAD_PRIORITY_DEFAULT;
728 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
729 } else if (pageDiff <= 1) {
730 // return Process.THREAD_PRIORITY_BACKGROUND;
731 return Process.THREAD_PRIORITY_DEFAULT;
732 } else {
733 // return Process.THREAD_PRIORITY_LOWEST;
734 return Process.THREAD_PRIORITY_DEFAULT;
735 }
736 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700737 private int getSleepForPage(int page) {
738 int pageDiff = Math.abs(page - mCurrentPage) - 1;
739 return Math.max(0, pageDiff * sPageSleepDelay);
740 }
Winson Chungb44b5242011-06-13 11:32:14 -0700741 /**
742 * Creates and executes a new AsyncTask to load a page of widget previews.
743 */
744 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700745 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700746 // Prune all tasks that are no longer needed
747 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
748 while (iter.hasNext()) {
749 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
750 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700751 if ((taskPage == page) ||
Adam Cohen0cd3b642011-10-14 14:58:00 -0700752 taskPage < getAssociatedLowerPageBound(mCurrentPage - mNumAppsPages) ||
753 taskPage > getAssociatedUpperPageBound(mCurrentPage - mNumAppsPages)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700754 task.cancel(false);
755 iter.remove();
756 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700757 task.setThreadPriority(getThreadPriorityForPage(taskPage + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700758 }
759 }
760
Winson Chungf314b0e2011-08-16 11:54:27 -0700761 // We introduce a slight delay to order the loading of side pages so that we don't thrash
Adam Cohen0cd3b642011-10-14 14:58:00 -0700762 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700763 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700764 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700765 @Override
766 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700767 try {
Winson Chung09945932011-09-20 14:22:40 -0700768 try {
769 Thread.sleep(sleepMs);
770 } catch (Exception e) {}
771 loadWidgetPreviewsInBackground(task, data);
772 } finally {
773 if (task.isCancelled()) {
774 data.cleanup(true);
775 }
776 }
Winson Chungb44b5242011-06-13 11:32:14 -0700777 }
778 },
779 new AsyncTaskCallback() {
780 @Override
781 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700782 try {
783 mRunningTasks.remove(task);
784 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700785 onSyncWidgetPageItems(data);
786 } finally {
787 data.cleanup(task.isCancelled());
788 }
Winson Chungb44b5242011-06-13 11:32:14 -0700789 }
Winson Chung09945932011-09-20 14:22:40 -0700790 });
Winson Chungb44b5242011-06-13 11:32:14 -0700791
792 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700793 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700794 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700795 t.setThreadPriority(getThreadPriorityForPage(page));
796 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
797 mRunningTasks.add(t);
798 }
799 /**
800 * Creates and executes a new AsyncTask to load the outlines for a page of content.
801 */
802 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
803 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700804 // Prune old tasks for this page
805 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
806 while (iter.hasNext()) {
807 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
808 int taskPage = task.page;
809 if ((taskPage == page) &&
810 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
811 task.cancel(false);
812 iter.remove();
813 }
814 }
815
Winson Chungb44b5242011-06-13 11:32:14 -0700816 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
817 new AsyncTaskCallback() {
818 @Override
819 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700820 try {
821 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700822 task.syncThreadPriority();
823
Winson Chung09945932011-09-20 14:22:40 -0700824 ArrayList<Bitmap> images = data.generatedImages;
825 ArrayList<Bitmap> srcImages = data.sourceImages;
826 int count = srcImages.size();
827 Canvas c = new Canvas();
828 for (int i = 0; i < count && !task.isCancelled(); ++i) {
829 // Before work on each item, ensure that this task is running at the correct
830 // priority
831 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700832
Winson Chung09945932011-09-20 14:22:40 -0700833 Bitmap b = srcImages.get(i);
834 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
835 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700836
Winson Chung09945932011-09-20 14:22:40 -0700837 c.setBitmap(outline);
838 c.save();
839 c.drawBitmap(b, 0, 0, null);
840 c.restore();
841 c.setBitmap(null);
842
843 images.add(outline);
844 }
845 } finally {
846 if (task.isCancelled()) {
847 data.cleanup(true);
848 }
Winson Chungb44b5242011-06-13 11:32:14 -0700849 }
850 }
851 },
852 new AsyncTaskCallback() {
853 @Override
854 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700855 try {
856 mRunningTasks.remove(task);
857 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700858 onHolographicPageItemsLoaded(data);
859 } finally {
860 data.cleanup(task.isCancelled());
861 }
Winson Chungb44b5242011-06-13 11:32:14 -0700862 }
863 });
864
865 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700866 AppsCustomizeAsyncTask t =
Adam Cohen0cd3b642011-10-14 14:58:00 -0700867 new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700868 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
869 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
870 mRunningTasks.add(t);
871 }
872
Winson Chung785d2eb2011-04-14 16:08:02 -0700873 /*
874 * Widgets PagedView implementation
875 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700876 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700877 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
878 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700879
880 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700881 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700882 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
883 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700884 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700885 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700886 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700887
Winson Chung5fc72b32011-10-11 17:53:58 -0700888 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
889 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700890 }
891 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung5fc72b32011-10-11 17:53:58 -0700892 float scale) {
893 renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
894 }
895 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
896 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700897 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700898 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700899 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700900 Rect oldBounds = d.copyBounds();
901 d.setBounds(x, y, x + w, y + h);
902 d.draw(c);
903 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700904 if (multiplyColor != 0xFFFFFFFF) {
905 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
906 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700907 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700908 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700909 }
Winson Chungd2945262011-06-24 15:22:14 -0700910 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700911 // Render the background
912 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
913 int bitmapSize = mAppIconSize + 2 * offset;
914 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
915 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapSize, bitmapSize);
916
Winson Chung1ed747a2011-05-03 16:18:34 -0700917 // Render the icon
Winson Chung4dbea792011-05-05 14:21:32 -0700918 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chung5fc72b32011-10-11 17:53:58 -0700919 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700920 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700921 }
Winson Chung4e076542011-06-23 13:04:10 -0700922 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700923 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700924
Winson Chung4b576be2011-04-27 17:40:20 -0700925 // Load the preview image if possible
926 String packageName = info.provider.getPackageName();
927 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700928 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700929 if (info.previewImage != 0) {
930 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
931 if (drawable == null) {
932 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
933 + " for provider: " + info.provider);
934 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700935 // Scale down the preview to something that is closer to the cellWidth/Height
Winson Chung4b576be2011-04-27 17:40:20 -0700936 int imageWidth = drawable.getIntrinsicWidth();
937 int imageHeight = drawable.getIntrinsicHeight();
Winson Chung5fc72b32011-10-11 17:53:58 -0700938 int bitmapWidth = imageWidth;
939 int bitmapHeight = imageHeight;
940 if (imageWidth > imageHeight) {
941 bitmapWidth = cellWidth;
942 bitmapHeight = (int) (imageHeight * ((float) bitmapWidth / imageWidth));
Winson Chung4b576be2011-04-27 17:40:20 -0700943 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700944 bitmapHeight = cellHeight;
945 bitmapWidth = (int) (imageWidth * ((float) bitmapHeight / imageHeight));
Winson Chung4b576be2011-04-27 17:40:20 -0700946 }
947
Winson Chung5fc72b32011-10-11 17:53:58 -0700948 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
949 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
Winson Chung4b576be2011-04-27 17:40:20 -0700950 }
951 }
952
953 // Generate a preview image if we couldn't load one
954 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700955 Resources resources = mLauncher.getResources();
Winson Chung5fc72b32011-10-11 17:53:58 -0700956 // TODO: This actually uses the apps customize cell layout params, where as we make want
957 // the Workspace params for more accuracy.
958 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
959 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
960 int bitmapWidth = targetWidth;
961 int bitmapHeight = targetHeight;
962 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
963 float iconScale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700964
Winson Chung5fc72b32011-10-11 17:53:58 -0700965 // Determine the size of the bitmap we want to draw
Winson Chung273c1022011-07-11 13:40:52 -0700966 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700967 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
968 if (cellHSpan <= 1) {
969 bitmapWidth = bitmapHeight = mAppIconSize + 2 * offset;
970 } else {
971 bitmapWidth = bitmapHeight = mAppIconSize + 4 * offset;
972 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700973 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700974 // Otherwise, ensure that we are properly sized within the cellWidth/Height
975 if (targetWidth > targetHeight) {
976 bitmapWidth = Math.min(targetWidth, cellWidth);
977 bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth));
978 iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * offset), 1f);
979 } else {
980 bitmapHeight = Math.min(targetHeight, cellHeight);
981 bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight));
982 iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * offset), 1f);
983 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700984 }
Winson Chung273c1022011-07-11 13:40:52 -0700985 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700986 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
987 bitmapWidth);
Winson Chung4b576be2011-04-27 17:40:20 -0700988
989 // Draw the icon in the top left corner
990 try {
991 Drawable icon = null;
992 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
993 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
994
Winson Chung5fc72b32011-10-11 17:53:58 -0700995 renderDrawableToBitmap(icon, preview, (int) (offset * iconScale),
996 (int) (offset * iconScale), (int) (mAppIconSize * iconScale),
997 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -0700998 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700999 }
Winson Chungb44b5242011-06-13 11:32:14 -07001000 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001002
Winson Chungf314b0e2011-08-16 11:54:27 -07001003 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001004 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001005 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1006 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -07001007
Winson Chungd2945262011-06-24 15:22:14 -07001008 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -07001009 ArrayList<Object> items = new ArrayList<Object>();
1010 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001011 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -07001012 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001013 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001014
Winson Chunge4a647f2011-09-30 14:41:25 -07001015 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001016 int offset = page * numItemsPerPage;
1017 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1018 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001019 }
1020
Winson Chunge4a647f2011-09-30 14:41:25 -07001021 // Prepopulate the pages with the other widget info, and fill in the previews later
Adam Cohen0cd3b642011-10-14 14:58:00 -07001022 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001023 layout.setColumnCount(layout.getCellCountX());
1024 for (int i = 0; i < items.size(); ++i) {
1025 Object rawInfo = items.get(i);
1026 PendingAddItemInfo createItemInfo = null;
1027 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1028 R.layout.apps_customize_widget, layout, false);
1029 if (rawInfo instanceof AppWidgetProviderInfo) {
1030 // Fill in the widget information
1031 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1032 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1033 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1034 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1035 mHolographicOutlineHelper);
1036 widget.setTag(createItemInfo);
1037 } else if (rawInfo instanceof ResolveInfo) {
1038 // Fill in the shortcuts information
1039 ResolveInfo info = (ResolveInfo) rawInfo;
1040 createItemInfo = new PendingAddItemInfo();
1041 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1042 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1043 info.activityInfo.name);
1044 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1045 widget.setTag(createItemInfo);
1046 }
1047 widget.setOnClickListener(this);
1048 widget.setOnLongClickListener(this);
1049 widget.setOnTouchListener(this);
1050
1051 // Layout each widget
1052 int ix = i % mWidgetCountX;
1053 int iy = i / mWidgetCountX;
1054 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1055 GridLayout.spec(iy, GridLayout.LEFT),
1056 GridLayout.spec(ix, GridLayout.TOP));
1057 lp.width = cellWidth;
1058 lp.height = cellHeight;
1059 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1060 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1061 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1062 layout.addView(widget, lp);
1063 }
1064
1065 // Load the widget previews
Winson Chungf314b0e2011-08-16 11:54:27 -07001066 if (immediate) {
1067 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
1068 mWidgetCountX, null, null);
1069 loadWidgetPreviewsInBackground(null, data);
1070 onSyncWidgetPageItems(data);
1071 } else {
1072 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
1073 }
1074 }
1075 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1076 AsyncTaskPageData data) {
1077 if (task != null) {
1078 // Ensure that this task starts running at the correct priority
1079 task.syncThreadPriority();
1080 }
1081
1082 // Load each of the widget/shortcut previews
1083 ArrayList<Object> items = data.items;
1084 ArrayList<Bitmap> images = data.generatedImages;
1085 int count = items.size();
1086 int cellWidth = data.cellWidth;
1087 int cellHeight = data.cellHeight;
1088 for (int i = 0; i < count; ++i) {
1089 if (task != null) {
1090 // Ensure we haven't been cancelled yet
1091 if (task.isCancelled()) break;
1092 // Before work on each item, ensure that this task is running at the correct
1093 // priority
1094 task.syncThreadPriority();
1095 }
1096
1097 Object rawInfo = items.get(i);
1098 if (rawInfo instanceof AppWidgetProviderInfo) {
1099 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001100 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001101 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1102 cellWidth, cellHeight));
1103 } else if (rawInfo instanceof ResolveInfo) {
1104 // Fill in the shortcuts information
1105 ResolveInfo info = (ResolveInfo) rawInfo;
1106 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1107 }
1108 }
Winson Chungb44b5242011-06-13 11:32:14 -07001109 }
1110 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1111 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001112 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001113
Winson Chungb44b5242011-06-13 11:32:14 -07001114 ArrayList<Object> items = data.items;
1115 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001116 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001117 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1118 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001119 Bitmap preview = data.generatedImages.get(i);
1120 boolean scale =
1121 (preview.getWidth() >= data.cellWidth ||
1122 preview.getHeight() >= data.cellHeight);
1123
1124 widget.applyPreview(new FastBitmapDrawable(preview), i, scale);
Winson Chung1ed747a2011-05-03 16:18:34 -07001125 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001126 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001127 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001128
1129 invalidate();
1130 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -07001131
Winson Chung8ff87132011-09-30 19:40:46 -07001132 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001133 if (mFadeInAdjacentScreens) {
1134 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1135 }
Winson Chung8ff87132011-09-30 19:40:46 -07001136 */
Winson Chungb44b5242011-06-13 11:32:14 -07001137 }
1138 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1139 // Invalidate early to short-circuit children invalidates
1140 invalidate();
1141
1142 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001143 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001144 if (layout instanceof PagedViewCellLayout) {
1145 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1146 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001147 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001148 for (int i = 0; i < count; ++i) {
1149 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001150 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001151 }
1152 } else {
1153 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001154 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001155 for (int i = 0; i < count; ++i) {
1156 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001157 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001158 }
1159 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001160 }
Winson Chung46af2e82011-05-09 16:00:53 -07001161
Winson Chung785d2eb2011-04-14 16:08:02 -07001162 @Override
1163 public void syncPages() {
1164 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001165 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001166
Adam Cohen0cd3b642011-10-14 14:58:00 -07001167 Context context = getContext();
1168 for (int j = 0; j < mNumWidgetPages; ++j) {
1169 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1170 mWidgetCountY);
1171 setupPage(layout);
1172 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1173 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001174 }
1175
Adam Cohen0cd3b642011-10-14 14:58:00 -07001176 for (int i = 0; i < mNumAppsPages; ++i) {
1177 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1178 setupPage(layout);
1179 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001180 }
1181 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001182
Winson Chung785d2eb2011-04-14 16:08:02 -07001183 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001184 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001185 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001186 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001187 } else {
1188 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001189 }
1190 }
1191
Adam Cohen22f823d2011-09-01 17:22:18 -07001192 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1193 // it is in the z-order. This is important to insure touch events are handled correctly.
1194 View getPageAt(int index) {
1195 return getChildAt(getChildCount() - index - 1);
1196 }
1197
1198 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1199 @Override
1200 protected void screenScrolled(int screenCenter) {
1201 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001202
1203 for (int i = 0; i < getChildCount(); i++) {
1204 View v = getPageAt(i);
1205 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001206 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001207
1208 float interpolatedProgress =
1209 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1210 float scale = (1 - interpolatedProgress) +
1211 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1212 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001213
1214 float alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
1215 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen22f823d2011-09-01 17:22:18 -07001216
1217 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1218 int pageWidth = v.getMeasuredWidth();
1219 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001220
1221 if (PERFORM_OVERSCROLL_ROTATION) {
1222 if (i == 0 && scrollProgress < 0) {
1223 // Overscroll to the left
1224 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1225 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1226 scale = 1.0f;
1227 alpha = 1.0f;
1228 // On the first page, we don't want the page to have any lateral motion
1229 translationX = getScrollX();
1230 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1231 // Overscroll to the right
1232 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1233 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1234 scale = 1.0f;
1235 alpha = 1.0f;
1236 // On the last page, we don't want the page to have any lateral motion.
1237 translationX = getScrollX() - mMaxScrollX;
1238 } else {
1239 v.setPivotY(pageHeight / 2.0f);
1240 v.setPivotX(pageWidth / 2.0f);
1241 v.setRotationY(0f);
1242 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001243 }
1244
1245 v.setTranslationX(translationX);
1246 v.setScaleX(scale);
1247 v.setScaleY(scale);
1248 v.setAlpha(alpha);
1249 }
1250 }
1251 }
1252
1253 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001254 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001255 }
1256
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 /**
1258 * Used by the parent to get the content width to set the tab bar to
1259 * @return
1260 */
1261 public int getPageContentWidth() {
1262 return mContentWidth;
1263 }
1264
Winson Chungb26f3d62011-06-02 10:49:29 -07001265 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001266 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001267 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001268
1269 // We reset the save index when we change pages so that it will be recalculated on next
1270 // rotation
1271 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001272 }
1273
Winson Chung785d2eb2011-04-14 16:08:02 -07001274 /*
1275 * AllAppsView implementation
1276 */
1277 @Override
1278 public void setup(Launcher launcher, DragController dragController) {
1279 mLauncher = launcher;
1280 mDragController = dragController;
1281 }
1282 @Override
1283 public void zoom(float zoom, boolean animate) {
1284 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1285 }
1286 @Override
1287 public boolean isVisible() {
1288 return (getVisibility() == VISIBLE);
1289 }
1290 @Override
1291 public boolean isAnimating() {
1292 return false;
1293 }
1294 @Override
1295 public void setApps(ArrayList<ApplicationInfo> list) {
1296 mApps = list;
1297 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001298 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299
Winson Chung875de7e2011-06-28 14:25:17 -07001300 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1301 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001302 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 }
1304 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1305 // We add it in place, in alphabetical order
1306 int count = list.size();
1307 for (int i = 0; i < count; ++i) {
1308 ApplicationInfo info = list.get(i);
1309 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1310 if (index < 0) {
1311 mApps.add(-(index + 1), info);
1312 }
1313 }
1314 }
1315 @Override
1316 public void addApps(ArrayList<ApplicationInfo> list) {
1317 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001318 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001319 invalidatePageData();
1320 }
1321 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1322 ComponentName removeComponent = item.intent.getComponent();
1323 int length = list.size();
1324 for (int i = 0; i < length; ++i) {
1325 ApplicationInfo info = list.get(i);
1326 if (info.intent.getComponent().equals(removeComponent)) {
1327 return i;
1328 }
1329 }
1330 return -1;
1331 }
1332 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1333 // loop through all the apps and remove apps that have the same component
1334 int length = list.size();
1335 for (int i = 0; i < length; ++i) {
1336 ApplicationInfo info = list.get(i);
1337 int removeIndex = findAppByComponent(mApps, info);
1338 if (removeIndex > -1) {
1339 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 }
1341 }
1342 }
1343 @Override
1344 public void removeApps(ArrayList<ApplicationInfo> list) {
1345 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001346 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 invalidatePageData();
1348 }
1349 @Override
1350 public void updateApps(ArrayList<ApplicationInfo> list) {
1351 // We remove and re-add the updated applications list because it's properties may have
1352 // changed (ie. the title), and this will ensure that the items will be in their proper
1353 // place in the list.
1354 removeAppsWithoutInvalidate(list);
1355 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001356 updatePageCounts();
1357
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 invalidatePageData();
1359 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001360
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 @Override
1362 public void reset() {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001363 AppsCustomizeTabHost tabHost = getTabHost();
1364 String tag = tabHost.getCurrentTabTag();
1365 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1366 tabHost.setCurrentTabFromContent(ContentType.Applications);
1367 }
1368 if (mCurrentPage != 0) {
1369 invalidatePageData(0);
1370 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001371 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001372
1373 private AppsCustomizeTabHost getTabHost() {
1374 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1375 }
1376
Winson Chung785d2eb2011-04-14 16:08:02 -07001377 @Override
1378 public void dumpState() {
1379 // TODO: Dump information related to current list of Applications, Widgets, etc.
1380 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1381 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1382 }
1383 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001384 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001385 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001386 for (Object i: list) {
1387 if (i instanceof AppWidgetProviderInfo) {
1388 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1389 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1390 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1391 + " initialLayout=" + info.initialLayout
1392 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1393 } else if (i instanceof ResolveInfo) {
1394 ResolveInfo info = (ResolveInfo) i;
1395 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1396 + info.icon);
1397 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001398 }
1399 }
1400 @Override
1401 public void surrender() {
1402 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1403 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001404
1405 // Stop all background tasks
1406 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001407 }
Winson Chung007c6982011-06-14 13:27:53 -07001408
Winson Chungb44b5242011-06-13 11:32:14 -07001409 /*
1410 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1411 * widget previews in the background with the AsyncTasks.
1412 */
1413 protected int getAssociatedLowerPageBound(int page) {
1414 return Math.max(0, page - 2);
1415 }
1416 protected int getAssociatedUpperPageBound(int page) {
1417 final int count = getChildCount();
1418 return Math.min(page + 2, count - 1);
1419 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001420
1421 @Override
1422 protected String getCurrentPageDescription() {
1423 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1424 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001425 int count = 0;
1426
Adam Cohen0cd3b642011-10-14 14:58:00 -07001427 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001428 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001429 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001430 } else {
1431 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001432 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001433 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001434 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001435
Adam Cohend3357b12011-10-18 14:58:11 -07001436 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001437 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001438}