blob: 691228d3a65a997cdb874f582c2eac1f7913ce45 [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;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080033import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070035import android.graphics.MaskFilter;
36import android.graphics.Paint;
Winson Chung70fc4382011-08-08 15:31:33 -070037import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.graphics.Rect;
Peter Ng8db70002011-10-25 15:40:08 -070039import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070041import android.os.AsyncTask;
42import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.util.AttributeSet;
44import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070045import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080046import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070048import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080050import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070051import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070053import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070054import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070055import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070056import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070057
58import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059import com.android.launcher2.DropTarget.DragObject;
60
61import java.util.ArrayList;
62import java.util.Collections;
63import java.util.Iterator;
64import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * A simple callback interface which also provides the results of the task.
68 */
69interface AsyncTaskCallback {
70 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
71}
Winson Chung4e076542011-06-23 13:04:10 -070072
Winson Chungb44b5242011-06-13 11:32:14 -070073/**
74 * The data needed to perform either of the custom AsyncTasks.
75 */
76class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070077 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080078 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070079 }
80
Winson Chungb44b5242011-06-13 11:32:14 -070081 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
82 AsyncTaskCallback postR) {
83 page = p;
84 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070085 sourceImages = si;
86 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070087 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070088 doInBackgroundCallback = bgR;
89 postExecuteCallback = postR;
90 }
Michael Jurka038f9d82011-11-03 13:50:45 -070091 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070092 AsyncTaskCallback postR) {
93 page = p;
94 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070095 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070096 maxImageWidth = cw;
97 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070098 doInBackgroundCallback = bgR;
99 postExecuteCallback = postR;
100 }
Winson Chung09945932011-09-20 14:22:40 -0700101 void cleanup(boolean cancelled) {
102 // Clean up any references to source/generated bitmaps
103 if (sourceImages != null) {
104 if (cancelled) {
105 for (Bitmap b : sourceImages) {
106 b.recycle();
107 }
108 }
109 sourceImages.clear();
110 }
111 if (generatedImages != null) {
112 if (cancelled) {
113 for (Bitmap b : generatedImages) {
114 b.recycle();
115 }
116 }
117 generatedImages.clear();
118 }
119 }
Winson Chungb44b5242011-06-13 11:32:14 -0700120 int page;
121 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700122 ArrayList<Bitmap> sourceImages;
123 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700124 int maxImageWidth;
125 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700126 AsyncTaskCallback doInBackgroundCallback;
127 AsyncTaskCallback postExecuteCallback;
128}
Winson Chung4e076542011-06-23 13:04:10 -0700129
Winson Chungb44b5242011-06-13 11:32:14 -0700130/**
131 * A generic template for an async task used in AppsCustomize.
132 */
133class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700134 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700135 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700136 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700137 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 }
139 @Override
140 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
141 if (params.length != 1) return null;
142 // Load each of the widget previews in the background
143 params[0].doInBackgroundCallback.run(this, params[0]);
144 return params[0];
145 }
146 @Override
147 protected void onPostExecute(AsyncTaskPageData result) {
148 // All the widget previews are loaded, so we can just callback to inflate the page
149 result.postExecuteCallback.run(this, result);
150 }
151
152 void setThreadPriority(int p) {
153 threadPriority = p;
154 }
155 void syncThreadPriority() {
156 Process.setThreadPriority(threadPriority);
157 }
158
159 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700160 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700161 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700162 int threadPriority;
163}
Winson Chungb44b5242011-06-13 11:32:14 -0700164
165/**
166 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
167 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700168public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungc6f10b92011-11-14 11:39:07 -0800169 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource {
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 static final String LOG_TAG = "AppsCustomizePagedView";
171
172 /**
173 * The different content types that this paged view can show.
174 */
175 public enum ContentType {
176 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700177 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700178 }
179
180 // Refs
181 private Launcher mLauncher;
182 private DragController mDragController;
183 private final LayoutInflater mLayoutInflater;
184 private final PackageManager mPackageManager;
185
Winson Chung5afbf7b2011-07-25 11:53:08 -0700186 // Save and Restore
187 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700188
Winson Chung785d2eb2011-04-14 16:08:02 -0700189 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700191 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700192
Winson Chung7d7541e2011-09-16 20:14:36 -0700193 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800194 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 private int mClingFocusedX;
196 private int mClingFocusedY;
197
Winson Chung1ed747a2011-05-03 16:18:34 -0700198 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700199 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700200 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700201 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700202 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700203
204 // Dimens
205 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700206 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800207 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700208 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700209 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700210 private final int mWidgetPreviewIconPaddedDimension;
211 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700212 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700213 private int mNumAppsPages;
214 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700215
Adam Cohen22f823d2011-09-01 17:22:18 -0700216 // Relating to the scroll and overscroll effects
217 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700218 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700219 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700220 private static float TRANSITION_PIVOT = 0.65f;
221 private static float TRANSITION_MAX_ROTATION = 22;
222 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700223 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700224 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700225
Winson Chungb44b5242011-06-13 11:32:14 -0700226 // Previews & outlines
227 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800228 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700229
Winson Chung785d2eb2011-04-14 16:08:02 -0700230 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
231 super(context, attrs);
232 mLayoutInflater = LayoutInflater.from(context);
233 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700234 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700235 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700236 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700237 mCanvas = new Canvas();
238 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700239
240 // Save the default widget preview background
241 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700242 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700243 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
244 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700245
Winson Chung6032e7e2011-11-08 15:47:17 -0800246 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
247 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
248 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 mWidgetWidthGap =
250 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
251 mWidgetHeightGap =
252 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700253 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
254 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700255 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
256 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700257 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700258 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700259
Winson Chung1ed747a2011-05-03 16:18:34 -0700260 // The padding on the non-matched dimension for the default widget preview icons
261 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700262 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700263 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700264 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700265 }
266
267 @Override
268 protected void init() {
269 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700270 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700271
272 Context context = getContext();
273 Resources r = context.getResources();
274 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
275 }
276
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700278 protected void onUnhandledTap(MotionEvent ev) {
279 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700280 // Dismiss AppsCustomize if we tap
281 mLauncher.showWorkspace(true);
282 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700283 }
284
Winson Chung5afbf7b2011-07-25 11:53:08 -0700285 /** Returns the item index of the center item on this page so that we can restore to this
286 * item index when we rotate. */
287 private int getMiddleComponentIndexOnCurrentPage() {
288 int i = -1;
289 if (getPageCount() > 0) {
290 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700291 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700292 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700293 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
294 int numItemsPerPage = mCellCountX * mCellCountY;
295 int childCount = childrenLayout.getChildCount();
296 if (childCount > 0) {
297 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700298 }
299 } else {
300 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700301 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700302 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
303 int childCount = layout.getChildCount();
304 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700305 i = numApps +
306 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
307 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700308 }
309 }
310 return i;
311 }
312
313 /** Get the index of the item to restore to if we need to restore the current page. */
314 int getSaveInstanceStateIndex() {
315 if (mSaveInstanceStateItemIndex == -1) {
316 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
317 }
318 return mSaveInstanceStateItemIndex;
319 }
320
321 /** Returns the page in the current orientation which is expected to contain the specified
322 * item index. */
323 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 if (index < 0) return 0;
325
326 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700327 int numItemsPerPage = mCellCountX * mCellCountY;
328 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700329 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700331 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
332 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 }
334
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 /**
336 * This differs from isDataReady as this is the test done if isDataReady is not set.
337 */
338 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700339 // We only do this test once, and we default to the Applications page, so we only really
340 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700341 // TODO: What if one of them is validly empty
342 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700343 }
344
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 /** Restores the page for an item at the specified index */
346 void restorePageForIndex(int index) {
347 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700348 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700349 }
350
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700351 private void updatePageCounts() {
352 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
353 (float) (mWidgetCountX * mWidgetCountY));
354 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
355 }
356
Winson Chungf0ea4d32011-06-06 14:27:16 -0700357 protected void onDataReady(int width, int height) {
358 // Note that we transpose the counts in portrait so that we get a similar layout
359 boolean isLandscape = getResources().getConfiguration().orientation ==
360 Configuration.ORIENTATION_LANDSCAPE;
361 int maxCellCountX = Integer.MAX_VALUE;
362 int maxCellCountY = Integer.MAX_VALUE;
363 if (LauncherApplication.isScreenLarge()) {
364 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
365 LauncherModel.getCellCountY());
366 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
367 LauncherModel.getCellCountX());
368 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800369 if (mMaxAppCellCountX > -1) {
370 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
371 }
372 if (mMaxAppCellCountY > -1) {
373 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
374 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375
376 // Now that the data is ready, we can calculate the content width, the number of cells to
377 // use for each page
378 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
379 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
380 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
381 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
382 mCellCountX = mWidgetSpacingLayout.getCellCountX();
383 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700384 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700385
Winson Chungdb1138b2011-06-30 14:39:35 -0700386 // Force a measure to update recalculate the gaps
387 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
388 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
389 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700390 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700391
Michael Jurkae326f182011-11-21 14:05:46 -0800392 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
393 final boolean hostIsTransitioning = host.isTransitioning();
394
Adam Cohen0cd3b642011-10-14 14:58:00 -0700395 // Restore the page
396 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800397 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700398
Winson Chung3f4e1422011-11-17 14:58:51 -0800399 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
400 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
401 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800402 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800403 post(new Runnable() {
404 @Override
405 public void run() {
406 showAllAppsCling();
407 }
408 });
409 }
410 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700411
Winson Chung3f4e1422011-11-17 14:58:51 -0800412 void showAllAppsCling() {
413 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
414 mHasShownAllAppsCling = true;
415 // Calculate the position for the cling punch through
416 int[] offset = new int[2];
417 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
418 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
419 // PagedViews are centered horizontally but top aligned
420 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
421 offset[0];
422 pos[1] += offset[1];
423 mLauncher.showFirstRunAllAppsCling(pos);
424 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700425 }
426
427 @Override
428 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
429 int width = MeasureSpec.getSize(widthMeasureSpec);
430 int height = MeasureSpec.getSize(heightMeasureSpec);
431 if (!isDataReady()) {
432 if (testDataReady()) {
433 setDataIsReady();
434 setMeasuredDimension(width, height);
435 onDataReady(width, height);
436 }
437 }
438
439 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
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();
Michael Jurkae3517d02012-01-12 07:46:24 -0800495 mLauncher.startActivitySafely(appInfo.intent, appInfo);
Winson Chung4b576be2011-04-27 17:40:20 -0700496 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700497 // Let the user know that they have to long press to add a widget
498 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
499 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700500
Winson Chungd2e87b32011-06-02 10:53:07 -0700501 // Create a little animation to show that the widget can move
502 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
503 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
504 AnimatorSet bounce = new AnimatorSet();
505 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
506 tyuAnim.setDuration(125);
507 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
508 tydAnim.setDuration(100);
509 bounce.play(tyuAnim).before(tydAnim);
510 bounce.setInterpolator(new AccelerateInterpolator());
511 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700512 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700513 }
514
Winson Chungc6f10b92011-11-14 11:39:07 -0800515 public boolean onKey(View v, int keyCode, KeyEvent event) {
516 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
517 }
518
Winson Chung785d2eb2011-04-14 16:08:02 -0700519 /*
520 * PagedViewWithDraggableItems implementation
521 */
522 @Override
523 protected void determineDraggingStart(android.view.MotionEvent ev) {
524 // Disable dragging by pulling an app down for now.
525 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700526
Winson Chung4b576be2011-04-27 17:40:20 -0700527 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700528 mLauncher.getWorkspace().onDragStartedWithItem(v);
529 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700530 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700531
Winson Chung4b576be2011-04-27 17:40:20 -0700532 private void beginDraggingWidget(View v) {
533 // Get the widget preview as the drag representation
534 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700535 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700536
537 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800538 Bitmap preview;
539 Bitmap outline;
Winson Chung1ed747a2011-05-03 16:18:34 -0700540 if (createItemInfo instanceof PendingAddWidgetInfo) {
541 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700542 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700543 createItemInfo.spanX = spanXY[0];
544 createItemInfo.spanY = spanXY[1];
545
Michael Jurka038f9d82011-11-03 13:50:45 -0700546 int[] maxSize = mLauncher.getWorkspace().estimateItemSize(spanXY[0], spanXY[1],
547 createWidgetInfo, true);
Winson Chung1120e032011-11-22 16:11:31 -0800548 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Michael Jurka038f9d82011-11-03 13:50:45 -0700549 createWidgetInfo.icon, spanXY[0], spanXY[1], maxSize[0], maxSize[1]);
Winson Chung1ed747a2011-05-03 16:18:34 -0700550 } else {
551 // Workaround for the fact that we don't keep the original ResolveInfo associated with
552 // the shortcut around. To get the icon, we just render the preview image (which has
553 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800554 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700555 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800556 Drawable d = image.getDrawable();
557 mCanvas.setBitmap(preview);
558 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700559 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700560 createItemInfo.spanX = createItemInfo.spanY = 1;
561 }
Winson Chung4b576be2011-04-27 17:40:20 -0700562
Peter Ng8db70002011-10-25 15:40:08 -0700563 // We use a custom alpha clip table for the default widget previews
564 Paint alphaClipPaint = null;
565 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700566 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700567 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
568 alphaClipPaint = new Paint();
569 alphaClipPaint.setMaskFilter(alphaClipTable);
570 }
571 }
572
Winson Chung1120e032011-11-22 16:11:31 -0800573 // Save the preview for the outline generation, then dim the preview
574 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
575 false);
576 mCanvas.setBitmap(preview);
577 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
578 mCanvas.setBitmap(null);
579
Winson Chung4b576be2011-04-27 17:40:20 -0700580 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800581 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700582 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800583 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
584 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700585 DragController.DRAG_ACTION_COPY, null);
Winson Chung1120e032011-11-22 16:11:31 -0800586 outline.recycle();
587 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700588 }
589 @Override
590 protected boolean beginDragging(View v) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700591 // Dismiss the cling
592 mLauncher.dismissAllAppsCling(null);
593
Winson Chung4b576be2011-04-27 17:40:20 -0700594 if (!super.beginDragging(v)) return false;
595
Winson Chungc07918d2011-07-01 15:35:26 -0700596 // Go into spring loaded mode (must happen before we startDrag())
Adam Cohen7777d962011-08-18 18:58:38 -0700597 mLauncher.enterSpringLoadedDragMode();
Winson Chungfc79c802011-05-02 13:35:34 -0700598
Winson Chung4b576be2011-04-27 17:40:20 -0700599 if (v instanceof PagedViewIcon) {
600 beginDraggingApplication(v);
601 } else if (v instanceof PagedViewWidget) {
602 beginDraggingWidget(v);
603 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700604 return true;
605 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700606 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700607 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700608 if (!success || (target != mLauncher.getWorkspace() &&
609 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 // Exit spring loaded mode if we have not successfully dropped or have not handled the
611 // drop in Workspace
612 mLauncher.exitSpringLoadedDragMode();
613 }
Adam Cohen446e9402011-09-15 18:21:21 -0700614 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chungb26f3d62011-06-02 10:49:29 -0700615
Winson Chung785d2eb2011-04-14 16:08:02 -0700616 }
617
Winson Chung785d2eb2011-04-14 16:08:02 -0700618 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700619 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700620 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700621
622 // Display an error message if the drag failed due to there not being enough space on the
623 // target layout we were dropping on.
624 if (!success) {
625 boolean showOutOfSpaceMessage = false;
626 if (target instanceof Workspace) {
627 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
628 Workspace workspace = (Workspace) target;
629 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700630 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700631 if (layout != null) {
632 layout.calculateSpans(itemInfo);
633 showOutOfSpaceMessage =
634 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
635 }
636 }
Winson Chungfc79c802011-05-02 13:35:34 -0700637 if (showOutOfSpaceMessage) {
638 mLauncher.showOutOfSpaceMessage();
639 }
640 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700641 }
642
Winson Chung7f0acdd2011-09-19 18:34:19 -0700643 @Override
644 protected void onDetachedFromWindow() {
645 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700646 cancelAllTasks();
647 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700648
Michael Jurkae326f182011-11-21 14:05:46 -0800649 public void clearAllWidgetPages() {
650 cancelAllTasks();
651 int count = getChildCount();
652 for (int i = 0; i < count; i++) {
653 View v = getPageAt(i);
654 if (v instanceof PagedViewGridLayout) {
655 ((PagedViewGridLayout) v).removeAllViewsOnPage();
656 mDirtyPageContent.set(i, true);
657 }
658 }
659 }
660
Adam Cohen0cd3b642011-10-14 14:58:00 -0700661 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700662 // Clean up all the async tasks
663 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
664 while (iter.hasNext()) {
665 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
666 task.cancel(false);
667 iter.remove();
668 }
669 }
670
Winson Chung785d2eb2011-04-14 16:08:02 -0700671 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700672 if (type == ContentType.Widgets) {
673 invalidatePageData(mNumAppsPages, true);
674 } else if (type == ContentType.Applications) {
675 invalidatePageData(0, true);
676 }
Winson Chungb44b5242011-06-13 11:32:14 -0700677 }
678
Adam Cohen0cd3b642011-10-14 14:58:00 -0700679 protected void snapToPage(int whichPage, int delta, int duration) {
680 super.snapToPage(whichPage, delta, duration);
681 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800682
683 // Update the thread priorities given the direction lookahead
684 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
685 while (iter.hasNext()) {
686 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
687 int pageIndex = task.page + mNumAppsPages;
688 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
689 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
690 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
691 } else {
692 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
693 }
694 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700695 }
696
697 private void updateCurrentTab(int currentPage) {
698 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800699 if (tabHost != null) {
700 String tag = tabHost.getCurrentTabTag();
701 if (tag != null) {
702 if (currentPage >= mNumAppsPages &&
703 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
704 tabHost.setCurrentTabFromContent(ContentType.Widgets);
705 } else if (currentPage < mNumAppsPages &&
706 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
707 tabHost.setCurrentTabFromContent(ContentType.Applications);
708 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700709 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700710 }
711 }
712
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 /*
714 * Apps PagedView implementation
715 */
Winson Chung63257c12011-05-05 17:06:13 -0700716 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
717 int childCount = layout.getChildCount();
718 for (int i = 0; i < childCount; ++i) {
719 layout.getChildAt(i).setVisibility(visibility);
720 }
721 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700722 private void setupPage(PagedViewCellLayout layout) {
723 layout.setCellCount(mCellCountX, mCellCountY);
724 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
725 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
726 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
727
Winson Chung63257c12011-05-05 17:06:13 -0700728 // Note: We force a measure here to get around the fact that when we do layout calculations
729 // immediately after syncing, we don't have a proper width. That said, we already know the
730 // expected page width, so we can actually optimize by hiding all the TextView-based
731 // children that are expensive to measure, and let that happen naturally later.
732 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700733 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700734 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700735 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700736 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700737 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700738 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700739
Winson Chungf314b0e2011-08-16 11:54:27 -0700740 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700741 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700742 int numCells = mCellCountX * mCellCountY;
743 int startIndex = page * numCells;
744 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700745 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700746
Winson Chung785d2eb2011-04-14 16:08:02 -0700747 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700748 ArrayList<Object> items = new ArrayList<Object>();
749 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700750 for (int i = startIndex; i < endIndex; ++i) {
751 ApplicationInfo info = mApps.get(i);
752 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
753 R.layout.apps_customize_application, layout, false);
Michael Jurka82369a12012-01-12 08:08:38 -0800754 icon.applyFromApplicationInfo(info, true);
Winson Chung785d2eb2011-04-14 16:08:02 -0700755 icon.setOnClickListener(this);
756 icon.setOnLongClickListener(this);
757 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800758 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700759
760 int index = i - startIndex;
761 int x = index % mCellCountX;
762 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700763 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700764
765 items.add(info);
766 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700767 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700768
Winson Chungf0ea4d32011-06-06 14:27:16 -0700769 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700770 }
Winson Chungb44b5242011-06-13 11:32:14 -0700771
772 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800773 * A helper to return the priority for loading of the specified widget page.
774 */
775 private int getWidgetPageLoadPriority(int page) {
776 // If we are snapping to another page, use that index as the target page index
777 int toPage = mCurrentPage;
778 if (mNextPage > -1) {
779 toPage = mNextPage;
780 }
781
782 // We use the distance from the target page as an initial guess of priority, but if there
783 // are no pages of higher priority than the page specified, then bump up the priority of
784 // the specified page.
785 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
786 int minPageDiff = Integer.MAX_VALUE;
787 while (iter.hasNext()) {
788 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
789 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
790 }
791
792 int rawPageDiff = Math.abs(page - toPage);
793 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
794 }
795 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700796 * Return the appropriate thread priority for loading for a given page (we give the current
797 * page much higher priority)
798 */
799 private int getThreadPriorityForPage(int page) {
800 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800801 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700802 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800803 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700804 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800805 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700806 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800807 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700808 }
809 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700810 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800811 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700812 return Math.max(0, pageDiff * sPageSleepDelay);
813 }
Winson Chungb44b5242011-06-13 11:32:14 -0700814 /**
815 * Creates and executes a new AsyncTask to load a page of widget previews.
816 */
817 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700818 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800819
Winson Chungb44b5242011-06-13 11:32:14 -0700820 // Prune all tasks that are no longer needed
821 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
822 while (iter.hasNext()) {
823 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800824 int taskPage = task.page + mNumAppsPages;
825 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
826 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700827 task.cancel(false);
828 iter.remove();
829 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800830 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700831 }
832 }
833
Winson Chungf314b0e2011-08-16 11:54:27 -0700834 // 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 -0700835 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700836 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700837 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700838 @Override
839 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700840 try {
Winson Chung09945932011-09-20 14:22:40 -0700841 try {
842 Thread.sleep(sleepMs);
843 } catch (Exception e) {}
844 loadWidgetPreviewsInBackground(task, data);
845 } finally {
846 if (task.isCancelled()) {
847 data.cleanup(true);
848 }
849 }
Winson Chungb44b5242011-06-13 11:32:14 -0700850 }
851 },
852 new AsyncTaskCallback() {
853 @Override
854 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700855 try {
856 mRunningTasks.remove(task);
857 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700858 onSyncWidgetPageItems(data);
859 } finally {
860 data.cleanup(task.isCancelled());
861 }
Winson Chungb44b5242011-06-13 11:32:14 -0700862 }
Winson Chung09945932011-09-20 14:22:40 -0700863 });
Winson Chungb44b5242011-06-13 11:32:14 -0700864
865 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700866 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700867 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800868 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700869 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
870 mRunningTasks.add(t);
871 }
Winson Chungb44b5242011-06-13 11:32:14 -0700872
Winson Chung785d2eb2011-04-14 16:08:02 -0700873 /*
874 * Widgets PagedView implementation
875 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700876 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700877 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
878 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700879
880 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700881 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700882 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
883 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700884 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700885 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700886 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700887
Winson Chung5fc72b32011-10-11 17:53:58 -0700888 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
889 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -0700890 }
Michael Jurka92f3d462011-11-22 21:02:29 -0800891
Winson Chung5fc72b32011-10-11 17:53:58 -0700892 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
893 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700894 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700895 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -0700896 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -0700897 Rect oldBounds = d.copyBounds();
898 d.setBounds(x, y, x + w, y + h);
899 d.draw(c);
900 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700901 if (multiplyColor != 0xFFFFFFFF) {
902 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
903 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700904 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700905 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 }
Michael Jurka038f9d82011-11-03 13:50:45 -0700907 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700908 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -0700909 int offset = 0;
910 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -0700911 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -0700912
Winson Chung1ed747a2011-05-03 16:18:34 -0700913 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -0700914 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -0700915 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -0700916 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700917 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700918
Michael Jurka038f9d82011-11-03 13:50:45 -0700919 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
920 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -0700921 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -0700922 String packageName = provider.getPackageName();
923 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
924 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -0700925
Michael Jurka038f9d82011-11-03 13:50:45 -0700926 Drawable drawable = null;
927 if (previewImage != 0) {
928 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
929 if (drawable == null) {
930 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
931 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -0700932 }
933 }
934
Michael Jurka038f9d82011-11-03 13:50:45 -0700935 int bitmapWidth;
936 int bitmapHeight;
937 boolean widgetPreviewExists = (drawable != null);
938 if (widgetPreviewExists) {
939 bitmapWidth = drawable.getIntrinsicWidth();
940 bitmapHeight = drawable.getIntrinsicHeight();
941
942 // Cap the size so widget previews don't appear larger than the actual widget
943 maxWidth = Math.min(maxWidth, mWidgetSpacingLayout.estimateCellWidth(cellHSpan));
944 maxHeight = Math.min(maxHeight, mWidgetSpacingLayout.estimateCellHeight(cellVSpan));
945 } else {
946 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -0700947 // TODO: This actually uses the apps customize cell layout params, where as we make want
948 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -0700949 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
950 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -0700951 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -0700952 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -0700953 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -0700954 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -0700955 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -0700956 } else {
Peter Ng8db70002011-10-25 15:40:08 -0700957 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -0700958 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700959 }
Michael Jurka038f9d82011-11-03 13:50:45 -0700960 }
961
962 float scale = 1f;
963 if (bitmapWidth > maxWidth) {
964 scale = maxWidth / (float) bitmapWidth;
965 }
966 if (bitmapHeight * scale > maxHeight) {
967 scale = maxHeight / (float) bitmapHeight;
968 }
969 if (scale != 1f) {
970 bitmapWidth = (int) (scale * bitmapWidth);
971 bitmapHeight = (int) (scale * bitmapHeight);
972 }
973
974 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
975
976 if (widgetPreviewExists) {
977 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
978 } else {
979 // Generate a preview image if we couldn't load one
980 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
981 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
982 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -0700983 if (cellHSpan != 1 || cellVSpan != 1) {
984 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
985 bitmapHeight);
986 }
Winson Chung4b576be2011-04-27 17:40:20 -0700987
988 // Draw the icon in the top left corner
989 try {
990 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -0700991 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
992 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -0700993 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -0800994 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -0700995 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
996
Peter Ng8db70002011-10-25 15:40:08 -0700997 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
998 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -0700999 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001000 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001001 }
Winson Chungb44b5242011-06-13 11:32:14 -07001002 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001003 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001004
Michael Jurka038f9d82011-11-03 13:50:45 -07001005 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001006 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001007
Winson Chungd2945262011-06-24 15:22:14 -07001008 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001009 final ArrayList<Object> items = new ArrayList<Object>();
1010 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1011 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001012 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001013 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1014 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001015 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001016
Winson Chunge4a647f2011-09-30 14:41:25 -07001017 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001018 int offset = page * numItemsPerPage;
1019 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1020 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001021 }
1022
Winson Chunge4a647f2011-09-30 14:41:25 -07001023 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka038f9d82011-11-03 13:50:45 -07001024 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001025 layout.setColumnCount(layout.getCellCountX());
1026 for (int i = 0; i < items.size(); ++i) {
1027 Object rawInfo = items.get(i);
1028 PendingAddItemInfo createItemInfo = null;
1029 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1030 R.layout.apps_customize_widget, layout, false);
1031 if (rawInfo instanceof AppWidgetProviderInfo) {
1032 // Fill in the widget information
1033 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1034 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1035 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka82369a12012-01-12 08:08:38 -08001036 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans);
Winson Chunge4a647f2011-09-30 14:41:25 -07001037 widget.setTag(createItemInfo);
1038 } else if (rawInfo instanceof ResolveInfo) {
1039 // Fill in the shortcuts information
1040 ResolveInfo info = (ResolveInfo) rawInfo;
1041 createItemInfo = new PendingAddItemInfo();
1042 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1043 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1044 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001045 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001046 widget.setTag(createItemInfo);
1047 }
1048 widget.setOnClickListener(this);
1049 widget.setOnLongClickListener(this);
1050 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001051 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001052
1053 // Layout each widget
1054 int ix = i % mWidgetCountX;
1055 int iy = i / mWidgetCountX;
1056 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1057 GridLayout.spec(iy, GridLayout.LEFT),
1058 GridLayout.spec(ix, GridLayout.TOP));
1059 lp.width = cellWidth;
1060 lp.height = cellHeight;
1061 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1062 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1063 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1064 layout.addView(widget, lp);
1065 }
1066
Michael Jurka038f9d82011-11-03 13:50:45 -07001067 // wait until a call on onLayout to start loading, because
1068 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1069 // TODO: can we do a measure/layout immediately?
1070 layout.setOnLayoutListener(new Runnable() {
1071 public void run() {
1072 // Load the widget previews
1073 int maxPreviewWidth = cellWidth;
1074 int maxPreviewHeight = cellHeight;
1075 if (layout.getChildCount() > 0) {
1076 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1077 int[] maxSize = w.getPreviewSize();
1078 maxPreviewWidth = maxSize[0];
1079 maxPreviewHeight = maxSize[1];
1080 }
1081 if (immediate) {
1082 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1083 maxPreviewWidth, maxPreviewHeight, null, null);
1084 loadWidgetPreviewsInBackground(null, data);
1085 onSyncWidgetPageItems(data);
1086 } else {
1087 prepareLoadWidgetPreviewsTask(page, items,
1088 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1089 }
1090 }
1091 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001092 }
1093 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1094 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001095 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1096 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001097 if (task != null) {
1098 // Ensure that this task starts running at the correct priority
1099 task.syncThreadPriority();
1100 }
1101
1102 // Load each of the widget/shortcut previews
1103 ArrayList<Object> items = data.items;
1104 ArrayList<Bitmap> images = data.generatedImages;
1105 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001106 for (int i = 0; i < count; ++i) {
1107 if (task != null) {
1108 // Ensure we haven't been cancelled yet
1109 if (task.isCancelled()) break;
1110 // Before work on each item, ensure that this task is running at the correct
1111 // priority
1112 task.syncThreadPriority();
1113 }
1114
1115 Object rawInfo = items.get(i);
1116 if (rawInfo instanceof AppWidgetProviderInfo) {
1117 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001118 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001119 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
1120 cellSpans[0], cellSpans[1], data.maxImageWidth, data.maxImageHeight);
1121 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001122 } else if (rawInfo instanceof ResolveInfo) {
1123 // Fill in the shortcuts information
1124 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001125 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001126 }
1127 }
Winson Chungb44b5242011-06-13 11:32:14 -07001128 }
1129 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1130 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001131 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001132
Winson Chungb44b5242011-06-13 11:32:14 -07001133 ArrayList<Object> items = data.items;
1134 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001135 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001136 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1137 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001138 Bitmap preview = data.generatedImages.get(i);
Michael Jurka038f9d82011-11-03 13:50:45 -07001139 widget.applyPreview(new FastBitmapDrawable(preview), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001140 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 }
Winson Chungb44b5242011-06-13 11:32:14 -07001142
Winson Chung68e4c642011-11-10 15:48:25 -08001143 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001144 invalidate();
Winson Chung68e4c642011-11-10 15:48:25 -08001145
Winson Chung68e4c642011-11-10 15:48:25 -08001146 // Update all thread priorities
1147 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1148 while (iter.hasNext()) {
1149 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1150 int pageIndex = task.page + mNumAppsPages;
1151 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1152 }
Winson Chungb44b5242011-06-13 11:32:14 -07001153 }
Winson Chung46af2e82011-05-09 16:00:53 -07001154
Winson Chung785d2eb2011-04-14 16:08:02 -07001155 @Override
1156 public void syncPages() {
1157 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001158 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001159
Adam Cohen0cd3b642011-10-14 14:58:00 -07001160 Context context = getContext();
1161 for (int j = 0; j < mNumWidgetPages; ++j) {
1162 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1163 mWidgetCountY);
1164 setupPage(layout);
1165 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1166 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001167 }
1168
Adam Cohen0cd3b642011-10-14 14:58:00 -07001169 for (int i = 0; i < mNumAppsPages; ++i) {
1170 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1171 setupPage(layout);
1172 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 }
1174 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001175
Winson Chung785d2eb2011-04-14 16:08:02 -07001176 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001177 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001178 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001179 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001180 } else {
1181 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001182 }
1183 }
1184
Adam Cohen22f823d2011-09-01 17:22:18 -07001185 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1186 // it is in the z-order. This is important to insure touch events are handled correctly.
1187 View getPageAt(int index) {
1188 return getChildAt(getChildCount() - index - 1);
1189 }
1190
Adam Cohenae4f1552011-10-20 00:15:42 -07001191 @Override
1192 protected int indexToPage(int index) {
1193 return getChildCount() - index - 1;
1194 }
1195
Adam Cohen22f823d2011-09-01 17:22:18 -07001196 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1197 @Override
1198 protected void screenScrolled(int screenCenter) {
1199 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001200
1201 for (int i = 0; i < getChildCount(); i++) {
1202 View v = getPageAt(i);
1203 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001204 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001205
1206 float interpolatedProgress =
1207 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1208 float scale = (1 - interpolatedProgress) +
1209 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1210 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001211
Adam Cohen2591f6a2011-10-25 14:36:40 -07001212 float alpha;
1213
1214 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1215 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001216 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001217 } else {
1218 // On large screens we need to fade the page as it nears its leftmost position
1219 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1220 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001221
1222 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1223 int pageWidth = v.getMeasuredWidth();
1224 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001225
1226 if (PERFORM_OVERSCROLL_ROTATION) {
1227 if (i == 0 && scrollProgress < 0) {
1228 // Overscroll to the left
1229 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1230 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1231 scale = 1.0f;
1232 alpha = 1.0f;
1233 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001234 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001235 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1236 // Overscroll to the right
1237 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1238 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1239 scale = 1.0f;
1240 alpha = 1.0f;
1241 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001242 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001243 } else {
1244 v.setPivotY(pageHeight / 2.0f);
1245 v.setPivotX(pageWidth / 2.0f);
1246 v.setRotationY(0f);
1247 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001248 }
1249
1250 v.setTranslationX(translationX);
1251 v.setScaleX(scale);
1252 v.setScaleY(scale);
1253 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001254
1255 // If the view has 0 alpha, we set it to be invisible so as to prevent
1256 // it from accepting touches
1257 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1258 v.setVisibility(INVISIBLE);
1259 } else if (v.getVisibility() != VISIBLE) {
1260 v.setVisibility(VISIBLE);
1261 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001262 }
1263 }
1264 }
1265
1266 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001267 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001268 }
1269
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 /**
1271 * Used by the parent to get the content width to set the tab bar to
1272 * @return
1273 */
1274 public int getPageContentWidth() {
1275 return mContentWidth;
1276 }
1277
Winson Chungb26f3d62011-06-02 10:49:29 -07001278 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001279 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001280 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001281
1282 // We reset the save index when we change pages so that it will be recalculated on next
1283 // rotation
1284 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001285 }
1286
Winson Chung785d2eb2011-04-14 16:08:02 -07001287 /*
1288 * AllAppsView implementation
1289 */
1290 @Override
1291 public void setup(Launcher launcher, DragController dragController) {
1292 mLauncher = launcher;
1293 mDragController = dragController;
1294 }
1295 @Override
1296 public void zoom(float zoom, boolean animate) {
1297 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1298 }
1299 @Override
1300 public boolean isVisible() {
1301 return (getVisibility() == VISIBLE);
1302 }
1303 @Override
1304 public boolean isAnimating() {
1305 return false;
1306 }
1307 @Override
1308 public void setApps(ArrayList<ApplicationInfo> list) {
1309 mApps = list;
1310 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001311 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001312
Winson Chung875de7e2011-06-28 14:25:17 -07001313 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1314 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001315 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001316 }
1317 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1318 // We add it in place, in alphabetical order
1319 int count = list.size();
1320 for (int i = 0; i < count; ++i) {
1321 ApplicationInfo info = list.get(i);
1322 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1323 if (index < 0) {
1324 mApps.add(-(index + 1), info);
1325 }
1326 }
1327 }
1328 @Override
1329 public void addApps(ArrayList<ApplicationInfo> list) {
1330 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001331 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 invalidatePageData();
1333 }
1334 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1335 ComponentName removeComponent = item.intent.getComponent();
1336 int length = list.size();
1337 for (int i = 0; i < length; ++i) {
1338 ApplicationInfo info = list.get(i);
1339 if (info.intent.getComponent().equals(removeComponent)) {
1340 return i;
1341 }
1342 }
1343 return -1;
1344 }
1345 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1346 // loop through all the apps and remove apps that have the same component
1347 int length = list.size();
1348 for (int i = 0; i < length; ++i) {
1349 ApplicationInfo info = list.get(i);
1350 int removeIndex = findAppByComponent(mApps, info);
1351 if (removeIndex > -1) {
1352 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001353 }
1354 }
1355 }
1356 @Override
1357 public void removeApps(ArrayList<ApplicationInfo> list) {
1358 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001359 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 invalidatePageData();
1361 }
1362 @Override
1363 public void updateApps(ArrayList<ApplicationInfo> list) {
1364 // We remove and re-add the updated applications list because it's properties may have
1365 // changed (ie. the title), and this will ensure that the items will be in their proper
1366 // place in the list.
1367 removeAppsWithoutInvalidate(list);
1368 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001369 updatePageCounts();
1370
Winson Chung785d2eb2011-04-14 16:08:02 -07001371 invalidatePageData();
1372 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001373
Winson Chung785d2eb2011-04-14 16:08:02 -07001374 @Override
1375 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001376 // If we have reset, then we should not continue to restore the previous state
1377 mSaveInstanceStateItemIndex = -1;
1378
Adam Cohenb64d36e2011-10-17 21:48:02 -07001379 AppsCustomizeTabHost tabHost = getTabHost();
1380 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001381 if (tag != null) {
1382 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1383 tabHost.setCurrentTabFromContent(ContentType.Applications);
1384 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001385 }
Winson Chung649668f2012-01-10 13:07:16 -08001386
Adam Cohenb64d36e2011-10-17 21:48:02 -07001387 if (mCurrentPage != 0) {
1388 invalidatePageData(0);
1389 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001390 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001391
1392 private AppsCustomizeTabHost getTabHost() {
1393 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1394 }
1395
Winson Chung785d2eb2011-04-14 16:08:02 -07001396 @Override
1397 public void dumpState() {
1398 // TODO: Dump information related to current list of Applications, Widgets, etc.
1399 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1400 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1401 }
Adam Cohen4e844012011-11-09 13:48:04 -08001402
Winson Chung785d2eb2011-04-14 16:08:02 -07001403 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001404 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001405 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001406 for (Object i: list) {
1407 if (i instanceof AppWidgetProviderInfo) {
1408 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1409 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1410 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1411 + " initialLayout=" + info.initialLayout
1412 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1413 } else if (i instanceof ResolveInfo) {
1414 ResolveInfo info = (ResolveInfo) i;
1415 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1416 + info.icon);
1417 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001418 }
1419 }
Adam Cohen4e844012011-11-09 13:48:04 -08001420
Winson Chung785d2eb2011-04-14 16:08:02 -07001421 @Override
1422 public void surrender() {
1423 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1424 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001425
1426 // Stop all background tasks
1427 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001428 }
Winson Chung007c6982011-06-14 13:27:53 -07001429
Winson Chung68e4c642011-11-10 15:48:25 -08001430
Winson Chungb44b5242011-06-13 11:32:14 -07001431 /*
1432 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1433 * widget previews in the background with the AsyncTasks.
1434 */
Winson Chung68e4c642011-11-10 15:48:25 -08001435 final static int sLookBehindPageCount = 2;
1436 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001437 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001438 final int count = getChildCount();
1439 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1440 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1441 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001442 }
1443 protected int getAssociatedUpperPageBound(int page) {
1444 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001445 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1446 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1447 count - 1);
1448 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001449 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001450
1451 @Override
1452 protected String getCurrentPageDescription() {
1453 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1454 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001455 int count = 0;
1456
Adam Cohen0cd3b642011-10-14 14:58:00 -07001457 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001458 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001459 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001460 } else {
1461 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001462 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001463 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001464 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001465
Adam Cohend3357b12011-10-18 14:58:11 -07001466 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001467 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001468}