blob: 1ab11ee8a091bb0869f1c992259283fab0302f7f [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010032import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070036import android.os.Build;
37import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.AttributeSet;
40import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070041import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080042import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
44import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070047import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070049import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.DropTarget.DragObject;
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
Winson Chung7d7541e2011-09-16 20:14:36 -0700173 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800174 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700175 private int mClingFocusedX;
176 private int mClingFocusedY;
177
Winson Chung1ed747a2011-05-03 16:18:34 -0700178 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700179 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700180 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700181
182 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700183 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700184 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700185 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700186 private int mNumAppsPages;
187 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700188 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700189
Winson Chungb44b5242011-06-13 11:32:14 -0700190 // Previews & outlines
191 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800192 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700193
Adam Cohened66b2b2012-01-23 17:28:51 -0800194 private Runnable mInflateWidgetRunnable = null;
195 private Runnable mBindWidgetRunnable = null;
196 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700197 static final int WIDGET_PRELOAD_PENDING = 0;
198 static final int WIDGET_BOUND = 1;
199 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800200 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
201 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800202 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800203 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700204 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800205
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700206 private Toast mWidgetInstructionToast;
207
Michael Jurka39e5d172012-03-12 18:36:12 -0700208 // Deferral of loading widget previews during launcher transitions
209 private boolean mInTransition;
210 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
211 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700212 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
213 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700214
Michael Jurka05713af2013-01-23 12:39:24 +0100215 WidgetPreviewLoader mWidgetPreviewLoader;
216
Michael Jurkac402cd92013-05-20 15:49:32 +0200217 private boolean mInBulkBind;
218 private boolean mNeedToUpdatePageCountsAndInvalidateData;
219
Winson Chung785d2eb2011-04-14 16:08:02 -0700220 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
221 super(context, attrs);
222 mLayoutInflater = LayoutInflater.from(context);
223 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200224 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700225 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400226 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700227 mCanvas = new Canvas();
228 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700229
230 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800231 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700232 LauncherAppState app = LauncherAppState.getInstance();
233 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung4b576be2011-04-27 17:40:20 -0700234 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
235 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700236 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
237 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700238 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700239 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700240
Winson Chung1ed747a2011-05-03 16:18:34 -0700241 // The padding on the non-matched dimension for the default widget preview icons
242 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700243 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700244
245 // Unless otherwise specified this view is important for accessibility.
246 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
247 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
248 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700249 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700250 }
251
252 @Override
253 protected void init() {
254 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700255 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700256
257 Context context = getContext();
258 Resources r = context.getResources();
259 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
260 }
261
Winson Chungc58497e2013-09-03 17:48:37 -0700262 public void onFinishInflate() {
263 super.onFinishInflate();
264
265 LauncherAppState app = LauncherAppState.getInstance();
266 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
267 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
268 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
269 }
270
Winson Chung67ca7e42013-10-31 16:53:19 -0700271 void setAllAppsPadding(Rect r) {
272 mAllAppsPadding.set(r);
273 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700274
Winson Chung67ca7e42013-10-31 16:53:19 -0700275 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700276 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700277 }
278
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100279 WidgetPreviewLoader getWidgetPreviewLoader() {
280 if (mWidgetPreviewLoader == null) {
281 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
282 }
283 return mWidgetPreviewLoader;
284 }
285
Winson Chung5afbf7b2011-07-25 11:53:08 -0700286 /** Returns the item index of the center item on this page so that we can restore to this
287 * item index when we rotate. */
288 private int getMiddleComponentIndexOnCurrentPage() {
289 int i = -1;
290 if (getPageCount() > 0) {
291 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700292 if (mContentType == ContentType.Applications) {
293 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
294 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700295 int numItemsPerPage = mCellCountX * mCellCountY;
296 int childCount = childrenLayout.getChildCount();
297 if (childCount > 0) {
298 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700299 }
Winson Chungc58497e2013-09-03 17:48:37 -0700300 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700301 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700302 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700303 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
304 int childCount = layout.getChildCount();
305 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700306 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700307 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700308 }
Winson Chungc58497e2013-09-03 17:48:37 -0700309 } else {
310 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 }
312 }
313 return i;
314 }
315
316 /** Get the index of the item to restore to if we need to restore the current page. */
317 int getSaveInstanceStateIndex() {
318 if (mSaveInstanceStateItemIndex == -1) {
319 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
320 }
321 return mSaveInstanceStateItemIndex;
322 }
323
324 /** Returns the page in the current orientation which is expected to contain the specified
325 * item index. */
326 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700327 if (index < 0) return 0;
328
329 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 int numItemsPerPage = mCellCountX * mCellCountY;
331 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700334 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 }
337
Winson Chung5afbf7b2011-07-25 11:53:08 -0700338 /** Restores the page for an item at the specified index */
339 void restorePageForIndex(int index) {
340 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700341 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700342 }
343
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700344 private void updatePageCounts() {
345 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
346 (float) (mWidgetCountX * mWidgetCountY));
347 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
348 }
349
Winson Chungf0ea4d32011-06-06 14:27:16 -0700350 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700351 // Now that the data is ready, we can calculate the content width, the number of cells to
352 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700353 LauncherAppState app = LauncherAppState.getInstance();
354 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700355 mCellCountX = (int) grid.allAppsNumCols;
356 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700357 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700358
Winson Chungdb1138b2011-06-30 14:39:35 -0700359 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700360 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
361 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
362 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
363 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700364 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700365
Adam Cohen6c5891a2014-07-09 23:53:15 -0700366 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700367 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800368 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800369 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700370
Adam Cohena00673c2014-08-14 12:57:28 -0700371 protected void onLayout(boolean changed, int l, int t, int r, int b) {
372 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700373
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800375 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 setDataIsReady();
Adam Cohena00673c2014-08-14 12:57:28 -0700377 onDataReady(getMeasuredWidth(), getMeasuredHeight());
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
379 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700380 }
381
Michael Jurkac402cd92013-05-20 15:49:32 +0200382 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700383 LauncherAppState app = LauncherAppState.getInstance();
384 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
385
Winson Chung1ed747a2011-05-03 16:18:34 -0700386 // Get the list of widgets and shortcuts
387 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200388 for (Object o : widgetsAndShortcuts) {
389 if (o instanceof AppWidgetProviderInfo) {
390 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100391 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
392 continue;
393 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200394 widget.label = widget.label.trim();
395 if (widget.minWidth > 0 && widget.minHeight > 0) {
396 // Ensure that all widgets we show can be added on a workspace of this size
397 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
398 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
399 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
400 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700401 if (minSpanX <= (int) grid.numColumns &&
402 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200403 mWidgets.add(widget);
404 } else {
405 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
406 widget.minWidth + ", " + widget.minHeight + ")");
407 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700408 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200409 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
410 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700411 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800412 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200413 // just add shortcuts
414 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800415 }
416 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200417 updatePageCountsAndInvalidateData();
418 }
419
420 public void setBulkBind(boolean bulkBind) {
421 if (bulkBind) {
422 mInBulkBind = true;
423 } else {
424 mInBulkBind = false;
425 if (mNeedToUpdatePageCountsAndInvalidateData) {
426 updatePageCountsAndInvalidateData();
427 }
428 }
429 }
430
431 private void updatePageCountsAndInvalidateData() {
432 if (mInBulkBind) {
433 mNeedToUpdatePageCountsAndInvalidateData = true;
434 } else {
435 updatePageCounts();
436 invalidateOnDataChange();
437 mNeedToUpdatePageCountsAndInvalidateData = false;
438 }
Winson Chung4b576be2011-04-27 17:40:20 -0700439 }
440
441 @Override
442 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700443 // When we have exited all apps or are in transition, disregard clicks
Sunny Goyal508da152014-08-14 10:53:27 -0700444 if (!mLauncher.isAllAppsVisible()
445 || mLauncher.getWorkspace().isSwitchingState()
446 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700447
Sunny Goyal508da152014-08-14 10:53:27 -0700448 // Let the user know that they have to long press to add a widget
449 if (mWidgetInstructionToast != null) {
450 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700451 }
Sunny Goyal508da152014-08-14 10:53:27 -0700452 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
453 Toast.LENGTH_SHORT);
454 mWidgetInstructionToast.show();
455
456 // Create a little animation to show that the widget can move
457 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
458 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
459 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
460 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
461 tyuAnim.setDuration(125);
462 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
463 tydAnim.setDuration(100);
464 bounce.play(tyuAnim).before(tydAnim);
465 bounce.setInterpolator(new AccelerateInterpolator());
466 bounce.start();
Winson Chung785d2eb2011-04-14 16:08:02 -0700467 }
468
Winson Chungc6f10b92011-11-14 11:39:07 -0800469 public boolean onKey(View v, int keyCode, KeyEvent event) {
470 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
471 }
472
Winson Chung785d2eb2011-04-14 16:08:02 -0700473 /*
474 * PagedViewWithDraggableItems implementation
475 */
476 @Override
477 protected void determineDraggingStart(android.view.MotionEvent ev) {
478 // Disable dragging by pulling an app down for now.
479 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700480
Winson Chung4b576be2011-04-27 17:40:20 -0700481 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700482 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700483 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700484
Sunny Goyalff572272014-07-23 13:58:07 -0700485 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700486 Bundle options = null;
487 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700488 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
489 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700490 info.componentName, null);
491
Sunny Goyalff572272014-07-23 13:58:07 -0700492 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700493 int xPaddingDips = (int) ((padding.left + padding.right) / density);
494 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
495
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700496 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700497 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700498 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700499 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700500 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700501 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700502 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700503 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700504 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700505 }
506 return options;
507 }
508
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700509 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800510 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700511 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
512
Adam Cohened66b2b2012-01-23 17:28:51 -0800513 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700514 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800515 return;
516 }
517
Adam Cohen21a170b2012-05-30 15:17:06 -0700518 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800519 mBindWidgetRunnable = new Runnable() {
520 @Override
521 public void run() {
522 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700523 // Options will be null for platforms with JB or lower, so this serves as an
524 // SDK level check.
525 if (options == null) {
526 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
527 mWidgetLoadingId, info.componentName)) {
528 mWidgetCleanupState = WIDGET_BOUND;
529 }
530 } else {
531 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
532 mWidgetLoadingId, info.componentName, options)) {
533 mWidgetCleanupState = WIDGET_BOUND;
534 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700535 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800536 }
537 };
538 post(mBindWidgetRunnable);
539
540 mInflateWidgetRunnable = new Runnable() {
541 @Override
542 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700543 if (mWidgetCleanupState != WIDGET_BOUND) {
544 return;
545 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 AppWidgetHostView hostView = mLauncher.
547 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 info.boundWidget = hostView;
549 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800550 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700551 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
552 info.spanY, info, false);
553
554 // We want the first widget layout to be the correct size. This will be important
555 // for width size reporting to the AppWidgetManager.
556 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
557 unScaledSize[1]);
558 lp.x = lp.y = 0;
559 lp.customPosition = true;
560 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800561 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800562 }
563 };
564 post(mInflateWidgetRunnable);
565 }
566
567 @Override
568 public void onShortPress(View v) {
569 // We are anticipating a long press, and we use this time to load bind and instantiate
570 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700571 if (mCreateWidgetInfo != null) {
572 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
573 cleanupWidgetPreloading(false);
574 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800575 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700576 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 }
578
Adam Cohen0e56cc92012-05-11 15:57:05 -0700579 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
580 if (!widgetWasAdded) {
581 // If the widget was not added, we may need to do further cleanup.
582 PendingAddWidgetInfo info = mCreateWidgetInfo;
583 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700584
585 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700586 // We never did any preloading, so just remove pending callbacks to do so
587 removeCallbacks(mBindWidgetRunnable);
588 removeCallbacks(mInflateWidgetRunnable);
589 } else if (mWidgetCleanupState == WIDGET_BOUND) {
590 // Delete the widget id which was allocated
591 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700592 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
593 }
594
595 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700596 removeCallbacks(mInflateWidgetRunnable);
597 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700598 // Delete the widget id which was allocated
599 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700600 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
601 }
602
Adam Cohen0e56cc92012-05-11 15:57:05 -0700603 // The widget was inflated and added to the DragLayer -- remove it.
604 AppWidgetHostView widget = info.boundWidget;
605 mLauncher.getDragLayer().removeView(widget);
606 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 }
608 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
609 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700610 mCreateWidgetInfo = null;
611 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800612 }
613
Adam Cohen7a326642012-02-22 12:03:22 -0800614 @Override
615 public void cleanUpShortPress(View v) {
616 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700617 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800618 }
619 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800620
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700621 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800622 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700623 // Get the widget preview as the drag representation
624 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700625 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700626
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700627 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
628 // we abort the drag.
629 if (image.getDrawable() == null) {
630 mDraggingWidget = false;
631 return false;
632 }
633
Winson Chung4b576be2011-04-27 17:40:20 -0700634 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800635 Bitmap preview;
636 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800637 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100638 Point previewPadding = null;
639
Winson Chung1ed747a2011-05-03 16:18:34 -0700640 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700641 // This can happen in some weird cases involving multi-touch. We can't start dragging
642 // the widget if this is null, so we break out.
643 if (mCreateWidgetInfo == null) {
644 return false;
645 }
646
Adam Cohen1b36dc32012-02-13 19:27:37 -0800647 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
648 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700649 int spanX = createItemInfo.spanX;
650 int spanY = createItemInfo.spanY;
651 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
652 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700653
Winson Chung72d59842012-02-22 13:51:36 -0800654 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
655 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700656 int maxWidth, maxHeight;
657 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
658 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800659
Michael Jurka05713af2013-01-23 12:39:24 +0100660 int[] previewSizeBeforeScale = new int[1];
661
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100662 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100663 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
664 maxWidth, maxHeight, null, previewSizeBeforeScale);
665
666 // Compare the size of the drag preview to the preview in the AppsCustomize tray
667 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100668 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100669 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
670
671 // The bitmap in the AppsCustomize tray is always the the same size, so there
672 // might be extra pixels around the preview itself - this accounts for that
673 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
674 int padding =
675 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
676 previewPadding = new Point(padding, 0);
677 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700678 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700679 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
680 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
681 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
682 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
683
Winson Chung1120e032011-11-22 16:11:31 -0800684 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700685 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100686 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700687 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700688 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700689 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700690 createItemInfo.spanX = createItemInfo.spanY = 1;
691 }
Winson Chung4b576be2011-04-27 17:40:20 -0700692
Michael Jurka8c3339b2012-06-14 16:18:21 -0700693 // Don't clip alpha values for the drag outline if we're using the default widget preview
694 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
695 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700696
Winson Chung1120e032011-11-22 16:11:31 -0800697 // Save the preview for the outline generation, then dim the preview
698 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
699 false);
Winson Chung1120e032011-11-22 16:11:31 -0800700
Winson Chung4b576be2011-04-27 17:40:20 -0700701 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700702 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700703 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800704 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100705 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800706 outline.recycle();
707 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700708 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700709 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800710
Winson Chung4b576be2011-04-27 17:40:20 -0700711 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700713 if (!super.beginDragging(v)) return false;
714
Sunny Goyal508da152014-08-14 10:53:27 -0700715 if (v instanceof BubbleTextView) {
Winson Chung4b576be2011-04-27 17:40:20 -0700716 beginDraggingApplication(v);
717 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700718 if (!beginDraggingWidget(v)) {
719 return false;
720 }
Winson Chung4b576be2011-04-27 17:40:20 -0700721 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800722
723 // We delay entering spring-loaded mode slightly to make sure the UI
724 // thready is free of any work.
725 postDelayed(new Runnable() {
726 @Override
727 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800728 // We don't enter spring-loaded mode if the drag has been cancelled
729 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800730 // Go into spring loaded mode (must happen before we startDrag())
731 mLauncher.enterSpringLoadedDragMode();
732 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 }
Winson Chung72d59842012-02-22 13:51:36 -0800734 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800735
Winson Chung785d2eb2011-04-14 16:08:02 -0700736 return true;
737 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800738
Winson Chunga48487a2012-03-20 16:19:37 -0700739 /**
740 * Clean up after dragging.
741 *
742 * @param target where the item was dragged to (can be null if the item was flung)
743 */
744 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700745 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800746 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700747 // Exit spring loaded mode if we have not successfully dropped or have not handled the
748 // drop in Workspace
Adam Cohen689ff162014-05-08 17:27:56 -0700749 mLauncher.exitSpringLoadedDragMode();
750 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700751 } else {
752 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700753 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700754 }
755
Winson Chung785d2eb2011-04-14 16:08:02 -0700756 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700757 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700758 if (getChildCount() > 0) {
759 return getChildAt(0);
760 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700761 return null;
762 }
763
764 @Override
765 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700766 mInTransition = true;
767 if (toWorkspace) {
768 cancelAllTasks();
769 }
770 }
771
772 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700773 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700774 }
775
776 @Override
777 public void onLauncherTransitionStep(Launcher l, float t) {
778 }
779
780 @Override
781 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
782 mInTransition = false;
783 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700784 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700785 }
786 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700787 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
788 r.run();
789 }
790 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700791 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700792 }
793
794 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700795 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
796 boolean success) {
797 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
798 if (isFlingToDelete) return;
799
800 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700801
802 // Display an error message if the drag failed due to there not being enough space on the
803 // target layout we were dropping on.
804 if (!success) {
805 boolean showOutOfSpaceMessage = false;
806 if (target instanceof Workspace) {
807 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
808 Workspace workspace = (Workspace) target;
809 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700810 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700811 if (layout != null) {
812 layout.calculateSpans(itemInfo);
813 showOutOfSpaceMessage =
814 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
815 }
816 }
Winson Chungfc79c802011-05-02 13:35:34 -0700817 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700818 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700819 }
Adam Cohen7a326642012-02-22 12:03:22 -0800820
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800821 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700822 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700823 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800824 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 }
826
Winson Chunga48487a2012-03-20 16:19:37 -0700827 @Override
828 public void onFlingToDeleteCompleted() {
829 // We just dismiss the drag when we fling, so cleanup here
830 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700831 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700832 mDraggingWidget = false;
833 }
834
835 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800836 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700837 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800838 }
839
Winson Chung7f0acdd2011-09-19 18:34:19 -0700840 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000841 public boolean supportsAppInfoDropTarget() {
842 return true;
843 }
844
845 @Override
846 public boolean supportsDeleteDropTarget() {
847 return false;
848 }
849
850 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800851 public float getIntrinsicIconScaleFactor() {
852 LauncherAppState app = LauncherAppState.getInstance();
853 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
854 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
855 }
856
857 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700858 protected void onDetachedFromWindow() {
859 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700860 cancelAllTasks();
861 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700862
Michael Jurkae326f182011-11-21 14:05:46 -0800863 public void clearAllWidgetPages() {
864 cancelAllTasks();
865 int count = getChildCount();
866 for (int i = 0; i < count; i++) {
867 View v = getPageAt(i);
868 if (v instanceof PagedViewGridLayout) {
869 ((PagedViewGridLayout) v).removeAllViewsOnPage();
870 mDirtyPageContent.set(i, true);
871 }
872 }
873 }
874
Adam Cohen0cd3b642011-10-14 14:58:00 -0700875 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700876 // Clean up all the async tasks
877 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
878 while (iter.hasNext()) {
879 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
880 task.cancel(false);
881 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700882 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700883
884 // We've already preallocated the views for the data to load into, so clear them as well
885 View v = getPageAt(task.page);
886 if (v instanceof PagedViewGridLayout) {
887 ((PagedViewGridLayout) v).removeAllViewsOnPage();
888 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700889 }
Winson Chung83687b12012-04-25 16:01:01 -0700890 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700891 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700892 }
893
Winson Chung785d2eb2011-04-14 16:08:02 -0700894 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200895 // Widgets appear to be cleared every time you leave, always force invalidate for them
896 if (mContentType != type || type == ContentType.Widgets) {
897 int page = (mContentType != type) ? 0 : getCurrentPage();
898 mContentType = type;
899 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700900 }
Winson Chungc58497e2013-09-03 17:48:37 -0700901 }
902
903 public ContentType getContentType() {
904 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700905 }
906
Adam Cohen0cd3b642011-10-14 14:58:00 -0700907 protected void snapToPage(int whichPage, int delta, int duration) {
908 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800909
910 // Update the thread priorities given the direction lookahead
911 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
912 while (iter.hasNext()) {
913 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700914 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800915 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
916 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
917 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
918 } else {
919 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
920 }
921 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700922 }
923
Winson Chung785d2eb2011-04-14 16:08:02 -0700924 /*
925 * Apps PagedView implementation
926 */
Winson Chung63257c12011-05-05 17:06:13 -0700927 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
928 int childCount = layout.getChildCount();
929 for (int i = 0; i < childCount; ++i) {
930 layout.getChildAt(i).setVisibility(visibility);
931 }
932 }
Winson Chungc58497e2013-09-03 17:48:37 -0700933 private void setupPage(AppsCustomizeCellLayout layout) {
934 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700935
Winson Chung63257c12011-05-05 17:06:13 -0700936 // Note: We force a measure here to get around the fact that when we do layout calculations
937 // immediately after syncing, we don't have a proper width. That said, we already know the
938 // expected page width, so we can actually optimize by hiding all the TextView-based
939 // children that are expensive to measure, and let that happen naturally later.
940 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700941 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
942 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700943 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700944
Adam Cohen4e243a22014-08-10 18:30:55 -0700945 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700946 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700947 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700948 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700949 }
950
951 setVisibilityOnChildren(layout, View.VISIBLE);
952 }
953
954 public void setPageBackgroundsVisible(boolean visible) {
955 mPageBackgroundsVisible = visible;
956 int childCount = getChildCount();
957 for (int i = 0; i < childCount; ++i) {
958 Drawable bg = getChildAt(i).getBackground();
959 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700960 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700961 }
962 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700964
Winson Chungf314b0e2011-08-16 11:54:27 -0700965 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700966 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700967 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 int numCells = mCellCountX * mCellCountY;
969 int startIndex = page * numCells;
970 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700971 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700972
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700974 ArrayList<Object> items = new ArrayList<Object>();
975 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200977 AppInfo info = mApps.get(i);
Sunny Goyal508da152014-08-14 10:53:27 -0700978 BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700979 R.layout.apps_customize_application, layout, false);
Sunny Goyal508da152014-08-14 10:53:27 -0700980 icon.applyFromApplicationInfo(info);
981 icon.setOnClickListener(mLauncher);
Winson Chung785d2eb2011-04-14 16:08:02 -0700982 icon.setOnLongClickListener(this);
983 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800984 icon.setOnKeyListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700985 icon.setOnFocusChangeListener(layout.mFocusHandlerView);
Winson Chung785d2eb2011-04-14 16:08:02 -0700986
987 int index = i - startIndex;
988 int x = index % mCellCountX;
989 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -0700990 if (isRtl) {
991 x = mCellCountX - x - 1;
992 }
Winson Chungc58497e2013-09-03 17:48:37 -0700993 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -0700994
995 items.add(info);
996 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700998
Michael Jurka47639b92013-01-14 12:42:27 +0100999 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 }
Winson Chungb44b5242011-06-13 11:32:14 -07001001
1002 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001003 * A helper to return the priority for loading of the specified widget page.
1004 */
1005 private int getWidgetPageLoadPriority(int page) {
1006 // If we are snapping to another page, use that index as the target page index
1007 int toPage = mCurrentPage;
1008 if (mNextPage > -1) {
1009 toPage = mNextPage;
1010 }
1011
1012 // We use the distance from the target page as an initial guess of priority, but if there
1013 // are no pages of higher priority than the page specified, then bump up the priority of
1014 // the specified page.
1015 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1016 int minPageDiff = Integer.MAX_VALUE;
1017 while (iter.hasNext()) {
1018 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001019 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001020 }
1021
1022 int rawPageDiff = Math.abs(page - toPage);
1023 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1024 }
1025 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001026 * Return the appropriate thread priority for loading for a given page (we give the current
1027 * page much higher priority)
1028 */
1029 private int getThreadPriorityForPage(int page) {
1030 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001031 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001032 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001033 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001034 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001035 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001036 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001037 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001038 }
1039 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001040 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001041 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001042 return Math.max(0, pageDiff * sPageSleepDelay);
1043 }
Winson Chungb44b5242011-06-13 11:32:14 -07001044 /**
1045 * Creates and executes a new AsyncTask to load a page of widget previews.
1046 */
1047 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001048 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001049
Winson Chungb44b5242011-06-13 11:32:14 -07001050 // Prune all tasks that are no longer needed
1051 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1052 while (iter.hasNext()) {
1053 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001054 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001055 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1056 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001057 task.cancel(false);
1058 iter.remove();
1059 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001060 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001061 }
1062 }
1063
Winson Chungf314b0e2011-08-16 11:54:27 -07001064 // 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 -07001065 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001066 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001067 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001068 @Override
1069 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001070 try {
Winson Chung09945932011-09-20 14:22:40 -07001071 try {
1072 Thread.sleep(sleepMs);
1073 } catch (Exception e) {}
1074 loadWidgetPreviewsInBackground(task, data);
1075 } finally {
1076 if (task.isCancelled()) {
1077 data.cleanup(true);
1078 }
1079 }
Winson Chungb44b5242011-06-13 11:32:14 -07001080 }
1081 },
1082 new AsyncTaskCallback() {
1083 @Override
1084 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001085 mRunningTasks.remove(task);
1086 if (task.isCancelled()) return;
1087 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001088 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001089 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001090 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001091
1092 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001093 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001094 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001095 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001096 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1097 mRunningTasks.add(t);
1098 }
Winson Chungb44b5242011-06-13 11:32:14 -07001099
Winson Chung785d2eb2011-04-14 16:08:02 -07001100 /*
1101 * Widgets PagedView implementation
1102 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001103 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001104 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001105 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001106 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1107 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001108
1109 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1110 if (bg != null) {
1111 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1112 layout.setBackground(bg);
1113 }
Winson Chung63257c12011-05-05 17:06:13 -07001114 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001115 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001116
Michael Jurka038f9d82011-11-03 13:50:45 -07001117 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001118 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001119
Adam Cohen4e243a22014-08-10 18:30:55 -07001120 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1121
Winson Chungd2945262011-06-24 15:22:14 -07001122 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001123 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001124 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001125 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001126 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1127
Adam Cohen4e243a22014-08-10 18:30:55 -07001128 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001129
Winson Chunge4a647f2011-09-30 14:41:25 -07001130 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001131 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001132 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1133 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001134 }
1135
Winson Chunge4a647f2011-09-30 14:41:25 -07001136 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001137 layout.setColumnCount(layout.getCellCountX());
1138 for (int i = 0; i < items.size(); ++i) {
1139 Object rawInfo = items.get(i);
1140 PendingAddItemInfo createItemInfo = null;
1141 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1142 R.layout.apps_customize_widget, layout, false);
1143 if (rawInfo instanceof AppWidgetProviderInfo) {
1144 // Fill in the widget information
1145 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1146 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001147
1148 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001149 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001150 createItemInfo.spanX = spanXY[0];
1151 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001152 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001153 createItemInfo.minSpanX = minSpanXY[0];
1154 createItemInfo.minSpanY = minSpanXY[1];
1155
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001156 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001157 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001158 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001159 } else if (rawInfo instanceof ResolveInfo) {
1160 // Fill in the shortcuts information
1161 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001162 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001163 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1164 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1165 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001166 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001167 widget.setTag(createItemInfo);
1168 }
1169 widget.setOnClickListener(this);
1170 widget.setOnLongClickListener(this);
1171 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001172 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001173
1174 // Layout each widget
1175 int ix = i % mWidgetCountX;
1176 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001177
1178 if (ix > 0) {
1179 View border = widget.findViewById(R.id.left_border);
1180 border.setVisibility(View.VISIBLE);
1181 }
1182 if (ix < mWidgetCountX - 1) {
1183 View border = widget.findViewById(R.id.right_border);
1184 border.setVisibility(View.VISIBLE);
1185 }
1186
Winson Chunge4a647f2011-09-30 14:41:25 -07001187 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001188 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001189 GridLayout.spec(ix, GridLayout.TOP));
1190 lp.width = cellWidth;
1191 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001192 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001193 layout.addView(widget, lp);
1194 }
1195
Michael Jurka038f9d82011-11-03 13:50:45 -07001196 // wait until a call on onLayout to start loading, because
1197 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1198 // TODO: can we do a measure/layout immediately?
1199 layout.setOnLayoutListener(new Runnable() {
1200 public void run() {
1201 // Load the widget previews
1202 int maxPreviewWidth = cellWidth;
1203 int maxPreviewHeight = cellHeight;
1204 if (layout.getChildCount() > 0) {
1205 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1206 int[] maxSize = w.getPreviewSize();
1207 maxPreviewWidth = maxSize[0];
1208 maxPreviewHeight = maxSize[1];
1209 }
Michael Jurka05713af2013-01-23 12:39:24 +01001210
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001211 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001212 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001213 if (immediate) {
1214 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001215 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001216 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001217 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001218 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001219 if (mInTransition) {
1220 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1221 } else {
1222 prepareLoadWidgetPreviewsTask(page, items,
1223 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1224 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001225 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001226 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001227 }
1228 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001229 }
1230 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1231 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001232 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1233 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001234 if (task != null) {
1235 // Ensure that this task starts running at the correct priority
1236 task.syncThreadPriority();
1237 }
1238
1239 // Load each of the widget/shortcut previews
1240 ArrayList<Object> items = data.items;
1241 ArrayList<Bitmap> images = data.generatedImages;
1242 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001243 for (int i = 0; i < count; ++i) {
1244 if (task != null) {
1245 // Ensure we haven't been cancelled yet
1246 if (task.isCancelled()) break;
1247 // Before work on each item, ensure that this task is running at the correct
1248 // priority
1249 task.syncThreadPriority();
1250 }
1251
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001252 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001253 }
Winson Chungb44b5242011-06-13 11:32:14 -07001254 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001255
Winson Chung7bb37522013-10-28 11:07:57 -07001256 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1257 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001258 mDeferredSyncWidgetPageItems.add(data);
1259 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001260 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001261 try {
1262 int page = data.page;
1263 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001264
Michael Jurka39e5d172012-03-12 18:36:12 -07001265 ArrayList<Object> items = data.items;
1266 int count = items.size();
1267 for (int i = 0; i < count; ++i) {
1268 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1269 if (widget != null) {
1270 Bitmap preview = data.generatedImages.get(i);
1271 widget.applyPreview(new FastBitmapDrawable(preview), i);
1272 }
1273 }
Winson Chung68e4c642011-11-10 15:48:25 -08001274
Michael Jurka47639b92013-01-14 12:42:27 +01001275 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001276
1277 // Update all thread priorities
1278 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1279 while (iter.hasNext()) {
1280 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1281 int pageIndex = task.page;
1282 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1283 }
1284 } finally {
1285 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001286 }
Winson Chungb44b5242011-06-13 11:32:14 -07001287 }
Winson Chung46af2e82011-05-09 16:00:53 -07001288
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 @Override
1290 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001291 disablePagedViewAnimations();
1292
Winson Chung785d2eb2011-04-14 16:08:02 -07001293 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001294 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001295
Adam Cohen0cd3b642011-10-14 14:58:00 -07001296 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001297 if (mContentType == ContentType.Applications) {
1298 for (int i = 0; i < mNumAppsPages; ++i) {
1299 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1300 setupPage(layout);
1301 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1302 LayoutParams.MATCH_PARENT));
1303 }
1304 } else if (mContentType == ContentType.Widgets) {
1305 for (int j = 0; j < mNumWidgetPages; ++j) {
1306 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1307 mWidgetCountY);
1308 setupPage(layout);
1309 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1310 LayoutParams.MATCH_PARENT));
1311 }
1312 } else {
1313 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001314 }
1315
Winson Chungc58497e2013-09-03 17:48:37 -07001316 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001317 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001318
Winson Chung785d2eb2011-04-14 16:08:02 -07001319 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001320 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001321 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001322 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001323 } else {
1324 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001325 }
1326 }
1327
Adam Cohen22f823d2011-09-01 17:22:18 -07001328 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1329 // it is in the z-order. This is important to insure touch events are handled correctly.
1330 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001331 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001332 }
1333
Adam Cohenae4f1552011-10-20 00:15:42 -07001334 @Override
1335 protected int indexToPage(int index) {
1336 return getChildCount() - index - 1;
1337 }
1338
Adam Cohen22f823d2011-09-01 17:22:18 -07001339 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1340 @Override
1341 protected void screenScrolled(int screenCenter) {
1342 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001343 enableHwLayersOnVisiblePages();
1344 }
1345
1346 private void enableHwLayersOnVisiblePages() {
1347 final int screenCount = getChildCount();
1348
1349 getVisiblePages(mTempVisiblePagesRange);
1350 int leftScreen = mTempVisiblePagesRange[0];
1351 int rightScreen = mTempVisiblePagesRange[1];
1352 int forceDrawScreen = -1;
1353 if (leftScreen == rightScreen) {
1354 // make sure we're caching at least two pages always
1355 if (rightScreen < screenCount - 1) {
1356 rightScreen++;
1357 forceDrawScreen = rightScreen;
1358 } else if (leftScreen > 0) {
1359 leftScreen--;
1360 forceDrawScreen = leftScreen;
1361 }
1362 } else {
1363 forceDrawScreen = leftScreen + 1;
1364 }
1365
1366 for (int i = 0; i < screenCount; i++) {
1367 final View layout = (View) getPageAt(i);
1368 if (!(leftScreen <= i && i <= rightScreen &&
1369 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1370 layout.setLayerType(LAYER_TYPE_NONE, null);
1371 }
1372 }
1373
Michael Jurka47639b92013-01-14 12:42:27 +01001374 for (int i = 0; i < screenCount; i++) {
1375 final View layout = (View) getPageAt(i);
1376
1377 if (leftScreen <= i && i <= rightScreen &&
1378 (i == forceDrawScreen || shouldDrawChild(layout))) {
1379 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1380 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1381 }
1382 }
1383 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001384 }
1385
1386 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001387 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001388 }
1389
Winson Chung785d2eb2011-04-14 16:08:02 -07001390 /**
1391 * Used by the parent to get the content width to set the tab bar to
1392 * @return
1393 */
1394 public int getPageContentWidth() {
1395 return mContentWidth;
1396 }
1397
Winson Chungb26f3d62011-06-02 10:49:29 -07001398 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001399 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001400 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001401 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001402 // We reset the save index when we change pages so that it will be recalculated on next
1403 // rotation
1404 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001405 }
1406
Winson Chung785d2eb2011-04-14 16:08:02 -07001407 /*
1408 * AllAppsView implementation
1409 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001410 public void setup(Launcher launcher, DragController dragController) {
1411 mLauncher = launcher;
1412 mDragController = dragController;
1413 }
Winson Chung9802ac92012-06-08 16:01:58 -07001414
1415 /**
1416 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1417 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1418 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1419 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1420 */
1421 private void invalidateOnDataChange() {
1422 if (!isDataReady()) {
1423 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1424 // request a layout to trigger the page data when ready.
1425 requestLayout();
1426 } else {
1427 cancelAllTasks();
1428 invalidatePageData();
1429 }
1430 }
1431
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001432 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001433 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001434 mApps = list;
1435 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1436 updatePageCountsAndInvalidateData();
1437 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001439 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001440 // We add it in place, in alphabetical order
1441 int count = list.size();
1442 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001443 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001444 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001445 if (index < 0) {
1446 mApps.add(-(index + 1), info);
1447 }
1448 }
1449 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001450 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001451 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001452 addAppsWithoutInvalidate(list);
1453 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001454 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001456 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 ComponentName removeComponent = item.intent.getComponent();
1458 int length = list.size();
1459 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001460 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001461 if (info.user.equals(item.user)
1462 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001463 return i;
1464 }
1465 }
1466 return -1;
1467 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001468 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001469 // loop through all the apps and remove apps that have the same component
1470 int length = list.size();
1471 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001472 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001473 int removeIndex = findAppByComponent(mApps, info);
1474 if (removeIndex > -1) {
1475 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001476 }
1477 }
1478 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001479 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001480 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001481 removeAppsWithoutInvalidate(appInfos);
1482 updatePageCountsAndInvalidateData();
1483 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001484 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001485 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 // We remove and re-add the updated applications list because it's properties may have
1487 // changed (ie. the title), and this will ensure that the items will be in their proper
1488 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001489 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001490 removeAppsWithoutInvalidate(list);
1491 addAppsWithoutInvalidate(list);
1492 updatePageCountsAndInvalidateData();
1493 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001494 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001495
Winson Chung785d2eb2011-04-14 16:08:02 -07001496 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001497 // If we have reset, then we should not continue to restore the previous state
1498 mSaveInstanceStateItemIndex = -1;
1499
Adam Cohen6c5891a2014-07-09 23:53:15 -07001500 if (mContentType != ContentType.Applications) {
1501 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001502 }
Winson Chung649668f2012-01-10 13:07:16 -08001503
Adam Cohenb64d36e2011-10-17 21:48:02 -07001504 if (mCurrentPage != 0) {
1505 invalidatePageData(0);
1506 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001507 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001508
1509 private AppsCustomizeTabHost getTabHost() {
1510 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1511 }
1512
Winson Chung785d2eb2011-04-14 16:08:02 -07001513 public void dumpState() {
1514 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001515 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001516 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001517 }
Adam Cohen4e844012011-11-09 13:48:04 -08001518
Winson Chung785d2eb2011-04-14 16:08:02 -07001519 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001520 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001521 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001522 for (Object i: list) {
1523 if (i instanceof AppWidgetProviderInfo) {
1524 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1525 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1526 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1527 + " initialLayout=" + info.initialLayout
1528 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1529 } else if (i instanceof ResolveInfo) {
1530 ResolveInfo info = (ResolveInfo) i;
1531 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1532 + info.icon);
1533 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001534 }
1535 }
Adam Cohen4e844012011-11-09 13:48:04 -08001536
Winson Chung785d2eb2011-04-14 16:08:02 -07001537 public void surrender() {
1538 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1539 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001540
1541 // Stop all background tasks
1542 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001543 }
Winson Chung007c6982011-06-14 13:27:53 -07001544
Winson Chungb44b5242011-06-13 11:32:14 -07001545 /*
1546 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1547 * widget previews in the background with the AsyncTasks.
1548 */
Winson Chung68e4c642011-11-10 15:48:25 -08001549 final static int sLookBehindPageCount = 2;
1550 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001551 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001552 final int count = getChildCount();
1553 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1554 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1555 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001556 }
1557 protected int getAssociatedUpperPageBound(int page) {
1558 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001559 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1560 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1561 count - 1);
1562 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001563 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001564
Winson Chung6a0f57d2011-06-29 20:10:49 -07001565 protected String getCurrentPageDescription() {
1566 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1567 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001568 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001569
1570 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001571 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001572 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001573 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001574 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001575 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001576 } else {
1577 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001578 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001579
Michael Jurka8b805b12012-04-18 14:23:14 -07001580 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001581 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001582}