blob: 787bf96d223763d443aaa62cf231c358cbe543ec [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
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static Rect sTmpRect = new Rect();
152
Winson Chung785d2eb2011-04-14 16:08:02 -0700153 /**
154 * The different content types that this paged view can show.
155 */
156 public enum ContentType {
157 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700158 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700159 }
Winson Chungc58497e2013-09-03 17:48:37 -0700160 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800170 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200173 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700174 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700175
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800177 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 private int mClingFocusedX;
179 private int mClingFocusedY;
180
Winson Chung1ed747a2011-05-03 16:18:34 -0700181 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700182 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700183 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700184
185 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700186 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700187 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700189 private int mNumAppsPages;
190 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700191 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700192
Adam Cohen22f823d2011-09-01 17:22:18 -0700193 // Relating to the scroll and overscroll effects
194 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700195 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700196 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700197 private static float TRANSITION_PIVOT = 0.65f;
198 private static float TRANSITION_MAX_ROTATION = 22;
Adam Cohen4e243a22014-08-10 18:30:55 -0700199 private static final boolean PERFORM_OVERSCROLL_ROTATION = false;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700200 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700201 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700202
Winson Chungb44b5242011-06-13 11:32:14 -0700203 // Previews & outlines
204 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800205 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700206
Adam Cohened66b2b2012-01-23 17:28:51 -0800207 private Runnable mInflateWidgetRunnable = null;
208 private Runnable mBindWidgetRunnable = null;
209 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700210 static final int WIDGET_PRELOAD_PENDING = 0;
211 static final int WIDGET_BOUND = 1;
212 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800213 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
214 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800215 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800216 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700217 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800218
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700219 private Toast mWidgetInstructionToast;
220
Michael Jurka39e5d172012-03-12 18:36:12 -0700221 // Deferral of loading widget previews during launcher transitions
222 private boolean mInTransition;
223 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
224 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700225 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
226 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700227
Michael Jurka05713af2013-01-23 12:39:24 +0100228 WidgetPreviewLoader mWidgetPreviewLoader;
229
Michael Jurkac402cd92013-05-20 15:49:32 +0200230 private boolean mInBulkBind;
231 private boolean mNeedToUpdatePageCountsAndInvalidateData;
232
Winson Chung785d2eb2011-04-14 16:08:02 -0700233 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
234 super(context, attrs);
235 mLayoutInflater = LayoutInflater.from(context);
236 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200237 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700238 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400239 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700240 mCanvas = new Canvas();
241 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700242
243 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800244 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700245 LauncherAppState app = LauncherAppState.getInstance();
246 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung4b576be2011-04-27 17:40:20 -0700247 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
248 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700249 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
250 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700251 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700252 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700253
Winson Chung1ed747a2011-05-03 16:18:34 -0700254 // The padding on the non-matched dimension for the default widget preview icons
255 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700256 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700257
258 // Unless otherwise specified this view is important for accessibility.
259 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
260 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
261 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700262 }
263
264 @Override
265 protected void init() {
266 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700267 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700268
269 Context context = getContext();
270 Resources r = context.getResources();
271 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
272 }
273
Winson Chungc58497e2013-09-03 17:48:37 -0700274 public void onFinishInflate() {
275 super.onFinishInflate();
276
277 LauncherAppState app = LauncherAppState.getInstance();
278 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
279 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
280 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
281 }
282
Winson Chung67ca7e42013-10-31 16:53:19 -0700283 void setAllAppsPadding(Rect r) {
284 mAllAppsPadding.set(r);
285 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700286
Winson Chung67ca7e42013-10-31 16:53:19 -0700287 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700288 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700289 }
290
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100291 WidgetPreviewLoader getWidgetPreviewLoader() {
292 if (mWidgetPreviewLoader == null) {
293 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
294 }
295 return mWidgetPreviewLoader;
296 }
297
Winson Chung5afbf7b2011-07-25 11:53:08 -0700298 /** Returns the item index of the center item on this page so that we can restore to this
299 * item index when we rotate. */
300 private int getMiddleComponentIndexOnCurrentPage() {
301 int i = -1;
302 if (getPageCount() > 0) {
303 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700304 if (mContentType == ContentType.Applications) {
305 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
306 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700307 int numItemsPerPage = mCellCountX * mCellCountY;
308 int childCount = childrenLayout.getChildCount();
309 if (childCount > 0) {
310 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 }
Winson Chungc58497e2013-09-03 17:48:37 -0700312 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700313 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700314 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700315 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
316 int childCount = layout.getChildCount();
317 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700318 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700319 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700320 }
Winson Chungc58497e2013-09-03 17:48:37 -0700321 } else {
322 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700323 }
324 }
325 return i;
326 }
327
328 /** Get the index of the item to restore to if we need to restore the current page. */
329 int getSaveInstanceStateIndex() {
330 if (mSaveInstanceStateItemIndex == -1) {
331 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
332 }
333 return mSaveInstanceStateItemIndex;
334 }
335
336 /** Returns the page in the current orientation which is expected to contain the specified
337 * item index. */
338 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700339 if (index < 0) return 0;
340
341 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700342 int numItemsPerPage = mCellCountX * mCellCountY;
343 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700346 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700347 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700348 }
349
Winson Chung5afbf7b2011-07-25 11:53:08 -0700350 /** Restores the page for an item at the specified index */
351 void restorePageForIndex(int index) {
352 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700354 }
355
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700356 private void updatePageCounts() {
357 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
358 (float) (mWidgetCountX * mWidgetCountY));
359 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
360 }
361
Winson Chungf0ea4d32011-06-06 14:27:16 -0700362 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700363 // Now that the data is ready, we can calculate the content width, the number of cells to
364 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700365 LauncherAppState app = LauncherAppState.getInstance();
366 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700367 mCellCountX = (int) grid.allAppsNumCols;
368 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700369 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700370
Winson Chungdb1138b2011-06-30 14:39:35 -0700371 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700372 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
373 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
374 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
375 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700376 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700377
Adam Cohen6c5891a2014-07-09 23:53:15 -0700378 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700379 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800380 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800381 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700382
Adam Cohena00673c2014-08-14 12:57:28 -0700383 protected void onLayout(boolean changed, int l, int t, int r, int b) {
384 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385
Winson Chungf0ea4d32011-06-06 14:27:16 -0700386 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800387 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700388 setDataIsReady();
Adam Cohena00673c2014-08-14 12:57:28 -0700389 onDataReady(getMeasuredWidth(), getMeasuredHeight());
Winson Chungf0ea4d32011-06-06 14:27:16 -0700390 }
391 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700392 }
393
Michael Jurkac402cd92013-05-20 15:49:32 +0200394 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700395 LauncherAppState app = LauncherAppState.getInstance();
396 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
397
Winson Chung1ed747a2011-05-03 16:18:34 -0700398 // Get the list of widgets and shortcuts
399 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200400 for (Object o : widgetsAndShortcuts) {
401 if (o instanceof AppWidgetProviderInfo) {
402 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100403 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
404 continue;
405 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200406 widget.label = widget.label.trim();
407 if (widget.minWidth > 0 && widget.minHeight > 0) {
408 // Ensure that all widgets we show can be added on a workspace of this size
409 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
410 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
411 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
412 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700413 if (minSpanX <= (int) grid.numColumns &&
414 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200415 mWidgets.add(widget);
416 } else {
417 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
418 widget.minWidth + ", " + widget.minHeight + ")");
419 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700420 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200421 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
422 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700423 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800424 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200425 // just add shortcuts
426 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800427 }
428 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200429 updatePageCountsAndInvalidateData();
430 }
431
432 public void setBulkBind(boolean bulkBind) {
433 if (bulkBind) {
434 mInBulkBind = true;
435 } else {
436 mInBulkBind = false;
437 if (mNeedToUpdatePageCountsAndInvalidateData) {
438 updatePageCountsAndInvalidateData();
439 }
440 }
441 }
442
443 private void updatePageCountsAndInvalidateData() {
444 if (mInBulkBind) {
445 mNeedToUpdatePageCountsAndInvalidateData = true;
446 } else {
447 updatePageCounts();
448 invalidateOnDataChange();
449 mNeedToUpdatePageCountsAndInvalidateData = false;
450 }
Winson Chung4b576be2011-04-27 17:40:20 -0700451 }
452
453 @Override
454 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700455 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700456 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700457 mLauncher.getWorkspace().isSwitchingState()) return;
458
Winson Chung4b576be2011-04-27 17:40:20 -0700459 if (v instanceof PagedViewIcon) {
460 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200461 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800462
463 // Lock the drawable state to pressed until we return to Launcher
464 if (mPressedIcon != null) {
465 mPressedIcon.lockDrawableState();
466 }
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -0700467 mLauncher.onClickPagedViewIcon(v, appInfo);
Winson Chung4b576be2011-04-27 17:40:20 -0700468 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700469 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700470 if (mWidgetInstructionToast != null) {
471 mWidgetInstructionToast.cancel();
472 }
473 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
474 Toast.LENGTH_SHORT);
475 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700476
Winson Chungd2e87b32011-06-02 10:53:07 -0700477 // Create a little animation to show that the widget can move
478 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
479 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700480 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
481 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700482 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700483 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700484 tydAnim.setDuration(100);
485 bounce.play(tyuAnim).before(tydAnim);
486 bounce.setInterpolator(new AccelerateInterpolator());
487 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700488 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700489 }
490
Winson Chungc6f10b92011-11-14 11:39:07 -0800491 public boolean onKey(View v, int keyCode, KeyEvent event) {
492 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
493 }
494
Winson Chung785d2eb2011-04-14 16:08:02 -0700495 /*
496 * PagedViewWithDraggableItems implementation
497 */
498 @Override
499 protected void determineDraggingStart(android.view.MotionEvent ev) {
500 // Disable dragging by pulling an app down for now.
501 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700502
Winson Chung4b576be2011-04-27 17:40:20 -0700503 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700504 mLauncher.getWorkspace().onDragStartedWithItem(v);
505 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700506 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700507
Sunny Goyalff572272014-07-23 13:58:07 -0700508 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700509 Bundle options = null;
510 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700511 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
512 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700513 info.componentName, null);
514
Sunny Goyalff572272014-07-23 13:58:07 -0700515 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700516 int xPaddingDips = (int) ((padding.left + padding.right) / density);
517 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
518
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700519 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700520 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700521 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700522 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700523 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700524 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700525 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700526 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700527 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700528 }
529 return options;
530 }
531
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700532 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700534 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
535
Adam Cohened66b2b2012-01-23 17:28:51 -0800536 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700537 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800538 return;
539 }
540
Adam Cohen21a170b2012-05-30 15:17:06 -0700541 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800542 mBindWidgetRunnable = new Runnable() {
543 @Override
544 public void run() {
545 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700546 // Options will be null for platforms with JB or lower, so this serves as an
547 // SDK level check.
548 if (options == null) {
549 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
550 mWidgetLoadingId, info.componentName)) {
551 mWidgetCleanupState = WIDGET_BOUND;
552 }
553 } else {
554 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
555 mWidgetLoadingId, info.componentName, options)) {
556 mWidgetCleanupState = WIDGET_BOUND;
557 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700558 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800559 }
560 };
561 post(mBindWidgetRunnable);
562
563 mInflateWidgetRunnable = new Runnable() {
564 @Override
565 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700566 if (mWidgetCleanupState != WIDGET_BOUND) {
567 return;
568 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 AppWidgetHostView hostView = mLauncher.
570 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800571 info.boundWidget = hostView;
572 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800573 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700574 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
575 info.spanY, info, false);
576
577 // We want the first widget layout to be the correct size. This will be important
578 // for width size reporting to the AppWidgetManager.
579 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
580 unScaledSize[1]);
581 lp.x = lp.y = 0;
582 lp.customPosition = true;
583 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800584 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800585 }
586 };
587 post(mInflateWidgetRunnable);
588 }
589
590 @Override
591 public void onShortPress(View v) {
592 // We are anticipating a long press, and we use this time to load bind and instantiate
593 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700594 if (mCreateWidgetInfo != null) {
595 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
596 cleanupWidgetPreloading(false);
597 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800598 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700599 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 }
601
Adam Cohen0e56cc92012-05-11 15:57:05 -0700602 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
603 if (!widgetWasAdded) {
604 // If the widget was not added, we may need to do further cleanup.
605 PendingAddWidgetInfo info = mCreateWidgetInfo;
606 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700607
608 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700609 // We never did any preloading, so just remove pending callbacks to do so
610 removeCallbacks(mBindWidgetRunnable);
611 removeCallbacks(mInflateWidgetRunnable);
612 } else if (mWidgetCleanupState == WIDGET_BOUND) {
613 // Delete the widget id which was allocated
614 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700615 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
616 }
617
618 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700619 removeCallbacks(mInflateWidgetRunnable);
620 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700621 // Delete the widget id which was allocated
622 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700623 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
624 }
625
Adam Cohen0e56cc92012-05-11 15:57:05 -0700626 // The widget was inflated and added to the DragLayer -- remove it.
627 AppWidgetHostView widget = info.boundWidget;
628 mLauncher.getDragLayer().removeView(widget);
629 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 }
631 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
632 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700633 mCreateWidgetInfo = null;
634 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 }
636
Adam Cohen7a326642012-02-22 12:03:22 -0800637 @Override
638 public void cleanUpShortPress(View v) {
639 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700640 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800641 }
642 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800643
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700644 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800645 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700646 // Get the widget preview as the drag representation
647 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700648 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700649
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700650 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
651 // we abort the drag.
652 if (image.getDrawable() == null) {
653 mDraggingWidget = false;
654 return false;
655 }
656
Winson Chung4b576be2011-04-27 17:40:20 -0700657 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800658 Bitmap preview;
659 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800660 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100661 Point previewPadding = null;
662
Winson Chung1ed747a2011-05-03 16:18:34 -0700663 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700664 // This can happen in some weird cases involving multi-touch. We can't start dragging
665 // the widget if this is null, so we break out.
666 if (mCreateWidgetInfo == null) {
667 return false;
668 }
669
Adam Cohen1b36dc32012-02-13 19:27:37 -0800670 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
671 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700672 int spanX = createItemInfo.spanX;
673 int spanY = createItemInfo.spanY;
674 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
675 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700676
Winson Chung72d59842012-02-22 13:51:36 -0800677 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
678 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700679 int maxWidth, maxHeight;
680 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
681 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800682
Michael Jurka05713af2013-01-23 12:39:24 +0100683 int[] previewSizeBeforeScale = new int[1];
684
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100685 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100686 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
687 maxWidth, maxHeight, null, previewSizeBeforeScale);
688
689 // Compare the size of the drag preview to the preview in the AppsCustomize tray
690 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100691 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100692 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
693
694 // The bitmap in the AppsCustomize tray is always the the same size, so there
695 // might be extra pixels around the preview itself - this accounts for that
696 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
697 int padding =
698 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
699 previewPadding = new Point(padding, 0);
700 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700701 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700702 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
703 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
704 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
705 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
706
Winson Chung1120e032011-11-22 16:11:31 -0800707 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700708 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100709 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700710 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700711 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700712 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700713 createItemInfo.spanX = createItemInfo.spanY = 1;
714 }
Winson Chung4b576be2011-04-27 17:40:20 -0700715
Michael Jurka8c3339b2012-06-14 16:18:21 -0700716 // Don't clip alpha values for the drag outline if we're using the default widget preview
717 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
718 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700719
Winson Chung1120e032011-11-22 16:11:31 -0800720 // Save the preview for the outline generation, then dim the preview
721 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
722 false);
Winson Chung1120e032011-11-22 16:11:31 -0800723
Winson Chung4b576be2011-04-27 17:40:20 -0700724 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700725 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700726 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800727 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100728 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800729 outline.recycle();
730 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700731 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700732 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800733
Winson Chung4b576be2011-04-27 17:40:20 -0700734 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700736 if (!super.beginDragging(v)) return false;
737
738 if (v instanceof PagedViewIcon) {
739 beginDraggingApplication(v);
740 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700741 if (!beginDraggingWidget(v)) {
742 return false;
743 }
Winson Chung4b576be2011-04-27 17:40:20 -0700744 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800745
746 // We delay entering spring-loaded mode slightly to make sure the UI
747 // thready is free of any work.
748 postDelayed(new Runnable() {
749 @Override
750 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800751 // We don't enter spring-loaded mode if the drag has been cancelled
752 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800753 // Reset the alpha on the dragged icon before we drag
754 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800755
Adam Cohen1b36dc32012-02-13 19:27:37 -0800756 // Go into spring loaded mode (must happen before we startDrag())
757 mLauncher.enterSpringLoadedDragMode();
758 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 }
Winson Chung72d59842012-02-22 13:51:36 -0800760 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800761
Winson Chung785d2eb2011-04-14 16:08:02 -0700762 return true;
763 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800764
Winson Chunga48487a2012-03-20 16:19:37 -0700765 /**
766 * Clean up after dragging.
767 *
768 * @param target where the item was dragged to (can be null if the item was flung)
769 */
770 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700771 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800772 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700773 // Exit spring loaded mode if we have not successfully dropped or have not handled the
774 // drop in Workspace
Adam Cohen689ff162014-05-08 17:27:56 -0700775 mLauncher.exitSpringLoadedDragMode();
776 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700777 } else {
778 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700779 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700780 }
781
Winson Chung785d2eb2011-04-14 16:08:02 -0700782 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700783 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700784 if (getChildCount() > 0) {
785 return getChildAt(0);
786 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700787 return null;
788 }
789
790 @Override
791 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700792 mInTransition = true;
793 if (toWorkspace) {
794 cancelAllTasks();
795 }
796 }
797
798 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700799 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700800 }
801
802 @Override
803 public void onLauncherTransitionStep(Launcher l, float t) {
804 }
805
806 @Override
807 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
808 mInTransition = false;
809 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700810 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700811 }
812 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700813 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
814 r.run();
815 }
816 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700817 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700818 }
819
820 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700821 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
822 boolean success) {
823 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
824 if (isFlingToDelete) return;
825
826 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700827
828 // Display an error message if the drag failed due to there not being enough space on the
829 // target layout we were dropping on.
830 if (!success) {
831 boolean showOutOfSpaceMessage = false;
832 if (target instanceof Workspace) {
833 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
834 Workspace workspace = (Workspace) target;
835 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700836 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700837 if (layout != null) {
838 layout.calculateSpans(itemInfo);
839 showOutOfSpaceMessage =
840 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
841 }
842 }
Winson Chungfc79c802011-05-02 13:35:34 -0700843 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700844 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700845 }
Adam Cohen7a326642012-02-22 12:03:22 -0800846
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800847 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700848 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700849 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800850 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700851 }
852
Winson Chunga48487a2012-03-20 16:19:37 -0700853 @Override
854 public void onFlingToDeleteCompleted() {
855 // We just dismiss the drag when we fling, so cleanup here
856 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700857 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700858 mDraggingWidget = false;
859 }
860
861 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800862 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700863 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800864 }
865
Winson Chung7f0acdd2011-09-19 18:34:19 -0700866 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000867 public boolean supportsAppInfoDropTarget() {
868 return true;
869 }
870
871 @Override
872 public boolean supportsDeleteDropTarget() {
873 return false;
874 }
875
876 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800877 public float getIntrinsicIconScaleFactor() {
878 LauncherAppState app = LauncherAppState.getInstance();
879 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
880 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
881 }
882
883 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700884 protected void onDetachedFromWindow() {
885 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700886 cancelAllTasks();
887 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700888
Michael Jurkae326f182011-11-21 14:05:46 -0800889 public void clearAllWidgetPages() {
890 cancelAllTasks();
891 int count = getChildCount();
892 for (int i = 0; i < count; i++) {
893 View v = getPageAt(i);
894 if (v instanceof PagedViewGridLayout) {
895 ((PagedViewGridLayout) v).removeAllViewsOnPage();
896 mDirtyPageContent.set(i, true);
897 }
898 }
899 }
900
Adam Cohen0cd3b642011-10-14 14:58:00 -0700901 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700902 // Clean up all the async tasks
903 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
904 while (iter.hasNext()) {
905 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
906 task.cancel(false);
907 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700908 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700909
910 // We've already preallocated the views for the data to load into, so clear them as well
911 View v = getPageAt(task.page);
912 if (v instanceof PagedViewGridLayout) {
913 ((PagedViewGridLayout) v).removeAllViewsOnPage();
914 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700915 }
Winson Chung83687b12012-04-25 16:01:01 -0700916 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700917 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700918 }
919
Winson Chung785d2eb2011-04-14 16:08:02 -0700920 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200921 // Widgets appear to be cleared every time you leave, always force invalidate for them
922 if (mContentType != type || type == ContentType.Widgets) {
923 int page = (mContentType != type) ? 0 : getCurrentPage();
924 mContentType = type;
925 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700926 }
Winson Chungc58497e2013-09-03 17:48:37 -0700927 }
928
929 public ContentType getContentType() {
930 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700931 }
932
Adam Cohen0cd3b642011-10-14 14:58:00 -0700933 protected void snapToPage(int whichPage, int delta, int duration) {
934 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800935
936 // Update the thread priorities given the direction lookahead
937 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
938 while (iter.hasNext()) {
939 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700940 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800941 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
942 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
943 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
944 } else {
945 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
946 }
947 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700948 }
949
Winson Chung785d2eb2011-04-14 16:08:02 -0700950 /*
951 * Apps PagedView implementation
952 */
Winson Chung63257c12011-05-05 17:06:13 -0700953 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
954 int childCount = layout.getChildCount();
955 for (int i = 0; i < childCount; ++i) {
956 layout.getChildAt(i).setVisibility(visibility);
957 }
958 }
Winson Chungc58497e2013-09-03 17:48:37 -0700959 private void setupPage(AppsCustomizeCellLayout layout) {
960 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700961
Winson Chung63257c12011-05-05 17:06:13 -0700962 // Note: We force a measure here to get around the fact that when we do layout calculations
963 // immediately after syncing, we don't have a proper width. That said, we already know the
964 // expected page width, so we can actually optimize by hiding all the TextView-based
965 // children that are expensive to measure, and let that happen naturally later.
966 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700967 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
968 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700970
Adam Cohen4e243a22014-08-10 18:30:55 -0700971 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700972 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700973 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700974 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700975 }
976
977 setVisibilityOnChildren(layout, View.VISIBLE);
978 }
979
980 public void setPageBackgroundsVisible(boolean visible) {
981 mPageBackgroundsVisible = visible;
982 int childCount = getChildCount();
983 for (int i = 0; i < childCount; ++i) {
984 Drawable bg = getChildAt(i).getBackground();
985 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700986 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700987 }
988 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700989 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700990
Winson Chungf314b0e2011-08-16 11:54:27 -0700991 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700992 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700993 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 int numCells = mCellCountX * mCellCountY;
995 int startIndex = page * numCells;
996 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700997 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700998
Winson Chung785d2eb2011-04-14 16:08:02 -0700999 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001000 ArrayList<Object> items = new ArrayList<Object>();
1001 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001003 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001004 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1005 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001006 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001007 icon.setOnClickListener(this);
1008 icon.setOnLongClickListener(this);
1009 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001010 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001011
1012 int index = i - startIndex;
1013 int x = index % mCellCountX;
1014 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001015 if (isRtl) {
1016 x = mCellCountX - x - 1;
1017 }
Winson Chungc58497e2013-09-03 17:48:37 -07001018 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001019
1020 items.add(info);
1021 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001022 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023
Michael Jurka47639b92013-01-14 12:42:27 +01001024 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 }
Winson Chungb44b5242011-06-13 11:32:14 -07001026
1027 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001028 * A helper to return the priority for loading of the specified widget page.
1029 */
1030 private int getWidgetPageLoadPriority(int page) {
1031 // If we are snapping to another page, use that index as the target page index
1032 int toPage = mCurrentPage;
1033 if (mNextPage > -1) {
1034 toPage = mNextPage;
1035 }
1036
1037 // We use the distance from the target page as an initial guess of priority, but if there
1038 // are no pages of higher priority than the page specified, then bump up the priority of
1039 // the specified page.
1040 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1041 int minPageDiff = Integer.MAX_VALUE;
1042 while (iter.hasNext()) {
1043 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001044 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001045 }
1046
1047 int rawPageDiff = Math.abs(page - toPage);
1048 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1049 }
1050 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001051 * Return the appropriate thread priority for loading for a given page (we give the current
1052 * page much higher priority)
1053 */
1054 private int getThreadPriorityForPage(int page) {
1055 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001056 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001057 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001058 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001059 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001060 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001061 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001062 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001063 }
1064 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001065 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001066 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001067 return Math.max(0, pageDiff * sPageSleepDelay);
1068 }
Winson Chungb44b5242011-06-13 11:32:14 -07001069 /**
1070 * Creates and executes a new AsyncTask to load a page of widget previews.
1071 */
1072 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001073 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001074
Winson Chungb44b5242011-06-13 11:32:14 -07001075 // Prune all tasks that are no longer needed
1076 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1077 while (iter.hasNext()) {
1078 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001079 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001080 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1081 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001082 task.cancel(false);
1083 iter.remove();
1084 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001085 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001086 }
1087 }
1088
Winson Chungf314b0e2011-08-16 11:54:27 -07001089 // 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 -07001090 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001091 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001092 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001093 @Override
1094 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001095 try {
Winson Chung09945932011-09-20 14:22:40 -07001096 try {
1097 Thread.sleep(sleepMs);
1098 } catch (Exception e) {}
1099 loadWidgetPreviewsInBackground(task, data);
1100 } finally {
1101 if (task.isCancelled()) {
1102 data.cleanup(true);
1103 }
1104 }
Winson Chungb44b5242011-06-13 11:32:14 -07001105 }
1106 },
1107 new AsyncTaskCallback() {
1108 @Override
1109 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001110 mRunningTasks.remove(task);
1111 if (task.isCancelled()) return;
1112 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001113 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001114 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001115 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001116
1117 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001118 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001119 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001120 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001121 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1122 mRunningTasks.add(t);
1123 }
Winson Chungb44b5242011-06-13 11:32:14 -07001124
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 /*
1126 * Widgets PagedView implementation
1127 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001128 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001129 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001130 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001131 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1132 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001133
1134 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1135 if (bg != null) {
1136 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1137 layout.setBackground(bg);
1138 }
Winson Chung63257c12011-05-05 17:06:13 -07001139 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001140 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001141
Michael Jurka038f9d82011-11-03 13:50:45 -07001142 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001143 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001144
Adam Cohen4e243a22014-08-10 18:30:55 -07001145 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1146
Winson Chungd2945262011-06-24 15:22:14 -07001147 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001148 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001149 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001150 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001151 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1152
Adam Cohen4e243a22014-08-10 18:30:55 -07001153 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001154
Winson Chunge4a647f2011-09-30 14:41:25 -07001155 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001156 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001157 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1158 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001159 }
1160
Winson Chunge4a647f2011-09-30 14:41:25 -07001161 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001162 layout.setColumnCount(layout.getCellCountX());
1163 for (int i = 0; i < items.size(); ++i) {
1164 Object rawInfo = items.get(i);
1165 PendingAddItemInfo createItemInfo = null;
1166 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1167 R.layout.apps_customize_widget, layout, false);
1168 if (rawInfo instanceof AppWidgetProviderInfo) {
1169 // Fill in the widget information
1170 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1171 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001172
1173 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001175 createItemInfo.spanX = spanXY[0];
1176 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001177 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001178 createItemInfo.minSpanX = minSpanXY[0];
1179 createItemInfo.minSpanY = minSpanXY[1];
1180
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001181 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001182 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001183 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001184 } else if (rawInfo instanceof ResolveInfo) {
1185 // Fill in the shortcuts information
1186 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001187 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001188 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1189 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1190 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001191 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001192 widget.setTag(createItemInfo);
1193 }
1194 widget.setOnClickListener(this);
1195 widget.setOnLongClickListener(this);
1196 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001197 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001198
1199 // Layout each widget
1200 int ix = i % mWidgetCountX;
1201 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001202
1203 if (ix > 0) {
1204 View border = widget.findViewById(R.id.left_border);
1205 border.setVisibility(View.VISIBLE);
1206 }
1207 if (ix < mWidgetCountX - 1) {
1208 View border = widget.findViewById(R.id.right_border);
1209 border.setVisibility(View.VISIBLE);
1210 }
1211
Winson Chunge4a647f2011-09-30 14:41:25 -07001212 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001213 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001214 GridLayout.spec(ix, GridLayout.TOP));
1215 lp.width = cellWidth;
1216 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001217 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001218 layout.addView(widget, lp);
1219 }
1220
Michael Jurka038f9d82011-11-03 13:50:45 -07001221 // wait until a call on onLayout to start loading, because
1222 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1223 // TODO: can we do a measure/layout immediately?
1224 layout.setOnLayoutListener(new Runnable() {
1225 public void run() {
1226 // Load the widget previews
1227 int maxPreviewWidth = cellWidth;
1228 int maxPreviewHeight = cellHeight;
1229 if (layout.getChildCount() > 0) {
1230 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1231 int[] maxSize = w.getPreviewSize();
1232 maxPreviewWidth = maxSize[0];
1233 maxPreviewHeight = maxSize[1];
1234 }
Michael Jurka05713af2013-01-23 12:39:24 +01001235
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001236 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001237 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001238 if (immediate) {
1239 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001240 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001241 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001242 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001243 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001244 if (mInTransition) {
1245 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1246 } else {
1247 prepareLoadWidgetPreviewsTask(page, items,
1248 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1249 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001250 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001251 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001252 }
1253 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001254 }
1255 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1256 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001257 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1258 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001259 if (task != null) {
1260 // Ensure that this task starts running at the correct priority
1261 task.syncThreadPriority();
1262 }
1263
1264 // Load each of the widget/shortcut previews
1265 ArrayList<Object> items = data.items;
1266 ArrayList<Bitmap> images = data.generatedImages;
1267 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001268 for (int i = 0; i < count; ++i) {
1269 if (task != null) {
1270 // Ensure we haven't been cancelled yet
1271 if (task.isCancelled()) break;
1272 // Before work on each item, ensure that this task is running at the correct
1273 // priority
1274 task.syncThreadPriority();
1275 }
1276
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001277 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001278 }
Winson Chungb44b5242011-06-13 11:32:14 -07001279 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001280
Winson Chung7bb37522013-10-28 11:07:57 -07001281 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1282 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001283 mDeferredSyncWidgetPageItems.add(data);
1284 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001285 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001286 try {
1287 int page = data.page;
1288 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001289
Michael Jurka39e5d172012-03-12 18:36:12 -07001290 ArrayList<Object> items = data.items;
1291 int count = items.size();
1292 for (int i = 0; i < count; ++i) {
1293 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1294 if (widget != null) {
1295 Bitmap preview = data.generatedImages.get(i);
1296 widget.applyPreview(new FastBitmapDrawable(preview), i);
1297 }
1298 }
Winson Chung68e4c642011-11-10 15:48:25 -08001299
Michael Jurka47639b92013-01-14 12:42:27 +01001300 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001301
1302 // Update all thread priorities
1303 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1304 while (iter.hasNext()) {
1305 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1306 int pageIndex = task.page;
1307 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1308 }
1309 } finally {
1310 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001311 }
Winson Chungb44b5242011-06-13 11:32:14 -07001312 }
Winson Chung46af2e82011-05-09 16:00:53 -07001313
Winson Chung785d2eb2011-04-14 16:08:02 -07001314 @Override
1315 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001316 disablePagedViewAnimations();
1317
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001319 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001320
Adam Cohen0cd3b642011-10-14 14:58:00 -07001321 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001322 if (mContentType == ContentType.Applications) {
1323 for (int i = 0; i < mNumAppsPages; ++i) {
1324 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1325 setupPage(layout);
1326 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1327 LayoutParams.MATCH_PARENT));
1328 }
1329 } else if (mContentType == ContentType.Widgets) {
1330 for (int j = 0; j < mNumWidgetPages; ++j) {
1331 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1332 mWidgetCountY);
1333 setupPage(layout);
1334 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1335 LayoutParams.MATCH_PARENT));
1336 }
1337 } else {
1338 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001339 }
1340
Winson Chungc58497e2013-09-03 17:48:37 -07001341 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001343
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001345 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001346 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001347 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001348 } else {
1349 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 }
1351 }
1352
Adam Cohen22f823d2011-09-01 17:22:18 -07001353 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1354 // it is in the z-order. This is important to insure touch events are handled correctly.
1355 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001356 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001357 }
1358
Adam Cohenae4f1552011-10-20 00:15:42 -07001359 @Override
1360 protected int indexToPage(int index) {
1361 return getChildCount() - index - 1;
1362 }
1363
Adam Cohen22f823d2011-09-01 17:22:18 -07001364 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1365 @Override
1366 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001367 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001368 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001369
1370 for (int i = 0; i < getChildCount(); i++) {
1371 View v = getPageAt(i);
1372 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001373 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001374
Winson Chung52aee602013-01-30 12:01:02 -08001375 float interpolatedProgress;
1376 float translationX;
1377 float maxScrollProgress = Math.max(0, scrollProgress);
1378 float minScrollProgress = Math.min(0, scrollProgress);
1379
1380 if (isRtl) {
1381 translationX = maxScrollProgress * v.getMeasuredWidth();
1382 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1383 } else {
1384 translationX = minScrollProgress * v.getMeasuredWidth();
1385 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1386 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001387 float scale = (1 - interpolatedProgress) +
1388 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001389
Adam Cohen2591f6a2011-10-25 14:36:40 -07001390 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001391 if (isRtl && (scrollProgress > 0)) {
1392 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1393 } else if (!isRtl && (scrollProgress < 0)) {
1394 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001395 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001396 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001397 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1398 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001399
1400 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1401 int pageWidth = v.getMeasuredWidth();
1402 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001403
1404 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001405 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1406 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1407 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1408
1409 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001410 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001411 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001412 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1413 scale = 1.0f;
1414 alpha = 1.0f;
1415 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001416 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001417 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001418 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001419 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001420 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1421 scale = 1.0f;
1422 alpha = 1.0f;
1423 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001424 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001425 } else {
1426 v.setPivotY(pageHeight / 2.0f);
1427 v.setPivotX(pageWidth / 2.0f);
1428 v.setRotationY(0f);
1429 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001430 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001431 }
1432 }
Michael Jurka47639b92013-01-14 12:42:27 +01001433
1434 enableHwLayersOnVisiblePages();
1435 }
1436
1437 private void enableHwLayersOnVisiblePages() {
1438 final int screenCount = getChildCount();
1439
1440 getVisiblePages(mTempVisiblePagesRange);
1441 int leftScreen = mTempVisiblePagesRange[0];
1442 int rightScreen = mTempVisiblePagesRange[1];
1443 int forceDrawScreen = -1;
1444 if (leftScreen == rightScreen) {
1445 // make sure we're caching at least two pages always
1446 if (rightScreen < screenCount - 1) {
1447 rightScreen++;
1448 forceDrawScreen = rightScreen;
1449 } else if (leftScreen > 0) {
1450 leftScreen--;
1451 forceDrawScreen = leftScreen;
1452 }
1453 } else {
1454 forceDrawScreen = leftScreen + 1;
1455 }
1456
1457 for (int i = 0; i < screenCount; i++) {
1458 final View layout = (View) getPageAt(i);
1459 if (!(leftScreen <= i && i <= rightScreen &&
1460 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1461 layout.setLayerType(LAYER_TYPE_NONE, null);
1462 }
1463 }
1464
Michael Jurka47639b92013-01-14 12:42:27 +01001465 for (int i = 0; i < screenCount; i++) {
1466 final View layout = (View) getPageAt(i);
1467
1468 if (leftScreen <= i && i <= rightScreen &&
1469 (i == forceDrawScreen || shouldDrawChild(layout))) {
1470 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1471 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1472 }
1473 }
1474 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001475 }
1476
1477 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001478 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001479 }
1480
Winson Chung785d2eb2011-04-14 16:08:02 -07001481 /**
1482 * Used by the parent to get the content width to set the tab bar to
1483 * @return
1484 */
1485 public int getPageContentWidth() {
1486 return mContentWidth;
1487 }
1488
Winson Chungb26f3d62011-06-02 10:49:29 -07001489 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001490 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001491 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001492 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001493 // We reset the save index when we change pages so that it will be recalculated on next
1494 // rotation
1495 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001496 }
1497
Winson Chung785d2eb2011-04-14 16:08:02 -07001498 /*
1499 * AllAppsView implementation
1500 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001501 public void setup(Launcher launcher, DragController dragController) {
1502 mLauncher = launcher;
1503 mDragController = dragController;
1504 }
Winson Chung9802ac92012-06-08 16:01:58 -07001505
1506 /**
1507 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1508 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1509 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1510 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1511 */
1512 private void invalidateOnDataChange() {
1513 if (!isDataReady()) {
1514 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1515 // request a layout to trigger the page data when ready.
1516 requestLayout();
1517 } else {
1518 cancelAllTasks();
1519 invalidatePageData();
1520 }
1521 }
1522
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001523 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001524 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001525 mApps = list;
1526 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1527 updatePageCountsAndInvalidateData();
1528 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001529 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001530 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001531 // We add it in place, in alphabetical order
1532 int count = list.size();
1533 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001534 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001535 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 if (index < 0) {
1537 mApps.add(-(index + 1), info);
1538 }
1539 }
1540 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001541 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001542 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001543 addAppsWithoutInvalidate(list);
1544 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001545 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001546 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001547 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001548 ComponentName removeComponent = item.intent.getComponent();
1549 int length = list.size();
1550 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001551 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001552 if (info.user.equals(item.user)
1553 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001554 return i;
1555 }
1556 }
1557 return -1;
1558 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001559 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 // loop through all the apps and remove apps that have the same component
1561 int length = list.size();
1562 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001563 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 int removeIndex = findAppByComponent(mApps, info);
1565 if (removeIndex > -1) {
1566 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
1568 }
1569 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001570 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001571 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001572 removeAppsWithoutInvalidate(appInfos);
1573 updatePageCountsAndInvalidateData();
1574 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001575 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001576 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001577 // We remove and re-add the updated applications list because it's properties may have
1578 // changed (ie. the title), and this will ensure that the items will be in their proper
1579 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001580 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001581 removeAppsWithoutInvalidate(list);
1582 addAppsWithoutInvalidate(list);
1583 updatePageCountsAndInvalidateData();
1584 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001586
Winson Chung785d2eb2011-04-14 16:08:02 -07001587 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001588 // If we have reset, then we should not continue to restore the previous state
1589 mSaveInstanceStateItemIndex = -1;
1590
Adam Cohen6c5891a2014-07-09 23:53:15 -07001591 if (mContentType != ContentType.Applications) {
1592 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001593 }
Winson Chung649668f2012-01-10 13:07:16 -08001594
Adam Cohenb64d36e2011-10-17 21:48:02 -07001595 if (mCurrentPage != 0) {
1596 invalidatePageData(0);
1597 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001598 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001599
1600 private AppsCustomizeTabHost getTabHost() {
1601 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1602 }
1603
Winson Chung785d2eb2011-04-14 16:08:02 -07001604 public void dumpState() {
1605 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001606 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001607 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 }
Adam Cohen4e844012011-11-09 13:48:04 -08001609
Winson Chung785d2eb2011-04-14 16:08:02 -07001610 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001611 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001612 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001613 for (Object i: list) {
1614 if (i instanceof AppWidgetProviderInfo) {
1615 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1616 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1617 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1618 + " initialLayout=" + info.initialLayout
1619 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1620 } else if (i instanceof ResolveInfo) {
1621 ResolveInfo info = (ResolveInfo) i;
1622 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1623 + info.icon);
1624 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001625 }
1626 }
Adam Cohen4e844012011-11-09 13:48:04 -08001627
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 public void surrender() {
1629 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1630 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001631
1632 // Stop all background tasks
1633 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 }
Winson Chung007c6982011-06-14 13:27:53 -07001635
Winson Chunge4e50662012-01-23 14:45:13 -08001636 @Override
1637 public void iconPressed(PagedViewIcon icon) {
1638 // Reset the previously pressed icon and store a reference to the pressed icon so that
1639 // we can reset it on return to Launcher (in Launcher.onResume())
1640 if (mPressedIcon != null) {
1641 mPressedIcon.resetDrawableState();
1642 }
1643 mPressedIcon = icon;
1644 }
1645
1646 public void resetDrawableState() {
1647 if (mPressedIcon != null) {
1648 mPressedIcon.resetDrawableState();
1649 mPressedIcon = null;
1650 }
1651 }
Winson Chung68e4c642011-11-10 15:48:25 -08001652
Winson Chungb44b5242011-06-13 11:32:14 -07001653 /*
1654 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1655 * widget previews in the background with the AsyncTasks.
1656 */
Winson Chung68e4c642011-11-10 15:48:25 -08001657 final static int sLookBehindPageCount = 2;
1658 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001659 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001660 final int count = getChildCount();
1661 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1662 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1663 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001664 }
1665 protected int getAssociatedUpperPageBound(int page) {
1666 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001667 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1668 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1669 count - 1);
1670 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001671 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001672
Winson Chung6a0f57d2011-06-29 20:10:49 -07001673 protected String getCurrentPageDescription() {
1674 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1675 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001676 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001677
1678 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001679 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001680 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001681 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001682 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001683 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001684 } else {
1685 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001686 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001687
Michael Jurka8b805b12012-04-18 14:23:14 -07001688 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001689 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001690}