blob: cde0b4b2d84539d3d2bfee3b409b7eded687e5f4 [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);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800463 for (AppWidgetProviderInfo widget : widgets) {
464 if (widget.minWidth > 0 && widget.minHeight > 0) {
465 mWidgets.add(widget);
466 } else {
467 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
468 widget.minWidth + ", " + widget.minHeight + ")");
469 }
470 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700471 mWidgets.addAll(shortcuts);
472 Collections.sort(mWidgets,
473 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700474 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700475
Winson Chung875de7e2011-06-28 14:25:17 -0700476 if (wasEmpty) {
477 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
478 // a layout to do this test and invalidate the page data when ready.
479 if (testDataReady()) requestLayout();
480 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700481 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700482 invalidatePageData();
483 }
Winson Chung4b576be2011-04-27 17:40:20 -0700484 }
485
486 @Override
487 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700488 // When we have exited all apps or are in transition, disregard clicks
489 if (!mLauncher.isAllAppsCustomizeOpen() ||
490 mLauncher.getWorkspace().isSwitchingState()) return;
491
Winson Chung4b576be2011-04-27 17:40:20 -0700492 if (v instanceof PagedViewIcon) {
493 // Animate some feedback to the click
494 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
495 animateClickFeedback(v, new Runnable() {
496 @Override
497 public void run() {
498 mLauncher.startActivitySafely(appInfo.intent, appInfo);
499 }
500 });
501 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700502 // Let the user know that they have to long press to add a widget
503 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
504 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700505
Winson Chungd2e87b32011-06-02 10:53:07 -0700506 // Create a little animation to show that the widget can move
507 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
508 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
509 AnimatorSet bounce = new AnimatorSet();
510 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
511 tyuAnim.setDuration(125);
512 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
513 tydAnim.setDuration(100);
514 bounce.play(tyuAnim).before(tydAnim);
515 bounce.setInterpolator(new AccelerateInterpolator());
516 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700517 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 }
519
520 /*
521 * PagedViewWithDraggableItems implementation
522 */
523 @Override
524 protected void determineDraggingStart(android.view.MotionEvent ev) {
525 // Disable dragging by pulling an app down for now.
526 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700527
Winson Chung4b576be2011-04-27 17:40:20 -0700528 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700529 mLauncher.getWorkspace().onDragStartedWithItem(v);
530 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700531 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700532
Winson Chung4b576be2011-04-27 17:40:20 -0700533 private void beginDraggingWidget(View v) {
534 // Get the widget preview as the drag representation
535 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700536 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700537
538 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700539 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700540 Drawable preview = image.getDrawable();
Winson Chung5fc72b32011-10-11 17:53:58 -0700541 RectF mTmpScaleRect = new RectF(0f,0f,1f,1f);
542 image.getImageMatrix().mapRect(mTmpScaleRect);
543 float scale = mTmpScaleRect.right;
544 int w = (int) (preview.getIntrinsicWidth() * scale);
545 int h = (int) (preview.getIntrinsicHeight() * scale);
Winson Chung1ed747a2011-05-03 16:18:34 -0700546 if (createItemInfo instanceof PendingAddWidgetInfo) {
547 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700548 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700549 createItemInfo.spanX = spanXY[0];
550 createItemInfo.spanY = spanXY[1];
551
552 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700553 renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700554 } else {
555 // Workaround for the fact that we don't keep the original ResolveInfo associated with
556 // the shortcut around. To get the icon, we just render the preview image (which has
557 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
558 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
559 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700560 mCanvas.setBitmap(b);
561 mCanvas.save();
562 preview.draw(mCanvas);
563 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700564 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700565 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700566 createItemInfo.spanX = createItemInfo.spanY = 1;
567 }
Winson Chung4b576be2011-04-27 17:40:20 -0700568
Peter Ng8db70002011-10-25 15:40:08 -0700569 // We use a custom alpha clip table for the default widget previews
570 Paint alphaClipPaint = null;
571 if (createItemInfo instanceof PendingAddWidgetInfo) {
572 if (((PendingAddWidgetInfo) createItemInfo).hasDefaultPreview) {
573 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
574 alphaClipPaint = new Paint();
575 alphaClipPaint.setMaskFilter(alphaClipTable);
576 }
577 }
578
Winson Chung4b576be2011-04-27 17:40:20 -0700579 // Start the drag
Adam Cohen446e9402011-09-15 18:21:21 -0700580 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1ed747a2011-05-03 16:18:34 -0700581 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
Peter Ng8db70002011-10-25 15:40:08 -0700582 createItemInfo.spanY, b, alphaClipPaint);
Winson Chung1ed747a2011-05-03 16:18:34 -0700583 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700584 DragController.DRAG_ACTION_COPY, null);
585 b.recycle();
586 }
587 @Override
588 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700589 // Dismiss the cling
590 mLauncher.dismissAllAppsCling(null);
591
Winson Chung4b576be2011-04-27 17:40:20 -0700592 if (!super.beginDragging(v)) return false;
593
Winson Chungc07918d2011-07-01 15:35:26 -0700594 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700595 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700596
Winson Chung4b576be2011-04-27 17:40:20 -0700597 if (v instanceof PagedViewIcon) {
598 beginDraggingApplication(v);
599 } else if (v instanceof PagedViewWidget) {
600 beginDraggingWidget(v);
601 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700602 return true;
603 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700604 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700605 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700606 if (!success || (target != mLauncher.getWorkspace() &&
607 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700608 // Exit spring loaded mode if we have not successfully dropped or have not handled the
609 // drop in Workspace
610 mLauncher.exitSpringLoadedDragMode();
611 }
Adam Cohen446e9402011-09-15 18:21:21 -0700612 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700613
Winson Chung785d2eb2011-04-14 16:08:02 -0700614 }
615
Winson Chung785d2eb2011-04-14 16:08:02 -0700616 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700617 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700618 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700619
620 // Display an error message if the drag failed due to there not being enough space on the
621 // target layout we were dropping on.
622 if (!success) {
623 boolean showOutOfSpaceMessage = false;
624 if (target instanceof Workspace) {
625 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
626 Workspace workspace = (Workspace) target;
627 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700628 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700629 if (layout != null) {
630 layout.calculateSpans(itemInfo);
631 showOutOfSpaceMessage =
632 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
633 }
634 }
Winson Chungfc79c802011-05-02 13:35:34 -0700635 if (showOutOfSpaceMessage) {
636 mLauncher.showOutOfSpaceMessage();
637 }
638 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700639 }
640
Winson Chung7f0acdd2011-09-19 18:34:19 -0700641 @Override
642 protected void onDetachedFromWindow() {
643 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700644 cancelAllTasks();
645 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700646
Adam Cohen0cd3b642011-10-14 14:58:00 -0700647 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700648 // Clean up all the async tasks
649 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
650 while (iter.hasNext()) {
651 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
652 task.cancel(false);
653 iter.remove();
654 }
655 }
656
Winson Chung785d2eb2011-04-14 16:08:02 -0700657 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700658 if (type == ContentType.Widgets) {
659 invalidatePageData(mNumAppsPages, true);
660 } else if (type == ContentType.Applications) {
661 invalidatePageData(0, true);
662 }
Winson Chungb44b5242011-06-13 11:32:14 -0700663 }
664
Adam Cohen0cd3b642011-10-14 14:58:00 -0700665 protected void snapToPage(int whichPage, int delta, int duration) {
666 super.snapToPage(whichPage, delta, duration);
667 updateCurrentTab(whichPage);
668 }
669
670 private void updateCurrentTab(int currentPage) {
671 AppsCustomizeTabHost tabHost = getTabHost();
672 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -0700673 if (tag != null) {
674 if (currentPage >= mNumAppsPages &&
675 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
676 tabHost.setCurrentTabFromContent(ContentType.Widgets);
677 } else if (currentPage < mNumAppsPages &&
678 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
679 tabHost.setCurrentTabFromContent(ContentType.Applications);
680 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700681 }
682 }
683
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 /*
685 * Apps PagedView implementation
686 */
Winson Chung63257c12011-05-05 17:06:13 -0700687 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
688 int childCount = layout.getChildCount();
689 for (int i = 0; i < childCount; ++i) {
690 layout.getChildAt(i).setVisibility(visibility);
691 }
692 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 private void setupPage(PagedViewCellLayout layout) {
694 layout.setCellCount(mCellCountX, mCellCountY);
695 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
696 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
697 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
698
Winson Chung63257c12011-05-05 17:06:13 -0700699 // Note: We force a measure here to get around the fact that when we do layout calculations
700 // immediately after syncing, we don't have a proper width. That said, we already know the
701 // expected page width, so we can actually optimize by hiding all the TextView-based
702 // children that are expensive to measure, and let that happen naturally later.
703 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700704 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700706 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700707 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700708 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700709 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700710
Winson Chungf314b0e2011-08-16 11:54:27 -0700711 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700712 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 int numCells = mCellCountX * mCellCountY;
714 int startIndex = page * numCells;
715 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700716 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700717
Winson Chung785d2eb2011-04-14 16:08:02 -0700718 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700719 ArrayList<Object> items = new ArrayList<Object>();
720 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700721 for (int i = startIndex; i < endIndex; ++i) {
722 ApplicationInfo info = mApps.get(i);
723 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
724 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700725 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700726 icon.setOnClickListener(this);
727 icon.setOnLongClickListener(this);
728 icon.setOnTouchListener(this);
729
730 int index = i - startIndex;
731 int x = index % mCellCountX;
732 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700733 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700734
735 items.add(info);
736 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700737 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700738
Winson Chungf0ea4d32011-06-06 14:27:16 -0700739 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700740
Winson Chung8ff87132011-09-30 19:40:46 -0700741 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -0700742 if (mFadeInAdjacentScreens) {
743 prepareGenerateHoloOutlinesTask(page, items, images);
744 }
Winson Chung8ff87132011-09-30 19:40:46 -0700745 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700746 }
Winson Chungb44b5242011-06-13 11:32:14 -0700747
748 /**
749 * Return the appropriate thread priority for loading for a given page (we give the current
750 * page much higher priority)
751 */
752 private int getThreadPriorityForPage(int page) {
753 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
754 int pageDiff = Math.abs(page - mCurrentPage);
755 if (pageDiff <= 0) {
756 // return Process.THREAD_PRIORITY_DEFAULT;
757 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
758 } else if (pageDiff <= 1) {
759 // return Process.THREAD_PRIORITY_BACKGROUND;
760 return Process.THREAD_PRIORITY_DEFAULT;
761 } else {
762 // return Process.THREAD_PRIORITY_LOWEST;
763 return Process.THREAD_PRIORITY_DEFAULT;
764 }
765 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700766 private int getSleepForPage(int page) {
767 int pageDiff = Math.abs(page - mCurrentPage) - 1;
768 return Math.max(0, pageDiff * sPageSleepDelay);
769 }
Winson Chungb44b5242011-06-13 11:32:14 -0700770 /**
771 * Creates and executes a new AsyncTask to load a page of widget previews.
772 */
773 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700774 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700775 // Prune all tasks that are no longer needed
776 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
777 while (iter.hasNext()) {
778 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
779 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700780 if ((taskPage == page) ||
Adam Cohen0cd3b642011-10-14 14:58:00 -0700781 taskPage < getAssociatedLowerPageBound(mCurrentPage - mNumAppsPages) ||
782 taskPage > getAssociatedUpperPageBound(mCurrentPage - mNumAppsPages)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700783 task.cancel(false);
784 iter.remove();
785 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700786 task.setThreadPriority(getThreadPriorityForPage(taskPage + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700787 }
788 }
789
Winson Chungf314b0e2011-08-16 11:54:27 -0700790 // 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 -0700791 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700792 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700793 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700794 @Override
795 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700796 try {
Winson Chung09945932011-09-20 14:22:40 -0700797 try {
798 Thread.sleep(sleepMs);
799 } catch (Exception e) {}
800 loadWidgetPreviewsInBackground(task, data);
801 } finally {
802 if (task.isCancelled()) {
803 data.cleanup(true);
804 }
805 }
Winson Chungb44b5242011-06-13 11:32:14 -0700806 }
807 },
808 new AsyncTaskCallback() {
809 @Override
810 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700811 try {
812 mRunningTasks.remove(task);
813 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700814 onSyncWidgetPageItems(data);
815 } finally {
816 data.cleanup(task.isCancelled());
817 }
Winson Chungb44b5242011-06-13 11:32:14 -0700818 }
Winson Chung09945932011-09-20 14:22:40 -0700819 });
Winson Chungb44b5242011-06-13 11:32:14 -0700820
821 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700822 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700823 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700824 t.setThreadPriority(getThreadPriorityForPage(page));
825 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
826 mRunningTasks.add(t);
827 }
828 /**
829 * Creates and executes a new AsyncTask to load the outlines for a page of content.
830 */
831 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
832 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700833 // Prune old tasks for this page
834 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
835 while (iter.hasNext()) {
836 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
837 int taskPage = task.page;
838 if ((taskPage == page) &&
839 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
840 task.cancel(false);
841 iter.remove();
842 }
843 }
844
Winson Chungb44b5242011-06-13 11:32:14 -0700845 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
846 new AsyncTaskCallback() {
847 @Override
848 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700849 try {
850 // Ensure that this task starts running at the correct priority
Winson Chungb44b5242011-06-13 11:32:14 -0700851 task.syncThreadPriority();
852
Winson Chung09945932011-09-20 14:22:40 -0700853 ArrayList<Bitmap> images = data.generatedImages;
854 ArrayList<Bitmap> srcImages = data.sourceImages;
855 int count = srcImages.size();
856 Canvas c = new Canvas();
857 for (int i = 0; i < count && !task.isCancelled(); ++i) {
858 // Before work on each item, ensure that this task is running at the correct
859 // priority
860 task.syncThreadPriority();
Winson Chungb44b5242011-06-13 11:32:14 -0700861
Winson Chung09945932011-09-20 14:22:40 -0700862 Bitmap b = srcImages.get(i);
863 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
864 Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700865
Winson Chung09945932011-09-20 14:22:40 -0700866 c.setBitmap(outline);
867 c.save();
868 c.drawBitmap(b, 0, 0, null);
869 c.restore();
870 c.setBitmap(null);
871
872 images.add(outline);
873 }
874 } finally {
875 if (task.isCancelled()) {
876 data.cleanup(true);
877 }
Winson Chungb44b5242011-06-13 11:32:14 -0700878 }
879 }
880 },
881 new AsyncTaskCallback() {
882 @Override
883 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700884 try {
885 mRunningTasks.remove(task);
886 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700887 onHolographicPageItemsLoaded(data);
888 } finally {
889 data.cleanup(task.isCancelled());
890 }
Winson Chungb44b5242011-06-13 11:32:14 -0700891 }
892 });
893
894 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700895 AppsCustomizeAsyncTask t =
Adam Cohen0cd3b642011-10-14 14:58:00 -0700896 new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700897 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
898 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
899 mRunningTasks.add(t);
900 }
901
Winson Chung785d2eb2011-04-14 16:08:02 -0700902 /*
903 * Widgets PagedView implementation
904 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700905 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
907 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700908
909 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700910 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700911 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
912 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700913 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700914 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700915 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700916
Winson Chung5fc72b32011-10-11 17:53:58 -0700917 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
918 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700919 }
920 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung5fc72b32011-10-11 17:53:58 -0700921 float scale) {
922 renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF);
923 }
924 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
925 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700926 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700927 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700928 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700929 Rect oldBounds = d.copyBounds();
930 d.setBounds(x, y, x + w, y + h);
931 d.draw(c);
932 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700933 if (multiplyColor != 0xFFFFFFFF) {
934 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
935 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700936 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700937 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700938 }
Winson Chungd2945262011-06-24 15:22:14 -0700939 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700940 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -0700941 int offset = 0;
942 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -0700943 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700944
Winson Chung1ed747a2011-05-03 16:18:34 -0700945 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -0700946 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -0700947 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700948 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700949 }
Winson Chung4e076542011-06-23 13:04:10 -0700950 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700951 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700952
Winson Chung4b576be2011-04-27 17:40:20 -0700953 // Load the preview image if possible
954 String packageName = info.provider.getPackageName();
955 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700956 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700957 if (info.previewImage != 0) {
958 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
959 if (drawable == null) {
960 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
961 + " for provider: " + info.provider);
962 } else {
Winson Chungb5e74c82011-10-28 16:39:20 -0700963 // Map the target width/height to the cell dimensions
964 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
965 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
966 int targetCellWidth;
967 int targetCellHeight;
968 if (targetWidth >= targetHeight) {
969 targetCellWidth = Math.min(targetWidth, cellWidth);
970 targetCellHeight = (int) (cellHeight * ((float) targetCellWidth / cellWidth));
Winson Chung4b576be2011-04-27 17:40:20 -0700971 } else {
Winson Chungb5e74c82011-10-28 16:39:20 -0700972 targetCellHeight = Math.min(targetHeight, cellHeight);
973 targetCellWidth = (int) (cellWidth * ((float) targetCellHeight / cellHeight));
Winson Chung4b576be2011-04-27 17:40:20 -0700974 }
Winson Chungb5e74c82011-10-28 16:39:20 -0700975 // Map the preview to the target cell dimensions
976 int bitmapWidth = Math.min(targetCellWidth, drawable.getIntrinsicWidth());
977 int bitmapHeight = (int) (drawable.getIntrinsicHeight() *
978 ((float) bitmapWidth / drawable.getIntrinsicWidth()));
Winson Chung4b576be2011-04-27 17:40:20 -0700979
Winson Chung5fc72b32011-10-11 17:53:58 -0700980 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
981 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
Winson Chung4b576be2011-04-27 17:40:20 -0700982 }
983 }
984
985 // Generate a preview image if we couldn't load one
986 if (drawable == null) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700987 // TODO: This actually uses the apps customize cell layout params, where as we make want
988 // the Workspace params for more accuracy.
989 int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
990 int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
991 int bitmapWidth = targetWidth;
992 int bitmapHeight = targetHeight;
Peter Ng8db70002011-10-25 15:40:08 -0700993 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -0700994 float iconScale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700995
Winson Chung5fc72b32011-10-11 17:53:58 -0700996 // Determine the size of the bitmap we want to draw
Winson Chung273c1022011-07-11 13:40:52 -0700997 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700998 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
999 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001000 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001001 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001002 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001003 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001004 } else {
Winson Chung5fc72b32011-10-11 17:53:58 -07001005 // Otherwise, ensure that we are properly sized within the cellWidth/Height
Winson Chungb5e74c82011-10-28 16:39:20 -07001006 if (targetWidth >= targetHeight) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001007 bitmapWidth = Math.min(targetWidth, cellWidth);
1008 bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth));
Peter Ng8db70002011-10-25 15:40:08 -07001009 iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * minOffset), 1f);
Winson Chung5fc72b32011-10-11 17:53:58 -07001010 } else {
1011 bitmapHeight = Math.min(targetHeight, cellHeight);
1012 bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight));
Peter Ng8db70002011-10-25 15:40:08 -07001013 iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * minOffset), 1f);
Winson Chung5fc72b32011-10-11 17:53:58 -07001014 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001015 }
Winson Chung273c1022011-07-11 13:40:52 -07001016 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Peter Ng8db70002011-10-25 15:40:08 -07001017 if (cellHSpan != 1 || cellVSpan != 1) {
1018 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1019 bitmapHeight);
1020 }
Winson Chung4b576be2011-04-27 17:40:20 -07001021
1022 // Draw the icon in the top left corner
1023 try {
1024 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001025 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1026 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Winson Chung0b9fcf52011-10-31 13:05:15 -07001027 if (info.icon > 0) icon = mIconCache.getFullResIcon(packageName, info.icon);
Michael Jurka31234d82011-11-10 15:15:15 -08001028 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001029 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1030
Peter Ng8db70002011-10-25 15:40:08 -07001031 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1032 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001033 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001034 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001035 }
Winson Chungb44b5242011-06-13 11:32:14 -07001036 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001038
Winson Chungf314b0e2011-08-16 11:54:27 -07001039 public void syncWidgetPageItems(int page, boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001040 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001041 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1042 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -07001043
Winson Chungd2945262011-06-24 15:22:14 -07001044 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -07001045 ArrayList<Object> items = new ArrayList<Object>();
1046 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001047 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -07001048 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001049 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001050
Winson Chunge4a647f2011-09-30 14:41:25 -07001051 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001052 int offset = page * numItemsPerPage;
1053 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1054 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001055 }
1056
Winson Chunge4a647f2011-09-30 14:41:25 -07001057 // Prepopulate the pages with the other widget info, and fill in the previews later
Adam Cohen0cd3b642011-10-14 14:58:00 -07001058 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001059 layout.setColumnCount(layout.getCellCountX());
1060 for (int i = 0; i < items.size(); ++i) {
1061 Object rawInfo = items.get(i);
1062 PendingAddItemInfo createItemInfo = null;
1063 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1064 R.layout.apps_customize_widget, layout, false);
1065 if (rawInfo instanceof AppWidgetProviderInfo) {
1066 // Fill in the widget information
1067 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1068 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1069 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
1070 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans,
1071 mHolographicOutlineHelper);
1072 widget.setTag(createItemInfo);
1073 } else if (rawInfo instanceof ResolveInfo) {
1074 // Fill in the shortcuts information
1075 ResolveInfo info = (ResolveInfo) rawInfo;
1076 createItemInfo = new PendingAddItemInfo();
1077 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1078 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1079 info.activityInfo.name);
1080 widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper);
1081 widget.setTag(createItemInfo);
1082 }
1083 widget.setOnClickListener(this);
1084 widget.setOnLongClickListener(this);
1085 widget.setOnTouchListener(this);
1086
1087 // Layout each widget
1088 int ix = i % mWidgetCountX;
1089 int iy = i / mWidgetCountX;
1090 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1091 GridLayout.spec(iy, GridLayout.LEFT),
1092 GridLayout.spec(ix, GridLayout.TOP));
1093 lp.width = cellWidth;
1094 lp.height = cellHeight;
1095 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1096 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1097 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1098 layout.addView(widget, lp);
1099 }
1100
1101 // Load the widget previews
Winson Chungf314b0e2011-08-16 11:54:27 -07001102 if (immediate) {
1103 AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight,
1104 mWidgetCountX, null, null);
1105 loadWidgetPreviewsInBackground(null, data);
1106 onSyncWidgetPageItems(data);
1107 } else {
1108 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
1109 }
1110 }
1111 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1112 AsyncTaskPageData data) {
1113 if (task != null) {
1114 // Ensure that this task starts running at the correct priority
1115 task.syncThreadPriority();
1116 }
1117
1118 // Load each of the widget/shortcut previews
1119 ArrayList<Object> items = data.items;
1120 ArrayList<Bitmap> images = data.generatedImages;
1121 int count = items.size();
1122 int cellWidth = data.cellWidth;
1123 int cellHeight = data.cellHeight;
1124 for (int i = 0; i < count; ++i) {
1125 if (task != null) {
1126 // Ensure we haven't been cancelled yet
1127 if (task.isCancelled()) break;
1128 // Before work on each item, ensure that this task is running at the correct
1129 // priority
1130 task.syncThreadPriority();
1131 }
1132
1133 Object rawInfo = items.get(i);
1134 if (rawInfo instanceof AppWidgetProviderInfo) {
1135 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001136 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chungf314b0e2011-08-16 11:54:27 -07001137 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
1138 cellWidth, cellHeight));
1139 } else if (rawInfo instanceof ResolveInfo) {
1140 // Fill in the shortcuts information
1141 ResolveInfo info = (ResolveInfo) rawInfo;
1142 images.add(getShortcutPreview(info, cellWidth, cellHeight));
1143 }
1144 }
Winson Chungb44b5242011-06-13 11:32:14 -07001145 }
1146 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1147 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001148 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001149
Winson Chungb44b5242011-06-13 11:32:14 -07001150 ArrayList<Object> items = data.items;
1151 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001152 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001153 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1154 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001155 Bitmap preview = data.generatedImages.get(i);
1156 boolean scale =
1157 (preview.getWidth() >= data.cellWidth ||
1158 preview.getHeight() >= data.cellHeight);
1159
1160 widget.applyPreview(new FastBitmapDrawable(preview), i, scale);
Winson Chung1ed747a2011-05-03 16:18:34 -07001161 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001162 }
Winson Chung7f0acdd2011-09-19 18:34:19 -07001163 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001164
1165 invalidate();
Winson Chung8ff87132011-09-30 19:40:46 -07001166 /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS
Winson Chung54c725c2011-08-03 12:03:40 -07001167 if (mFadeInAdjacentScreens) {
1168 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
1169 }
Winson Chung8ff87132011-09-30 19:40:46 -07001170 */
Winson Chungb44b5242011-06-13 11:32:14 -07001171 }
1172 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
1173 // Invalidate early to short-circuit children invalidates
1174 invalidate();
1175
1176 int page = data.page;
Adam Cohen22f823d2011-09-01 17:22:18 -07001177 ViewGroup layout = (ViewGroup) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001178 if (layout instanceof PagedViewCellLayout) {
1179 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
1180 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001181 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001182 for (int i = 0; i < count; ++i) {
1183 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001184 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001185 }
1186 } else {
1187 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -07001188 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -07001189 for (int i = 0; i < count; ++i) {
1190 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -07001191 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001192 }
1193 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001194 }
Winson Chung46af2e82011-05-09 16:00:53 -07001195
Winson Chung785d2eb2011-04-14 16:08:02 -07001196 @Override
1197 public void syncPages() {
1198 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001199 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001200
Adam Cohen0cd3b642011-10-14 14:58:00 -07001201 Context context = getContext();
1202 for (int j = 0; j < mNumWidgetPages; ++j) {
1203 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1204 mWidgetCountY);
1205 setupPage(layout);
1206 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1207 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001208 }
1209
Adam Cohen0cd3b642011-10-14 14:58:00 -07001210 for (int i = 0; i < mNumAppsPages; ++i) {
1211 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1212 setupPage(layout);
1213 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
1215 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001216
Winson Chung785d2eb2011-04-14 16:08:02 -07001217 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001218 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001219 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001220 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001221 } else {
1222 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001223 }
1224 }
1225
Adam Cohen22f823d2011-09-01 17:22:18 -07001226 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1227 // it is in the z-order. This is important to insure touch events are handled correctly.
1228 View getPageAt(int index) {
1229 return getChildAt(getChildCount() - index - 1);
1230 }
1231
Adam Cohenae4f1552011-10-20 00:15:42 -07001232 @Override
1233 protected int indexToPage(int index) {
1234 return getChildCount() - index - 1;
1235 }
1236
Adam Cohen22f823d2011-09-01 17:22:18 -07001237 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1238 @Override
1239 protected void screenScrolled(int screenCenter) {
1240 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001241
1242 for (int i = 0; i < getChildCount(); i++) {
1243 View v = getPageAt(i);
1244 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001245 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001246
1247 float interpolatedProgress =
1248 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1249 float scale = (1 - interpolatedProgress) +
1250 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1251 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001252
Adam Cohen2591f6a2011-10-25 14:36:40 -07001253 float alpha;
1254
1255 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1256 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001257 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001258 } else {
1259 // On large screens we need to fade the page as it nears its leftmost position
1260 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1261 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001262
1263 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1264 int pageWidth = v.getMeasuredWidth();
1265 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001266
1267 if (PERFORM_OVERSCROLL_ROTATION) {
1268 if (i == 0 && scrollProgress < 0) {
1269 // Overscroll to the left
1270 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1271 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1272 scale = 1.0f;
1273 alpha = 1.0f;
1274 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001275 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001276 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1277 // Overscroll to the right
1278 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1279 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1280 scale = 1.0f;
1281 alpha = 1.0f;
1282 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001283 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001284 } else {
1285 v.setPivotY(pageHeight / 2.0f);
1286 v.setPivotX(pageWidth / 2.0f);
1287 v.setRotationY(0f);
1288 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001289 }
1290
1291 v.setTranslationX(translationX);
1292 v.setScaleX(scale);
1293 v.setScaleY(scale);
1294 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001295
1296 // If the view has 0 alpha, we set it to be invisible so as to prevent
1297 // it from accepting touches
1298 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1299 v.setVisibility(INVISIBLE);
1300 } else if (v.getVisibility() != VISIBLE) {
1301 v.setVisibility(VISIBLE);
1302 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001303 }
1304 }
1305 }
1306
1307 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001308 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001309 }
1310
Winson Chung785d2eb2011-04-14 16:08:02 -07001311 /**
1312 * Used by the parent to get the content width to set the tab bar to
1313 * @return
1314 */
1315 public int getPageContentWidth() {
1316 return mContentWidth;
1317 }
1318
Winson Chungb26f3d62011-06-02 10:49:29 -07001319 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001320 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001321 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001322
1323 // We reset the save index when we change pages so that it will be recalculated on next
1324 // rotation
1325 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001326 }
1327
Winson Chung785d2eb2011-04-14 16:08:02 -07001328 /*
1329 * AllAppsView implementation
1330 */
1331 @Override
1332 public void setup(Launcher launcher, DragController dragController) {
1333 mLauncher = launcher;
1334 mDragController = dragController;
1335 }
1336 @Override
1337 public void zoom(float zoom, boolean animate) {
1338 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1339 }
1340 @Override
1341 public boolean isVisible() {
1342 return (getVisibility() == VISIBLE);
1343 }
1344 @Override
1345 public boolean isAnimating() {
1346 return false;
1347 }
1348 @Override
1349 public void setApps(ArrayList<ApplicationInfo> list) {
1350 mApps = list;
1351 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001352 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001353
Winson Chung875de7e2011-06-28 14:25:17 -07001354 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1355 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001356 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001357 }
1358 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1359 // We add it in place, in alphabetical order
1360 int count = list.size();
1361 for (int i = 0; i < count; ++i) {
1362 ApplicationInfo info = list.get(i);
1363 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1364 if (index < 0) {
1365 mApps.add(-(index + 1), info);
1366 }
1367 }
1368 }
1369 @Override
1370 public void addApps(ArrayList<ApplicationInfo> list) {
1371 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001372 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001373 invalidatePageData();
1374 }
1375 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1376 ComponentName removeComponent = item.intent.getComponent();
1377 int length = list.size();
1378 for (int i = 0; i < length; ++i) {
1379 ApplicationInfo info = list.get(i);
1380 if (info.intent.getComponent().equals(removeComponent)) {
1381 return i;
1382 }
1383 }
1384 return -1;
1385 }
1386 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1387 // loop through all the apps and remove apps that have the same component
1388 int length = list.size();
1389 for (int i = 0; i < length; ++i) {
1390 ApplicationInfo info = list.get(i);
1391 int removeIndex = findAppByComponent(mApps, info);
1392 if (removeIndex > -1) {
1393 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001394 }
1395 }
1396 }
1397 @Override
1398 public void removeApps(ArrayList<ApplicationInfo> list) {
1399 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001400 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001401 invalidatePageData();
1402 }
1403 @Override
1404 public void updateApps(ArrayList<ApplicationInfo> list) {
1405 // We remove and re-add the updated applications list because it's properties may have
1406 // changed (ie. the title), and this will ensure that the items will be in their proper
1407 // place in the list.
1408 removeAppsWithoutInvalidate(list);
1409 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001410 updatePageCounts();
1411
Winson Chung785d2eb2011-04-14 16:08:02 -07001412 invalidatePageData();
1413 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001414
Winson Chung785d2eb2011-04-14 16:08:02 -07001415 @Override
1416 public void reset() {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001417 AppsCustomizeTabHost tabHost = getTabHost();
1418 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001419 if (tag != null) {
1420 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1421 tabHost.setCurrentTabFromContent(ContentType.Applications);
1422 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001423 }
1424 if (mCurrentPage != 0) {
1425 invalidatePageData(0);
1426 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001428
1429 private AppsCustomizeTabHost getTabHost() {
1430 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1431 }
1432
Winson Chung785d2eb2011-04-14 16:08:02 -07001433 @Override
1434 public void dumpState() {
1435 // TODO: Dump information related to current list of Applications, Widgets, etc.
1436 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1437 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1438 }
Adam Cohen4e844012011-11-09 13:48:04 -08001439
Winson Chung785d2eb2011-04-14 16:08:02 -07001440 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001441 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001442 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001443 for (Object i: list) {
1444 if (i instanceof AppWidgetProviderInfo) {
1445 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1446 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1447 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1448 + " initialLayout=" + info.initialLayout
1449 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1450 } else if (i instanceof ResolveInfo) {
1451 ResolveInfo info = (ResolveInfo) i;
1452 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1453 + info.icon);
1454 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 }
1456 }
Adam Cohen4e844012011-11-09 13:48:04 -08001457
Winson Chung785d2eb2011-04-14 16:08:02 -07001458 @Override
1459 public void surrender() {
1460 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1461 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001462
1463 // Stop all background tasks
1464 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001465 }
Winson Chung007c6982011-06-14 13:27:53 -07001466
Winson Chungb44b5242011-06-13 11:32:14 -07001467 /*
1468 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1469 * widget previews in the background with the AsyncTasks.
1470 */
1471 protected int getAssociatedLowerPageBound(int page) {
1472 return Math.max(0, page - 2);
1473 }
1474 protected int getAssociatedUpperPageBound(int page) {
1475 final int count = getChildCount();
1476 return Math.min(page + 2, count - 1);
1477 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001478
1479 @Override
1480 protected String getCurrentPageDescription() {
1481 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1482 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001483 int count = 0;
1484
Adam Cohen0cd3b642011-10-14 14:58:00 -07001485 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001486 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001487 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001488 } else {
1489 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001490 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001491 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001492 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001493
Adam Cohend3357b12011-10-18 14:58:11 -07001494 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001495 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001496}