blob: 34fec46bfee43cc30c24ca935b4b7d03aa9dc1e2 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070028import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070029import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010033import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.graphics.Rect;
35import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070036import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070037import android.os.Build;
38import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070039import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.util.AttributeSet;
41import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070042import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080043import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.view.LayoutInflater;
45import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070046import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070047import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070049import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070051import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070052
Daniel Sandler325dc232013-06-05 22:57:57 -040053import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054
55import java.util.ArrayList;
56import java.util.Collections;
57import java.util.Iterator;
58import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070059
Winson Chungb44b5242011-06-13 11:32:14 -070060/**
61 * A simple callback interface which also provides the results of the task.
62 */
63interface AsyncTaskCallback {
64 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
65}
Winson Chung4e076542011-06-23 13:04:10 -070066
Winson Chungb44b5242011-06-13 11:32:14 -070067/**
68 * The data needed to perform either of the custom AsyncTasks.
69 */
70class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070071 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080072 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070073 }
74
Michael Jurka038f9d82011-11-03 13:50:45 -070075 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010076 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070077 page = p;
78 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070079 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070080 maxImageWidth = cw;
81 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070082 doInBackgroundCallback = bgR;
83 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010084 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070085 }
Winson Chung09945932011-09-20 14:22:40 -070086 void cleanup(boolean cancelled) {
87 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070088 if (generatedImages != null) {
89 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010090 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010091 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070092 }
93 }
94 generatedImages.clear();
95 }
96 }
Winson Chungb44b5242011-06-13 11:32:14 -070097 int page;
98 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070099 ArrayList<Bitmap> sourceImages;
100 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700101 int maxImageWidth;
102 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 AsyncTaskCallback doInBackgroundCallback;
104 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100105 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700106}
Winson Chung4e076542011-06-23 13:04:10 -0700107
Winson Chungb44b5242011-06-13 11:32:14 -0700108/**
109 * A generic template for an async task used in AppsCustomize.
110 */
111class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700112 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700113 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700114 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700115 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700116 }
117 @Override
118 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
119 if (params.length != 1) return null;
120 // Load each of the widget previews in the background
121 params[0].doInBackgroundCallback.run(this, params[0]);
122 return params[0];
123 }
124 @Override
125 protected void onPostExecute(AsyncTaskPageData result) {
126 // All the widget previews are loaded, so we can just callback to inflate the page
127 result.postExecuteCallback.run(this, result);
128 }
129
130 void setThreadPriority(int p) {
131 threadPriority = p;
132 }
133 void syncThreadPriority() {
134 Process.setThreadPriority(threadPriority);
135 }
136
137 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700138 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 int threadPriority;
141}
Winson Chungb44b5242011-06-13 11:32:14 -0700142
143/**
144 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
145 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700146public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700147 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700148 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
149 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700150 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700151
152 /**
153 * The different content types that this paged view can show.
154 */
155 public enum ContentType {
156 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700157 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700158 }
159
160 // Refs
161 private Launcher mLauncher;
162 private DragController mDragController;
163 private final LayoutInflater mLayoutInflater;
164 private final PackageManager mPackageManager;
165
Winson Chung5afbf7b2011-07-25 11:53:08 -0700166 // Save and Restore
167 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800168 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700169
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200171 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700172 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700173
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800175 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private int mClingFocusedX;
177 private int mClingFocusedY;
178
Winson Chung1ed747a2011-05-03 16:18:34 -0700179 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700180 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700181 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700182
183 // Dimens
184 private int mContentWidth;
Winson Chung6032e7e2011-11-08 15:47:17 -0800185 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700186 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700187 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700189 private int mNumAppsPages;
190 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700191
Adam Cohen22f823d2011-09-01 17:22:18 -0700192 // Relating to the scroll and overscroll effects
193 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700194 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float TRANSITION_PIVOT = 0.65f;
197 private static float TRANSITION_MAX_ROTATION = 22;
198 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700199 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700200 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700201
Adam Cohen947dc542013-06-06 22:43:33 -0700202 public static boolean DISABLE_ALL_APPS = true;
203
Winson Chungb44b5242011-06-13 11:32:14 -0700204 // Previews & outlines
205 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800206 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700207
Adam Cohened66b2b2012-01-23 17:28:51 -0800208 private Runnable mInflateWidgetRunnable = null;
209 private Runnable mBindWidgetRunnable = null;
210 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700211 static final int WIDGET_PRELOAD_PENDING = 0;
212 static final int WIDGET_BOUND = 1;
213 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800214 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
215 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800216 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800217 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800218
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700219 private Toast mWidgetInstructionToast;
220
Michael Jurka39e5d172012-03-12 18:36:12 -0700221 // Deferral of loading widget previews during launcher transitions
222 private boolean mInTransition;
223 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
224 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700225 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
226 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700227
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700228 private Rect mTmpRect = new Rect();
229
Michael Jurkadac85912012-05-18 15:04:49 -0700230 // Used for drawing shortcut previews
231 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
232 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
233 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
234
235 // Used for drawing widget previews
236 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
237 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
238 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
239 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
240
Michael Jurka05713af2013-01-23 12:39:24 +0100241 WidgetPreviewLoader mWidgetPreviewLoader;
242
Michael Jurkac402cd92013-05-20 15:49:32 +0200243 private boolean mInBulkBind;
244 private boolean mNeedToUpdatePageCountsAndInvalidateData;
245
Winson Chung785d2eb2011-04-14 16:08:02 -0700246 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
247 super(context, attrs);
248 mLayoutInflater = LayoutInflater.from(context);
249 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200250 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700251 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400252 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700253 mCanvas = new Canvas();
254 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700255
256 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800257 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
258 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
259 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700260 mWidgetWidthGap =
261 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
262 mWidgetHeightGap =
263 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700264 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
265 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700266 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
267 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700268 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700270
Winson Chung1ed747a2011-05-03 16:18:34 -0700271 // The padding on the non-matched dimension for the default widget preview icons
272 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700273 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700274
275 // Unless otherwise specified this view is important for accessibility.
276 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
277 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
278 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700279 }
280
281 @Override
282 protected void init() {
283 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700284 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700285
286 Context context = getContext();
287 Resources r = context.getResources();
288 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
289 }
290
Winson Chung5afbf7b2011-07-25 11:53:08 -0700291 /** Returns the item index of the center item on this page so that we can restore to this
292 * item index when we rotate. */
293 private int getMiddleComponentIndexOnCurrentPage() {
294 int i = -1;
295 if (getPageCount() > 0) {
296 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700297 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700298 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700299 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
300 int numItemsPerPage = mCellCountX * mCellCountY;
301 int childCount = childrenLayout.getChildCount();
302 if (childCount > 0) {
303 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700304 }
305 } else {
306 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700307 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700308 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
309 int childCount = layout.getChildCount();
310 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 i = numApps +
312 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
313 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700314 }
315 }
316 return i;
317 }
318
319 /** Get the index of the item to restore to if we need to restore the current page. */
320 int getSaveInstanceStateIndex() {
321 if (mSaveInstanceStateItemIndex == -1) {
322 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
323 }
324 return mSaveInstanceStateItemIndex;
325 }
326
327 /** Returns the page in the current orientation which is expected to contain the specified
328 * item index. */
329 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 if (index < 0) return 0;
331
332 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mCellCountX * mCellCountY;
334 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700337 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
338 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700339 }
340
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 /** Restores the page for an item at the specified index */
342 void restorePageForIndex(int index) {
343 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 }
346
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700347 private void updatePageCounts() {
348 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
349 (float) (mWidgetCountX * mWidgetCountY));
350 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
351 }
352
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100354 if (mWidgetPreviewLoader == null) {
355 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
356 }
357
Winson Chungf0ea4d32011-06-06 14:27:16 -0700358 int maxCellCountX = Integer.MAX_VALUE;
359 int maxCellCountY = Integer.MAX_VALUE;
Winson Chung6032e7e2011-11-08 15:47:17 -0800360 if (mMaxAppCellCountX > -1) {
361 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
362 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200363 // Temp hack for now: only use the max cell count Y for widget layout
364 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800365 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200366 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800367 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700368
369 // Now that the data is ready, we can calculate the content width, the number of cells to
370 // use for each page
371 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
372 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
373 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
374 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
375 mCellCountX = mWidgetSpacingLayout.getCellCountX();
376 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700377 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700378
Winson Chungdb1138b2011-06-30 14:39:35 -0700379 // Force a measure to update recalculate the gaps
380 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
381 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200382 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700383 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700384 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700385
Michael Jurkae326f182011-11-21 14:05:46 -0800386 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
387 final boolean hostIsTransitioning = host.isTransitioning();
388
Adam Cohen0cd3b642011-10-14 14:58:00 -0700389 // Restore the page
390 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800391 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700392
Winson Chung3f4e1422011-11-17 14:58:51 -0800393 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
394 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
395 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800396 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800397 post(new Runnable() {
398 @Override
399 public void run() {
400 showAllAppsCling();
401 }
402 });
403 }
404 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700405
Winson Chung3f4e1422011-11-17 14:58:51 -0800406 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700407 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800408 mHasShownAllAppsCling = true;
409 // Calculate the position for the cling punch through
410 int[] offset = new int[2];
411 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
412 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
413 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800414 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
416 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800417 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800418 mLauncher.showFirstRunAllAppsCling(pos);
419 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700420 }
421
422 @Override
423 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
424 int width = MeasureSpec.getSize(widthMeasureSpec);
425 int height = MeasureSpec.getSize(heightMeasureSpec);
426 if (!isDataReady()) {
Adam Cohen947dc542013-06-06 22:43:33 -0700427 if ((DISABLE_ALL_APPS || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700428 setDataIsReady();
429 setMeasuredDimension(width, height);
430 onDataReady(width, height);
431 }
432 }
433
434 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
435 }
436
Michael Jurkac402cd92013-05-20 15:49:32 +0200437 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700438 LauncherAppState app = LauncherAppState.getInstance();
439 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
440
Winson Chung1ed747a2011-05-03 16:18:34 -0700441 // Get the list of widgets and shortcuts
442 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200443 for (Object o : widgetsAndShortcuts) {
444 if (o instanceof AppWidgetProviderInfo) {
445 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
446 widget.label = widget.label.trim();
447 if (widget.minWidth > 0 && widget.minHeight > 0) {
448 // Ensure that all widgets we show can be added on a workspace of this size
449 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
450 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
451 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
452 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700453 if (minSpanX <= (int) grid.numColumns &&
454 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200455 mWidgets.add(widget);
456 } else {
457 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
458 widget.minWidth + ", " + widget.minHeight + ")");
459 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700460 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200461 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
462 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700463 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800464 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200465 // just add shortcuts
466 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800467 }
468 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200469 updatePageCountsAndInvalidateData();
470 }
471
472 public void setBulkBind(boolean bulkBind) {
473 if (bulkBind) {
474 mInBulkBind = true;
475 } else {
476 mInBulkBind = false;
477 if (mNeedToUpdatePageCountsAndInvalidateData) {
478 updatePageCountsAndInvalidateData();
479 }
480 }
481 }
482
483 private void updatePageCountsAndInvalidateData() {
484 if (mInBulkBind) {
485 mNeedToUpdatePageCountsAndInvalidateData = true;
486 } else {
487 updatePageCounts();
488 invalidateOnDataChange();
489 mNeedToUpdatePageCountsAndInvalidateData = false;
490 }
Winson Chung4b576be2011-04-27 17:40:20 -0700491 }
492
493 @Override
494 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700495 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700496 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700497 mLauncher.getWorkspace().isSwitchingState()) return;
498
Winson Chung4b576be2011-04-27 17:40:20 -0700499 if (v instanceof PagedViewIcon) {
500 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200501 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800502
503 // Lock the drawable state to pressed until we return to Launcher
504 if (mPressedIcon != null) {
505 mPressedIcon.lockDrawableState();
506 }
Winson Chungc7450e32012-04-17 17:34:08 -0700507
Winson Chung18f41f82012-05-09 13:28:10 -0700508 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
509 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
510 // when Launcher resumes and we are still in AllApps.
511 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700512 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
513
Winson Chung4b576be2011-04-27 17:40:20 -0700514 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700515 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700516 if (mWidgetInstructionToast != null) {
517 mWidgetInstructionToast.cancel();
518 }
519 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
520 Toast.LENGTH_SHORT);
521 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700522
Winson Chungd2e87b32011-06-02 10:53:07 -0700523 // Create a little animation to show that the widget can move
524 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
525 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700526 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
527 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700528 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700529 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700530 tydAnim.setDuration(100);
531 bounce.play(tyuAnim).before(tydAnim);
532 bounce.setInterpolator(new AccelerateInterpolator());
533 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700534 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700535 }
536
Winson Chungc6f10b92011-11-14 11:39:07 -0800537 public boolean onKey(View v, int keyCode, KeyEvent event) {
538 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
539 }
540
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 /*
542 * PagedViewWithDraggableItems implementation
543 */
544 @Override
545 protected void determineDraggingStart(android.view.MotionEvent ev) {
546 // Disable dragging by pulling an app down for now.
547 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700548
Winson Chung4b576be2011-04-27 17:40:20 -0700549 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700550 mLauncher.getWorkspace().onDragStartedWithItem(v);
551 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700552 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700553
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700554 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
555 Bundle options = null;
556 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
557 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700558 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
559 info.componentName, null);
560
561 float density = getResources().getDisplayMetrics().density;
562 int xPaddingDips = (int) ((padding.left + padding.right) / density);
563 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
564
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700565 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700566 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
567 mTmpRect.left - xPaddingDips);
568 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
569 mTmpRect.top - yPaddingDips);
570 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
571 mTmpRect.right - xPaddingDips);
572 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
573 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700574 }
575 return options;
576 }
577
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700578 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700580 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
581
Adam Cohened66b2b2012-01-23 17:28:51 -0800582 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700583 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800584 return;
585 }
586
Adam Cohen21a170b2012-05-30 15:17:06 -0700587 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 mBindWidgetRunnable = new Runnable() {
589 @Override
590 public void run() {
591 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700592 // Options will be null for platforms with JB or lower, so this serves as an
593 // SDK level check.
594 if (options == null) {
595 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
596 mWidgetLoadingId, info.componentName)) {
597 mWidgetCleanupState = WIDGET_BOUND;
598 }
599 } else {
600 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
601 mWidgetLoadingId, info.componentName, options)) {
602 mWidgetCleanupState = WIDGET_BOUND;
603 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700604 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 }
606 };
607 post(mBindWidgetRunnable);
608
609 mInflateWidgetRunnable = new Runnable() {
610 @Override
611 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700612 if (mWidgetCleanupState != WIDGET_BOUND) {
613 return;
614 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 AppWidgetHostView hostView = mLauncher.
616 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 info.boundWidget = hostView;
618 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800619 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700620 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
621 info.spanY, info, false);
622
623 // We want the first widget layout to be the correct size. This will be important
624 // for width size reporting to the AppWidgetManager.
625 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
626 unScaledSize[1]);
627 lp.x = lp.y = 0;
628 lp.customPosition = true;
629 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800630 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 }
632 };
633 post(mInflateWidgetRunnable);
634 }
635
636 @Override
637 public void onShortPress(View v) {
638 // We are anticipating a long press, and we use this time to load bind and instantiate
639 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700640 if (mCreateWidgetInfo != null) {
641 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
642 cleanupWidgetPreloading(false);
643 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800644 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700645 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 }
647
Adam Cohen0e56cc92012-05-11 15:57:05 -0700648 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
649 if (!widgetWasAdded) {
650 // If the widget was not added, we may need to do further cleanup.
651 PendingAddWidgetInfo info = mCreateWidgetInfo;
652 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700653
654 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700655 // We never did any preloading, so just remove pending callbacks to do so
656 removeCallbacks(mBindWidgetRunnable);
657 removeCallbacks(mInflateWidgetRunnable);
658 } else if (mWidgetCleanupState == WIDGET_BOUND) {
659 // Delete the widget id which was allocated
660 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700661 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
662 }
663
664 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700665 removeCallbacks(mInflateWidgetRunnable);
666 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700667 // Delete the widget id which was allocated
668 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700669 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
670 }
671
Adam Cohen0e56cc92012-05-11 15:57:05 -0700672 // The widget was inflated and added to the DragLayer -- remove it.
673 AppWidgetHostView widget = info.boundWidget;
674 mLauncher.getDragLayer().removeView(widget);
675 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800676 }
677 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
678 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700679 mCreateWidgetInfo = null;
680 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 }
682
Adam Cohen7a326642012-02-22 12:03:22 -0800683 @Override
684 public void cleanUpShortPress(View v) {
685 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700686 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800687 }
688 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800689
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700690 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800691 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700692 // Get the widget preview as the drag representation
693 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700694 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700695
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700696 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
697 // we abort the drag.
698 if (image.getDrawable() == null) {
699 mDraggingWidget = false;
700 return false;
701 }
702
Winson Chung4b576be2011-04-27 17:40:20 -0700703 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800704 Bitmap preview;
705 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800706 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100707 Point previewPadding = null;
708
Winson Chung1ed747a2011-05-03 16:18:34 -0700709 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700710 // This can happen in some weird cases involving multi-touch. We can't start dragging
711 // the widget if this is null, so we break out.
712 if (mCreateWidgetInfo == null) {
713 return false;
714 }
715
Adam Cohen1b36dc32012-02-13 19:27:37 -0800716 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
717 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700718 int spanX = createItemInfo.spanX;
719 int spanY = createItemInfo.spanY;
720 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
721 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700722
Winson Chung72d59842012-02-22 13:51:36 -0800723 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
724 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700725 int maxWidth, maxHeight;
726 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
727 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800728
Michael Jurka05713af2013-01-23 12:39:24 +0100729 int[] previewSizeBeforeScale = new int[1];
730
731 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
732 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
733 maxWidth, maxHeight, null, previewSizeBeforeScale);
734
735 // Compare the size of the drag preview to the preview in the AppsCustomize tray
736 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
737 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
738 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
739
740 // The bitmap in the AppsCustomize tray is always the the same size, so there
741 // might be extra pixels around the preview itself - this accounts for that
742 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
743 int padding =
744 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
745 previewPadding = new Point(padding, 0);
746 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700747 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700748 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
749 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
750 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
751 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
752
Winson Chung1120e032011-11-22 16:11:31 -0800753 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700754 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100755 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700756 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700757 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700758 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700759 createItemInfo.spanX = createItemInfo.spanY = 1;
760 }
Winson Chung4b576be2011-04-27 17:40:20 -0700761
Michael Jurka8c3339b2012-06-14 16:18:21 -0700762 // Don't clip alpha values for the drag outline if we're using the default widget preview
763 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
764 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700765
Winson Chung1120e032011-11-22 16:11:31 -0800766 // Save the preview for the outline generation, then dim the preview
767 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
768 false);
Winson Chung1120e032011-11-22 16:11:31 -0800769
Winson Chung4b576be2011-04-27 17:40:20 -0700770 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700771 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700772 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800773 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100774 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800775 outline.recycle();
776 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700777 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700778 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800779
Winson Chung4b576be2011-04-27 17:40:20 -0700780 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800781 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700782 if (!super.beginDragging(v)) return false;
783
784 if (v instanceof PagedViewIcon) {
785 beginDraggingApplication(v);
786 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700787 if (!beginDraggingWidget(v)) {
788 return false;
789 }
Winson Chung4b576be2011-04-27 17:40:20 -0700790 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800791
792 // We delay entering spring-loaded mode slightly to make sure the UI
793 // thready is free of any work.
794 postDelayed(new Runnable() {
795 @Override
796 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800797 // We don't enter spring-loaded mode if the drag has been cancelled
798 if (mLauncher.getDragController().isDragging()) {
799 // Dismiss the cling
800 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800801
Adam Cohen1b36dc32012-02-13 19:27:37 -0800802 // Reset the alpha on the dragged icon before we drag
803 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800804
Adam Cohen1b36dc32012-02-13 19:27:37 -0800805 // Go into spring loaded mode (must happen before we startDrag())
806 mLauncher.enterSpringLoadedDragMode();
807 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 }
Winson Chung72d59842012-02-22 13:51:36 -0800809 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800810
Winson Chung785d2eb2011-04-14 16:08:02 -0700811 return true;
812 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800813
Winson Chunga48487a2012-03-20 16:19:37 -0700814 /**
815 * Clean up after dragging.
816 *
817 * @param target where the item was dragged to (can be null if the item was flung)
818 */
819 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700820 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700821 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700822 // Exit spring loaded mode if we have not successfully dropped or have not handled the
823 // drop in Workspace
824 mLauncher.exitSpringLoadedDragMode();
825 }
Winson Chung4b919f82012-05-01 10:44:08 -0700826 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700827 }
828
Winson Chung785d2eb2011-04-14 16:08:02 -0700829 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700830 public View getContent() {
831 return null;
832 }
833
834 @Override
835 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700836 mInTransition = true;
837 if (toWorkspace) {
838 cancelAllTasks();
839 }
840 }
841
842 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700843 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700844 }
845
846 @Override
847 public void onLauncherTransitionStep(Launcher l, float t) {
848 }
849
850 @Override
851 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
852 mInTransition = false;
853 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
854 onSyncWidgetPageItems(d);
855 }
856 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700857 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
858 r.run();
859 }
860 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700861 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700862 }
863
864 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700865 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
866 boolean success) {
867 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
868 if (isFlingToDelete) return;
869
870 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700871
872 // Display an error message if the drag failed due to there not being enough space on the
873 // target layout we were dropping on.
874 if (!success) {
875 boolean showOutOfSpaceMessage = false;
876 if (target instanceof Workspace) {
877 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
878 Workspace workspace = (Workspace) target;
879 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700880 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700881 if (layout != null) {
882 layout.calculateSpans(itemInfo);
883 showOutOfSpaceMessage =
884 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
885 }
886 }
Winson Chungfc79c802011-05-02 13:35:34 -0700887 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700888 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700889 }
Adam Cohen7a326642012-02-22 12:03:22 -0800890
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800891 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700892 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700893 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800894 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700895 }
896
Winson Chunga48487a2012-03-20 16:19:37 -0700897 @Override
898 public void onFlingToDeleteCompleted() {
899 // We just dismiss the drag when we fling, so cleanup here
900 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700901 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700902 mDraggingWidget = false;
903 }
904
905 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800906 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700907 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800908 }
909
Winson Chung7f0acdd2011-09-19 18:34:19 -0700910 @Override
911 protected void onDetachedFromWindow() {
912 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700913 cancelAllTasks();
914 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700915
Michael Jurkae326f182011-11-21 14:05:46 -0800916 public void clearAllWidgetPages() {
917 cancelAllTasks();
918 int count = getChildCount();
919 for (int i = 0; i < count; i++) {
920 View v = getPageAt(i);
921 if (v instanceof PagedViewGridLayout) {
922 ((PagedViewGridLayout) v).removeAllViewsOnPage();
923 mDirtyPageContent.set(i, true);
924 }
925 }
926 }
927
Adam Cohen0cd3b642011-10-14 14:58:00 -0700928 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700929 // Clean up all the async tasks
930 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
931 while (iter.hasNext()) {
932 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
933 task.cancel(false);
934 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700935 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700936
937 // We've already preallocated the views for the data to load into, so clear them as well
938 View v = getPageAt(task.page);
939 if (v instanceof PagedViewGridLayout) {
940 ((PagedViewGridLayout) v).removeAllViewsOnPage();
941 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700942 }
Winson Chung83687b12012-04-25 16:01:01 -0700943 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700944 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700945 }
946
Winson Chung785d2eb2011-04-14 16:08:02 -0700947 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700948 if (type == ContentType.Widgets) {
949 invalidatePageData(mNumAppsPages, true);
950 } else if (type == ContentType.Applications) {
951 invalidatePageData(0, true);
952 }
Winson Chungb44b5242011-06-13 11:32:14 -0700953 }
954
Adam Cohen0cd3b642011-10-14 14:58:00 -0700955 protected void snapToPage(int whichPage, int delta, int duration) {
956 super.snapToPage(whichPage, delta, duration);
957 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800958
959 // Update the thread priorities given the direction lookahead
960 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
961 while (iter.hasNext()) {
962 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700963 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800964 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
965 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
966 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
967 } else {
968 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
969 }
970 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700971 }
972
973 private void updateCurrentTab(int currentPage) {
974 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800975 if (tabHost != null) {
976 String tag = tabHost.getCurrentTabTag();
977 if (tag != null) {
978 if (currentPage >= mNumAppsPages &&
979 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
980 tabHost.setCurrentTabFromContent(ContentType.Widgets);
981 } else if (currentPage < mNumAppsPages &&
982 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
983 tabHost.setCurrentTabFromContent(ContentType.Applications);
984 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700985 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700986 }
987 }
988
Winson Chung785d2eb2011-04-14 16:08:02 -0700989 /*
990 * Apps PagedView implementation
991 */
Winson Chung63257c12011-05-05 17:06:13 -0700992 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
993 int childCount = layout.getChildCount();
994 for (int i = 0; i < childCount; ++i) {
995 layout.getChildAt(i).setVisibility(visibility);
996 }
997 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700998 private void setupPage(PagedViewCellLayout layout) {
999 layout.setCellCount(mCellCountX, mCellCountY);
1000 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1001 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1002 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1003
Winson Chung63257c12011-05-05 17:06:13 -07001004 // Note: We force a measure here to get around the fact that when we do layout calculations
1005 // immediately after syncing, we don't have a proper width. That said, we already know the
1006 // expected page width, so we can actually optimize by hiding all the TextView-based
1007 // children that are expensive to measure, and let that happen naturally later.
1008 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001009 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001010 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001011 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001013 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001014 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001015
Winson Chungf314b0e2011-08-16 11:54:27 -07001016 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001017 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001018 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001019 int numCells = mCellCountX * mCellCountY;
1020 int startIndex = page * numCells;
1021 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001022 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001023
Winson Chung785d2eb2011-04-14 16:08:02 -07001024 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001025 ArrayList<Object> items = new ArrayList<Object>();
1026 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001028 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1030 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001031 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 icon.setOnClickListener(this);
1033 icon.setOnLongClickListener(this);
1034 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001035 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001036
1037 int index = i - startIndex;
1038 int x = index % mCellCountX;
1039 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001040 if (isRtl) {
1041 x = mCellCountX - x - 1;
1042 }
Winson Chung6a70e9f2011-05-17 16:24:49 -07001043 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001044
1045 items.add(info);
1046 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001047 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001048
Michael Jurka47639b92013-01-14 12:42:27 +01001049 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001050 }
Winson Chungb44b5242011-06-13 11:32:14 -07001051
1052 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001053 * A helper to return the priority for loading of the specified widget page.
1054 */
1055 private int getWidgetPageLoadPriority(int page) {
1056 // If we are snapping to another page, use that index as the target page index
1057 int toPage = mCurrentPage;
1058 if (mNextPage > -1) {
1059 toPage = mNextPage;
1060 }
1061
1062 // We use the distance from the target page as an initial guess of priority, but if there
1063 // are no pages of higher priority than the page specified, then bump up the priority of
1064 // the specified page.
1065 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1066 int minPageDiff = Integer.MAX_VALUE;
1067 while (iter.hasNext()) {
1068 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001069 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001070 }
1071
1072 int rawPageDiff = Math.abs(page - toPage);
1073 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1074 }
1075 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001076 * Return the appropriate thread priority for loading for a given page (we give the current
1077 * page much higher priority)
1078 */
1079 private int getThreadPriorityForPage(int page) {
1080 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001081 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001082 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001083 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001084 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001085 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001086 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001087 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001088 }
1089 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001090 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001091 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001092 return Math.max(0, pageDiff * sPageSleepDelay);
1093 }
Winson Chungb44b5242011-06-13 11:32:14 -07001094 /**
1095 * Creates and executes a new AsyncTask to load a page of widget previews.
1096 */
1097 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001098 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001099
Winson Chungb44b5242011-06-13 11:32:14 -07001100 // Prune all tasks that are no longer needed
1101 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1102 while (iter.hasNext()) {
1103 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001104 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001105 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1106 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001107 task.cancel(false);
1108 iter.remove();
1109 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001110 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001111 }
1112 }
1113
Winson Chungf314b0e2011-08-16 11:54:27 -07001114 // 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 -07001115 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001116 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001117 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001118 @Override
1119 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001120 try {
Winson Chung09945932011-09-20 14:22:40 -07001121 try {
1122 Thread.sleep(sleepMs);
1123 } catch (Exception e) {}
1124 loadWidgetPreviewsInBackground(task, data);
1125 } finally {
1126 if (task.isCancelled()) {
1127 data.cleanup(true);
1128 }
1129 }
Winson Chungb44b5242011-06-13 11:32:14 -07001130 }
1131 },
1132 new AsyncTaskCallback() {
1133 @Override
1134 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001135 mRunningTasks.remove(task);
1136 if (task.isCancelled()) return;
1137 // do cleanup inside onSyncWidgetPageItems
1138 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001139 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001140 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001141
1142 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001143 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001144 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001145 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001146 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1147 mRunningTasks.add(t);
1148 }
Winson Chungb44b5242011-06-13 11:32:14 -07001149
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 /*
1151 * Widgets PagedView implementation
1152 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001153 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001154 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1155 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001156
1157 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001158 // immediately after syncing, we don't have a proper width.
Winson Chung5f8afe62013-08-12 16:19:28 -07001159 int workspaceWidth = mLauncher.getWorkspace().getMeasuredWidth();
1160 int widthSpec = MeasureSpec.makeMeasureSpec(Math.min(getMeasuredWidth(), workspaceWidth),
1161 MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001162 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001163 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001164 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001165 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001166
Michael Jurka038f9d82011-11-03 13:50:45 -07001167 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001168 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001169
Winson Chungd2945262011-06-24 15:22:14 -07001170 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001171 final ArrayList<Object> items = new ArrayList<Object>();
1172 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1173 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001174 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001175 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1176 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001177 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001178
Winson Chunge4a647f2011-09-30 14:41:25 -07001179 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001180 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001181 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1182 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001183 }
1184
Winson Chunge4a647f2011-09-30 14:41:25 -07001185 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001186 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001187 layout.setColumnCount(layout.getCellCountX());
1188 for (int i = 0; i < items.size(); ++i) {
1189 Object rawInfo = items.get(i);
1190 PendingAddItemInfo createItemInfo = null;
1191 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1192 R.layout.apps_customize_widget, layout, false);
1193 if (rawInfo instanceof AppWidgetProviderInfo) {
1194 // Fill in the widget information
1195 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1196 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001197
1198 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001199 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001200 createItemInfo.spanX = spanXY[0];
1201 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001202 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001203 createItemInfo.minSpanX = minSpanXY[0];
1204 createItemInfo.minSpanY = minSpanXY[1];
1205
Michael Jurka3f4e0702013-02-05 11:21:28 +01001206 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001207 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001208 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001209 } else if (rawInfo instanceof ResolveInfo) {
1210 // Fill in the shortcuts information
1211 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001212 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001213 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1214 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1215 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001216 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001217 widget.setTag(createItemInfo);
1218 }
1219 widget.setOnClickListener(this);
1220 widget.setOnLongClickListener(this);
1221 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001222 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001223
1224 // Layout each widget
1225 int ix = i % mWidgetCountX;
1226 int iy = i / mWidgetCountX;
1227 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001228 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001229 GridLayout.spec(ix, GridLayout.TOP));
1230 lp.width = cellWidth;
1231 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001232 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001233 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1234 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1235 layout.addView(widget, lp);
1236 }
1237
Michael Jurka038f9d82011-11-03 13:50:45 -07001238 // wait until a call on onLayout to start loading, because
1239 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1240 // TODO: can we do a measure/layout immediately?
1241 layout.setOnLayoutListener(new Runnable() {
1242 public void run() {
1243 // Load the widget previews
1244 int maxPreviewWidth = cellWidth;
1245 int maxPreviewHeight = cellHeight;
1246 if (layout.getChildCount() > 0) {
1247 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1248 int[] maxSize = w.getPreviewSize();
1249 maxPreviewWidth = maxSize[0];
1250 maxPreviewHeight = maxSize[1];
1251 }
Michael Jurka05713af2013-01-23 12:39:24 +01001252
Michael Jurka3f4e0702013-02-05 11:21:28 +01001253 mWidgetPreviewLoader.setPreviewSize(
1254 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001255 if (immediate) {
1256 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001257 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001258 loadWidgetPreviewsInBackground(null, data);
1259 onSyncWidgetPageItems(data);
1260 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001261 if (mInTransition) {
1262 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1263 } else {
1264 prepareLoadWidgetPreviewsTask(page, items,
1265 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1266 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001267 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001268 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001269 }
1270 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001271 }
1272 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1273 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001274 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1275 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001276 if (task != null) {
1277 // Ensure that this task starts running at the correct priority
1278 task.syncThreadPriority();
1279 }
1280
1281 // Load each of the widget/shortcut previews
1282 ArrayList<Object> items = data.items;
1283 ArrayList<Bitmap> images = data.generatedImages;
1284 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001285 for (int i = 0; i < count; ++i) {
1286 if (task != null) {
1287 // Ensure we haven't been cancelled yet
1288 if (task.isCancelled()) break;
1289 // Before work on each item, ensure that this task is running at the correct
1290 // priority
1291 task.syncThreadPriority();
1292 }
1293
Michael Jurka05713af2013-01-23 12:39:24 +01001294 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001295 }
Winson Chungb44b5242011-06-13 11:32:14 -07001296 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001297
Winson Chungb44b5242011-06-13 11:32:14 -07001298 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001299 if (mInTransition) {
1300 mDeferredSyncWidgetPageItems.add(data);
1301 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001302 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001303 try {
1304 int page = data.page;
1305 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001306
Michael Jurka39e5d172012-03-12 18:36:12 -07001307 ArrayList<Object> items = data.items;
1308 int count = items.size();
1309 for (int i = 0; i < count; ++i) {
1310 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1311 if (widget != null) {
1312 Bitmap preview = data.generatedImages.get(i);
1313 widget.applyPreview(new FastBitmapDrawable(preview), i);
1314 }
1315 }
Winson Chung68e4c642011-11-10 15:48:25 -08001316
Michael Jurka47639b92013-01-14 12:42:27 +01001317 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001318
1319 // Update all thread priorities
1320 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1321 while (iter.hasNext()) {
1322 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1323 int pageIndex = task.page;
1324 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1325 }
1326 } finally {
1327 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001328 }
Winson Chungb44b5242011-06-13 11:32:14 -07001329 }
Winson Chung46af2e82011-05-09 16:00:53 -07001330
Winson Chung785d2eb2011-04-14 16:08:02 -07001331 @Override
1332 public void syncPages() {
1333 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001334 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001335
Adam Cohen0cd3b642011-10-14 14:58:00 -07001336 Context context = getContext();
1337 for (int j = 0; j < mNumWidgetPages; ++j) {
1338 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1339 mWidgetCountY);
1340 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001341 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001342 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001343 }
1344
Adam Cohen0cd3b642011-10-14 14:58:00 -07001345 for (int i = 0; i < mNumAppsPages; ++i) {
1346 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1347 setupPage(layout);
1348 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001349 }
1350 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001351
Winson Chung785d2eb2011-04-14 16:08:02 -07001352 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001353 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001354 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001355 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001356 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001357 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 }
1359 }
1360
Adam Cohen22f823d2011-09-01 17:22:18 -07001361 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1362 // it is in the z-order. This is important to insure touch events are handled correctly.
1363 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001364 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001365 }
1366
Adam Cohenae4f1552011-10-20 00:15:42 -07001367 @Override
1368 protected int indexToPage(int index) {
1369 return getChildCount() - index - 1;
1370 }
1371
Adam Cohen22f823d2011-09-01 17:22:18 -07001372 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1373 @Override
1374 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001375 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001376 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001377
1378 for (int i = 0; i < getChildCount(); i++) {
1379 View v = getPageAt(i);
1380 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001381 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001382
Winson Chung52aee602013-01-30 12:01:02 -08001383 float interpolatedProgress;
1384 float translationX;
1385 float maxScrollProgress = Math.max(0, scrollProgress);
1386 float minScrollProgress = Math.min(0, scrollProgress);
1387
1388 if (isRtl) {
1389 translationX = maxScrollProgress * v.getMeasuredWidth();
1390 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1391 } else {
1392 translationX = minScrollProgress * v.getMeasuredWidth();
1393 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1394 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001395 float scale = (1 - interpolatedProgress) +
1396 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001397
Adam Cohen2591f6a2011-10-25 14:36:40 -07001398 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001399 if (isRtl && (scrollProgress > 0)) {
1400 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1401 } else if (!isRtl && (scrollProgress < 0)) {
1402 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001403 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001404 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001405 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1406 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001407
1408 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1409 int pageWidth = v.getMeasuredWidth();
1410 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001411
1412 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001413 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1414 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1415 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1416
1417 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001418 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001419 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001420 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1421 scale = 1.0f;
1422 alpha = 1.0f;
1423 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001424 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001425 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001426 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001427 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001428 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1429 scale = 1.0f;
1430 alpha = 1.0f;
1431 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001432 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001433 } else {
1434 v.setPivotY(pageHeight / 2.0f);
1435 v.setPivotX(pageWidth / 2.0f);
1436 v.setRotationY(0f);
1437 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001438 }
1439
1440 v.setTranslationX(translationX);
1441 v.setScaleX(scale);
1442 v.setScaleY(scale);
1443 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001444
1445 // If the view has 0 alpha, we set it to be invisible so as to prevent
1446 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001447 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001448 v.setVisibility(INVISIBLE);
1449 } else if (v.getVisibility() != VISIBLE) {
1450 v.setVisibility(VISIBLE);
1451 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001452 }
1453 }
Michael Jurka47639b92013-01-14 12:42:27 +01001454
1455 enableHwLayersOnVisiblePages();
1456 }
1457
1458 private void enableHwLayersOnVisiblePages() {
1459 final int screenCount = getChildCount();
1460
1461 getVisiblePages(mTempVisiblePagesRange);
1462 int leftScreen = mTempVisiblePagesRange[0];
1463 int rightScreen = mTempVisiblePagesRange[1];
1464 int forceDrawScreen = -1;
1465 if (leftScreen == rightScreen) {
1466 // make sure we're caching at least two pages always
1467 if (rightScreen < screenCount - 1) {
1468 rightScreen++;
1469 forceDrawScreen = rightScreen;
1470 } else if (leftScreen > 0) {
1471 leftScreen--;
1472 forceDrawScreen = leftScreen;
1473 }
1474 } else {
1475 forceDrawScreen = leftScreen + 1;
1476 }
1477
1478 for (int i = 0; i < screenCount; i++) {
1479 final View layout = (View) getPageAt(i);
1480 if (!(leftScreen <= i && i <= rightScreen &&
1481 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1482 layout.setLayerType(LAYER_TYPE_NONE, null);
1483 }
1484 }
1485
Michael Jurka47639b92013-01-14 12:42:27 +01001486 for (int i = 0; i < screenCount; i++) {
1487 final View layout = (View) getPageAt(i);
1488
1489 if (leftScreen <= i && i <= rightScreen &&
1490 (i == forceDrawScreen || shouldDrawChild(layout))) {
1491 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1492 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1493 }
1494 }
1495 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001496 }
1497
1498 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001499 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001500 }
1501
Winson Chung785d2eb2011-04-14 16:08:02 -07001502 /**
1503 * Used by the parent to get the content width to set the tab bar to
1504 * @return
1505 */
1506 public int getPageContentWidth() {
1507 return mContentWidth;
1508 }
1509
Winson Chungb26f3d62011-06-02 10:49:29 -07001510 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001511 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001512 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001513 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001514 // We reset the save index when we change pages so that it will be recalculated on next
1515 // rotation
1516 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001517 }
1518
Winson Chung785d2eb2011-04-14 16:08:02 -07001519 /*
1520 * AllAppsView implementation
1521 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001522 public void setup(Launcher launcher, DragController dragController) {
1523 mLauncher = launcher;
1524 mDragController = dragController;
1525 }
Winson Chung9802ac92012-06-08 16:01:58 -07001526
1527 /**
1528 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1529 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1530 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1531 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1532 */
1533 private void invalidateOnDataChange() {
1534 if (!isDataReady()) {
1535 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1536 // request a layout to trigger the page data when ready.
1537 requestLayout();
1538 } else {
1539 cancelAllTasks();
1540 invalidatePageData();
1541 }
1542 }
1543
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001544 public void setApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001545 if (!DISABLE_ALL_APPS) {
1546 mApps = list;
1547 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1548 updatePageCountsAndInvalidateData();
1549 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001550 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001551 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001552 // We add it in place, in alphabetical order
1553 int count = list.size();
1554 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001555 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001556 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001557 if (index < 0) {
1558 mApps.add(-(index + 1), info);
1559 }
1560 }
1561 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001562 public void addApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001563 if (!DISABLE_ALL_APPS) {
1564 addAppsWithoutInvalidate(list);
1565 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001566 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001568 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 ComponentName removeComponent = item.intent.getComponent();
1570 int length = list.size();
1571 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001572 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001573 if (info.intent.getComponent().equals(removeComponent)) {
1574 return i;
1575 }
1576 }
1577 return -1;
1578 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001579 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001580 // loop through all the apps and remove apps that have the same component
1581 int length = list.size();
1582 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001583 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 int removeIndex = findAppByComponent(mApps, info);
1585 if (removeIndex > -1) {
1586 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001587 }
1588 }
1589 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001590 public void removeApps(ArrayList<AppInfo> appInfos) {
Winson Chung64359a52013-07-08 17:17:08 -07001591 if (!DISABLE_ALL_APPS) {
1592 removeAppsWithoutInvalidate(appInfos);
1593 updatePageCountsAndInvalidateData();
1594 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001595 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001596 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001597 // We remove and re-add the updated applications list because it's properties may have
1598 // changed (ie. the title), and this will ensure that the items will be in their proper
1599 // place in the list.
Adam Cohen947dc542013-06-06 22:43:33 -07001600 if (!DISABLE_ALL_APPS) {
1601 removeAppsWithoutInvalidate(list);
1602 addAppsWithoutInvalidate(list);
1603 updatePageCountsAndInvalidateData();
1604 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001605 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001606
Winson Chung785d2eb2011-04-14 16:08:02 -07001607 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001608 // If we have reset, then we should not continue to restore the previous state
1609 mSaveInstanceStateItemIndex = -1;
1610
Adam Cohenb64d36e2011-10-17 21:48:02 -07001611 AppsCustomizeTabHost tabHost = getTabHost();
1612 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001613 if (tag != null) {
1614 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1615 tabHost.setCurrentTabFromContent(ContentType.Applications);
1616 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001617 }
Winson Chung649668f2012-01-10 13:07:16 -08001618
Adam Cohenb64d36e2011-10-17 21:48:02 -07001619 if (mCurrentPage != 0) {
1620 invalidatePageData(0);
1621 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001622 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001623
1624 private AppsCustomizeTabHost getTabHost() {
1625 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1626 }
1627
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 public void dumpState() {
1629 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001630 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001631 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 }
Adam Cohen4e844012011-11-09 13:48:04 -08001633
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001635 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001636 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001637 for (Object i: list) {
1638 if (i instanceof AppWidgetProviderInfo) {
1639 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1640 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1641 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1642 + " initialLayout=" + info.initialLayout
1643 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1644 } else if (i instanceof ResolveInfo) {
1645 ResolveInfo info = (ResolveInfo) i;
1646 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1647 + info.icon);
1648 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001649 }
1650 }
Adam Cohen4e844012011-11-09 13:48:04 -08001651
Winson Chung785d2eb2011-04-14 16:08:02 -07001652 public void surrender() {
1653 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1654 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001655
1656 // Stop all background tasks
1657 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001658 }
Winson Chung007c6982011-06-14 13:27:53 -07001659
Winson Chunge4e50662012-01-23 14:45:13 -08001660 @Override
1661 public void iconPressed(PagedViewIcon icon) {
1662 // Reset the previously pressed icon and store a reference to the pressed icon so that
1663 // we can reset it on return to Launcher (in Launcher.onResume())
1664 if (mPressedIcon != null) {
1665 mPressedIcon.resetDrawableState();
1666 }
1667 mPressedIcon = icon;
1668 }
1669
1670 public void resetDrawableState() {
1671 if (mPressedIcon != null) {
1672 mPressedIcon.resetDrawableState();
1673 mPressedIcon = null;
1674 }
1675 }
Winson Chung68e4c642011-11-10 15:48:25 -08001676
Winson Chungb44b5242011-06-13 11:32:14 -07001677 /*
1678 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1679 * widget previews in the background with the AsyncTasks.
1680 */
Winson Chung68e4c642011-11-10 15:48:25 -08001681 final static int sLookBehindPageCount = 2;
1682 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001683 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001684 final int count = getChildCount();
1685 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1686 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1687 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001688 }
1689 protected int getAssociatedUpperPageBound(int page) {
1690 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001691 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1692 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1693 count - 1);
1694 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001695 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001696
Winson Chung6a0f57d2011-06-29 20:10:49 -07001697 protected String getCurrentPageDescription() {
1698 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1699 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001700 int count = 0;
1701
Adam Cohen0cd3b642011-10-14 14:58:00 -07001702 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001703 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001704 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001705 } else {
1706 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001707 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001708 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001709 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001710
Michael Jurka8b805b12012-04-18 14:23:14 -07001711 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001712 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001713}