blob: 9f8d499eb453d3cf72e11b0006c1f1db73f1be2a [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 Chung785d2eb2011-04-14 16:08:02 -070039import android.view.LayoutInflater;
40import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070041import android.view.ViewGroup;
Winson Chungfd3385f2011-06-15 19:51:24 -070042import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070044import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070045
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal290800b2015-03-05 11:33:33 -080047import com.android.launcher3.FocusHelper.PagedViewKeyListener;
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
Sunny Goyal290800b2015-03-05 11:33:33 -0800142 View.OnClickListener, 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
Sunny Goyal290800b2015-03-05 11:33:33 -0800185 private final PagedViewKeyListener mKeyListener = new PagedViewKeyListener();
186
Adam Cohened66b2b2012-01-23 17:28:51 -0800187 private Runnable mInflateWidgetRunnable = null;
188 private Runnable mBindWidgetRunnable = null;
189 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700190 static final int WIDGET_PRELOAD_PENDING = 0;
191 static final int WIDGET_BOUND = 1;
192 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800193 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
194 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800195 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800196 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700197 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800198
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700199 private Toast mWidgetInstructionToast;
200
Michael Jurka39e5d172012-03-12 18:36:12 -0700201 // Deferral of loading widget previews during launcher transitions
202 private boolean mInTransition;
203 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
204 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700205 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
206 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700207
Michael Jurka05713af2013-01-23 12:39:24 +0100208 WidgetPreviewLoader mWidgetPreviewLoader;
209
Michael Jurkac402cd92013-05-20 15:49:32 +0200210 private boolean mInBulkBind;
211 private boolean mNeedToUpdatePageCountsAndInvalidateData;
212
Winson Chung785d2eb2011-04-14 16:08:02 -0700213 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
214 super(context, attrs);
215 mLayoutInflater = LayoutInflater.from(context);
216 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200217 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700218 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400219 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700220 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700221
222 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800223 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700224 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
225 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
226 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700227 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700228
Winson Chung1ed747a2011-05-03 16:18:34 -0700229 // The padding on the non-matched dimension for the default widget preview icons
230 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700231 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700232
233 // Unless otherwise specified this view is important for accessibility.
234 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
235 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
236 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700237 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700238 }
239
240 @Override
241 protected void init() {
242 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700243 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700244
245 Context context = getContext();
246 Resources r = context.getResources();
247 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
248 }
249
Winson Chungc58497e2013-09-03 17:48:37 -0700250 public void onFinishInflate() {
251 super.onFinishInflate();
252
253 LauncherAppState app = LauncherAppState.getInstance();
254 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
255 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
256 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
257 }
258
Winson Chung67ca7e42013-10-31 16:53:19 -0700259 void setAllAppsPadding(Rect r) {
260 mAllAppsPadding.set(r);
261 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700262
Winson Chung67ca7e42013-10-31 16:53:19 -0700263 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700264 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700265 }
266
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100267 WidgetPreviewLoader getWidgetPreviewLoader() {
268 if (mWidgetPreviewLoader == null) {
269 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
270 }
271 return mWidgetPreviewLoader;
272 }
273
Winson Chung5afbf7b2011-07-25 11:53:08 -0700274 /** Returns the item index of the center item on this page so that we can restore to this
275 * item index when we rotate. */
276 private int getMiddleComponentIndexOnCurrentPage() {
277 int i = -1;
278 if (getPageCount() > 0) {
279 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700280 if (mContentType == ContentType.Applications) {
281 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
282 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700283 int numItemsPerPage = mCellCountX * mCellCountY;
284 int childCount = childrenLayout.getChildCount();
285 if (childCount > 0) {
286 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700287 }
Winson Chungc58497e2013-09-03 17:48:37 -0700288 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700289 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700290 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700291 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
292 int childCount = layout.getChildCount();
293 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700294 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700295 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700296 }
Winson Chungc58497e2013-09-03 17:48:37 -0700297 } else {
298 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700299 }
300 }
301 return i;
302 }
303
304 /** Get the index of the item to restore to if we need to restore the current page. */
305 int getSaveInstanceStateIndex() {
306 if (mSaveInstanceStateItemIndex == -1) {
307 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
308 }
309 return mSaveInstanceStateItemIndex;
310 }
311
312 /** Returns the page in the current orientation which is expected to contain the specified
313 * item index. */
314 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700315 if (index < 0) return 0;
316
317 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700318 int numItemsPerPage = mCellCountX * mCellCountY;
319 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700320 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700321 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700322 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700323 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700324 }
325
Winson Chung5afbf7b2011-07-25 11:53:08 -0700326 /** Restores the page for an item at the specified index */
327 void restorePageForIndex(int index) {
328 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700329 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 }
331
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700332 private void updatePageCounts() {
333 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
334 (float) (mWidgetCountX * mWidgetCountY));
335 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
336 }
337
Winson Chungf0ea4d32011-06-06 14:27:16 -0700338 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 // Now that the data is ready, we can calculate the content width, the number of cells to
340 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700341 LauncherAppState app = LauncherAppState.getInstance();
342 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700343 mCellCountX = (int) grid.allAppsNumCols;
344 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700345 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700346
Winson Chungdb1138b2011-06-30 14:39:35 -0700347 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700348 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
349 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
350 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
351 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700352 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700353
Adam Cohen6c5891a2014-07-09 23:53:15 -0700354 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700355 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800356 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800357 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700358
Adam Cohena00673c2014-08-14 12:57:28 -0700359 protected void onLayout(boolean changed, int l, int t, int r, int b) {
360 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700361
Winson Chungf0ea4d32011-06-06 14:27:16 -0700362 if (!isDataReady()) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800363 if ((!mApps.isEmpty()) && !mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700364 post(new Runnable() {
365 // This code triggers requestLayout so must be posted outside of the
366 // layout pass.
367 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800368 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200369 setDataIsReady();
370 onDataReady(getMeasuredWidth(), getMeasuredHeight());
371 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700372 }
373 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
375 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 }
377
Michael Jurkac402cd92013-05-20 15:49:32 +0200378 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700379 LauncherAppState app = LauncherAppState.getInstance();
380 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
381
Winson Chung1ed747a2011-05-03 16:18:34 -0700382 // Get the list of widgets and shortcuts
383 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200384 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800385 if (o instanceof LauncherAppWidgetProviderInfo) {
386 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
387 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100388 continue;
389 }
Adam Cohen59400422014-03-05 18:07:04 -0800390
391 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200392 // Ensure that all widgets we show can be added on a workspace of this size
393 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
394 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
395 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
396 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700397 if (minSpanX <= (int) grid.numColumns &&
398 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200399 mWidgets.add(widget);
400 } else {
401 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
402 widget.minWidth + ", " + widget.minHeight + ")");
403 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700404 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200405 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
406 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700407 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800408 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200409 // just add shortcuts
410 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800411 }
412 }
Adam Cohen59400422014-03-05 18:07:04 -0800413
Michael Jurkac402cd92013-05-20 15:49:32 +0200414 updatePageCountsAndInvalidateData();
415 }
416
417 public void setBulkBind(boolean bulkBind) {
418 if (bulkBind) {
419 mInBulkBind = true;
420 } else {
421 mInBulkBind = false;
422 if (mNeedToUpdatePageCountsAndInvalidateData) {
423 updatePageCountsAndInvalidateData();
424 }
425 }
426 }
427
428 private void updatePageCountsAndInvalidateData() {
429 if (mInBulkBind) {
430 mNeedToUpdatePageCountsAndInvalidateData = true;
431 } else {
432 updatePageCounts();
433 invalidateOnDataChange();
434 mNeedToUpdatePageCountsAndInvalidateData = false;
435 }
Winson Chung4b576be2011-04-27 17:40:20 -0700436 }
437
438 @Override
439 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700440 // When we have exited all apps or are in transition, disregard clicks
Sunny Goyal508da152014-08-14 10:53:27 -0700441 if (!mLauncher.isAllAppsVisible()
442 || mLauncher.getWorkspace().isSwitchingState()
443 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700444
Sunny Goyal508da152014-08-14 10:53:27 -0700445 // Let the user know that they have to long press to add a widget
446 if (mWidgetInstructionToast != null) {
447 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700448 }
Sunny Goyal508da152014-08-14 10:53:27 -0700449 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
450 Toast.LENGTH_SHORT);
451 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700452 }
453
454 /*
455 * PagedViewWithDraggableItems implementation
456 */
457 @Override
458 protected void determineDraggingStart(android.view.MotionEvent ev) {
459 // Disable dragging by pulling an app down for now.
460 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700461
Winson Chung4b576be2011-04-27 17:40:20 -0700462 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700463 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700464 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700465
Sunny Goyalff572272014-07-23 13:58:07 -0700466 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700467 Bundle options = null;
468 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700469 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
470 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700471 info.componentName, null);
472
Sunny Goyalff572272014-07-23 13:58:07 -0700473 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700474 int xPaddingDips = (int) ((padding.left + padding.right) / density);
475 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
476
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700477 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700478 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700479 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700480 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700481 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700482 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700483 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700484 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700485 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700486 }
487 return options;
488 }
489
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700490 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800491 final LauncherAppWidgetProviderInfo pInfo = info.info;
492 final Bundle options = pInfo.isCustomWidget ? null :
493 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700494
Adam Cohened66b2b2012-01-23 17:28:51 -0800495 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700496 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800497 return;
498 }
499
Adam Cohen21a170b2012-05-30 15:17:06 -0700500 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800501 mBindWidgetRunnable = new Runnable() {
502 @Override
503 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800504 if (pInfo.isCustomWidget) {
505 mWidgetCleanupState = WIDGET_BOUND;
506 return;
507 }
508
Adam Cohened66b2b2012-01-23 17:28:51 -0800509 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700510 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
511 mWidgetLoadingId, pInfo, options)) {
512 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700513 }
Adam Cohen59400422014-03-05 18:07:04 -0800514
Adam Cohened66b2b2012-01-23 17:28:51 -0800515 }
516 };
517 post(mBindWidgetRunnable);
518
519 mInflateWidgetRunnable = new Runnable() {
520 @Override
521 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700522 if (mWidgetCleanupState != WIDGET_BOUND) {
523 return;
524 }
Adam Cohen59400422014-03-05 18:07:04 -0800525 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
526 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800527 info.boundWidget = hostView;
528 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800529 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700530 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
531 info.spanY, info, false);
532
533 // We want the first widget layout to be the correct size. This will be important
534 // for width size reporting to the AppWidgetManager.
535 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
536 unScaledSize[1]);
537 lp.x = lp.y = 0;
538 lp.customPosition = true;
539 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800540 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800541 }
542 };
543 post(mInflateWidgetRunnable);
544 }
545
546 @Override
547 public void onShortPress(View v) {
548 // We are anticipating a long press, and we use this time to load bind and instantiate
549 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700550 if (mCreateWidgetInfo != null) {
551 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
552 cleanupWidgetPreloading(false);
553 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800554 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700555 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800556 }
557
Adam Cohen0e56cc92012-05-11 15:57:05 -0700558 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
559 if (!widgetWasAdded) {
560 // If the widget was not added, we may need to do further cleanup.
561 PendingAddWidgetInfo info = mCreateWidgetInfo;
562 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700563
564 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700565 // We never did any preloading, so just remove pending callbacks to do so
566 removeCallbacks(mBindWidgetRunnable);
567 removeCallbacks(mInflateWidgetRunnable);
568 } else if (mWidgetCleanupState == WIDGET_BOUND) {
569 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800570 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700571 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
572 }
573
574 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700575 removeCallbacks(mInflateWidgetRunnable);
576 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700577 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800578 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700579 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
580 }
581
Adam Cohen0e56cc92012-05-11 15:57:05 -0700582 // The widget was inflated and added to the DragLayer -- remove it.
583 AppWidgetHostView widget = info.boundWidget;
584 mLauncher.getDragLayer().removeView(widget);
585 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 }
587 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
588 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700589 mCreateWidgetInfo = null;
590 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 }
592
Adam Cohen7a326642012-02-22 12:03:22 -0800593 @Override
594 public void cleanUpShortPress(View v) {
595 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700596 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800597 }
598 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800599
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700600 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800601 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700602 // Get the widget preview as the drag representation
603 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700604 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700605
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700606 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
607 // we abort the drag.
608 if (image.getDrawable() == null) {
609 mDraggingWidget = false;
610 return false;
611 }
612
Winson Chung4b576be2011-04-27 17:40:20 -0700613 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800614 Bitmap preview;
615 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800616 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100617 Point previewPadding = null;
618
Winson Chung1ed747a2011-05-03 16:18:34 -0700619 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700620 // This can happen in some weird cases involving multi-touch. We can't start dragging
621 // the widget if this is null, so we break out.
622 if (mCreateWidgetInfo == null) {
623 return false;
624 }
625
Adam Cohen1b36dc32012-02-13 19:27:37 -0800626 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
627 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700628 int spanX = createItemInfo.spanX;
629 int spanY = createItemInfo.spanY;
630 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
631 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700632
Winson Chung72d59842012-02-22 13:51:36 -0800633 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
634 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700635 int maxWidth, maxHeight;
636 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
637 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800638
Michael Jurka05713af2013-01-23 12:39:24 +0100639 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700640 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
641 spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100642
643 // Compare the size of the drag preview to the preview in the AppsCustomize tray
644 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100645 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100646 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
647
648 // The bitmap in the AppsCustomize tray is always the the same size, so there
649 // might be extra pixels around the preview itself - this accounts for that
650 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
651 int padding =
652 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
653 previewPadding = new Point(padding, 0);
654 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700655 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700656 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
657 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700658 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700659 createItemInfo.spanX = createItemInfo.spanY = 1;
660 }
Winson Chung4b576be2011-04-27 17:40:20 -0700661
Michael Jurka8c3339b2012-06-14 16:18:21 -0700662 // Don't clip alpha values for the drag outline if we're using the default widget preview
663 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
664 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700665
Winson Chung1120e032011-11-22 16:11:31 -0800666 // Save the preview for the outline generation, then dim the preview
667 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
668 false);
Winson Chung1120e032011-11-22 16:11:31 -0800669
Winson Chung4b576be2011-04-27 17:40:20 -0700670 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700671 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700672 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800673 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100674 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800675 outline.recycle();
676 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700677 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679
Winson Chung4b576be2011-04-27 17:40:20 -0700680 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700682 if (!super.beginDragging(v)) return false;
683
Sunny Goyal508da152014-08-14 10:53:27 -0700684 if (v instanceof BubbleTextView) {
Winson Chung4b576be2011-04-27 17:40:20 -0700685 beginDraggingApplication(v);
686 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700687 if (!beginDraggingWidget(v)) {
688 return false;
689 }
Winson Chung4b576be2011-04-27 17:40:20 -0700690 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800691
692 // We delay entering spring-loaded mode slightly to make sure the UI
693 // thready is free of any work.
694 postDelayed(new Runnable() {
695 @Override
696 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800697 // We don't enter spring-loaded mode if the drag has been cancelled
698 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800699 // Go into spring loaded mode (must happen before we startDrag())
700 mLauncher.enterSpringLoadedDragMode();
701 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 }
Winson Chung72d59842012-02-22 13:51:36 -0800703 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800704
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 return true;
706 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800707
Winson Chunga48487a2012-03-20 16:19:37 -0700708 /**
709 * Clean up after dragging.
710 *
711 * @param target where the item was dragged to (can be null if the item was flung)
712 */
713 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700714 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800715 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700716 // Exit spring loaded mode if we have not successfully dropped or have not handled the
717 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700718 mLauncher.exitSpringLoadedDragModeDelayed(true,
719 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700721 } else {
722 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700723 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 }
725
Winson Chung785d2eb2011-04-14 16:08:02 -0700726 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700727 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700728 if (getChildCount() > 0) {
729 return getChildAt(0);
730 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700731 return null;
732 }
733
734 @Override
735 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700736 mInTransition = true;
737 if (toWorkspace) {
738 cancelAllTasks();
739 }
740 }
741
742 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700743 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700744 }
745
746 @Override
747 public void onLauncherTransitionStep(Launcher l, float t) {
748 }
749
750 @Override
751 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
752 mInTransition = false;
753 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700754 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700755 }
756 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700757 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
758 r.run();
759 }
760 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700761 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700762 }
763
764 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700765 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
766 boolean success) {
767 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
768 if (isFlingToDelete) return;
769
770 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700771
772 // Display an error message if the drag failed due to there not being enough space on the
773 // target layout we were dropping on.
774 if (!success) {
775 boolean showOutOfSpaceMessage = false;
776 if (target instanceof Workspace) {
777 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
778 Workspace workspace = (Workspace) target;
779 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700780 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700781 if (layout != null) {
782 layout.calculateSpans(itemInfo);
783 showOutOfSpaceMessage =
784 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
785 }
786 }
Winson Chungfc79c802011-05-02 13:35:34 -0700787 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700788 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700789 }
Adam Cohen7a326642012-02-22 12:03:22 -0800790
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800791 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700792 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700793 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800794 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700795 }
796
Winson Chunga48487a2012-03-20 16:19:37 -0700797 @Override
798 public void onFlingToDeleteCompleted() {
799 // We just dismiss the drag when we fling, so cleanup here
800 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700801 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700802 mDraggingWidget = false;
803 }
804
805 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800806 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700807 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800808 }
809
Winson Chung7f0acdd2011-09-19 18:34:19 -0700810 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000811 public boolean supportsAppInfoDropTarget() {
812 return true;
813 }
814
815 @Override
816 public boolean supportsDeleteDropTarget() {
817 return false;
818 }
819
820 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800821 public float getIntrinsicIconScaleFactor() {
822 LauncherAppState app = LauncherAppState.getInstance();
823 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
824 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
825 }
826
827 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700828 protected void onDetachedFromWindow() {
829 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700830 cancelAllTasks();
831 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700832
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800833 @Override
834 public void trimMemory() {
835 super.trimMemory();
836 clearAllWidgetPages();
837 }
838
Michael Jurkae326f182011-11-21 14:05:46 -0800839 public void clearAllWidgetPages() {
840 cancelAllTasks();
841 int count = getChildCount();
842 for (int i = 0; i < count; i++) {
843 View v = getPageAt(i);
844 if (v instanceof PagedViewGridLayout) {
845 ((PagedViewGridLayout) v).removeAllViewsOnPage();
846 mDirtyPageContent.set(i, true);
847 }
848 }
849 }
850
Adam Cohen0cd3b642011-10-14 14:58:00 -0700851 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700852 // Clean up all the async tasks
853 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
854 while (iter.hasNext()) {
855 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
856 task.cancel(false);
857 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700858 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700859
860 // We've already preallocated the views for the data to load into, so clear them as well
861 View v = getPageAt(task.page);
862 if (v instanceof PagedViewGridLayout) {
863 ((PagedViewGridLayout) v).removeAllViewsOnPage();
864 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700865 }
Winson Chung83687b12012-04-25 16:01:01 -0700866 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700867 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700868 }
869
Winson Chung785d2eb2011-04-14 16:08:02 -0700870 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200871 // Widgets appear to be cleared every time you leave, always force invalidate for them
872 if (mContentType != type || type == ContentType.Widgets) {
873 int page = (mContentType != type) ? 0 : getCurrentPage();
874 mContentType = type;
875 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700876 }
Winson Chungc58497e2013-09-03 17:48:37 -0700877 }
878
879 public ContentType getContentType() {
880 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700881 }
882
Adam Cohen0cd3b642011-10-14 14:58:00 -0700883 protected void snapToPage(int whichPage, int delta, int duration) {
884 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800885
886 // Update the thread priorities given the direction lookahead
887 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
888 while (iter.hasNext()) {
889 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700890 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800891 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
892 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
893 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
894 } else {
895 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
896 }
897 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700898 }
899
Winson Chung785d2eb2011-04-14 16:08:02 -0700900 /*
901 * Apps PagedView implementation
902 */
Winson Chung63257c12011-05-05 17:06:13 -0700903 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
904 int childCount = layout.getChildCount();
905 for (int i = 0; i < childCount; ++i) {
906 layout.getChildAt(i).setVisibility(visibility);
907 }
908 }
Winson Chungc58497e2013-09-03 17:48:37 -0700909 private void setupPage(AppsCustomizeCellLayout layout) {
910 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700911
Winson Chung63257c12011-05-05 17:06:13 -0700912 // Note: We force a measure here to get around the fact that when we do layout calculations
913 // immediately after syncing, we don't have a proper width. That said, we already know the
914 // expected page width, so we can actually optimize by hiding all the TextView-based
915 // children that are expensive to measure, and let that happen naturally later.
916 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700917 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
918 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700919 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700920
Adam Cohen4e243a22014-08-10 18:30:55 -0700921 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700922 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700923 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700924 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700925 }
926
927 setVisibilityOnChildren(layout, View.VISIBLE);
928 }
929
930 public void setPageBackgroundsVisible(boolean visible) {
931 mPageBackgroundsVisible = visible;
932 int childCount = getChildCount();
933 for (int i = 0; i < childCount; ++i) {
934 Drawable bg = getChildAt(i).getBackground();
935 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700936 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700937 }
938 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700939 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700940
Winson Chungf314b0e2011-08-16 11:54:27 -0700941 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700942 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700943 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 int numCells = mCellCountX * mCellCountY;
945 int startIndex = page * numCells;
946 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700947 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700948
Winson Chung785d2eb2011-04-14 16:08:02 -0700949 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700950 ArrayList<Object> items = new ArrayList<Object>();
951 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700952 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200953 AppInfo info = mApps.get(i);
Sunny Goyal508da152014-08-14 10:53:27 -0700954 BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700955 R.layout.apps_customize_application, layout, false);
Sunny Goyal508da152014-08-14 10:53:27 -0700956 icon.applyFromApplicationInfo(info);
957 icon.setOnClickListener(mLauncher);
Winson Chung785d2eb2011-04-14 16:08:02 -0700958 icon.setOnLongClickListener(this);
959 icon.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -0800960 icon.setOnKeyListener(mKeyListener);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700961 icon.setOnFocusChangeListener(layout.mFocusHandlerView);
Winson Chung785d2eb2011-04-14 16:08:02 -0700962
963 int index = i - startIndex;
964 int x = index % mCellCountX;
965 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -0700966 if (isRtl) {
967 x = mCellCountX - x - 1;
968 }
Winson Chungc58497e2013-09-03 17:48:37 -0700969 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -0700970
971 items.add(info);
972 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700974
Michael Jurka47639b92013-01-14 12:42:27 +0100975 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
Winson Chungb44b5242011-06-13 11:32:14 -0700977
978 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800979 * A helper to return the priority for loading of the specified widget page.
980 */
981 private int getWidgetPageLoadPriority(int page) {
982 // If we are snapping to another page, use that index as the target page index
983 int toPage = mCurrentPage;
984 if (mNextPage > -1) {
985 toPage = mNextPage;
986 }
987
988 // We use the distance from the target page as an initial guess of priority, but if there
989 // are no pages of higher priority than the page specified, then bump up the priority of
990 // the specified page.
991 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
992 int minPageDiff = Integer.MAX_VALUE;
993 while (iter.hasNext()) {
994 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700995 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800996 }
997
998 int rawPageDiff = Math.abs(page - toPage);
999 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1000 }
1001 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001002 * Return the appropriate thread priority for loading for a given page (we give the current
1003 * page much higher priority)
1004 */
1005 private int getThreadPriorityForPage(int page) {
1006 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001007 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001008 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001009 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001010 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001011 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001012 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001013 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001014 }
1015 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001016 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001017 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001018 return Math.max(0, pageDiff * sPageSleepDelay);
1019 }
Winson Chungb44b5242011-06-13 11:32:14 -07001020 /**
1021 * Creates and executes a new AsyncTask to load a page of widget previews.
1022 */
1023 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001024 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001025
Winson Chungb44b5242011-06-13 11:32:14 -07001026 // Prune all tasks that are no longer needed
1027 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1028 while (iter.hasNext()) {
1029 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001030 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001031 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1032 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001033 task.cancel(false);
1034 iter.remove();
1035 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001036 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001037 }
1038 }
1039
Winson Chungf314b0e2011-08-16 11:54:27 -07001040 // 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 -07001041 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001042 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001043 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001044 @Override
1045 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001046 try {
Winson Chung09945932011-09-20 14:22:40 -07001047 try {
1048 Thread.sleep(sleepMs);
1049 } catch (Exception e) {}
1050 loadWidgetPreviewsInBackground(task, data);
1051 } finally {
1052 if (task.isCancelled()) {
1053 data.cleanup(true);
1054 }
1055 }
Winson Chungb44b5242011-06-13 11:32:14 -07001056 }
1057 },
1058 new AsyncTaskCallback() {
1059 @Override
1060 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001061 mRunningTasks.remove(task);
1062 if (task.isCancelled()) return;
1063 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001064 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001065 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001066 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001067
1068 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001069 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001070 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001071 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001072 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1073 mRunningTasks.add(t);
1074 }
Winson Chungb44b5242011-06-13 11:32:14 -07001075
Winson Chung785d2eb2011-04-14 16:08:02 -07001076 /*
1077 * Widgets PagedView implementation
1078 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001079 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001080 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001081 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001082 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1083 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001084
1085 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1086 if (bg != null) {
1087 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1088 layout.setBackground(bg);
1089 }
Winson Chung63257c12011-05-05 17:06:13 -07001090 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001092
Michael Jurka038f9d82011-11-03 13:50:45 -07001093 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001094 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001095
Adam Cohen4e243a22014-08-10 18:30:55 -07001096 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1097
Winson Chungd2945262011-06-24 15:22:14 -07001098 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001099 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001100 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001101 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001102 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1103
Adam Cohen4e243a22014-08-10 18:30:55 -07001104 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001105
Winson Chunge4a647f2011-09-30 14:41:25 -07001106 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001107 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001108 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1109 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001110 }
1111
Winson Chunge4a647f2011-09-30 14:41:25 -07001112 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001113 layout.setColumnCount(layout.getCellCountX());
1114 for (int i = 0; i < items.size(); ++i) {
1115 Object rawInfo = items.get(i);
1116 PendingAddItemInfo createItemInfo = null;
1117 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1118 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001119
1120 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001121 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001122 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1123 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001124
Adam Cohen59400422014-03-05 18:07:04 -08001125 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001126 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001127 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001128 } else if (rawInfo instanceof ResolveInfo) {
1129 // Fill in the shortcuts information
1130 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001131 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001132 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1133 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1134 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001135 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001136 widget.setTag(createItemInfo);
1137 }
Adam Cohen59400422014-03-05 18:07:04 -08001138
Winson Chunge4a647f2011-09-30 14:41:25 -07001139 widget.setOnClickListener(this);
1140 widget.setOnLongClickListener(this);
1141 widget.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -08001142 widget.setOnKeyListener(mKeyListener);
Winson Chunge4a647f2011-09-30 14:41:25 -07001143
1144 // Layout each widget
1145 int ix = i % mWidgetCountX;
1146 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001147
1148 if (ix > 0) {
1149 View border = widget.findViewById(R.id.left_border);
1150 border.setVisibility(View.VISIBLE);
1151 }
1152 if (ix < mWidgetCountX - 1) {
1153 View border = widget.findViewById(R.id.right_border);
1154 border.setVisibility(View.VISIBLE);
1155 }
1156
Winson Chunge4a647f2011-09-30 14:41:25 -07001157 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001158 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001159 GridLayout.spec(ix, GridLayout.TOP));
1160 lp.width = cellWidth;
1161 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001162 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001163 layout.addView(widget, lp);
1164 }
1165
Michael Jurka038f9d82011-11-03 13:50:45 -07001166 // wait until a call on onLayout to start loading, because
1167 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1168 // TODO: can we do a measure/layout immediately?
1169 layout.setOnLayoutListener(new Runnable() {
1170 public void run() {
1171 // Load the widget previews
1172 int maxPreviewWidth = cellWidth;
1173 int maxPreviewHeight = cellHeight;
1174 if (layout.getChildCount() > 0) {
1175 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1176 int[] maxSize = w.getPreviewSize();
1177 maxPreviewWidth = maxSize[0];
1178 maxPreviewHeight = maxSize[1];
1179 }
Michael Jurka05713af2013-01-23 12:39:24 +01001180
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001181 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001182 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001183 if (immediate) {
1184 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001185 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001186 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001187 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001188 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001189 if (mInTransition) {
1190 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1191 } else {
1192 prepareLoadWidgetPreviewsTask(page, items,
1193 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1194 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001195 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001196 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001197 }
1198 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001199 }
1200 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1201 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001202 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1203 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001204 if (task != null) {
1205 // Ensure that this task starts running at the correct priority
1206 task.syncThreadPriority();
1207 }
1208
1209 // Load each of the widget/shortcut previews
1210 ArrayList<Object> items = data.items;
1211 ArrayList<Bitmap> images = data.generatedImages;
1212 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001213 for (int i = 0; i < count; ++i) {
1214 if (task != null) {
1215 // Ensure we haven't been cancelled yet
1216 if (task.isCancelled()) break;
1217 // Before work on each item, ensure that this task is running at the correct
1218 // priority
1219 task.syncThreadPriority();
1220 }
1221
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001222 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001223 }
Winson Chungb44b5242011-06-13 11:32:14 -07001224 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001225
Winson Chung7bb37522013-10-28 11:07:57 -07001226 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1227 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001228 mDeferredSyncWidgetPageItems.add(data);
1229 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001230 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001231 try {
1232 int page = data.page;
1233 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001234
Michael Jurka39e5d172012-03-12 18:36:12 -07001235 ArrayList<Object> items = data.items;
1236 int count = items.size();
1237 for (int i = 0; i < count; ++i) {
1238 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1239 if (widget != null) {
1240 Bitmap preview = data.generatedImages.get(i);
1241 widget.applyPreview(new FastBitmapDrawable(preview), i);
1242 }
1243 }
Winson Chung68e4c642011-11-10 15:48:25 -08001244
Michael Jurka47639b92013-01-14 12:42:27 +01001245 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001246
1247 // Update all thread priorities
1248 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1249 while (iter.hasNext()) {
1250 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1251 int pageIndex = task.page;
1252 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1253 }
1254 } finally {
1255 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001256 }
Winson Chungb44b5242011-06-13 11:32:14 -07001257 }
Winson Chung46af2e82011-05-09 16:00:53 -07001258
Winson Chung785d2eb2011-04-14 16:08:02 -07001259 @Override
1260 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001261 disablePagedViewAnimations();
1262
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001264 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001265
Adam Cohen0cd3b642011-10-14 14:58:00 -07001266 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001267 if (mContentType == ContentType.Applications) {
1268 for (int i = 0; i < mNumAppsPages; ++i) {
1269 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1270 setupPage(layout);
1271 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1272 LayoutParams.MATCH_PARENT));
1273 }
1274 } else if (mContentType == ContentType.Widgets) {
1275 for (int j = 0; j < mNumWidgetPages; ++j) {
1276 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1277 mWidgetCountY);
1278 setupPage(layout);
1279 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1280 LayoutParams.MATCH_PARENT));
1281 }
1282 } else {
1283 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001284 }
1285
Winson Chungc58497e2013-09-03 17:48:37 -07001286 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001287 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001288
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001290 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001291 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001292 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001293 } else {
1294 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001295 }
1296 }
1297
Adam Cohen22f823d2011-09-01 17:22:18 -07001298 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1299 // it is in the z-order. This is important to insure touch events are handled correctly.
1300 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001301 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001302 }
1303
Adam Cohenae4f1552011-10-20 00:15:42 -07001304 @Override
1305 protected int indexToPage(int index) {
1306 return getChildCount() - index - 1;
1307 }
1308
Adam Cohen22f823d2011-09-01 17:22:18 -07001309 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1310 @Override
1311 protected void screenScrolled(int screenCenter) {
1312 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001313 enableHwLayersOnVisiblePages();
1314 }
1315
1316 private void enableHwLayersOnVisiblePages() {
1317 final int screenCount = getChildCount();
1318
1319 getVisiblePages(mTempVisiblePagesRange);
1320 int leftScreen = mTempVisiblePagesRange[0];
1321 int rightScreen = mTempVisiblePagesRange[1];
1322 int forceDrawScreen = -1;
1323 if (leftScreen == rightScreen) {
1324 // make sure we're caching at least two pages always
1325 if (rightScreen < screenCount - 1) {
1326 rightScreen++;
1327 forceDrawScreen = rightScreen;
1328 } else if (leftScreen > 0) {
1329 leftScreen--;
1330 forceDrawScreen = leftScreen;
1331 }
1332 } else {
1333 forceDrawScreen = leftScreen + 1;
1334 }
1335
1336 for (int i = 0; i < screenCount; i++) {
1337 final View layout = (View) getPageAt(i);
1338 if (!(leftScreen <= i && i <= rightScreen &&
1339 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1340 layout.setLayerType(LAYER_TYPE_NONE, null);
1341 }
1342 }
1343
Michael Jurka47639b92013-01-14 12:42:27 +01001344 for (int i = 0; i < screenCount; i++) {
1345 final View layout = (View) getPageAt(i);
1346
1347 if (leftScreen <= i && i <= rightScreen &&
1348 (i == forceDrawScreen || shouldDrawChild(layout))) {
1349 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1350 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1351 }
1352 }
1353 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001354 }
1355
1356 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001357 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001358 }
1359
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 /**
1361 * Used by the parent to get the content width to set the tab bar to
1362 * @return
1363 */
1364 public int getPageContentWidth() {
1365 return mContentWidth;
1366 }
1367
Winson Chungb26f3d62011-06-02 10:49:29 -07001368 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001369 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001370 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001371 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001372 // We reset the save index when we change pages so that it will be recalculated on next
1373 // rotation
1374 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001375 }
1376
Winson Chung785d2eb2011-04-14 16:08:02 -07001377 /*
1378 * AllAppsView implementation
1379 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001380 public void setup(Launcher launcher, DragController dragController) {
1381 mLauncher = launcher;
1382 mDragController = dragController;
1383 }
Winson Chung9802ac92012-06-08 16:01:58 -07001384
1385 /**
1386 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1387 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1388 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1389 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1390 */
1391 private void invalidateOnDataChange() {
1392 if (!isDataReady()) {
1393 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1394 // request a layout to trigger the page data when ready.
1395 requestLayout();
1396 } else {
1397 cancelAllTasks();
1398 invalidatePageData();
1399 }
1400 }
1401
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001402 public void setApps(ArrayList<AppInfo> list) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001403 mApps = list;
1404 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1405 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001406 }
Adam Cohen59400422014-03-05 18:07:04 -08001407
1408 public ArrayList<AppInfo> getApps() {
1409 return mApps;
1410 }
1411
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001412 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001413 // We add it in place, in alphabetical order
1414 int count = list.size();
1415 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001416 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001417 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001418 if (index < 0) {
1419 mApps.add(-(index + 1), info);
1420 }
1421 }
1422 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001423 public void addApps(ArrayList<AppInfo> list) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001424 addAppsWithoutInvalidate(list);
1425 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001426 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001427 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001428 ComponentName removeComponent = item.intent.getComponent();
1429 int length = list.size();
1430 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001431 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001432 if (info.user.equals(item.user)
1433 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001434 return i;
1435 }
1436 }
1437 return -1;
1438 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001439 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001440 // loop through all the apps and remove apps that have the same component
1441 int length = list.size();
1442 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001443 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001444 int removeIndex = findAppByComponent(mApps, info);
1445 if (removeIndex > -1) {
1446 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001447 }
1448 }
1449 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001450 public void removeApps(ArrayList<AppInfo> appInfos) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001451 removeAppsWithoutInvalidate(appInfos);
1452 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001453 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001454 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 // We remove and re-add the updated applications list because it's properties may have
1456 // changed (ie. the title), and this will ensure that the items will be in their proper
1457 // place in the list.
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001458 removeAppsWithoutInvalidate(list);
1459 addAppsWithoutInvalidate(list);
1460 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001461 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001462
Winson Chung785d2eb2011-04-14 16:08:02 -07001463 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001464 // If we have reset, then we should not continue to restore the previous state
1465 mSaveInstanceStateItemIndex = -1;
1466
Adam Cohen6c5891a2014-07-09 23:53:15 -07001467 if (mContentType != ContentType.Applications) {
1468 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001469 }
Winson Chung649668f2012-01-10 13:07:16 -08001470
Adam Cohenb64d36e2011-10-17 21:48:02 -07001471 if (mCurrentPage != 0) {
1472 invalidatePageData(0);
1473 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001474 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001475
1476 private AppsCustomizeTabHost getTabHost() {
1477 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1478 }
1479
Winson Chung785d2eb2011-04-14 16:08:02 -07001480 public void dumpState() {
1481 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001482 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001483 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001484 }
Adam Cohen4e844012011-11-09 13:48:04 -08001485
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001487 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001489 for (Object i: list) {
1490 if (i instanceof AppWidgetProviderInfo) {
1491 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1492 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1493 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1494 + " initialLayout=" + info.initialLayout
1495 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1496 } else if (i instanceof ResolveInfo) {
1497 ResolveInfo info = (ResolveInfo) i;
1498 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1499 + info.icon);
1500 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001501 }
1502 }
Adam Cohen4e844012011-11-09 13:48:04 -08001503
Winson Chung785d2eb2011-04-14 16:08:02 -07001504 public void surrender() {
1505 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1506 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001507
1508 // Stop all background tasks
1509 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001510 }
Winson Chung007c6982011-06-14 13:27:53 -07001511
Winson Chungb44b5242011-06-13 11:32:14 -07001512 /*
1513 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1514 * widget previews in the background with the AsyncTasks.
1515 */
Winson Chung68e4c642011-11-10 15:48:25 -08001516 final static int sLookBehindPageCount = 2;
1517 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001518 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001519 final int count = getChildCount();
1520 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1521 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1522 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001523 }
1524 protected int getAssociatedUpperPageBound(int page) {
1525 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001526 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1527 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1528 count - 1);
1529 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001530 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001531
Winson Chung6a0f57d2011-06-29 20:10:49 -07001532 protected String getCurrentPageDescription() {
1533 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1534 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001535 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001536
1537 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001538 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001539 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001540 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001541 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001542 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001543 } else {
1544 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001545 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001546
Michael Jurka8b805b12012-04-18 14:23:14 -07001547 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001548 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001549}