blob: 924349daf68b2c6c22819ea088bdc1c65c885b46 [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;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070036import android.graphics.MaskFilter;
37import android.graphics.Paint;
Winson Chung70fc4382011-08-08 15:31:33 -070038import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.Rect;
Winson Chung5fc72b32011-10-11 17:53:58 -070040import android.graphics.RectF;
Peter Ng8db70002011-10-25 15:40:08 -070041import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070043import android.os.AsyncTask;
44import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.util.AttributeSet;
46import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070047import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070049import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080051import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070052import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070053import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070054import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070055import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070056import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070057import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
59import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import com.android.launcher2.DropTarget.DragObject;
61
62import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Iterator;
65import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070066
Winson Chungb44b5242011-06-13 11:32:14 -070067/**
68 * A simple callback interface which also provides the results of the task.
69 */
70interface AsyncTaskCallback {
71 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
72}
Winson Chung4e076542011-06-23 13:04:10 -070073
Winson Chungb44b5242011-06-13 11:32:14 -070074/**
75 * The data needed to perform either of the custom AsyncTasks.
76 */
77class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070078 enum Type {
79 LoadWidgetPreviewData,
80 LoadHolographicIconsData
81 }
82
Winson Chungb44b5242011-06-13 11:32:14 -070083 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
84 AsyncTaskCallback postR) {
85 page = p;
86 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070087 sourceImages = si;
88 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070089 cellWidth = cellHeight = -1;
90 doInBackgroundCallback = bgR;
91 postExecuteCallback = postR;
92 }
Winson Chungd2945262011-06-24 15:22:14 -070093 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070094 AsyncTaskCallback postR) {
95 page = p;
96 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070097 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070098 cellWidth = cw;
99 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -0700100 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -0700101 doInBackgroundCallback = bgR;
102 postExecuteCallback = postR;
103 }
Winson Chung09945932011-09-20 14:22:40 -0700104 void cleanup(boolean cancelled) {
105 // Clean up any references to source/generated bitmaps
106 if (sourceImages != null) {
107 if (cancelled) {
108 for (Bitmap b : sourceImages) {
109 b.recycle();
110 }
111 }
112 sourceImages.clear();
113 }
114 if (generatedImages != null) {
115 if (cancelled) {
116 for (Bitmap b : generatedImages) {
117 b.recycle();
118 }
119 }
120 generatedImages.clear();
121 }
122 }
Winson Chungb44b5242011-06-13 11:32:14 -0700123 int page;
124 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700125 ArrayList<Bitmap> sourceImages;
126 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700127 int cellWidth;
128 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700129 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700130 AsyncTaskCallback doInBackgroundCallback;
131 AsyncTaskCallback postExecuteCallback;
132}
Winson Chung4e076542011-06-23 13:04:10 -0700133
Winson Chungb44b5242011-06-13 11:32:14 -0700134/**
135 * A generic template for an async task used in AppsCustomize.
136 */
137class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700138 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700139 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700141 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 }
143 @Override
144 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
145 if (params.length != 1) return null;
146 // Load each of the widget previews in the background
147 params[0].doInBackgroundCallback.run(this, params[0]);
148 return params[0];
149 }
150 @Override
151 protected void onPostExecute(AsyncTaskPageData result) {
152 // All the widget previews are loaded, so we can just callback to inflate the page
153 result.postExecuteCallback.run(this, result);
154 }
155
156 void setThreadPriority(int p) {
157 threadPriority = p;
158 }
159 void syncThreadPriority() {
160 Process.setThreadPriority(threadPriority);
161 }
162
163 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700164 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700165 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700166 int threadPriority;
167}
Winson Chungb44b5242011-06-13 11:32:14 -0700168
169/**
170 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
171 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700172public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
173 AllAppsView, View.OnClickListener, DragSource {
174 static final String LOG_TAG = "AppsCustomizePagedView";
175
176 /**
177 * The different content types that this paged view can show.
178 */
179 public enum ContentType {
180 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700181 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700182 }
183
184 // Refs
185 private Launcher mLauncher;
186 private DragController mDragController;
187 private final LayoutInflater mLayoutInflater;
188 private final PackageManager mPackageManager;
189
Winson Chung5afbf7b2011-07-25 11:53:08 -0700190 // Save and Restore
191 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700192
Winson Chung785d2eb2011-04-14 16:08:02 -0700193 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700195 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700196
Winson Chung7d7541e2011-09-16 20:14:36 -0700197 // Cling
198 private int mClingFocusedX;
199 private int mClingFocusedY;
200
Winson Chung1ed747a2011-05-03 16:18:34 -0700201 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700202 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700203 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700204 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700205 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700206
207 // Dimens
208 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700209 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800210 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700211 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700212 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700213 private final int mWidgetPreviewIconPaddedDimension;
214 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700215 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700216 private int mNumAppsPages;
217 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700218
Adam Cohen22f823d2011-09-01 17:22:18 -0700219 // Relating to the scroll and overscroll effects
220 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700221 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700222 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700223 private static float TRANSITION_PIVOT = 0.65f;
224 private static float TRANSITION_MAX_ROTATION = 22;
225 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700226 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700227 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700228
Winson Chungb44b5242011-06-13 11:32:14 -0700229 // Previews & outlines
230 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
231 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700232 private static final int sPageSleepDelay = 150;
Winson Chung4b576be2011-04-27 17:40:20 -0700233
Winson Chung785d2eb2011-04-14 16:08:02 -0700234 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
235 super(context, attrs);
236 mLayoutInflater = LayoutInflater.from(context);
237 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700238 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700239 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700240 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700241 mHolographicOutlineHelper = new HolographicOutlineHelper();
242 mCanvas = new Canvas();
243 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700244
245 // Save the default widget preview background
246 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700247 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700248 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
249 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700250
Winson Chung6032e7e2011-11-08 15:47:17 -0800251 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
252 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
253 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700254 mWidgetWidthGap =
255 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
256 mWidgetHeightGap =
257 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700258 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
259 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700260 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
261 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700262 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700263 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700264
Winson Chung1ed747a2011-05-03 16:18:34 -0700265 // The padding on the non-matched dimension for the default widget preview icons
266 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700267 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700268 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700269 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700270 }
271
272 @Override
273 protected void init() {
274 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700275 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700276
277 Context context = getContext();
278 Resources r = context.getResources();
279 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
280 }
281
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700283 protected void onUnhandledTap(MotionEvent ev) {
284 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700285 // Dismiss AppsCustomize if we tap
286 mLauncher.showWorkspace(true);
287 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700288 }
289
Winson Chung5afbf7b2011-07-25 11:53:08 -0700290 /** Returns the item index of the center item on this page so that we can restore to this
291 * item index when we rotate. */
292 private int getMiddleComponentIndexOnCurrentPage() {
293 int i = -1;
294 if (getPageCount() > 0) {
295 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700296 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700297 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700298 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
299 int numItemsPerPage = mCellCountX * mCellCountY;
300 int childCount = childrenLayout.getChildCount();
301 if (childCount > 0) {
302 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700303 }
304 } else {
305 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700306 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700307 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
308 int childCount = layout.getChildCount();
309 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700310 i = numApps +
311 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
312 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700313 }
314 }
315 return i;
316 }
317
318 /** Get the index of the item to restore to if we need to restore the current page. */
319 int getSaveInstanceStateIndex() {
320 if (mSaveInstanceStateItemIndex == -1) {
321 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
322 }
323 return mSaveInstanceStateItemIndex;
324 }
325
326 /** Returns the page in the current orientation which is expected to contain the specified
327 * item index. */
328 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700329 if (index < 0) return 0;
330
331 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700332 int numItemsPerPage = mCellCountX * mCellCountY;
333 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700334 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700335 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700336 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
337 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700338 }
339
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 /**
341 * This differs from isDataReady as this is the test done if isDataReady is not set.
342 */
343 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700344 // We only do this test once, and we default to the Applications page, so we only really
345 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700346 // TODO: What if one of them is validly empty
347 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 }
349
Winson Chung5afbf7b2011-07-25 11:53:08 -0700350 /** Restores the page for an item at the specified index */
351 void restorePageForIndex(int index) {
352 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700354 }
355
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700356 private void updatePageCounts() {
357 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
358 (float) (mWidgetCountX * mWidgetCountY));
359 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
360 }
361
Winson Chungf0ea4d32011-06-06 14:27:16 -0700362 protected void onDataReady(int width, int height) {
363 // Note that we transpose the counts in portrait so that we get a similar layout
364 boolean isLandscape = getResources().getConfiguration().orientation ==
365 Configuration.ORIENTATION_LANDSCAPE;
366 int maxCellCountX = Integer.MAX_VALUE;
367 int maxCellCountY = Integer.MAX_VALUE;
368 if (LauncherApplication.isScreenLarge()) {
369 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
370 LauncherModel.getCellCountY());
371 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
372 LauncherModel.getCellCountX());
373 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800374 if (mMaxAppCellCountX > -1) {
375 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
376 }
377 if (mMaxAppCellCountY > -1) {
378 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
379 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700380
381 // Now that the data is ready, we can calculate the content width, the number of cells to
382 // use for each page
383 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
384 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
385 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
386 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
387 mCellCountX = mWidgetSpacingLayout.getCellCountX();
388 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700389 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700390
Winson Chungdb1138b2011-06-30 14:39:35 -0700391 // Force a measure to update recalculate the gaps
392 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
393 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
394 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700395 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700396
397 // Restore the page
398 int page = getPageForComponent(mSaveInstanceStateItemIndex);
399 invalidatePageData(Math.max(0, page));
Winson Chung7d7541e2011-09-16 20:14:36 -0700400
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700401 // Calculate the position for the cling punch through
Winson Chung7d7541e2011-09-16 20:14:36 -0700402 int[] offset = new int[2];
403 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
404 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
405 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + offset[0];
406 pos[1] += (getMeasuredHeight() - mWidgetSpacingLayout.getMeasuredHeight()) / 2 + offset[1];
407 mLauncher.showFirstRunAllAppsCling(pos);
408
Winson Chungf0ea4d32011-06-06 14:27:16 -0700409 }
410
411 @Override
412 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
413 int width = MeasureSpec.getSize(widthMeasureSpec);
414 int height = MeasureSpec.getSize(heightMeasureSpec);
415 if (!isDataReady()) {
416 if (testDataReady()) {
417 setDataIsReady();
418 setMeasuredDimension(width, height);
419 onDataReady(width, height);
420 }
421 }
422
423 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
424 }
425
Winson Chung34efdaf2011-05-24 14:19:56 -0700426 /** Removes and returns the ResolveInfo with the specified ComponentName */
427 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
428 ComponentName cn) {
429 Iterator<ResolveInfo> iter = list.iterator();
430 while (iter.hasNext()) {
431 ResolveInfo rinfo = iter.next();
432 ActivityInfo info = rinfo.activityInfo;
433 ComponentName c = new ComponentName(info.packageName, info.name);
434 if (c.equals(cn)) {
435 iter.remove();
436 return rinfo;
437 }
438 }
439 return null;
440 }
441
Winson Chung785d2eb2011-04-14 16:08:02 -0700442 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700443 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
444 // by a broadcast receiver, and in order for it to work correctly, we need to know that
445 // the AppWidgetService has already received and processed the same broadcast. Since there
446 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
447 // we should have a more precise way of ensuring the AppWidgetService is up to date.
448 postDelayed(new Runnable() {
449 public void run() {
450 updatePackages();
451 }
452 }, 500);
453 }
454
455 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700456 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700457 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700458 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 List<AppWidgetProviderInfo> widgets =
460 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
462 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700463 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700464 mWidgets.addAll(shortcuts);
465 Collections.sort(mWidgets,
466 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700467 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700468
Winson Chung875de7e2011-06-28 14:25:17 -0700469 if (wasEmpty) {
470 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
471 // a layout to do this test and invalidate the page data when ready.
472 if (testDataReady()) requestLayout();
473 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700474 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700475 invalidatePageData();
476 }
Winson Chung4b576be2011-04-27 17:40:20 -0700477 }
478
479 @Override
480 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700481 // When we have exited all apps or are in transition, disregard clicks
482 if (!mLauncher.isAllAppsCustomizeOpen() ||
483 mLauncher.getWorkspace().isSwitchingState()) return;
484
Winson Chung4b576be2011-04-27 17:40:20 -0700485 if (v instanceof PagedViewIcon) {
486 // Animate some feedback to the click
487 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
488 animateClickFeedback(v, new Runnable() {
489 @Override
490 public void run() {
491 mLauncher.startActivitySafely(appInfo.intent, appInfo);
492 }
493 });
494 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700495 // Let the user know that they have to long press to add a widget
496 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
497 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700498
Winson Chungd2e87b32011-06-02 10:53:07 -0700499 // Create a little animation to show that the widget can move
500 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
501 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
502 AnimatorSet bounce = new AnimatorSet();
503 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
504 tyuAnim.setDuration(125);
505 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
506 tydAnim.setDuration(100);
507 bounce.play(tyuAnim).before(tydAnim);
508 bounce.setInterpolator(new AccelerateInterpolator());
509 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700510 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700511 }
512
513 /*
514 * PagedViewWithDraggableItems implementation
515 */
516 @Override
517 protected void determineDraggingStart(android.view.MotionEvent ev) {
518 // Disable dragging by pulling an app down for now.
519 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700520
Winson Chung4b576be2011-04-27 17:40:20 -0700521 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700522 mLauncher.getWorkspace().onDragStartedWithItem(v);
523 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700524 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700525
Winson Chung4b576be2011-04-27 17:40:20 -0700526 private void beginDraggingWidget(View v) {
527 // Get the widget preview as the drag representation
528 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700529 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700530
531 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700532 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700533 Drawable preview = image.getDrawable();
Winson Chung5fc72b32011-10-11 17:53:58 -0700534 RectF mTmpScaleRect = new RectF(0f,0f,1f,1f);
535 image.getImageMatrix().mapRect(mTmpScaleRect);
536 float scale = mTmpScaleRect.right;
537 int w = (int) (preview.getIntrinsicWidth() * scale);
538 int h = (int) (preview.getIntrinsicHeight() * scale);
Winson Chung1ed747a2011-05-03 16:18:34 -0700539 if (createItemInfo instanceof PendingAddWidgetInfo) {
540 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700541 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700542 createItemInfo.spanX = spanXY[0];
543 createItemInfo.spanY = spanXY[1];
544
545 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700546 renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700547 } else {
548 // Workaround for the fact that we don't keep the original ResolveInfo associated with
549 // the shortcut around. To get the icon, we just render the preview image (which has
550 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
551 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
552 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700553 mCanvas.setBitmap(b);
554 mCanvas.save();
555 preview.draw(mCanvas);
556 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700557 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700558 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700559 createItemInfo.spanX = createItemInfo.spanY = 1;
560 }
Winson Chung4b576be2011-04-27 17:40:20 -0700561
Peter Ng8db70002011-10-25 15:40:08 -0700562 // We use a custom alpha clip table for the default widget previews
563 Paint alphaClipPaint = null;
564 if (createItemInfo instanceof PendingAddWidgetInfo) {
565 if (((PendingAddWidgetInfo) createItemInfo).hasDefaultPreview) {
566 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
567 alphaClipPaint = new Paint();
568 alphaClipPaint.setMaskFilter(alphaClipTable);
569 }
570 }
571
Winson Chung4b576be2011-04-27 17:40:20 -0700572 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700573 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700574 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
Peter Ng8db70002011-10-25 15:40:08 -0700575 createItemInfo.spanY, b, alphaClipPaint);
Winson Chung1ed747a2011-05-03 16:18:34 -0700576 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700577 DragController.DRAG_ACTION_COPY, null);
578 b.recycle();
579 }
580 @Override
581 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700582 // Dismiss the cling
583 mLauncher.dismissAllAppsCling(null);
584
Winson Chung4b576be2011-04-27 17:40:20 -0700585 if (!super.beginDragging(v)) return false;
586
Winson Chungc07918d2011-07-01 15:35:26 -0700587 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700588 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700589
Winson Chung4b576be2011-04-27 17:40:20 -0700590 if (v instanceof PagedViewIcon) {
591 beginDraggingApplication(v);
592 } else if (v instanceof PagedViewWidget) {
593 beginDraggingWidget(v);
594 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700595 return true;
596 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700598 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700599 if (!success || (target != mLauncher.getWorkspace() &&
600 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 // Exit spring loaded mode if we have not successfully dropped or have not handled the
602 // drop in Workspace
603 mLauncher.exitSpringLoadedDragMode();
604 }
Adam Cohen446e9402011-09-15 18:21:21 -0700605 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700606
Winson Chung785d2eb2011-04-14 16:08:02 -0700607 }
608
Winson Chung785d2eb2011-04-14 16:08:02 -0700609 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700610 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700611 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700612
613 // Display an error message if the drag failed due to there not being enough space on the
614 // target layout we were dropping on.
615 if (!success) {
616 boolean showOutOfSpaceMessage = false;
617 if (target instanceof Workspace) {
618 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
619 Workspace workspace = (Workspace) target;
620 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700621 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700622 if (layout != null) {
623 layout.calculateSpans(itemInfo);
624 showOutOfSpaceMessage =
625 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
626 }
627 }
Winson Chungfc79c802011-05-02 13:35:34 -0700628 if (showOutOfSpaceMessage) {
629 mLauncher.showOutOfSpaceMessage();
630 }
631 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700632 }
633
Winson Chung7f0acdd2011-09-19 18:34:19 -0700634 @Override
635 protected void onDetachedFromWindow() {
636 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700637 cancelAllTasks();
638 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700639
Adam Cohen0cd3b642011-10-14 14:58:00 -0700640 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700641 // Clean up all the async tasks
642 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
643 while (iter.hasNext()) {
644 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
645 task.cancel(false);
646 iter.remove();
647 }
648 }
649
Winson Chung785d2eb2011-04-14 16:08:02 -0700650 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700651 if (type == ContentType.Widgets) {
652 invalidatePageData(mNumAppsPages, true);
653 } else if (type == ContentType.Applications) {
654 invalidatePageData(0, true);
655 }
Winson Chungb44b5242011-06-13 11:32:14 -0700656 }
657
Adam Cohen0cd3b642011-10-14 14:58:00 -0700658 protected void snapToPage(int whichPage, int delta, int duration) {
659 super.snapToPage(whichPage, delta, duration);
660 updateCurrentTab(whichPage);
661 }
662
663 private void updateCurrentTab(int currentPage) {
664 AppsCustomizeTabHost tabHost = getTabHost();
665 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -0700666 if (tag != null) {
667 if (currentPage >= mNumAppsPages &&
668 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
669 tabHost.setCurrentTabFromContent(ContentType.Widgets);
670 } else if (currentPage < mNumAppsPages &&
671 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
672 tabHost.setCurrentTabFromContent(ContentType.Applications);
673 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700674 }
675 }
676
Winson Chung785d2eb2011-04-14 16:08:02 -0700677 /*
678 * Apps PagedView implementation
679 */
Winson Chung63257c12011-05-05 17:06:13 -0700680 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
681 int childCount = layout.getChildCount();
682 for (int i = 0; i < childCount; ++i) {
683 layout.getChildAt(i).setVisibility(visibility);
684 }
685 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700686 private void setupPage(PagedViewCellLayout layout) {
687 layout.setCellCount(mCellCountX, mCellCountY);
688 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
689 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
690 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
691
Winson Chung63257c12011-05-05 17:06:13 -0700692 // Note: We force a measure here to get around the fact that when we do layout calculations
693 // immediately after syncing, we don't have a proper width. That said, we already know the
694 // expected page width, so we can actually optimize by hiding all the TextView-based
695 // children that are expensive to measure, and let that happen naturally later.
696 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700697 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700698 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700699 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700700 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700701 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700702 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700703
Winson Chungf314b0e2011-08-16 11:54:27 -0700704 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700706 int numCells = mCellCountX * mCellCountY;
707 int startIndex = page * numCells;
708 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700709 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700710
Winson Chung785d2eb2011-04-14 16:08:02 -0700711 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700712 ArrayList<Object> items = new ArrayList<Object>();
713 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700714 for (int i = startIndex; i < endIndex; ++i) {
715 ApplicationInfo info = mApps.get(i);
716 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
717 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700718 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700719 icon.setOnClickListener(this);
720 icon.setOnLongClickListener(this);
721 icon.setOnTouchListener(this);
722
723 int index = i - startIndex;
724 int x = index % mCellCountX;
725 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700726 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700727
728 items.add(info);
729 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700730 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700731
Winson Chungf0ea4d32011-06-06 14:27:16 -0700732 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700733
Winson Chung8ff87132011-09-30 19:40:46 -0700734 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700735 if (mFadeInAdjacentScreens) {
736 prepareGenerateHoloOutlinesTask(page, items, images);
737 }
Winson Chung8ff87132011-09-30 19:40:46 -0700738 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700739 }
Winson Chungb44b5242011-06-13 11:32:14 -0700740
741 /**
742 * Return the appropriate thread priority for loading for a given page (we give the current
743 * page much higher priority)
744 */
745 private int getThreadPriorityForPage(int page) {
746 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
747 int pageDiff = Math.abs(page - mCurrentPage);
748 if (pageDiff <= 0) {
749 // return Process.THREAD_PRIORITY_DEFAULT;
750 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
751 } else if (pageDiff <= 1) {
752 // return Process.THREAD_PRIORITY_BACKGROUND;
753 return Process.THREAD_PRIORITY_DEFAULT;
754 } else {
755 // return Process.THREAD_PRIORITY_LOWEST;
756 return Process.THREAD_PRIORITY_DEFAULT;
757 }
758 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700759 private int getSleepForPage(int page) {
760 int pageDiff = Math.abs(page - mCurrentPage) - 1;
761 return Math.max(0, pageDiff * sPageSleepDelay);
762 }
Winson Chungb44b5242011-06-13 11:32:14 -0700763 /**
764 * Creates and executes a new AsyncTask to load a page of widget previews.
765 */
766 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700767 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700768 // Prune all tasks that are no longer needed
769 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
770 while (iter.hasNext()) {
771 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
772 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700773 if ((taskPage == page) ||
Adam Cohen0cd3b642011-10-14 14:58:00 -0700774 taskPage < getAssociatedLowerPageBound(mCurrentPage - mNumAppsPages) ||
775 taskPage > getAssociatedUpperPageBound(mCurrentPage - mNumAppsPages)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700776 task.cancel(false);
777 iter.remove();
778 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700779 task.setThreadPriority(getThreadPriorityForPage(taskPage + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700780 }
781 }
782
Winson Chungf314b0e2011-08-16 11:54:27 -0700783 // 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 -0700784 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700785 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700786 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700787 @Override
788 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700789 try {
Winson Chung09945932011-09-20 14:22:40 -0700790 try {
791 Thread.sleep(sleepMs);
792 } catch (Exception e) {}
793 loadWidgetPreviewsInBackground(task, data);
794 } finally {
795 if (task.isCancelled()) {
796 data.cleanup(true);
797 }
798 }
Winson Chungb44b5242011-06-13 11:32:14 -0700799 }
800 },
801 new AsyncTaskCallback() {
802 @Override
803 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700804 try {
805 mRunningTasks.remove(task);
806 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700807 onSyncWidgetPageItems(data);
808 } finally {
809 data.cleanup(task.isCancelled());
810 }
Winson Chungb44b5242011-06-13 11:32:14 -0700811 }
Winson Chung09945932011-09-20 14:22:40 -0700812 });
Winson Chungb44b5242011-06-13 11:32:14 -0700813
814 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700815 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700816 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700817 t.setThreadPriority(getThreadPriorityForPage(page));
818 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
819 mRunningTasks.add(t);
820 }
821 /**
822 * Creates and executes a new AsyncTask to load the outlines for a page of content.
823 */
824 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
825 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700826 // Prune old tasks for this page
827 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
828 while (iter.hasNext()) {
829 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
830 int taskPage = task.page;
831 if ((taskPage == page) &&
832 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
833 task.cancel(false);
834 iter.remove();
835 }
836 }
837
Winson Chungb44b5242011-06-13 11:32:14 -0700838 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
839 new AsyncTaskCallback() {
840 @Override
841 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700842 try {
843 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700844 task.syncThreadPriority();
845
Winson Chung09945932011-09-20 14:22:40 -0700846 ArrayList<Bitmap> images = data.generatedImages;
847 ArrayList<Bitmap> srcImages = data.sourceImages;
848 int count = srcImages.size();
849 Canvas c = new Canvas();
850 for (int i = 0; i < count && !task.isCancelled(); ++i) {
851 // Before work on each item, ensure that this task is running at the correct
852 // priority
853 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700854
Winson Chung09945932011-09-20 14:22:40 -0700855 Bitmap b = srcImages.get(i);
856 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
857 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700858
Winson Chung09945932011-09-20 14:22:40 -0700859 c.setBitmap(outline);
860 c.save();
861 c.drawBitmap(b, 0, 0, null);
862 c.restore();
863 c.setBitmap(null);
864
865 images.add(outline);
866 }
867 } finally {
868 if (task.isCancelled()) {
869 data.cleanup(true);
870 }
Winson Chungb44b5242011-06-13 11:32:14 -0700871 }
872 }
873 },
874 new AsyncTaskCallback() {
875 @Override
876 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700877 try {
878 mRunningTasks.remove(task);
879 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700880 onHolographicPageItemsLoaded(data);
881 } finally {
882 data.cleanup(task.isCancelled());
883 }
Winson Chungb44b5242011-06-13 11:32:14 -0700884 }
885 });
886
887 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700888 AppsCustomizeAsyncTask t =
Adam Cohen0cd3b642011-10-14 14:58:00 -0700889 new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700890 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
891 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
892 mRunningTasks.add(t);
893 }
894
Winson Chung785d2eb2011-04-14 16:08:02 -0700895 /*
896 * Widgets PagedView implementation
897 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700898 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700899 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
900 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700901
902 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700903 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700904 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
905 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700907 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700908 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700909
Winson Chung5fc72b32011-10-11 17:53:58 -0700910 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
911 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700912 }
913 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung5fc72b32011-10-11 17:53:58 -0700914 float scale) {
915 renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
916 }
917 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
918 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700919 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700920 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700921 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700922 Rect oldBounds = d.copyBounds();
923 d.setBounds(x, y, x + w, y + h);
924 d.draw(c);
925 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700926 if (multiplyColor != 0xFFFFFFFF) {
927 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
928 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700929 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700930 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700931 }
Winson Chungd2945262011-06-24 15:22:14 -0700932 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700933 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -0700934 int offset = 0;
935 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -0700936 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700937
Winson Chung1ed747a2011-05-03 16:18:34 -0700938 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -0700939 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -0700940 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700941 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700942 }
Winson Chung4e076542011-06-23 13:04:10 -0700943 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700944 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700945
Winson Chung4b576be2011-04-27 17:40:20 -0700946 // Load the preview image if possible
947 String packageName = info.provider.getPackageName();
948 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700949 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700950 if (info.previewImage != 0) {
951 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
952 if (drawable == null) {
953 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
954 + " for provider: " + info.provider);
955 } else {
Winson Chungb5e74c82011-10-28 16:39:20 -0700956 // Map the target width/height to the cell dimensions
957 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
958 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
959 int targetCellWidth;
960 int targetCellHeight;
961 if (targetWidth >= targetHeight) {
962 targetCellWidth = Math.min(targetWidth, cellWidth);
963 targetCellHeight = (int) (cellHeight * ((float) targetCellWidth / cellWidth));
Winson Chung4b576be2011-04-27 17:40:20 -0700964 } else {
Winson Chungb5e74c82011-10-28 16:39:20 -0700965 targetCellHeight = Math.min(targetHeight, cellHeight);
966 targetCellWidth = (int) (cellWidth * ((float) targetCellHeight / cellHeight));
Winson Chung4b576be2011-04-27 17:40:20 -0700967 }
Winson Chungb5e74c82011-10-28 16:39:20 -0700968 // Map the preview to the target cell dimensions
969 int bitmapWidth = Math.min(targetCellWidth, drawable.getIntrinsicWidth());
970 int bitmapHeight = (int) (drawable.getIntrinsicHeight() *
971 ((float) bitmapWidth / drawable.getIntrinsicWidth()));
Winson Chung4b576be2011-04-27 17:40:20 -0700972
Winson Chung5fc72b32011-10-11 17:53:58 -0700973 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
974 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
Winson Chung4b576be2011-04-27 17:40:20 -0700975 }
976 }
977
978 // Generate a preview image if we couldn't load one
979 if (drawable == null) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700980 // TODO: This actually uses the apps customize cell layout params, where as we make want
981 // the Workspace params for more accuracy.
982 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
983 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
984 int bitmapWidth = targetWidth;
985 int bitmapHeight = targetHeight;
Peter Ng8db70002011-10-25 15:40:08 -0700986 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -0700987 float iconScale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700988
Winson Chung5fc72b32011-10-11 17:53:58 -0700989 // Determine the size of the bitmap we want to draw
Winson Chung273c1022011-07-11 13:40:52 -0700990 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700991 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
992 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -0700993 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -0700994 } else {
Peter Ng8db70002011-10-25 15:40:08 -0700995 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -0700996 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700997 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -0700998 // Otherwise, ensure that we are properly sized within the cellWidth/Height
Winson Chungb5e74c82011-10-28 16:39:20 -0700999 if (targetWidth >= targetHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001000 bitmapWidth = Math.min(targetWidth, cellWidth);
1001 bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth));
Peter Ng8db70002011-10-25 15:40:08 -07001002 iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * minOffset), 1f);
Winson Chung5fc72b32011-10-11 17:53:58 -07001003 } else {
1004 bitmapHeight = Math.min(targetHeight, cellHeight);
1005 bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight));
Peter Ng8db70002011-10-25 15:40:08 -07001006 iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * minOffset), 1f);
Winson Chung5fc72b32011-10-11 17:53:58 -07001007 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001008 }
Winson Chung273c1022011-07-11 13:40:52 -07001009 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Peter Ng8db70002011-10-25 15:40:08 -07001010 if (cellHSpan != 1 || cellVSpan != 1) {
1011 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1012 bitmapHeight);
1013 }
Winson Chung4b576be2011-04-27 17:40:20 -07001014
1015 // Draw the icon in the top left corner
1016 try {
1017 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001018 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1019 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Winson Chung0b9fcf52011-10-31 13:05:15 -07001020 if (info.icon > 0) icon = mIconCache.getFullResIcon(packageName, info.icon);
Winson Chung4b576be2011-04-27 17:40:20 -07001021 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1022
Peter Ng8db70002011-10-25 15:40:08 -07001023 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1024 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001025 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001026 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001027 }
Winson Chungb44b5242011-06-13 11:32:14 -07001028 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001030
Winson Chungf314b0e2011-08-16 11:54:27 -07001031 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001032 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001033 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1034 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -07001035
Winson Chungd2945262011-06-24 15:22:14 -07001036 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -07001037 ArrayList<Object> items = new ArrayList<Object>();
1038 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001039 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -07001040 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001041 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001042
Winson Chunge4a647f2011-09-30 14:41:25 -07001043 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001044 int offset = page * numItemsPerPage;
1045 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1046 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001047 }
1048
Winson Chunge4a647f2011-09-30 14:41:25 -07001049 // Prepopulate the pages with the other widget info, and fill in the previews later
Adam Cohen0cd3b642011-10-14 14:58:00 -07001050 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001051 layout.setColumnCount(layout.getCellCountX());
1052 for (int i = 0; i < items.size(); ++i) {
1053 Object rawInfo = items.get(i);
1054 PendingAddItemInfo createItemInfo = null;
1055 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1056 R.layout.apps_customize_widget, layout, false);
1057 if (rawInfo instanceof AppWidgetProviderInfo) {
1058 // Fill in the widget information
1059 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1060 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1061 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1062 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1063 mHolographicOutlineHelper);
1064 widget.setTag(createItemInfo);
1065 } else if (rawInfo instanceof ResolveInfo) {
1066 // Fill in the shortcuts information
1067 ResolveInfo info = (ResolveInfo) rawInfo;
1068 createItemInfo = new PendingAddItemInfo();
1069 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1070 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1071 info.activityInfo.name);
1072 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1073 widget.setTag(createItemInfo);
1074 }
1075 widget.setOnClickListener(this);
1076 widget.setOnLongClickListener(this);
1077 widget.setOnTouchListener(this);
1078
1079 // Layout each widget
1080 int ix = i % mWidgetCountX;
1081 int iy = i / mWidgetCountX;
1082 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1083 GridLayout.spec(iy, GridLayout.LEFT),
1084 GridLayout.spec(ix, GridLayout.TOP));
1085 lp.width = cellWidth;
1086 lp.height = cellHeight;
1087 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1088 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1089 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1090 layout.addView(widget, lp);
1091 }
1092
1093 // Load the widget previews
Winson Chungf314b0e2011-08-16 11:54:27 -07001094 if (immediate) {
1095 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
1096 mWidgetCountX, null, null);
1097 loadWidgetPreviewsInBackground(null, data);
1098 onSyncWidgetPageItems(data);
1099 } else {
1100 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
1101 }
1102 }
1103 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1104 AsyncTaskPageData data) {
1105 if (task != null) {
1106 // Ensure that this task starts running at the correct priority
1107 task.syncThreadPriority();
1108 }
1109
1110 // Load each of the widget/shortcut previews
1111 ArrayList<Object> items = data.items;
1112 ArrayList<Bitmap> images = data.generatedImages;
1113 int count = items.size();
1114 int cellWidth = data.cellWidth;
1115 int cellHeight = data.cellHeight;
1116 for (int i = 0; i < count; ++i) {
1117 if (task != null) {
1118 // Ensure we haven't been cancelled yet
1119 if (task.isCancelled()) break;
1120 // Before work on each item, ensure that this task is running at the correct
1121 // priority
1122 task.syncThreadPriority();
1123 }
1124
1125 Object rawInfo = items.get(i);
1126 if (rawInfo instanceof AppWidgetProviderInfo) {
1127 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001128 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001129 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1130 cellWidth, cellHeight));
1131 } else if (rawInfo instanceof ResolveInfo) {
1132 // Fill in the shortcuts information
1133 ResolveInfo info = (ResolveInfo) rawInfo;
1134 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1135 }
1136 }
Winson Chungb44b5242011-06-13 11:32:14 -07001137 }
1138 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1139 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001140 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001141
Winson Chungb44b5242011-06-13 11:32:14 -07001142 ArrayList<Object> items = data.items;
1143 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001144 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001145 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1146 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001147 Bitmap preview = data.generatedImages.get(i);
1148 boolean scale =
1149 (preview.getWidth() >= data.cellWidth ||
1150 preview.getHeight() >= data.cellHeight);
1151
1152 widget.applyPreview(new FastBitmapDrawable(preview), i, scale);
Winson Chung1ed747a2011-05-03 16:18:34 -07001153 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001154 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001155 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001156
1157 invalidate();
Winson Chung8ff87132011-09-30 19:40:46 -07001158 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001159 if (mFadeInAdjacentScreens) {
1160 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1161 }
Winson Chung8ff87132011-09-30 19:40:46 -07001162 */
Winson Chungb44b5242011-06-13 11:32:14 -07001163 }
1164 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1165 // Invalidate early to short-circuit children invalidates
1166 invalidate();
1167
1168 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001169 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001170 if (layout instanceof PagedViewCellLayout) {
1171 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1172 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001173 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001174 for (int i = 0; i < count; ++i) {
1175 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001176 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001177 }
1178 } else {
1179 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001180 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001181 for (int i = 0; i < count; ++i) {
1182 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001183 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001184 }
1185 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001186 }
Winson Chung46af2e82011-05-09 16:00:53 -07001187
Winson Chung785d2eb2011-04-14 16:08:02 -07001188 @Override
1189 public void syncPages() {
1190 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001191 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001192
Adam Cohen0cd3b642011-10-14 14:58:00 -07001193 Context context = getContext();
1194 for (int j = 0; j < mNumWidgetPages; ++j) {
1195 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1196 mWidgetCountY);
1197 setupPage(layout);
1198 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1199 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001200 }
1201
Adam Cohen0cd3b642011-10-14 14:58:00 -07001202 for (int i = 0; i < mNumAppsPages; ++i) {
1203 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1204 setupPage(layout);
1205 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 }
1207 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001208
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001210 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001211 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001212 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001213 } else {
1214 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
1216 }
1217
Adam Cohen22f823d2011-09-01 17:22:18 -07001218 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1219 // it is in the z-order. This is important to insure touch events are handled correctly.
1220 View getPageAt(int index) {
1221 return getChildAt(getChildCount() - index - 1);
1222 }
1223
Adam Cohenae4f1552011-10-20 00:15:42 -07001224 @Override
1225 protected int indexToPage(int index) {
1226 return getChildCount() - index - 1;
1227 }
1228
Adam Cohen22f823d2011-09-01 17:22:18 -07001229 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1230 @Override
1231 protected void screenScrolled(int screenCenter) {
1232 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001233
1234 for (int i = 0; i < getChildCount(); i++) {
1235 View v = getPageAt(i);
1236 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001237 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001238
1239 float interpolatedProgress =
1240 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1241 float scale = (1 - interpolatedProgress) +
1242 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1243 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001244
Adam Cohen2591f6a2011-10-25 14:36:40 -07001245 float alpha;
1246
1247 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1248 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001249 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001250 } else {
1251 // On large screens we need to fade the page as it nears its leftmost position
1252 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1253 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001254
1255 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1256 int pageWidth = v.getMeasuredWidth();
1257 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001258
1259 if (PERFORM_OVERSCROLL_ROTATION) {
1260 if (i == 0 && scrollProgress < 0) {
1261 // Overscroll to the left
1262 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1263 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1264 scale = 1.0f;
1265 alpha = 1.0f;
1266 // On the first page, we don't want the page to have any lateral motion
1267 translationX = getScrollX();
1268 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1269 // Overscroll to the right
1270 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1271 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1272 scale = 1.0f;
1273 alpha = 1.0f;
1274 // On the last page, we don't want the page to have any lateral motion.
1275 translationX = getScrollX() - mMaxScrollX;
1276 } else {
1277 v.setPivotY(pageHeight / 2.0f);
1278 v.setPivotX(pageWidth / 2.0f);
1279 v.setRotationY(0f);
1280 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001281 }
1282
1283 v.setTranslationX(translationX);
1284 v.setScaleX(scale);
1285 v.setScaleY(scale);
1286 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001287
1288 // If the view has 0 alpha, we set it to be invisible so as to prevent
1289 // it from accepting touches
1290 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1291 v.setVisibility(INVISIBLE);
1292 } else if (v.getVisibility() != VISIBLE) {
1293 v.setVisibility(VISIBLE);
1294 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001295 }
1296 }
1297 }
1298
1299 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001300 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001301 }
1302
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 /**
1304 * Used by the parent to get the content width to set the tab bar to
1305 * @return
1306 */
1307 public int getPageContentWidth() {
1308 return mContentWidth;
1309 }
1310
Winson Chungb26f3d62011-06-02 10:49:29 -07001311 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001312 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001313 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001314
1315 // We reset the save index when we change pages so that it will be recalculated on next
1316 // rotation
1317 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001318 }
1319
Winson Chung785d2eb2011-04-14 16:08:02 -07001320 /*
1321 * AllAppsView implementation
1322 */
1323 @Override
1324 public void setup(Launcher launcher, DragController dragController) {
1325 mLauncher = launcher;
1326 mDragController = dragController;
1327 }
1328 @Override
1329 public void zoom(float zoom, boolean animate) {
1330 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1331 }
1332 @Override
1333 public boolean isVisible() {
1334 return (getVisibility() == VISIBLE);
1335 }
1336 @Override
1337 public boolean isAnimating() {
1338 return false;
1339 }
1340 @Override
1341 public void setApps(ArrayList<ApplicationInfo> list) {
1342 mApps = list;
1343 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001344 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001345
Winson Chung875de7e2011-06-28 14:25:17 -07001346 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1347 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001348 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001349 }
1350 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1351 // We add it in place, in alphabetical order
1352 int count = list.size();
1353 for (int i = 0; i < count; ++i) {
1354 ApplicationInfo info = list.get(i);
1355 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1356 if (index < 0) {
1357 mApps.add(-(index + 1), info);
1358 }
1359 }
1360 }
1361 @Override
1362 public void addApps(ArrayList<ApplicationInfo> list) {
1363 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001364 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001365 invalidatePageData();
1366 }
1367 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1368 ComponentName removeComponent = item.intent.getComponent();
1369 int length = list.size();
1370 for (int i = 0; i < length; ++i) {
1371 ApplicationInfo info = list.get(i);
1372 if (info.intent.getComponent().equals(removeComponent)) {
1373 return i;
1374 }
1375 }
1376 return -1;
1377 }
1378 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1379 // loop through all the apps and remove apps that have the same component
1380 int length = list.size();
1381 for (int i = 0; i < length; ++i) {
1382 ApplicationInfo info = list.get(i);
1383 int removeIndex = findAppByComponent(mApps, info);
1384 if (removeIndex > -1) {
1385 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001386 }
1387 }
1388 }
1389 @Override
1390 public void removeApps(ArrayList<ApplicationInfo> list) {
1391 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001392 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001393 invalidatePageData();
1394 }
1395 @Override
1396 public void updateApps(ArrayList<ApplicationInfo> list) {
1397 // We remove and re-add the updated applications list because it's properties may have
1398 // changed (ie. the title), and this will ensure that the items will be in their proper
1399 // place in the list.
1400 removeAppsWithoutInvalidate(list);
1401 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001402 updatePageCounts();
1403
Winson Chung785d2eb2011-04-14 16:08:02 -07001404 invalidatePageData();
1405 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001406
Winson Chung785d2eb2011-04-14 16:08:02 -07001407 @Override
1408 public void reset() {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001409 AppsCustomizeTabHost tabHost = getTabHost();
1410 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001411 if (tag != null) {
1412 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1413 tabHost.setCurrentTabFromContent(ContentType.Applications);
1414 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001415 }
1416 if (mCurrentPage != 0) {
1417 invalidatePageData(0);
1418 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001419 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001420
1421 private AppsCustomizeTabHost getTabHost() {
1422 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1423 }
1424
Winson Chung785d2eb2011-04-14 16:08:02 -07001425 @Override
1426 public void dumpState() {
1427 // TODO: Dump information related to current list of Applications, Widgets, etc.
1428 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1429 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1430 }
Adam Cohen4e844012011-11-09 13:48:04 -08001431
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001433 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001434 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001435 for (Object i: list) {
1436 if (i instanceof AppWidgetProviderInfo) {
1437 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1438 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1439 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1440 + " initialLayout=" + info.initialLayout
1441 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1442 } else if (i instanceof ResolveInfo) {
1443 ResolveInfo info = (ResolveInfo) i;
1444 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1445 + info.icon);
1446 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001447 }
1448 }
Adam Cohen4e844012011-11-09 13:48:04 -08001449
Winson Chung785d2eb2011-04-14 16:08:02 -07001450 @Override
1451 public void surrender() {
1452 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1453 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001454
1455 // Stop all background tasks
1456 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 }
Winson Chung007c6982011-06-14 13:27:53 -07001458
Winson Chungb44b5242011-06-13 11:32:14 -07001459 /*
1460 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1461 * widget previews in the background with the AsyncTasks.
1462 */
1463 protected int getAssociatedLowerPageBound(int page) {
1464 return Math.max(0, page - 2);
1465 }
1466 protected int getAssociatedUpperPageBound(int page) {
1467 final int count = getChildCount();
1468 return Math.min(page + 2, count - 1);
1469 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001470
1471 @Override
1472 protected String getCurrentPageDescription() {
1473 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1474 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001475 int count = 0;
1476
Adam Cohen0cd3b642011-10-14 14:58:00 -07001477 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001478 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001479 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001480 } else {
1481 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001482 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001483 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001484 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001485
Adam Cohend3357b12011-10-18 14:58:11 -07001486 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001487 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001488}