blob: 9d86959c415b7375f04fe0decd59ad8c5201333d [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010032import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070036import android.os.Build;
37import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.AttributeSet;
40import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070041import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080042import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
44import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070048import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070050import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * A simple callback interface which also provides the results of the task.
61 */
62interface AsyncTaskCallback {
63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
64}
Winson Chung4e076542011-06-23 13:04:10 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * The data needed to perform either of the custom AsyncTasks.
68 */
69class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070070 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080071 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070072 }
73
Michael Jurka038f9d82011-11-03 13:50:45 -070074 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010075 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070076 page = p;
77 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070078 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070079 maxImageWidth = cw;
80 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070081 doInBackgroundCallback = bgR;
82 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010083 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070084 }
Winson Chung09945932011-09-20 14:22:40 -070085 void cleanup(boolean cancelled) {
86 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070087 if (generatedImages != null) {
88 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010089 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010090 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070091 }
92 }
93 generatedImages.clear();
94 }
95 }
Winson Chungb44b5242011-06-13 11:32:14 -070096 int page;
97 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070098 ArrayList<Bitmap> sourceImages;
99 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 int maxImageWidth;
101 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 AsyncTaskCallback doInBackgroundCallback;
103 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100104 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700105}
Winson Chung4e076542011-06-23 13:04:10 -0700106
Winson Chungb44b5242011-06-13 11:32:14 -0700107/**
108 * A generic template for an async task used in AppsCustomize.
109 */
110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700111 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700112 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700113 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700114 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700115 }
116 @Override
117 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
118 if (params.length != 1) return null;
119 // Load each of the widget previews in the background
120 params[0].doInBackgroundCallback.run(this, params[0]);
121 return params[0];
122 }
123 @Override
124 protected void onPostExecute(AsyncTaskPageData result) {
125 // All the widget previews are loaded, so we can just callback to inflate the page
126 result.postExecuteCallback.run(this, result);
127 }
128
129 void setThreadPriority(int p) {
130 threadPriority = p;
131 }
132 void syncThreadPriority() {
133 Process.setThreadPriority(threadPriority);
134 }
135
136 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700137 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int threadPriority;
140}
Winson Chungb44b5242011-06-13 11:32:14 -0700141
142/**
143 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
144 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700145public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700146 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700147 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
148 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700149 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700150
151 /**
152 * The different content types that this paged view can show.
153 */
154 public enum ContentType {
155 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700156 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700157 }
Winson Chungc58497e2013-09-03 17:48:37 -0700158 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700159
160 // Refs
161 private Launcher mLauncher;
162 private DragController mDragController;
163 private final LayoutInflater mLayoutInflater;
164 private final PackageManager mPackageManager;
165
Winson Chung5afbf7b2011-07-25 11:53:08 -0700166 // Save and Restore
167 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800168 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700169
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200171 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700172 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700173
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800175 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private int mClingFocusedX;
177 private int mClingFocusedY;
178
Winson Chung1ed747a2011-05-03 16:18:34 -0700179 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700180 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700181 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700182
183 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700184 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700185 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700186 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700187 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700188 private int mNumAppsPages;
189 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700190 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700191
Adam Cohen22f823d2011-09-01 17:22:18 -0700192 // Relating to the scroll and overscroll effects
193 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700194 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float TRANSITION_PIVOT = 0.65f;
197 private static float TRANSITION_MAX_ROTATION = 22;
198 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700199 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700200 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700201
Winson Chungb44b5242011-06-13 11:32:14 -0700202 // Previews & outlines
203 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800204 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700205
Adam Cohened66b2b2012-01-23 17:28:51 -0800206 private Runnable mInflateWidgetRunnable = null;
207 private Runnable mBindWidgetRunnable = null;
208 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700209 static final int WIDGET_PRELOAD_PENDING = 0;
210 static final int WIDGET_BOUND = 1;
211 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800212 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
213 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800214 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800215 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800216
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700217 private Toast mWidgetInstructionToast;
218
Michael Jurka39e5d172012-03-12 18:36:12 -0700219 // Deferral of loading widget previews during launcher transitions
220 private boolean mInTransition;
221 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
222 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700223 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
224 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700225
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700226 private Rect mTmpRect = new Rect();
227
Michael 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();
247 mWidgetWidthGap = mWidgetHeightGap = grid.edgeMarginPx;
Winson Chung4b576be2011-04-27 17:40:20 -0700248 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
249 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700250 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
251 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700252 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700253 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700254
Winson Chung1ed747a2011-05-03 16:18:34 -0700255 // The padding on the non-matched dimension for the default widget preview icons
256 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700257 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700258
259 // Unless otherwise specified this view is important for accessibility.
260 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
261 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
262 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700263 }
264
265 @Override
266 protected void init() {
267 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700268 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700269
270 Context context = getContext();
271 Resources r = context.getResources();
272 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
273 }
274
Winson Chungc58497e2013-09-03 17:48:37 -0700275 public void onFinishInflate() {
276 super.onFinishInflate();
277
278 LauncherAppState app = LauncherAppState.getInstance();
279 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
280 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
281 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
282 }
283
Winson Chung67ca7e42013-10-31 16:53:19 -0700284 void setAllAppsPadding(Rect r) {
285 mAllAppsPadding.set(r);
286 }
287 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
288 mPageLayoutPaddingBottom = pageIndicatorHeight;
289 }
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 Chungf0ea4d32011-06-06 14:27:16 -0700367 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
368 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungc58497e2013-09-03 17:48:37 -0700369 mCellCountX = (int) grid.allAppsNumCols;
370 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700371 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700372
Winson Chungdb1138b2011-06-30 14:39:35 -0700373 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700374 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
375 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
376 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
377 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700378 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700379
Michael Jurkae326f182011-11-21 14:05:46 -0800380 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
381 final boolean hostIsTransitioning = host.isTransitioning();
382
Adam Cohen0cd3b642011-10-14 14:58:00 -0700383 // Restore the page
384 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800385 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700386
Winson Chung3f4e1422011-11-17 14:58:51 -0800387 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
388 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
389 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800390 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800391 post(new Runnable() {
392 @Override
393 public void run() {
394 showAllAppsCling();
395 }
396 });
397 }
398 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700399
Winson Chung3f4e1422011-11-17 14:58:51 -0800400 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700401 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800402 mHasShownAllAppsCling = true;
403 // Calculate the position for the cling punch through
404 int[] offset = new int[2];
405 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
406 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
407 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800408 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800409 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
410 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800411 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800412 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700413 }
414
415 @Override
416 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
417 int width = MeasureSpec.getSize(widthMeasureSpec);
418 int height = MeasureSpec.getSize(heightMeasureSpec);
419 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800420 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700421 setDataIsReady();
422 setMeasuredDimension(width, height);
423 onDataReady(width, height);
424 }
425 }
426
427 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
428 }
429
Michael Jurkac402cd92013-05-20 15:49:32 +0200430 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700431 LauncherAppState app = LauncherAppState.getInstance();
432 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
433
Winson Chung1ed747a2011-05-03 16:18:34 -0700434 // Get the list of widgets and shortcuts
435 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200436 for (Object o : widgetsAndShortcuts) {
437 if (o instanceof AppWidgetProviderInfo) {
438 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100439 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
440 continue;
441 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200442 widget.label = widget.label.trim();
443 if (widget.minWidth > 0 && widget.minHeight > 0) {
444 // Ensure that all widgets we show can be added on a workspace of this size
445 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
446 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
447 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
448 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700449 if (minSpanX <= (int) grid.numColumns &&
450 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200451 mWidgets.add(widget);
452 } else {
453 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
454 widget.minWidth + ", " + widget.minHeight + ")");
455 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700456 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200457 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
458 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700459 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800460 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200461 // just add shortcuts
462 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800463 }
464 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200465 updatePageCountsAndInvalidateData();
466 }
467
468 public void setBulkBind(boolean bulkBind) {
469 if (bulkBind) {
470 mInBulkBind = true;
471 } else {
472 mInBulkBind = false;
473 if (mNeedToUpdatePageCountsAndInvalidateData) {
474 updatePageCountsAndInvalidateData();
475 }
476 }
477 }
478
479 private void updatePageCountsAndInvalidateData() {
480 if (mInBulkBind) {
481 mNeedToUpdatePageCountsAndInvalidateData = true;
482 } else {
483 updatePageCounts();
484 invalidateOnDataChange();
485 mNeedToUpdatePageCountsAndInvalidateData = false;
486 }
Winson Chung4b576be2011-04-27 17:40:20 -0700487 }
488
489 @Override
490 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700491 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700492 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700493 mLauncher.getWorkspace().isSwitchingState()) return;
494
Winson Chung4b576be2011-04-27 17:40:20 -0700495 if (v instanceof PagedViewIcon) {
496 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200497 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800498
499 // Lock the drawable state to pressed until we return to Launcher
500 if (mPressedIcon != null) {
501 mPressedIcon.lockDrawableState();
502 }
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -0700503 mLauncher.onClickPagedViewIcon(v, appInfo);
Winson Chung4b576be2011-04-27 17:40:20 -0700504 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700505 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700506 if (mWidgetInstructionToast != null) {
507 mWidgetInstructionToast.cancel();
508 }
509 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
510 Toast.LENGTH_SHORT);
511 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700512
Winson Chungd2e87b32011-06-02 10:53:07 -0700513 // Create a little animation to show that the widget can move
514 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
515 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700516 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
517 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700518 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700519 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700520 tydAnim.setDuration(100);
521 bounce.play(tyuAnim).before(tydAnim);
522 bounce.setInterpolator(new AccelerateInterpolator());
523 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700524 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700525 }
526
Winson Chungc6f10b92011-11-14 11:39:07 -0800527 public boolean onKey(View v, int keyCode, KeyEvent event) {
528 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
529 }
530
Winson Chung785d2eb2011-04-14 16:08:02 -0700531 /*
532 * PagedViewWithDraggableItems implementation
533 */
534 @Override
535 protected void determineDraggingStart(android.view.MotionEvent ev) {
536 // Disable dragging by pulling an app down for now.
537 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700538
Winson Chung4b576be2011-04-27 17:40:20 -0700539 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700540 mLauncher.getWorkspace().onDragStartedWithItem(v);
541 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700542 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700543
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700544 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
545 Bundle options = null;
546 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
547 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700548 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
549 info.componentName, null);
550
551 float density = getResources().getDisplayMetrics().density;
552 int xPaddingDips = (int) ((padding.left + padding.right) / density);
553 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
554
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700555 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700556 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
557 mTmpRect.left - xPaddingDips);
558 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
559 mTmpRect.top - yPaddingDips);
560 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
561 mTmpRect.right - xPaddingDips);
562 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
563 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700564 }
565 return options;
566 }
567
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700568 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800569 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700570 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
571
Adam Cohened66b2b2012-01-23 17:28:51 -0800572 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700573 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800574 return;
575 }
576
Adam Cohen21a170b2012-05-30 15:17:06 -0700577 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 mBindWidgetRunnable = new Runnable() {
579 @Override
580 public void run() {
581 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700582 // Options will be null for platforms with JB or lower, so this serves as an
583 // SDK level check.
584 if (options == null) {
585 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
586 mWidgetLoadingId, info.componentName)) {
587 mWidgetCleanupState = WIDGET_BOUND;
588 }
589 } else {
590 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
591 mWidgetLoadingId, info.componentName, options)) {
592 mWidgetCleanupState = WIDGET_BOUND;
593 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800595 }
596 };
597 post(mBindWidgetRunnable);
598
599 mInflateWidgetRunnable = new Runnable() {
600 @Override
601 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700602 if (mWidgetCleanupState != WIDGET_BOUND) {
603 return;
604 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700605 AppWidgetHostView hostView = mLauncher.
606 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 info.boundWidget = hostView;
608 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800609 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700610 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
611 info.spanY, info, false);
612
613 // We want the first widget layout to be the correct size. This will be important
614 // for width size reporting to the AppWidgetManager.
615 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
616 unScaledSize[1]);
617 lp.x = lp.y = 0;
618 lp.customPosition = true;
619 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800620 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 }
622 };
623 post(mInflateWidgetRunnable);
624 }
625
626 @Override
627 public void onShortPress(View v) {
628 // We are anticipating a long press, and we use this time to load bind and instantiate
629 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700630 if (mCreateWidgetInfo != null) {
631 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
632 cleanupWidgetPreloading(false);
633 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800634 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700635 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 }
637
Adam Cohen0e56cc92012-05-11 15:57:05 -0700638 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
639 if (!widgetWasAdded) {
640 // If the widget was not added, we may need to do further cleanup.
641 PendingAddWidgetInfo info = mCreateWidgetInfo;
642 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700643
644 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700645 // We never did any preloading, so just remove pending callbacks to do so
646 removeCallbacks(mBindWidgetRunnable);
647 removeCallbacks(mInflateWidgetRunnable);
648 } else if (mWidgetCleanupState == WIDGET_BOUND) {
649 // Delete the widget id which was allocated
650 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700651 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
652 }
653
654 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700655 removeCallbacks(mInflateWidgetRunnable);
656 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700657 // Delete the widget id which was allocated
658 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700659 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
660 }
661
Adam Cohen0e56cc92012-05-11 15:57:05 -0700662 // The widget was inflated and added to the DragLayer -- remove it.
663 AppWidgetHostView widget = info.boundWidget;
664 mLauncher.getDragLayer().removeView(widget);
665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 }
667 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
668 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700669 mCreateWidgetInfo = null;
670 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 }
672
Adam Cohen7a326642012-02-22 12:03:22 -0800673 @Override
674 public void cleanUpShortPress(View v) {
675 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700676 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800677 }
678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700680 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800681 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700682 // Get the widget preview as the drag representation
683 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700684 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700685
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700686 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
687 // we abort the drag.
688 if (image.getDrawable() == null) {
689 mDraggingWidget = false;
690 return false;
691 }
692
Winson Chung4b576be2011-04-27 17:40:20 -0700693 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800694 Bitmap preview;
695 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800696 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100697 Point previewPadding = null;
698
Winson Chung1ed747a2011-05-03 16:18:34 -0700699 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700700 // This can happen in some weird cases involving multi-touch. We can't start dragging
701 // the widget if this is null, so we break out.
702 if (mCreateWidgetInfo == null) {
703 return false;
704 }
705
Adam Cohen1b36dc32012-02-13 19:27:37 -0800706 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
707 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700708 int spanX = createItemInfo.spanX;
709 int spanY = createItemInfo.spanY;
710 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
711 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700712
Winson Chung72d59842012-02-22 13:51:36 -0800713 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
714 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700715 int maxWidth, maxHeight;
716 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
717 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800718
Michael Jurka05713af2013-01-23 12:39:24 +0100719 int[] previewSizeBeforeScale = new int[1];
720
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100721 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100722 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
723 maxWidth, maxHeight, null, previewSizeBeforeScale);
724
725 // Compare the size of the drag preview to the preview in the AppsCustomize tray
726 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100727 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100728 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
729
730 // The bitmap in the AppsCustomize tray is always the the same size, so there
731 // might be extra pixels around the preview itself - this accounts for that
732 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
733 int padding =
734 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
735 previewPadding = new Point(padding, 0);
736 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700737 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700738 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
739 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
740 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
741 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
742
Winson Chung1120e032011-11-22 16:11:31 -0800743 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700744 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100745 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700746 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700747 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700748 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700749 createItemInfo.spanX = createItemInfo.spanY = 1;
750 }
Winson Chung4b576be2011-04-27 17:40:20 -0700751
Michael Jurka8c3339b2012-06-14 16:18:21 -0700752 // Don't clip alpha values for the drag outline if we're using the default widget preview
753 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
754 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700755
Winson Chung1120e032011-11-22 16:11:31 -0800756 // Save the preview for the outline generation, then dim the preview
757 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
758 false);
Winson Chung1120e032011-11-22 16:11:31 -0800759
Winson Chung4b576be2011-04-27 17:40:20 -0700760 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700761 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700762 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800763 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100764 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800765 outline.recycle();
766 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700767 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700768 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800769
Winson Chung4b576be2011-04-27 17:40:20 -0700770 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800771 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700772 if (!super.beginDragging(v)) return false;
773
774 if (v instanceof PagedViewIcon) {
775 beginDraggingApplication(v);
776 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700777 if (!beginDraggingWidget(v)) {
778 return false;
779 }
Winson Chung4b576be2011-04-27 17:40:20 -0700780 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800781
782 // We delay entering spring-loaded mode slightly to make sure the UI
783 // thready is free of any work.
784 postDelayed(new Runnable() {
785 @Override
786 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800787 // We don't enter spring-loaded mode if the drag has been cancelled
788 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800789 // Reset the alpha on the dragged icon before we drag
790 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800791
Adam Cohen1b36dc32012-02-13 19:27:37 -0800792 // Go into spring loaded mode (must happen before we startDrag())
793 mLauncher.enterSpringLoadedDragMode();
794 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 }
Winson Chung72d59842012-02-22 13:51:36 -0800796 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800797
Winson Chung785d2eb2011-04-14 16:08:02 -0700798 return true;
799 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800800
Winson Chunga48487a2012-03-20 16:19:37 -0700801 /**
802 * Clean up after dragging.
803 *
804 * @param target where the item was dragged to (can be null if the item was flung)
805 */
806 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700807 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800808 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700809 // Exit spring loaded mode if we have not successfully dropped or have not handled the
810 // drop in Workspace
Adam Cohene97a3b32013-10-23 16:11:50 -0700811 mLauncher.getWorkspace().removeExtraEmptyScreen(true, new Runnable() {
812 @Override
813 public void run() {
814 mLauncher.exitSpringLoadedDragMode();
815 mLauncher.unlockScreenOrientation(false);
816 }
817 });
818 } else {
819 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700820 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 }
822
Winson Chung785d2eb2011-04-14 16:08:02 -0700823 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700824 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700825 if (getChildCount() > 0) {
826 return getChildAt(0);
827 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700828 return null;
829 }
830
831 @Override
832 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700833 mInTransition = true;
834 if (toWorkspace) {
835 cancelAllTasks();
836 }
837 }
838
839 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700840 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700841 }
842
843 @Override
844 public void onLauncherTransitionStep(Launcher l, float t) {
845 }
846
847 @Override
848 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
849 mInTransition = false;
850 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700851 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700852 }
853 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700854 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
855 r.run();
856 }
857 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700858 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700859 }
860
861 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700862 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
863 boolean success) {
864 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
865 if (isFlingToDelete) return;
866
867 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700868
869 // Display an error message if the drag failed due to there not being enough space on the
870 // target layout we were dropping on.
871 if (!success) {
872 boolean showOutOfSpaceMessage = false;
873 if (target instanceof Workspace) {
874 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
875 Workspace workspace = (Workspace) target;
876 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700877 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700878 if (layout != null) {
879 layout.calculateSpans(itemInfo);
880 showOutOfSpaceMessage =
881 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
882 }
883 }
Winson Chungfc79c802011-05-02 13:35:34 -0700884 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700885 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700886 }
Adam Cohen7a326642012-02-22 12:03:22 -0800887
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800888 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700889 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700890 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800891 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700892 }
893
Winson Chunga48487a2012-03-20 16:19:37 -0700894 @Override
895 public void onFlingToDeleteCompleted() {
896 // We just dismiss the drag when we fling, so cleanup here
897 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700898 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700899 mDraggingWidget = false;
900 }
901
902 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800903 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700904 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800905 }
906
Winson Chung7f0acdd2011-09-19 18:34:19 -0700907 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000908 public boolean supportsAppInfoDropTarget() {
909 return true;
910 }
911
912 @Override
913 public boolean supportsDeleteDropTarget() {
914 return false;
915 }
916
917 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800918 public float getIntrinsicIconScaleFactor() {
919 LauncherAppState app = LauncherAppState.getInstance();
920 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
921 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
922 }
923
924 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700925 protected void onDetachedFromWindow() {
926 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700927 cancelAllTasks();
928 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700929
Michael Jurkae326f182011-11-21 14:05:46 -0800930 public void clearAllWidgetPages() {
931 cancelAllTasks();
932 int count = getChildCount();
933 for (int i = 0; i < count; i++) {
934 View v = getPageAt(i);
935 if (v instanceof PagedViewGridLayout) {
936 ((PagedViewGridLayout) v).removeAllViewsOnPage();
937 mDirtyPageContent.set(i, true);
938 }
939 }
940 }
941
Adam Cohen0cd3b642011-10-14 14:58:00 -0700942 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700943 // Clean up all the async tasks
944 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
945 while (iter.hasNext()) {
946 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
947 task.cancel(false);
948 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700949 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700950
951 // We've already preallocated the views for the data to load into, so clear them as well
952 View v = getPageAt(task.page);
953 if (v instanceof PagedViewGridLayout) {
954 ((PagedViewGridLayout) v).removeAllViewsOnPage();
955 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700956 }
Winson Chung83687b12012-04-25 16:01:01 -0700957 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700958 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700959 }
960
Winson Chung785d2eb2011-04-14 16:08:02 -0700961 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200962 // Widgets appear to be cleared every time you leave, always force invalidate for them
963 if (mContentType != type || type == ContentType.Widgets) {
964 int page = (mContentType != type) ? 0 : getCurrentPage();
965 mContentType = type;
966 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700967 }
Winson Chungc58497e2013-09-03 17:48:37 -0700968 }
969
970 public ContentType getContentType() {
971 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700972 }
973
Adam Cohen0cd3b642011-10-14 14:58:00 -0700974 protected void snapToPage(int whichPage, int delta, int duration) {
975 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800976
977 // Update the thread priorities given the direction lookahead
978 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
979 while (iter.hasNext()) {
980 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700981 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800982 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
983 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
984 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
985 } else {
986 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
987 }
988 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700989 }
990
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 /*
992 * Apps PagedView implementation
993 */
Winson Chung63257c12011-05-05 17:06:13 -0700994 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
995 int childCount = layout.getChildCount();
996 for (int i = 0; i < childCount; ++i) {
997 layout.getChildAt(i).setVisibility(visibility);
998 }
999 }
Winson Chungc58497e2013-09-03 17:48:37 -07001000 private void setupPage(AppsCustomizeCellLayout layout) {
1001 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -07001002
Winson Chung63257c12011-05-05 17:06:13 -07001003 // Note: We force a measure here to get around the fact that when we do layout calculations
1004 // immediately after syncing, we don't have a proper width. That said, we already know the
1005 // expected page width, so we can actually optimize by hiding all the TextView-based
1006 // children that are expensive to measure, and let that happen naturally later.
1007 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -07001008 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1009 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001010 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001011 layout.measure(widthSpec, heightSpec);
Winson Chung67ca7e42013-10-31 16:53:19 -07001012 layout.setPadding(mAllAppsPadding.left, mAllAppsPadding.top, mAllAppsPadding.right,
1013 mAllAppsPadding.bottom);
Winson Chung63257c12011-05-05 17:06:13 -07001014 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001015 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001016
Winson Chungf314b0e2011-08-16 11:54:27 -07001017 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001018 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001019 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001020 int numCells = mCellCountX * mCellCountY;
1021 int startIndex = page * numCells;
1022 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -07001023 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001024
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001026 ArrayList<Object> items = new ArrayList<Object>();
1027 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001028 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001029 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1031 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001032 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001033 icon.setOnClickListener(this);
1034 icon.setOnLongClickListener(this);
1035 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001036 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001037
1038 int index = i - startIndex;
1039 int x = index % mCellCountX;
1040 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001041 if (isRtl) {
1042 x = mCellCountX - x - 1;
1043 }
Winson Chungc58497e2013-09-03 17:48:37 -07001044 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001045
1046 items.add(info);
1047 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001048 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001049
Michael Jurka47639b92013-01-14 12:42:27 +01001050 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001051 }
Winson Chungb44b5242011-06-13 11:32:14 -07001052
1053 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001054 * A helper to return the priority for loading of the specified widget page.
1055 */
1056 private int getWidgetPageLoadPriority(int page) {
1057 // If we are snapping to another page, use that index as the target page index
1058 int toPage = mCurrentPage;
1059 if (mNextPage > -1) {
1060 toPage = mNextPage;
1061 }
1062
1063 // We use the distance from the target page as an initial guess of priority, but if there
1064 // are no pages of higher priority than the page specified, then bump up the priority of
1065 // the specified page.
1066 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1067 int minPageDiff = Integer.MAX_VALUE;
1068 while (iter.hasNext()) {
1069 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001070 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001071 }
1072
1073 int rawPageDiff = Math.abs(page - toPage);
1074 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1075 }
1076 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001077 * Return the appropriate thread priority for loading for a given page (we give the current
1078 * page much higher priority)
1079 */
1080 private int getThreadPriorityForPage(int page) {
1081 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001082 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001083 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001084 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001085 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001086 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001087 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001088 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001089 }
1090 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001091 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001092 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001093 return Math.max(0, pageDiff * sPageSleepDelay);
1094 }
Winson Chungb44b5242011-06-13 11:32:14 -07001095 /**
1096 * Creates and executes a new AsyncTask to load a page of widget previews.
1097 */
1098 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001099 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001100
Winson Chungb44b5242011-06-13 11:32:14 -07001101 // Prune all tasks that are no longer needed
1102 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1103 while (iter.hasNext()) {
1104 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001105 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001106 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1107 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001108 task.cancel(false);
1109 iter.remove();
1110 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001111 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001112 }
1113 }
1114
Winson Chungf314b0e2011-08-16 11:54:27 -07001115 // 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 -07001116 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001117 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001118 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001119 @Override
1120 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001121 try {
Winson Chung09945932011-09-20 14:22:40 -07001122 try {
1123 Thread.sleep(sleepMs);
1124 } catch (Exception e) {}
1125 loadWidgetPreviewsInBackground(task, data);
1126 } finally {
1127 if (task.isCancelled()) {
1128 data.cleanup(true);
1129 }
1130 }
Winson Chungb44b5242011-06-13 11:32:14 -07001131 }
1132 },
1133 new AsyncTaskCallback() {
1134 @Override
1135 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001136 mRunningTasks.remove(task);
1137 if (task.isCancelled()) return;
1138 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001139 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001140 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001141 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001142
1143 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001144 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001145 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001146 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001147 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1148 mRunningTasks.add(t);
1149 }
Winson Chungb44b5242011-06-13 11:32:14 -07001150
Winson Chung785d2eb2011-04-14 16:08:02 -07001151 /*
1152 * Widgets PagedView implementation
1153 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001154 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001155 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001156 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001157 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1158 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001159 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001160 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001161 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001162
Michael Jurka038f9d82011-11-03 13:50:45 -07001163 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001164 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001165
Winson Chungd2945262011-06-24 15:22:14 -07001166 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001167 final ArrayList<Object> items = new ArrayList<Object>();
Winson Chungc58497e2013-09-03 17:48:37 -07001168 int contentWidth = mContentWidth;
Michael Jurka038f9d82011-11-03 13:50:45 -07001169 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001170 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungc58497e2013-09-03 17:48:37 -07001171 int contentHeight = mContentHeight;
Michael Jurka038f9d82011-11-03 13:50:45 -07001172 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001173 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001174
Winson Chunge4a647f2011-09-30 14:41:25 -07001175 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001176 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001177 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1178 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001179 }
1180
Winson Chunge4a647f2011-09-30 14:41:25 -07001181 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001182 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001183 layout.setColumnCount(layout.getCellCountX());
1184 for (int i = 0; i < items.size(); ++i) {
1185 Object rawInfo = items.get(i);
1186 PendingAddItemInfo createItemInfo = null;
1187 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1188 R.layout.apps_customize_widget, layout, false);
1189 if (rawInfo instanceof AppWidgetProviderInfo) {
1190 // Fill in the widget information
1191 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1192 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001193
1194 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001195 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001196 createItemInfo.spanX = spanXY[0];
1197 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001198 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001199 createItemInfo.minSpanX = minSpanXY[0];
1200 createItemInfo.minSpanY = minSpanXY[1];
1201
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001202 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001203 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001204 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001205 } else if (rawInfo instanceof ResolveInfo) {
1206 // Fill in the shortcuts information
1207 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001208 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001209 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1210 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1211 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001212 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001213 widget.setTag(createItemInfo);
1214 }
1215 widget.setOnClickListener(this);
1216 widget.setOnLongClickListener(this);
1217 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001218 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001219
1220 // Layout each widget
1221 int ix = i % mWidgetCountX;
1222 int iy = i / mWidgetCountX;
1223 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001224 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001225 GridLayout.spec(ix, GridLayout.TOP));
1226 lp.width = cellWidth;
1227 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001228 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001229 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1230 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1231 layout.addView(widget, lp);
1232 }
1233
Michael Jurka038f9d82011-11-03 13:50:45 -07001234 // wait until a call on onLayout to start loading, because
1235 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1236 // TODO: can we do a measure/layout immediately?
1237 layout.setOnLayoutListener(new Runnable() {
1238 public void run() {
1239 // Load the widget previews
1240 int maxPreviewWidth = cellWidth;
1241 int maxPreviewHeight = cellHeight;
1242 if (layout.getChildCount() > 0) {
1243 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1244 int[] maxSize = w.getPreviewSize();
1245 maxPreviewWidth = maxSize[0];
1246 maxPreviewHeight = maxSize[1];
1247 }
Michael Jurka05713af2013-01-23 12:39:24 +01001248
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001249 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001250 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001251 if (immediate) {
1252 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001253 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001254 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001255 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001256 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001257 if (mInTransition) {
1258 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1259 } else {
1260 prepareLoadWidgetPreviewsTask(page, items,
1261 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1262 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001263 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001264 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001265 }
1266 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001267 }
1268 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1269 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001270 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1271 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001272 if (task != null) {
1273 // Ensure that this task starts running at the correct priority
1274 task.syncThreadPriority();
1275 }
1276
1277 // Load each of the widget/shortcut previews
1278 ArrayList<Object> items = data.items;
1279 ArrayList<Bitmap> images = data.generatedImages;
1280 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001281 for (int i = 0; i < count; ++i) {
1282 if (task != null) {
1283 // Ensure we haven't been cancelled yet
1284 if (task.isCancelled()) break;
1285 // Before work on each item, ensure that this task is running at the correct
1286 // priority
1287 task.syncThreadPriority();
1288 }
1289
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001290 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001291 }
Winson Chungb44b5242011-06-13 11:32:14 -07001292 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001293
Winson Chung7bb37522013-10-28 11:07:57 -07001294 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1295 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001296 mDeferredSyncWidgetPageItems.add(data);
1297 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001298 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001299 try {
1300 int page = data.page;
1301 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001302
Michael Jurka39e5d172012-03-12 18:36:12 -07001303 ArrayList<Object> items = data.items;
1304 int count = items.size();
1305 for (int i = 0; i < count; ++i) {
1306 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1307 if (widget != null) {
1308 Bitmap preview = data.generatedImages.get(i);
1309 widget.applyPreview(new FastBitmapDrawable(preview), i);
1310 }
1311 }
Winson Chung68e4c642011-11-10 15:48:25 -08001312
Michael Jurka47639b92013-01-14 12:42:27 +01001313 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001314
1315 // Update all thread priorities
1316 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1317 while (iter.hasNext()) {
1318 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1319 int pageIndex = task.page;
1320 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1321 }
1322 } finally {
1323 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001324 }
Winson Chungb44b5242011-06-13 11:32:14 -07001325 }
Winson Chung46af2e82011-05-09 16:00:53 -07001326
Winson Chung785d2eb2011-04-14 16:08:02 -07001327 @Override
1328 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001329 disablePagedViewAnimations();
1330
Winson Chung785d2eb2011-04-14 16:08:02 -07001331 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001332 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001333
Adam Cohen0cd3b642011-10-14 14:58:00 -07001334 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001335 if (mContentType == ContentType.Applications) {
1336 for (int i = 0; i < mNumAppsPages; ++i) {
1337 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1338 setupPage(layout);
1339 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1340 LayoutParams.MATCH_PARENT));
1341 }
1342 } else if (mContentType == ContentType.Widgets) {
1343 for (int j = 0; j < mNumWidgetPages; ++j) {
1344 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1345 mWidgetCountY);
1346 setupPage(layout);
1347 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1348 LayoutParams.MATCH_PARENT));
1349 }
1350 } else {
1351 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001352 }
1353
Winson Chungc58497e2013-09-03 17:48:37 -07001354 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001355 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001356
Winson Chung785d2eb2011-04-14 16:08:02 -07001357 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001358 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001359 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001360 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001361 } else {
1362 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001363 }
1364 }
1365
Adam Cohen22f823d2011-09-01 17:22:18 -07001366 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1367 // it is in the z-order. This is important to insure touch events are handled correctly.
1368 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001369 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001370 }
1371
Adam Cohenae4f1552011-10-20 00:15:42 -07001372 @Override
1373 protected int indexToPage(int index) {
1374 return getChildCount() - index - 1;
1375 }
1376
Adam Cohen22f823d2011-09-01 17:22:18 -07001377 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1378 @Override
1379 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001380 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001381 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001382
1383 for (int i = 0; i < getChildCount(); i++) {
1384 View v = getPageAt(i);
1385 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001386 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001387
Winson Chung52aee602013-01-30 12:01:02 -08001388 float interpolatedProgress;
1389 float translationX;
1390 float maxScrollProgress = Math.max(0, scrollProgress);
1391 float minScrollProgress = Math.min(0, scrollProgress);
1392
1393 if (isRtl) {
1394 translationX = maxScrollProgress * v.getMeasuredWidth();
1395 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1396 } else {
1397 translationX = minScrollProgress * v.getMeasuredWidth();
1398 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1399 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001400 float scale = (1 - interpolatedProgress) +
1401 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001402
Adam Cohen2591f6a2011-10-25 14:36:40 -07001403 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001404 if (isRtl && (scrollProgress > 0)) {
1405 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1406 } else if (!isRtl && (scrollProgress < 0)) {
1407 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001408 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001409 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001410 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1411 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001412
1413 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1414 int pageWidth = v.getMeasuredWidth();
1415 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001416
1417 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001418 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1419 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1420 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1421
1422 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001423 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001424 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001425 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1426 scale = 1.0f;
1427 alpha = 1.0f;
1428 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001429 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001430 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001431 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001432 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001433 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1434 scale = 1.0f;
1435 alpha = 1.0f;
1436 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001437 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001438 } else {
1439 v.setPivotY(pageHeight / 2.0f);
1440 v.setPivotX(pageWidth / 2.0f);
1441 v.setRotationY(0f);
1442 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001443 }
1444
1445 v.setTranslationX(translationX);
1446 v.setScaleX(scale);
1447 v.setScaleY(scale);
1448 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001449
1450 // If the view has 0 alpha, we set it to be invisible so as to prevent
1451 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001452 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001453 v.setVisibility(INVISIBLE);
1454 } else if (v.getVisibility() != VISIBLE) {
1455 v.setVisibility(VISIBLE);
1456 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001457 }
1458 }
Michael Jurka47639b92013-01-14 12:42:27 +01001459
1460 enableHwLayersOnVisiblePages();
1461 }
1462
1463 private void enableHwLayersOnVisiblePages() {
1464 final int screenCount = getChildCount();
1465
1466 getVisiblePages(mTempVisiblePagesRange);
1467 int leftScreen = mTempVisiblePagesRange[0];
1468 int rightScreen = mTempVisiblePagesRange[1];
1469 int forceDrawScreen = -1;
1470 if (leftScreen == rightScreen) {
1471 // make sure we're caching at least two pages always
1472 if (rightScreen < screenCount - 1) {
1473 rightScreen++;
1474 forceDrawScreen = rightScreen;
1475 } else if (leftScreen > 0) {
1476 leftScreen--;
1477 forceDrawScreen = leftScreen;
1478 }
1479 } else {
1480 forceDrawScreen = leftScreen + 1;
1481 }
1482
1483 for (int i = 0; i < screenCount; i++) {
1484 final View layout = (View) getPageAt(i);
1485 if (!(leftScreen <= i && i <= rightScreen &&
1486 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1487 layout.setLayerType(LAYER_TYPE_NONE, null);
1488 }
1489 }
1490
Michael Jurka47639b92013-01-14 12:42:27 +01001491 for (int i = 0; i < screenCount; i++) {
1492 final View layout = (View) getPageAt(i);
1493
1494 if (leftScreen <= i && i <= rightScreen &&
1495 (i == forceDrawScreen || shouldDrawChild(layout))) {
1496 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1497 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1498 }
1499 }
1500 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001501 }
1502
1503 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001504 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001505 }
1506
Winson Chung785d2eb2011-04-14 16:08:02 -07001507 /**
1508 * Used by the parent to get the content width to set the tab bar to
1509 * @return
1510 */
1511 public int getPageContentWidth() {
1512 return mContentWidth;
1513 }
1514
Winson Chungb26f3d62011-06-02 10:49:29 -07001515 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001516 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001517 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001518 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001519 // We reset the save index when we change pages so that it will be recalculated on next
1520 // rotation
1521 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001522 }
1523
Winson Chung785d2eb2011-04-14 16:08:02 -07001524 /*
1525 * AllAppsView implementation
1526 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001527 public void setup(Launcher launcher, DragController dragController) {
1528 mLauncher = launcher;
1529 mDragController = dragController;
1530 }
Winson Chung9802ac92012-06-08 16:01:58 -07001531
1532 /**
1533 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1534 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1535 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1536 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1537 */
1538 private void invalidateOnDataChange() {
1539 if (!isDataReady()) {
1540 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1541 // request a layout to trigger the page data when ready.
1542 requestLayout();
1543 } else {
1544 cancelAllTasks();
1545 invalidatePageData();
1546 }
1547 }
1548
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001549 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001550 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001551 mApps = list;
1552 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1553 updatePageCountsAndInvalidateData();
1554 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001555 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001556 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001557 // We add it in place, in alphabetical order
1558 int count = list.size();
1559 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001560 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001561 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001562 if (index < 0) {
1563 mApps.add(-(index + 1), info);
1564 }
1565 }
1566 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001567 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001568 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001569 addAppsWithoutInvalidate(list);
1570 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001571 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001572 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001573 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001574 ComponentName removeComponent = item.intent.getComponent();
1575 int length = list.size();
1576 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001577 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001578 if (info.user.equals(item.user)
1579 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001580 return i;
1581 }
1582 }
1583 return -1;
1584 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001585 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 // loop through all the apps and remove apps that have the same component
1587 int length = list.size();
1588 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001589 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 int removeIndex = findAppByComponent(mApps, info);
1591 if (removeIndex > -1) {
1592 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001593 }
1594 }
1595 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001596 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001597 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001598 removeAppsWithoutInvalidate(appInfos);
1599 updatePageCountsAndInvalidateData();
1600 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001602 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001603 // We remove and re-add the updated applications list because it's properties may have
1604 // changed (ie. the title), and this will ensure that the items will be in their proper
1605 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001606 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001607 removeAppsWithoutInvalidate(list);
1608 addAppsWithoutInvalidate(list);
1609 updatePageCountsAndInvalidateData();
1610 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001611 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001612
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001614 // If we have reset, then we should not continue to restore the previous state
1615 mSaveInstanceStateItemIndex = -1;
1616
Adam Cohenb64d36e2011-10-17 21:48:02 -07001617 AppsCustomizeTabHost tabHost = getTabHost();
1618 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001619 if (tag != null) {
1620 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1621 tabHost.setCurrentTabFromContent(ContentType.Applications);
1622 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001623 }
Winson Chung649668f2012-01-10 13:07:16 -08001624
Adam Cohenb64d36e2011-10-17 21:48:02 -07001625 if (mCurrentPage != 0) {
1626 invalidatePageData(0);
1627 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001629
1630 private AppsCustomizeTabHost getTabHost() {
1631 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1632 }
1633
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 public void dumpState() {
1635 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001636 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001637 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001638 }
Adam Cohen4e844012011-11-09 13:48:04 -08001639
Winson Chung785d2eb2011-04-14 16:08:02 -07001640 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001641 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001642 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001643 for (Object i: list) {
1644 if (i instanceof AppWidgetProviderInfo) {
1645 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1646 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1647 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1648 + " initialLayout=" + info.initialLayout
1649 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1650 } else if (i instanceof ResolveInfo) {
1651 ResolveInfo info = (ResolveInfo) i;
1652 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1653 + info.icon);
1654 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 }
1656 }
Adam Cohen4e844012011-11-09 13:48:04 -08001657
Winson Chung785d2eb2011-04-14 16:08:02 -07001658 public void surrender() {
1659 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1660 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001661
1662 // Stop all background tasks
1663 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001664 }
Winson Chung007c6982011-06-14 13:27:53 -07001665
Winson Chunge4e50662012-01-23 14:45:13 -08001666 @Override
1667 public void iconPressed(PagedViewIcon icon) {
1668 // Reset the previously pressed icon and store a reference to the pressed icon so that
1669 // we can reset it on return to Launcher (in Launcher.onResume())
1670 if (mPressedIcon != null) {
1671 mPressedIcon.resetDrawableState();
1672 }
1673 mPressedIcon = icon;
1674 }
1675
1676 public void resetDrawableState() {
1677 if (mPressedIcon != null) {
1678 mPressedIcon.resetDrawableState();
1679 mPressedIcon = null;
1680 }
1681 }
Winson Chung68e4c642011-11-10 15:48:25 -08001682
Winson Chungb44b5242011-06-13 11:32:14 -07001683 /*
1684 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1685 * widget previews in the background with the AsyncTasks.
1686 */
Winson Chung68e4c642011-11-10 15:48:25 -08001687 final static int sLookBehindPageCount = 2;
1688 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001689 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001690 final int count = getChildCount();
1691 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1692 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1693 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001694 }
1695 protected int getAssociatedUpperPageBound(int page) {
1696 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001697 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1698 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1699 count - 1);
1700 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001701 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001702
Winson Chung6a0f57d2011-06-29 20:10:49 -07001703 protected String getCurrentPageDescription() {
1704 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1705 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001706 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001707
1708 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001709 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001710 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001711 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001712 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001713 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001714 } else {
1715 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001716 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001717
Michael Jurka8b805b12012-04-18 14:23:14 -07001718 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001719 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001720}