blob: f90189cedc51a0cb3721b947b4b0605b90b23f1e [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;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010032import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070036import android.os.Build;
37import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.AttributeSet;
40import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070041import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080042import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
44import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070048import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070050import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * A simple callback interface which also provides the results of the task.
61 */
62interface AsyncTaskCallback {
63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
64}
Winson Chung4e076542011-06-23 13:04:10 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * The data needed to perform either of the custom AsyncTasks.
68 */
69class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070070 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080071 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070072 }
73
Michael Jurka038f9d82011-11-03 13:50:45 -070074 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010075 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070076 page = p;
77 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070078 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070079 maxImageWidth = cw;
80 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070081 doInBackgroundCallback = bgR;
82 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010083 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070084 }
Winson Chung09945932011-09-20 14:22:40 -070085 void cleanup(boolean cancelled) {
86 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070087 if (generatedImages != null) {
88 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010089 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010090 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070091 }
92 }
93 generatedImages.clear();
94 }
95 }
Winson Chungb44b5242011-06-13 11:32:14 -070096 int page;
97 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070098 ArrayList<Bitmap> sourceImages;
99 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 int maxImageWidth;
101 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 AsyncTaskCallback doInBackgroundCallback;
103 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100104 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700105}
Winson Chung4e076542011-06-23 13:04:10 -0700106
Winson Chungb44b5242011-06-13 11:32:14 -0700107/**
108 * A generic template for an async task used in AppsCustomize.
109 */
110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700111 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700112 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700113 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700114 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700115 }
116 @Override
117 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
118 if (params.length != 1) return null;
119 // Load each of the widget previews in the background
120 params[0].doInBackgroundCallback.run(this, params[0]);
121 return params[0];
122 }
123 @Override
124 protected void onPostExecute(AsyncTaskPageData result) {
125 // All the widget previews are loaded, so we can just callback to inflate the page
126 result.postExecuteCallback.run(this, result);
127 }
128
129 void setThreadPriority(int p) {
130 threadPriority = p;
131 }
132 void syncThreadPriority() {
133 Process.setThreadPriority(threadPriority);
134 }
135
136 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700137 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int threadPriority;
140}
Winson Chungb44b5242011-06-13 11:32:14 -0700141
142/**
143 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
144 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700145public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700146 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700147 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
148 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700149 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700150
151 /**
152 * The different content types that this paged view can show.
153 */
154 public enum ContentType {
155 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700156 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700157 }
Winson Chungc58497e2013-09-03 17:48:37 -0700158 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700159
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
Winson Chungc58497e2013-09-03 17:48:37 -0700184 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700185 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700186 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700187 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700188 private int mNumAppsPages;
189 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700190 private Rect mAllAppsPadding = new Rect();
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
Winson Chungc58497e2013-09-03 17:48:37 -0700202 public static boolean DISABLE_ALL_APPS = false;
Adam Cohen947dc542013-06-06 22:43:33 -0700203
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);
Winson Chungc58497e2013-09-03 17:48:37 -0700258 LauncherAppState app = LauncherAppState.getInstance();
259 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
260 mWidgetWidthGap = mWidgetHeightGap = grid.edgeMarginPx;
Winson Chung4b576be2011-04-27 17:40:20 -0700261 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
262 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
264 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700265 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700267
Winson Chung1ed747a2011-05-03 16:18:34 -0700268 // The padding on the non-matched dimension for the default widget preview icons
269 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700270 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700271
272 // Unless otherwise specified this view is important for accessibility.
273 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
274 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
275 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700276 }
277
278 @Override
279 protected void init() {
280 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700281 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700282
283 Context context = getContext();
284 Resources r = context.getResources();
285 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
286 }
287
Winson Chungc58497e2013-09-03 17:48:37 -0700288 public void onFinishInflate() {
289 super.onFinishInflate();
290
291 LauncherAppState app = LauncherAppState.getInstance();
292 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
293 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
294 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
295 }
296
Winson Chung67ca7e42013-10-31 16:53:19 -0700297 void setAllAppsPadding(Rect r) {
298 mAllAppsPadding.set(r);
299 }
300 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
301 mPageLayoutPaddingBottom = pageIndicatorHeight;
302 }
303
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100304 WidgetPreviewLoader getWidgetPreviewLoader() {
305 if (mWidgetPreviewLoader == null) {
306 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
307 }
308 return mWidgetPreviewLoader;
309 }
310
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 /** Returns the item index of the center item on this page so that we can restore to this
312 * item index when we rotate. */
313 private int getMiddleComponentIndexOnCurrentPage() {
314 int i = -1;
315 if (getPageCount() > 0) {
316 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700317 if (mContentType == ContentType.Applications) {
318 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
319 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700320 int numItemsPerPage = mCellCountX * mCellCountY;
321 int childCount = childrenLayout.getChildCount();
322 if (childCount > 0) {
323 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 }
Winson Chungc58497e2013-09-03 17:48:37 -0700325 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700326 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700327 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700328 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
329 int childCount = layout.getChildCount();
330 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700331 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700332 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700333 }
Winson Chungc58497e2013-09-03 17:48:37 -0700334 } else {
335 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 }
337 }
338 return i;
339 }
340
341 /** Get the index of the item to restore to if we need to restore the current page. */
342 int getSaveInstanceStateIndex() {
343 if (mSaveInstanceStateItemIndex == -1) {
344 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
345 }
346 return mSaveInstanceStateItemIndex;
347 }
348
349 /** Returns the page in the current orientation which is expected to contain the specified
350 * item index. */
351 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700352 if (index < 0) return 0;
353
354 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700355 int numItemsPerPage = mCellCountX * mCellCountY;
356 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700357 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700358 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700359 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700360 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700361 }
362
Winson Chung5afbf7b2011-07-25 11:53:08 -0700363 /** Restores the page for an item at the specified index */
364 void restorePageForIndex(int index) {
365 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700366 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700367 }
368
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700369 private void updatePageCounts() {
370 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
371 (float) (mWidgetCountX * mWidgetCountY));
372 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
373 }
374
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 // Now that the data is ready, we can calculate the content width, the number of cells to
377 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700378 LauncherAppState app = LauncherAppState.getInstance();
379 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700380 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
381 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungc58497e2013-09-03 17:48:37 -0700382 mCellCountX = (int) grid.allAppsNumCols;
383 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700384 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700385
Winson Chungdb1138b2011-06-30 14:39:35 -0700386 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700387 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
388 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
389 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
390 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700391 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700392
Michael Jurkae326f182011-11-21 14:05:46 -0800393 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
394 final boolean hostIsTransitioning = host.isTransitioning();
395
Adam Cohen0cd3b642011-10-14 14:58:00 -0700396 // Restore the page
397 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800398 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700399
Winson Chung3f4e1422011-11-17 14:58:51 -0800400 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
401 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
402 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800403 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800404 post(new Runnable() {
405 @Override
406 public void run() {
407 showAllAppsCling();
408 }
409 });
410 }
411 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700412
Winson Chung3f4e1422011-11-17 14:58:51 -0800413 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700414 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 mHasShownAllAppsCling = true;
416 // Calculate the position for the cling punch through
417 int[] offset = new int[2];
418 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
419 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
420 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800421 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800422 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
423 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800424 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800425 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700426 }
427
428 @Override
429 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
430 int width = MeasureSpec.getSize(widthMeasureSpec);
431 int height = MeasureSpec.getSize(heightMeasureSpec);
432 if (!isDataReady()) {
Adam Cohen947dc542013-06-06 22:43:33 -0700433 if ((DISABLE_ALL_APPS || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700434 setDataIsReady();
435 setMeasuredDimension(width, height);
436 onDataReady(width, height);
437 }
438 }
439
440 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
441 }
442
Michael Jurkac402cd92013-05-20 15:49:32 +0200443 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700444 LauncherAppState app = LauncherAppState.getInstance();
445 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
446
Winson Chung1ed747a2011-05-03 16:18:34 -0700447 // Get the list of widgets and shortcuts
448 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200449 for (Object o : widgetsAndShortcuts) {
450 if (o instanceof AppWidgetProviderInfo) {
451 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100452 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
453 continue;
454 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200455 widget.label = widget.label.trim();
456 if (widget.minWidth > 0 && widget.minHeight > 0) {
457 // Ensure that all widgets we show can be added on a workspace of this size
458 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
459 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
460 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
461 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700462 if (minSpanX <= (int) grid.numColumns &&
463 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200464 mWidgets.add(widget);
465 } else {
466 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
467 widget.minWidth + ", " + widget.minHeight + ")");
468 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700469 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200470 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
471 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700472 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800473 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200474 // just add shortcuts
475 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800476 }
477 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200478 updatePageCountsAndInvalidateData();
479 }
480
481 public void setBulkBind(boolean bulkBind) {
482 if (bulkBind) {
483 mInBulkBind = true;
484 } else {
485 mInBulkBind = false;
486 if (mNeedToUpdatePageCountsAndInvalidateData) {
487 updatePageCountsAndInvalidateData();
488 }
489 }
490 }
491
492 private void updatePageCountsAndInvalidateData() {
493 if (mInBulkBind) {
494 mNeedToUpdatePageCountsAndInvalidateData = true;
495 } else {
496 updatePageCounts();
497 invalidateOnDataChange();
498 mNeedToUpdatePageCountsAndInvalidateData = false;
499 }
Winson Chung4b576be2011-04-27 17:40:20 -0700500 }
501
502 @Override
503 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700504 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700505 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700506 mLauncher.getWorkspace().isSwitchingState()) return;
507
Winson Chung4b576be2011-04-27 17:40:20 -0700508 if (v instanceof PagedViewIcon) {
509 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200510 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800511
512 // Lock the drawable state to pressed until we return to Launcher
513 if (mPressedIcon != null) {
514 mPressedIcon.lockDrawableState();
515 }
Winson Chungc7450e32012-04-17 17:34:08 -0700516 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
Anton Hanssona2f665f2013-09-26 12:18:32 +0100517 mLauncher.getStats().recordLaunch(appInfo.intent);
Winson Chung4b576be2011-04-27 17:40:20 -0700518 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700519 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700520 if (mWidgetInstructionToast != null) {
521 mWidgetInstructionToast.cancel();
522 }
523 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
524 Toast.LENGTH_SHORT);
525 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700526
Winson Chungd2e87b32011-06-02 10:53:07 -0700527 // Create a little animation to show that the widget can move
528 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
529 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700530 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
531 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700532 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700533 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700534 tydAnim.setDuration(100);
535 bounce.play(tyuAnim).before(tydAnim);
536 bounce.setInterpolator(new AccelerateInterpolator());
537 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700538 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700539 }
540
Winson Chungc6f10b92011-11-14 11:39:07 -0800541 public boolean onKey(View v, int keyCode, KeyEvent event) {
542 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
543 }
544
Winson Chung785d2eb2011-04-14 16:08:02 -0700545 /*
546 * PagedViewWithDraggableItems implementation
547 */
548 @Override
549 protected void determineDraggingStart(android.view.MotionEvent ev) {
550 // Disable dragging by pulling an app down for now.
551 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700552
Winson Chung4b576be2011-04-27 17:40:20 -0700553 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700554 mLauncher.getWorkspace().onDragStartedWithItem(v);
555 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700556 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700557
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700558 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
559 Bundle options = null;
560 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
561 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700562 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
563 info.componentName, null);
564
565 float density = getResources().getDisplayMetrics().density;
566 int xPaddingDips = (int) ((padding.left + padding.right) / density);
567 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
568
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700569 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700570 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
571 mTmpRect.left - xPaddingDips);
572 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
573 mTmpRect.top - yPaddingDips);
574 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
575 mTmpRect.right - xPaddingDips);
576 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
577 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700578 }
579 return options;
580 }
581
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700582 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700584 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
585
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700587 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 return;
589 }
590
Adam Cohen21a170b2012-05-30 15:17:06 -0700591 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 mBindWidgetRunnable = new Runnable() {
593 @Override
594 public void run() {
595 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700596 // Options will be null for platforms with JB or lower, so this serves as an
597 // SDK level check.
598 if (options == null) {
599 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
600 mWidgetLoadingId, info.componentName)) {
601 mWidgetCleanupState = WIDGET_BOUND;
602 }
603 } else {
604 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
605 mWidgetLoadingId, info.componentName, options)) {
606 mWidgetCleanupState = WIDGET_BOUND;
607 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800609 }
610 };
611 post(mBindWidgetRunnable);
612
613 mInflateWidgetRunnable = new Runnable() {
614 @Override
615 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700616 if (mWidgetCleanupState != WIDGET_BOUND) {
617 return;
618 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 AppWidgetHostView hostView = mLauncher.
620 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 info.boundWidget = hostView;
622 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800623 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700624 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
625 info.spanY, info, false);
626
627 // We want the first widget layout to be the correct size. This will be important
628 // for width size reporting to the AppWidgetManager.
629 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
630 unScaledSize[1]);
631 lp.x = lp.y = 0;
632 lp.customPosition = true;
633 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800634 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 }
636 };
637 post(mInflateWidgetRunnable);
638 }
639
640 @Override
641 public void onShortPress(View v) {
642 // We are anticipating a long press, and we use this time to load bind and instantiate
643 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700644 if (mCreateWidgetInfo != null) {
645 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
646 cleanupWidgetPreloading(false);
647 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800648 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700649 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 }
651
Adam Cohen0e56cc92012-05-11 15:57:05 -0700652 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
653 if (!widgetWasAdded) {
654 // If the widget was not added, we may need to do further cleanup.
655 PendingAddWidgetInfo info = mCreateWidgetInfo;
656 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700657
658 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700659 // We never did any preloading, so just remove pending callbacks to do so
660 removeCallbacks(mBindWidgetRunnable);
661 removeCallbacks(mInflateWidgetRunnable);
662 } else if (mWidgetCleanupState == WIDGET_BOUND) {
663 // Delete the widget id which was allocated
664 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700665 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
666 }
667
668 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700669 removeCallbacks(mInflateWidgetRunnable);
670 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700671 // Delete the widget id which was allocated
672 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700673 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
674 }
675
Adam Cohen0e56cc92012-05-11 15:57:05 -0700676 // The widget was inflated and added to the DragLayer -- remove it.
677 AppWidgetHostView widget = info.boundWidget;
678 mLauncher.getDragLayer().removeView(widget);
679 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 }
681 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
682 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700683 mCreateWidgetInfo = null;
684 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 }
686
Adam Cohen7a326642012-02-22 12:03:22 -0800687 @Override
688 public void cleanUpShortPress(View v) {
689 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700690 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800691 }
692 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800693
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700694 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800695 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700696 // Get the widget preview as the drag representation
697 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700698 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700699
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700700 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
701 // we abort the drag.
702 if (image.getDrawable() == null) {
703 mDraggingWidget = false;
704 return false;
705 }
706
Winson Chung4b576be2011-04-27 17:40:20 -0700707 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800708 Bitmap preview;
709 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800710 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100711 Point previewPadding = null;
712
Winson Chung1ed747a2011-05-03 16:18:34 -0700713 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700714 // This can happen in some weird cases involving multi-touch. We can't start dragging
715 // the widget if this is null, so we break out.
716 if (mCreateWidgetInfo == null) {
717 return false;
718 }
719
Adam Cohen1b36dc32012-02-13 19:27:37 -0800720 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
721 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700722 int spanX = createItemInfo.spanX;
723 int spanY = createItemInfo.spanY;
724 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
725 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700726
Winson Chung72d59842012-02-22 13:51:36 -0800727 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
728 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700729 int maxWidth, maxHeight;
730 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
731 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800732
Michael Jurka05713af2013-01-23 12:39:24 +0100733 int[] previewSizeBeforeScale = new int[1];
734
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100735 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100736 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
737 maxWidth, maxHeight, null, previewSizeBeforeScale);
738
739 // Compare the size of the drag preview to the preview in the AppsCustomize tray
740 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100741 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100742 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
743
744 // The bitmap in the AppsCustomize tray is always the the same size, so there
745 // might be extra pixels around the preview itself - this accounts for that
746 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
747 int padding =
748 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
749 previewPadding = new Point(padding, 0);
750 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700751 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700752 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
753 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
754 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
755 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
756
Winson Chung1120e032011-11-22 16:11:31 -0800757 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700758 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100759 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700760 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700761 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700762 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700763 createItemInfo.spanX = createItemInfo.spanY = 1;
764 }
Winson Chung4b576be2011-04-27 17:40:20 -0700765
Michael Jurka8c3339b2012-06-14 16:18:21 -0700766 // Don't clip alpha values for the drag outline if we're using the default widget preview
767 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
768 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700769
Winson Chung1120e032011-11-22 16:11:31 -0800770 // Save the preview for the outline generation, then dim the preview
771 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
772 false);
Winson Chung1120e032011-11-22 16:11:31 -0800773
Winson Chung4b576be2011-04-27 17:40:20 -0700774 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700775 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700776 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800777 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100778 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800779 outline.recycle();
780 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700781 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700782 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800783
Winson Chung4b576be2011-04-27 17:40:20 -0700784 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700786 if (!super.beginDragging(v)) return false;
787
788 if (v instanceof PagedViewIcon) {
789 beginDraggingApplication(v);
790 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700791 if (!beginDraggingWidget(v)) {
792 return false;
793 }
Winson Chung4b576be2011-04-27 17:40:20 -0700794 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800795
796 // We delay entering spring-loaded mode slightly to make sure the UI
797 // thready is free of any work.
798 postDelayed(new Runnable() {
799 @Override
800 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800801 // We don't enter spring-loaded mode if the drag has been cancelled
802 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800803 // Reset the alpha on the dragged icon before we drag
804 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800805
Adam Cohen1b36dc32012-02-13 19:27:37 -0800806 // Go into spring loaded mode (must happen before we startDrag())
807 mLauncher.enterSpringLoadedDragMode();
808 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 }
Winson Chung72d59842012-02-22 13:51:36 -0800810 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800811
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 return true;
813 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800814
Winson Chunga48487a2012-03-20 16:19:37 -0700815 /**
816 * Clean up after dragging.
817 *
818 * @param target where the item was dragged to (can be null if the item was flung)
819 */
820 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700821 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700822 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700823 // Exit spring loaded mode if we have not successfully dropped or have not handled the
824 // drop in Workspace
Adam Cohene97a3b32013-10-23 16:11:50 -0700825 mLauncher.getWorkspace().removeExtraEmptyScreen(true, new Runnable() {
826 @Override
827 public void run() {
828 mLauncher.exitSpringLoadedDragMode();
829 mLauncher.unlockScreenOrientation(false);
830 }
831 });
832 } else {
833 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700834 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 }
836
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700838 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700839 if (getChildCount() > 0) {
840 return getChildAt(0);
841 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700842 return null;
843 }
844
845 @Override
846 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700847 mInTransition = true;
848 if (toWorkspace) {
849 cancelAllTasks();
850 }
851 }
852
853 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700854 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700855 }
856
857 @Override
858 public void onLauncherTransitionStep(Launcher l, float t) {
859 }
860
861 @Override
862 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
863 mInTransition = false;
864 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700865 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700866 }
867 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700868 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
869 r.run();
870 }
871 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700872 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700873 }
874
875 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700876 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
877 boolean success) {
878 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
879 if (isFlingToDelete) return;
880
881 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700882
883 // Display an error message if the drag failed due to there not being enough space on the
884 // target layout we were dropping on.
885 if (!success) {
886 boolean showOutOfSpaceMessage = false;
887 if (target instanceof Workspace) {
888 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
889 Workspace workspace = (Workspace) target;
890 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700891 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700892 if (layout != null) {
893 layout.calculateSpans(itemInfo);
894 showOutOfSpaceMessage =
895 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
896 }
897 }
Winson Chungfc79c802011-05-02 13:35:34 -0700898 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700899 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700900 }
Adam Cohen7a326642012-02-22 12:03:22 -0800901
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800902 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700903 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700904 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800905 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 }
907
Winson Chunga48487a2012-03-20 16:19:37 -0700908 @Override
909 public void onFlingToDeleteCompleted() {
910 // We just dismiss the drag when we fling, so cleanup here
911 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700912 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700913 mDraggingWidget = false;
914 }
915
916 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800917 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700918 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800919 }
920
Winson Chung7f0acdd2011-09-19 18:34:19 -0700921 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000922 public boolean supportsAppInfoDropTarget() {
923 return true;
924 }
925
926 @Override
927 public boolean supportsDeleteDropTarget() {
928 return false;
929 }
930
931 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800932 public float getIntrinsicIconScaleFactor() {
933 LauncherAppState app = LauncherAppState.getInstance();
934 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
935 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
936 }
937
938 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700939 protected void onDetachedFromWindow() {
940 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700941 cancelAllTasks();
942 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700943
Michael Jurkae326f182011-11-21 14:05:46 -0800944 public void clearAllWidgetPages() {
945 cancelAllTasks();
946 int count = getChildCount();
947 for (int i = 0; i < count; i++) {
948 View v = getPageAt(i);
949 if (v instanceof PagedViewGridLayout) {
950 ((PagedViewGridLayout) v).removeAllViewsOnPage();
951 mDirtyPageContent.set(i, true);
952 }
953 }
954 }
955
Adam Cohen0cd3b642011-10-14 14:58:00 -0700956 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700957 // Clean up all the async tasks
958 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
959 while (iter.hasNext()) {
960 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
961 task.cancel(false);
962 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700963 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700964
965 // We've already preallocated the views for the data to load into, so clear them as well
966 View v = getPageAt(task.page);
967 if (v instanceof PagedViewGridLayout) {
968 ((PagedViewGridLayout) v).removeAllViewsOnPage();
969 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700970 }
Winson Chung83687b12012-04-25 16:01:01 -0700971 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700972 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700973 }
974
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200976 // Widgets appear to be cleared every time you leave, always force invalidate for them
977 if (mContentType != type || type == ContentType.Widgets) {
978 int page = (mContentType != type) ? 0 : getCurrentPage();
979 mContentType = type;
980 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700981 }
Winson Chungc58497e2013-09-03 17:48:37 -0700982 }
983
984 public ContentType getContentType() {
985 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700986 }
987
Adam Cohen0cd3b642011-10-14 14:58:00 -0700988 protected void snapToPage(int whichPage, int delta, int duration) {
989 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800990
991 // Update the thread priorities given the direction lookahead
992 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
993 while (iter.hasNext()) {
994 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700995 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800996 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
997 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
998 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
999 } else {
1000 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
1001 }
1002 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001003 }
1004
Winson Chung785d2eb2011-04-14 16:08:02 -07001005 /*
1006 * Apps PagedView implementation
1007 */
Winson Chung63257c12011-05-05 17:06:13 -07001008 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1009 int childCount = layout.getChildCount();
1010 for (int i = 0; i < childCount; ++i) {
1011 layout.getChildAt(i).setVisibility(visibility);
1012 }
1013 }
Winson Chungc58497e2013-09-03 17:48:37 -07001014 private void setupPage(AppsCustomizeCellLayout layout) {
1015 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -07001016
Winson Chung63257c12011-05-05 17:06:13 -07001017 // Note: We force a measure here to get around the fact that when we do layout calculations
1018 // immediately after syncing, we don't have a proper width. That said, we already know the
1019 // expected page width, so we can actually optimize by hiding all the TextView-based
1020 // children that are expensive to measure, and let that happen naturally later.
1021 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -07001022 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1023 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001024 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 layout.measure(widthSpec, heightSpec);
Winson Chung67ca7e42013-10-31 16:53:19 -07001026 layout.setPadding(mAllAppsPadding.left, mAllAppsPadding.top, mAllAppsPadding.right,
1027 mAllAppsPadding.bottom);
Winson Chung63257c12011-05-05 17:06:13 -07001028 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001030
Winson Chungf314b0e2011-08-16 11:54:27 -07001031 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001033 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 int numCells = mCellCountX * mCellCountY;
1035 int startIndex = page * numCells;
1036 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -07001037 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001038
Winson Chung785d2eb2011-04-14 16:08:02 -07001039 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001040 ArrayList<Object> items = new ArrayList<Object>();
1041 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001042 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001043 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001044 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1045 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001046 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001047 icon.setOnClickListener(this);
1048 icon.setOnLongClickListener(this);
1049 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001050 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001051
1052 int index = i - startIndex;
1053 int x = index % mCellCountX;
1054 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001055 if (isRtl) {
1056 x = mCellCountX - x - 1;
1057 }
Winson Chungc58497e2013-09-03 17:48:37 -07001058 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001059
1060 items.add(info);
1061 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001062 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001063
Michael Jurka47639b92013-01-14 12:42:27 +01001064 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001065 }
Winson Chungb44b5242011-06-13 11:32:14 -07001066
1067 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001068 * A helper to return the priority for loading of the specified widget page.
1069 */
1070 private int getWidgetPageLoadPriority(int page) {
1071 // If we are snapping to another page, use that index as the target page index
1072 int toPage = mCurrentPage;
1073 if (mNextPage > -1) {
1074 toPage = mNextPage;
1075 }
1076
1077 // We use the distance from the target page as an initial guess of priority, but if there
1078 // are no pages of higher priority than the page specified, then bump up the priority of
1079 // the specified page.
1080 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1081 int minPageDiff = Integer.MAX_VALUE;
1082 while (iter.hasNext()) {
1083 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001084 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001085 }
1086
1087 int rawPageDiff = Math.abs(page - toPage);
1088 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1089 }
1090 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001091 * Return the appropriate thread priority for loading for a given page (we give the current
1092 * page much higher priority)
1093 */
1094 private int getThreadPriorityForPage(int page) {
1095 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001096 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001097 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001098 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001099 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001100 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001101 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001102 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001103 }
1104 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001105 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001106 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001107 return Math.max(0, pageDiff * sPageSleepDelay);
1108 }
Winson Chungb44b5242011-06-13 11:32:14 -07001109 /**
1110 * Creates and executes a new AsyncTask to load a page of widget previews.
1111 */
1112 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001113 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001114
Winson Chungb44b5242011-06-13 11:32:14 -07001115 // Prune all tasks that are no longer needed
1116 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1117 while (iter.hasNext()) {
1118 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001119 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001120 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1121 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001122 task.cancel(false);
1123 iter.remove();
1124 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001125 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001126 }
1127 }
1128
Winson Chungf314b0e2011-08-16 11:54:27 -07001129 // 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 -07001130 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001131 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001132 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001133 @Override
1134 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001135 try {
Winson Chung09945932011-09-20 14:22:40 -07001136 try {
1137 Thread.sleep(sleepMs);
1138 } catch (Exception e) {}
1139 loadWidgetPreviewsInBackground(task, data);
1140 } finally {
1141 if (task.isCancelled()) {
1142 data.cleanup(true);
1143 }
1144 }
Winson Chungb44b5242011-06-13 11:32:14 -07001145 }
1146 },
1147 new AsyncTaskCallback() {
1148 @Override
1149 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001150 mRunningTasks.remove(task);
1151 if (task.isCancelled()) return;
1152 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001153 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001154 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001155 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001156
1157 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001158 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001159 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001160 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001161 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1162 mRunningTasks.add(t);
1163 }
Winson Chungb44b5242011-06-13 11:32:14 -07001164
Winson Chung785d2eb2011-04-14 16:08:02 -07001165 /*
1166 * Widgets PagedView implementation
1167 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001168 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001169 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001170 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001171 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1172 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001174 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001175 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001176
Michael Jurka038f9d82011-11-03 13:50:45 -07001177 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001178 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001179
Winson Chungd2945262011-06-24 15:22:14 -07001180 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 final ArrayList<Object> items = new ArrayList<Object>();
Winson Chungc58497e2013-09-03 17:48:37 -07001182 int contentWidth = mContentWidth;
Michael Jurka038f9d82011-11-03 13:50:45 -07001183 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001184 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungc58497e2013-09-03 17:48:37 -07001185 int contentHeight = mContentHeight;
Michael Jurka038f9d82011-11-03 13:50:45 -07001186 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001187 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001188
Winson Chunge4a647f2011-09-30 14:41:25 -07001189 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001190 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001191 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1192 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001193 }
1194
Winson Chunge4a647f2011-09-30 14:41:25 -07001195 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001196 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001197 layout.setColumnCount(layout.getCellCountX());
1198 for (int i = 0; i < items.size(); ++i) {
1199 Object rawInfo = items.get(i);
1200 PendingAddItemInfo createItemInfo = null;
1201 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1202 R.layout.apps_customize_widget, layout, false);
1203 if (rawInfo instanceof AppWidgetProviderInfo) {
1204 // Fill in the widget information
1205 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1206 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001207
1208 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001209 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001210 createItemInfo.spanX = spanXY[0];
1211 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001212 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001213 createItemInfo.minSpanX = minSpanXY[0];
1214 createItemInfo.minSpanY = minSpanXY[1];
1215
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001216 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001217 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001218 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001219 } else if (rawInfo instanceof ResolveInfo) {
1220 // Fill in the shortcuts information
1221 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001222 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001223 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1224 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1225 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001226 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001227 widget.setTag(createItemInfo);
1228 }
1229 widget.setOnClickListener(this);
1230 widget.setOnLongClickListener(this);
1231 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001232 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001233
1234 // Layout each widget
1235 int ix = i % mWidgetCountX;
1236 int iy = i / mWidgetCountX;
1237 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001238 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001239 GridLayout.spec(ix, GridLayout.TOP));
1240 lp.width = cellWidth;
1241 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001242 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001243 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1244 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1245 layout.addView(widget, lp);
1246 }
1247
Michael Jurka038f9d82011-11-03 13:50:45 -07001248 // wait until a call on onLayout to start loading, because
1249 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1250 // TODO: can we do a measure/layout immediately?
1251 layout.setOnLayoutListener(new Runnable() {
1252 public void run() {
1253 // Load the widget previews
1254 int maxPreviewWidth = cellWidth;
1255 int maxPreviewHeight = cellHeight;
1256 if (layout.getChildCount() > 0) {
1257 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1258 int[] maxSize = w.getPreviewSize();
1259 maxPreviewWidth = maxSize[0];
1260 maxPreviewHeight = maxSize[1];
1261 }
Michael Jurka05713af2013-01-23 12:39:24 +01001262
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001263 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001264 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001265 if (immediate) {
1266 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001267 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001268 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001269 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001270 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001271 if (mInTransition) {
1272 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1273 } else {
1274 prepareLoadWidgetPreviewsTask(page, items,
1275 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1276 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001277 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001278 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001279 }
1280 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001281 }
1282 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1283 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001284 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1285 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001286 if (task != null) {
1287 // Ensure that this task starts running at the correct priority
1288 task.syncThreadPriority();
1289 }
1290
1291 // Load each of the widget/shortcut previews
1292 ArrayList<Object> items = data.items;
1293 ArrayList<Bitmap> images = data.generatedImages;
1294 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001295 for (int i = 0; i < count; ++i) {
1296 if (task != null) {
1297 // Ensure we haven't been cancelled yet
1298 if (task.isCancelled()) break;
1299 // Before work on each item, ensure that this task is running at the correct
1300 // priority
1301 task.syncThreadPriority();
1302 }
1303
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001304 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001305 }
Winson Chungb44b5242011-06-13 11:32:14 -07001306 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001307
Winson Chung7bb37522013-10-28 11:07:57 -07001308 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1309 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001310 mDeferredSyncWidgetPageItems.add(data);
1311 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001313 try {
1314 int page = data.page;
1315 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001316
Michael Jurka39e5d172012-03-12 18:36:12 -07001317 ArrayList<Object> items = data.items;
1318 int count = items.size();
1319 for (int i = 0; i < count; ++i) {
1320 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1321 if (widget != null) {
1322 Bitmap preview = data.generatedImages.get(i);
1323 widget.applyPreview(new FastBitmapDrawable(preview), i);
1324 }
1325 }
Winson Chung68e4c642011-11-10 15:48:25 -08001326
Michael Jurka47639b92013-01-14 12:42:27 +01001327 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001328
1329 // Update all thread priorities
1330 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1331 while (iter.hasNext()) {
1332 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1333 int pageIndex = task.page;
1334 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1335 }
1336 } finally {
1337 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001338 }
Winson Chungb44b5242011-06-13 11:32:14 -07001339 }
Winson Chung46af2e82011-05-09 16:00:53 -07001340
Winson Chung785d2eb2011-04-14 16:08:02 -07001341 @Override
1342 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001343 disablePagedViewAnimations();
1344
Winson Chung785d2eb2011-04-14 16:08:02 -07001345 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001346 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001347
Adam Cohen0cd3b642011-10-14 14:58:00 -07001348 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001349 if (mContentType == ContentType.Applications) {
1350 for (int i = 0; i < mNumAppsPages; ++i) {
1351 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1352 setupPage(layout);
1353 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1354 LayoutParams.MATCH_PARENT));
1355 }
1356 } else if (mContentType == ContentType.Widgets) {
1357 for (int j = 0; j < mNumWidgetPages; ++j) {
1358 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1359 mWidgetCountY);
1360 setupPage(layout);
1361 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1362 LayoutParams.MATCH_PARENT));
1363 }
1364 } else {
1365 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001366 }
1367
Winson Chungc58497e2013-09-03 17:48:37 -07001368 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001369 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001370
Winson Chung785d2eb2011-04-14 16:08:02 -07001371 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001372 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001373 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001374 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001375 } else {
1376 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001377 }
1378 }
1379
Adam Cohen22f823d2011-09-01 17:22:18 -07001380 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1381 // it is in the z-order. This is important to insure touch events are handled correctly.
1382 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001383 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001384 }
1385
Adam Cohenae4f1552011-10-20 00:15:42 -07001386 @Override
1387 protected int indexToPage(int index) {
1388 return getChildCount() - index - 1;
1389 }
1390
Adam Cohen22f823d2011-09-01 17:22:18 -07001391 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1392 @Override
1393 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001394 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001395 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001396
1397 for (int i = 0; i < getChildCount(); i++) {
1398 View v = getPageAt(i);
1399 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001400 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001401
Winson Chung52aee602013-01-30 12:01:02 -08001402 float interpolatedProgress;
1403 float translationX;
1404 float maxScrollProgress = Math.max(0, scrollProgress);
1405 float minScrollProgress = Math.min(0, scrollProgress);
1406
1407 if (isRtl) {
1408 translationX = maxScrollProgress * v.getMeasuredWidth();
1409 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1410 } else {
1411 translationX = minScrollProgress * v.getMeasuredWidth();
1412 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1413 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001414 float scale = (1 - interpolatedProgress) +
1415 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001416
Adam Cohen2591f6a2011-10-25 14:36:40 -07001417 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001418 if (isRtl && (scrollProgress > 0)) {
1419 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1420 } else if (!isRtl && (scrollProgress < 0)) {
1421 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001422 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001423 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001424 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1425 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001426
1427 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1428 int pageWidth = v.getMeasuredWidth();
1429 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001430
1431 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001432 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1433 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1434 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1435
1436 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001437 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001438 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001439 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1440 scale = 1.0f;
1441 alpha = 1.0f;
1442 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001443 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001444 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001445 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001446 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001447 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1448 scale = 1.0f;
1449 alpha = 1.0f;
1450 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001451 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001452 } else {
1453 v.setPivotY(pageHeight / 2.0f);
1454 v.setPivotX(pageWidth / 2.0f);
1455 v.setRotationY(0f);
1456 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001457 }
1458
1459 v.setTranslationX(translationX);
1460 v.setScaleX(scale);
1461 v.setScaleY(scale);
1462 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001463
1464 // If the view has 0 alpha, we set it to be invisible so as to prevent
1465 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001466 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001467 v.setVisibility(INVISIBLE);
1468 } else if (v.getVisibility() != VISIBLE) {
1469 v.setVisibility(VISIBLE);
1470 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001471 }
1472 }
Michael Jurka47639b92013-01-14 12:42:27 +01001473
1474 enableHwLayersOnVisiblePages();
1475 }
1476
1477 private void enableHwLayersOnVisiblePages() {
1478 final int screenCount = getChildCount();
1479
1480 getVisiblePages(mTempVisiblePagesRange);
1481 int leftScreen = mTempVisiblePagesRange[0];
1482 int rightScreen = mTempVisiblePagesRange[1];
1483 int forceDrawScreen = -1;
1484 if (leftScreen == rightScreen) {
1485 // make sure we're caching at least two pages always
1486 if (rightScreen < screenCount - 1) {
1487 rightScreen++;
1488 forceDrawScreen = rightScreen;
1489 } else if (leftScreen > 0) {
1490 leftScreen--;
1491 forceDrawScreen = leftScreen;
1492 }
1493 } else {
1494 forceDrawScreen = leftScreen + 1;
1495 }
1496
1497 for (int i = 0; i < screenCount; i++) {
1498 final View layout = (View) getPageAt(i);
1499 if (!(leftScreen <= i && i <= rightScreen &&
1500 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1501 layout.setLayerType(LAYER_TYPE_NONE, null);
1502 }
1503 }
1504
Michael Jurka47639b92013-01-14 12:42:27 +01001505 for (int i = 0; i < screenCount; i++) {
1506 final View layout = (View) getPageAt(i);
1507
1508 if (leftScreen <= i && i <= rightScreen &&
1509 (i == forceDrawScreen || shouldDrawChild(layout))) {
1510 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1511 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1512 }
1513 }
1514 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001515 }
1516
1517 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001518 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001519 }
1520
Winson Chung785d2eb2011-04-14 16:08:02 -07001521 /**
1522 * Used by the parent to get the content width to set the tab bar to
1523 * @return
1524 */
1525 public int getPageContentWidth() {
1526 return mContentWidth;
1527 }
1528
Winson Chungb26f3d62011-06-02 10:49:29 -07001529 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001530 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001531 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001532 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001533 // We reset the save index when we change pages so that it will be recalculated on next
1534 // rotation
1535 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001536 }
1537
Winson Chung785d2eb2011-04-14 16:08:02 -07001538 /*
1539 * AllAppsView implementation
1540 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001541 public void setup(Launcher launcher, DragController dragController) {
1542 mLauncher = launcher;
1543 mDragController = dragController;
1544 }
Winson Chung9802ac92012-06-08 16:01:58 -07001545
1546 /**
1547 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1548 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1549 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1550 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1551 */
1552 private void invalidateOnDataChange() {
1553 if (!isDataReady()) {
1554 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1555 // request a layout to trigger the page data when ready.
1556 requestLayout();
1557 } else {
1558 cancelAllTasks();
1559 invalidatePageData();
1560 }
1561 }
1562
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001563 public void setApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001564 if (!DISABLE_ALL_APPS) {
1565 mApps = list;
1566 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1567 updatePageCountsAndInvalidateData();
1568 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001570 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001571 // We add it in place, in alphabetical order
1572 int count = list.size();
1573 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001574 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001575 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001576 if (index < 0) {
1577 mApps.add(-(index + 1), info);
1578 }
1579 }
1580 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001581 public void addApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001582 if (!DISABLE_ALL_APPS) {
1583 addAppsWithoutInvalidate(list);
1584 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001585 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001587 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001588 ComponentName removeComponent = item.intent.getComponent();
1589 int length = list.size();
1590 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001591 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001592 if (info.intent.getComponent().equals(removeComponent)) {
1593 return i;
1594 }
1595 }
1596 return -1;
1597 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001598 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 // loop through all the apps and remove apps that have the same component
1600 int length = list.size();
1601 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001602 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001603 int removeIndex = findAppByComponent(mApps, info);
1604 if (removeIndex > -1) {
1605 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001606 }
1607 }
1608 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001609 public void removeApps(ArrayList<AppInfo> appInfos) {
Winson Chung64359a52013-07-08 17:17:08 -07001610 if (!DISABLE_ALL_APPS) {
1611 removeAppsWithoutInvalidate(appInfos);
1612 updatePageCountsAndInvalidateData();
1613 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001615 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001616 // We remove and re-add the updated applications list because it's properties may have
1617 // changed (ie. the title), and this will ensure that the items will be in their proper
1618 // place in the list.
Adam Cohen947dc542013-06-06 22:43:33 -07001619 if (!DISABLE_ALL_APPS) {
1620 removeAppsWithoutInvalidate(list);
1621 addAppsWithoutInvalidate(list);
1622 updatePageCountsAndInvalidateData();
1623 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001625
Winson Chung785d2eb2011-04-14 16:08:02 -07001626 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001627 // If we have reset, then we should not continue to restore the previous state
1628 mSaveInstanceStateItemIndex = -1;
1629
Adam Cohenb64d36e2011-10-17 21:48:02 -07001630 AppsCustomizeTabHost tabHost = getTabHost();
1631 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001632 if (tag != null) {
1633 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1634 tabHost.setCurrentTabFromContent(ContentType.Applications);
1635 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001636 }
Winson Chung649668f2012-01-10 13:07:16 -08001637
Adam Cohenb64d36e2011-10-17 21:48:02 -07001638 if (mCurrentPage != 0) {
1639 invalidatePageData(0);
1640 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001641 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001642
1643 private AppsCustomizeTabHost getTabHost() {
1644 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1645 }
1646
Winson Chung785d2eb2011-04-14 16:08:02 -07001647 public void dumpState() {
1648 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001649 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001650 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001651 }
Adam Cohen4e844012011-11-09 13:48:04 -08001652
Winson Chung785d2eb2011-04-14 16:08:02 -07001653 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001654 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001656 for (Object i: list) {
1657 if (i instanceof AppWidgetProviderInfo) {
1658 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1659 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1660 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1661 + " initialLayout=" + info.initialLayout
1662 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1663 } else if (i instanceof ResolveInfo) {
1664 ResolveInfo info = (ResolveInfo) i;
1665 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1666 + info.icon);
1667 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001668 }
1669 }
Adam Cohen4e844012011-11-09 13:48:04 -08001670
Winson Chung785d2eb2011-04-14 16:08:02 -07001671 public void surrender() {
1672 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1673 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001674
1675 // Stop all background tasks
1676 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001677 }
Winson Chung007c6982011-06-14 13:27:53 -07001678
Winson Chunge4e50662012-01-23 14:45:13 -08001679 @Override
1680 public void iconPressed(PagedViewIcon icon) {
1681 // Reset the previously pressed icon and store a reference to the pressed icon so that
1682 // we can reset it on return to Launcher (in Launcher.onResume())
1683 if (mPressedIcon != null) {
1684 mPressedIcon.resetDrawableState();
1685 }
1686 mPressedIcon = icon;
1687 }
1688
1689 public void resetDrawableState() {
1690 if (mPressedIcon != null) {
1691 mPressedIcon.resetDrawableState();
1692 mPressedIcon = null;
1693 }
1694 }
Winson Chung68e4c642011-11-10 15:48:25 -08001695
Winson Chungb44b5242011-06-13 11:32:14 -07001696 /*
1697 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1698 * widget previews in the background with the AsyncTasks.
1699 */
Winson Chung68e4c642011-11-10 15:48:25 -08001700 final static int sLookBehindPageCount = 2;
1701 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001702 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001703 final int count = getChildCount();
1704 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1705 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1706 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001707 }
1708 protected int getAssociatedUpperPageBound(int page) {
1709 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001710 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1711 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1712 count - 1);
1713 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001714 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001715
Winson Chung6a0f57d2011-06-29 20:10:49 -07001716 protected String getCurrentPageDescription() {
1717 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1718 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001719 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001720
1721 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001722 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001723 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001724 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001725 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001726 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001727 } else {
1728 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001729 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001730
Michael Jurka8b805b12012-04-18 14:23:14 -07001731 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001732 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001733}