blob: c1aa19ae7a15d108a30fa8d8bb9b113dea0dd948 [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
Adam Cohened66b2b2012-01-23 17:28:51 -080019import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
22import android.content.ComponentName;
23import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Michael Jurka05713af2013-01-23 12:39:24 +010029import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.graphics.Rect;
31import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070032import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070033import android.os.Build;
34import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.util.AttributeSet;
37import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070038import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080039import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.view.LayoutInflater;
41import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070042import android.view.ViewGroup;
Winson Chungfd3385f2011-06-15 19:51:24 -070043import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070045import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Adam Cohenc0dcf592011-06-01 15:30:43 -070049
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.Iterator;
53import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070054
Winson Chungb44b5242011-06-13 11:32:14 -070055/**
56 * A simple callback interface which also provides the results of the task.
57 */
58interface AsyncTaskCallback {
59 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
60}
Winson Chung4e076542011-06-23 13:04:10 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * The data needed to perform either of the custom AsyncTasks.
64 */
65class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070066 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080067 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070068 }
69
Michael Jurka038f9d82011-11-03 13:50:45 -070070 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010071 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070072 page = p;
73 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070074 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070075 maxImageWidth = cw;
76 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070077 doInBackgroundCallback = bgR;
78 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010079 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070080 }
Winson Chung09945932011-09-20 14:22:40 -070081 void cleanup(boolean cancelled) {
82 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070083 if (generatedImages != null) {
84 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010085 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010086 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070087 }
88 }
89 generatedImages.clear();
90 }
91 }
Winson Chungb44b5242011-06-13 11:32:14 -070092 int page;
93 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070094 ArrayList<Bitmap> sourceImages;
95 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -070096 int maxImageWidth;
97 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -070098 AsyncTaskCallback doInBackgroundCallback;
99 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100100 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700101}
Winson Chung4e076542011-06-23 13:04:10 -0700102
Winson Chungb44b5242011-06-13 11:32:14 -0700103/**
104 * A generic template for an async task used in AppsCustomize.
105 */
106class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700107 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700108 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700109 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700110 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700111 }
112 @Override
113 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
114 if (params.length != 1) return null;
115 // Load each of the widget previews in the background
116 params[0].doInBackgroundCallback.run(this, params[0]);
117 return params[0];
118 }
119 @Override
120 protected void onPostExecute(AsyncTaskPageData result) {
121 // All the widget previews are loaded, so we can just callback to inflate the page
122 result.postExecuteCallback.run(this, result);
123 }
124
125 void setThreadPriority(int p) {
126 threadPriority = p;
127 }
128 void syncThreadPriority() {
129 Process.setThreadPriority(threadPriority);
130 }
131
132 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700133 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700134 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700135 int threadPriority;
136}
Winson Chungb44b5242011-06-13 11:32:14 -0700137
138/**
139 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
140 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700141public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700142 View.OnClickListener, View.OnKeyListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -0700143 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700144 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700145
Sunny Goyalff572272014-07-23 13:58:07 -0700146 private static Rect sTmpRect = new Rect();
147
Winson Chung785d2eb2011-04-14 16:08:02 -0700148 /**
149 * The different content types that this paged view can show.
150 */
151 public enum ContentType {
152 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700153 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700154 }
Winson Chungc58497e2013-09-03 17:48:37 -0700155 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700156
157 // Refs
158 private Launcher mLauncher;
159 private DragController mDragController;
160 private final LayoutInflater mLayoutInflater;
161 private final PackageManager mPackageManager;
162
Winson Chung5afbf7b2011-07-25 11:53:08 -0700163 // Save and Restore
164 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700165
Winson Chung785d2eb2011-04-14 16:08:02 -0700166 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700168 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700169
170 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -0700171 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700172
173 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700174 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700175 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700176 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700177 private int mNumAppsPages;
178 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700179 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700180
Winson Chungb44b5242011-06-13 11:32:14 -0700181 // Previews & outlines
182 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800183 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700184
Adam Cohened66b2b2012-01-23 17:28:51 -0800185 private Runnable mInflateWidgetRunnable = null;
186 private Runnable mBindWidgetRunnable = null;
187 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700188 static final int WIDGET_PRELOAD_PENDING = 0;
189 static final int WIDGET_BOUND = 1;
190 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800191 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
192 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800193 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800194 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700195 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800196
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700197 private Toast mWidgetInstructionToast;
198
Michael Jurka39e5d172012-03-12 18:36:12 -0700199 // Deferral of loading widget previews during launcher transitions
200 private boolean mInTransition;
201 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
202 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700203 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
204 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700205
Michael Jurka05713af2013-01-23 12:39:24 +0100206 WidgetPreviewLoader mWidgetPreviewLoader;
207
Michael Jurkac402cd92013-05-20 15:49:32 +0200208 private boolean mInBulkBind;
209 private boolean mNeedToUpdatePageCountsAndInvalidateData;
210
Winson Chung785d2eb2011-04-14 16:08:02 -0700211 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
212 super(context, attrs);
213 mLayoutInflater = LayoutInflater.from(context);
214 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200215 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700216 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400217 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700218 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700219
220 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800221 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700222 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
223 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
224 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700225 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700226
Winson Chung1ed747a2011-05-03 16:18:34 -0700227 // The padding on the non-matched dimension for the default widget preview icons
228 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700229 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700230
231 // Unless otherwise specified this view is important for accessibility.
232 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
233 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
234 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700235 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700236 }
237
238 @Override
239 protected void init() {
240 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700241 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700242
243 Context context = getContext();
244 Resources r = context.getResources();
245 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
246 }
247
Winson Chungc58497e2013-09-03 17:48:37 -0700248 public void onFinishInflate() {
249 super.onFinishInflate();
250
251 LauncherAppState app = LauncherAppState.getInstance();
252 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
253 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
254 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
255 }
256
Winson Chung67ca7e42013-10-31 16:53:19 -0700257 void setAllAppsPadding(Rect r) {
258 mAllAppsPadding.set(r);
259 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700260
Winson Chung67ca7e42013-10-31 16:53:19 -0700261 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700262 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700263 }
264
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100265 WidgetPreviewLoader getWidgetPreviewLoader() {
266 if (mWidgetPreviewLoader == null) {
267 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
268 }
269 return mWidgetPreviewLoader;
270 }
271
Winson Chung5afbf7b2011-07-25 11:53:08 -0700272 /** Returns the item index of the center item on this page so that we can restore to this
273 * item index when we rotate. */
274 private int getMiddleComponentIndexOnCurrentPage() {
275 int i = -1;
276 if (getPageCount() > 0) {
277 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700278 if (mContentType == ContentType.Applications) {
279 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
280 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700281 int numItemsPerPage = mCellCountX * mCellCountY;
282 int childCount = childrenLayout.getChildCount();
283 if (childCount > 0) {
284 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700285 }
Winson Chungc58497e2013-09-03 17:48:37 -0700286 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700287 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700288 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700289 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
290 int childCount = layout.getChildCount();
291 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700292 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700293 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700294 }
Winson Chungc58497e2013-09-03 17:48:37 -0700295 } else {
296 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700297 }
298 }
299 return i;
300 }
301
302 /** Get the index of the item to restore to if we need to restore the current page. */
303 int getSaveInstanceStateIndex() {
304 if (mSaveInstanceStateItemIndex == -1) {
305 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
306 }
307 return mSaveInstanceStateItemIndex;
308 }
309
310 /** Returns the page in the current orientation which is expected to contain the specified
311 * item index. */
312 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700313 if (index < 0) return 0;
314
315 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700316 int numItemsPerPage = mCellCountX * mCellCountY;
317 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700318 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700319 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700320 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700321 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700322 }
323
Winson Chung5afbf7b2011-07-25 11:53:08 -0700324 /** Restores the page for an item at the specified index */
325 void restorePageForIndex(int index) {
326 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700327 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700328 }
329
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700330 private void updatePageCounts() {
331 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
332 (float) (mWidgetCountX * mWidgetCountY));
333 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
334 }
335
Winson Chungf0ea4d32011-06-06 14:27:16 -0700336 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700337 // Now that the data is ready, we can calculate the content width, the number of cells to
338 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700339 LauncherAppState app = LauncherAppState.getInstance();
340 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700341 mCellCountX = (int) grid.allAppsNumCols;
342 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700343 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700344
Winson Chungdb1138b2011-06-30 14:39:35 -0700345 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700346 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
347 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
348 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
349 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700350 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700351
Adam Cohen6c5891a2014-07-09 23:53:15 -0700352 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800354 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800355 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700356
Adam Cohena00673c2014-08-14 12:57:28 -0700357 protected void onLayout(boolean changed, int l, int t, int r, int b) {
358 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700359
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 if (!isDataReady()) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800361 if ((!mApps.isEmpty()) && !mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700362 post(new Runnable() {
363 // This code triggers requestLayout so must be posted outside of the
364 // layout pass.
365 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800366 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200367 setDataIsReady();
368 onDataReady(getMeasuredWidth(), getMeasuredHeight());
369 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700370 }
371 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700372 }
373 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
375
Michael Jurkac402cd92013-05-20 15:49:32 +0200376 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700377 LauncherAppState app = LauncherAppState.getInstance();
378 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
379
Winson Chung1ed747a2011-05-03 16:18:34 -0700380 // Get the list of widgets and shortcuts
381 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200382 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800383 if (o instanceof LauncherAppWidgetProviderInfo) {
384 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
385 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100386 continue;
387 }
Adam Cohen59400422014-03-05 18:07:04 -0800388
389 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200390 // Ensure that all widgets we show can be added on a workspace of this size
391 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
392 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
393 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
394 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700395 if (minSpanX <= (int) grid.numColumns &&
396 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200397 mWidgets.add(widget);
398 } else {
399 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
400 widget.minWidth + ", " + widget.minHeight + ")");
401 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700402 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200403 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
404 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700405 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800406 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200407 // just add shortcuts
408 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800409 }
410 }
Adam Cohen59400422014-03-05 18:07:04 -0800411
Michael Jurkac402cd92013-05-20 15:49:32 +0200412 updatePageCountsAndInvalidateData();
413 }
414
415 public void setBulkBind(boolean bulkBind) {
416 if (bulkBind) {
417 mInBulkBind = true;
418 } else {
419 mInBulkBind = false;
420 if (mNeedToUpdatePageCountsAndInvalidateData) {
421 updatePageCountsAndInvalidateData();
422 }
423 }
424 }
425
426 private void updatePageCountsAndInvalidateData() {
427 if (mInBulkBind) {
428 mNeedToUpdatePageCountsAndInvalidateData = true;
429 } else {
430 updatePageCounts();
431 invalidateOnDataChange();
432 mNeedToUpdatePageCountsAndInvalidateData = false;
433 }
Winson Chung4b576be2011-04-27 17:40:20 -0700434 }
435
436 @Override
437 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700438 // When we have exited all apps or are in transition, disregard clicks
Sunny Goyal508da152014-08-14 10:53:27 -0700439 if (!mLauncher.isAllAppsVisible()
440 || mLauncher.getWorkspace().isSwitchingState()
441 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700442
Sunny Goyal508da152014-08-14 10:53:27 -0700443 // Let the user know that they have to long press to add a widget
444 if (mWidgetInstructionToast != null) {
445 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700446 }
Sunny Goyal508da152014-08-14 10:53:27 -0700447 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
448 Toast.LENGTH_SHORT);
449 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700450 }
451
Winson Chungc6f10b92011-11-14 11:39:07 -0800452 public boolean onKey(View v, int keyCode, KeyEvent event) {
453 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
454 }
455
Winson Chung785d2eb2011-04-14 16:08:02 -0700456 /*
457 * PagedViewWithDraggableItems implementation
458 */
459 @Override
460 protected void determineDraggingStart(android.view.MotionEvent ev) {
461 // Disable dragging by pulling an app down for now.
462 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700463
Winson Chung4b576be2011-04-27 17:40:20 -0700464 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700465 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700466 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700467
Sunny Goyalff572272014-07-23 13:58:07 -0700468 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700469 Bundle options = null;
470 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700471 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
472 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700473 info.componentName, null);
474
Sunny Goyalff572272014-07-23 13:58:07 -0700475 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700476 int xPaddingDips = (int) ((padding.left + padding.right) / density);
477 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
478
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700479 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700480 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700481 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700482 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700483 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700484 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700485 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700486 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700487 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700488 }
489 return options;
490 }
491
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700492 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800493 final LauncherAppWidgetProviderInfo pInfo = info.info;
494 final Bundle options = pInfo.isCustomWidget ? null :
495 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700496
Adam Cohened66b2b2012-01-23 17:28:51 -0800497 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700498 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800499 return;
500 }
501
Adam Cohen21a170b2012-05-30 15:17:06 -0700502 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800503 mBindWidgetRunnable = new Runnable() {
504 @Override
505 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800506 if (pInfo.isCustomWidget) {
507 mWidgetCleanupState = WIDGET_BOUND;
508 return;
509 }
510
Adam Cohened66b2b2012-01-23 17:28:51 -0800511 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700512 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
513 mWidgetLoadingId, pInfo, options)) {
514 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700515 }
Adam Cohen59400422014-03-05 18:07:04 -0800516
Adam Cohened66b2b2012-01-23 17:28:51 -0800517 }
518 };
519 post(mBindWidgetRunnable);
520
521 mInflateWidgetRunnable = new Runnable() {
522 @Override
523 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700524 if (mWidgetCleanupState != WIDGET_BOUND) {
525 return;
526 }
Adam Cohen59400422014-03-05 18:07:04 -0800527 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
528 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800529 info.boundWidget = hostView;
530 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800531 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700532 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
533 info.spanY, info, false);
534
535 // We want the first widget layout to be the correct size. This will be important
536 // for width size reporting to the AppWidgetManager.
537 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
538 unScaledSize[1]);
539 lp.x = lp.y = 0;
540 lp.customPosition = true;
541 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800542 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800543 }
544 };
545 post(mInflateWidgetRunnable);
546 }
547
548 @Override
549 public void onShortPress(View v) {
550 // We are anticipating a long press, and we use this time to load bind and instantiate
551 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700552 if (mCreateWidgetInfo != null) {
553 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
554 cleanupWidgetPreloading(false);
555 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800556 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700557 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800558 }
559
Adam Cohen0e56cc92012-05-11 15:57:05 -0700560 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
561 if (!widgetWasAdded) {
562 // If the widget was not added, we may need to do further cleanup.
563 PendingAddWidgetInfo info = mCreateWidgetInfo;
564 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700565
566 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700567 // We never did any preloading, so just remove pending callbacks to do so
568 removeCallbacks(mBindWidgetRunnable);
569 removeCallbacks(mInflateWidgetRunnable);
570 } else if (mWidgetCleanupState == WIDGET_BOUND) {
571 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800572 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700573 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
574 }
575
576 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700577 removeCallbacks(mInflateWidgetRunnable);
578 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700579 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800580 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700581 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
582 }
583
Adam Cohen0e56cc92012-05-11 15:57:05 -0700584 // The widget was inflated and added to the DragLayer -- remove it.
585 AppWidgetHostView widget = info.boundWidget;
586 mLauncher.getDragLayer().removeView(widget);
587 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 }
589 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
590 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700591 mCreateWidgetInfo = null;
592 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800593 }
594
Adam Cohen7a326642012-02-22 12:03:22 -0800595 @Override
596 public void cleanUpShortPress(View v) {
597 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700598 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800599 }
600 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800601
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700602 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800603 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700604 // Get the widget preview as the drag representation
605 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700606 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700607
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700608 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
609 // we abort the drag.
610 if (image.getDrawable() == null) {
611 mDraggingWidget = false;
612 return false;
613 }
614
Winson Chung4b576be2011-04-27 17:40:20 -0700615 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800616 Bitmap preview;
617 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800618 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100619 Point previewPadding = null;
620
Winson Chung1ed747a2011-05-03 16:18:34 -0700621 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700622 // This can happen in some weird cases involving multi-touch. We can't start dragging
623 // the widget if this is null, so we break out.
624 if (mCreateWidgetInfo == null) {
625 return false;
626 }
627
Adam Cohen1b36dc32012-02-13 19:27:37 -0800628 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
629 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700630 int spanX = createItemInfo.spanX;
631 int spanY = createItemInfo.spanY;
632 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
633 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700634
Winson Chung72d59842012-02-22 13:51:36 -0800635 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
636 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700637 int maxWidth, maxHeight;
638 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
639 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800640
Michael Jurka05713af2013-01-23 12:39:24 +0100641 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700642 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
643 spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100644
645 // Compare the size of the drag preview to the preview in the AppsCustomize tray
646 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100647 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100648 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
649
650 // The bitmap in the AppsCustomize tray is always the the same size, so there
651 // might be extra pixels around the preview itself - this accounts for that
652 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
653 int padding =
654 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
655 previewPadding = new Point(padding, 0);
656 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700657 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700658 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
659 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700660 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700661 createItemInfo.spanX = createItemInfo.spanY = 1;
662 }
Winson Chung4b576be2011-04-27 17:40:20 -0700663
Michael Jurka8c3339b2012-06-14 16:18:21 -0700664 // Don't clip alpha values for the drag outline if we're using the default widget preview
665 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
666 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700667
Winson Chung1120e032011-11-22 16:11:31 -0800668 // Save the preview for the outline generation, then dim the preview
669 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
670 false);
Winson Chung1120e032011-11-22 16:11:31 -0800671
Winson Chung4b576be2011-04-27 17:40:20 -0700672 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700673 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700674 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800675 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100676 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800677 outline.recycle();
678 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700679 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700680 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800681
Winson Chung4b576be2011-04-27 17:40:20 -0700682 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700684 if (!super.beginDragging(v)) return false;
685
Sunny Goyal508da152014-08-14 10:53:27 -0700686 if (v instanceof BubbleTextView) {
Winson Chung4b576be2011-04-27 17:40:20 -0700687 beginDraggingApplication(v);
688 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700689 if (!beginDraggingWidget(v)) {
690 return false;
691 }
Winson Chung4b576be2011-04-27 17:40:20 -0700692 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800693
694 // We delay entering spring-loaded mode slightly to make sure the UI
695 // thready is free of any work.
696 postDelayed(new Runnable() {
697 @Override
698 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800699 // We don't enter spring-loaded mode if the drag has been cancelled
700 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800701 // Go into spring loaded mode (must happen before we startDrag())
702 mLauncher.enterSpringLoadedDragMode();
703 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800704 }
Winson Chung72d59842012-02-22 13:51:36 -0800705 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800706
Winson Chung785d2eb2011-04-14 16:08:02 -0700707 return true;
708 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800709
Winson Chunga48487a2012-03-20 16:19:37 -0700710 /**
711 * Clean up after dragging.
712 *
713 * @param target where the item was dragged to (can be null if the item was flung)
714 */
715 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700716 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800717 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700718 // Exit spring loaded mode if we have not successfully dropped or have not handled the
719 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700720 mLauncher.exitSpringLoadedDragModeDelayed(true,
721 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700722 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700723 } else {
724 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700725 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700726 }
727
Winson Chung785d2eb2011-04-14 16:08:02 -0700728 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700729 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700730 if (getChildCount() > 0) {
731 return getChildAt(0);
732 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700733 return null;
734 }
735
736 @Override
737 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700738 mInTransition = true;
739 if (toWorkspace) {
740 cancelAllTasks();
741 }
742 }
743
744 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700745 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700746 }
747
748 @Override
749 public void onLauncherTransitionStep(Launcher l, float t) {
750 }
751
752 @Override
753 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
754 mInTransition = false;
755 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700756 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700757 }
758 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700759 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
760 r.run();
761 }
762 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700763 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700764 }
765
766 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700767 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
768 boolean success) {
769 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
770 if (isFlingToDelete) return;
771
772 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700773
774 // Display an error message if the drag failed due to there not being enough space on the
775 // target layout we were dropping on.
776 if (!success) {
777 boolean showOutOfSpaceMessage = false;
778 if (target instanceof Workspace) {
779 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
780 Workspace workspace = (Workspace) target;
781 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700782 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700783 if (layout != null) {
784 layout.calculateSpans(itemInfo);
785 showOutOfSpaceMessage =
786 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
787 }
788 }
Winson Chungfc79c802011-05-02 13:35:34 -0700789 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700790 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700791 }
Adam Cohen7a326642012-02-22 12:03:22 -0800792
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800793 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700794 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700795 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800796 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700797 }
798
Winson Chunga48487a2012-03-20 16:19:37 -0700799 @Override
800 public void onFlingToDeleteCompleted() {
801 // We just dismiss the drag when we fling, so cleanup here
802 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700803 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700804 mDraggingWidget = false;
805 }
806
807 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800808 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700809 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800810 }
811
Winson Chung7f0acdd2011-09-19 18:34:19 -0700812 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000813 public boolean supportsAppInfoDropTarget() {
814 return true;
815 }
816
817 @Override
818 public boolean supportsDeleteDropTarget() {
819 return false;
820 }
821
822 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800823 public float getIntrinsicIconScaleFactor() {
824 LauncherAppState app = LauncherAppState.getInstance();
825 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
826 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
827 }
828
829 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700830 protected void onDetachedFromWindow() {
831 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700832 cancelAllTasks();
833 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700834
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800835 @Override
836 public void trimMemory() {
837 super.trimMemory();
838 clearAllWidgetPages();
839 }
840
Michael Jurkae326f182011-11-21 14:05:46 -0800841 public void clearAllWidgetPages() {
842 cancelAllTasks();
843 int count = getChildCount();
844 for (int i = 0; i < count; i++) {
845 View v = getPageAt(i);
846 if (v instanceof PagedViewGridLayout) {
847 ((PagedViewGridLayout) v).removeAllViewsOnPage();
848 mDirtyPageContent.set(i, true);
849 }
850 }
851 }
852
Adam Cohen0cd3b642011-10-14 14:58:00 -0700853 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700854 // Clean up all the async tasks
855 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
856 while (iter.hasNext()) {
857 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
858 task.cancel(false);
859 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700860 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700861
862 // We've already preallocated the views for the data to load into, so clear them as well
863 View v = getPageAt(task.page);
864 if (v instanceof PagedViewGridLayout) {
865 ((PagedViewGridLayout) v).removeAllViewsOnPage();
866 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700867 }
Winson Chung83687b12012-04-25 16:01:01 -0700868 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700869 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700870 }
871
Winson Chung785d2eb2011-04-14 16:08:02 -0700872 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200873 // Widgets appear to be cleared every time you leave, always force invalidate for them
874 if (mContentType != type || type == ContentType.Widgets) {
875 int page = (mContentType != type) ? 0 : getCurrentPage();
876 mContentType = type;
877 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700878 }
Winson Chungc58497e2013-09-03 17:48:37 -0700879 }
880
881 public ContentType getContentType() {
882 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700883 }
884
Adam Cohen0cd3b642011-10-14 14:58:00 -0700885 protected void snapToPage(int whichPage, int delta, int duration) {
886 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800887
888 // Update the thread priorities given the direction lookahead
889 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
890 while (iter.hasNext()) {
891 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700892 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800893 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
894 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
895 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
896 } else {
897 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
898 }
899 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700900 }
901
Winson Chung785d2eb2011-04-14 16:08:02 -0700902 /*
903 * Apps PagedView implementation
904 */
Winson Chung63257c12011-05-05 17:06:13 -0700905 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
906 int childCount = layout.getChildCount();
907 for (int i = 0; i < childCount; ++i) {
908 layout.getChildAt(i).setVisibility(visibility);
909 }
910 }
Winson Chungc58497e2013-09-03 17:48:37 -0700911 private void setupPage(AppsCustomizeCellLayout layout) {
912 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700913
Winson Chung63257c12011-05-05 17:06:13 -0700914 // Note: We force a measure here to get around the fact that when we do layout calculations
915 // immediately after syncing, we don't have a proper width. That said, we already know the
916 // expected page width, so we can actually optimize by hiding all the TextView-based
917 // children that are expensive to measure, and let that happen naturally later.
918 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700919 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
920 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700921 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700922
Adam Cohen4e243a22014-08-10 18:30:55 -0700923 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700924 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700925 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700926 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700927 }
928
929 setVisibilityOnChildren(layout, View.VISIBLE);
930 }
931
932 public void setPageBackgroundsVisible(boolean visible) {
933 mPageBackgroundsVisible = visible;
934 int childCount = getChildCount();
935 for (int i = 0; i < childCount; ++i) {
936 Drawable bg = getChildAt(i).getBackground();
937 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700938 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700939 }
940 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700941 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700942
Winson Chungf314b0e2011-08-16 11:54:27 -0700943 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700945 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700946 int numCells = mCellCountX * mCellCountY;
947 int startIndex = page * numCells;
948 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700949 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700950
Winson Chung785d2eb2011-04-14 16:08:02 -0700951 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700952 ArrayList<Object> items = new ArrayList<Object>();
953 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700954 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200955 AppInfo info = mApps.get(i);
Sunny Goyal508da152014-08-14 10:53:27 -0700956 BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700957 R.layout.apps_customize_application, layout, false);
Sunny Goyal508da152014-08-14 10:53:27 -0700958 icon.applyFromApplicationInfo(info);
959 icon.setOnClickListener(mLauncher);
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 icon.setOnLongClickListener(this);
961 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800962 icon.setOnKeyListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700963 icon.setOnFocusChangeListener(layout.mFocusHandlerView);
Winson Chung785d2eb2011-04-14 16:08:02 -0700964
965 int index = i - startIndex;
966 int x = index % mCellCountX;
967 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -0700968 if (isRtl) {
969 x = mCellCountX - x - 1;
970 }
Winson Chungc58497e2013-09-03 17:48:37 -0700971 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -0700972
973 items.add(info);
974 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700976
Michael Jurka47639b92013-01-14 12:42:27 +0100977 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -0700978 }
Winson Chungb44b5242011-06-13 11:32:14 -0700979
980 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800981 * A helper to return the priority for loading of the specified widget page.
982 */
983 private int getWidgetPageLoadPriority(int page) {
984 // If we are snapping to another page, use that index as the target page index
985 int toPage = mCurrentPage;
986 if (mNextPage > -1) {
987 toPage = mNextPage;
988 }
989
990 // We use the distance from the target page as an initial guess of priority, but if there
991 // are no pages of higher priority than the page specified, then bump up the priority of
992 // the specified page.
993 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
994 int minPageDiff = Integer.MAX_VALUE;
995 while (iter.hasNext()) {
996 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700997 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800998 }
999
1000 int rawPageDiff = Math.abs(page - toPage);
1001 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1002 }
1003 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001004 * Return the appropriate thread priority for loading for a given page (we give the current
1005 * page much higher priority)
1006 */
1007 private int getThreadPriorityForPage(int page) {
1008 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001009 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001010 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001011 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001012 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001013 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001014 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001015 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001016 }
1017 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001018 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001019 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001020 return Math.max(0, pageDiff * sPageSleepDelay);
1021 }
Winson Chungb44b5242011-06-13 11:32:14 -07001022 /**
1023 * Creates and executes a new AsyncTask to load a page of widget previews.
1024 */
1025 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001026 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001027
Winson Chungb44b5242011-06-13 11:32:14 -07001028 // Prune all tasks that are no longer needed
1029 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1030 while (iter.hasNext()) {
1031 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001032 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001033 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1034 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001035 task.cancel(false);
1036 iter.remove();
1037 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001038 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001039 }
1040 }
1041
Winson Chungf314b0e2011-08-16 11:54:27 -07001042 // 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 -07001043 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001044 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001045 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001046 @Override
1047 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001048 try {
Winson Chung09945932011-09-20 14:22:40 -07001049 try {
1050 Thread.sleep(sleepMs);
1051 } catch (Exception e) {}
1052 loadWidgetPreviewsInBackground(task, data);
1053 } finally {
1054 if (task.isCancelled()) {
1055 data.cleanup(true);
1056 }
1057 }
Winson Chungb44b5242011-06-13 11:32:14 -07001058 }
1059 },
1060 new AsyncTaskCallback() {
1061 @Override
1062 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001063 mRunningTasks.remove(task);
1064 if (task.isCancelled()) return;
1065 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001066 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001067 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001068 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001069
1070 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001071 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001072 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001073 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001074 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1075 mRunningTasks.add(t);
1076 }
Winson Chungb44b5242011-06-13 11:32:14 -07001077
Winson Chung785d2eb2011-04-14 16:08:02 -07001078 /*
1079 * Widgets PagedView implementation
1080 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001081 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001082 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001083 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001084 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1085 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001086
1087 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1088 if (bg != null) {
1089 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1090 layout.setBackground(bg);
1091 }
Winson Chung63257c12011-05-05 17:06:13 -07001092 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001093 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001094
Michael Jurka038f9d82011-11-03 13:50:45 -07001095 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001096 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001097
Adam Cohen4e243a22014-08-10 18:30:55 -07001098 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1099
Winson Chungd2945262011-06-24 15:22:14 -07001100 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001101 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001102 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001103 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001104 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1105
Adam Cohen4e243a22014-08-10 18:30:55 -07001106 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001107
Winson Chunge4a647f2011-09-30 14:41:25 -07001108 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001109 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001110 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1111 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001112 }
1113
Winson Chunge4a647f2011-09-30 14:41:25 -07001114 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001115 layout.setColumnCount(layout.getCellCountX());
1116 for (int i = 0; i < items.size(); ++i) {
1117 Object rawInfo = items.get(i);
1118 PendingAddItemInfo createItemInfo = null;
1119 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1120 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001121
1122 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001123 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001124 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1125 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001126
Adam Cohen59400422014-03-05 18:07:04 -08001127 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001128 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001129 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001130 } else if (rawInfo instanceof ResolveInfo) {
1131 // Fill in the shortcuts information
1132 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001133 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001134 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1135 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1136 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001137 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001138 widget.setTag(createItemInfo);
1139 }
Adam Cohen59400422014-03-05 18:07:04 -08001140
Winson Chunge4a647f2011-09-30 14:41:25 -07001141 widget.setOnClickListener(this);
1142 widget.setOnLongClickListener(this);
1143 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001144 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001145
1146 // Layout each widget
1147 int ix = i % mWidgetCountX;
1148 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001149
1150 if (ix > 0) {
1151 View border = widget.findViewById(R.id.left_border);
1152 border.setVisibility(View.VISIBLE);
1153 }
1154 if (ix < mWidgetCountX - 1) {
1155 View border = widget.findViewById(R.id.right_border);
1156 border.setVisibility(View.VISIBLE);
1157 }
1158
Winson Chunge4a647f2011-09-30 14:41:25 -07001159 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001160 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001161 GridLayout.spec(ix, GridLayout.TOP));
1162 lp.width = cellWidth;
1163 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001164 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001165 layout.addView(widget, lp);
1166 }
1167
Michael Jurka038f9d82011-11-03 13:50:45 -07001168 // wait until a call on onLayout to start loading, because
1169 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1170 // TODO: can we do a measure/layout immediately?
1171 layout.setOnLayoutListener(new Runnable() {
1172 public void run() {
1173 // Load the widget previews
1174 int maxPreviewWidth = cellWidth;
1175 int maxPreviewHeight = cellHeight;
1176 if (layout.getChildCount() > 0) {
1177 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1178 int[] maxSize = w.getPreviewSize();
1179 maxPreviewWidth = maxSize[0];
1180 maxPreviewHeight = maxSize[1];
1181 }
Michael Jurka05713af2013-01-23 12:39:24 +01001182
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001183 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001184 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001185 if (immediate) {
1186 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001187 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001188 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001189 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001190 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001191 if (mInTransition) {
1192 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1193 } else {
1194 prepareLoadWidgetPreviewsTask(page, items,
1195 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1196 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001197 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001198 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001199 }
1200 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001201 }
1202 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1203 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001204 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1205 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001206 if (task != null) {
1207 // Ensure that this task starts running at the correct priority
1208 task.syncThreadPriority();
1209 }
1210
1211 // Load each of the widget/shortcut previews
1212 ArrayList<Object> items = data.items;
1213 ArrayList<Bitmap> images = data.generatedImages;
1214 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001215 for (int i = 0; i < count; ++i) {
1216 if (task != null) {
1217 // Ensure we haven't been cancelled yet
1218 if (task.isCancelled()) break;
1219 // Before work on each item, ensure that this task is running at the correct
1220 // priority
1221 task.syncThreadPriority();
1222 }
1223
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001224 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001225 }
Winson Chungb44b5242011-06-13 11:32:14 -07001226 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001227
Winson Chung7bb37522013-10-28 11:07:57 -07001228 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1229 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001230 mDeferredSyncWidgetPageItems.add(data);
1231 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001232 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001233 try {
1234 int page = data.page;
1235 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001236
Michael Jurka39e5d172012-03-12 18:36:12 -07001237 ArrayList<Object> items = data.items;
1238 int count = items.size();
1239 for (int i = 0; i < count; ++i) {
1240 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1241 if (widget != null) {
1242 Bitmap preview = data.generatedImages.get(i);
1243 widget.applyPreview(new FastBitmapDrawable(preview), i);
1244 }
1245 }
Winson Chung68e4c642011-11-10 15:48:25 -08001246
Michael Jurka47639b92013-01-14 12:42:27 +01001247 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001248
1249 // Update all thread priorities
1250 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1251 while (iter.hasNext()) {
1252 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1253 int pageIndex = task.page;
1254 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1255 }
1256 } finally {
1257 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001258 }
Winson Chungb44b5242011-06-13 11:32:14 -07001259 }
Winson Chung46af2e82011-05-09 16:00:53 -07001260
Winson Chung785d2eb2011-04-14 16:08:02 -07001261 @Override
1262 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001263 disablePagedViewAnimations();
1264
Winson Chung785d2eb2011-04-14 16:08:02 -07001265 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001266 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001267
Adam Cohen0cd3b642011-10-14 14:58:00 -07001268 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001269 if (mContentType == ContentType.Applications) {
1270 for (int i = 0; i < mNumAppsPages; ++i) {
1271 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1272 setupPage(layout);
1273 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1274 LayoutParams.MATCH_PARENT));
1275 }
1276 } else if (mContentType == ContentType.Widgets) {
1277 for (int j = 0; j < mNumWidgetPages; ++j) {
1278 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1279 mWidgetCountY);
1280 setupPage(layout);
1281 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1282 LayoutParams.MATCH_PARENT));
1283 }
1284 } else {
1285 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001286 }
1287
Winson Chungc58497e2013-09-03 17:48:37 -07001288 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001290
Winson Chung785d2eb2011-04-14 16:08:02 -07001291 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001292 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001293 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001294 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001295 } else {
1296 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001297 }
1298 }
1299
Adam Cohen22f823d2011-09-01 17:22:18 -07001300 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1301 // it is in the z-order. This is important to insure touch events are handled correctly.
1302 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001303 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001304 }
1305
Adam Cohenae4f1552011-10-20 00:15:42 -07001306 @Override
1307 protected int indexToPage(int index) {
1308 return getChildCount() - index - 1;
1309 }
1310
Adam Cohen22f823d2011-09-01 17:22:18 -07001311 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1312 @Override
1313 protected void screenScrolled(int screenCenter) {
1314 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001315 enableHwLayersOnVisiblePages();
1316 }
1317
1318 private void enableHwLayersOnVisiblePages() {
1319 final int screenCount = getChildCount();
1320
1321 getVisiblePages(mTempVisiblePagesRange);
1322 int leftScreen = mTempVisiblePagesRange[0];
1323 int rightScreen = mTempVisiblePagesRange[1];
1324 int forceDrawScreen = -1;
1325 if (leftScreen == rightScreen) {
1326 // make sure we're caching at least two pages always
1327 if (rightScreen < screenCount - 1) {
1328 rightScreen++;
1329 forceDrawScreen = rightScreen;
1330 } else if (leftScreen > 0) {
1331 leftScreen--;
1332 forceDrawScreen = leftScreen;
1333 }
1334 } else {
1335 forceDrawScreen = leftScreen + 1;
1336 }
1337
1338 for (int i = 0; i < screenCount; i++) {
1339 final View layout = (View) getPageAt(i);
1340 if (!(leftScreen <= i && i <= rightScreen &&
1341 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1342 layout.setLayerType(LAYER_TYPE_NONE, null);
1343 }
1344 }
1345
Michael Jurka47639b92013-01-14 12:42:27 +01001346 for (int i = 0; i < screenCount; i++) {
1347 final View layout = (View) getPageAt(i);
1348
1349 if (leftScreen <= i && i <= rightScreen &&
1350 (i == forceDrawScreen || shouldDrawChild(layout))) {
1351 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1352 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1353 }
1354 }
1355 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 }
1357
1358 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001359 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001360 }
1361
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 /**
1363 * Used by the parent to get the content width to set the tab bar to
1364 * @return
1365 */
1366 public int getPageContentWidth() {
1367 return mContentWidth;
1368 }
1369
Winson Chungb26f3d62011-06-02 10:49:29 -07001370 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001371 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001372 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001373 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001374 // We reset the save index when we change pages so that it will be recalculated on next
1375 // rotation
1376 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001377 }
1378
Winson Chung785d2eb2011-04-14 16:08:02 -07001379 /*
1380 * AllAppsView implementation
1381 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001382 public void setup(Launcher launcher, DragController dragController) {
1383 mLauncher = launcher;
1384 mDragController = dragController;
1385 }
Winson Chung9802ac92012-06-08 16:01:58 -07001386
1387 /**
1388 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1389 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1390 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1391 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1392 */
1393 private void invalidateOnDataChange() {
1394 if (!isDataReady()) {
1395 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1396 // request a layout to trigger the page data when ready.
1397 requestLayout();
1398 } else {
1399 cancelAllTasks();
1400 invalidatePageData();
1401 }
1402 }
1403
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001404 public void setApps(ArrayList<AppInfo> list) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001405 mApps = list;
1406 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1407 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001408 }
Adam Cohen59400422014-03-05 18:07:04 -08001409
1410 public ArrayList<AppInfo> getApps() {
1411 return mApps;
1412 }
1413
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001414 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001415 // We add it in place, in alphabetical order
1416 int count = list.size();
1417 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001418 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001419 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001420 if (index < 0) {
1421 mApps.add(-(index + 1), info);
1422 }
1423 }
1424 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001425 public void addApps(ArrayList<AppInfo> list) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001426 addAppsWithoutInvalidate(list);
1427 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001428 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001429 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001430 ComponentName removeComponent = item.intent.getComponent();
1431 int length = list.size();
1432 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001433 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001434 if (info.user.equals(item.user)
1435 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001436 return i;
1437 }
1438 }
1439 return -1;
1440 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001441 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001442 // loop through all the apps and remove apps that have the same component
1443 int length = list.size();
1444 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001445 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001446 int removeIndex = findAppByComponent(mApps, info);
1447 if (removeIndex > -1) {
1448 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001449 }
1450 }
1451 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001452 public void removeApps(ArrayList<AppInfo> appInfos) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001453 removeAppsWithoutInvalidate(appInfos);
1454 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001456 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 // We remove and re-add the updated applications list because it's properties may have
1458 // changed (ie. the title), and this will ensure that the items will be in their proper
1459 // place in the list.
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001460 removeAppsWithoutInvalidate(list);
1461 addAppsWithoutInvalidate(list);
1462 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001463 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001464
Winson Chung785d2eb2011-04-14 16:08:02 -07001465 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001466 // If we have reset, then we should not continue to restore the previous state
1467 mSaveInstanceStateItemIndex = -1;
1468
Adam Cohen6c5891a2014-07-09 23:53:15 -07001469 if (mContentType != ContentType.Applications) {
1470 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001471 }
Winson Chung649668f2012-01-10 13:07:16 -08001472
Adam Cohenb64d36e2011-10-17 21:48:02 -07001473 if (mCurrentPage != 0) {
1474 invalidatePageData(0);
1475 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001476 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001477
1478 private AppsCustomizeTabHost getTabHost() {
1479 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1480 }
1481
Winson Chung785d2eb2011-04-14 16:08:02 -07001482 public void dumpState() {
1483 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001484 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001485 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 }
Adam Cohen4e844012011-11-09 13:48:04 -08001487
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001489 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001490 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001491 for (Object i: list) {
1492 if (i instanceof AppWidgetProviderInfo) {
1493 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1494 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1495 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1496 + " initialLayout=" + info.initialLayout
1497 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1498 } else if (i instanceof ResolveInfo) {
1499 ResolveInfo info = (ResolveInfo) i;
1500 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1501 + info.icon);
1502 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001503 }
1504 }
Adam Cohen4e844012011-11-09 13:48:04 -08001505
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 public void surrender() {
1507 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1508 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001509
1510 // Stop all background tasks
1511 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001512 }
Winson Chung007c6982011-06-14 13:27:53 -07001513
Winson Chungb44b5242011-06-13 11:32:14 -07001514 /*
1515 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1516 * widget previews in the background with the AsyncTasks.
1517 */
Winson Chung68e4c642011-11-10 15:48:25 -08001518 final static int sLookBehindPageCount = 2;
1519 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001520 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001521 final int count = getChildCount();
1522 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1523 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1524 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001525 }
1526 protected int getAssociatedUpperPageBound(int page) {
1527 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001528 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1529 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1530 count - 1);
1531 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001532 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001533
Winson Chung6a0f57d2011-06-29 20:10:49 -07001534 protected String getCurrentPageDescription() {
1535 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1536 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001537 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001538
1539 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001540 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001541 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001542 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001543 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001544 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001545 } else {
1546 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001547 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001548
Michael Jurka8b805b12012-04-18 14:23:14 -07001549 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001550 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001551}