blob: 9e7e523e03b4961906815c90bf7c49a6fd8dd86c [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Michael Jurka05713af2013-01-23 12:39:24 +010031import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.graphics.Rect;
33import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070034import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070035import android.os.Build;
36import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070037import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.AttributeSet;
39import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070040import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080041import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.view.LayoutInflater;
43import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070044import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070045import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070046import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070047import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070049
Daniel Sandler325dc232013-06-05 22:57:57 -040050import com.android.launcher3.DropTarget.DragObject;
Sunny Goyalffe83f12014-08-14 17:39:34 -070051import com.android.launcher3.compat.AppWidgetManagerCompat;
Adam Cohenc0dcf592011-06-01 15:30:43 -070052
53import java.util.ArrayList;
54import java.util.Collections;
55import java.util.Iterator;
56import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070057
Winson Chungb44b5242011-06-13 11:32:14 -070058/**
59 * A simple callback interface which also provides the results of the task.
60 */
61interface AsyncTaskCallback {
62 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
63}
Winson Chung4e076542011-06-23 13:04:10 -070064
Winson Chungb44b5242011-06-13 11:32:14 -070065/**
66 * The data needed to perform either of the custom AsyncTasks.
67 */
68class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070069 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080070 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070071 }
72
Michael Jurka038f9d82011-11-03 13:50:45 -070073 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010074 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070075 page = p;
76 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070077 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070078 maxImageWidth = cw;
79 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070080 doInBackgroundCallback = bgR;
81 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010082 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070083 }
Winson Chung09945932011-09-20 14:22:40 -070084 void cleanup(boolean cancelled) {
85 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070086 if (generatedImages != null) {
87 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010088 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010089 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070090 }
91 }
92 generatedImages.clear();
93 }
94 }
Winson Chungb44b5242011-06-13 11:32:14 -070095 int page;
96 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070097 ArrayList<Bitmap> sourceImages;
98 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -070099 int maxImageWidth;
100 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700101 AsyncTaskCallback doInBackgroundCallback;
102 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100103 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700104}
Winson Chung4e076542011-06-23 13:04:10 -0700105
Winson Chungb44b5242011-06-13 11:32:14 -0700106/**
107 * A generic template for an async task used in AppsCustomize.
108 */
109class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700110 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700111 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700112 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700113 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700114 }
115 @Override
116 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
117 if (params.length != 1) return null;
118 // Load each of the widget previews in the background
119 params[0].doInBackgroundCallback.run(this, params[0]);
120 return params[0];
121 }
122 @Override
123 protected void onPostExecute(AsyncTaskPageData result) {
124 // All the widget previews are loaded, so we can just callback to inflate the page
125 result.postExecuteCallback.run(this, result);
126 }
127
128 void setThreadPriority(int p) {
129 threadPriority = p;
130 }
131 void syncThreadPriority() {
132 Process.setThreadPriority(threadPriority);
133 }
134
135 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700136 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700137 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 int threadPriority;
139}
Winson Chungb44b5242011-06-13 11:32:14 -0700140
141/**
142 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
143 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700144public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700145 View.OnClickListener, View.OnKeyListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -0700146 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700147 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700148
Sunny Goyalff572272014-07-23 13:58:07 -0700149 private static Rect sTmpRect = new Rect();
150
Winson Chung785d2eb2011-04-14 16:08:02 -0700151 /**
152 * The different content types that this paged view can show.
153 */
154 public enum ContentType {
155 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700156 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700157 }
Winson Chungc58497e2013-09-03 17:48:37 -0700158 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700159
160 // Refs
161 private Launcher mLauncher;
162 private DragController mDragController;
163 private final LayoutInflater mLayoutInflater;
164 private final PackageManager mPackageManager;
165
Winson Chung5afbf7b2011-07-25 11:53:08 -0700166 // Save and Restore
167 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168
Winson Chung785d2eb2011-04-14 16:08:02 -0700169 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700171 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700172
173 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -0700174 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700175
176 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700177 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700178 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700179 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700180 private int mNumAppsPages;
181 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700182 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700183
Winson Chungb44b5242011-06-13 11:32:14 -0700184 // Previews & outlines
185 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800186 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700187
Adam Cohened66b2b2012-01-23 17:28:51 -0800188 private Runnable mInflateWidgetRunnable = null;
189 private Runnable mBindWidgetRunnable = null;
190 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700191 static final int WIDGET_PRELOAD_PENDING = 0;
192 static final int WIDGET_BOUND = 1;
193 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800194 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
195 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800196 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800197 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700198 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800199
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700200 private Toast mWidgetInstructionToast;
201
Michael Jurka39e5d172012-03-12 18:36:12 -0700202 // Deferral of loading widget previews during launcher transitions
203 private boolean mInTransition;
204 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
205 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700206 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
207 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700208
Michael Jurka05713af2013-01-23 12:39:24 +0100209 WidgetPreviewLoader mWidgetPreviewLoader;
210
Michael Jurkac402cd92013-05-20 15:49:32 +0200211 private boolean mInBulkBind;
212 private boolean mNeedToUpdatePageCountsAndInvalidateData;
213
Winson Chung785d2eb2011-04-14 16:08:02 -0700214 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
215 super(context, attrs);
216 mLayoutInflater = LayoutInflater.from(context);
217 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200218 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700219 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400220 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700221 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700222
223 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800224 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700225 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
226 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
227 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700228 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700229
Winson Chung1ed747a2011-05-03 16:18:34 -0700230 // The padding on the non-matched dimension for the default widget preview icons
231 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700232 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700233
234 // Unless otherwise specified this view is important for accessibility.
235 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
236 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
237 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700238 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700239 }
240
241 @Override
242 protected void init() {
243 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700244 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700245
246 Context context = getContext();
247 Resources r = context.getResources();
248 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
249 }
250
Winson Chungc58497e2013-09-03 17:48:37 -0700251 public void onFinishInflate() {
252 super.onFinishInflate();
253
254 LauncherAppState app = LauncherAppState.getInstance();
255 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
256 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
257 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
258 }
259
Winson Chung67ca7e42013-10-31 16:53:19 -0700260 void setAllAppsPadding(Rect r) {
261 mAllAppsPadding.set(r);
262 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700263
Winson Chung67ca7e42013-10-31 16:53:19 -0700264 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700265 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700266 }
267
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100268 WidgetPreviewLoader getWidgetPreviewLoader() {
269 if (mWidgetPreviewLoader == null) {
270 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
271 }
272 return mWidgetPreviewLoader;
273 }
274
Winson Chung5afbf7b2011-07-25 11:53:08 -0700275 /** Returns the item index of the center item on this page so that we can restore to this
276 * item index when we rotate. */
277 private int getMiddleComponentIndexOnCurrentPage() {
278 int i = -1;
279 if (getPageCount() > 0) {
280 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700281 if (mContentType == ContentType.Applications) {
282 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
283 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700284 int numItemsPerPage = mCellCountX * mCellCountY;
285 int childCount = childrenLayout.getChildCount();
286 if (childCount > 0) {
287 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700288 }
Winson Chungc58497e2013-09-03 17:48:37 -0700289 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700290 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700291 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700292 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
293 int childCount = layout.getChildCount();
294 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700295 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700296 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700297 }
Winson Chungc58497e2013-09-03 17:48:37 -0700298 } else {
299 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700300 }
301 }
302 return i;
303 }
304
305 /** Get the index of the item to restore to if we need to restore the current page. */
306 int getSaveInstanceStateIndex() {
307 if (mSaveInstanceStateItemIndex == -1) {
308 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
309 }
310 return mSaveInstanceStateItemIndex;
311 }
312
313 /** Returns the page in the current orientation which is expected to contain the specified
314 * item index. */
315 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 if (index < 0) return 0;
317
318 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700319 int numItemsPerPage = mCellCountX * mCellCountY;
320 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700321 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700322 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700323 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700325 }
326
Winson Chung5afbf7b2011-07-25 11:53:08 -0700327 /** Restores the page for an item at the specified index */
328 void restorePageForIndex(int index) {
329 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700331 }
332
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700333 private void updatePageCounts() {
334 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
335 (float) (mWidgetCountX * mWidgetCountY));
336 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
337 }
338
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 // Now that the data is ready, we can calculate the content width, the number of cells to
341 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700342 LauncherAppState app = LauncherAppState.getInstance();
343 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700344 mCellCountX = (int) grid.allAppsNumCols;
345 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700346 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700347
Winson Chungdb1138b2011-06-30 14:39:35 -0700348 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700349 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
350 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
351 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
352 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700353 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700354
Adam Cohen6c5891a2014-07-09 23:53:15 -0700355 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700356 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800357 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800358 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700359
Adam Cohena00673c2014-08-14 12:57:28 -0700360 protected void onLayout(boolean changed, int l, int t, int r, int b) {
361 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700362
Winson Chungf0ea4d32011-06-06 14:27:16 -0700363 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800364 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700365 post(new Runnable() {
366 // This code triggers requestLayout so must be posted outside of the
367 // layout pass.
368 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800369 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200370 setDataIsReady();
371 onDataReady(getMeasuredWidth(), getMeasuredHeight());
372 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700373 }
374 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 }
376 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377 }
378
Michael Jurkac402cd92013-05-20 15:49:32 +0200379 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700380 LauncherAppState app = LauncherAppState.getInstance();
381 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
382
Winson Chung1ed747a2011-05-03 16:18:34 -0700383 // Get the list of widgets and shortcuts
384 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200385 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800386 if (o instanceof LauncherAppWidgetProviderInfo) {
387 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
388 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100389 continue;
390 }
Adam Cohen59400422014-03-05 18:07:04 -0800391
392 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200393 // Ensure that all widgets we show can be added on a workspace of this size
394 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
395 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
396 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
397 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700398 if (minSpanX <= (int) grid.numColumns &&
399 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200400 mWidgets.add(widget);
401 } else {
402 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
403 widget.minWidth + ", " + widget.minHeight + ")");
404 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700405 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200406 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
407 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700408 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800409 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200410 // just add shortcuts
411 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800412 }
413 }
Adam Cohen59400422014-03-05 18:07:04 -0800414
Michael Jurkac402cd92013-05-20 15:49:32 +0200415 updatePageCountsAndInvalidateData();
416 }
417
418 public void setBulkBind(boolean bulkBind) {
419 if (bulkBind) {
420 mInBulkBind = true;
421 } else {
422 mInBulkBind = false;
423 if (mNeedToUpdatePageCountsAndInvalidateData) {
424 updatePageCountsAndInvalidateData();
425 }
426 }
427 }
428
429 private void updatePageCountsAndInvalidateData() {
430 if (mInBulkBind) {
431 mNeedToUpdatePageCountsAndInvalidateData = true;
432 } else {
433 updatePageCounts();
434 invalidateOnDataChange();
435 mNeedToUpdatePageCountsAndInvalidateData = false;
436 }
Winson Chung4b576be2011-04-27 17:40:20 -0700437 }
438
439 @Override
440 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700441 // When we have exited all apps or are in transition, disregard clicks
Sunny Goyal508da152014-08-14 10:53:27 -0700442 if (!mLauncher.isAllAppsVisible()
443 || mLauncher.getWorkspace().isSwitchingState()
444 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700445
Sunny Goyal508da152014-08-14 10:53:27 -0700446 // Let the user know that they have to long press to add a widget
447 if (mWidgetInstructionToast != null) {
448 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700449 }
Sunny Goyal508da152014-08-14 10:53:27 -0700450 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
451 Toast.LENGTH_SHORT);
452 mWidgetInstructionToast.show();
453
454 // Create a little animation to show that the widget can move
455 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
456 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
457 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
458 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
459 tyuAnim.setDuration(125);
460 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
461 tydAnim.setDuration(100);
462 bounce.play(tyuAnim).before(tydAnim);
463 bounce.setInterpolator(new AccelerateInterpolator());
464 bounce.start();
Winson Chung785d2eb2011-04-14 16:08:02 -0700465 }
466
Winson Chungc6f10b92011-11-14 11:39:07 -0800467 public boolean onKey(View v, int keyCode, KeyEvent event) {
468 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
469 }
470
Winson Chung785d2eb2011-04-14 16:08:02 -0700471 /*
472 * PagedViewWithDraggableItems implementation
473 */
474 @Override
475 protected void determineDraggingStart(android.view.MotionEvent ev) {
476 // Disable dragging by pulling an app down for now.
477 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700478
Winson Chung4b576be2011-04-27 17:40:20 -0700479 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700480 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700481 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700482
Sunny Goyalff572272014-07-23 13:58:07 -0700483 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700484 Bundle options = null;
485 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700486 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
487 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700488 info.componentName, null);
489
Sunny Goyalff572272014-07-23 13:58:07 -0700490 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700491 int xPaddingDips = (int) ((padding.left + padding.right) / density);
492 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
493
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700494 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700495 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700496 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700497 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700498 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700499 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700500 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700501 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700502 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700503 }
504 return options;
505 }
506
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700507 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800508 final LauncherAppWidgetProviderInfo pInfo = info.info;
509 final Bundle options = pInfo.isCustomWidget ? null :
510 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700511
Adam Cohened66b2b2012-01-23 17:28:51 -0800512 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700513 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800514 return;
515 }
516
Adam Cohen21a170b2012-05-30 15:17:06 -0700517 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800518 mBindWidgetRunnable = new Runnable() {
519 @Override
520 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800521 if (pInfo.isCustomWidget) {
522 mWidgetCleanupState = WIDGET_BOUND;
523 return;
524 }
525
Adam Cohened66b2b2012-01-23 17:28:51 -0800526 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700527 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
528 mWidgetLoadingId, pInfo, options)) {
529 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700530 }
Adam Cohen59400422014-03-05 18:07:04 -0800531
Adam Cohened66b2b2012-01-23 17:28:51 -0800532 }
533 };
534 post(mBindWidgetRunnable);
535
536 mInflateWidgetRunnable = new Runnable() {
537 @Override
538 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700539 if (mWidgetCleanupState != WIDGET_BOUND) {
540 return;
541 }
Adam Cohen59400422014-03-05 18:07:04 -0800542 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
543 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800544 info.boundWidget = hostView;
545 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800546 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700547 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
548 info.spanY, info, false);
549
550 // We want the first widget layout to be the correct size. This will be important
551 // for width size reporting to the AppWidgetManager.
552 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
553 unScaledSize[1]);
554 lp.x = lp.y = 0;
555 lp.customPosition = true;
556 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800557 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800558 }
559 };
560 post(mInflateWidgetRunnable);
561 }
562
563 @Override
564 public void onShortPress(View v) {
565 // We are anticipating a long press, and we use this time to load bind and instantiate
566 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700567 if (mCreateWidgetInfo != null) {
568 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
569 cleanupWidgetPreloading(false);
570 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800571 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700572 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 }
574
Adam Cohen0e56cc92012-05-11 15:57:05 -0700575 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
576 if (!widgetWasAdded) {
577 // If the widget was not added, we may need to do further cleanup.
578 PendingAddWidgetInfo info = mCreateWidgetInfo;
579 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700580
581 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700582 // We never did any preloading, so just remove pending callbacks to do so
583 removeCallbacks(mBindWidgetRunnable);
584 removeCallbacks(mInflateWidgetRunnable);
585 } else if (mWidgetCleanupState == WIDGET_BOUND) {
586 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800587 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700588 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
589 }
590
591 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700592 removeCallbacks(mInflateWidgetRunnable);
593 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700594 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800595 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700596 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
597 }
598
Adam Cohen0e56cc92012-05-11 15:57:05 -0700599 // The widget was inflated and added to the DragLayer -- remove it.
600 AppWidgetHostView widget = info.boundWidget;
601 mLauncher.getDragLayer().removeView(widget);
602 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 }
604 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
605 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700606 mCreateWidgetInfo = null;
607 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 }
609
Adam Cohen7a326642012-02-22 12:03:22 -0800610 @Override
611 public void cleanUpShortPress(View v) {
612 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700613 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800614 }
615 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800616
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700617 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800618 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700619 // Get the widget preview as the drag representation
620 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700621 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700622
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700623 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
624 // we abort the drag.
625 if (image.getDrawable() == null) {
626 mDraggingWidget = false;
627 return false;
628 }
629
Winson Chung4b576be2011-04-27 17:40:20 -0700630 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800631 Bitmap preview;
632 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800633 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100634 Point previewPadding = null;
635
Winson Chung1ed747a2011-05-03 16:18:34 -0700636 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700637 // This can happen in some weird cases involving multi-touch. We can't start dragging
638 // the widget if this is null, so we break out.
639 if (mCreateWidgetInfo == null) {
640 return false;
641 }
642
Adam Cohen1b36dc32012-02-13 19:27:37 -0800643 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
644 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700645 int spanX = createItemInfo.spanX;
646 int spanY = createItemInfo.spanY;
647 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
648 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700649
Winson Chung72d59842012-02-22 13:51:36 -0800650 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
651 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700652 int maxWidth, maxHeight;
653 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
654 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800655
Michael Jurka05713af2013-01-23 12:39:24 +0100656 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700657 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
658 spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100659
660 // Compare the size of the drag preview to the preview in the AppsCustomize tray
661 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100662 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100663 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
664
665 // The bitmap in the AppsCustomize tray is always the the same size, so there
666 // might be extra pixels around the preview itself - this accounts for that
667 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
668 int padding =
669 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
670 previewPadding = new Point(padding, 0);
671 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700672 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700673 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
674 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700675 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700676 createItemInfo.spanX = createItemInfo.spanY = 1;
677 }
Winson Chung4b576be2011-04-27 17:40:20 -0700678
Michael Jurka8c3339b2012-06-14 16:18:21 -0700679 // Don't clip alpha values for the drag outline if we're using the default widget preview
680 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
681 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700682
Winson Chung1120e032011-11-22 16:11:31 -0800683 // Save the preview for the outline generation, then dim the preview
684 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
685 false);
Winson Chung1120e032011-11-22 16:11:31 -0800686
Winson Chung4b576be2011-04-27 17:40:20 -0700687 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700688 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700689 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800690 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100691 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800692 outline.recycle();
693 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700694 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700695 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800696
Winson Chung4b576be2011-04-27 17:40:20 -0700697 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700699 if (!super.beginDragging(v)) return false;
700
Sunny Goyal508da152014-08-14 10:53:27 -0700701 if (v instanceof BubbleTextView) {
Winson Chung4b576be2011-04-27 17:40:20 -0700702 beginDraggingApplication(v);
703 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700704 if (!beginDraggingWidget(v)) {
705 return false;
706 }
Winson Chung4b576be2011-04-27 17:40:20 -0700707 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800708
709 // We delay entering spring-loaded mode slightly to make sure the UI
710 // thready is free of any work.
711 postDelayed(new Runnable() {
712 @Override
713 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800714 // We don't enter spring-loaded mode if the drag has been cancelled
715 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800716 // Go into spring loaded mode (must happen before we startDrag())
717 mLauncher.enterSpringLoadedDragMode();
718 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800719 }
Winson Chung72d59842012-02-22 13:51:36 -0800720 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800721
Winson Chung785d2eb2011-04-14 16:08:02 -0700722 return true;
723 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800724
Winson Chunga48487a2012-03-20 16:19:37 -0700725 /**
726 * Clean up after dragging.
727 *
728 * @param target where the item was dragged to (can be null if the item was flung)
729 */
730 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700731 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800732 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700733 // Exit spring loaded mode if we have not successfully dropped or have not handled the
734 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700735 mLauncher.exitSpringLoadedDragModeDelayed(true,
736 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700738 } else {
739 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700740 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700741 }
742
Winson Chung785d2eb2011-04-14 16:08:02 -0700743 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700744 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700745 if (getChildCount() > 0) {
746 return getChildAt(0);
747 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700748 return null;
749 }
750
751 @Override
752 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700753 mInTransition = true;
754 if (toWorkspace) {
755 cancelAllTasks();
756 }
757 }
758
759 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700760 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700761 }
762
763 @Override
764 public void onLauncherTransitionStep(Launcher l, float t) {
765 }
766
767 @Override
768 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
769 mInTransition = false;
770 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700771 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700772 }
773 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700774 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
775 r.run();
776 }
777 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700778 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700779 }
780
781 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700782 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
783 boolean success) {
784 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
785 if (isFlingToDelete) return;
786
787 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700788
789 // Display an error message if the drag failed due to there not being enough space on the
790 // target layout we were dropping on.
791 if (!success) {
792 boolean showOutOfSpaceMessage = false;
793 if (target instanceof Workspace) {
794 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
795 Workspace workspace = (Workspace) target;
796 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700797 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700798 if (layout != null) {
799 layout.calculateSpans(itemInfo);
800 showOutOfSpaceMessage =
801 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
802 }
803 }
Winson Chungfc79c802011-05-02 13:35:34 -0700804 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700805 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700806 }
Adam Cohen7a326642012-02-22 12:03:22 -0800807
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800808 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700809 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700810 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800811 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 }
813
Winson Chunga48487a2012-03-20 16:19:37 -0700814 @Override
815 public void onFlingToDeleteCompleted() {
816 // We just dismiss the drag when we fling, so cleanup here
817 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700818 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700819 mDraggingWidget = false;
820 }
821
822 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800823 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700824 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800825 }
826
Winson Chung7f0acdd2011-09-19 18:34:19 -0700827 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000828 public boolean supportsAppInfoDropTarget() {
829 return true;
830 }
831
832 @Override
833 public boolean supportsDeleteDropTarget() {
834 return false;
835 }
836
837 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800838 public float getIntrinsicIconScaleFactor() {
839 LauncherAppState app = LauncherAppState.getInstance();
840 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
841 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
842 }
843
844 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700845 protected void onDetachedFromWindow() {
846 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700847 cancelAllTasks();
848 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700849
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800850 @Override
851 public void trimMemory() {
852 super.trimMemory();
853 clearAllWidgetPages();
854 }
855
Michael Jurkae326f182011-11-21 14:05:46 -0800856 public void clearAllWidgetPages() {
857 cancelAllTasks();
858 int count = getChildCount();
859 for (int i = 0; i < count; i++) {
860 View v = getPageAt(i);
861 if (v instanceof PagedViewGridLayout) {
862 ((PagedViewGridLayout) v).removeAllViewsOnPage();
863 mDirtyPageContent.set(i, true);
864 }
865 }
866 }
867
Adam Cohen0cd3b642011-10-14 14:58:00 -0700868 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700869 // Clean up all the async tasks
870 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
871 while (iter.hasNext()) {
872 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
873 task.cancel(false);
874 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700875 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700876
877 // We've already preallocated the views for the data to load into, so clear them as well
878 View v = getPageAt(task.page);
879 if (v instanceof PagedViewGridLayout) {
880 ((PagedViewGridLayout) v).removeAllViewsOnPage();
881 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700882 }
Winson Chung83687b12012-04-25 16:01:01 -0700883 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700884 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700885 }
886
Winson Chung785d2eb2011-04-14 16:08:02 -0700887 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200888 // Widgets appear to be cleared every time you leave, always force invalidate for them
889 if (mContentType != type || type == ContentType.Widgets) {
890 int page = (mContentType != type) ? 0 : getCurrentPage();
891 mContentType = type;
892 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700893 }
Winson Chungc58497e2013-09-03 17:48:37 -0700894 }
895
896 public ContentType getContentType() {
897 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700898 }
899
Adam Cohen0cd3b642011-10-14 14:58:00 -0700900 protected void snapToPage(int whichPage, int delta, int duration) {
901 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800902
903 // Update the thread priorities given the direction lookahead
904 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
905 while (iter.hasNext()) {
906 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700907 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800908 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
909 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
910 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
911 } else {
912 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
913 }
914 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700915 }
916
Winson Chung785d2eb2011-04-14 16:08:02 -0700917 /*
918 * Apps PagedView implementation
919 */
Winson Chung63257c12011-05-05 17:06:13 -0700920 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
921 int childCount = layout.getChildCount();
922 for (int i = 0; i < childCount; ++i) {
923 layout.getChildAt(i).setVisibility(visibility);
924 }
925 }
Winson Chungc58497e2013-09-03 17:48:37 -0700926 private void setupPage(AppsCustomizeCellLayout layout) {
927 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700928
Winson Chung63257c12011-05-05 17:06:13 -0700929 // Note: We force a measure here to get around the fact that when we do layout calculations
930 // immediately after syncing, we don't have a proper width. That said, we already know the
931 // expected page width, so we can actually optimize by hiding all the TextView-based
932 // children that are expensive to measure, and let that happen naturally later.
933 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700934 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
935 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700936 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700937
Adam Cohen4e243a22014-08-10 18:30:55 -0700938 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700939 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700940 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700941 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700942 }
943
944 setVisibilityOnChildren(layout, View.VISIBLE);
945 }
946
947 public void setPageBackgroundsVisible(boolean visible) {
948 mPageBackgroundsVisible = visible;
949 int childCount = getChildCount();
950 for (int i = 0; i < childCount; ++i) {
951 Drawable bg = getChildAt(i).getBackground();
952 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700953 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700954 }
955 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700957
Winson Chungf314b0e2011-08-16 11:54:27 -0700958 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700959 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700960 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700961 int numCells = mCellCountX * mCellCountY;
962 int startIndex = page * numCells;
963 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700964 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700965
Winson Chung785d2eb2011-04-14 16:08:02 -0700966 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700967 ArrayList<Object> items = new ArrayList<Object>();
968 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200970 AppInfo info = mApps.get(i);
Sunny Goyal508da152014-08-14 10:53:27 -0700971 BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 R.layout.apps_customize_application, layout, false);
Sunny Goyal508da152014-08-14 10:53:27 -0700973 icon.applyFromApplicationInfo(info);
974 icon.setOnClickListener(mLauncher);
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 icon.setOnLongClickListener(this);
976 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800977 icon.setOnKeyListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700978 icon.setOnFocusChangeListener(layout.mFocusHandlerView);
Winson Chung785d2eb2011-04-14 16:08:02 -0700979
980 int index = i - startIndex;
981 int x = index % mCellCountX;
982 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -0700983 if (isRtl) {
984 x = mCellCountX - x - 1;
985 }
Winson Chungc58497e2013-09-03 17:48:37 -0700986 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -0700987
988 items.add(info);
989 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700990 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700991
Michael Jurka47639b92013-01-14 12:42:27 +0100992 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -0700993 }
Winson Chungb44b5242011-06-13 11:32:14 -0700994
995 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800996 * A helper to return the priority for loading of the specified widget page.
997 */
998 private int getWidgetPageLoadPriority(int page) {
999 // If we are snapping to another page, use that index as the target page index
1000 int toPage = mCurrentPage;
1001 if (mNextPage > -1) {
1002 toPage = mNextPage;
1003 }
1004
1005 // We use the distance from the target page as an initial guess of priority, but if there
1006 // are no pages of higher priority than the page specified, then bump up the priority of
1007 // the specified page.
1008 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1009 int minPageDiff = Integer.MAX_VALUE;
1010 while (iter.hasNext()) {
1011 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001012 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001013 }
1014
1015 int rawPageDiff = Math.abs(page - toPage);
1016 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1017 }
1018 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001019 * Return the appropriate thread priority for loading for a given page (we give the current
1020 * page much higher priority)
1021 */
1022 private int getThreadPriorityForPage(int page) {
1023 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001024 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001025 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001026 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001027 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001028 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001029 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001030 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001031 }
1032 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001033 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001034 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001035 return Math.max(0, pageDiff * sPageSleepDelay);
1036 }
Winson Chungb44b5242011-06-13 11:32:14 -07001037 /**
1038 * Creates and executes a new AsyncTask to load a page of widget previews.
1039 */
1040 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001041 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001042
Winson Chungb44b5242011-06-13 11:32:14 -07001043 // Prune all tasks that are no longer needed
1044 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1045 while (iter.hasNext()) {
1046 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001047 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001048 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1049 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001050 task.cancel(false);
1051 iter.remove();
1052 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001053 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001054 }
1055 }
1056
Winson Chungf314b0e2011-08-16 11:54:27 -07001057 // 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 -07001058 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001059 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001060 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001061 @Override
1062 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001063 try {
Winson Chung09945932011-09-20 14:22:40 -07001064 try {
1065 Thread.sleep(sleepMs);
1066 } catch (Exception e) {}
1067 loadWidgetPreviewsInBackground(task, data);
1068 } finally {
1069 if (task.isCancelled()) {
1070 data.cleanup(true);
1071 }
1072 }
Winson Chungb44b5242011-06-13 11:32:14 -07001073 }
1074 },
1075 new AsyncTaskCallback() {
1076 @Override
1077 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001078 mRunningTasks.remove(task);
1079 if (task.isCancelled()) return;
1080 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001081 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001082 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001083 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001084
1085 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001086 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001087 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001088 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001089 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1090 mRunningTasks.add(t);
1091 }
Winson Chungb44b5242011-06-13 11:32:14 -07001092
Winson Chung785d2eb2011-04-14 16:08:02 -07001093 /*
1094 * Widgets PagedView implementation
1095 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001096 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001097 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001098 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001099 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1100 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001101
1102 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1103 if (bg != null) {
1104 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1105 layout.setBackground(bg);
1106 }
Winson Chung63257c12011-05-05 17:06:13 -07001107 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001108 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001109
Michael Jurka038f9d82011-11-03 13:50:45 -07001110 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001111 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001112
Adam Cohen4e243a22014-08-10 18:30:55 -07001113 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1114
Winson Chungd2945262011-06-24 15:22:14 -07001115 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001116 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001117 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001118 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001119 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1120
Adam Cohen4e243a22014-08-10 18:30:55 -07001121 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001122
Winson Chunge4a647f2011-09-30 14:41:25 -07001123 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001124 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001125 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1126 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001127 }
1128
Winson Chunge4a647f2011-09-30 14:41:25 -07001129 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001130 layout.setColumnCount(layout.getCellCountX());
1131 for (int i = 0; i < items.size(); ++i) {
1132 Object rawInfo = items.get(i);
1133 PendingAddItemInfo createItemInfo = null;
1134 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1135 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001136
1137 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001138 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001139 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1140 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001141
Adam Cohen59400422014-03-05 18:07:04 -08001142 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001143 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001144 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001145 } else if (rawInfo instanceof ResolveInfo) {
1146 // Fill in the shortcuts information
1147 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001148 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001149 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1150 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1151 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001152 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001153 widget.setTag(createItemInfo);
1154 }
Adam Cohen59400422014-03-05 18:07:04 -08001155
Winson Chunge4a647f2011-09-30 14:41:25 -07001156 widget.setOnClickListener(this);
1157 widget.setOnLongClickListener(this);
1158 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001159 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001160
1161 // Layout each widget
1162 int ix = i % mWidgetCountX;
1163 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001164
1165 if (ix > 0) {
1166 View border = widget.findViewById(R.id.left_border);
1167 border.setVisibility(View.VISIBLE);
1168 }
1169 if (ix < mWidgetCountX - 1) {
1170 View border = widget.findViewById(R.id.right_border);
1171 border.setVisibility(View.VISIBLE);
1172 }
1173
Winson Chunge4a647f2011-09-30 14:41:25 -07001174 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001175 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001176 GridLayout.spec(ix, GridLayout.TOP));
1177 lp.width = cellWidth;
1178 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001179 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001180 layout.addView(widget, lp);
1181 }
1182
Michael Jurka038f9d82011-11-03 13:50:45 -07001183 // wait until a call on onLayout to start loading, because
1184 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1185 // TODO: can we do a measure/layout immediately?
1186 layout.setOnLayoutListener(new Runnable() {
1187 public void run() {
1188 // Load the widget previews
1189 int maxPreviewWidth = cellWidth;
1190 int maxPreviewHeight = cellHeight;
1191 if (layout.getChildCount() > 0) {
1192 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1193 int[] maxSize = w.getPreviewSize();
1194 maxPreviewWidth = maxSize[0];
1195 maxPreviewHeight = maxSize[1];
1196 }
Michael Jurka05713af2013-01-23 12:39:24 +01001197
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001198 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001199 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001200 if (immediate) {
1201 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001202 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001203 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001204 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001205 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001206 if (mInTransition) {
1207 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1208 } else {
1209 prepareLoadWidgetPreviewsTask(page, items,
1210 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1211 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001212 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001213 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001214 }
1215 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001216 }
1217 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1218 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001219 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1220 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001221 if (task != null) {
1222 // Ensure that this task starts running at the correct priority
1223 task.syncThreadPriority();
1224 }
1225
1226 // Load each of the widget/shortcut previews
1227 ArrayList<Object> items = data.items;
1228 ArrayList<Bitmap> images = data.generatedImages;
1229 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001230 for (int i = 0; i < count; ++i) {
1231 if (task != null) {
1232 // Ensure we haven't been cancelled yet
1233 if (task.isCancelled()) break;
1234 // Before work on each item, ensure that this task is running at the correct
1235 // priority
1236 task.syncThreadPriority();
1237 }
1238
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001239 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001240 }
Winson Chungb44b5242011-06-13 11:32:14 -07001241 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001242
Winson Chung7bb37522013-10-28 11:07:57 -07001243 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1244 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001245 mDeferredSyncWidgetPageItems.add(data);
1246 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001247 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001248 try {
1249 int page = data.page;
1250 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001251
Michael Jurka39e5d172012-03-12 18:36:12 -07001252 ArrayList<Object> items = data.items;
1253 int count = items.size();
1254 for (int i = 0; i < count; ++i) {
1255 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1256 if (widget != null) {
1257 Bitmap preview = data.generatedImages.get(i);
1258 widget.applyPreview(new FastBitmapDrawable(preview), i);
1259 }
1260 }
Winson Chung68e4c642011-11-10 15:48:25 -08001261
Michael Jurka47639b92013-01-14 12:42:27 +01001262 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001263
1264 // Update all thread priorities
1265 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1266 while (iter.hasNext()) {
1267 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1268 int pageIndex = task.page;
1269 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1270 }
1271 } finally {
1272 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001273 }
Winson Chungb44b5242011-06-13 11:32:14 -07001274 }
Winson Chung46af2e82011-05-09 16:00:53 -07001275
Winson Chung785d2eb2011-04-14 16:08:02 -07001276 @Override
1277 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001278 disablePagedViewAnimations();
1279
Winson Chung785d2eb2011-04-14 16:08:02 -07001280 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001281 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001282
Adam Cohen0cd3b642011-10-14 14:58:00 -07001283 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001284 if (mContentType == ContentType.Applications) {
1285 for (int i = 0; i < mNumAppsPages; ++i) {
1286 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1287 setupPage(layout);
1288 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1289 LayoutParams.MATCH_PARENT));
1290 }
1291 } else if (mContentType == ContentType.Widgets) {
1292 for (int j = 0; j < mNumWidgetPages; ++j) {
1293 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1294 mWidgetCountY);
1295 setupPage(layout);
1296 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1297 LayoutParams.MATCH_PARENT));
1298 }
1299 } else {
1300 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001301 }
1302
Winson Chungc58497e2013-09-03 17:48:37 -07001303 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001305
Winson Chung785d2eb2011-04-14 16:08:02 -07001306 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001307 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001308 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001309 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001310 } else {
1311 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 }
1313 }
1314
Adam Cohen22f823d2011-09-01 17:22:18 -07001315 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1316 // it is in the z-order. This is important to insure touch events are handled correctly.
1317 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001318 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001319 }
1320
Adam Cohenae4f1552011-10-20 00:15:42 -07001321 @Override
1322 protected int indexToPage(int index) {
1323 return getChildCount() - index - 1;
1324 }
1325
Adam Cohen22f823d2011-09-01 17:22:18 -07001326 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1327 @Override
1328 protected void screenScrolled(int screenCenter) {
1329 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001330 enableHwLayersOnVisiblePages();
1331 }
1332
1333 private void enableHwLayersOnVisiblePages() {
1334 final int screenCount = getChildCount();
1335
1336 getVisiblePages(mTempVisiblePagesRange);
1337 int leftScreen = mTempVisiblePagesRange[0];
1338 int rightScreen = mTempVisiblePagesRange[1];
1339 int forceDrawScreen = -1;
1340 if (leftScreen == rightScreen) {
1341 // make sure we're caching at least two pages always
1342 if (rightScreen < screenCount - 1) {
1343 rightScreen++;
1344 forceDrawScreen = rightScreen;
1345 } else if (leftScreen > 0) {
1346 leftScreen--;
1347 forceDrawScreen = leftScreen;
1348 }
1349 } else {
1350 forceDrawScreen = leftScreen + 1;
1351 }
1352
1353 for (int i = 0; i < screenCount; i++) {
1354 final View layout = (View) getPageAt(i);
1355 if (!(leftScreen <= i && i <= rightScreen &&
1356 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1357 layout.setLayerType(LAYER_TYPE_NONE, null);
1358 }
1359 }
1360
Michael Jurka47639b92013-01-14 12:42:27 +01001361 for (int i = 0; i < screenCount; i++) {
1362 final View layout = (View) getPageAt(i);
1363
1364 if (leftScreen <= i && i <= rightScreen &&
1365 (i == forceDrawScreen || shouldDrawChild(layout))) {
1366 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1367 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1368 }
1369 }
1370 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001371 }
1372
1373 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001374 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001375 }
1376
Winson Chung785d2eb2011-04-14 16:08:02 -07001377 /**
1378 * Used by the parent to get the content width to set the tab bar to
1379 * @return
1380 */
1381 public int getPageContentWidth() {
1382 return mContentWidth;
1383 }
1384
Winson Chungb26f3d62011-06-02 10:49:29 -07001385 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001386 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001387 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001388 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001389 // We reset the save index when we change pages so that it will be recalculated on next
1390 // rotation
1391 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001392 }
1393
Winson Chung785d2eb2011-04-14 16:08:02 -07001394 /*
1395 * AllAppsView implementation
1396 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001397 public void setup(Launcher launcher, DragController dragController) {
1398 mLauncher = launcher;
1399 mDragController = dragController;
1400 }
Winson Chung9802ac92012-06-08 16:01:58 -07001401
1402 /**
1403 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1404 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1405 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1406 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1407 */
1408 private void invalidateOnDataChange() {
1409 if (!isDataReady()) {
1410 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1411 // request a layout to trigger the page data when ready.
1412 requestLayout();
1413 } else {
1414 cancelAllTasks();
1415 invalidatePageData();
1416 }
1417 }
1418
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001419 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001420 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001421 mApps = list;
1422 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1423 updatePageCountsAndInvalidateData();
1424 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001425 }
Adam Cohen59400422014-03-05 18:07:04 -08001426
1427 public ArrayList<AppInfo> getApps() {
1428 return mApps;
1429 }
1430
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001431 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 // We add it in place, in alphabetical order
1433 int count = list.size();
1434 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001435 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001436 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001437 if (index < 0) {
1438 mApps.add(-(index + 1), info);
1439 }
1440 }
1441 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001442 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001443 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001444 addAppsWithoutInvalidate(list);
1445 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001446 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001447 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001448 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001449 ComponentName removeComponent = item.intent.getComponent();
1450 int length = list.size();
1451 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001452 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001453 if (info.user.equals(item.user)
1454 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 return i;
1456 }
1457 }
1458 return -1;
1459 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001460 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001461 // loop through all the apps and remove apps that have the same component
1462 int length = list.size();
1463 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001464 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001465 int removeIndex = findAppByComponent(mApps, info);
1466 if (removeIndex > -1) {
1467 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001468 }
1469 }
1470 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001471 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001472 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001473 removeAppsWithoutInvalidate(appInfos);
1474 updatePageCountsAndInvalidateData();
1475 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001476 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001477 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001478 // We remove and re-add the updated applications list because it's properties may have
1479 // changed (ie. the title), and this will ensure that the items will be in their proper
1480 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001481 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001482 removeAppsWithoutInvalidate(list);
1483 addAppsWithoutInvalidate(list);
1484 updatePageCountsAndInvalidateData();
1485 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001487
Winson Chung785d2eb2011-04-14 16:08:02 -07001488 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001489 // If we have reset, then we should not continue to restore the previous state
1490 mSaveInstanceStateItemIndex = -1;
1491
Adam Cohen6c5891a2014-07-09 23:53:15 -07001492 if (mContentType != ContentType.Applications) {
1493 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001494 }
Winson Chung649668f2012-01-10 13:07:16 -08001495
Adam Cohenb64d36e2011-10-17 21:48:02 -07001496 if (mCurrentPage != 0) {
1497 invalidatePageData(0);
1498 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001499 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001500
1501 private AppsCustomizeTabHost getTabHost() {
1502 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1503 }
1504
Winson Chung785d2eb2011-04-14 16:08:02 -07001505 public void dumpState() {
1506 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001507 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001508 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 }
Adam Cohen4e844012011-11-09 13:48:04 -08001510
Winson Chung785d2eb2011-04-14 16:08:02 -07001511 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001512 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001513 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001514 for (Object i: list) {
1515 if (i instanceof AppWidgetProviderInfo) {
1516 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1517 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1518 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1519 + " initialLayout=" + info.initialLayout
1520 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1521 } else if (i instanceof ResolveInfo) {
1522 ResolveInfo info = (ResolveInfo) i;
1523 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1524 + info.icon);
1525 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001526 }
1527 }
Adam Cohen4e844012011-11-09 13:48:04 -08001528
Winson Chung785d2eb2011-04-14 16:08:02 -07001529 public void surrender() {
1530 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1531 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001532
1533 // Stop all background tasks
1534 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001535 }
Winson Chung007c6982011-06-14 13:27:53 -07001536
Winson Chungb44b5242011-06-13 11:32:14 -07001537 /*
1538 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1539 * widget previews in the background with the AsyncTasks.
1540 */
Winson Chung68e4c642011-11-10 15:48:25 -08001541 final static int sLookBehindPageCount = 2;
1542 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001543 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001544 final int count = getChildCount();
1545 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1546 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1547 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001548 }
1549 protected int getAssociatedUpperPageBound(int page) {
1550 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001551 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1552 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1553 count - 1);
1554 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001555 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001556
Winson Chung6a0f57d2011-06-29 20:10:49 -07001557 protected String getCurrentPageDescription() {
1558 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1559 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001560 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001561
1562 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001563 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001564 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001565 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001566 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001567 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001568 } else {
1569 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001570 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001571
Michael Jurka8b805b12012-04-18 14:23:14 -07001572 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001573 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001574}