blob: 67ae1f11232a2f7292bc62d5497ccd9b7ca5c1fd [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;
Adam Cohen4e844012011-11-09 13:48:04 -080052import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070053import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070054import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070055import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070056import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070057import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070058import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070059
60import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070061import com.android.launcher2.DropTarget.DragObject;
62
63import java.util.ArrayList;
64import java.util.Collections;
65import java.util.Iterator;
66import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070067
Winson Chungb44b5242011-06-13 11:32:14 -070068/**
69 * A simple callback interface which also provides the results of the task.
70 */
71interface AsyncTaskCallback {
72 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
73}
Winson Chung4e076542011-06-23 13:04:10 -070074
Winson Chungb44b5242011-06-13 11:32:14 -070075/**
76 * The data needed to perform either of the custom AsyncTasks.
77 */
78class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070079 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080080 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070081 }
82
Winson Chungb44b5242011-06-13 11:32:14 -070083 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
84 AsyncTaskCallback postR) {
85 page = p;
86 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070087 sourceImages = si;
88 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070089 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070090 doInBackgroundCallback = bgR;
91 postExecuteCallback = postR;
92 }
Michael Jurka038f9d82011-11-03 13:50:45 -070093 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070094 AsyncTaskCallback postR) {
95 page = p;
96 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070097 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070098 maxImageWidth = cw;
99 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700100 doInBackgroundCallback = bgR;
101 postExecuteCallback = postR;
102 }
Winson Chung09945932011-09-20 14:22:40 -0700103 void cleanup(boolean cancelled) {
104 // Clean up any references to source/generated bitmaps
105 if (sourceImages != null) {
106 if (cancelled) {
107 for (Bitmap b : sourceImages) {
108 b.recycle();
109 }
110 }
111 sourceImages.clear();
112 }
113 if (generatedImages != null) {
114 if (cancelled) {
115 for (Bitmap b : generatedImages) {
116 b.recycle();
117 }
118 }
119 generatedImages.clear();
120 }
121 }
Winson Chungb44b5242011-06-13 11:32:14 -0700122 int page;
123 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700124 ArrayList<Bitmap> sourceImages;
125 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700126 int maxImageWidth;
127 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700128 AsyncTaskCallback doInBackgroundCallback;
129 AsyncTaskCallback postExecuteCallback;
130}
Winson Chung4e076542011-06-23 13:04:10 -0700131
Winson Chungb44b5242011-06-13 11:32:14 -0700132/**
133 * A generic template for an async task used in AppsCustomize.
134 */
135class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700136 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700137 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700139 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 }
141 @Override
142 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
143 if (params.length != 1) return null;
144 // Load each of the widget previews in the background
145 params[0].doInBackgroundCallback.run(this, params[0]);
146 return params[0];
147 }
148 @Override
149 protected void onPostExecute(AsyncTaskPageData result) {
150 // All the widget previews are loaded, so we can just callback to inflate the page
151 result.postExecuteCallback.run(this, result);
152 }
153
154 void setThreadPriority(int p) {
155 threadPriority = p;
156 }
157 void syncThreadPriority() {
158 Process.setThreadPriority(threadPriority);
159 }
160
161 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700162 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700164 int threadPriority;
165}
Winson Chungb44b5242011-06-13 11:32:14 -0700166
167/**
168 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
169 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800171 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700172 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
173 LauncherTransitionable {
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 static final String LOG_TAG = "AppsCustomizePagedView";
175
176 /**
177 * The different content types that this paged view can show.
178 */
179 public enum ContentType {
180 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700181 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700182 }
183
184 // Refs
185 private Launcher mLauncher;
186 private DragController mDragController;
187 private final LayoutInflater mLayoutInflater;
188 private final PackageManager mPackageManager;
189
Winson Chung5afbf7b2011-07-25 11:53:08 -0700190 // Save and Restore
191 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800192 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700193
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700195 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700196 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700197
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800199 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700200 private int mClingFocusedX;
201 private int mClingFocusedY;
202
Winson Chung1ed747a2011-05-03 16:18:34 -0700203 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700204 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700205 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700206 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700207
208 // Dimens
209 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700210 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800211 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700212 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700213 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700214 private final int mWidgetPreviewIconPaddedDimension;
215 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700216 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700217 private int mNumAppsPages;
218 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700219
Adam Cohen22f823d2011-09-01 17:22:18 -0700220 // Relating to the scroll and overscroll effects
221 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700222 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700223 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700224 private static float TRANSITION_PIVOT = 0.65f;
225 private static float TRANSITION_MAX_ROTATION = 22;
226 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700227 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700228 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700229
Winson Chungb44b5242011-06-13 11:32:14 -0700230 // Previews & outlines
231 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800232 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700233
Adam Cohened66b2b2012-01-23 17:28:51 -0800234 private Runnable mInflateWidgetRunnable = null;
235 private Runnable mBindWidgetRunnable = null;
236 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
237 static final int WIDGET_BOUND = 0;
238 static final int WIDGET_INFLATED = 1;
239 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
240 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800241 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800242 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800243
Michael Jurka39e5d172012-03-12 18:36:12 -0700244 // Deferral of loading widget previews during launcher transitions
245 private boolean mInTransition;
246 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
247 new ArrayList<AsyncTaskPageData>();
248
Winson Chung785d2eb2011-04-14 16:08:02 -0700249 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
250 super(context, attrs);
251 mLayoutInflater = LayoutInflater.from(context);
252 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700253 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700254 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700255 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700256 mCanvas = new Canvas();
257 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700258
259 // Save the default widget preview background
260 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700261 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700262 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700263
Winson Chung6032e7e2011-11-08 15:47:17 -0800264 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
265 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
266 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700267 mWidgetWidthGap =
268 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
269 mWidgetHeightGap =
270 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700271 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
272 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700273 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
274 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700275 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700277
Winson Chung1ed747a2011-05-03 16:18:34 -0700278 // The padding on the non-matched dimension for the default widget preview icons
279 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700280 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700281 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700282 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700283 }
284
285 @Override
286 protected void init() {
287 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700288 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700289
290 Context context = getContext();
291 Resources r = context.getResources();
292 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
293 }
294
Winson Chungf0ea4d32011-06-06 14:27:16 -0700295 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700296 protected void onUnhandledTap(MotionEvent ev) {
297 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700298 // Dismiss AppsCustomize if we tap
299 mLauncher.showWorkspace(true);
300 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700301 }
302
Winson Chung5afbf7b2011-07-25 11:53:08 -0700303 /** Returns the item index of the center item on this page so that we can restore to this
304 * item index when we rotate. */
305 private int getMiddleComponentIndexOnCurrentPage() {
306 int i = -1;
307 if (getPageCount() > 0) {
308 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700310 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
312 int numItemsPerPage = mCellCountX * mCellCountY;
313 int childCount = childrenLayout.getChildCount();
314 if (childCount > 0) {
315 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 }
317 } else {
318 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700319 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700320 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
321 int childCount = layout.getChildCount();
322 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700323 i = numApps +
324 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
325 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700326 }
327 }
328 return i;
329 }
330
331 /** Get the index of the item to restore to if we need to restore the current page. */
332 int getSaveInstanceStateIndex() {
333 if (mSaveInstanceStateItemIndex == -1) {
334 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
335 }
336 return mSaveInstanceStateItemIndex;
337 }
338
339 /** Returns the page in the current orientation which is expected to contain the specified
340 * item index. */
341 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 if (index < 0) return 0;
343
344 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 int numItemsPerPage = mCellCountX * mCellCountY;
346 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700347 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700348 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700349 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
350 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700351 }
352
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 /**
354 * This differs from isDataReady as this is the test done if isDataReady is not set.
355 */
356 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700357 // We only do this test once, and we default to the Applications page, so we only really
358 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700359 // TODO: What if one of them is validly empty
360 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700361 }
362
Winson Chung5afbf7b2011-07-25 11:53:08 -0700363 /** Restores the page for an item at the specified index */
364 void restorePageForIndex(int index) {
365 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700366 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700367 }
368
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700369 private void updatePageCounts() {
370 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
371 (float) (mWidgetCountX * mWidgetCountY));
372 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
373 }
374
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 protected void onDataReady(int width, int height) {
376 // Note that we transpose the counts in portrait so that we get a similar layout
377 boolean isLandscape = getResources().getConfiguration().orientation ==
378 Configuration.ORIENTATION_LANDSCAPE;
379 int maxCellCountX = Integer.MAX_VALUE;
380 int maxCellCountY = Integer.MAX_VALUE;
381 if (LauncherApplication.isScreenLarge()) {
382 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
383 LauncherModel.getCellCountY());
384 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
385 LauncherModel.getCellCountX());
386 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800387 if (mMaxAppCellCountX > -1) {
388 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
389 }
390 if (mMaxAppCellCountY > -1) {
391 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
392 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393
394 // Now that the data is ready, we can calculate the content width, the number of cells to
395 // use for each page
396 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
397 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
398 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
399 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
400 mCellCountX = mWidgetSpacingLayout.getCellCountX();
401 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700402 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700403
Winson Chungdb1138b2011-06-30 14:39:35 -0700404 // Force a measure to update recalculate the gaps
405 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
406 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
407 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700408 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700409
Michael Jurkae326f182011-11-21 14:05:46 -0800410 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
411 final boolean hostIsTransitioning = host.isTransitioning();
412
Adam Cohen0cd3b642011-10-14 14:58:00 -0700413 // Restore the page
414 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800415 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700416
Winson Chung3f4e1422011-11-17 14:58:51 -0800417 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
418 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
419 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800420 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800421 post(new Runnable() {
422 @Override
423 public void run() {
424 showAllAppsCling();
425 }
426 });
427 }
428 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700429
Winson Chung3f4e1422011-11-17 14:58:51 -0800430 void showAllAppsCling() {
431 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
432 mHasShownAllAppsCling = true;
433 // Calculate the position for the cling punch through
434 int[] offset = new int[2];
435 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
436 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
437 // PagedViews are centered horizontally but top aligned
438 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
439 offset[0];
440 pos[1] += offset[1];
441 mLauncher.showFirstRunAllAppsCling(pos);
442 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
444
445 @Override
446 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
447 int width = MeasureSpec.getSize(widthMeasureSpec);
448 int height = MeasureSpec.getSize(heightMeasureSpec);
449 if (!isDataReady()) {
450 if (testDataReady()) {
451 setDataIsReady();
452 setMeasuredDimension(width, height);
453 onDataReady(width, height);
454 }
455 }
456
457 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
458 }
459
Winson Chung785d2eb2011-04-14 16:08:02 -0700460 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700461 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
462 // by a broadcast receiver, and in order for it to work correctly, we need to know that
463 // the AppWidgetService has already received and processed the same broadcast. Since there
464 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
465 // we should have a more precise way of ensuring the AppWidgetService is up to date.
466 postDelayed(new Runnable() {
467 public void run() {
468 updatePackages();
469 }
470 }, 500);
471 }
472
473 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700474 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700475 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700476 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700477 List<AppWidgetProviderInfo> widgets =
478 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700479 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
480 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800481 for (AppWidgetProviderInfo widget : widgets) {
482 if (widget.minWidth > 0 && widget.minHeight > 0) {
483 mWidgets.add(widget);
484 } else {
485 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
486 widget.minWidth + ", " + widget.minHeight + ")");
487 }
488 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700489 mWidgets.addAll(shortcuts);
490 Collections.sort(mWidgets,
491 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700492 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700493
Winson Chung875de7e2011-06-28 14:25:17 -0700494 if (wasEmpty) {
495 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
496 // a layout to do this test and invalidate the page data when ready.
497 if (testDataReady()) requestLayout();
498 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700499 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700500 invalidatePageData();
501 }
Winson Chung4b576be2011-04-27 17:40:20 -0700502 }
503
504 @Override
505 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700506 // When we have exited all apps or are in transition, disregard clicks
507 if (!mLauncher.isAllAppsCustomizeOpen() ||
508 mLauncher.getWorkspace().isSwitchingState()) return;
509
Winson Chung4b576be2011-04-27 17:40:20 -0700510 if (v instanceof PagedViewIcon) {
511 // Animate some feedback to the click
512 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Michael Jurkae3517d02012-01-12 07:46:24 -0800513 mLauncher.startActivitySafely(appInfo.intent, appInfo);
Winson Chung3b187b82012-01-30 15:11:08 -0800514
515 // Lock the drawable state to pressed until we return to Launcher
516 if (mPressedIcon != null) {
517 mPressedIcon.lockDrawableState();
518 }
Winson Chung4b576be2011-04-27 17:40:20 -0700519 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700520 // Let the user know that they have to long press to add a widget
521 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
522 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700523
Winson Chungd2e87b32011-06-02 10:53:07 -0700524 // Create a little animation to show that the widget can move
525 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
526 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
527 AnimatorSet bounce = new AnimatorSet();
528 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
529 tyuAnim.setDuration(125);
530 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
531 tydAnim.setDuration(100);
532 bounce.play(tyuAnim).before(tydAnim);
533 bounce.setInterpolator(new AccelerateInterpolator());
534 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700535 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700536 }
537
Winson Chungc6f10b92011-11-14 11:39:07 -0800538 public boolean onKey(View v, int keyCode, KeyEvent event) {
539 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
540 }
541
Winson Chung785d2eb2011-04-14 16:08:02 -0700542 /*
543 * PagedViewWithDraggableItems implementation
544 */
545 @Override
546 protected void determineDraggingStart(android.view.MotionEvent ev) {
547 // Disable dragging by pulling an app down for now.
548 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700549
Winson Chung4b576be2011-04-27 17:40:20 -0700550 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700551 mLauncher.getWorkspace().onDragStartedWithItem(v);
552 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700553 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700554
Adam Cohened66b2b2012-01-23 17:28:51 -0800555 private void loadWidgetInBackground(final PendingAddWidgetInfo info) {
556 final AppWidgetProviderInfo pInfo = info.info;
557 if (pInfo.configure != null) {
558 return;
559 }
560
561 mBindWidgetRunnable = new Runnable() {
562 @Override
563 public void run() {
564 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
565 AppWidgetManager.getInstance(mLauncher).bindAppWidgetId(mWidgetLoadingId,
566 info.componentName);
567 mWidgetCleanupState = WIDGET_BOUND;
568 }
569 };
570 post(mBindWidgetRunnable);
571
572 mInflateWidgetRunnable = new Runnable() {
573 @Override
574 public void run() {
575 AppWidgetHostView hostView =
576 mLauncher.getAppWidgetHost().createView(mContext, mWidgetLoadingId, pInfo);
577 info.boundWidget = hostView;
578 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800579 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700580 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
581 info.spanY, info, false);
582
583 // We want the first widget layout to be the correct size. This will be important
584 // for width size reporting to the AppWidgetManager.
585 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
586 unScaledSize[1]);
587 lp.x = lp.y = 0;
588 lp.customPosition = true;
589 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800590 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 }
592 };
593 post(mInflateWidgetRunnable);
594 }
595
596 @Override
597 public void onShortPress(View v) {
598 // We are anticipating a long press, and we use this time to load bind and instantiate
599 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen1b36dc32012-02-13 19:27:37 -0800600 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
601 loadWidgetInBackground(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 }
603
Adam Cohen7a326642012-02-22 12:03:22 -0800604 private void cleanupWidgetPreloading() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800605 PendingAddWidgetInfo info = mCreateWidgetInfo;
606 mCreateWidgetInfo = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
608 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
609 }
610 if (mWidgetCleanupState == WIDGET_BOUND) {
611 removeCallbacks(mInflateWidgetRunnable);
612 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
613 AppWidgetHostView widget = info.boundWidget;
614 int widgetId = widget.getAppWidgetId();
615 mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800616 mLauncher.getDragLayer().removeView(widget);
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 }
618 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
619 mWidgetLoadingId = -1;
620 }
621
Adam Cohen7a326642012-02-22 12:03:22 -0800622 @Override
623 public void cleanUpShortPress(View v) {
624 if (!mDraggingWidget) {
625 cleanupWidgetPreloading();
626 }
627 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800628
Adam Cohen7a326642012-02-22 12:03:22 -0800629 private void beginDraggingWidget(View v) {
630 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700631 // Get the widget preview as the drag representation
632 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700633 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700634
635 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800636 Bitmap preview;
637 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800638 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700639 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800640 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
641 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700642 int spanX = createItemInfo.spanX;
643 int spanY = createItemInfo.spanY;
644 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
645 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700646
Winson Chung72d59842012-02-22 13:51:36 -0800647 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
648 float minScale = 1.25f;
649 int minWidth, minHeight;
650 minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
651 minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800652 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohen1f362702012-04-04 14:58:12 -0700653 createWidgetInfo.icon, spanX, spanY, minWidth, minHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800654
655 // Determine the image view drawable scale relative to the preview
656 float[] mv = new float[9];
657 Matrix m = new Matrix();
658 m.setRectToRect(
659 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
660 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
661 (float) previewDrawable.getIntrinsicHeight()),
662 Matrix.ScaleToFit.START);
663 m.getValues(mv);
664 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700665 } else {
666 // Workaround for the fact that we don't keep the original ResolveInfo associated with
667 // the shortcut around. To get the icon, we just render the preview image (which has
668 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800669 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700670 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800671 Drawable d = image.getDrawable();
672 mCanvas.setBitmap(preview);
673 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700674 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700675 createItemInfo.spanX = createItemInfo.spanY = 1;
676 }
Winson Chung4b576be2011-04-27 17:40:20 -0700677
Peter Ng8db70002011-10-25 15:40:08 -0700678 // We use a custom alpha clip table for the default widget previews
679 Paint alphaClipPaint = null;
680 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700681 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700682 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
683 alphaClipPaint = new Paint();
684 alphaClipPaint.setMaskFilter(alphaClipTable);
685 }
686 }
687
Winson Chung1120e032011-11-22 16:11:31 -0800688 // Save the preview for the outline generation, then dim the preview
689 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
690 false);
Winson Chung1120e032011-11-22 16:11:31 -0800691
Winson Chung4b576be2011-04-27 17:40:20 -0700692 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800693 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700694 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800695 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
696 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800697 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800698 outline.recycle();
699 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700700 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800701
Winson Chung4b576be2011-04-27 17:40:20 -0700702 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800703 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700704 if (!super.beginDragging(v)) return false;
705
706 if (v instanceof PagedViewIcon) {
707 beginDraggingApplication(v);
708 } else if (v instanceof PagedViewWidget) {
709 beginDraggingWidget(v);
710 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800711
712 // We delay entering spring-loaded mode slightly to make sure the UI
713 // thready is free of any work.
714 postDelayed(new Runnable() {
715 @Override
716 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800717 // We don't enter spring-loaded mode if the drag has been cancelled
718 if (mLauncher.getDragController().isDragging()) {
719 // Dismiss the cling
720 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800721
Adam Cohen1b36dc32012-02-13 19:27:37 -0800722 // Reset the alpha on the dragged icon before we drag
723 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800724
Adam Cohen1b36dc32012-02-13 19:27:37 -0800725 // Go into spring loaded mode (must happen before we startDrag())
726 mLauncher.enterSpringLoadedDragMode();
727 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 }
Winson Chung72d59842012-02-22 13:51:36 -0800729 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800730
Winson Chung785d2eb2011-04-14 16:08:02 -0700731 return true;
732 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800733
Winson Chunga48487a2012-03-20 16:19:37 -0700734 /**
735 * Clean up after dragging.
736 *
737 * @param target where the item was dragged to (can be null if the item was flung)
738 */
739 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700740 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700741 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700742 // Exit spring loaded mode if we have not successfully dropped or have not handled the
743 // drop in Workspace
744 mLauncher.exitSpringLoadedDragMode();
745 }
Adam Cohen446e9402011-09-15 18:21:21 -0700746 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chung785d2eb2011-04-14 16:08:02 -0700747 }
748
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 @Override
Michael Jurka39e5d172012-03-12 18:36:12 -0700750 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
751 mInTransition = true;
752 if (toWorkspace) {
753 cancelAllTasks();
754 }
755 }
756
757 @Override
758 public View getContent() {
759 return null;
760 }
761
762 @Override
763 public void onLauncherTransitionStep(Launcher l, float t) {
764 }
765
766 @Override
767 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
768 mInTransition = false;
769 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
770 onSyncWidgetPageItems(d);
771 }
772 mDeferredSyncWidgetPageItems.clear();
773 }
774
775 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700776 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
777 boolean success) {
778 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
779 if (isFlingToDelete) return;
780
781 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700782
783 // Display an error message if the drag failed due to there not being enough space on the
784 // target layout we were dropping on.
785 if (!success) {
786 boolean showOutOfSpaceMessage = false;
787 if (target instanceof Workspace) {
788 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
789 Workspace workspace = (Workspace) target;
790 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700791 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700792 if (layout != null) {
793 layout.calculateSpans(itemInfo);
794 showOutOfSpaceMessage =
795 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
796 }
797 }
Winson Chungfc79c802011-05-02 13:35:34 -0700798 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700799 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700800 }
Adam Cohen7a326642012-02-22 12:03:22 -0800801
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800802 d.deferDragViewCleanupPostAnimation = false;
Adam Cohen7a326642012-02-22 12:03:22 -0800803 cleanupWidgetPreloading();
Winson Chungfc79c802011-05-02 13:35:34 -0700804 }
Adam Cohen7a326642012-02-22 12:03:22 -0800805 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700806 }
807
Winson Chunga48487a2012-03-20 16:19:37 -0700808 @Override
809 public void onFlingToDeleteCompleted() {
810 // We just dismiss the drag when we fling, so cleanup here
811 endDragging(null, true, true);
812 cleanupWidgetPreloading();
813 mDraggingWidget = false;
814 }
815
816 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800817 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700818 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800819 }
820
Winson Chung7f0acdd2011-09-19 18:34:19 -0700821 @Override
822 protected void onDetachedFromWindow() {
823 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700824 cancelAllTasks();
825 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700826
Michael Jurkae326f182011-11-21 14:05:46 -0800827 public void clearAllWidgetPages() {
828 cancelAllTasks();
829 int count = getChildCount();
830 for (int i = 0; i < count; i++) {
831 View v = getPageAt(i);
832 if (v instanceof PagedViewGridLayout) {
833 ((PagedViewGridLayout) v).removeAllViewsOnPage();
834 mDirtyPageContent.set(i, true);
835 }
836 }
837 }
838
Adam Cohen0cd3b642011-10-14 14:58:00 -0700839 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700840 // Clean up all the async tasks
841 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
842 while (iter.hasNext()) {
843 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
844 task.cancel(false);
845 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700846 mDirtyPageContent.set(task.page, true);
Winson Chung7f0acdd2011-09-19 18:34:19 -0700847 }
848 }
849
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700851 if (type == ContentType.Widgets) {
852 invalidatePageData(mNumAppsPages, true);
853 } else if (type == ContentType.Applications) {
854 invalidatePageData(0, true);
855 }
Winson Chungb44b5242011-06-13 11:32:14 -0700856 }
857
Adam Cohen0cd3b642011-10-14 14:58:00 -0700858 protected void snapToPage(int whichPage, int delta, int duration) {
859 super.snapToPage(whichPage, delta, duration);
860 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800861
862 // Update the thread priorities given the direction lookahead
863 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
864 while (iter.hasNext()) {
865 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700866 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800867 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
868 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
869 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
870 } else {
871 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
872 }
873 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700874 }
875
876 private void updateCurrentTab(int currentPage) {
877 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800878 if (tabHost != null) {
879 String tag = tabHost.getCurrentTabTag();
880 if (tag != null) {
881 if (currentPage >= mNumAppsPages &&
882 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
883 tabHost.setCurrentTabFromContent(ContentType.Widgets);
884 } else if (currentPage < mNumAppsPages &&
885 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
886 tabHost.setCurrentTabFromContent(ContentType.Applications);
887 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700888 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700889 }
890 }
891
Winson Chung785d2eb2011-04-14 16:08:02 -0700892 /*
893 * Apps PagedView implementation
894 */
Winson Chung63257c12011-05-05 17:06:13 -0700895 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
896 int childCount = layout.getChildCount();
897 for (int i = 0; i < childCount; ++i) {
898 layout.getChildAt(i).setVisibility(visibility);
899 }
900 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700901 private void setupPage(PagedViewCellLayout layout) {
902 layout.setCellCount(mCellCountX, mCellCountY);
903 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
904 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
905 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
906
Winson Chung63257c12011-05-05 17:06:13 -0700907 // Note: We force a measure here to get around the fact that when we do layout calculations
908 // immediately after syncing, we don't have a proper width. That said, we already know the
909 // expected page width, so we can actually optimize by hiding all the TextView-based
910 // children that are expensive to measure, and let that happen naturally later.
911 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700912 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700913 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700914 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700915 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700916 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700917 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700918
Winson Chungf314b0e2011-08-16 11:54:27 -0700919 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700920 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700921 int numCells = mCellCountX * mCellCountY;
922 int startIndex = page * numCells;
923 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700924 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700925
Winson Chung785d2eb2011-04-14 16:08:02 -0700926 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700927 ArrayList<Object> items = new ArrayList<Object>();
928 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700929 for (int i = startIndex; i < endIndex; ++i) {
930 ApplicationInfo info = mApps.get(i);
931 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
932 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800933 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700934 icon.setOnClickListener(this);
935 icon.setOnLongClickListener(this);
936 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800937 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700938
939 int index = i - startIndex;
940 int x = index % mCellCountX;
941 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700942 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700943
944 items.add(info);
945 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700946 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700947
Winson Chungf0ea4d32011-06-06 14:27:16 -0700948 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700949 }
Winson Chungb44b5242011-06-13 11:32:14 -0700950
951 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800952 * A helper to return the priority for loading of the specified widget page.
953 */
954 private int getWidgetPageLoadPriority(int page) {
955 // If we are snapping to another page, use that index as the target page index
956 int toPage = mCurrentPage;
957 if (mNextPage > -1) {
958 toPage = mNextPage;
959 }
960
961 // We use the distance from the target page as an initial guess of priority, but if there
962 // are no pages of higher priority than the page specified, then bump up the priority of
963 // the specified page.
964 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
965 int minPageDiff = Integer.MAX_VALUE;
966 while (iter.hasNext()) {
967 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700968 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800969 }
970
971 int rawPageDiff = Math.abs(page - toPage);
972 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
973 }
974 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700975 * Return the appropriate thread priority for loading for a given page (we give the current
976 * page much higher priority)
977 */
978 private int getThreadPriorityForPage(int page) {
979 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800980 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700981 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800982 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700983 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800984 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700985 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800986 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700987 }
988 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700989 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800990 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700991 return Math.max(0, pageDiff * sPageSleepDelay);
992 }
Winson Chungb44b5242011-06-13 11:32:14 -0700993 /**
994 * Creates and executes a new AsyncTask to load a page of widget previews.
995 */
996 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700997 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800998
Winson Chungb44b5242011-06-13 11:32:14 -0700999 // Prune all tasks that are no longer needed
1000 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1001 while (iter.hasNext()) {
1002 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001003 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001004 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1005 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001006 task.cancel(false);
1007 iter.remove();
1008 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001009 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001010 }
1011 }
1012
Winson Chungf314b0e2011-08-16 11:54:27 -07001013 // 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 -07001014 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001015 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001016 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001017 @Override
1018 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001019 try {
Winson Chung09945932011-09-20 14:22:40 -07001020 try {
1021 Thread.sleep(sleepMs);
1022 } catch (Exception e) {}
1023 loadWidgetPreviewsInBackground(task, data);
1024 } finally {
1025 if (task.isCancelled()) {
1026 data.cleanup(true);
1027 }
1028 }
Winson Chungb44b5242011-06-13 11:32:14 -07001029 }
1030 },
1031 new AsyncTaskCallback() {
1032 @Override
1033 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001034 mRunningTasks.remove(task);
1035 if (task.isCancelled()) return;
1036 // do cleanup inside onSyncWidgetPageItems
1037 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001038 }
Winson Chung09945932011-09-20 14:22:40 -07001039 });
Winson Chungb44b5242011-06-13 11:32:14 -07001040
1041 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001042 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001043 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001044 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001045 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1046 mRunningTasks.add(t);
1047 }
Winson Chungb44b5242011-06-13 11:32:14 -07001048
Winson Chung785d2eb2011-04-14 16:08:02 -07001049 /*
1050 * Widgets PagedView implementation
1051 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001052 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001053 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1054 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001055
1056 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001057 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001058 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1059 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001061 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001062 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001063
Winson Chung5fc72b32011-10-11 17:53:58 -07001064 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
1065 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -07001066 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001067
Winson Chung5fc72b32011-10-11 17:53:58 -07001068 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
1069 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -07001070 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001071 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001072 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001073 Rect oldBounds = d.copyBounds();
1074 d.setBounds(x, y, x + w, y + h);
1075 d.draw(c);
1076 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001077 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001078 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001080 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001081 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001082 int offset = 0;
1083 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001084 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001085
Winson Chung1ed747a2011-05-03 16:18:34 -07001086 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001087 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001088 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001089 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001090 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001091
Michael Jurka038f9d82011-11-03 13:50:45 -07001092 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1093 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001094 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001095 String packageName = provider.getPackageName();
1096 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1097 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001098
Michael Jurka038f9d82011-11-03 13:50:45 -07001099 Drawable drawable = null;
1100 if (previewImage != 0) {
1101 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1102 if (drawable == null) {
1103 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1104 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001105 }
1106 }
1107
Michael Jurka038f9d82011-11-03 13:50:45 -07001108 int bitmapWidth;
1109 int bitmapHeight;
1110 boolean widgetPreviewExists = (drawable != null);
1111 if (widgetPreviewExists) {
1112 bitmapWidth = drawable.getIntrinsicWidth();
1113 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001114 } else {
Michael Jurkac7e52f52012-03-26 06:20:31 -07001115 if (cellHSpan < 1) cellHSpan = 1;
1116 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurka038f9d82011-11-03 13:50:45 -07001117 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001118 // TODO: This actually uses the apps customize cell layout params, where as we make want
1119 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001120 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1121 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001122 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001123 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001124 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001125 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001126 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001127 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001128 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001129 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001130 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001131 }
1132
1133 float scale = 1f;
1134 if (bitmapWidth > maxWidth) {
1135 scale = maxWidth / (float) bitmapWidth;
1136 }
1137 if (bitmapHeight * scale > maxHeight) {
1138 scale = maxHeight / (float) bitmapHeight;
1139 }
1140 if (scale != 1f) {
1141 bitmapWidth = (int) (scale * bitmapWidth);
1142 bitmapHeight = (int) (scale * bitmapHeight);
1143 }
1144
1145 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1146
1147 if (widgetPreviewExists) {
1148 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1149 } else {
1150 // Generate a preview image if we couldn't load one
1151 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1152 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1153 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001154 if (cellHSpan != 1 || cellVSpan != 1) {
1155 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1156 bitmapHeight);
1157 }
Winson Chung4b576be2011-04-27 17:40:20 -07001158
1159 // Draw the icon in the top left corner
1160 try {
1161 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001162 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1163 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001164 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001165 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001166 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1167
Peter Ng8db70002011-10-25 15:40:08 -07001168 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1169 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001170 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001171 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001172 }
Winson Chungb44b5242011-06-13 11:32:14 -07001173 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001174 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001175
Michael Jurka038f9d82011-11-03 13:50:45 -07001176 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001177 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001178
Winson Chungd2945262011-06-24 15:22:14 -07001179 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001180 final ArrayList<Object> items = new ArrayList<Object>();
1181 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1182 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001183 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001184 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1185 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001186 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001187
Winson Chunge4a647f2011-09-30 14:41:25 -07001188 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001189 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001190 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1191 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001192 }
1193
Winson Chunge4a647f2011-09-30 14:41:25 -07001194 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001195 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001196 layout.setColumnCount(layout.getCellCountX());
1197 for (int i = 0; i < items.size(); ++i) {
1198 Object rawInfo = items.get(i);
1199 PendingAddItemInfo createItemInfo = null;
1200 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1201 R.layout.apps_customize_widget, layout, false);
1202 if (rawInfo instanceof AppWidgetProviderInfo) {
1203 // Fill in the widget information
1204 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1205 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001206
1207 // Determine the widget spans and min resize spans.
1208 int[] spanXY = mLauncher.getSpanForWidget(info, null);
1209 int[] size = mLauncher.getWorkspace().estimateItemSize(spanXY[0],
1210 spanXY[1], createItemInfo, true);
1211 createItemInfo.spanX = spanXY[0];
1212 createItemInfo.spanY = spanXY[1];
1213 int[] minSpanXY = mLauncher.getMinSpanForWidget(info, null);
1214 createItemInfo.minSpanX = minSpanXY[0];
1215 createItemInfo.minSpanY = minSpanXY[1];
1216
1217 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001218 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001219 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001220 } else if (rawInfo instanceof ResolveInfo) {
1221 // Fill in the shortcuts information
1222 ResolveInfo info = (ResolveInfo) rawInfo;
1223 createItemInfo = new PendingAddItemInfo();
1224 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1225 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1226 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001227 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001228 widget.setTag(createItemInfo);
1229 }
1230 widget.setOnClickListener(this);
1231 widget.setOnLongClickListener(this);
1232 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001233 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001234
1235 // Layout each widget
1236 int ix = i % mWidgetCountX;
1237 int iy = i / mWidgetCountX;
1238 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1239 GridLayout.spec(iy, GridLayout.LEFT),
1240 GridLayout.spec(ix, GridLayout.TOP));
1241 lp.width = cellWidth;
1242 lp.height = cellHeight;
1243 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1244 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1245 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1246 layout.addView(widget, lp);
1247 }
1248
Michael Jurka038f9d82011-11-03 13:50:45 -07001249 // wait until a call on onLayout to start loading, because
1250 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1251 // TODO: can we do a measure/layout immediately?
1252 layout.setOnLayoutListener(new Runnable() {
1253 public void run() {
1254 // Load the widget previews
1255 int maxPreviewWidth = cellWidth;
1256 int maxPreviewHeight = cellHeight;
1257 if (layout.getChildCount() > 0) {
1258 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1259 int[] maxSize = w.getPreviewSize();
1260 maxPreviewWidth = maxSize[0];
1261 maxPreviewHeight = maxSize[1];
1262 }
1263 if (immediate) {
1264 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1265 maxPreviewWidth, maxPreviewHeight, null, null);
1266 loadWidgetPreviewsInBackground(null, data);
1267 onSyncWidgetPageItems(data);
1268 } else {
1269 prepareLoadWidgetPreviewsTask(page, items,
1270 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1271 }
1272 }
1273 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001274 }
1275 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1276 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001277 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1278 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001279 if (task != null) {
1280 // Ensure that this task starts running at the correct priority
1281 task.syncThreadPriority();
1282 }
1283
1284 // Load each of the widget/shortcut previews
1285 ArrayList<Object> items = data.items;
1286 ArrayList<Bitmap> images = data.generatedImages;
1287 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001288 for (int i = 0; i < count; ++i) {
1289 if (task != null) {
1290 // Ensure we haven't been cancelled yet
1291 if (task.isCancelled()) break;
1292 // Before work on each item, ensure that this task is running at the correct
1293 // priority
1294 task.syncThreadPriority();
1295 }
1296
1297 Object rawInfo = items.get(i);
1298 if (rawInfo instanceof AppWidgetProviderInfo) {
1299 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001300 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chung72d59842012-02-22 13:51:36 -08001301
1302 int maxWidth = Math.min(data.maxImageWidth,
1303 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1304 int maxHeight = Math.min(data.maxImageHeight,
1305 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001306 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001307 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001308 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001309 } else if (rawInfo instanceof ResolveInfo) {
1310 // Fill in the shortcuts information
1311 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001312 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001313 }
1314 }
Winson Chungb44b5242011-06-13 11:32:14 -07001315 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001316
Winson Chungb44b5242011-06-13 11:32:14 -07001317 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001318 if (mInTransition) {
1319 mDeferredSyncWidgetPageItems.add(data);
1320 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001322 try {
1323 int page = data.page;
1324 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001325
Michael Jurka39e5d172012-03-12 18:36:12 -07001326 ArrayList<Object> items = data.items;
1327 int count = items.size();
1328 for (int i = 0; i < count; ++i) {
1329 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1330 if (widget != null) {
1331 Bitmap preview = data.generatedImages.get(i);
1332 widget.applyPreview(new FastBitmapDrawable(preview), i);
1333 }
1334 }
Winson Chung68e4c642011-11-10 15:48:25 -08001335
Michael Jurka39e5d172012-03-12 18:36:12 -07001336 layout.createHardwareLayer();
1337 invalidate();
1338
1339 // Update all thread priorities
1340 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1341 while (iter.hasNext()) {
1342 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1343 int pageIndex = task.page;
1344 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1345 }
1346 } finally {
1347 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001348 }
Winson Chungb44b5242011-06-13 11:32:14 -07001349 }
Winson Chung46af2e82011-05-09 16:00:53 -07001350
Winson Chung785d2eb2011-04-14 16:08:02 -07001351 @Override
1352 public void syncPages() {
1353 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001354 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001355
Adam Cohen0cd3b642011-10-14 14:58:00 -07001356 Context context = getContext();
1357 for (int j = 0; j < mNumWidgetPages; ++j) {
1358 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1359 mWidgetCountY);
1360 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001361 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001362 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001363 }
1364
Adam Cohen0cd3b642011-10-14 14:58:00 -07001365 for (int i = 0; i < mNumAppsPages; ++i) {
1366 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1367 setupPage(layout);
1368 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001369 }
1370 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001371
Winson Chung785d2eb2011-04-14 16:08:02 -07001372 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001373 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001374 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001375 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001376 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001377 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001378 }
1379 }
1380
Adam Cohen22f823d2011-09-01 17:22:18 -07001381 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1382 // it is in the z-order. This is important to insure touch events are handled correctly.
1383 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001384 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001385 }
1386
Adam Cohenae4f1552011-10-20 00:15:42 -07001387 @Override
1388 protected int indexToPage(int index) {
1389 return getChildCount() - index - 1;
1390 }
1391
Adam Cohen22f823d2011-09-01 17:22:18 -07001392 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1393 @Override
1394 protected void screenScrolled(int screenCenter) {
1395 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001396
1397 for (int i = 0; i < getChildCount(); i++) {
1398 View v = getPageAt(i);
1399 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001400 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001401
1402 float interpolatedProgress =
1403 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1404 float scale = (1 - interpolatedProgress) +
1405 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1406 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001407
Adam Cohen2591f6a2011-10-25 14:36:40 -07001408 float alpha;
1409
1410 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1411 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001412 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001413 } else {
1414 // On large screens we need to fade the page as it nears its leftmost position
1415 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1416 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001417
1418 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1419 int pageWidth = v.getMeasuredWidth();
1420 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001421
1422 if (PERFORM_OVERSCROLL_ROTATION) {
1423 if (i == 0 && scrollProgress < 0) {
1424 // Overscroll to the left
1425 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1426 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1427 scale = 1.0f;
1428 alpha = 1.0f;
1429 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001430 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001431 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1432 // Overscroll to the right
1433 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1434 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1435 scale = 1.0f;
1436 alpha = 1.0f;
1437 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001438 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001439 } else {
1440 v.setPivotY(pageHeight / 2.0f);
1441 v.setPivotX(pageWidth / 2.0f);
1442 v.setRotationY(0f);
1443 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001444 }
1445
1446 v.setTranslationX(translationX);
1447 v.setScaleX(scale);
1448 v.setScaleY(scale);
1449 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001450
1451 // If the view has 0 alpha, we set it to be invisible so as to prevent
1452 // it from accepting touches
1453 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1454 v.setVisibility(INVISIBLE);
1455 } else if (v.getVisibility() != VISIBLE) {
1456 v.setVisibility(VISIBLE);
1457 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 }
1459 }
1460 }
1461
1462 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001463 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001464 }
1465
Winson Chung785d2eb2011-04-14 16:08:02 -07001466 /**
1467 * Used by the parent to get the content width to set the tab bar to
1468 * @return
1469 */
1470 public int getPageContentWidth() {
1471 return mContentWidth;
1472 }
1473
Winson Chungb26f3d62011-06-02 10:49:29 -07001474 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001475 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001476 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001477
1478 // We reset the save index when we change pages so that it will be recalculated on next
1479 // rotation
1480 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001481 }
1482
Winson Chung785d2eb2011-04-14 16:08:02 -07001483 /*
1484 * AllAppsView implementation
1485 */
1486 @Override
1487 public void setup(Launcher launcher, DragController dragController) {
1488 mLauncher = launcher;
1489 mDragController = dragController;
1490 }
1491 @Override
1492 public void zoom(float zoom, boolean animate) {
1493 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1494 }
1495 @Override
1496 public boolean isVisible() {
1497 return (getVisibility() == VISIBLE);
1498 }
1499 @Override
1500 public boolean isAnimating() {
1501 return false;
1502 }
1503 @Override
1504 public void setApps(ArrayList<ApplicationInfo> list) {
1505 mApps = list;
1506 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001507 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001508
Winson Chung875de7e2011-06-28 14:25:17 -07001509 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1510 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001511 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001512 }
1513 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1514 // We add it in place, in alphabetical order
1515 int count = list.size();
1516 for (int i = 0; i < count; ++i) {
1517 ApplicationInfo info = list.get(i);
1518 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1519 if (index < 0) {
1520 mApps.add(-(index + 1), info);
1521 }
1522 }
1523 }
1524 @Override
1525 public void addApps(ArrayList<ApplicationInfo> list) {
1526 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001527 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001528 invalidatePageData();
1529 }
1530 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1531 ComponentName removeComponent = item.intent.getComponent();
1532 int length = list.size();
1533 for (int i = 0; i < length; ++i) {
1534 ApplicationInfo info = list.get(i);
1535 if (info.intent.getComponent().equals(removeComponent)) {
1536 return i;
1537 }
1538 }
1539 return -1;
1540 }
1541 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1542 // loop through all the apps and remove apps that have the same component
1543 int length = list.size();
1544 for (int i = 0; i < length; ++i) {
1545 ApplicationInfo info = list.get(i);
1546 int removeIndex = findAppByComponent(mApps, info);
1547 if (removeIndex > -1) {
1548 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001549 }
1550 }
1551 }
1552 @Override
1553 public void removeApps(ArrayList<ApplicationInfo> list) {
1554 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001555 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001556 invalidatePageData();
1557 }
1558 @Override
1559 public void updateApps(ArrayList<ApplicationInfo> list) {
1560 // We remove and re-add the updated applications list because it's properties may have
1561 // changed (ie. the title), and this will ensure that the items will be in their proper
1562 // place in the list.
1563 removeAppsWithoutInvalidate(list);
1564 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001565 updatePageCounts();
1566
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 invalidatePageData();
1568 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001569
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 @Override
1571 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001572 // If we have reset, then we should not continue to restore the previous state
1573 mSaveInstanceStateItemIndex = -1;
1574
Adam Cohenb64d36e2011-10-17 21:48:02 -07001575 AppsCustomizeTabHost tabHost = getTabHost();
1576 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001577 if (tag != null) {
1578 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1579 tabHost.setCurrentTabFromContent(ContentType.Applications);
1580 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001581 }
Winson Chung649668f2012-01-10 13:07:16 -08001582
Adam Cohenb64d36e2011-10-17 21:48:02 -07001583 if (mCurrentPage != 0) {
1584 invalidatePageData(0);
1585 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001587
1588 private AppsCustomizeTabHost getTabHost() {
1589 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1590 }
1591
Winson Chung785d2eb2011-04-14 16:08:02 -07001592 @Override
1593 public void dumpState() {
1594 // TODO: Dump information related to current list of Applications, Widgets, etc.
1595 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1596 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1597 }
Adam Cohen4e844012011-11-09 13:48:04 -08001598
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001600 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001602 for (Object i: list) {
1603 if (i instanceof AppWidgetProviderInfo) {
1604 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1605 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1606 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1607 + " initialLayout=" + info.initialLayout
1608 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1609 } else if (i instanceof ResolveInfo) {
1610 ResolveInfo info = (ResolveInfo) i;
1611 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1612 + info.icon);
1613 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 }
1615 }
Adam Cohen4e844012011-11-09 13:48:04 -08001616
Winson Chung785d2eb2011-04-14 16:08:02 -07001617 @Override
1618 public void surrender() {
1619 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1620 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001621
1622 // Stop all background tasks
1623 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 }
Winson Chung007c6982011-06-14 13:27:53 -07001625
Winson Chunge4e50662012-01-23 14:45:13 -08001626 @Override
1627 public void iconPressed(PagedViewIcon icon) {
1628 // Reset the previously pressed icon and store a reference to the pressed icon so that
1629 // we can reset it on return to Launcher (in Launcher.onResume())
1630 if (mPressedIcon != null) {
1631 mPressedIcon.resetDrawableState();
1632 }
1633 mPressedIcon = icon;
1634 }
1635
1636 public void resetDrawableState() {
1637 if (mPressedIcon != null) {
1638 mPressedIcon.resetDrawableState();
1639 mPressedIcon = null;
1640 }
1641 }
Winson Chung68e4c642011-11-10 15:48:25 -08001642
Winson Chungb44b5242011-06-13 11:32:14 -07001643 /*
1644 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1645 * widget previews in the background with the AsyncTasks.
1646 */
Winson Chung68e4c642011-11-10 15:48:25 -08001647 final static int sLookBehindPageCount = 2;
1648 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001649 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001650 final int count = getChildCount();
1651 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1652 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1653 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001654 }
1655 protected int getAssociatedUpperPageBound(int page) {
1656 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001657 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1658 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1659 count - 1);
1660 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001661 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001662
1663 @Override
1664 protected String getCurrentPageDescription() {
1665 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1666 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001667 int count = 0;
1668
Adam Cohen0cd3b642011-10-14 14:58:00 -07001669 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001670 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001671 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001672 } else {
1673 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001674 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001675 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001676 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001677
Adam Cohend3357b12011-10-18 14:58:11 -07001678 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001679 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001680}