blob: 49b12b1b18fa59169c5c527d0edd699c1b350c89 [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 Chungb44b5242011-06-13 11:32:14 -0700202 // Previews & outlines
203 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800204 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700205
Adam Cohened66b2b2012-01-23 17:28:51 -0800206 private Runnable mInflateWidgetRunnable = null;
207 private Runnable mBindWidgetRunnable = null;
208 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700209 static final int WIDGET_PRELOAD_PENDING = 0;
210 static final int WIDGET_BOUND = 1;
211 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800212 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
213 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800214 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800215 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800216
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700217 private Toast mWidgetInstructionToast;
218
Michael Jurka39e5d172012-03-12 18:36:12 -0700219 // Deferral of loading widget previews during launcher transitions
220 private boolean mInTransition;
221 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
222 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700223 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
224 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700225
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700226 private Rect mTmpRect = new Rect();
227
Michael Jurkadac85912012-05-18 15:04:49 -0700228 // Used for drawing shortcut previews
229 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
230 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
231 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
232
233 // Used for drawing widget previews
234 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
235 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
236 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
237 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
238
Michael Jurka05713af2013-01-23 12:39:24 +0100239 WidgetPreviewLoader mWidgetPreviewLoader;
240
Michael Jurkac402cd92013-05-20 15:49:32 +0200241 private boolean mInBulkBind;
242 private boolean mNeedToUpdatePageCountsAndInvalidateData;
243
Winson Chung785d2eb2011-04-14 16:08:02 -0700244 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
245 super(context, attrs);
246 mLayoutInflater = LayoutInflater.from(context);
247 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200248 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700249 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400250 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700251 mCanvas = new Canvas();
252 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253
254 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800255 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700256 LauncherAppState app = LauncherAppState.getInstance();
257 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
258 mWidgetWidthGap = mWidgetHeightGap = grid.edgeMarginPx;
Winson Chung4b576be2011-04-27 17:40:20 -0700259 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
260 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
262 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700263 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700265
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 // The padding on the non-matched dimension for the default widget preview icons
267 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700268 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700269
270 // Unless otherwise specified this view is important for accessibility.
271 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
272 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
273 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700274 }
275
276 @Override
277 protected void init() {
278 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700279 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280
281 Context context = getContext();
282 Resources r = context.getResources();
283 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
284 }
285
Winson Chungc58497e2013-09-03 17:48:37 -0700286 public void onFinishInflate() {
287 super.onFinishInflate();
288
289 LauncherAppState app = LauncherAppState.getInstance();
290 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
291 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
292 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
293 }
294
Winson Chung67ca7e42013-10-31 16:53:19 -0700295 void setAllAppsPadding(Rect r) {
296 mAllAppsPadding.set(r);
297 }
298 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
299 mPageLayoutPaddingBottom = pageIndicatorHeight;
300 }
301
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100302 WidgetPreviewLoader getWidgetPreviewLoader() {
303 if (mWidgetPreviewLoader == null) {
304 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
305 }
306 return mWidgetPreviewLoader;
307 }
308
Winson Chung5afbf7b2011-07-25 11:53:08 -0700309 /** Returns the item index of the center item on this page so that we can restore to this
310 * item index when we rotate. */
311 private int getMiddleComponentIndexOnCurrentPage() {
312 int i = -1;
313 if (getPageCount() > 0) {
314 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700315 if (mContentType == ContentType.Applications) {
316 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
317 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700318 int numItemsPerPage = mCellCountX * mCellCountY;
319 int childCount = childrenLayout.getChildCount();
320 if (childCount > 0) {
321 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700322 }
Winson Chungc58497e2013-09-03 17:48:37 -0700323 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700325 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700326 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
327 int childCount = layout.getChildCount();
328 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700329 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700330 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700331 }
Winson Chungc58497e2013-09-03 17:48:37 -0700332 } else {
333 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700334 }
335 }
336 return i;
337 }
338
339 /** Get the index of the item to restore to if we need to restore the current page. */
340 int getSaveInstanceStateIndex() {
341 if (mSaveInstanceStateItemIndex == -1) {
342 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
343 }
344 return mSaveInstanceStateItemIndex;
345 }
346
347 /** Returns the page in the current orientation which is expected to contain the specified
348 * item index. */
349 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700350 if (index < 0) return 0;
351
352 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700353 int numItemsPerPage = mCellCountX * mCellCountY;
354 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700355 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700356 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700357 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700358 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700359 }
360
Winson Chung5afbf7b2011-07-25 11:53:08 -0700361 /** Restores the page for an item at the specified index */
362 void restorePageForIndex(int index) {
363 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700364 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700365 }
366
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700367 private void updatePageCounts() {
368 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
369 (float) (mWidgetCountX * mWidgetCountY));
370 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
371 }
372
Winson Chungf0ea4d32011-06-06 14:27:16 -0700373 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 // Now that the data is ready, we can calculate the content width, the number of cells to
375 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700376 LauncherAppState app = LauncherAppState.getInstance();
377 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
379 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungc58497e2013-09-03 17:48:37 -0700380 mCellCountX = (int) grid.allAppsNumCols;
381 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700382 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700383
Winson Chungdb1138b2011-06-30 14:39:35 -0700384 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700385 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
386 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
387 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
388 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700389 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700390
Michael Jurkae326f182011-11-21 14:05:46 -0800391 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
392 final boolean hostIsTransitioning = host.isTransitioning();
393
Adam Cohen0cd3b642011-10-14 14:58:00 -0700394 // Restore the page
395 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800396 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700397
Winson Chung3f4e1422011-11-17 14:58:51 -0800398 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
399 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
400 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800401 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800402 post(new Runnable() {
403 @Override
404 public void run() {
405 showAllAppsCling();
406 }
407 });
408 }
409 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700410
Winson Chung3f4e1422011-11-17 14:58:51 -0800411 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700412 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800413 mHasShownAllAppsCling = true;
414 // Calculate the position for the cling punch through
415 int[] offset = new int[2];
416 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
417 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
418 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800419 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800420 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
421 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800422 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800423 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700424 }
425
426 @Override
427 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
428 int width = MeasureSpec.getSize(widthMeasureSpec);
429 int height = MeasureSpec.getSize(heightMeasureSpec);
430 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800431 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700432 setDataIsReady();
433 setMeasuredDimension(width, height);
434 onDataReady(width, height);
435 }
436 }
437
438 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
439 }
440
Michael Jurkac402cd92013-05-20 15:49:32 +0200441 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700442 LauncherAppState app = LauncherAppState.getInstance();
443 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
444
Winson Chung1ed747a2011-05-03 16:18:34 -0700445 // Get the list of widgets and shortcuts
446 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200447 for (Object o : widgetsAndShortcuts) {
448 if (o instanceof AppWidgetProviderInfo) {
449 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100450 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
451 continue;
452 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200453 widget.label = widget.label.trim();
454 if (widget.minWidth > 0 && widget.minHeight > 0) {
455 // Ensure that all widgets we show can be added on a workspace of this size
456 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
457 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
458 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
459 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700460 if (minSpanX <= (int) grid.numColumns &&
461 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200462 mWidgets.add(widget);
463 } else {
464 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
465 widget.minWidth + ", " + widget.minHeight + ")");
466 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700467 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200468 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
469 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700470 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800471 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200472 // just add shortcuts
473 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800474 }
475 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200476 updatePageCountsAndInvalidateData();
477 }
478
479 public void setBulkBind(boolean bulkBind) {
480 if (bulkBind) {
481 mInBulkBind = true;
482 } else {
483 mInBulkBind = false;
484 if (mNeedToUpdatePageCountsAndInvalidateData) {
485 updatePageCountsAndInvalidateData();
486 }
487 }
488 }
489
490 private void updatePageCountsAndInvalidateData() {
491 if (mInBulkBind) {
492 mNeedToUpdatePageCountsAndInvalidateData = true;
493 } else {
494 updatePageCounts();
495 invalidateOnDataChange();
496 mNeedToUpdatePageCountsAndInvalidateData = false;
497 }
Winson Chung4b576be2011-04-27 17:40:20 -0700498 }
499
500 @Override
501 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700502 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700503 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700504 mLauncher.getWorkspace().isSwitchingState()) return;
505
Winson Chung4b576be2011-04-27 17:40:20 -0700506 if (v instanceof PagedViewIcon) {
507 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200508 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800509
510 // Lock the drawable state to pressed until we return to Launcher
511 if (mPressedIcon != null) {
512 mPressedIcon.lockDrawableState();
513 }
Winson Chungc7450e32012-04-17 17:34:08 -0700514 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
Anton Hanssona2f665f2013-09-26 12:18:32 +0100515 mLauncher.getStats().recordLaunch(appInfo.intent);
Winson Chung4b576be2011-04-27 17:40:20 -0700516 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700517 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700518 if (mWidgetInstructionToast != null) {
519 mWidgetInstructionToast.cancel();
520 }
521 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
522 Toast.LENGTH_SHORT);
523 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700524
Winson Chungd2e87b32011-06-02 10:53:07 -0700525 // Create a little animation to show that the widget can move
526 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
527 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700528 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
529 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700530 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700531 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700532 tydAnim.setDuration(100);
533 bounce.play(tyuAnim).before(tydAnim);
534 bounce.setInterpolator(new AccelerateInterpolator());
535 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700536 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700537 }
538
Winson Chungc6f10b92011-11-14 11:39:07 -0800539 public boolean onKey(View v, int keyCode, KeyEvent event) {
540 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
541 }
542
Winson Chung785d2eb2011-04-14 16:08:02 -0700543 /*
544 * PagedViewWithDraggableItems implementation
545 */
546 @Override
547 protected void determineDraggingStart(android.view.MotionEvent ev) {
548 // Disable dragging by pulling an app down for now.
549 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700550
Winson Chung4b576be2011-04-27 17:40:20 -0700551 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700552 mLauncher.getWorkspace().onDragStartedWithItem(v);
553 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700554 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700555
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700556 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
557 Bundle options = null;
558 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
559 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700560 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
561 info.componentName, null);
562
563 float density = getResources().getDisplayMetrics().density;
564 int xPaddingDips = (int) ((padding.left + padding.right) / density);
565 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
566
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700567 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700568 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
569 mTmpRect.left - xPaddingDips);
570 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
571 mTmpRect.top - yPaddingDips);
572 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
573 mTmpRect.right - xPaddingDips);
574 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
575 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700576 }
577 return options;
578 }
579
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700580 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700582 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
583
Adam Cohened66b2b2012-01-23 17:28:51 -0800584 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700585 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 return;
587 }
588
Adam Cohen21a170b2012-05-30 15:17:06 -0700589 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 mBindWidgetRunnable = new Runnable() {
591 @Override
592 public void run() {
593 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700594 // Options will be null for platforms with JB or lower, so this serves as an
595 // SDK level check.
596 if (options == null) {
597 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
598 mWidgetLoadingId, info.componentName)) {
599 mWidgetCleanupState = WIDGET_BOUND;
600 }
601 } else {
602 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
603 mWidgetLoadingId, info.componentName, options)) {
604 mWidgetCleanupState = WIDGET_BOUND;
605 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 }
608 };
609 post(mBindWidgetRunnable);
610
611 mInflateWidgetRunnable = new Runnable() {
612 @Override
613 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700614 if (mWidgetCleanupState != WIDGET_BOUND) {
615 return;
616 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700617 AppWidgetHostView hostView = mLauncher.
618 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800619 info.boundWidget = hostView;
620 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800621 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700622 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
623 info.spanY, info, false);
624
625 // We want the first widget layout to be the correct size. This will be important
626 // for width size reporting to the AppWidgetManager.
627 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
628 unScaledSize[1]);
629 lp.x = lp.y = 0;
630 lp.customPosition = true;
631 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800632 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 }
634 };
635 post(mInflateWidgetRunnable);
636 }
637
638 @Override
639 public void onShortPress(View v) {
640 // We are anticipating a long press, and we use this time to load bind and instantiate
641 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700642 if (mCreateWidgetInfo != null) {
643 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
644 cleanupWidgetPreloading(false);
645 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800646 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700647 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 }
649
Adam Cohen0e56cc92012-05-11 15:57:05 -0700650 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
651 if (!widgetWasAdded) {
652 // If the widget was not added, we may need to do further cleanup.
653 PendingAddWidgetInfo info = mCreateWidgetInfo;
654 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700655
656 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700657 // We never did any preloading, so just remove pending callbacks to do so
658 removeCallbacks(mBindWidgetRunnable);
659 removeCallbacks(mInflateWidgetRunnable);
660 } else if (mWidgetCleanupState == WIDGET_BOUND) {
661 // Delete the widget id which was allocated
662 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700663 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
664 }
665
666 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700667 removeCallbacks(mInflateWidgetRunnable);
668 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700669 // Delete the widget id which was allocated
670 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700671 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
672 }
673
Adam Cohen0e56cc92012-05-11 15:57:05 -0700674 // The widget was inflated and added to the DragLayer -- remove it.
675 AppWidgetHostView widget = info.boundWidget;
676 mLauncher.getDragLayer().removeView(widget);
677 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 }
679 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
680 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700681 mCreateWidgetInfo = null;
682 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 }
684
Adam Cohen7a326642012-02-22 12:03:22 -0800685 @Override
686 public void cleanUpShortPress(View v) {
687 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700688 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800689 }
690 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800691
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700692 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800693 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700694 // Get the widget preview as the drag representation
695 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700696 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700697
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700698 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
699 // we abort the drag.
700 if (image.getDrawable() == null) {
701 mDraggingWidget = false;
702 return false;
703 }
704
Winson Chung4b576be2011-04-27 17:40:20 -0700705 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800706 Bitmap preview;
707 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800708 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100709 Point previewPadding = null;
710
Winson Chung1ed747a2011-05-03 16:18:34 -0700711 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700712 // This can happen in some weird cases involving multi-touch. We can't start dragging
713 // the widget if this is null, so we break out.
714 if (mCreateWidgetInfo == null) {
715 return false;
716 }
717
Adam Cohen1b36dc32012-02-13 19:27:37 -0800718 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
719 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700720 int spanX = createItemInfo.spanX;
721 int spanY = createItemInfo.spanY;
722 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
723 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700724
Winson Chung72d59842012-02-22 13:51:36 -0800725 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
726 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700727 int maxWidth, maxHeight;
728 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
729 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800730
Michael Jurka05713af2013-01-23 12:39:24 +0100731 int[] previewSizeBeforeScale = new int[1];
732
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100733 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100734 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
735 maxWidth, maxHeight, null, previewSizeBeforeScale);
736
737 // Compare the size of the drag preview to the preview in the AppsCustomize tray
738 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100739 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100740 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
741
742 // The bitmap in the AppsCustomize tray is always the the same size, so there
743 // might be extra pixels around the preview itself - this accounts for that
744 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
745 int padding =
746 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
747 previewPadding = new Point(padding, 0);
748 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700749 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700750 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
751 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
752 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
753 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
754
Winson Chung1120e032011-11-22 16:11:31 -0800755 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700756 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100757 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700758 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700759 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700760 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700761 createItemInfo.spanX = createItemInfo.spanY = 1;
762 }
Winson Chung4b576be2011-04-27 17:40:20 -0700763
Michael Jurka8c3339b2012-06-14 16:18:21 -0700764 // Don't clip alpha values for the drag outline if we're using the default widget preview
765 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
766 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700767
Winson Chung1120e032011-11-22 16:11:31 -0800768 // Save the preview for the outline generation, then dim the preview
769 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
770 false);
Winson Chung1120e032011-11-22 16:11:31 -0800771
Winson Chung4b576be2011-04-27 17:40:20 -0700772 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700773 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700774 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800775 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100776 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800777 outline.recycle();
778 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700779 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700780 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800781
Winson Chung4b576be2011-04-27 17:40:20 -0700782 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800783 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700784 if (!super.beginDragging(v)) return false;
785
786 if (v instanceof PagedViewIcon) {
787 beginDraggingApplication(v);
788 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700789 if (!beginDraggingWidget(v)) {
790 return false;
791 }
Winson Chung4b576be2011-04-27 17:40:20 -0700792 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800793
794 // We delay entering spring-loaded mode slightly to make sure the UI
795 // thready is free of any work.
796 postDelayed(new Runnable() {
797 @Override
798 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800799 // We don't enter spring-loaded mode if the drag has been cancelled
800 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800801 // Reset the alpha on the dragged icon before we drag
802 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800803
Adam Cohen1b36dc32012-02-13 19:27:37 -0800804 // Go into spring loaded mode (must happen before we startDrag())
805 mLauncher.enterSpringLoadedDragMode();
806 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 }
Winson Chung72d59842012-02-22 13:51:36 -0800808 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800809
Winson Chung785d2eb2011-04-14 16:08:02 -0700810 return true;
811 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800812
Winson Chunga48487a2012-03-20 16:19:37 -0700813 /**
814 * Clean up after dragging.
815 *
816 * @param target where the item was dragged to (can be null if the item was flung)
817 */
818 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700819 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700820 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700821 // Exit spring loaded mode if we have not successfully dropped or have not handled the
822 // drop in Workspace
Adam Cohene97a3b32013-10-23 16:11:50 -0700823 mLauncher.getWorkspace().removeExtraEmptyScreen(true, new Runnable() {
824 @Override
825 public void run() {
826 mLauncher.exitSpringLoadedDragMode();
827 mLauncher.unlockScreenOrientation(false);
828 }
829 });
830 } else {
831 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700832 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 }
834
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700836 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700837 if (getChildCount() > 0) {
838 return getChildAt(0);
839 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700840 return null;
841 }
842
843 @Override
844 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700845 mInTransition = true;
846 if (toWorkspace) {
847 cancelAllTasks();
848 }
849 }
850
851 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700852 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700853 }
854
855 @Override
856 public void onLauncherTransitionStep(Launcher l, float t) {
857 }
858
859 @Override
860 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
861 mInTransition = false;
862 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700863 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700864 }
865 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700866 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
867 r.run();
868 }
869 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700870 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700871 }
872
873 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700874 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
875 boolean success) {
876 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
877 if (isFlingToDelete) return;
878
879 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700880
881 // Display an error message if the drag failed due to there not being enough space on the
882 // target layout we were dropping on.
883 if (!success) {
884 boolean showOutOfSpaceMessage = false;
885 if (target instanceof Workspace) {
886 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
887 Workspace workspace = (Workspace) target;
888 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700889 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700890 if (layout != null) {
891 layout.calculateSpans(itemInfo);
892 showOutOfSpaceMessage =
893 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
894 }
895 }
Winson Chungfc79c802011-05-02 13:35:34 -0700896 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700897 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700898 }
Adam Cohen7a326642012-02-22 12:03:22 -0800899
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800900 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700901 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700902 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800903 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700904 }
905
Winson Chunga48487a2012-03-20 16:19:37 -0700906 @Override
907 public void onFlingToDeleteCompleted() {
908 // We just dismiss the drag when we fling, so cleanup here
909 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700910 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700911 mDraggingWidget = false;
912 }
913
914 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800915 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700916 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800917 }
918
Winson Chung7f0acdd2011-09-19 18:34:19 -0700919 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000920 public boolean supportsAppInfoDropTarget() {
921 return true;
922 }
923
924 @Override
925 public boolean supportsDeleteDropTarget() {
926 return false;
927 }
928
929 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800930 public float getIntrinsicIconScaleFactor() {
931 LauncherAppState app = LauncherAppState.getInstance();
932 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
933 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
934 }
935
936 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700937 protected void onDetachedFromWindow() {
938 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700939 cancelAllTasks();
940 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700941
Michael Jurkae326f182011-11-21 14:05:46 -0800942 public void clearAllWidgetPages() {
943 cancelAllTasks();
944 int count = getChildCount();
945 for (int i = 0; i < count; i++) {
946 View v = getPageAt(i);
947 if (v instanceof PagedViewGridLayout) {
948 ((PagedViewGridLayout) v).removeAllViewsOnPage();
949 mDirtyPageContent.set(i, true);
950 }
951 }
952 }
953
Adam Cohen0cd3b642011-10-14 14:58:00 -0700954 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700955 // Clean up all the async tasks
956 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
957 while (iter.hasNext()) {
958 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
959 task.cancel(false);
960 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700961 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700962
963 // We've already preallocated the views for the data to load into, so clear them as well
964 View v = getPageAt(task.page);
965 if (v instanceof PagedViewGridLayout) {
966 ((PagedViewGridLayout) v).removeAllViewsOnPage();
967 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700968 }
Winson Chung83687b12012-04-25 16:01:01 -0700969 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700970 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700971 }
972
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200974 // Widgets appear to be cleared every time you leave, always force invalidate for them
975 if (mContentType != type || type == ContentType.Widgets) {
976 int page = (mContentType != type) ? 0 : getCurrentPage();
977 mContentType = type;
978 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700979 }
Winson Chungc58497e2013-09-03 17:48:37 -0700980 }
981
982 public ContentType getContentType() {
983 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700984 }
985
Adam Cohen0cd3b642011-10-14 14:58:00 -0700986 protected void snapToPage(int whichPage, int delta, int duration) {
987 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800988
989 // Update the thread priorities given the direction lookahead
990 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
991 while (iter.hasNext()) {
992 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700993 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800994 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
995 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
996 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
997 } else {
998 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
999 }
1000 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001001 }
1002
Winson Chung785d2eb2011-04-14 16:08:02 -07001003 /*
1004 * Apps PagedView implementation
1005 */
Winson Chung63257c12011-05-05 17:06:13 -07001006 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1007 int childCount = layout.getChildCount();
1008 for (int i = 0; i < childCount; ++i) {
1009 layout.getChildAt(i).setVisibility(visibility);
1010 }
1011 }
Winson Chungc58497e2013-09-03 17:48:37 -07001012 private void setupPage(AppsCustomizeCellLayout layout) {
1013 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -07001014
Winson Chung63257c12011-05-05 17:06:13 -07001015 // Note: We force a measure here to get around the fact that when we do layout calculations
1016 // immediately after syncing, we don't have a proper width. That said, we already know the
1017 // expected page width, so we can actually optimize by hiding all the TextView-based
1018 // children that are expensive to measure, and let that happen naturally later.
1019 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -07001020 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1021 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001022 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 layout.measure(widthSpec, heightSpec);
Winson Chung67ca7e42013-10-31 16:53:19 -07001024 layout.setPadding(mAllAppsPadding.left, mAllAppsPadding.top, mAllAppsPadding.right,
1025 mAllAppsPadding.bottom);
Winson Chung63257c12011-05-05 17:06:13 -07001026 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001028
Winson Chungf314b0e2011-08-16 11:54:27 -07001029 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001031 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 int numCells = mCellCountX * mCellCountY;
1033 int startIndex = page * numCells;
1034 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -07001035 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001036
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001038 ArrayList<Object> items = new ArrayList<Object>();
1039 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001041 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001042 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1043 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001044 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001045 icon.setOnClickListener(this);
1046 icon.setOnLongClickListener(this);
1047 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001048 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001049
1050 int index = i - startIndex;
1051 int x = index % mCellCountX;
1052 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001053 if (isRtl) {
1054 x = mCellCountX - x - 1;
1055 }
Winson Chungc58497e2013-09-03 17:48:37 -07001056 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001057
1058 items.add(info);
1059 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001061
Michael Jurka47639b92013-01-14 12:42:27 +01001062 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001063 }
Winson Chungb44b5242011-06-13 11:32:14 -07001064
1065 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001066 * A helper to return the priority for loading of the specified widget page.
1067 */
1068 private int getWidgetPageLoadPriority(int page) {
1069 // If we are snapping to another page, use that index as the target page index
1070 int toPage = mCurrentPage;
1071 if (mNextPage > -1) {
1072 toPage = mNextPage;
1073 }
1074
1075 // We use the distance from the target page as an initial guess of priority, but if there
1076 // are no pages of higher priority than the page specified, then bump up the priority of
1077 // the specified page.
1078 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1079 int minPageDiff = Integer.MAX_VALUE;
1080 while (iter.hasNext()) {
1081 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001082 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001083 }
1084
1085 int rawPageDiff = Math.abs(page - toPage);
1086 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1087 }
1088 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001089 * Return the appropriate thread priority for loading for a given page (we give the current
1090 * page much higher priority)
1091 */
1092 private int getThreadPriorityForPage(int page) {
1093 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001094 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001095 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001096 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001097 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001098 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001099 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001100 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001101 }
1102 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001103 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001104 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001105 return Math.max(0, pageDiff * sPageSleepDelay);
1106 }
Winson Chungb44b5242011-06-13 11:32:14 -07001107 /**
1108 * Creates and executes a new AsyncTask to load a page of widget previews.
1109 */
1110 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001111 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001112
Winson Chungb44b5242011-06-13 11:32:14 -07001113 // Prune all tasks that are no longer needed
1114 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1115 while (iter.hasNext()) {
1116 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001117 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001118 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1119 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001120 task.cancel(false);
1121 iter.remove();
1122 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001123 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001124 }
1125 }
1126
Winson Chungf314b0e2011-08-16 11:54:27 -07001127 // 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 -07001128 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001129 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001130 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001131 @Override
1132 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001133 try {
Winson Chung09945932011-09-20 14:22:40 -07001134 try {
1135 Thread.sleep(sleepMs);
1136 } catch (Exception e) {}
1137 loadWidgetPreviewsInBackground(task, data);
1138 } finally {
1139 if (task.isCancelled()) {
1140 data.cleanup(true);
1141 }
1142 }
Winson Chungb44b5242011-06-13 11:32:14 -07001143 }
1144 },
1145 new AsyncTaskCallback() {
1146 @Override
1147 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001148 mRunningTasks.remove(task);
1149 if (task.isCancelled()) return;
1150 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001151 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001152 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001153 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001154
1155 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001156 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001157 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001158 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001159 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1160 mRunningTasks.add(t);
1161 }
Winson Chungb44b5242011-06-13 11:32:14 -07001162
Winson Chung785d2eb2011-04-14 16:08:02 -07001163 /*
1164 * Widgets PagedView implementation
1165 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001166 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001167 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001168 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001169 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1170 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001171 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001172 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001174
Michael Jurka038f9d82011-11-03 13:50:45 -07001175 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001176 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001177
Winson Chungd2945262011-06-24 15:22:14 -07001178 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001179 final ArrayList<Object> items = new ArrayList<Object>();
Winson Chungc58497e2013-09-03 17:48:37 -07001180 int contentWidth = mContentWidth;
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001182 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungc58497e2013-09-03 17:48:37 -07001183 int contentHeight = mContentHeight;
Michael Jurka038f9d82011-11-03 13:50:45 -07001184 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001185 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001186
Winson Chunge4a647f2011-09-30 14:41:25 -07001187 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001188 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001189 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1190 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001191 }
1192
Winson Chunge4a647f2011-09-30 14:41:25 -07001193 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001194 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001195 layout.setColumnCount(layout.getCellCountX());
1196 for (int i = 0; i < items.size(); ++i) {
1197 Object rawInfo = items.get(i);
1198 PendingAddItemInfo createItemInfo = null;
1199 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1200 R.layout.apps_customize_widget, layout, false);
1201 if (rawInfo instanceof AppWidgetProviderInfo) {
1202 // Fill in the widget information
1203 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1204 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001205
1206 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001207 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001208 createItemInfo.spanX = spanXY[0];
1209 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001210 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001211 createItemInfo.minSpanX = minSpanXY[0];
1212 createItemInfo.minSpanY = minSpanXY[1];
1213
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001214 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001215 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001216 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001217 } else if (rawInfo instanceof ResolveInfo) {
1218 // Fill in the shortcuts information
1219 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001220 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001221 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1222 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1223 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001224 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001225 widget.setTag(createItemInfo);
1226 }
1227 widget.setOnClickListener(this);
1228 widget.setOnLongClickListener(this);
1229 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001230 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001231
1232 // Layout each widget
1233 int ix = i % mWidgetCountX;
1234 int iy = i / mWidgetCountX;
1235 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001236 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001237 GridLayout.spec(ix, GridLayout.TOP));
1238 lp.width = cellWidth;
1239 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001240 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001241 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1242 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1243 layout.addView(widget, lp);
1244 }
1245
Michael Jurka038f9d82011-11-03 13:50:45 -07001246 // wait until a call on onLayout to start loading, because
1247 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1248 // TODO: can we do a measure/layout immediately?
1249 layout.setOnLayoutListener(new Runnable() {
1250 public void run() {
1251 // Load the widget previews
1252 int maxPreviewWidth = cellWidth;
1253 int maxPreviewHeight = cellHeight;
1254 if (layout.getChildCount() > 0) {
1255 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1256 int[] maxSize = w.getPreviewSize();
1257 maxPreviewWidth = maxSize[0];
1258 maxPreviewHeight = maxSize[1];
1259 }
Michael Jurka05713af2013-01-23 12:39:24 +01001260
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001261 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001262 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001263 if (immediate) {
1264 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001265 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001266 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001267 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001268 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001269 if (mInTransition) {
1270 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1271 } else {
1272 prepareLoadWidgetPreviewsTask(page, items,
1273 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1274 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001275 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001276 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001277 }
1278 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001279 }
1280 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1281 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001282 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1283 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001284 if (task != null) {
1285 // Ensure that this task starts running at the correct priority
1286 task.syncThreadPriority();
1287 }
1288
1289 // Load each of the widget/shortcut previews
1290 ArrayList<Object> items = data.items;
1291 ArrayList<Bitmap> images = data.generatedImages;
1292 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001293 for (int i = 0; i < count; ++i) {
1294 if (task != null) {
1295 // Ensure we haven't been cancelled yet
1296 if (task.isCancelled()) break;
1297 // Before work on each item, ensure that this task is running at the correct
1298 // priority
1299 task.syncThreadPriority();
1300 }
1301
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001302 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001303 }
Winson Chungb44b5242011-06-13 11:32:14 -07001304 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001305
Winson Chung7bb37522013-10-28 11:07:57 -07001306 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1307 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001308 mDeferredSyncWidgetPageItems.add(data);
1309 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001311 try {
1312 int page = data.page;
1313 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001314
Michael Jurka39e5d172012-03-12 18:36:12 -07001315 ArrayList<Object> items = data.items;
1316 int count = items.size();
1317 for (int i = 0; i < count; ++i) {
1318 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1319 if (widget != null) {
1320 Bitmap preview = data.generatedImages.get(i);
1321 widget.applyPreview(new FastBitmapDrawable(preview), i);
1322 }
1323 }
Winson Chung68e4c642011-11-10 15:48:25 -08001324
Michael Jurka47639b92013-01-14 12:42:27 +01001325 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001326
1327 // Update all thread priorities
1328 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1329 while (iter.hasNext()) {
1330 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1331 int pageIndex = task.page;
1332 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1333 }
1334 } finally {
1335 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001336 }
Winson Chungb44b5242011-06-13 11:32:14 -07001337 }
Winson Chung46af2e82011-05-09 16:00:53 -07001338
Winson Chung785d2eb2011-04-14 16:08:02 -07001339 @Override
1340 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001341 disablePagedViewAnimations();
1342
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001344 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001345
Adam Cohen0cd3b642011-10-14 14:58:00 -07001346 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001347 if (mContentType == ContentType.Applications) {
1348 for (int i = 0; i < mNumAppsPages; ++i) {
1349 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1350 setupPage(layout);
1351 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1352 LayoutParams.MATCH_PARENT));
1353 }
1354 } else if (mContentType == ContentType.Widgets) {
1355 for (int j = 0; j < mNumWidgetPages; ++j) {
1356 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1357 mWidgetCountY);
1358 setupPage(layout);
1359 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1360 LayoutParams.MATCH_PARENT));
1361 }
1362 } else {
1363 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001364 }
1365
Winson Chungc58497e2013-09-03 17:48:37 -07001366 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001367 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001368
Winson Chung785d2eb2011-04-14 16:08:02 -07001369 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001370 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001371 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001372 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001373 } else {
1374 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001375 }
1376 }
1377
Adam Cohen22f823d2011-09-01 17:22:18 -07001378 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1379 // it is in the z-order. This is important to insure touch events are handled correctly.
1380 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001381 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001382 }
1383
Adam Cohenae4f1552011-10-20 00:15:42 -07001384 @Override
1385 protected int indexToPage(int index) {
1386 return getChildCount() - index - 1;
1387 }
1388
Adam Cohen22f823d2011-09-01 17:22:18 -07001389 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1390 @Override
1391 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001392 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001393 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001394
1395 for (int i = 0; i < getChildCount(); i++) {
1396 View v = getPageAt(i);
1397 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001398 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001399
Winson Chung52aee602013-01-30 12:01:02 -08001400 float interpolatedProgress;
1401 float translationX;
1402 float maxScrollProgress = Math.max(0, scrollProgress);
1403 float minScrollProgress = Math.min(0, scrollProgress);
1404
1405 if (isRtl) {
1406 translationX = maxScrollProgress * v.getMeasuredWidth();
1407 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1408 } else {
1409 translationX = minScrollProgress * v.getMeasuredWidth();
1410 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1411 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001412 float scale = (1 - interpolatedProgress) +
1413 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001414
Adam Cohen2591f6a2011-10-25 14:36:40 -07001415 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001416 if (isRtl && (scrollProgress > 0)) {
1417 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1418 } else if (!isRtl && (scrollProgress < 0)) {
1419 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001420 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001421 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001422 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1423 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001424
1425 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1426 int pageWidth = v.getMeasuredWidth();
1427 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001428
1429 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001430 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1431 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1432 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1433
1434 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001435 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001436 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001437 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1438 scale = 1.0f;
1439 alpha = 1.0f;
1440 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001441 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001442 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001443 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001444 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001445 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1446 scale = 1.0f;
1447 alpha = 1.0f;
1448 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001449 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001450 } else {
1451 v.setPivotY(pageHeight / 2.0f);
1452 v.setPivotX(pageWidth / 2.0f);
1453 v.setRotationY(0f);
1454 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001455 }
1456
1457 v.setTranslationX(translationX);
1458 v.setScaleX(scale);
1459 v.setScaleY(scale);
1460 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001461
1462 // If the view has 0 alpha, we set it to be invisible so as to prevent
1463 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001464 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001465 v.setVisibility(INVISIBLE);
1466 } else if (v.getVisibility() != VISIBLE) {
1467 v.setVisibility(VISIBLE);
1468 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001469 }
1470 }
Michael Jurka47639b92013-01-14 12:42:27 +01001471
1472 enableHwLayersOnVisiblePages();
1473 }
1474
1475 private void enableHwLayersOnVisiblePages() {
1476 final int screenCount = getChildCount();
1477
1478 getVisiblePages(mTempVisiblePagesRange);
1479 int leftScreen = mTempVisiblePagesRange[0];
1480 int rightScreen = mTempVisiblePagesRange[1];
1481 int forceDrawScreen = -1;
1482 if (leftScreen == rightScreen) {
1483 // make sure we're caching at least two pages always
1484 if (rightScreen < screenCount - 1) {
1485 rightScreen++;
1486 forceDrawScreen = rightScreen;
1487 } else if (leftScreen > 0) {
1488 leftScreen--;
1489 forceDrawScreen = leftScreen;
1490 }
1491 } else {
1492 forceDrawScreen = leftScreen + 1;
1493 }
1494
1495 for (int i = 0; i < screenCount; i++) {
1496 final View layout = (View) getPageAt(i);
1497 if (!(leftScreen <= i && i <= rightScreen &&
1498 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1499 layout.setLayerType(LAYER_TYPE_NONE, null);
1500 }
1501 }
1502
Michael Jurka47639b92013-01-14 12:42:27 +01001503 for (int i = 0; i < screenCount; i++) {
1504 final View layout = (View) getPageAt(i);
1505
1506 if (leftScreen <= i && i <= rightScreen &&
1507 (i == forceDrawScreen || shouldDrawChild(layout))) {
1508 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1509 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1510 }
1511 }
1512 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001513 }
1514
1515 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001516 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001517 }
1518
Winson Chung785d2eb2011-04-14 16:08:02 -07001519 /**
1520 * Used by the parent to get the content width to set the tab bar to
1521 * @return
1522 */
1523 public int getPageContentWidth() {
1524 return mContentWidth;
1525 }
1526
Winson Chungb26f3d62011-06-02 10:49:29 -07001527 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001528 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001529 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001530 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001531 // We reset the save index when we change pages so that it will be recalculated on next
1532 // rotation
1533 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001534 }
1535
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 /*
1537 * AllAppsView implementation
1538 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001539 public void setup(Launcher launcher, DragController dragController) {
1540 mLauncher = launcher;
1541 mDragController = dragController;
1542 }
Winson Chung9802ac92012-06-08 16:01:58 -07001543
1544 /**
1545 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1546 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1547 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1548 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1549 */
1550 private void invalidateOnDataChange() {
1551 if (!isDataReady()) {
1552 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1553 // request a layout to trigger the page data when ready.
1554 requestLayout();
1555 } else {
1556 cancelAllTasks();
1557 invalidatePageData();
1558 }
1559 }
1560
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001561 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001562 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001563 mApps = list;
1564 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1565 updatePageCountsAndInvalidateData();
1566 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001568 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 // We add it in place, in alphabetical order
1570 int count = list.size();
1571 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001572 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001573 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001574 if (index < 0) {
1575 mApps.add(-(index + 1), info);
1576 }
1577 }
1578 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001579 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001580 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001581 addAppsWithoutInvalidate(list);
1582 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001583 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001585 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 ComponentName removeComponent = item.intent.getComponent();
1587 int length = list.size();
1588 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001589 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 if (info.intent.getComponent().equals(removeComponent)) {
1591 return i;
1592 }
1593 }
1594 return -1;
1595 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001596 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001597 // loop through all the apps and remove apps that have the same component
1598 int length = list.size();
1599 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001600 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 int removeIndex = findAppByComponent(mApps, info);
1602 if (removeIndex > -1) {
1603 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001604 }
1605 }
1606 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001607 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001608 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001609 removeAppsWithoutInvalidate(appInfos);
1610 updatePageCountsAndInvalidateData();
1611 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001612 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001613 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 // We remove and re-add the updated applications list because it's properties may have
1615 // changed (ie. the title), and this will ensure that the items will be in their proper
1616 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001617 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001618 removeAppsWithoutInvalidate(list);
1619 addAppsWithoutInvalidate(list);
1620 updatePageCountsAndInvalidateData();
1621 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001622 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001623
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001625 // If we have reset, then we should not continue to restore the previous state
1626 mSaveInstanceStateItemIndex = -1;
1627
Adam Cohenb64d36e2011-10-17 21:48:02 -07001628 AppsCustomizeTabHost tabHost = getTabHost();
1629 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001630 if (tag != null) {
1631 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1632 tabHost.setCurrentTabFromContent(ContentType.Applications);
1633 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001634 }
Winson Chung649668f2012-01-10 13:07:16 -08001635
Adam Cohenb64d36e2011-10-17 21:48:02 -07001636 if (mCurrentPage != 0) {
1637 invalidatePageData(0);
1638 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001639 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001640
1641 private AppsCustomizeTabHost getTabHost() {
1642 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1643 }
1644
Winson Chung785d2eb2011-04-14 16:08:02 -07001645 public void dumpState() {
1646 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001647 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001648 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001649 }
Adam Cohen4e844012011-11-09 13:48:04 -08001650
Winson Chung785d2eb2011-04-14 16:08:02 -07001651 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001652 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001653 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001654 for (Object i: list) {
1655 if (i instanceof AppWidgetProviderInfo) {
1656 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1657 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1658 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1659 + " initialLayout=" + info.initialLayout
1660 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1661 } else if (i instanceof ResolveInfo) {
1662 ResolveInfo info = (ResolveInfo) i;
1663 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1664 + info.icon);
1665 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001666 }
1667 }
Adam Cohen4e844012011-11-09 13:48:04 -08001668
Winson Chung785d2eb2011-04-14 16:08:02 -07001669 public void surrender() {
1670 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1671 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001672
1673 // Stop all background tasks
1674 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001675 }
Winson Chung007c6982011-06-14 13:27:53 -07001676
Winson Chunge4e50662012-01-23 14:45:13 -08001677 @Override
1678 public void iconPressed(PagedViewIcon icon) {
1679 // Reset the previously pressed icon and store a reference to the pressed icon so that
1680 // we can reset it on return to Launcher (in Launcher.onResume())
1681 if (mPressedIcon != null) {
1682 mPressedIcon.resetDrawableState();
1683 }
1684 mPressedIcon = icon;
1685 }
1686
1687 public void resetDrawableState() {
1688 if (mPressedIcon != null) {
1689 mPressedIcon.resetDrawableState();
1690 mPressedIcon = null;
1691 }
1692 }
Winson Chung68e4c642011-11-10 15:48:25 -08001693
Winson Chungb44b5242011-06-13 11:32:14 -07001694 /*
1695 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1696 * widget previews in the background with the AsyncTasks.
1697 */
Winson Chung68e4c642011-11-10 15:48:25 -08001698 final static int sLookBehindPageCount = 2;
1699 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001700 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001701 final int count = getChildCount();
1702 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1703 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1704 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001705 }
1706 protected int getAssociatedUpperPageBound(int page) {
1707 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001708 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1709 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1710 count - 1);
1711 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001712 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001713
Winson Chung6a0f57d2011-06-29 20:10:49 -07001714 protected String getCurrentPageDescription() {
1715 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1716 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001717 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001718
1719 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001720 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001721 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001722 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001723 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001724 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001725 } else {
1726 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001727 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001728
Michael Jurka8b805b12012-04-18 14:23:14 -07001729 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001730 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001731}