blob: 37cdb9e137b294f198ecd9662bcae8bd7965fdb4 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010032import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070036import android.os.Build;
37import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.AttributeSet;
40import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070041import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080042import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
44import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070048import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070050import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * A simple callback interface which also provides the results of the task.
61 */
62interface AsyncTaskCallback {
63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
64}
Winson Chung4e076542011-06-23 13:04:10 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * The data needed to perform either of the custom AsyncTasks.
68 */
69class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070070 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080071 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070072 }
73
Michael Jurka038f9d82011-11-03 13:50:45 -070074 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010075 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070076 page = p;
77 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070078 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070079 maxImageWidth = cw;
80 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070081 doInBackgroundCallback = bgR;
82 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010083 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070084 }
Winson Chung09945932011-09-20 14:22:40 -070085 void cleanup(boolean cancelled) {
86 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070087 if (generatedImages != null) {
88 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010089 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010090 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070091 }
92 }
93 generatedImages.clear();
94 }
95 }
Winson Chungb44b5242011-06-13 11:32:14 -070096 int page;
97 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070098 ArrayList<Bitmap> sourceImages;
99 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 int maxImageWidth;
101 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 AsyncTaskCallback doInBackgroundCallback;
103 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100104 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700105}
Winson Chung4e076542011-06-23 13:04:10 -0700106
Winson Chungb44b5242011-06-13 11:32:14 -0700107/**
108 * A generic template for an async task used in AppsCustomize.
109 */
110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700111 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700112 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700113 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700114 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700115 }
116 @Override
117 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
118 if (params.length != 1) return null;
119 // Load each of the widget previews in the background
120 params[0].doInBackgroundCallback.run(this, params[0]);
121 return params[0];
122 }
123 @Override
124 protected void onPostExecute(AsyncTaskPageData result) {
125 // All the widget previews are loaded, so we can just callback to inflate the page
126 result.postExecuteCallback.run(this, result);
127 }
128
129 void setThreadPriority(int p) {
130 threadPriority = p;
131 }
132 void syncThreadPriority() {
133 Process.setThreadPriority(threadPriority);
134 }
135
136 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700137 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int threadPriority;
140}
Winson Chungb44b5242011-06-13 11:32:14 -0700141
142/**
143 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
144 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700145public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700146 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700147 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
148 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700149 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700150
151 /**
152 * The different content types that this paged view can show.
153 */
154 public enum ContentType {
155 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700156 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700157 }
Winson Chungc58497e2013-09-03 17:48:37 -0700158 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700159
160 // Refs
161 private Launcher mLauncher;
162 private DragController mDragController;
163 private final LayoutInflater mLayoutInflater;
164 private final PackageManager mPackageManager;
165
Winson Chung5afbf7b2011-07-25 11:53:08 -0700166 // Save and Restore
167 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800168 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700169
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200171 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700172 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700173
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800175 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private int mClingFocusedX;
177 private int mClingFocusedY;
178
Winson Chung1ed747a2011-05-03 16:18:34 -0700179 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700180 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700181 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700182
183 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700184 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700185 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700186 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700187 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700188 private int mNumAppsPages;
189 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700190 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700191
Adam Cohen22f823d2011-09-01 17:22:18 -0700192 // Relating to the scroll and overscroll effects
193 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700194 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float TRANSITION_PIVOT = 0.65f;
197 private static float TRANSITION_MAX_ROTATION = 22;
198 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700199 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700200 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700201
Winson Chungb44b5242011-06-13 11:32:14 -0700202 // Previews & outlines
203 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800204 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700205
Adam Cohened66b2b2012-01-23 17:28:51 -0800206 private Runnable mInflateWidgetRunnable = null;
207 private Runnable mBindWidgetRunnable = null;
208 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700209 static final int WIDGET_PRELOAD_PENDING = 0;
210 static final int WIDGET_BOUND = 1;
211 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800212 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
213 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800214 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800215 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800216
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700217 private Toast mWidgetInstructionToast;
218
Michael Jurka39e5d172012-03-12 18:36:12 -0700219 // Deferral of loading widget previews during launcher transitions
220 private boolean mInTransition;
221 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
222 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700223 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
224 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700225
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700226 private Rect mTmpRect = new Rect();
227
Michael Jurkadac85912012-05-18 15:04:49 -0700228 // Used for drawing shortcut previews
229 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
230 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
231 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
232
233 // Used for drawing widget previews
234 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
235 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
236 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
237 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
238
Michael Jurka05713af2013-01-23 12:39:24 +0100239 WidgetPreviewLoader mWidgetPreviewLoader;
240
Michael Jurkac402cd92013-05-20 15:49:32 +0200241 private boolean mInBulkBind;
242 private boolean mNeedToUpdatePageCountsAndInvalidateData;
243
Winson Chung785d2eb2011-04-14 16:08:02 -0700244 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
245 super(context, attrs);
246 mLayoutInflater = LayoutInflater.from(context);
247 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200248 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700249 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400250 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700251 mCanvas = new Canvas();
252 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253
254 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800255 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700256 LauncherAppState app = LauncherAppState.getInstance();
257 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
258 mWidgetWidthGap = mWidgetHeightGap = grid.edgeMarginPx;
Winson Chung4b576be2011-04-27 17:40:20 -0700259 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
260 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
262 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700263 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700265
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 // The padding on the non-matched dimension for the default widget preview icons
267 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700268 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700269
270 // Unless otherwise specified this view is important for accessibility.
271 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
272 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
273 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700274 }
275
276 @Override
277 protected void init() {
278 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700279 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280
281 Context context = getContext();
282 Resources r = context.getResources();
283 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
284 }
285
Winson Chungc58497e2013-09-03 17:48:37 -0700286 public void onFinishInflate() {
287 super.onFinishInflate();
288
289 LauncherAppState app = LauncherAppState.getInstance();
290 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
291 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
292 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
293 }
294
Winson Chung67ca7e42013-10-31 16:53:19 -0700295 void setAllAppsPadding(Rect r) {
296 mAllAppsPadding.set(r);
297 }
298 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
299 mPageLayoutPaddingBottom = pageIndicatorHeight;
300 }
301
Winson Chung5afbf7b2011-07-25 11:53:08 -0700302 /** Returns the item index of the center item on this page so that we can restore to this
303 * item index when we rotate. */
304 private int getMiddleComponentIndexOnCurrentPage() {
305 int i = -1;
306 if (getPageCount() > 0) {
307 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700308 if (mContentType == ContentType.Applications) {
309 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
310 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 int numItemsPerPage = mCellCountX * mCellCountY;
312 int childCount = childrenLayout.getChildCount();
313 if (childCount > 0) {
314 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700315 }
Winson Chungc58497e2013-09-03 17:48:37 -0700316 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700317 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700318 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700319 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
320 int childCount = layout.getChildCount();
321 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700322 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700323 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 }
Winson Chungc58497e2013-09-03 17:48:37 -0700325 } else {
326 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700327 }
328 }
329 return i;
330 }
331
332 /** Get the index of the item to restore to if we need to restore the current page. */
333 int getSaveInstanceStateIndex() {
334 if (mSaveInstanceStateItemIndex == -1) {
335 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
336 }
337 return mSaveInstanceStateItemIndex;
338 }
339
340 /** Returns the page in the current orientation which is expected to contain the specified
341 * item index. */
342 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700343 if (index < 0) return 0;
344
345 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700346 int numItemsPerPage = mCellCountX * mCellCountY;
347 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700348 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700349 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700350 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700351 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700352 }
353
Winson Chung5afbf7b2011-07-25 11:53:08 -0700354 /** Restores the page for an item at the specified index */
355 void restorePageForIndex(int index) {
356 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700357 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700358 }
359
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700360 private void updatePageCounts() {
361 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
362 (float) (mWidgetCountX * mWidgetCountY));
363 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
364 }
365
Winson Chungf0ea4d32011-06-06 14:27:16 -0700366 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100367 if (mWidgetPreviewLoader == null) {
368 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
369 }
370
Winson Chungf0ea4d32011-06-06 14:27:16 -0700371 // Now that the data is ready, we can calculate the content width, the number of cells to
372 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700373 LauncherAppState app = LauncherAppState.getInstance();
374 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
376 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungc58497e2013-09-03 17:48:37 -0700377 mCellCountX = (int) grid.allAppsNumCols;
378 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700379 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700380
Winson Chungdb1138b2011-06-30 14:39:35 -0700381 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700382 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
383 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
384 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
385 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700386 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700387
Michael Jurkae326f182011-11-21 14:05:46 -0800388 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
389 final boolean hostIsTransitioning = host.isTransitioning();
390
Adam Cohen0cd3b642011-10-14 14:58:00 -0700391 // Restore the page
392 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800393 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700394
Winson Chung3f4e1422011-11-17 14:58:51 -0800395 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
396 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
397 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800398 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800399 post(new Runnable() {
400 @Override
401 public void run() {
402 showAllAppsCling();
403 }
404 });
405 }
406 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700407
Winson Chung3f4e1422011-11-17 14:58:51 -0800408 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700409 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800410 mHasShownAllAppsCling = true;
411 // Calculate the position for the cling punch through
412 int[] offset = new int[2];
413 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
414 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
415 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800416 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800417 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
418 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800419 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800420 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700421 }
422
423 @Override
424 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
425 int width = MeasureSpec.getSize(widthMeasureSpec);
426 int height = MeasureSpec.getSize(heightMeasureSpec);
427 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800428 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700429 setDataIsReady();
430 setMeasuredDimension(width, height);
431 onDataReady(width, height);
432 }
433 }
434
435 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
436 }
437
Michael Jurkac402cd92013-05-20 15:49:32 +0200438 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700439 LauncherAppState app = LauncherAppState.getInstance();
440 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
441
Winson Chung1ed747a2011-05-03 16:18:34 -0700442 // Get the list of widgets and shortcuts
443 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200444 for (Object o : widgetsAndShortcuts) {
445 if (o instanceof AppWidgetProviderInfo) {
446 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100447 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
448 continue;
449 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200450 widget.label = widget.label.trim();
451 if (widget.minWidth > 0 && widget.minHeight > 0) {
452 // Ensure that all widgets we show can be added on a workspace of this size
453 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
454 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
455 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
456 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700457 if (minSpanX <= (int) grid.numColumns &&
458 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200459 mWidgets.add(widget);
460 } else {
461 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
462 widget.minWidth + ", " + widget.minHeight + ")");
463 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700464 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200465 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
466 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700467 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800468 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200469 // just add shortcuts
470 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800471 }
472 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200473 updatePageCountsAndInvalidateData();
474 }
475
476 public void setBulkBind(boolean bulkBind) {
477 if (bulkBind) {
478 mInBulkBind = true;
479 } else {
480 mInBulkBind = false;
481 if (mNeedToUpdatePageCountsAndInvalidateData) {
482 updatePageCountsAndInvalidateData();
483 }
484 }
485 }
486
487 private void updatePageCountsAndInvalidateData() {
488 if (mInBulkBind) {
489 mNeedToUpdatePageCountsAndInvalidateData = true;
490 } else {
491 updatePageCounts();
492 invalidateOnDataChange();
493 mNeedToUpdatePageCountsAndInvalidateData = false;
494 }
Winson Chung4b576be2011-04-27 17:40:20 -0700495 }
496
497 @Override
498 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700499 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700500 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700501 mLauncher.getWorkspace().isSwitchingState()) return;
502
Winson Chung4b576be2011-04-27 17:40:20 -0700503 if (v instanceof PagedViewIcon) {
504 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200505 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800506
507 // Lock the drawable state to pressed until we return to Launcher
508 if (mPressedIcon != null) {
509 mPressedIcon.lockDrawableState();
510 }
Winson Chungc7450e32012-04-17 17:34:08 -0700511 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
Anton Hanssona2f665f2013-09-26 12:18:32 +0100512 mLauncher.getStats().recordLaunch(appInfo.intent);
Winson Chung4b576be2011-04-27 17:40:20 -0700513 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700514 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700515 if (mWidgetInstructionToast != null) {
516 mWidgetInstructionToast.cancel();
517 }
518 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
519 Toast.LENGTH_SHORT);
520 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700521
Winson Chungd2e87b32011-06-02 10:53:07 -0700522 // Create a little animation to show that the widget can move
523 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
524 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700525 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
526 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700527 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700528 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700529 tydAnim.setDuration(100);
530 bounce.play(tyuAnim).before(tydAnim);
531 bounce.setInterpolator(new AccelerateInterpolator());
532 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700533 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700534 }
535
Winson Chungc6f10b92011-11-14 11:39:07 -0800536 public boolean onKey(View v, int keyCode, KeyEvent event) {
537 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
538 }
539
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 /*
541 * PagedViewWithDraggableItems implementation
542 */
543 @Override
544 protected void determineDraggingStart(android.view.MotionEvent ev) {
545 // Disable dragging by pulling an app down for now.
546 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700547
Winson Chung4b576be2011-04-27 17:40:20 -0700548 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700549 mLauncher.getWorkspace().onDragStartedWithItem(v);
550 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700551 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700552
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700553 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
554 Bundle options = null;
555 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
556 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700557 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
558 info.componentName, null);
559
560 float density = getResources().getDisplayMetrics().density;
561 int xPaddingDips = (int) ((padding.left + padding.right) / density);
562 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
563
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700564 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700565 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
566 mTmpRect.left - xPaddingDips);
567 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
568 mTmpRect.top - yPaddingDips);
569 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
570 mTmpRect.right - xPaddingDips);
571 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
572 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700573 }
574 return options;
575 }
576
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700577 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700579 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
580
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700582 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 return;
584 }
585
Adam Cohen21a170b2012-05-30 15:17:06 -0700586 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800587 mBindWidgetRunnable = new Runnable() {
588 @Override
589 public void run() {
590 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700591 // Options will be null for platforms with JB or lower, so this serves as an
592 // SDK level check.
593 if (options == null) {
594 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
595 mWidgetLoadingId, info.componentName)) {
596 mWidgetCleanupState = WIDGET_BOUND;
597 }
598 } else {
599 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
600 mWidgetLoadingId, info.componentName, options)) {
601 mWidgetCleanupState = WIDGET_BOUND;
602 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800604 }
605 };
606 post(mBindWidgetRunnable);
607
608 mInflateWidgetRunnable = new Runnable() {
609 @Override
610 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700611 if (mWidgetCleanupState != WIDGET_BOUND) {
612 return;
613 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700614 AppWidgetHostView hostView = mLauncher.
615 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 info.boundWidget = hostView;
617 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800618 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700619 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
620 info.spanY, info, false);
621
622 // We want the first widget layout to be the correct size. This will be important
623 // for width size reporting to the AppWidgetManager.
624 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
625 unScaledSize[1]);
626 lp.x = lp.y = 0;
627 lp.customPosition = true;
628 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800629 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 }
631 };
632 post(mInflateWidgetRunnable);
633 }
634
635 @Override
636 public void onShortPress(View v) {
637 // We are anticipating a long press, and we use this time to load bind and instantiate
638 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700639 if (mCreateWidgetInfo != null) {
640 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
641 cleanupWidgetPreloading(false);
642 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800643 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700644 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800645 }
646
Adam Cohen0e56cc92012-05-11 15:57:05 -0700647 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
648 if (!widgetWasAdded) {
649 // If the widget was not added, we may need to do further cleanup.
650 PendingAddWidgetInfo info = mCreateWidgetInfo;
651 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700652
653 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700654 // We never did any preloading, so just remove pending callbacks to do so
655 removeCallbacks(mBindWidgetRunnable);
656 removeCallbacks(mInflateWidgetRunnable);
657 } else if (mWidgetCleanupState == WIDGET_BOUND) {
658 // Delete the widget id which was allocated
659 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700660 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
661 }
662
663 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700664 removeCallbacks(mInflateWidgetRunnable);
665 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700666 // Delete the widget id which was allocated
667 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700668 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
669 }
670
Adam Cohen0e56cc92012-05-11 15:57:05 -0700671 // The widget was inflated and added to the DragLayer -- remove it.
672 AppWidgetHostView widget = info.boundWidget;
673 mLauncher.getDragLayer().removeView(widget);
674 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 }
676 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
677 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700678 mCreateWidgetInfo = null;
679 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 }
681
Adam Cohen7a326642012-02-22 12:03:22 -0800682 @Override
683 public void cleanUpShortPress(View v) {
684 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700685 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800686 }
687 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800688
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700689 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800690 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700691 // Get the widget preview as the drag representation
692 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700693 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700694
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700695 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
696 // we abort the drag.
697 if (image.getDrawable() == null) {
698 mDraggingWidget = false;
699 return false;
700 }
701
Winson Chung4b576be2011-04-27 17:40:20 -0700702 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800703 Bitmap preview;
704 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800705 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100706 Point previewPadding = null;
707
Winson Chung1ed747a2011-05-03 16:18:34 -0700708 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700709 // This can happen in some weird cases involving multi-touch. We can't start dragging
710 // the widget if this is null, so we break out.
711 if (mCreateWidgetInfo == null) {
712 return false;
713 }
714
Adam Cohen1b36dc32012-02-13 19:27:37 -0800715 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
716 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700717 int spanX = createItemInfo.spanX;
718 int spanY = createItemInfo.spanY;
719 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
720 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700721
Winson Chung72d59842012-02-22 13:51:36 -0800722 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
723 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700724 int maxWidth, maxHeight;
725 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
726 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800727
Michael Jurka05713af2013-01-23 12:39:24 +0100728 int[] previewSizeBeforeScale = new int[1];
729
730 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
731 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
732 maxWidth, maxHeight, null, previewSizeBeforeScale);
733
734 // Compare the size of the drag preview to the preview in the AppsCustomize tray
735 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
736 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
737 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
738
739 // The bitmap in the AppsCustomize tray is always the the same size, so there
740 // might be extra pixels around the preview itself - this accounts for that
741 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
742 int padding =
743 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
744 previewPadding = new Point(padding, 0);
745 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700746 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700747 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
748 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
749 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
750 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
751
Winson Chung1120e032011-11-22 16:11:31 -0800752 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700753 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100754 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700755 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700756 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700757 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700758 createItemInfo.spanX = createItemInfo.spanY = 1;
759 }
Winson Chung4b576be2011-04-27 17:40:20 -0700760
Michael Jurka8c3339b2012-06-14 16:18:21 -0700761 // Don't clip alpha values for the drag outline if we're using the default widget preview
762 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
763 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700764
Winson Chung1120e032011-11-22 16:11:31 -0800765 // Save the preview for the outline generation, then dim the preview
766 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
767 false);
Winson Chung1120e032011-11-22 16:11:31 -0800768
Winson Chung4b576be2011-04-27 17:40:20 -0700769 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700770 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700771 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800772 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100773 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800774 outline.recycle();
775 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700776 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700777 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800778
Winson Chung4b576be2011-04-27 17:40:20 -0700779 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700781 if (!super.beginDragging(v)) return false;
782
783 if (v instanceof PagedViewIcon) {
784 beginDraggingApplication(v);
785 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700786 if (!beginDraggingWidget(v)) {
787 return false;
788 }
Winson Chung4b576be2011-04-27 17:40:20 -0700789 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800790
791 // We delay entering spring-loaded mode slightly to make sure the UI
792 // thready is free of any work.
793 postDelayed(new Runnable() {
794 @Override
795 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800796 // We don't enter spring-loaded mode if the drag has been cancelled
797 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800798 // Reset the alpha on the dragged icon before we drag
799 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800800
Adam Cohen1b36dc32012-02-13 19:27:37 -0800801 // Go into spring loaded mode (must happen before we startDrag())
802 mLauncher.enterSpringLoadedDragMode();
803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 }
Winson Chung72d59842012-02-22 13:51:36 -0800805 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800806
Winson Chung785d2eb2011-04-14 16:08:02 -0700807 return true;
808 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800809
Winson Chunga48487a2012-03-20 16:19:37 -0700810 /**
811 * Clean up after dragging.
812 *
813 * @param target where the item was dragged to (can be null if the item was flung)
814 */
815 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700816 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700817 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700818 // Exit spring loaded mode if we have not successfully dropped or have not handled the
819 // drop in Workspace
Adam Cohene97a3b32013-10-23 16:11:50 -0700820 mLauncher.getWorkspace().removeExtraEmptyScreen(true, new Runnable() {
821 @Override
822 public void run() {
823 mLauncher.exitSpringLoadedDragMode();
824 mLauncher.unlockScreenOrientation(false);
825 }
826 });
827 } else {
828 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700829 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700830 }
831
Winson Chung785d2eb2011-04-14 16:08:02 -0700832 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700833 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700834 if (getChildCount() > 0) {
835 return getChildAt(0);
836 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700837 return null;
838 }
839
840 @Override
841 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700842 mInTransition = true;
843 if (toWorkspace) {
844 cancelAllTasks();
845 }
846 }
847
848 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700849 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700850 }
851
852 @Override
853 public void onLauncherTransitionStep(Launcher l, float t) {
854 }
855
856 @Override
857 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
858 mInTransition = false;
859 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700860 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700861 }
862 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700863 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
864 r.run();
865 }
866 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700867 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700868 }
869
870 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700871 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
872 boolean success) {
873 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
874 if (isFlingToDelete) return;
875
876 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700877
878 // Display an error message if the drag failed due to there not being enough space on the
879 // target layout we were dropping on.
880 if (!success) {
881 boolean showOutOfSpaceMessage = false;
882 if (target instanceof Workspace) {
883 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
884 Workspace workspace = (Workspace) target;
885 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700886 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700887 if (layout != null) {
888 layout.calculateSpans(itemInfo);
889 showOutOfSpaceMessage =
890 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
891 }
892 }
Winson Chungfc79c802011-05-02 13:35:34 -0700893 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700894 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700895 }
Adam Cohen7a326642012-02-22 12:03:22 -0800896
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800897 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700898 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700899 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800900 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700901 }
902
Winson Chunga48487a2012-03-20 16:19:37 -0700903 @Override
904 public void onFlingToDeleteCompleted() {
905 // We just dismiss the drag when we fling, so cleanup here
906 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700907 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700908 mDraggingWidget = false;
909 }
910
911 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800912 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700913 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800914 }
915
Winson Chung7f0acdd2011-09-19 18:34:19 -0700916 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000917 public boolean supportsAppInfoDropTarget() {
918 return true;
919 }
920
921 @Override
922 public boolean supportsDeleteDropTarget() {
923 return false;
924 }
925
926 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800927 public float getIntrinsicIconScaleFactor() {
928 LauncherAppState app = LauncherAppState.getInstance();
929 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
930 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
931 }
932
933 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700934 protected void onDetachedFromWindow() {
935 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700936 cancelAllTasks();
937 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700938
Michael Jurkae326f182011-11-21 14:05:46 -0800939 public void clearAllWidgetPages() {
940 cancelAllTasks();
941 int count = getChildCount();
942 for (int i = 0; i < count; i++) {
943 View v = getPageAt(i);
944 if (v instanceof PagedViewGridLayout) {
945 ((PagedViewGridLayout) v).removeAllViewsOnPage();
946 mDirtyPageContent.set(i, true);
947 }
948 }
949 }
950
Adam Cohen0cd3b642011-10-14 14:58:00 -0700951 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700952 // Clean up all the async tasks
953 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
954 while (iter.hasNext()) {
955 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
956 task.cancel(false);
957 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700958 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700959
960 // We've already preallocated the views for the data to load into, so clear them as well
961 View v = getPageAt(task.page);
962 if (v instanceof PagedViewGridLayout) {
963 ((PagedViewGridLayout) v).removeAllViewsOnPage();
964 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700965 }
Winson Chung83687b12012-04-25 16:01:01 -0700966 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700967 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700968 }
969
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200971 // Widgets appear to be cleared every time you leave, always force invalidate for them
972 if (mContentType != type || type == ContentType.Widgets) {
973 int page = (mContentType != type) ? 0 : getCurrentPage();
974 mContentType = type;
975 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700976 }
Winson Chungc58497e2013-09-03 17:48:37 -0700977 }
978
979 public ContentType getContentType() {
980 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700981 }
982
Adam Cohen0cd3b642011-10-14 14:58:00 -0700983 protected void snapToPage(int whichPage, int delta, int duration) {
984 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800985
986 // Update the thread priorities given the direction lookahead
987 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
988 while (iter.hasNext()) {
989 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700990 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800991 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
992 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
993 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
994 } else {
995 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
996 }
997 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700998 }
999
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 /*
1001 * Apps PagedView implementation
1002 */
Winson Chung63257c12011-05-05 17:06:13 -07001003 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1004 int childCount = layout.getChildCount();
1005 for (int i = 0; i < childCount; ++i) {
1006 layout.getChildAt(i).setVisibility(visibility);
1007 }
1008 }
Winson Chungc58497e2013-09-03 17:48:37 -07001009 private void setupPage(AppsCustomizeCellLayout layout) {
1010 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -07001011
Winson Chung63257c12011-05-05 17:06:13 -07001012 // Note: We force a measure here to get around the fact that when we do layout calculations
1013 // immediately after syncing, we don't have a proper width. That said, we already know the
1014 // expected page width, so we can actually optimize by hiding all the TextView-based
1015 // children that are expensive to measure, and let that happen naturally later.
1016 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -07001017 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1018 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001019 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001020 layout.measure(widthSpec, heightSpec);
Winson Chung67ca7e42013-10-31 16:53:19 -07001021 layout.setPadding(mAllAppsPadding.left, mAllAppsPadding.top, mAllAppsPadding.right,
1022 mAllAppsPadding.bottom);
Winson Chung63257c12011-05-05 17:06:13 -07001023 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001024 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001025
Winson Chungf314b0e2011-08-16 11:54:27 -07001026 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001028 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 int numCells = mCellCountX * mCellCountY;
1030 int startIndex = page * numCells;
1031 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -07001032 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001033
Winson Chung785d2eb2011-04-14 16:08:02 -07001034 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001035 ArrayList<Object> items = new ArrayList<Object>();
1036 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001038 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001039 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1040 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001041 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001042 icon.setOnClickListener(this);
1043 icon.setOnLongClickListener(this);
1044 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001045 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001046
1047 int index = i - startIndex;
1048 int x = index % mCellCountX;
1049 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001050 if (isRtl) {
1051 x = mCellCountX - x - 1;
1052 }
Winson Chungc58497e2013-09-03 17:48:37 -07001053 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001054
1055 items.add(info);
1056 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001057 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001058
Michael Jurka47639b92013-01-14 12:42:27 +01001059 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 }
Winson Chungb44b5242011-06-13 11:32:14 -07001061
1062 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001063 * A helper to return the priority for loading of the specified widget page.
1064 */
1065 private int getWidgetPageLoadPriority(int page) {
1066 // If we are snapping to another page, use that index as the target page index
1067 int toPage = mCurrentPage;
1068 if (mNextPage > -1) {
1069 toPage = mNextPage;
1070 }
1071
1072 // We use the distance from the target page as an initial guess of priority, but if there
1073 // are no pages of higher priority than the page specified, then bump up the priority of
1074 // the specified page.
1075 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1076 int minPageDiff = Integer.MAX_VALUE;
1077 while (iter.hasNext()) {
1078 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001079 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001080 }
1081
1082 int rawPageDiff = Math.abs(page - toPage);
1083 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1084 }
1085 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001086 * Return the appropriate thread priority for loading for a given page (we give the current
1087 * page much higher priority)
1088 */
1089 private int getThreadPriorityForPage(int page) {
1090 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001091 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001092 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001093 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001094 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001095 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001096 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001097 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001098 }
1099 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001100 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001101 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001102 return Math.max(0, pageDiff * sPageSleepDelay);
1103 }
Winson Chungb44b5242011-06-13 11:32:14 -07001104 /**
1105 * Creates and executes a new AsyncTask to load a page of widget previews.
1106 */
1107 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001108 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001109
Winson Chungb44b5242011-06-13 11:32:14 -07001110 // Prune all tasks that are no longer needed
1111 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1112 while (iter.hasNext()) {
1113 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001114 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001115 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1116 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001117 task.cancel(false);
1118 iter.remove();
1119 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001120 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001121 }
1122 }
1123
Winson Chungf314b0e2011-08-16 11:54:27 -07001124 // 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 -07001125 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001126 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001127 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001128 @Override
1129 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001130 try {
Winson Chung09945932011-09-20 14:22:40 -07001131 try {
1132 Thread.sleep(sleepMs);
1133 } catch (Exception e) {}
1134 loadWidgetPreviewsInBackground(task, data);
1135 } finally {
1136 if (task.isCancelled()) {
1137 data.cleanup(true);
1138 }
1139 }
Winson Chungb44b5242011-06-13 11:32:14 -07001140 }
1141 },
1142 new AsyncTaskCallback() {
1143 @Override
1144 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001145 mRunningTasks.remove(task);
1146 if (task.isCancelled()) return;
1147 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001148 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001149 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001150 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001151
1152 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001153 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001154 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001155 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001156 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1157 mRunningTasks.add(t);
1158 }
Winson Chungb44b5242011-06-13 11:32:14 -07001159
Winson Chung785d2eb2011-04-14 16:08:02 -07001160 /*
1161 * Widgets PagedView implementation
1162 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001163 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001164 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001165 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001166 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1167 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001168 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001169 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001170 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001171
Michael Jurka038f9d82011-11-03 13:50:45 -07001172 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001173 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001174
Winson Chungd2945262011-06-24 15:22:14 -07001175 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001176 final ArrayList<Object> items = new ArrayList<Object>();
Winson Chungc58497e2013-09-03 17:48:37 -07001177 int contentWidth = mContentWidth;
Michael Jurka038f9d82011-11-03 13:50:45 -07001178 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001179 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungc58497e2013-09-03 17:48:37 -07001180 int contentHeight = mContentHeight;
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001182 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001183
Winson Chunge4a647f2011-09-30 14:41:25 -07001184 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001185 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001186 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1187 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001188 }
1189
Winson Chunge4a647f2011-09-30 14:41:25 -07001190 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001191 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001192 layout.setColumnCount(layout.getCellCountX());
1193 for (int i = 0; i < items.size(); ++i) {
1194 Object rawInfo = items.get(i);
1195 PendingAddItemInfo createItemInfo = null;
1196 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1197 R.layout.apps_customize_widget, layout, false);
1198 if (rawInfo instanceof AppWidgetProviderInfo) {
1199 // Fill in the widget information
1200 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1201 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001202
1203 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001204 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001205 createItemInfo.spanX = spanXY[0];
1206 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001207 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001208 createItemInfo.minSpanX = minSpanXY[0];
1209 createItemInfo.minSpanY = minSpanXY[1];
1210
Michael Jurka3f4e0702013-02-05 11:21:28 +01001211 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001212 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001213 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001214 } else if (rawInfo instanceof ResolveInfo) {
1215 // Fill in the shortcuts information
1216 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001217 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001218 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1219 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1220 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001221 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001222 widget.setTag(createItemInfo);
1223 }
1224 widget.setOnClickListener(this);
1225 widget.setOnLongClickListener(this);
1226 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001227 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001228
1229 // Layout each widget
1230 int ix = i % mWidgetCountX;
1231 int iy = i / mWidgetCountX;
1232 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001233 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001234 GridLayout.spec(ix, GridLayout.TOP));
1235 lp.width = cellWidth;
1236 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001237 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001238 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1239 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1240 layout.addView(widget, lp);
1241 }
1242
Michael Jurka038f9d82011-11-03 13:50:45 -07001243 // wait until a call on onLayout to start loading, because
1244 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1245 // TODO: can we do a measure/layout immediately?
1246 layout.setOnLayoutListener(new Runnable() {
1247 public void run() {
1248 // Load the widget previews
1249 int maxPreviewWidth = cellWidth;
1250 int maxPreviewHeight = cellHeight;
1251 if (layout.getChildCount() > 0) {
1252 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1253 int[] maxSize = w.getPreviewSize();
1254 maxPreviewWidth = maxSize[0];
1255 maxPreviewHeight = maxSize[1];
1256 }
Michael Jurka05713af2013-01-23 12:39:24 +01001257
Michael Jurka3f4e0702013-02-05 11:21:28 +01001258 mWidgetPreviewLoader.setPreviewSize(
1259 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001260 if (immediate) {
1261 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001262 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001263 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001264 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001265 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001266 if (mInTransition) {
1267 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1268 } else {
1269 prepareLoadWidgetPreviewsTask(page, items,
1270 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1271 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001272 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001273 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001274 }
1275 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001276 }
1277 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1278 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001279 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1280 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001281 if (task != null) {
1282 // Ensure that this task starts running at the correct priority
1283 task.syncThreadPriority();
1284 }
1285
1286 // Load each of the widget/shortcut previews
1287 ArrayList<Object> items = data.items;
1288 ArrayList<Bitmap> images = data.generatedImages;
1289 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001290 for (int i = 0; i < count; ++i) {
1291 if (task != null) {
1292 // Ensure we haven't been cancelled yet
1293 if (task.isCancelled()) break;
1294 // Before work on each item, ensure that this task is running at the correct
1295 // priority
1296 task.syncThreadPriority();
1297 }
1298
Michael Jurka05713af2013-01-23 12:39:24 +01001299 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001300 }
Winson Chungb44b5242011-06-13 11:32:14 -07001301 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001302
Winson Chung7bb37522013-10-28 11:07:57 -07001303 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1304 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001305 mDeferredSyncWidgetPageItems.add(data);
1306 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001307 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001308 try {
1309 int page = data.page;
1310 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001311
Michael Jurka39e5d172012-03-12 18:36:12 -07001312 ArrayList<Object> items = data.items;
1313 int count = items.size();
1314 for (int i = 0; i < count; ++i) {
1315 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1316 if (widget != null) {
1317 Bitmap preview = data.generatedImages.get(i);
1318 widget.applyPreview(new FastBitmapDrawable(preview), i);
1319 }
1320 }
Winson Chung68e4c642011-11-10 15:48:25 -08001321
Michael Jurka47639b92013-01-14 12:42:27 +01001322 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001323
1324 // Update all thread priorities
1325 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1326 while (iter.hasNext()) {
1327 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1328 int pageIndex = task.page;
1329 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1330 }
1331 } finally {
1332 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001333 }
Winson Chungb44b5242011-06-13 11:32:14 -07001334 }
Winson Chung46af2e82011-05-09 16:00:53 -07001335
Winson Chung785d2eb2011-04-14 16:08:02 -07001336 @Override
1337 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001338 disablePagedViewAnimations();
1339
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001341 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001342
Adam Cohen0cd3b642011-10-14 14:58:00 -07001343 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001344 if (mContentType == ContentType.Applications) {
1345 for (int i = 0; i < mNumAppsPages; ++i) {
1346 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1347 setupPage(layout);
1348 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1349 LayoutParams.MATCH_PARENT));
1350 }
1351 } else if (mContentType == ContentType.Widgets) {
1352 for (int j = 0; j < mNumWidgetPages; ++j) {
1353 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1354 mWidgetCountY);
1355 setupPage(layout);
1356 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1357 LayoutParams.MATCH_PARENT));
1358 }
1359 } else {
1360 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001361 }
1362
Winson Chungc58497e2013-09-03 17:48:37 -07001363 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001364 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001365
Winson Chung785d2eb2011-04-14 16:08:02 -07001366 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001367 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001368 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001369 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001370 } else {
1371 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001372 }
1373 }
1374
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1376 // it is in the z-order. This is important to insure touch events are handled correctly.
1377 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001378 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001379 }
1380
Adam Cohenae4f1552011-10-20 00:15:42 -07001381 @Override
1382 protected int indexToPage(int index) {
1383 return getChildCount() - index - 1;
1384 }
1385
Adam Cohen22f823d2011-09-01 17:22:18 -07001386 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1387 @Override
1388 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001389 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001390 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001391
1392 for (int i = 0; i < getChildCount(); i++) {
1393 View v = getPageAt(i);
1394 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001395 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001396
Winson Chung52aee602013-01-30 12:01:02 -08001397 float interpolatedProgress;
1398 float translationX;
1399 float maxScrollProgress = Math.max(0, scrollProgress);
1400 float minScrollProgress = Math.min(0, scrollProgress);
1401
1402 if (isRtl) {
1403 translationX = maxScrollProgress * v.getMeasuredWidth();
1404 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1405 } else {
1406 translationX = minScrollProgress * v.getMeasuredWidth();
1407 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1408 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001409 float scale = (1 - interpolatedProgress) +
1410 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001411
Adam Cohen2591f6a2011-10-25 14:36:40 -07001412 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001413 if (isRtl && (scrollProgress > 0)) {
1414 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1415 } else if (!isRtl && (scrollProgress < 0)) {
1416 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001417 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001418 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001419 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1420 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001421
1422 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1423 int pageWidth = v.getMeasuredWidth();
1424 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001425
1426 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001427 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1428 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1429 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1430
1431 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001432 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001433 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001434 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1435 scale = 1.0f;
1436 alpha = 1.0f;
1437 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001438 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001439 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001440 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001441 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001442 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1443 scale = 1.0f;
1444 alpha = 1.0f;
1445 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001446 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001447 } else {
1448 v.setPivotY(pageHeight / 2.0f);
1449 v.setPivotX(pageWidth / 2.0f);
1450 v.setRotationY(0f);
1451 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001452 }
1453
1454 v.setTranslationX(translationX);
1455 v.setScaleX(scale);
1456 v.setScaleY(scale);
1457 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001458
1459 // If the view has 0 alpha, we set it to be invisible so as to prevent
1460 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001461 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001462 v.setVisibility(INVISIBLE);
1463 } else if (v.getVisibility() != VISIBLE) {
1464 v.setVisibility(VISIBLE);
1465 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001466 }
1467 }
Michael Jurka47639b92013-01-14 12:42:27 +01001468
1469 enableHwLayersOnVisiblePages();
1470 }
1471
1472 private void enableHwLayersOnVisiblePages() {
1473 final int screenCount = getChildCount();
1474
1475 getVisiblePages(mTempVisiblePagesRange);
1476 int leftScreen = mTempVisiblePagesRange[0];
1477 int rightScreen = mTempVisiblePagesRange[1];
1478 int forceDrawScreen = -1;
1479 if (leftScreen == rightScreen) {
1480 // make sure we're caching at least two pages always
1481 if (rightScreen < screenCount - 1) {
1482 rightScreen++;
1483 forceDrawScreen = rightScreen;
1484 } else if (leftScreen > 0) {
1485 leftScreen--;
1486 forceDrawScreen = leftScreen;
1487 }
1488 } else {
1489 forceDrawScreen = leftScreen + 1;
1490 }
1491
1492 for (int i = 0; i < screenCount; i++) {
1493 final View layout = (View) getPageAt(i);
1494 if (!(leftScreen <= i && i <= rightScreen &&
1495 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1496 layout.setLayerType(LAYER_TYPE_NONE, null);
1497 }
1498 }
1499
Michael Jurka47639b92013-01-14 12:42:27 +01001500 for (int i = 0; i < screenCount; i++) {
1501 final View layout = (View) getPageAt(i);
1502
1503 if (leftScreen <= i && i <= rightScreen &&
1504 (i == forceDrawScreen || shouldDrawChild(layout))) {
1505 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1506 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1507 }
1508 }
1509 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001510 }
1511
1512 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001513 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001514 }
1515
Winson Chung785d2eb2011-04-14 16:08:02 -07001516 /**
1517 * Used by the parent to get the content width to set the tab bar to
1518 * @return
1519 */
1520 public int getPageContentWidth() {
1521 return mContentWidth;
1522 }
1523
Winson Chungb26f3d62011-06-02 10:49:29 -07001524 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001525 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001526 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001527 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001528 // We reset the save index when we change pages so that it will be recalculated on next
1529 // rotation
1530 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001531 }
1532
Winson Chung785d2eb2011-04-14 16:08:02 -07001533 /*
1534 * AllAppsView implementation
1535 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 public void setup(Launcher launcher, DragController dragController) {
1537 mLauncher = launcher;
1538 mDragController = dragController;
1539 }
Winson Chung9802ac92012-06-08 16:01:58 -07001540
1541 /**
1542 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1543 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1544 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1545 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1546 */
1547 private void invalidateOnDataChange() {
1548 if (!isDataReady()) {
1549 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1550 // request a layout to trigger the page data when ready.
1551 requestLayout();
1552 } else {
1553 cancelAllTasks();
1554 invalidatePageData();
1555 }
1556 }
1557
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001558 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001559 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001560 mApps = list;
1561 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1562 updatePageCountsAndInvalidateData();
1563 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001565 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 // We add it in place, in alphabetical order
1567 int count = list.size();
1568 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001569 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001570 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001571 if (index < 0) {
1572 mApps.add(-(index + 1), info);
1573 }
1574 }
1575 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001576 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001577 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001578 addAppsWithoutInvalidate(list);
1579 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001580 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001582 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001583 ComponentName removeComponent = item.intent.getComponent();
1584 int length = list.size();
1585 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001586 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001587 if (info.intent.getComponent().equals(removeComponent)) {
1588 return i;
1589 }
1590 }
1591 return -1;
1592 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001593 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001594 // loop through all the apps and remove apps that have the same component
1595 int length = list.size();
1596 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001597 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001598 int removeIndex = findAppByComponent(mApps, info);
1599 if (removeIndex > -1) {
1600 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 }
1602 }
1603 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001604 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001605 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001606 removeAppsWithoutInvalidate(appInfos);
1607 updatePageCountsAndInvalidateData();
1608 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001609 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001610 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001611 // We remove and re-add the updated applications list because it's properties may have
1612 // changed (ie. the title), and this will ensure that the items will be in their proper
1613 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001614 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001615 removeAppsWithoutInvalidate(list);
1616 addAppsWithoutInvalidate(list);
1617 updatePageCountsAndInvalidateData();
1618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001620
Winson Chung785d2eb2011-04-14 16:08:02 -07001621 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001622 // If we have reset, then we should not continue to restore the previous state
1623 mSaveInstanceStateItemIndex = -1;
1624
Adam Cohenb64d36e2011-10-17 21:48:02 -07001625 AppsCustomizeTabHost tabHost = getTabHost();
1626 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001627 if (tag != null) {
1628 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1629 tabHost.setCurrentTabFromContent(ContentType.Applications);
1630 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001631 }
Winson Chung649668f2012-01-10 13:07:16 -08001632
Adam Cohenb64d36e2011-10-17 21:48:02 -07001633 if (mCurrentPage != 0) {
1634 invalidatePageData(0);
1635 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001636 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001637
1638 private AppsCustomizeTabHost getTabHost() {
1639 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1640 }
1641
Winson Chung785d2eb2011-04-14 16:08:02 -07001642 public void dumpState() {
1643 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001644 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001645 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001646 }
Adam Cohen4e844012011-11-09 13:48:04 -08001647
Winson Chung785d2eb2011-04-14 16:08:02 -07001648 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001649 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001650 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001651 for (Object i: list) {
1652 if (i instanceof AppWidgetProviderInfo) {
1653 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1654 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1655 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1656 + " initialLayout=" + info.initialLayout
1657 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1658 } else if (i instanceof ResolveInfo) {
1659 ResolveInfo info = (ResolveInfo) i;
1660 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1661 + info.icon);
1662 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001663 }
1664 }
Adam Cohen4e844012011-11-09 13:48:04 -08001665
Winson Chung785d2eb2011-04-14 16:08:02 -07001666 public void surrender() {
1667 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1668 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001669
1670 // Stop all background tasks
1671 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001672 }
Winson Chung007c6982011-06-14 13:27:53 -07001673
Winson Chunge4e50662012-01-23 14:45:13 -08001674 @Override
1675 public void iconPressed(PagedViewIcon icon) {
1676 // Reset the previously pressed icon and store a reference to the pressed icon so that
1677 // we can reset it on return to Launcher (in Launcher.onResume())
1678 if (mPressedIcon != null) {
1679 mPressedIcon.resetDrawableState();
1680 }
1681 mPressedIcon = icon;
1682 }
1683
1684 public void resetDrawableState() {
1685 if (mPressedIcon != null) {
1686 mPressedIcon.resetDrawableState();
1687 mPressedIcon = null;
1688 }
1689 }
Winson Chung68e4c642011-11-10 15:48:25 -08001690
Winson Chungb44b5242011-06-13 11:32:14 -07001691 /*
1692 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1693 * widget previews in the background with the AsyncTasks.
1694 */
Winson Chung68e4c642011-11-10 15:48:25 -08001695 final static int sLookBehindPageCount = 2;
1696 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001697 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001698 final int count = getChildCount();
1699 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1700 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1701 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001702 }
1703 protected int getAssociatedUpperPageBound(int page) {
1704 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001705 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1706 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1707 count - 1);
1708 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001709 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001710
Winson Chung6a0f57d2011-06-29 20:10:49 -07001711 protected String getCurrentPageDescription() {
1712 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1713 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001714 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001715
1716 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001717 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001718 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001719 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001720 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001721 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001722 } else {
1723 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001724 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001725
Michael Jurka8b805b12012-04-18 14:23:14 -07001726 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001727 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001728}