blob: 4712a37b8b85be5bc4bc4a2d83681d1675cbb821 [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;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import 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;
Winson Chung72d59842012-02-22 13:51:36 -080037import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.Paint;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080040import 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 Chungc6f10b92011-11-14 11:39:07 -080048import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070050import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.view.View;
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 {
Michael Jurka82369a12012-01-12 08:08:38 -080079 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070080 }
81
Winson Chungb44b5242011-06-13 11:32:14 -070082 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
83 AsyncTaskCallback postR) {
84 page = p;
85 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070086 sourceImages = si;
87 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070088 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070089 doInBackgroundCallback = bgR;
90 postExecuteCallback = postR;
91 }
Michael Jurka038f9d82011-11-03 13:50:45 -070092 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070093 AsyncTaskCallback postR) {
94 page = p;
95 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070096 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070097 maxImageWidth = cw;
98 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070099 doInBackgroundCallback = bgR;
100 postExecuteCallback = postR;
101 }
Winson Chung09945932011-09-20 14:22:40 -0700102 void cleanup(boolean cancelled) {
103 // Clean up any references to source/generated bitmaps
104 if (sourceImages != null) {
105 if (cancelled) {
106 for (Bitmap b : sourceImages) {
107 b.recycle();
108 }
109 }
110 sourceImages.clear();
111 }
112 if (generatedImages != null) {
113 if (cancelled) {
114 for (Bitmap b : generatedImages) {
115 b.recycle();
116 }
117 }
118 generatedImages.clear();
119 }
120 }
Winson Chungb44b5242011-06-13 11:32:14 -0700121 int page;
122 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700123 ArrayList<Bitmap> sourceImages;
124 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700125 int maxImageWidth;
126 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700127 AsyncTaskCallback doInBackgroundCallback;
128 AsyncTaskCallback postExecuteCallback;
129}
Winson Chung4e076542011-06-23 13:04:10 -0700130
Winson Chungb44b5242011-06-13 11:32:14 -0700131/**
132 * A generic template for an async task used in AppsCustomize.
133 */
134class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700135 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700136 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700137 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700138 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 }
140 @Override
141 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
142 if (params.length != 1) return null;
143 // Load each of the widget previews in the background
144 params[0].doInBackgroundCallback.run(this, params[0]);
145 return params[0];
146 }
147 @Override
148 protected void onPostExecute(AsyncTaskPageData result) {
149 // All the widget previews are loaded, so we can just callback to inflate the page
150 result.postExecuteCallback.run(this, result);
151 }
152
153 void setThreadPriority(int p) {
154 threadPriority = p;
155 }
156 void syncThreadPriority() {
157 Process.setThreadPriority(threadPriority);
158 }
159
160 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700161 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700162 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int threadPriority;
164}
Winson Chungb44b5242011-06-13 11:32:14 -0700165
166/**
167 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
168 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700169public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800170 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700171 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
172 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700173 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700174
175 /**
176 * The different content types that this paged view can show.
177 */
178 public enum ContentType {
179 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700180 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700181 }
182
183 // Refs
184 private Launcher mLauncher;
185 private DragController mDragController;
186 private final LayoutInflater mLayoutInflater;
187 private final PackageManager mPackageManager;
188
Winson Chung5afbf7b2011-07-25 11:53:08 -0700189 // Save and Restore
190 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800191 private PagedViewIcon mPressedIcon;
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
Winson Chung3f4e1422011-11-17 14:58:51 -0800198 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700199 private int mClingFocusedX;
200 private int mClingFocusedY;
201
Winson Chung1ed747a2011-05-03 16:18:34 -0700202 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700203 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700204 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700205 private IconCache mIconCache;
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;
Winson Chung68e4c642011-11-10 15:48:25 -0800231 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700232
Adam Cohened66b2b2012-01-23 17:28:51 -0800233 private Runnable mInflateWidgetRunnable = null;
234 private Runnable mBindWidgetRunnable = null;
235 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
236 static final int WIDGET_BOUND = 0;
237 static final int WIDGET_INFLATED = 1;
238 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
239 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800240 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800241 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800242
Michael Jurka39e5d172012-03-12 18:36:12 -0700243 // Deferral of loading widget previews during launcher transitions
244 private boolean mInTransition;
245 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
246 new ArrayList<AsyncTaskPageData>();
247
Winson Chung785d2eb2011-04-14 16:08:02 -0700248 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
249 super(context, attrs);
250 mLayoutInflater = LayoutInflater.from(context);
251 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700252 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700254 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700255 mCanvas = new Canvas();
256 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700257
258 // Save the default widget preview background
259 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700260 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700261 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700262
Winson Chung6032e7e2011-11-08 15:47:17 -0800263 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
264 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
265 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 mWidgetWidthGap =
267 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
268 mWidgetHeightGap =
269 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700270 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
271 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700272 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
273 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700274 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700275 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700276
Winson Chung1ed747a2011-05-03 16:18:34 -0700277 // The padding on the non-matched dimension for the default widget preview icons
278 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700279 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700280 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700281 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700282
283 // Unless otherwise specified this view is important for accessibility.
284 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
285 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
286 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700287 }
288
289 @Override
290 protected void init() {
291 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700292 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700293
294 Context context = getContext();
295 Resources r = context.getResources();
296 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
297 }
298
Winson Chungf0ea4d32011-06-06 14:27:16 -0700299 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700300 protected void onUnhandledTap(MotionEvent ev) {
301 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700302 // Dismiss AppsCustomize if we tap
303 mLauncher.showWorkspace(true);
304 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700305 }
306
Winson Chung5afbf7b2011-07-25 11:53:08 -0700307 /** Returns the item index of the center item on this page so that we can restore to this
308 * item index when we rotate. */
309 private int getMiddleComponentIndexOnCurrentPage() {
310 int i = -1;
311 if (getPageCount() > 0) {
312 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700313 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700314 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700315 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
316 int numItemsPerPage = mCellCountX * mCellCountY;
317 int childCount = childrenLayout.getChildCount();
318 if (childCount > 0) {
319 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700320 }
321 } else {
322 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700323 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700324 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
325 int childCount = layout.getChildCount();
326 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700327 i = numApps +
328 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
329 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 }
331 }
332 return i;
333 }
334
335 /** Get the index of the item to restore to if we need to restore the current page. */
336 int getSaveInstanceStateIndex() {
337 if (mSaveInstanceStateItemIndex == -1) {
338 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
339 }
340 return mSaveInstanceStateItemIndex;
341 }
342
343 /** Returns the page in the current orientation which is expected to contain the specified
344 * item index. */
345 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700346 if (index < 0) return 0;
347
348 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700349 int numItemsPerPage = mCellCountX * mCellCountY;
350 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700351 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700352 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
354 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700355 }
356
Winson Chungf0ea4d32011-06-06 14:27:16 -0700357 /**
358 * This differs from isDataReady as this is the test done if isDataReady is not set.
359 */
360 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700361 // We only do this test once, and we default to the Applications page, so we only really
362 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700363 // TODO: What if one of them is validly empty
364 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700365 }
366
Winson Chung5afbf7b2011-07-25 11:53:08 -0700367 /** Restores the page for an item at the specified index */
368 void restorePageForIndex(int index) {
369 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700370 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700371 }
372
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700373 private void updatePageCounts() {
374 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
375 (float) (mWidgetCountX * mWidgetCountY));
376 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
377 }
378
Winson Chungf0ea4d32011-06-06 14:27:16 -0700379 protected void onDataReady(int width, int height) {
380 // Note that we transpose the counts in portrait so that we get a similar layout
381 boolean isLandscape = getResources().getConfiguration().orientation ==
382 Configuration.ORIENTATION_LANDSCAPE;
383 int maxCellCountX = Integer.MAX_VALUE;
384 int maxCellCountY = Integer.MAX_VALUE;
385 if (LauncherApplication.isScreenLarge()) {
386 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
387 LauncherModel.getCellCountY());
388 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
389 LauncherModel.getCellCountX());
390 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800391 if (mMaxAppCellCountX > -1) {
392 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
393 }
394 if (mMaxAppCellCountY > -1) {
395 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
396 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700397
398 // Now that the data is ready, we can calculate the content width, the number of cells to
399 // use for each page
400 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
401 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
402 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
403 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
404 mCellCountX = mWidgetSpacingLayout.getCellCountX();
405 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700406 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700407
Winson Chungdb1138b2011-06-30 14:39:35 -0700408 // Force a measure to update recalculate the gaps
409 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
410 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
411 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700412 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700413
Michael Jurkae326f182011-11-21 14:05:46 -0800414 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
415 final boolean hostIsTransitioning = host.isTransitioning();
416
Adam Cohen0cd3b642011-10-14 14:58:00 -0700417 // Restore the page
418 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800419 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700420
Winson Chung3f4e1422011-11-17 14:58:51 -0800421 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
422 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
423 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800424 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800425 post(new Runnable() {
426 @Override
427 public void run() {
428 showAllAppsCling();
429 }
430 });
431 }
432 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700433
Winson Chung3f4e1422011-11-17 14:58:51 -0800434 void showAllAppsCling() {
435 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
436 mHasShownAllAppsCling = true;
437 // Calculate the position for the cling punch through
438 int[] offset = new int[2];
439 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
440 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
441 // PagedViews are centered horizontally but top aligned
442 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
443 offset[0];
444 pos[1] += offset[1];
445 mLauncher.showFirstRunAllAppsCling(pos);
446 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
448
449 @Override
450 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
451 int width = MeasureSpec.getSize(widthMeasureSpec);
452 int height = MeasureSpec.getSize(heightMeasureSpec);
453 if (!isDataReady()) {
454 if (testDataReady()) {
455 setDataIsReady();
456 setMeasuredDimension(width, height);
457 onDataReady(width, height);
458 }
459 }
460
461 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
462 }
463
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700465 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
466 // by a broadcast receiver, and in order for it to work correctly, we need to know that
467 // the AppWidgetService has already received and processed the same broadcast. Since there
468 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
469 // we should have a more precise way of ensuring the AppWidgetService is up to date.
470 postDelayed(new Runnable() {
471 public void run() {
472 updatePackages();
473 }
474 }, 500);
475 }
476
477 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700478 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700479 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700480 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700481 List<AppWidgetProviderInfo> widgets =
482 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700483 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
484 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800485 for (AppWidgetProviderInfo widget : widgets) {
486 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700487 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700488 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
489 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700490 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
491 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700492 if (minSpanX <= LauncherModel.getCellCountX() &&
493 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700494 mWidgets.add(widget);
495 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800496 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700497 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800498 widget.minWidth + ", " + widget.minHeight + ")");
499 }
500 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700501 mWidgets.addAll(shortcuts);
502 Collections.sort(mWidgets,
503 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700504 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700505
Winson Chung875de7e2011-06-28 14:25:17 -0700506 if (wasEmpty) {
507 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
508 // a layout to do this test and invalidate the page data when ready.
509 if (testDataReady()) requestLayout();
510 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700511 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700512 invalidatePageData();
513 }
Winson Chung4b576be2011-04-27 17:40:20 -0700514 }
515
516 @Override
517 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700518 // When we have exited all apps or are in transition, disregard clicks
519 if (!mLauncher.isAllAppsCustomizeOpen() ||
520 mLauncher.getWorkspace().isSwitchingState()) return;
521
Winson Chung4b576be2011-04-27 17:40:20 -0700522 if (v instanceof PagedViewIcon) {
523 // Animate some feedback to the click
524 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800525
526 // Lock the drawable state to pressed until we return to Launcher
527 if (mPressedIcon != null) {
528 mPressedIcon.lockDrawableState();
529 }
Winson Chungc7450e32012-04-17 17:34:08 -0700530
Winson Chung18f41f82012-05-09 13:28:10 -0700531 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
532 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
533 // when Launcher resumes and we are still in AllApps.
534 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700535 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
536
Winson Chung4b576be2011-04-27 17:40:20 -0700537 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700538 // Let the user know that they have to long press to add a widget
539 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
540 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700541
Winson Chungd2e87b32011-06-02 10:53:07 -0700542 // Create a little animation to show that the widget can move
543 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
544 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
545 AnimatorSet bounce = new AnimatorSet();
546 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
547 tyuAnim.setDuration(125);
548 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
549 tydAnim.setDuration(100);
550 bounce.play(tyuAnim).before(tydAnim);
551 bounce.setInterpolator(new AccelerateInterpolator());
552 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700553 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700554 }
555
Winson Chungc6f10b92011-11-14 11:39:07 -0800556 public boolean onKey(View v, int keyCode, KeyEvent event) {
557 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
558 }
559
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 /*
561 * PagedViewWithDraggableItems implementation
562 */
563 @Override
564 protected void determineDraggingStart(android.view.MotionEvent ev) {
565 // Disable dragging by pulling an app down for now.
566 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700567
Winson Chung4b576be2011-04-27 17:40:20 -0700568 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700569 mLauncher.getWorkspace().onDragStartedWithItem(v);
570 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700571 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700572
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700573 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800574 final AppWidgetProviderInfo pInfo = info.info;
575 if (pInfo.configure != null) {
576 return;
577 }
578
579 mBindWidgetRunnable = new Runnable() {
580 @Override
581 public void run() {
582 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 if (AppWidgetManager.getInstance(mLauncher)
584 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
585 mWidgetCleanupState = WIDGET_BOUND;
586 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800587 }
588 };
589 post(mBindWidgetRunnable);
590
591 mInflateWidgetRunnable = new Runnable() {
592 @Override
593 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 AppWidgetHostView hostView = mLauncher.
595 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800596 info.boundWidget = hostView;
597 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800598 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700599 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
600 info.spanY, info, false);
601
602 // We want the first widget layout to be the correct size. This will be important
603 // for width size reporting to the AppWidgetManager.
604 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
605 unScaledSize[1]);
606 lp.x = lp.y = 0;
607 lp.customPosition = true;
608 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800609 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 }
611 };
612 post(mInflateWidgetRunnable);
613 }
614
615 @Override
616 public void onShortPress(View v) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700617 Log.d(TAG, "onShortPress, view: " + v);
Adam Cohened66b2b2012-01-23 17:28:51 -0800618 // We are anticipating a long press, and we use this time to load bind and instantiate
619 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700620 if (mCreateWidgetInfo != null) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700621 Log.d(TAG, "onShortPress --> cleanup previous, view: " + v + ", create info: " + mCreateWidgetInfo);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700622 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
623 cleanupWidgetPreloading(false);
624 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800625 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohen90340cf2012-05-16 21:37:32 -0700626 Log.d(TAG, "onShortPress --> create widget info: " + mCreateWidgetInfo);
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700627 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800628 }
629
Adam Cohen0e56cc92012-05-11 15:57:05 -0700630 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700631 Log.d(TAG, "cleanup widget preloading");
632
Adam Cohen0e56cc92012-05-11 15:57:05 -0700633 if (!widgetWasAdded) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700634 Log.d(TAG, "cleanup widget preloading --> widget wasn't added");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700635 // If the widget was not added, we may need to do further cleanup.
636 PendingAddWidgetInfo info = mCreateWidgetInfo;
637 mCreateWidgetInfo = null;
638 // First step was to allocate a widget id, revert that.
639 if ((mWidgetCleanupState == WIDGET_BOUND || mWidgetCleanupState == WIDGET_INFLATED) &&
640 mWidgetLoadingId != -1) {
641 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
642 }
643 if (mWidgetCleanupState == WIDGET_BOUND) {
644 // We never actually inflated the widget, so remove the callback to do so.
645 removeCallbacks(mInflateWidgetRunnable);
646 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
647 // The widget was inflated and added to the DragLayer -- remove it.
648 AppWidgetHostView widget = info.boundWidget;
649 mLauncher.getDragLayer().removeView(widget);
650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 }
652 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
653 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700654 mCreateWidgetInfo = null;
655 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800656 }
657
Adam Cohen7a326642012-02-22 12:03:22 -0800658 @Override
659 public void cleanUpShortPress(View v) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700660 Log.d(TAG, "cleanup shortpress, view: " + v);
Adam Cohen7a326642012-02-22 12:03:22 -0800661 if (!mDraggingWidget) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700662 Log.d(TAG, "cleanup shortpress --> cleanup preloading");
Adam Cohen0e56cc92012-05-11 15:57:05 -0700663 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800664 }
665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700667 private boolean beginDraggingWidget(View v) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700668 Log.d(TAG, "begin dragging widget, view: " + v);
669
Adam Cohen7a326642012-02-22 12:03:22 -0800670 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700671 // Get the widget preview as the drag representation
672 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700673 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700674
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700675 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
676 // we abort the drag.
677 if (image.getDrawable() == null) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700678 Log.d(TAG, "begin dragging widget, no drawable");
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700679 mDraggingWidget = false;
680 return false;
681 }
682
Adam Cohenc93ea442012-05-16 17:15:26 -0700683 // This can happen in some weird cases involving multi-touch. We can't start dragging the
684 // widget if this is null, so we break out.
685 if (mCreateWidgetInfo == null) {
Adam Cohen90340cf2012-05-16 21:37:32 -0700686 Log.d(TAG, "begin dragging widget, create widget info null");
Adam Cohenc93ea442012-05-16 17:15:26 -0700687 return false;
688 }
689
Winson Chung4b576be2011-04-27 17:40:20 -0700690 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800691 Bitmap preview;
692 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800693 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700694 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800695 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
696 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700697 int spanX = createItemInfo.spanX;
698 int spanY = createItemInfo.spanY;
699 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
700 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700701
Winson Chung72d59842012-02-22 13:51:36 -0800702 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
703 float minScale = 1.25f;
704 int minWidth, minHeight;
705 minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
706 minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800707 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohen1f362702012-04-04 14:58:12 -0700708 createWidgetInfo.icon, spanX, spanY, minWidth, minHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800709
710 // Determine the image view drawable scale relative to the preview
711 float[] mv = new float[9];
712 Matrix m = new Matrix();
713 m.setRectToRect(
714 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
715 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
716 (float) previewDrawable.getIntrinsicHeight()),
717 Matrix.ScaleToFit.START);
718 m.getValues(mv);
719 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700720 } else {
721 // Workaround for the fact that we don't keep the original ResolveInfo associated with
722 // the shortcut around. To get the icon, we just render the preview image (which has
723 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800724 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700725 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800726 Drawable d = image.getDrawable();
727 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700728 mCanvas.save();
Michael Jurka1202b332012-05-14 19:33:37 -0700729 mCanvas.translate((mWidgetPreviewIconPaddedDimension - d.getIntrinsicWidth()) / 2,
730 (mWidgetPreviewIconPaddedDimension - d.getIntrinsicHeight()) / 2);
Winson Chung1120e032011-11-22 16:11:31 -0800731 d.draw(mCanvas);
Michael Jurka4ca39222012-05-15 17:18:34 -0700732 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700733 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700734 createItemInfo.spanX = createItemInfo.spanY = 1;
735 }
Winson Chung4b576be2011-04-27 17:40:20 -0700736
Peter Ng8db70002011-10-25 15:40:08 -0700737 // We use a custom alpha clip table for the default widget previews
738 Paint alphaClipPaint = null;
739 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700740 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700741 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
742 alphaClipPaint = new Paint();
743 alphaClipPaint.setMaskFilter(alphaClipTable);
744 }
745 }
746
Winson Chung1120e032011-11-22 16:11:31 -0800747 // Save the preview for the outline generation, then dim the preview
748 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
749 false);
Winson Chung1120e032011-11-22 16:11:31 -0800750
Winson Chung4b576be2011-04-27 17:40:20 -0700751 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800752 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700753 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800754 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
755 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800756 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800757 outline.recycle();
758 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700759 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700760 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800761
Winson Chung4b576be2011-04-27 17:40:20 -0700762 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700764 if (!super.beginDragging(v)) return false;
765
766 if (v instanceof PagedViewIcon) {
767 beginDraggingApplication(v);
768 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700769 if (!beginDraggingWidget(v)) {
770 return false;
771 }
Winson Chung4b576be2011-04-27 17:40:20 -0700772 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800773
774 // We delay entering spring-loaded mode slightly to make sure the UI
775 // thready is free of any work.
776 postDelayed(new Runnable() {
777 @Override
778 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800779 // We don't enter spring-loaded mode if the drag has been cancelled
780 if (mLauncher.getDragController().isDragging()) {
781 // Dismiss the cling
782 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800783
Adam Cohen1b36dc32012-02-13 19:27:37 -0800784 // Reset the alpha on the dragged icon before we drag
785 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800786
Adam Cohen1b36dc32012-02-13 19:27:37 -0800787 // Go into spring loaded mode (must happen before we startDrag())
788 mLauncher.enterSpringLoadedDragMode();
789 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 }
Winson Chung72d59842012-02-22 13:51:36 -0800791 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800792
Winson Chung785d2eb2011-04-14 16:08:02 -0700793 return true;
794 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800795
Winson Chunga48487a2012-03-20 16:19:37 -0700796 /**
797 * Clean up after dragging.
798 *
799 * @param target where the item was dragged to (can be null if the item was flung)
800 */
801 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700802 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700803 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700804 // Exit spring loaded mode if we have not successfully dropped or have not handled the
805 // drop in Workspace
806 mLauncher.exitSpringLoadedDragMode();
807 }
Winson Chung4b919f82012-05-01 10:44:08 -0700808 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700809 }
810
Winson Chung785d2eb2011-04-14 16:08:02 -0700811 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700812 public View getContent() {
813 return null;
814 }
815
816 @Override
817 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700818 mInTransition = true;
819 if (toWorkspace) {
820 cancelAllTasks();
821 }
822 }
823
824 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700825 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700826 }
827
828 @Override
829 public void onLauncherTransitionStep(Launcher l, float t) {
830 }
831
832 @Override
833 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
834 mInTransition = false;
835 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
836 onSyncWidgetPageItems(d);
837 }
838 mDeferredSyncWidgetPageItems.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700839 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700840 }
841
842 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700843 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
844 boolean success) {
845 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
846 if (isFlingToDelete) return;
847
848 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700849
850 // Display an error message if the drag failed due to there not being enough space on the
851 // target layout we were dropping on.
852 if (!success) {
853 boolean showOutOfSpaceMessage = false;
854 if (target instanceof Workspace) {
855 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
856 Workspace workspace = (Workspace) target;
857 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700858 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700859 if (layout != null) {
860 layout.calculateSpans(itemInfo);
861 showOutOfSpaceMessage =
862 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
863 }
864 }
Winson Chungfc79c802011-05-02 13:35:34 -0700865 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700866 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700867 }
Adam Cohen7a326642012-02-22 12:03:22 -0800868
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800869 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700870 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700871 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800872 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700873 }
874
Winson Chunga48487a2012-03-20 16:19:37 -0700875 @Override
876 public void onFlingToDeleteCompleted() {
877 // We just dismiss the drag when we fling, so cleanup here
878 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700879 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700880 mDraggingWidget = false;
881 }
882
883 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800884 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700885 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800886 }
887
Winson Chung7f0acdd2011-09-19 18:34:19 -0700888 @Override
889 protected void onDetachedFromWindow() {
890 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700891 cancelAllTasks();
892 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700893
Michael Jurkae326f182011-11-21 14:05:46 -0800894 public void clearAllWidgetPages() {
895 cancelAllTasks();
896 int count = getChildCount();
897 for (int i = 0; i < count; i++) {
898 View v = getPageAt(i);
899 if (v instanceof PagedViewGridLayout) {
900 ((PagedViewGridLayout) v).removeAllViewsOnPage();
901 mDirtyPageContent.set(i, true);
902 }
903 }
904 }
905
Adam Cohen0cd3b642011-10-14 14:58:00 -0700906 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700907 // Clean up all the async tasks
908 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
909 while (iter.hasNext()) {
910 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
911 task.cancel(false);
912 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700913 mDirtyPageContent.set(task.page, true);
Winson Chung7f0acdd2011-09-19 18:34:19 -0700914 }
Winson Chung83687b12012-04-25 16:01:01 -0700915 mDeferredSyncWidgetPageItems.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700916 }
917
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700919 if (type == ContentType.Widgets) {
920 invalidatePageData(mNumAppsPages, true);
921 } else if (type == ContentType.Applications) {
922 invalidatePageData(0, true);
923 }
Winson Chungb44b5242011-06-13 11:32:14 -0700924 }
925
Adam Cohen0cd3b642011-10-14 14:58:00 -0700926 protected void snapToPage(int whichPage, int delta, int duration) {
927 super.snapToPage(whichPage, delta, duration);
928 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800929
930 // Update the thread priorities given the direction lookahead
931 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
932 while (iter.hasNext()) {
933 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700934 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800935 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
936 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
937 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
938 } else {
939 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
940 }
941 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700942 }
943
944 private void updateCurrentTab(int currentPage) {
945 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800946 if (tabHost != null) {
947 String tag = tabHost.getCurrentTabTag();
948 if (tag != null) {
949 if (currentPage >= mNumAppsPages &&
950 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
951 tabHost.setCurrentTabFromContent(ContentType.Widgets);
952 } else if (currentPage < mNumAppsPages &&
953 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
954 tabHost.setCurrentTabFromContent(ContentType.Applications);
955 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700956 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700957 }
958 }
959
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 /*
961 * Apps PagedView implementation
962 */
Winson Chung63257c12011-05-05 17:06:13 -0700963 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
964 int childCount = layout.getChildCount();
965 for (int i = 0; i < childCount; ++i) {
966 layout.getChildAt(i).setVisibility(visibility);
967 }
968 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 private void setupPage(PagedViewCellLayout layout) {
970 layout.setCellCount(mCellCountX, mCellCountY);
971 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
972 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
973 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
974
Winson Chung63257c12011-05-05 17:06:13 -0700975 // Note: We force a measure here to get around the fact that when we do layout calculations
976 // immediately after syncing, we don't have a proper width. That said, we already know the
977 // expected page width, so we can actually optimize by hiding all the TextView-based
978 // children that are expensive to measure, and let that happen naturally later.
979 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700980 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700981 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700982 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700984 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700986
Winson Chungf314b0e2011-08-16 11:54:27 -0700987 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700989 int numCells = mCellCountX * mCellCountY;
990 int startIndex = page * numCells;
991 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700992 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700993
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700995 ArrayList<Object> items = new ArrayList<Object>();
996 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 for (int i = startIndex; i < endIndex; ++i) {
998 ApplicationInfo info = mApps.get(i);
999 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1000 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001001 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 icon.setOnClickListener(this);
1003 icon.setOnLongClickListener(this);
1004 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001005 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001006
1007 int index = i - startIndex;
1008 int x = index % mCellCountX;
1009 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001010 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001011
1012 items.add(info);
1013 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001014 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001015
Winson Chungf0ea4d32011-06-06 14:27:16 -07001016 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -07001017 }
Winson Chungb44b5242011-06-13 11:32:14 -07001018
1019 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001020 * A helper to return the priority for loading of the specified widget page.
1021 */
1022 private int getWidgetPageLoadPriority(int page) {
1023 // If we are snapping to another page, use that index as the target page index
1024 int toPage = mCurrentPage;
1025 if (mNextPage > -1) {
1026 toPage = mNextPage;
1027 }
1028
1029 // We use the distance from the target page as an initial guess of priority, but if there
1030 // are no pages of higher priority than the page specified, then bump up the priority of
1031 // the specified page.
1032 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1033 int minPageDiff = Integer.MAX_VALUE;
1034 while (iter.hasNext()) {
1035 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001036 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001037 }
1038
1039 int rawPageDiff = Math.abs(page - toPage);
1040 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1041 }
1042 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001043 * Return the appropriate thread priority for loading for a given page (we give the current
1044 * page much higher priority)
1045 */
1046 private int getThreadPriorityForPage(int page) {
1047 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001048 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001049 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001050 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001051 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001052 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001053 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001054 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001055 }
1056 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001057 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001058 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001059 return Math.max(0, pageDiff * sPageSleepDelay);
1060 }
Winson Chungb44b5242011-06-13 11:32:14 -07001061 /**
1062 * Creates and executes a new AsyncTask to load a page of widget previews.
1063 */
1064 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001065 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001066
Winson Chungb44b5242011-06-13 11:32:14 -07001067 // Prune all tasks that are no longer needed
1068 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1069 while (iter.hasNext()) {
1070 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001071 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001072 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1073 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001074 task.cancel(false);
1075 iter.remove();
1076 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001077 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001078 }
1079 }
1080
Winson Chungf314b0e2011-08-16 11:54:27 -07001081 // We introduce a slight delay to order the loading of side pages so that we don't thrash
Michael Jurka39e5d172012-03-12 18:36:12 -07001082 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001083 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001084 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001085 @Override
1086 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001087 try {
Winson Chung09945932011-09-20 14:22:40 -07001088 try {
1089 Thread.sleep(sleepMs);
1090 } catch (Exception e) {}
1091 loadWidgetPreviewsInBackground(task, data);
1092 } finally {
1093 if (task.isCancelled()) {
1094 data.cleanup(true);
1095 }
1096 }
Winson Chungb44b5242011-06-13 11:32:14 -07001097 }
1098 },
1099 new AsyncTaskCallback() {
1100 @Override
1101 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001102 mRunningTasks.remove(task);
1103 if (task.isCancelled()) return;
1104 // do cleanup inside onSyncWidgetPageItems
1105 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001106 }
Winson Chung09945932011-09-20 14:22:40 -07001107 });
Winson Chungb44b5242011-06-13 11:32:14 -07001108
1109 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001110 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001111 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001112 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001113 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1114 mRunningTasks.add(t);
1115 }
Winson Chungb44b5242011-06-13 11:32:14 -07001116
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 /*
1118 * Widgets PagedView implementation
1119 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001120 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1122 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001123
1124 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001125 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001126 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1127 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001128 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001129 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001130 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001131
Winson Chung5fc72b32011-10-11 17:53:58 -07001132 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
1133 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -07001134 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001135
Winson Chung5fc72b32011-10-11 17:53:58 -07001136 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
1137 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -07001138 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001139 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001140 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001141 Rect oldBounds = d.copyBounds();
1142 d.setBounds(x, y, x + w, y + h);
1143 d.draw(c);
1144 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001145 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001146 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001147 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001148 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001149 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001150 int offset = 0;
1151 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001152 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001153
Winson Chung1ed747a2011-05-03 16:18:34 -07001154 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001155 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001156 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001157 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001158 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001159
Michael Jurka038f9d82011-11-03 13:50:45 -07001160 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1161 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001162 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001163 String packageName = provider.getPackageName();
1164 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1165 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001166
Michael Jurka038f9d82011-11-03 13:50:45 -07001167 Drawable drawable = null;
1168 if (previewImage != 0) {
1169 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1170 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001171 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001172 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001173 }
1174 }
1175
Michael Jurka038f9d82011-11-03 13:50:45 -07001176 int bitmapWidth;
1177 int bitmapHeight;
1178 boolean widgetPreviewExists = (drawable != null);
1179 if (widgetPreviewExists) {
1180 bitmapWidth = drawable.getIntrinsicWidth();
1181 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001182 } else {
Michael Jurkac7e52f52012-03-26 06:20:31 -07001183 if (cellHSpan < 1) cellHSpan = 1;
1184 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurka038f9d82011-11-03 13:50:45 -07001185 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001186 // TODO: This actually uses the apps customize cell layout params, where as we make want
1187 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001188 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1189 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001190 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001191 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001192 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001193 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001194 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001195 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001196 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001197 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001198 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001199 }
1200
1201 float scale = 1f;
1202 if (bitmapWidth > maxWidth) {
1203 scale = maxWidth / (float) bitmapWidth;
1204 }
1205 if (bitmapHeight * scale > maxHeight) {
1206 scale = maxHeight / (float) bitmapHeight;
1207 }
1208 if (scale != 1f) {
1209 bitmapWidth = (int) (scale * bitmapWidth);
1210 bitmapHeight = (int) (scale * bitmapHeight);
1211 }
1212
1213 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1214
1215 if (widgetPreviewExists) {
1216 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1217 } else {
1218 // Generate a preview image if we couldn't load one
1219 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1220 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1221 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001222 if (cellHSpan != 1 || cellVSpan != 1) {
1223 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1224 bitmapHeight);
1225 }
Winson Chung4b576be2011-04-27 17:40:20 -07001226
1227 // Draw the icon in the top left corner
1228 try {
1229 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001230 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1231 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001232 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001233 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001234 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1235
Peter Ng8db70002011-10-25 15:40:08 -07001236 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1237 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001238 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001239 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001240 }
Winson Chungb44b5242011-06-13 11:32:14 -07001241 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001242 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001243
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001245 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001246
Winson Chungd2945262011-06-24 15:22:14 -07001247 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001248 final ArrayList<Object> items = new ArrayList<Object>();
1249 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1250 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001251 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001252 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1253 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001254 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001255
Winson Chunge4a647f2011-09-30 14:41:25 -07001256 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001257 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001258 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1259 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001260 }
1261
Winson Chunge4a647f2011-09-30 14:41:25 -07001262 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001263 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001264 layout.setColumnCount(layout.getCellCountX());
1265 for (int i = 0; i < items.size(); ++i) {
1266 Object rawInfo = items.get(i);
1267 PendingAddItemInfo createItemInfo = null;
1268 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1269 R.layout.apps_customize_widget, layout, false);
1270 if (rawInfo instanceof AppWidgetProviderInfo) {
1271 // Fill in the widget information
1272 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1273 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001274
1275 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001276 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001277 createItemInfo.spanX = spanXY[0];
1278 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001279 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001280 createItemInfo.minSpanX = minSpanXY[0];
1281 createItemInfo.minSpanY = minSpanXY[1];
1282
1283 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001284 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001285 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001286 } else if (rawInfo instanceof ResolveInfo) {
1287 // Fill in the shortcuts information
1288 ResolveInfo info = (ResolveInfo) rawInfo;
1289 createItemInfo = new PendingAddItemInfo();
1290 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1291 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1292 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001293 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001294 widget.setTag(createItemInfo);
1295 }
1296 widget.setOnClickListener(this);
1297 widget.setOnLongClickListener(this);
1298 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001299 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001300
1301 // Layout each widget
1302 int ix = i % mWidgetCountX;
1303 int iy = i / mWidgetCountX;
1304 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1305 GridLayout.spec(iy, GridLayout.LEFT),
1306 GridLayout.spec(ix, GridLayout.TOP));
1307 lp.width = cellWidth;
1308 lp.height = cellHeight;
1309 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1310 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1311 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1312 layout.addView(widget, lp);
1313 }
1314
Michael Jurka038f9d82011-11-03 13:50:45 -07001315 // wait until a call on onLayout to start loading, because
1316 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1317 // TODO: can we do a measure/layout immediately?
1318 layout.setOnLayoutListener(new Runnable() {
1319 public void run() {
1320 // Load the widget previews
1321 int maxPreviewWidth = cellWidth;
1322 int maxPreviewHeight = cellHeight;
1323 if (layout.getChildCount() > 0) {
1324 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1325 int[] maxSize = w.getPreviewSize();
1326 maxPreviewWidth = maxSize[0];
1327 maxPreviewHeight = maxSize[1];
1328 }
1329 if (immediate) {
1330 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1331 maxPreviewWidth, maxPreviewHeight, null, null);
1332 loadWidgetPreviewsInBackground(null, data);
1333 onSyncWidgetPageItems(data);
1334 } else {
1335 prepareLoadWidgetPreviewsTask(page, items,
1336 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1337 }
1338 }
1339 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001340 }
1341 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1342 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001343 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1344 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001345 if (task != null) {
1346 // Ensure that this task starts running at the correct priority
1347 task.syncThreadPriority();
1348 }
1349
1350 // Load each of the widget/shortcut previews
1351 ArrayList<Object> items = data.items;
1352 ArrayList<Bitmap> images = data.generatedImages;
1353 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001354 for (int i = 0; i < count; ++i) {
1355 if (task != null) {
1356 // Ensure we haven't been cancelled yet
1357 if (task.isCancelled()) break;
1358 // Before work on each item, ensure that this task is running at the correct
1359 // priority
1360 task.syncThreadPriority();
1361 }
1362
1363 Object rawInfo = items.get(i);
1364 if (rawInfo instanceof AppWidgetProviderInfo) {
1365 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001366 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001367
1368 int maxWidth = Math.min(data.maxImageWidth,
1369 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1370 int maxHeight = Math.min(data.maxImageHeight,
1371 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001372 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001373 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001374 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001375 } else if (rawInfo instanceof ResolveInfo) {
1376 // Fill in the shortcuts information
1377 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001378 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001379 }
1380 }
Winson Chungb44b5242011-06-13 11:32:14 -07001381 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001382
Winson Chungb44b5242011-06-13 11:32:14 -07001383 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001384 if (mInTransition) {
1385 mDeferredSyncWidgetPageItems.add(data);
1386 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001387 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001388 try {
1389 int page = data.page;
1390 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001391
Michael Jurka39e5d172012-03-12 18:36:12 -07001392 ArrayList<Object> items = data.items;
1393 int count = items.size();
1394 for (int i = 0; i < count; ++i) {
1395 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1396 if (widget != null) {
1397 Bitmap preview = data.generatedImages.get(i);
1398 widget.applyPreview(new FastBitmapDrawable(preview), i);
1399 }
1400 }
Winson Chung68e4c642011-11-10 15:48:25 -08001401
Michael Jurka39e5d172012-03-12 18:36:12 -07001402 layout.createHardwareLayer();
1403 invalidate();
1404
1405 // Update all thread priorities
1406 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1407 while (iter.hasNext()) {
1408 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1409 int pageIndex = task.page;
1410 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1411 }
1412 } finally {
1413 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001414 }
Winson Chungb44b5242011-06-13 11:32:14 -07001415 }
Winson Chung46af2e82011-05-09 16:00:53 -07001416
Winson Chung785d2eb2011-04-14 16:08:02 -07001417 @Override
1418 public void syncPages() {
1419 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001420 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001421
Adam Cohen0cd3b642011-10-14 14:58:00 -07001422 Context context = getContext();
1423 for (int j = 0; j < mNumWidgetPages; ++j) {
1424 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1425 mWidgetCountY);
1426 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001427 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001428 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001429 }
1430
Adam Cohen0cd3b642011-10-14 14:58:00 -07001431 for (int i = 0; i < mNumAppsPages; ++i) {
1432 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1433 setupPage(layout);
1434 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001435 }
1436 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001437
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001439 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001440 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001441 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001442 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001443 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001444 }
1445 }
1446
Adam Cohen22f823d2011-09-01 17:22:18 -07001447 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1448 // it is in the z-order. This is important to insure touch events are handled correctly.
1449 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001450 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001451 }
1452
Adam Cohenae4f1552011-10-20 00:15:42 -07001453 @Override
1454 protected int indexToPage(int index) {
1455 return getChildCount() - index - 1;
1456 }
1457
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1459 @Override
1460 protected void screenScrolled(int screenCenter) {
1461 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001462
1463 for (int i = 0; i < getChildCount(); i++) {
1464 View v = getPageAt(i);
1465 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001466 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001467
1468 float interpolatedProgress =
1469 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1470 float scale = (1 - interpolatedProgress) +
1471 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1472 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001473
Adam Cohen2591f6a2011-10-25 14:36:40 -07001474 float alpha;
1475
Winson Chungd167e2a2012-04-26 13:13:01 -07001476 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001477 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001478 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001479 } else {
1480 // On large screens we need to fade the page as it nears its leftmost position
1481 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1482 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001483
1484 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1485 int pageWidth = v.getMeasuredWidth();
1486 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001487
1488 if (PERFORM_OVERSCROLL_ROTATION) {
1489 if (i == 0 && scrollProgress < 0) {
1490 // Overscroll to the left
1491 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1492 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1493 scale = 1.0f;
1494 alpha = 1.0f;
1495 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001496 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001497 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1498 // Overscroll to the right
1499 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1500 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1501 scale = 1.0f;
1502 alpha = 1.0f;
1503 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001504 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001505 } else {
1506 v.setPivotY(pageHeight / 2.0f);
1507 v.setPivotX(pageWidth / 2.0f);
1508 v.setRotationY(0f);
1509 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001510 }
1511
1512 v.setTranslationX(translationX);
1513 v.setScaleX(scale);
1514 v.setScaleY(scale);
1515 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001516
1517 // If the view has 0 alpha, we set it to be invisible so as to prevent
1518 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001519 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001520 v.setVisibility(INVISIBLE);
1521 } else if (v.getVisibility() != VISIBLE) {
1522 v.setVisibility(VISIBLE);
1523 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001524 }
1525 }
1526 }
1527
1528 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001529 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001530 }
1531
Winson Chung785d2eb2011-04-14 16:08:02 -07001532 /**
1533 * Used by the parent to get the content width to set the tab bar to
1534 * @return
1535 */
1536 public int getPageContentWidth() {
1537 return mContentWidth;
1538 }
1539
Winson Chungb26f3d62011-06-02 10:49:29 -07001540 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001541 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001542 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001543 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001544 // We reset the save index when we change pages so that it will be recalculated on next
1545 // rotation
1546 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001547 }
1548
Winson Chung785d2eb2011-04-14 16:08:02 -07001549 /*
1550 * AllAppsView implementation
1551 */
1552 @Override
1553 public void setup(Launcher launcher, DragController dragController) {
1554 mLauncher = launcher;
1555 mDragController = dragController;
1556 }
1557 @Override
1558 public void zoom(float zoom, boolean animate) {
1559 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1560 }
1561 @Override
1562 public boolean isVisible() {
1563 return (getVisibility() == VISIBLE);
1564 }
1565 @Override
1566 public boolean isAnimating() {
1567 return false;
1568 }
1569 @Override
1570 public void setApps(ArrayList<ApplicationInfo> list) {
1571 mApps = list;
1572 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001573 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001574
Winson Chung875de7e2011-06-28 14:25:17 -07001575 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1576 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001577 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001578 }
1579 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1580 // We add it in place, in alphabetical order
1581 int count = list.size();
1582 for (int i = 0; i < count; ++i) {
1583 ApplicationInfo info = list.get(i);
1584 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1585 if (index < 0) {
1586 mApps.add(-(index + 1), info);
1587 }
1588 }
1589 }
1590 @Override
1591 public void addApps(ArrayList<ApplicationInfo> list) {
1592 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001593 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001594 invalidatePageData();
1595 }
1596 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1597 ComponentName removeComponent = item.intent.getComponent();
1598 int length = list.size();
1599 for (int i = 0; i < length; ++i) {
1600 ApplicationInfo info = list.get(i);
1601 if (info.intent.getComponent().equals(removeComponent)) {
1602 return i;
1603 }
1604 }
1605 return -1;
1606 }
1607 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1608 // loop through all the apps and remove apps that have the same component
1609 int length = list.size();
1610 for (int i = 0; i < length; ++i) {
1611 ApplicationInfo info = list.get(i);
1612 int removeIndex = findAppByComponent(mApps, info);
1613 if (removeIndex > -1) {
1614 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 }
1616 }
1617 }
1618 @Override
1619 public void removeApps(ArrayList<ApplicationInfo> list) {
1620 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001621 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001622 invalidatePageData();
1623 }
1624 @Override
1625 public void updateApps(ArrayList<ApplicationInfo> list) {
1626 // We remove and re-add the updated applications list because it's properties may have
1627 // changed (ie. the title), and this will ensure that the items will be in their proper
1628 // place in the list.
1629 removeAppsWithoutInvalidate(list);
1630 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001631 updatePageCounts();
1632
Winson Chung785d2eb2011-04-14 16:08:02 -07001633 invalidatePageData();
1634 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001635
Winson Chung785d2eb2011-04-14 16:08:02 -07001636 @Override
1637 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001638 // If we have reset, then we should not continue to restore the previous state
1639 mSaveInstanceStateItemIndex = -1;
1640
Adam Cohenb64d36e2011-10-17 21:48:02 -07001641 AppsCustomizeTabHost tabHost = getTabHost();
1642 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001643 if (tag != null) {
1644 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1645 tabHost.setCurrentTabFromContent(ContentType.Applications);
1646 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001647 }
Winson Chung649668f2012-01-10 13:07:16 -08001648
Adam Cohenb64d36e2011-10-17 21:48:02 -07001649 if (mCurrentPage != 0) {
1650 invalidatePageData(0);
1651 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001652 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001653
1654 private AppsCustomizeTabHost getTabHost() {
1655 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1656 }
1657
Winson Chung785d2eb2011-04-14 16:08:02 -07001658 @Override
1659 public void dumpState() {
1660 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001661 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1662 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001663 }
Adam Cohen4e844012011-11-09 13:48:04 -08001664
Winson Chung785d2eb2011-04-14 16:08:02 -07001665 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001666 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001667 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001668 for (Object i: list) {
1669 if (i instanceof AppWidgetProviderInfo) {
1670 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1671 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1672 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1673 + " initialLayout=" + info.initialLayout
1674 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1675 } else if (i instanceof ResolveInfo) {
1676 ResolveInfo info = (ResolveInfo) i;
1677 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1678 + info.icon);
1679 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001680 }
1681 }
Adam Cohen4e844012011-11-09 13:48:04 -08001682
Winson Chung785d2eb2011-04-14 16:08:02 -07001683 @Override
1684 public void surrender() {
1685 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1686 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001687
1688 // Stop all background tasks
1689 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001690 }
Winson Chung007c6982011-06-14 13:27:53 -07001691
Winson Chunge4e50662012-01-23 14:45:13 -08001692 @Override
1693 public void iconPressed(PagedViewIcon icon) {
1694 // Reset the previously pressed icon and store a reference to the pressed icon so that
1695 // we can reset it on return to Launcher (in Launcher.onResume())
1696 if (mPressedIcon != null) {
1697 mPressedIcon.resetDrawableState();
1698 }
1699 mPressedIcon = icon;
1700 }
1701
1702 public void resetDrawableState() {
1703 if (mPressedIcon != null) {
1704 mPressedIcon.resetDrawableState();
1705 mPressedIcon = null;
1706 }
1707 }
Winson Chung68e4c642011-11-10 15:48:25 -08001708
Winson Chungb44b5242011-06-13 11:32:14 -07001709 /*
1710 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1711 * widget previews in the background with the AsyncTasks.
1712 */
Winson Chung68e4c642011-11-10 15:48:25 -08001713 final static int sLookBehindPageCount = 2;
1714 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001715 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001716 final int count = getChildCount();
1717 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1718 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1719 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001720 }
1721 protected int getAssociatedUpperPageBound(int page) {
1722 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001723 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1724 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1725 count - 1);
1726 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001727 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001728
1729 @Override
1730 protected String getCurrentPageDescription() {
1731 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1732 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001733 int count = 0;
1734
Adam Cohen0cd3b642011-10-14 14:58:00 -07001735 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001736 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001737 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001738 } else {
1739 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001740 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001741 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001742 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001743
Michael Jurka8b805b12012-04-18 14:23:14 -07001744 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001745 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001746}