blob: 175d5f83ea15989141d3b378884f232c94ee65d2 [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 Chung785d2eb2011-04-14 16:08:02 -0700190
Adam Cohen22f823d2011-09-01 17:22:18 -0700191 // Relating to the scroll and overscroll effects
192 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700193 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700194 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700195 private static float TRANSITION_PIVOT = 0.65f;
196 private static float TRANSITION_MAX_ROTATION = 22;
197 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700198 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700199 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700200
Winson Chungc58497e2013-09-03 17:48:37 -0700201 public static boolean DISABLE_ALL_APPS = false;
Adam Cohen947dc542013-06-06 22:43:33 -0700202
Winson Chungb44b5242011-06-13 11:32:14 -0700203 // Previews & outlines
204 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800205 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700206
Adam Cohened66b2b2012-01-23 17:28:51 -0800207 private Runnable mInflateWidgetRunnable = null;
208 private Runnable mBindWidgetRunnable = null;
209 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700210 static final int WIDGET_PRELOAD_PENDING = 0;
211 static final int WIDGET_BOUND = 1;
212 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800213 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
214 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800215 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800216 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800217
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700218 private Toast mWidgetInstructionToast;
219
Michael Jurka39e5d172012-03-12 18:36:12 -0700220 // Deferral of loading widget previews during launcher transitions
221 private boolean mInTransition;
222 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
223 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700224 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
225 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700226
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700227 private Rect mTmpRect = new Rect();
228
Michael Jurkadac85912012-05-18 15:04:49 -0700229 // Used for drawing shortcut previews
230 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
231 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
232 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
233
234 // Used for drawing widget previews
235 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
236 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
237 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
238 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
239
Michael Jurka05713af2013-01-23 12:39:24 +0100240 WidgetPreviewLoader mWidgetPreviewLoader;
241
Michael Jurkac402cd92013-05-20 15:49:32 +0200242 private boolean mInBulkBind;
243 private boolean mNeedToUpdatePageCountsAndInvalidateData;
244
Winson Chung785d2eb2011-04-14 16:08:02 -0700245 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
246 super(context, attrs);
247 mLayoutInflater = LayoutInflater.from(context);
248 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200249 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700250 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400251 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700252 mCanvas = new Canvas();
253 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700254
255 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800256 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700257 LauncherAppState app = LauncherAppState.getInstance();
258 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
259 mWidgetWidthGap = mWidgetHeightGap = grid.edgeMarginPx;
Winson Chung4b576be2011-04-27 17:40:20 -0700260 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
261 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700262 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
263 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700264 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700265 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700266
Winson Chung1ed747a2011-05-03 16:18:34 -0700267 // The padding on the non-matched dimension for the default widget preview icons
268 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700269 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700270
271 // Unless otherwise specified this view is important for accessibility.
272 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
273 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
274 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700275 }
276
277 @Override
278 protected void init() {
279 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700280 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700281
282 Context context = getContext();
283 Resources r = context.getResources();
284 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
285 }
286
Winson Chungc58497e2013-09-03 17:48:37 -0700287 public void onFinishInflate() {
288 super.onFinishInflate();
289
290 LauncherAppState app = LauncherAppState.getInstance();
291 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
292 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
293 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
294 }
295
Winson Chung5afbf7b2011-07-25 11:53:08 -0700296 /** Returns the item index of the center item on this page so that we can restore to this
297 * item index when we rotate. */
298 private int getMiddleComponentIndexOnCurrentPage() {
299 int i = -1;
300 if (getPageCount() > 0) {
301 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700302 if (mContentType == ContentType.Applications) {
303 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
304 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700305 int numItemsPerPage = mCellCountX * mCellCountY;
306 int childCount = childrenLayout.getChildCount();
307 if (childCount > 0) {
308 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 }
Winson Chungc58497e2013-09-03 17:48:37 -0700310 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700312 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700313 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
314 int childCount = layout.getChildCount();
315 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700317 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700318 }
Winson Chungc58497e2013-09-03 17:48:37 -0700319 } else {
320 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700321 }
322 }
323 return i;
324 }
325
326 /** Get the index of the item to restore to if we need to restore the current page. */
327 int getSaveInstanceStateIndex() {
328 if (mSaveInstanceStateItemIndex == -1) {
329 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
330 }
331 return mSaveInstanceStateItemIndex;
332 }
333
334 /** Returns the page in the current orientation which is expected to contain the specified
335 * item index. */
336 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700337 if (index < 0) return 0;
338
339 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700340 int numItemsPerPage = mCellCountX * mCellCountY;
341 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700343 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700344 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700345 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700346 }
347
Winson Chung5afbf7b2011-07-25 11:53:08 -0700348 /** Restores the page for an item at the specified index */
349 void restorePageForIndex(int index) {
350 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700351 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700352 }
353
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700354 private void updatePageCounts() {
355 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
356 (float) (mWidgetCountX * mWidgetCountY));
357 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
358 }
359
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100361 if (mWidgetPreviewLoader == null) {
362 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
363 }
364
Winson Chungf0ea4d32011-06-06 14:27:16 -0700365 // Now that the data is ready, we can calculate the content width, the number of cells to
366 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700367 LauncherAppState app = LauncherAppState.getInstance();
368 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700369 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
370 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungc58497e2013-09-03 17:48:37 -0700371 mCellCountX = (int) grid.allAppsNumCols;
372 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700373 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700374
Winson Chungdb1138b2011-06-30 14:39:35 -0700375 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700376 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
377 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
378 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
379 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700380 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700381
Michael Jurkae326f182011-11-21 14:05:46 -0800382 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
383 final boolean hostIsTransitioning = host.isTransitioning();
384
Adam Cohen0cd3b642011-10-14 14:58:00 -0700385 // Restore the page
386 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800387 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700388
Winson Chung3f4e1422011-11-17 14:58:51 -0800389 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
390 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
391 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800392 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800393 post(new Runnable() {
394 @Override
395 public void run() {
396 showAllAppsCling();
397 }
398 });
399 }
400 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700401
Winson Chung3f4e1422011-11-17 14:58:51 -0800402 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700403 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800404 mHasShownAllAppsCling = true;
405 // Calculate the position for the cling punch through
406 int[] offset = new int[2];
407 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
408 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
409 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800410 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800411 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
412 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800413 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800414 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700415 }
416
417 @Override
418 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
419 int width = MeasureSpec.getSize(widthMeasureSpec);
420 int height = MeasureSpec.getSize(heightMeasureSpec);
421 if (!isDataReady()) {
Adam Cohen947dc542013-06-06 22:43:33 -0700422 if ((DISABLE_ALL_APPS || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700423 setDataIsReady();
424 setMeasuredDimension(width, height);
425 onDataReady(width, height);
426 }
427 }
428
429 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
430 }
431
Michael Jurkac402cd92013-05-20 15:49:32 +0200432 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700433 LauncherAppState app = LauncherAppState.getInstance();
434 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
435
Winson Chung1ed747a2011-05-03 16:18:34 -0700436 // Get the list of widgets and shortcuts
437 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200438 for (Object o : widgetsAndShortcuts) {
439 if (o instanceof AppWidgetProviderInfo) {
440 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100441 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
442 continue;
443 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200444 widget.label = widget.label.trim();
445 if (widget.minWidth > 0 && widget.minHeight > 0) {
446 // Ensure that all widgets we show can be added on a workspace of this size
447 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
448 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
449 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
450 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700451 if (minSpanX <= (int) grid.numColumns &&
452 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200453 mWidgets.add(widget);
454 } else {
455 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
456 widget.minWidth + ", " + widget.minHeight + ")");
457 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700458 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200459 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
460 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700461 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800462 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200463 // just add shortcuts
464 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800465 }
466 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200467 updatePageCountsAndInvalidateData();
468 }
469
470 public void setBulkBind(boolean bulkBind) {
471 if (bulkBind) {
472 mInBulkBind = true;
473 } else {
474 mInBulkBind = false;
475 if (mNeedToUpdatePageCountsAndInvalidateData) {
476 updatePageCountsAndInvalidateData();
477 }
478 }
479 }
480
481 private void updatePageCountsAndInvalidateData() {
482 if (mInBulkBind) {
483 mNeedToUpdatePageCountsAndInvalidateData = true;
484 } else {
485 updatePageCounts();
486 invalidateOnDataChange();
487 mNeedToUpdatePageCountsAndInvalidateData = false;
488 }
Winson Chung4b576be2011-04-27 17:40:20 -0700489 }
490
491 @Override
492 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700493 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700494 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700495 mLauncher.getWorkspace().isSwitchingState()) return;
496
Winson Chung4b576be2011-04-27 17:40:20 -0700497 if (v instanceof PagedViewIcon) {
498 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200499 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800500
501 // Lock the drawable state to pressed until we return to Launcher
502 if (mPressedIcon != null) {
503 mPressedIcon.lockDrawableState();
504 }
Winson Chungc7450e32012-04-17 17:34:08 -0700505 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
Anton Hanssona2f665f2013-09-26 12:18:32 +0100506 mLauncher.getStats().recordLaunch(appInfo.intent);
Winson Chung4b576be2011-04-27 17:40:20 -0700507 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700508 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700509 if (mWidgetInstructionToast != null) {
510 mWidgetInstructionToast.cancel();
511 }
512 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
513 Toast.LENGTH_SHORT);
514 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700515
Winson Chungd2e87b32011-06-02 10:53:07 -0700516 // Create a little animation to show that the widget can move
517 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
518 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700519 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
520 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700521 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700522 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700523 tydAnim.setDuration(100);
524 bounce.play(tyuAnim).before(tydAnim);
525 bounce.setInterpolator(new AccelerateInterpolator());
526 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700527 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700528 }
529
Winson Chungc6f10b92011-11-14 11:39:07 -0800530 public boolean onKey(View v, int keyCode, KeyEvent event) {
531 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
532 }
533
Winson Chung785d2eb2011-04-14 16:08:02 -0700534 /*
535 * PagedViewWithDraggableItems implementation
536 */
537 @Override
538 protected void determineDraggingStart(android.view.MotionEvent ev) {
539 // Disable dragging by pulling an app down for now.
540 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700541
Winson Chung4b576be2011-04-27 17:40:20 -0700542 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700543 mLauncher.getWorkspace().onDragStartedWithItem(v);
544 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700545 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700546
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700547 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
548 Bundle options = null;
549 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
550 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700551 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
552 info.componentName, null);
553
554 float density = getResources().getDisplayMetrics().density;
555 int xPaddingDips = (int) ((padding.left + padding.right) / density);
556 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
557
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700558 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700559 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
560 mTmpRect.left - xPaddingDips);
561 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
562 mTmpRect.top - yPaddingDips);
563 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
564 mTmpRect.right - xPaddingDips);
565 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
566 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700567 }
568 return options;
569 }
570
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700571 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800572 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700573 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
574
Adam Cohened66b2b2012-01-23 17:28:51 -0800575 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700576 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 return;
578 }
579
Adam Cohen21a170b2012-05-30 15:17:06 -0700580 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 mBindWidgetRunnable = new Runnable() {
582 @Override
583 public void run() {
584 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700585 // Options will be null for platforms with JB or lower, so this serves as an
586 // SDK level check.
587 if (options == null) {
588 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
589 mWidgetLoadingId, info.componentName)) {
590 mWidgetCleanupState = WIDGET_BOUND;
591 }
592 } else {
593 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
594 mWidgetLoadingId, info.componentName, options)) {
595 mWidgetCleanupState = WIDGET_BOUND;
596 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700597 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800598 }
599 };
600 post(mBindWidgetRunnable);
601
602 mInflateWidgetRunnable = new Runnable() {
603 @Override
604 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700605 if (mWidgetCleanupState != WIDGET_BOUND) {
606 return;
607 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 AppWidgetHostView hostView = mLauncher.
609 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 info.boundWidget = hostView;
611 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800612 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700613 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
614 info.spanY, info, false);
615
616 // We want the first widget layout to be the correct size. This will be important
617 // for width size reporting to the AppWidgetManager.
618 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
619 unScaledSize[1]);
620 lp.x = lp.y = 0;
621 lp.customPosition = true;
622 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800623 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800624 }
625 };
626 post(mInflateWidgetRunnable);
627 }
628
629 @Override
630 public void onShortPress(View v) {
631 // We are anticipating a long press, and we use this time to load bind and instantiate
632 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700633 if (mCreateWidgetInfo != null) {
634 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
635 cleanupWidgetPreloading(false);
636 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800637 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700638 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 }
640
Adam Cohen0e56cc92012-05-11 15:57:05 -0700641 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
642 if (!widgetWasAdded) {
643 // If the widget was not added, we may need to do further cleanup.
644 PendingAddWidgetInfo info = mCreateWidgetInfo;
645 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700646
647 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700648 // We never did any preloading, so just remove pending callbacks to do so
649 removeCallbacks(mBindWidgetRunnable);
650 removeCallbacks(mInflateWidgetRunnable);
651 } else if (mWidgetCleanupState == WIDGET_BOUND) {
652 // Delete the widget id which was allocated
653 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700654 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
655 }
656
657 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700658 removeCallbacks(mInflateWidgetRunnable);
659 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700660 // Delete the widget id which was allocated
661 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700662 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
663 }
664
Adam Cohen0e56cc92012-05-11 15:57:05 -0700665 // The widget was inflated and added to the DragLayer -- remove it.
666 AppWidgetHostView widget = info.boundWidget;
667 mLauncher.getDragLayer().removeView(widget);
668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 }
670 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
671 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700672 mCreateWidgetInfo = null;
673 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 }
675
Adam Cohen7a326642012-02-22 12:03:22 -0800676 @Override
677 public void cleanUpShortPress(View v) {
678 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700679 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800680 }
681 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800682
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700683 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800684 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700685 // Get the widget preview as the drag representation
686 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700687 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700688
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700689 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
690 // we abort the drag.
691 if (image.getDrawable() == null) {
692 mDraggingWidget = false;
693 return false;
694 }
695
Winson Chung4b576be2011-04-27 17:40:20 -0700696 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800697 Bitmap preview;
698 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800699 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100700 Point previewPadding = null;
701
Winson Chung1ed747a2011-05-03 16:18:34 -0700702 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700703 // This can happen in some weird cases involving multi-touch. We can't start dragging
704 // the widget if this is null, so we break out.
705 if (mCreateWidgetInfo == null) {
706 return false;
707 }
708
Adam Cohen1b36dc32012-02-13 19:27:37 -0800709 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
710 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700711 int spanX = createItemInfo.spanX;
712 int spanY = createItemInfo.spanY;
713 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
714 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700715
Winson Chung72d59842012-02-22 13:51:36 -0800716 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
717 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700718 int maxWidth, maxHeight;
719 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
720 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800721
Michael Jurka05713af2013-01-23 12:39:24 +0100722 int[] previewSizeBeforeScale = new int[1];
723
724 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
725 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
726 maxWidth, maxHeight, null, previewSizeBeforeScale);
727
728 // Compare the size of the drag preview to the preview in the AppsCustomize tray
729 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
730 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
731 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
732
733 // The bitmap in the AppsCustomize tray is always the the same size, so there
734 // might be extra pixels around the preview itself - this accounts for that
735 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
736 int padding =
737 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
738 previewPadding = new Point(padding, 0);
739 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700740 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700741 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
742 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
743 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
744 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
745
Winson Chung1120e032011-11-22 16:11:31 -0800746 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700747 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100748 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700749 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700750 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700751 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700752 createItemInfo.spanX = createItemInfo.spanY = 1;
753 }
Winson Chung4b576be2011-04-27 17:40:20 -0700754
Michael Jurka8c3339b2012-06-14 16:18:21 -0700755 // Don't clip alpha values for the drag outline if we're using the default widget preview
756 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
757 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700758
Winson Chung1120e032011-11-22 16:11:31 -0800759 // Save the preview for the outline generation, then dim the preview
760 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
761 false);
Winson Chung1120e032011-11-22 16:11:31 -0800762
Winson Chung4b576be2011-04-27 17:40:20 -0700763 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700764 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700765 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800766 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100767 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800768 outline.recycle();
769 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700770 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700771 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800772
Winson Chung4b576be2011-04-27 17:40:20 -0700773 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700775 if (!super.beginDragging(v)) return false;
776
777 if (v instanceof PagedViewIcon) {
778 beginDraggingApplication(v);
779 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700780 if (!beginDraggingWidget(v)) {
781 return false;
782 }
Winson Chung4b576be2011-04-27 17:40:20 -0700783 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800784
785 // We delay entering spring-loaded mode slightly to make sure the UI
786 // thready is free of any work.
787 postDelayed(new Runnable() {
788 @Override
789 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800790 // We don't enter spring-loaded mode if the drag has been cancelled
791 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800792 // Reset the alpha on the dragged icon before we drag
793 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800794
Adam Cohen1b36dc32012-02-13 19:27:37 -0800795 // Go into spring loaded mode (must happen before we startDrag())
796 mLauncher.enterSpringLoadedDragMode();
797 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 }
Winson Chung72d59842012-02-22 13:51:36 -0800799 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800800
Winson Chung785d2eb2011-04-14 16:08:02 -0700801 return true;
802 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800803
Winson Chunga48487a2012-03-20 16:19:37 -0700804 /**
805 * Clean up after dragging.
806 *
807 * @param target where the item was dragged to (can be null if the item was flung)
808 */
809 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700810 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700811 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700812 // Exit spring loaded mode if we have not successfully dropped or have not handled the
813 // drop in Workspace
Adam Cohene97a3b32013-10-23 16:11:50 -0700814 mLauncher.getWorkspace().removeExtraEmptyScreen(true, new Runnable() {
815 @Override
816 public void run() {
817 mLauncher.exitSpringLoadedDragMode();
818 mLauncher.unlockScreenOrientation(false);
819 }
820 });
821 } else {
822 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700823 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 }
825
Winson Chung785d2eb2011-04-14 16:08:02 -0700826 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700827 public View getContent() {
828 return null;
829 }
830
831 @Override
832 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700833 mInTransition = true;
834 if (toWorkspace) {
835 cancelAllTasks();
836 }
837 }
838
839 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700840 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700841 }
842
843 @Override
844 public void onLauncherTransitionStep(Launcher l, float t) {
845 }
846
847 @Override
848 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
849 mInTransition = false;
850 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
851 onSyncWidgetPageItems(d);
852 }
853 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700854 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
855 r.run();
856 }
857 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700858 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700859 }
860
861 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700862 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
863 boolean success) {
864 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
865 if (isFlingToDelete) return;
866
867 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700868
869 // Display an error message if the drag failed due to there not being enough space on the
870 // target layout we were dropping on.
871 if (!success) {
872 boolean showOutOfSpaceMessage = false;
873 if (target instanceof Workspace) {
874 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
875 Workspace workspace = (Workspace) target;
876 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700877 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700878 if (layout != null) {
879 layout.calculateSpans(itemInfo);
880 showOutOfSpaceMessage =
881 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
882 }
883 }
Winson Chungfc79c802011-05-02 13:35:34 -0700884 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700885 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700886 }
Adam Cohen7a326642012-02-22 12:03:22 -0800887
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800888 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700889 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700890 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800891 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700892 }
893
Winson Chunga48487a2012-03-20 16:19:37 -0700894 @Override
895 public void onFlingToDeleteCompleted() {
896 // We just dismiss the drag when we fling, so cleanup here
897 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700898 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700899 mDraggingWidget = false;
900 }
901
902 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800903 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700904 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800905 }
906
Winson Chung7f0acdd2011-09-19 18:34:19 -0700907 @Override
908 protected void onDetachedFromWindow() {
909 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700910 cancelAllTasks();
911 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700912
Michael Jurkae326f182011-11-21 14:05:46 -0800913 public void clearAllWidgetPages() {
914 cancelAllTasks();
915 int count = getChildCount();
916 for (int i = 0; i < count; i++) {
917 View v = getPageAt(i);
918 if (v instanceof PagedViewGridLayout) {
919 ((PagedViewGridLayout) v).removeAllViewsOnPage();
920 mDirtyPageContent.set(i, true);
921 }
922 }
923 }
924
Adam Cohen0cd3b642011-10-14 14:58:00 -0700925 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700926 // Clean up all the async tasks
927 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
928 while (iter.hasNext()) {
929 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
930 task.cancel(false);
931 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700932 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700933
934 // We've already preallocated the views for the data to load into, so clear them as well
935 View v = getPageAt(task.page);
936 if (v instanceof PagedViewGridLayout) {
937 ((PagedViewGridLayout) v).removeAllViewsOnPage();
938 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700939 }
Winson Chung83687b12012-04-25 16:01:01 -0700940 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700941 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700942 }
943
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200945 // Widgets appear to be cleared every time you leave, always force invalidate for them
946 if (mContentType != type || type == ContentType.Widgets) {
947 int page = (mContentType != type) ? 0 : getCurrentPage();
948 mContentType = type;
949 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700950 }
Winson Chungc58497e2013-09-03 17:48:37 -0700951 }
952
953 public ContentType getContentType() {
954 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700955 }
956
Adam Cohen0cd3b642011-10-14 14:58:00 -0700957 protected void snapToPage(int whichPage, int delta, int duration) {
958 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800959
960 // Update the thread priorities given the direction lookahead
961 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
962 while (iter.hasNext()) {
963 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700964 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800965 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
966 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
967 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
968 } else {
969 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
970 }
971 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700972 }
973
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 /*
975 * Apps PagedView implementation
976 */
Winson Chung63257c12011-05-05 17:06:13 -0700977 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
978 int childCount = layout.getChildCount();
979 for (int i = 0; i < childCount; ++i) {
980 layout.getChildAt(i).setVisibility(visibility);
981 }
982 }
Winson Chungc58497e2013-09-03 17:48:37 -0700983 private void setupPage(AppsCustomizeCellLayout layout) {
984 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700985
Winson Chung63257c12011-05-05 17:06:13 -0700986 // Note: We force a measure here to get around the fact that when we do layout calculations
987 // immediately after syncing, we don't have a proper width. That said, we already know the
988 // expected page width, so we can actually optimize by hiding all the TextView-based
989 // children that are expensive to measure, and let that happen naturally later.
990 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700991 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
992 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700993 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700995 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700996 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700997
Winson Chungf314b0e2011-08-16 11:54:27 -0700998 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700999 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001000 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 int numCells = mCellCountX * mCellCountY;
1002 int startIndex = page * numCells;
1003 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -07001004 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001005
Winson Chung785d2eb2011-04-14 16:08:02 -07001006 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001007 ArrayList<Object> items = new ArrayList<Object>();
1008 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001009 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001010 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001011 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1012 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001013 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001014 icon.setOnClickListener(this);
1015 icon.setOnLongClickListener(this);
1016 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001017 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001018
1019 int index = i - startIndex;
1020 int x = index % mCellCountX;
1021 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001022 if (isRtl) {
1023 x = mCellCountX - x - 1;
1024 }
Winson Chungc58497e2013-09-03 17:48:37 -07001025 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001026
1027 items.add(info);
1028 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001029 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001030
Michael Jurka47639b92013-01-14 12:42:27 +01001031 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 }
Winson Chungb44b5242011-06-13 11:32:14 -07001033
1034 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001035 * A helper to return the priority for loading of the specified widget page.
1036 */
1037 private int getWidgetPageLoadPriority(int page) {
1038 // If we are snapping to another page, use that index as the target page index
1039 int toPage = mCurrentPage;
1040 if (mNextPage > -1) {
1041 toPage = mNextPage;
1042 }
1043
1044 // We use the distance from the target page as an initial guess of priority, but if there
1045 // are no pages of higher priority than the page specified, then bump up the priority of
1046 // the specified page.
1047 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1048 int minPageDiff = Integer.MAX_VALUE;
1049 while (iter.hasNext()) {
1050 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001051 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001052 }
1053
1054 int rawPageDiff = Math.abs(page - toPage);
1055 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1056 }
1057 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001058 * Return the appropriate thread priority for loading for a given page (we give the current
1059 * page much higher priority)
1060 */
1061 private int getThreadPriorityForPage(int page) {
1062 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001063 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001064 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001065 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001066 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001067 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001068 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001069 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001070 }
1071 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001072 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001073 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001074 return Math.max(0, pageDiff * sPageSleepDelay);
1075 }
Winson Chungb44b5242011-06-13 11:32:14 -07001076 /**
1077 * Creates and executes a new AsyncTask to load a page of widget previews.
1078 */
1079 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001080 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001081
Winson Chungb44b5242011-06-13 11:32:14 -07001082 // Prune all tasks that are no longer needed
1083 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1084 while (iter.hasNext()) {
1085 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001086 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001087 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1088 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001089 task.cancel(false);
1090 iter.remove();
1091 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001092 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001093 }
1094 }
1095
Winson Chungf314b0e2011-08-16 11:54:27 -07001096 // 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 -07001097 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001098 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001099 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001100 @Override
1101 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001102 try {
Winson Chung09945932011-09-20 14:22:40 -07001103 try {
1104 Thread.sleep(sleepMs);
1105 } catch (Exception e) {}
1106 loadWidgetPreviewsInBackground(task, data);
1107 } finally {
1108 if (task.isCancelled()) {
1109 data.cleanup(true);
1110 }
1111 }
Winson Chungb44b5242011-06-13 11:32:14 -07001112 }
1113 },
1114 new AsyncTaskCallback() {
1115 @Override
1116 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001117 mRunningTasks.remove(task);
1118 if (task.isCancelled()) return;
1119 // do cleanup inside onSyncWidgetPageItems
1120 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001121 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001122 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001123
1124 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001125 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001126 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001127 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001128 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1129 mRunningTasks.add(t);
1130 }
Winson Chungb44b5242011-06-13 11:32:14 -07001131
Winson Chung785d2eb2011-04-14 16:08:02 -07001132 /*
1133 * Widgets PagedView implementation
1134 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001135 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001136 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001137 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001138 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1139 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001140 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001141 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001142 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001143
Michael Jurka038f9d82011-11-03 13:50:45 -07001144 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001145 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001146
Winson Chungd2945262011-06-24 15:22:14 -07001147 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001148 final ArrayList<Object> items = new ArrayList<Object>();
Winson Chungc58497e2013-09-03 17:48:37 -07001149 int contentWidth = mContentWidth;
Michael Jurka038f9d82011-11-03 13:50:45 -07001150 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001151 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungc58497e2013-09-03 17:48:37 -07001152 int contentHeight = mContentHeight;
Michael Jurka038f9d82011-11-03 13:50:45 -07001153 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001154 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001155
Winson Chunge4a647f2011-09-30 14:41:25 -07001156 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001157 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001158 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1159 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001160 }
1161
Winson Chunge4a647f2011-09-30 14:41:25 -07001162 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001163 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001164 layout.setColumnCount(layout.getCellCountX());
1165 for (int i = 0; i < items.size(); ++i) {
1166 Object rawInfo = items.get(i);
1167 PendingAddItemInfo createItemInfo = null;
1168 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1169 R.layout.apps_customize_widget, layout, false);
1170 if (rawInfo instanceof AppWidgetProviderInfo) {
1171 // Fill in the widget information
1172 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1173 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001174
1175 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001176 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001177 createItemInfo.spanX = spanXY[0];
1178 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001180 createItemInfo.minSpanX = minSpanXY[0];
1181 createItemInfo.minSpanY = minSpanXY[1];
1182
Michael Jurka3f4e0702013-02-05 11:21:28 +01001183 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001184 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001185 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001186 } else if (rawInfo instanceof ResolveInfo) {
1187 // Fill in the shortcuts information
1188 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001189 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001190 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1191 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1192 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001193 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001194 widget.setTag(createItemInfo);
1195 }
1196 widget.setOnClickListener(this);
1197 widget.setOnLongClickListener(this);
1198 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001199 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001200
1201 // Layout each widget
1202 int ix = i % mWidgetCountX;
1203 int iy = i / mWidgetCountX;
1204 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001205 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001206 GridLayout.spec(ix, GridLayout.TOP));
1207 lp.width = cellWidth;
1208 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001209 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001210 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1211 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1212 layout.addView(widget, lp);
1213 }
1214
Michael Jurka038f9d82011-11-03 13:50:45 -07001215 // wait until a call on onLayout to start loading, because
1216 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1217 // TODO: can we do a measure/layout immediately?
1218 layout.setOnLayoutListener(new Runnable() {
1219 public void run() {
1220 // Load the widget previews
1221 int maxPreviewWidth = cellWidth;
1222 int maxPreviewHeight = cellHeight;
1223 if (layout.getChildCount() > 0) {
1224 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1225 int[] maxSize = w.getPreviewSize();
1226 maxPreviewWidth = maxSize[0];
1227 maxPreviewHeight = maxSize[1];
1228 }
Michael Jurka05713af2013-01-23 12:39:24 +01001229
Michael Jurka3f4e0702013-02-05 11:21:28 +01001230 mWidgetPreviewLoader.setPreviewSize(
1231 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001232 if (immediate) {
1233 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001234 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001235 loadWidgetPreviewsInBackground(null, data);
1236 onSyncWidgetPageItems(data);
1237 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001238 if (mInTransition) {
1239 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1240 } else {
1241 prepareLoadWidgetPreviewsTask(page, items,
1242 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1243 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001245 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001246 }
1247 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001248 }
1249 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1250 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001251 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1252 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001253 if (task != null) {
1254 // Ensure that this task starts running at the correct priority
1255 task.syncThreadPriority();
1256 }
1257
1258 // Load each of the widget/shortcut previews
1259 ArrayList<Object> items = data.items;
1260 ArrayList<Bitmap> images = data.generatedImages;
1261 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001262 for (int i = 0; i < count; ++i) {
1263 if (task != null) {
1264 // Ensure we haven't been cancelled yet
1265 if (task.isCancelled()) break;
1266 // Before work on each item, ensure that this task is running at the correct
1267 // priority
1268 task.syncThreadPriority();
1269 }
1270
Michael Jurka05713af2013-01-23 12:39:24 +01001271 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001272 }
Winson Chungb44b5242011-06-13 11:32:14 -07001273 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001274
Winson Chungb44b5242011-06-13 11:32:14 -07001275 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001276 if (mInTransition) {
1277 mDeferredSyncWidgetPageItems.add(data);
1278 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001279 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001280 try {
1281 int page = data.page;
1282 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001283
Michael Jurka39e5d172012-03-12 18:36:12 -07001284 ArrayList<Object> items = data.items;
1285 int count = items.size();
1286 for (int i = 0; i < count; ++i) {
1287 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1288 if (widget != null) {
1289 Bitmap preview = data.generatedImages.get(i);
1290 widget.applyPreview(new FastBitmapDrawable(preview), i);
1291 }
1292 }
Winson Chung68e4c642011-11-10 15:48:25 -08001293
Michael Jurka47639b92013-01-14 12:42:27 +01001294 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001295
1296 // Update all thread priorities
1297 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1298 while (iter.hasNext()) {
1299 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1300 int pageIndex = task.page;
1301 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1302 }
1303 } finally {
1304 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001305 }
Winson Chungb44b5242011-06-13 11:32:14 -07001306 }
Winson Chung46af2e82011-05-09 16:00:53 -07001307
Winson Chung785d2eb2011-04-14 16:08:02 -07001308 @Override
1309 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001310 disablePagedViewAnimations();
1311
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001313 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001314
Adam Cohen0cd3b642011-10-14 14:58:00 -07001315 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001316 if (mContentType == ContentType.Applications) {
1317 for (int i = 0; i < mNumAppsPages; ++i) {
1318 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1319 setupPage(layout);
1320 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1321 LayoutParams.MATCH_PARENT));
1322 }
1323 } else if (mContentType == ContentType.Widgets) {
1324 for (int j = 0; j < mNumWidgetPages; ++j) {
1325 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1326 mWidgetCountY);
1327 setupPage(layout);
1328 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1329 LayoutParams.MATCH_PARENT));
1330 }
1331 } else {
1332 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001333 }
1334
Winson Chungc58497e2013-09-03 17:48:37 -07001335 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001336 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001337
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001339 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001340 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001341 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001342 } else {
1343 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 }
1345 }
1346
Adam Cohen22f823d2011-09-01 17:22:18 -07001347 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1348 // it is in the z-order. This is important to insure touch events are handled correctly.
1349 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001350 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001351 }
1352
Adam Cohenae4f1552011-10-20 00:15:42 -07001353 @Override
1354 protected int indexToPage(int index) {
1355 return getChildCount() - index - 1;
1356 }
1357
Adam Cohen22f823d2011-09-01 17:22:18 -07001358 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1359 @Override
1360 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001361 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001362 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001363
1364 for (int i = 0; i < getChildCount(); i++) {
1365 View v = getPageAt(i);
1366 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001367 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001368
Winson Chung52aee602013-01-30 12:01:02 -08001369 float interpolatedProgress;
1370 float translationX;
1371 float maxScrollProgress = Math.max(0, scrollProgress);
1372 float minScrollProgress = Math.min(0, scrollProgress);
1373
1374 if (isRtl) {
1375 translationX = maxScrollProgress * v.getMeasuredWidth();
1376 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1377 } else {
1378 translationX = minScrollProgress * v.getMeasuredWidth();
1379 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1380 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001381 float scale = (1 - interpolatedProgress) +
1382 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001383
Adam Cohen2591f6a2011-10-25 14:36:40 -07001384 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001385 if (isRtl && (scrollProgress > 0)) {
1386 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1387 } else if (!isRtl && (scrollProgress < 0)) {
1388 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001389 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001390 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001391 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1392 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001393
1394 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1395 int pageWidth = v.getMeasuredWidth();
1396 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001397
1398 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001399 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1400 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1401 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1402
1403 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001404 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001405 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001406 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1407 scale = 1.0f;
1408 alpha = 1.0f;
1409 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001410 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001411 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001412 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001413 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001414 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1415 scale = 1.0f;
1416 alpha = 1.0f;
1417 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001418 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001419 } else {
1420 v.setPivotY(pageHeight / 2.0f);
1421 v.setPivotX(pageWidth / 2.0f);
1422 v.setRotationY(0f);
1423 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001424 }
1425
1426 v.setTranslationX(translationX);
1427 v.setScaleX(scale);
1428 v.setScaleY(scale);
1429 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001430
1431 // If the view has 0 alpha, we set it to be invisible so as to prevent
1432 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001433 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001434 v.setVisibility(INVISIBLE);
1435 } else if (v.getVisibility() != VISIBLE) {
1436 v.setVisibility(VISIBLE);
1437 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001438 }
1439 }
Michael Jurka47639b92013-01-14 12:42:27 +01001440
1441 enableHwLayersOnVisiblePages();
1442 }
1443
1444 private void enableHwLayersOnVisiblePages() {
1445 final int screenCount = getChildCount();
1446
1447 getVisiblePages(mTempVisiblePagesRange);
1448 int leftScreen = mTempVisiblePagesRange[0];
1449 int rightScreen = mTempVisiblePagesRange[1];
1450 int forceDrawScreen = -1;
1451 if (leftScreen == rightScreen) {
1452 // make sure we're caching at least two pages always
1453 if (rightScreen < screenCount - 1) {
1454 rightScreen++;
1455 forceDrawScreen = rightScreen;
1456 } else if (leftScreen > 0) {
1457 leftScreen--;
1458 forceDrawScreen = leftScreen;
1459 }
1460 } else {
1461 forceDrawScreen = leftScreen + 1;
1462 }
1463
1464 for (int i = 0; i < screenCount; i++) {
1465 final View layout = (View) getPageAt(i);
1466 if (!(leftScreen <= i && i <= rightScreen &&
1467 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1468 layout.setLayerType(LAYER_TYPE_NONE, null);
1469 }
1470 }
1471
Michael Jurka47639b92013-01-14 12:42:27 +01001472 for (int i = 0; i < screenCount; i++) {
1473 final View layout = (View) getPageAt(i);
1474
1475 if (leftScreen <= i && i <= rightScreen &&
1476 (i == forceDrawScreen || shouldDrawChild(layout))) {
1477 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1478 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1479 }
1480 }
1481 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001482 }
1483
1484 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001485 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001486 }
1487
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 /**
1489 * Used by the parent to get the content width to set the tab bar to
1490 * @return
1491 */
1492 public int getPageContentWidth() {
1493 return mContentWidth;
1494 }
1495
Winson Chungb26f3d62011-06-02 10:49:29 -07001496 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001497 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001498 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001499 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001500 // We reset the save index when we change pages so that it will be recalculated on next
1501 // rotation
1502 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001503 }
1504
Winson Chung785d2eb2011-04-14 16:08:02 -07001505 /*
1506 * AllAppsView implementation
1507 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001508 public void setup(Launcher launcher, DragController dragController) {
1509 mLauncher = launcher;
1510 mDragController = dragController;
1511 }
Winson Chung9802ac92012-06-08 16:01:58 -07001512
1513 /**
1514 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1515 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1516 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1517 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1518 */
1519 private void invalidateOnDataChange() {
1520 if (!isDataReady()) {
1521 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1522 // request a layout to trigger the page data when ready.
1523 requestLayout();
1524 } else {
1525 cancelAllTasks();
1526 invalidatePageData();
1527 }
1528 }
1529
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001530 public void setApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001531 if (!DISABLE_ALL_APPS) {
1532 mApps = list;
1533 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1534 updatePageCountsAndInvalidateData();
1535 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001537 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001538 // We add it in place, in alphabetical order
1539 int count = list.size();
1540 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001541 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001542 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001543 if (index < 0) {
1544 mApps.add(-(index + 1), info);
1545 }
1546 }
1547 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001548 public void addApps(ArrayList<AppInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001549 if (!DISABLE_ALL_APPS) {
1550 addAppsWithoutInvalidate(list);
1551 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001552 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001553 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001554 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001555 ComponentName removeComponent = item.intent.getComponent();
1556 int length = list.size();
1557 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001558 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001559 if (info.intent.getComponent().equals(removeComponent)) {
1560 return i;
1561 }
1562 }
1563 return -1;
1564 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001565 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 // loop through all the apps and remove apps that have the same component
1567 int length = list.size();
1568 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001569 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 int removeIndex = findAppByComponent(mApps, info);
1571 if (removeIndex > -1) {
1572 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001573 }
1574 }
1575 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001576 public void removeApps(ArrayList<AppInfo> appInfos) {
Winson Chung64359a52013-07-08 17:17:08 -07001577 if (!DISABLE_ALL_APPS) {
1578 removeAppsWithoutInvalidate(appInfos);
1579 updatePageCountsAndInvalidateData();
1580 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001582 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001583 // We remove and re-add the updated applications list because it's properties may have
1584 // changed (ie. the title), and this will ensure that the items will be in their proper
1585 // place in the list.
Adam Cohen947dc542013-06-06 22:43:33 -07001586 if (!DISABLE_ALL_APPS) {
1587 removeAppsWithoutInvalidate(list);
1588 addAppsWithoutInvalidate(list);
1589 updatePageCountsAndInvalidateData();
1590 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001591 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001592
Winson Chung785d2eb2011-04-14 16:08:02 -07001593 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001594 // If we have reset, then we should not continue to restore the previous state
1595 mSaveInstanceStateItemIndex = -1;
1596
Adam Cohenb64d36e2011-10-17 21:48:02 -07001597 AppsCustomizeTabHost tabHost = getTabHost();
1598 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001599 if (tag != null) {
1600 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1601 tabHost.setCurrentTabFromContent(ContentType.Applications);
1602 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001603 }
Winson Chung649668f2012-01-10 13:07:16 -08001604
Adam Cohenb64d36e2011-10-17 21:48:02 -07001605 if (mCurrentPage != 0) {
1606 invalidatePageData(0);
1607 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001609
1610 private AppsCustomizeTabHost getTabHost() {
1611 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1612 }
1613
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 public void dumpState() {
1615 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001616 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001617 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 }
Adam Cohen4e844012011-11-09 13:48:04 -08001619
Winson Chung785d2eb2011-04-14 16:08:02 -07001620 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001621 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001622 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001623 for (Object i: list) {
1624 if (i instanceof AppWidgetProviderInfo) {
1625 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1626 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1627 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1628 + " initialLayout=" + info.initialLayout
1629 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1630 } else if (i instanceof ResolveInfo) {
1631 ResolveInfo info = (ResolveInfo) i;
1632 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1633 + info.icon);
1634 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 }
1636 }
Adam Cohen4e844012011-11-09 13:48:04 -08001637
Winson Chung785d2eb2011-04-14 16:08:02 -07001638 public void surrender() {
1639 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1640 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001641
1642 // Stop all background tasks
1643 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001644 }
Winson Chung007c6982011-06-14 13:27:53 -07001645
Winson Chunge4e50662012-01-23 14:45:13 -08001646 @Override
1647 public void iconPressed(PagedViewIcon icon) {
1648 // Reset the previously pressed icon and store a reference to the pressed icon so that
1649 // we can reset it on return to Launcher (in Launcher.onResume())
1650 if (mPressedIcon != null) {
1651 mPressedIcon.resetDrawableState();
1652 }
1653 mPressedIcon = icon;
1654 }
1655
1656 public void resetDrawableState() {
1657 if (mPressedIcon != null) {
1658 mPressedIcon.resetDrawableState();
1659 mPressedIcon = null;
1660 }
1661 }
Winson Chung68e4c642011-11-10 15:48:25 -08001662
Winson Chungb44b5242011-06-13 11:32:14 -07001663 /*
1664 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1665 * widget previews in the background with the AsyncTasks.
1666 */
Winson Chung68e4c642011-11-10 15:48:25 -08001667 final static int sLookBehindPageCount = 2;
1668 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001669 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001670 final int count = getChildCount();
1671 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1672 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1673 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001674 }
1675 protected int getAssociatedUpperPageBound(int page) {
1676 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001677 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1678 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1679 count - 1);
1680 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001681 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001682
Winson Chung6a0f57d2011-06-29 20:10:49 -07001683 protected String getCurrentPageDescription() {
1684 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1685 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001686 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001687
1688 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001689 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001690 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001691 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001692 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001693 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001694 } else {
1695 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001696 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001697
Michael Jurka8b805b12012-04-18 14:23:14 -07001698 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001699 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001700}