blob: b2228f7aff0a395d216ada5f23a4b7ad2dc8c22b [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
29import android.content.res.TypedArray;
30import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010032import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070036import android.os.Build;
37import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.util.AttributeSet;
40import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070041import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080042import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
44import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070047import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070048import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070050import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * A simple callback interface which also provides the results of the task.
61 */
62interface AsyncTaskCallback {
63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
64}
Winson Chung4e076542011-06-23 13:04:10 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * The data needed to perform either of the custom AsyncTasks.
68 */
69class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070070 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080071 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070072 }
73
Michael Jurka038f9d82011-11-03 13:50:45 -070074 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010075 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070076 page = p;
77 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070078 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070079 maxImageWidth = cw;
80 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070081 doInBackgroundCallback = bgR;
82 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010083 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070084 }
Winson Chung09945932011-09-20 14:22:40 -070085 void cleanup(boolean cancelled) {
86 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070087 if (generatedImages != null) {
88 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010089 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010090 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070091 }
92 }
93 generatedImages.clear();
94 }
95 }
Winson Chungb44b5242011-06-13 11:32:14 -070096 int page;
97 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070098 ArrayList<Bitmap> sourceImages;
99 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700100 int maxImageWidth;
101 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 AsyncTaskCallback doInBackgroundCallback;
103 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100104 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700105}
Winson Chung4e076542011-06-23 13:04:10 -0700106
Winson Chungb44b5242011-06-13 11:32:14 -0700107/**
108 * A generic template for an async task used in AppsCustomize.
109 */
110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700111 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700112 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700113 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700114 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700115 }
116 @Override
117 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
118 if (params.length != 1) return null;
119 // Load each of the widget previews in the background
120 params[0].doInBackgroundCallback.run(this, params[0]);
121 return params[0];
122 }
123 @Override
124 protected void onPostExecute(AsyncTaskPageData result) {
125 // All the widget previews are loaded, so we can just callback to inflate the page
126 result.postExecuteCallback.run(this, result);
127 }
128
129 void setThreadPriority(int p) {
130 threadPriority = p;
131 }
132 void syncThreadPriority() {
133 Process.setThreadPriority(threadPriority);
134 }
135
136 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700137 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int threadPriority;
140}
Winson Chungb44b5242011-06-13 11:32:14 -0700141
142/**
143 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
144 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700145public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700146 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700147 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
148 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700149 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700150
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static Rect sTmpRect = new Rect();
152
Winson Chung785d2eb2011-04-14 16:08:02 -0700153 /**
154 * The different content types that this paged view can show.
155 */
156 public enum ContentType {
157 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700158 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700159 }
Winson Chungc58497e2013-09-03 17:48:37 -0700160 private ContentType mContentType = ContentType.Applications;
Winson Chung785d2eb2011-04-14 16:08:02 -0700161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800170 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200173 private ArrayList<AppInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700174 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700175
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800177 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 private int mClingFocusedX;
179 private int mClingFocusedY;
180
Winson Chung1ed747a2011-05-03 16:18:34 -0700181 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700182 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700183 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700184
185 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700186 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700187 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700189 private int mNumAppsPages;
190 private int mNumWidgetPages;
Winson Chung67ca7e42013-10-31 16:53:19 -0700191 private Rect mAllAppsPadding = new Rect();
Winson Chung785d2eb2011-04-14 16:08:02 -0700192
Winson Chungb44b5242011-06-13 11:32:14 -0700193 // Previews & outlines
194 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800195 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700196
Adam Cohened66b2b2012-01-23 17:28:51 -0800197 private Runnable mInflateWidgetRunnable = null;
198 private Runnable mBindWidgetRunnable = null;
199 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700200 static final int WIDGET_PRELOAD_PENDING = 0;
201 static final int WIDGET_BOUND = 1;
202 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800203 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
204 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800205 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800206 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700207 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800208
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700209 private Toast mWidgetInstructionToast;
210
Michael Jurka39e5d172012-03-12 18:36:12 -0700211 // Deferral of loading widget previews during launcher transitions
212 private boolean mInTransition;
213 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
214 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700215 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
216 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700217
Michael Jurka05713af2013-01-23 12:39:24 +0100218 WidgetPreviewLoader mWidgetPreviewLoader;
219
Michael Jurkac402cd92013-05-20 15:49:32 +0200220 private boolean mInBulkBind;
221 private boolean mNeedToUpdatePageCountsAndInvalidateData;
222
Winson Chung785d2eb2011-04-14 16:08:02 -0700223 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
224 super(context, attrs);
225 mLayoutInflater = LayoutInflater.from(context);
226 mPackageManager = context.getPackageManager();
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200227 mApps = new ArrayList<AppInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700228 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400229 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700230 mCanvas = new Canvas();
231 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700232
233 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800234 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungc58497e2013-09-03 17:48:37 -0700235 LauncherAppState app = LauncherAppState.getInstance();
236 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chung4b576be2011-04-27 17:40:20 -0700237 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
238 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700239 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
240 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700241 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700243
Winson Chung1ed747a2011-05-03 16:18:34 -0700244 // The padding on the non-matched dimension for the default widget preview icons
245 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700246 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700247
248 // Unless otherwise specified this view is important for accessibility.
249 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
250 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
251 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700252 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700253 }
254
255 @Override
256 protected void init() {
257 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700258 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700259
260 Context context = getContext();
261 Resources r = context.getResources();
262 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
263 }
264
Winson Chungc58497e2013-09-03 17:48:37 -0700265 public void onFinishInflate() {
266 super.onFinishInflate();
267
268 LauncherAppState app = LauncherAppState.getInstance();
269 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
270 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
271 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
272 }
273
Winson Chung67ca7e42013-10-31 16:53:19 -0700274 void setAllAppsPadding(Rect r) {
275 mAllAppsPadding.set(r);
276 }
Adam Cohen4e243a22014-08-10 18:30:55 -0700277
Winson Chung67ca7e42013-10-31 16:53:19 -0700278 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700279 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700280 }
281
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100282 WidgetPreviewLoader getWidgetPreviewLoader() {
283 if (mWidgetPreviewLoader == null) {
284 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
285 }
286 return mWidgetPreviewLoader;
287 }
288
Winson Chung5afbf7b2011-07-25 11:53:08 -0700289 /** Returns the item index of the center item on this page so that we can restore to this
290 * item index when we rotate. */
291 private int getMiddleComponentIndexOnCurrentPage() {
292 int i = -1;
293 if (getPageCount() > 0) {
294 int currentPage = getCurrentPage();
Winson Chungc58497e2013-09-03 17:48:37 -0700295 if (mContentType == ContentType.Applications) {
296 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage);
297 ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets();
Winson Chung5afbf7b2011-07-25 11:53:08 -0700298 int numItemsPerPage = mCellCountX * mCellCountY;
299 int childCount = childrenLayout.getChildCount();
300 if (childCount > 0) {
301 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700302 }
Winson Chungc58497e2013-09-03 17:48:37 -0700303 } else if (mContentType == ContentType.Widgets) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700304 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700305 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700306 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
307 int childCount = layout.getChildCount();
308 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 i = numApps +
Winson Chungc58497e2013-09-03 17:48:37 -0700310 (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 }
Winson Chungc58497e2013-09-03 17:48:37 -0700312 } else {
313 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700314 }
315 }
316 return i;
317 }
318
319 /** Get the index of the item to restore to if we need to restore the current page. */
320 int getSaveInstanceStateIndex() {
321 if (mSaveInstanceStateItemIndex == -1) {
322 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
323 }
324 return mSaveInstanceStateItemIndex;
325 }
326
327 /** Returns the page in the current orientation which is expected to contain the specified
328 * item index. */
329 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 if (index < 0) return 0;
331
332 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mCellCountX * mCellCountY;
334 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungc58497e2013-09-03 17:48:37 -0700337 return (index - mApps.size()) / numItemsPerPage;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700338 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700339 }
340
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 /** Restores the page for an item at the specified index */
342 void restorePageForIndex(int index) {
343 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 }
346
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700347 private void updatePageCounts() {
348 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
349 (float) (mWidgetCountX * mWidgetCountY));
350 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
351 }
352
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 protected void onDataReady(int width, int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700354 // Now that the data is ready, we can calculate the content width, the number of cells to
355 // use for each page
Winson Chungc58497e2013-09-03 17:48:37 -0700356 LauncherAppState app = LauncherAppState.getInstance();
357 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungc58497e2013-09-03 17:48:37 -0700358 mCellCountX = (int) grid.allAppsNumCols;
359 mCellCountY = (int) grid.allAppsNumRows;
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700360 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700361
Winson Chungdb1138b2011-06-30 14:39:35 -0700362 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700363 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
364 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
365 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
366 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700367 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700368
Adam Cohen6c5891a2014-07-09 23:53:15 -0700369 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700370 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800371 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800372 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700373
Adam Cohena00673c2014-08-14 12:57:28 -0700374 protected void onLayout(boolean changed, int l, int t, int r, int b) {
375 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377 if (!isDataReady()) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800378 if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700379 setDataIsReady();
Adam Cohena00673c2014-08-14 12:57:28 -0700380 onDataReady(getMeasuredWidth(), getMeasuredHeight());
Winson Chungf0ea4d32011-06-06 14:27:16 -0700381 }
382 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700383 }
384
Michael Jurkac402cd92013-05-20 15:49:32 +0200385 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700386 LauncherAppState app = LauncherAppState.getInstance();
387 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
388
Winson Chung1ed747a2011-05-03 16:18:34 -0700389 // Get the list of widgets and shortcuts
390 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200391 for (Object o : widgetsAndShortcuts) {
392 if (o instanceof AppWidgetProviderInfo) {
393 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100394 if (!app.shouldShowAppOrWidgetProvider(widget.provider)) {
395 continue;
396 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200397 widget.label = widget.label.trim();
398 if (widget.minWidth > 0 && widget.minHeight > 0) {
399 // Ensure that all widgets we show can be added on a workspace of this size
400 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
401 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
402 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
403 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700404 if (minSpanX <= (int) grid.numColumns &&
405 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200406 mWidgets.add(widget);
407 } else {
408 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
409 widget.minWidth + ", " + widget.minHeight + ")");
410 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700411 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200412 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
413 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700414 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800415 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200416 // just add shortcuts
417 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800418 }
419 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200420 updatePageCountsAndInvalidateData();
421 }
422
423 public void setBulkBind(boolean bulkBind) {
424 if (bulkBind) {
425 mInBulkBind = true;
426 } else {
427 mInBulkBind = false;
428 if (mNeedToUpdatePageCountsAndInvalidateData) {
429 updatePageCountsAndInvalidateData();
430 }
431 }
432 }
433
434 private void updatePageCountsAndInvalidateData() {
435 if (mInBulkBind) {
436 mNeedToUpdatePageCountsAndInvalidateData = true;
437 } else {
438 updatePageCounts();
439 invalidateOnDataChange();
440 mNeedToUpdatePageCountsAndInvalidateData = false;
441 }
Winson Chung4b576be2011-04-27 17:40:20 -0700442 }
443
444 @Override
445 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700446 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700447 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700448 mLauncher.getWorkspace().isSwitchingState()) return;
449
Winson Chung4b576be2011-04-27 17:40:20 -0700450 if (v instanceof PagedViewIcon) {
451 // Animate some feedback to the click
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200452 final AppInfo appInfo = (AppInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800453
454 // Lock the drawable state to pressed until we return to Launcher
455 if (mPressedIcon != null) {
456 mPressedIcon.lockDrawableState();
457 }
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -0700458 mLauncher.onClickPagedViewIcon(v, appInfo);
Winson Chung4b576be2011-04-27 17:40:20 -0700459 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700460 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700461 if (mWidgetInstructionToast != null) {
462 mWidgetInstructionToast.cancel();
463 }
464 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
465 Toast.LENGTH_SHORT);
466 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700467
Winson Chungd2e87b32011-06-02 10:53:07 -0700468 // Create a little animation to show that the widget can move
469 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
470 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700471 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
472 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700473 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700474 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700475 tydAnim.setDuration(100);
476 bounce.play(tyuAnim).before(tydAnim);
477 bounce.setInterpolator(new AccelerateInterpolator());
478 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700479 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700480 }
481
Winson Chungc6f10b92011-11-14 11:39:07 -0800482 public boolean onKey(View v, int keyCode, KeyEvent event) {
483 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
484 }
485
Winson Chung785d2eb2011-04-14 16:08:02 -0700486 /*
487 * PagedViewWithDraggableItems implementation
488 */
489 @Override
490 protected void determineDraggingStart(android.view.MotionEvent ev) {
491 // Disable dragging by pulling an app down for now.
492 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700493
Winson Chung4b576be2011-04-27 17:40:20 -0700494 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700495 mLauncher.getWorkspace().onDragStartedWithItem(v);
496 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700497 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700498
Sunny Goyalff572272014-07-23 13:58:07 -0700499 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700500 Bundle options = null;
501 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700502 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
503 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700504 info.componentName, null);
505
Sunny Goyalff572272014-07-23 13:58:07 -0700506 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700507 int xPaddingDips = (int) ((padding.left + padding.right) / density);
508 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
509
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700510 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700511 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700512 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700513 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700514 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700515 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700516 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700517 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700518 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700519 }
520 return options;
521 }
522
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700523 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800524 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700525 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
526
Adam Cohened66b2b2012-01-23 17:28:51 -0800527 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700528 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800529 return;
530 }
531
Adam Cohen21a170b2012-05-30 15:17:06 -0700532 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 mBindWidgetRunnable = new Runnable() {
534 @Override
535 public void run() {
536 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700537 // Options will be null for platforms with JB or lower, so this serves as an
538 // SDK level check.
539 if (options == null) {
540 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
541 mWidgetLoadingId, info.componentName)) {
542 mWidgetCleanupState = WIDGET_BOUND;
543 }
544 } else {
545 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
546 mWidgetLoadingId, info.componentName, options)) {
547 mWidgetCleanupState = WIDGET_BOUND;
548 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700549 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 }
551 };
552 post(mBindWidgetRunnable);
553
554 mInflateWidgetRunnable = new Runnable() {
555 @Override
556 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700557 if (mWidgetCleanupState != WIDGET_BOUND) {
558 return;
559 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 AppWidgetHostView hostView = mLauncher.
561 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800562 info.boundWidget = hostView;
563 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800564 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700565 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
566 info.spanY, info, false);
567
568 // We want the first widget layout to be the correct size. This will be important
569 // for width size reporting to the AppWidgetManager.
570 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
571 unScaledSize[1]);
572 lp.x = lp.y = 0;
573 lp.customPosition = true;
574 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800575 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 }
577 };
578 post(mInflateWidgetRunnable);
579 }
580
581 @Override
582 public void onShortPress(View v) {
583 // We are anticipating a long press, and we use this time to load bind and instantiate
584 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700585 if (mCreateWidgetInfo != null) {
586 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
587 cleanupWidgetPreloading(false);
588 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800589 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700590 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 }
592
Adam Cohen0e56cc92012-05-11 15:57:05 -0700593 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
594 if (!widgetWasAdded) {
595 // If the widget was not added, we may need to do further cleanup.
596 PendingAddWidgetInfo info = mCreateWidgetInfo;
597 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700598
599 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700600 // We never did any preloading, so just remove pending callbacks to do so
601 removeCallbacks(mBindWidgetRunnable);
602 removeCallbacks(mInflateWidgetRunnable);
603 } else if (mWidgetCleanupState == WIDGET_BOUND) {
604 // Delete the widget id which was allocated
605 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700606 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
607 }
608
609 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700610 removeCallbacks(mInflateWidgetRunnable);
611 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700612 // Delete the widget id which was allocated
613 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700614 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
615 }
616
Adam Cohen0e56cc92012-05-11 15:57:05 -0700617 // The widget was inflated and added to the DragLayer -- remove it.
618 AppWidgetHostView widget = info.boundWidget;
619 mLauncher.getDragLayer().removeView(widget);
620 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 }
622 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
623 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700624 mCreateWidgetInfo = null;
625 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800626 }
627
Adam Cohen7a326642012-02-22 12:03:22 -0800628 @Override
629 public void cleanUpShortPress(View v) {
630 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700631 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800632 }
633 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800634
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700635 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800636 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700637 // Get the widget preview as the drag representation
638 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700639 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700640
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700641 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
642 // we abort the drag.
643 if (image.getDrawable() == null) {
644 mDraggingWidget = false;
645 return false;
646 }
647
Winson Chung4b576be2011-04-27 17:40:20 -0700648 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800649 Bitmap preview;
650 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800651 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100652 Point previewPadding = null;
653
Winson Chung1ed747a2011-05-03 16:18:34 -0700654 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700655 // This can happen in some weird cases involving multi-touch. We can't start dragging
656 // the widget if this is null, so we break out.
657 if (mCreateWidgetInfo == null) {
658 return false;
659 }
660
Adam Cohen1b36dc32012-02-13 19:27:37 -0800661 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
662 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700663 int spanX = createItemInfo.spanX;
664 int spanY = createItemInfo.spanY;
665 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
666 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700667
Winson Chung72d59842012-02-22 13:51:36 -0800668 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
669 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700670 int maxWidth, maxHeight;
671 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
672 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800673
Michael Jurka05713af2013-01-23 12:39:24 +0100674 int[] previewSizeBeforeScale = new int[1];
675
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100676 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName,
Michael Jurka05713af2013-01-23 12:39:24 +0100677 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
678 maxWidth, maxHeight, null, previewSizeBeforeScale);
679
680 // Compare the size of the drag preview to the preview in the AppsCustomize tray
681 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100682 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100683 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
684
685 // The bitmap in the AppsCustomize tray is always the the same size, so there
686 // might be extra pixels around the preview itself - this accounts for that
687 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
688 int padding =
689 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
690 previewPadding = new Point(padding, 0);
691 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700692 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700693 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
694 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
695 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
696 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
697
Winson Chung1120e032011-11-22 16:11:31 -0800698 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700699 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100700 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700701 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700702 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700703 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700704 createItemInfo.spanX = createItemInfo.spanY = 1;
705 }
Winson Chung4b576be2011-04-27 17:40:20 -0700706
Michael Jurka8c3339b2012-06-14 16:18:21 -0700707 // Don't clip alpha values for the drag outline if we're using the default widget preview
708 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
709 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700710
Winson Chung1120e032011-11-22 16:11:31 -0800711 // Save the preview for the outline generation, then dim the preview
712 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
713 false);
Winson Chung1120e032011-11-22 16:11:31 -0800714
Winson Chung4b576be2011-04-27 17:40:20 -0700715 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700716 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700717 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800718 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100719 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800720 outline.recycle();
721 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700722 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700723 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800724
Winson Chung4b576be2011-04-27 17:40:20 -0700725 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800726 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700727 if (!super.beginDragging(v)) return false;
728
729 if (v instanceof PagedViewIcon) {
730 beginDraggingApplication(v);
731 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700732 if (!beginDraggingWidget(v)) {
733 return false;
734 }
Winson Chung4b576be2011-04-27 17:40:20 -0700735 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800736
737 // We delay entering spring-loaded mode slightly to make sure the UI
738 // thready is free of any work.
739 postDelayed(new Runnable() {
740 @Override
741 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800742 // We don't enter spring-loaded mode if the drag has been cancelled
743 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800744 // Reset the alpha on the dragged icon before we drag
745 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800746
Adam Cohen1b36dc32012-02-13 19:27:37 -0800747 // Go into spring loaded mode (must happen before we startDrag())
748 mLauncher.enterSpringLoadedDragMode();
749 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 }
Winson Chung72d59842012-02-22 13:51:36 -0800751 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800752
Winson Chung785d2eb2011-04-14 16:08:02 -0700753 return true;
754 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800755
Winson Chunga48487a2012-03-20 16:19:37 -0700756 /**
757 * Clean up after dragging.
758 *
759 * @param target where the item was dragged to (can be null if the item was flung)
760 */
761 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700762 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800763 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700764 // Exit spring loaded mode if we have not successfully dropped or have not handled the
765 // drop in Workspace
Adam Cohen689ff162014-05-08 17:27:56 -0700766 mLauncher.exitSpringLoadedDragMode();
767 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700768 } else {
769 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700770 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700771 }
772
Winson Chung785d2eb2011-04-14 16:08:02 -0700773 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700774 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700775 if (getChildCount() > 0) {
776 return getChildAt(0);
777 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700778 return null;
779 }
780
781 @Override
782 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700783 mInTransition = true;
784 if (toWorkspace) {
785 cancelAllTasks();
786 }
787 }
788
789 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700790 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700791 }
792
793 @Override
794 public void onLauncherTransitionStep(Launcher l, float t) {
795 }
796
797 @Override
798 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
799 mInTransition = false;
800 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700801 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700802 }
803 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700804 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
805 r.run();
806 }
807 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700808 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700809 }
810
811 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700812 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
813 boolean success) {
814 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
815 if (isFlingToDelete) return;
816
817 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700818
819 // Display an error message if the drag failed due to there not being enough space on the
820 // target layout we were dropping on.
821 if (!success) {
822 boolean showOutOfSpaceMessage = false;
823 if (target instanceof Workspace) {
824 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
825 Workspace workspace = (Workspace) target;
826 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700827 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700828 if (layout != null) {
829 layout.calculateSpans(itemInfo);
830 showOutOfSpaceMessage =
831 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
832 }
833 }
Winson Chungfc79c802011-05-02 13:35:34 -0700834 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700835 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700836 }
Adam Cohen7a326642012-02-22 12:03:22 -0800837
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800838 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700839 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700840 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800841 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700842 }
843
Winson Chunga48487a2012-03-20 16:19:37 -0700844 @Override
845 public void onFlingToDeleteCompleted() {
846 // We just dismiss the drag when we fling, so cleanup here
847 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700848 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700849 mDraggingWidget = false;
850 }
851
852 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800853 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700854 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800855 }
856
Winson Chung7f0acdd2011-09-19 18:34:19 -0700857 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000858 public boolean supportsAppInfoDropTarget() {
859 return true;
860 }
861
862 @Override
863 public boolean supportsDeleteDropTarget() {
864 return false;
865 }
866
867 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800868 public float getIntrinsicIconScaleFactor() {
869 LauncherAppState app = LauncherAppState.getInstance();
870 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
871 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
872 }
873
874 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700875 protected void onDetachedFromWindow() {
876 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700877 cancelAllTasks();
878 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700879
Michael Jurkae326f182011-11-21 14:05:46 -0800880 public void clearAllWidgetPages() {
881 cancelAllTasks();
882 int count = getChildCount();
883 for (int i = 0; i < count; i++) {
884 View v = getPageAt(i);
885 if (v instanceof PagedViewGridLayout) {
886 ((PagedViewGridLayout) v).removeAllViewsOnPage();
887 mDirtyPageContent.set(i, true);
888 }
889 }
890 }
891
Adam Cohen0cd3b642011-10-14 14:58:00 -0700892 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700893 // Clean up all the async tasks
894 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
895 while (iter.hasNext()) {
896 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
897 task.cancel(false);
898 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700899 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700900
901 // We've already preallocated the views for the data to load into, so clear them as well
902 View v = getPageAt(task.page);
903 if (v instanceof PagedViewGridLayout) {
904 ((PagedViewGridLayout) v).removeAllViewsOnPage();
905 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700906 }
Winson Chung83687b12012-04-25 16:01:01 -0700907 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700908 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700909 }
910
Winson Chung785d2eb2011-04-14 16:08:02 -0700911 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200912 // Widgets appear to be cleared every time you leave, always force invalidate for them
913 if (mContentType != type || type == ContentType.Widgets) {
914 int page = (mContentType != type) ? 0 : getCurrentPage();
915 mContentType = type;
916 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700917 }
Winson Chungc58497e2013-09-03 17:48:37 -0700918 }
919
920 public ContentType getContentType() {
921 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700922 }
923
Adam Cohen0cd3b642011-10-14 14:58:00 -0700924 protected void snapToPage(int whichPage, int delta, int duration) {
925 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800926
927 // Update the thread priorities given the direction lookahead
928 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
929 while (iter.hasNext()) {
930 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700931 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800932 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
933 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
934 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
935 } else {
936 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
937 }
938 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700939 }
940
Winson Chung785d2eb2011-04-14 16:08:02 -0700941 /*
942 * Apps PagedView implementation
943 */
Winson Chung63257c12011-05-05 17:06:13 -0700944 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
945 int childCount = layout.getChildCount();
946 for (int i = 0; i < childCount; ++i) {
947 layout.getChildAt(i).setVisibility(visibility);
948 }
949 }
Winson Chungc58497e2013-09-03 17:48:37 -0700950 private void setupPage(AppsCustomizeCellLayout layout) {
951 layout.setGridSize(mCellCountX, mCellCountY);
Winson Chung785d2eb2011-04-14 16:08:02 -0700952
Winson Chung63257c12011-05-05 17:06:13 -0700953 // Note: We force a measure here to get around the fact that when we do layout calculations
954 // immediately after syncing, we don't have a proper width. That said, we already know the
955 // expected page width, so we can actually optimize by hiding all the TextView-based
956 // children that are expensive to measure, and let that happen naturally later.
957 setVisibilityOnChildren(layout, View.GONE);
Winson Chungc58497e2013-09-03 17:48:37 -0700958 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
959 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700960 layout.measure(widthSpec, heightSpec);
Adam Cohen96bb7982014-07-07 11:58:56 -0700961
Adam Cohen4e243a22014-08-10 18:30:55 -0700962 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700963 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700964 bg.setAlpha(mPageBackgroundsVisible ? 255: 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700965 layout.setBackground(bg);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700966 }
967
968 setVisibilityOnChildren(layout, View.VISIBLE);
969 }
970
971 public void setPageBackgroundsVisible(boolean visible) {
972 mPageBackgroundsVisible = visible;
973 int childCount = getChildCount();
974 for (int i = 0; i < childCount; ++i) {
975 Drawable bg = getChildAt(i).getBackground();
976 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700977 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700978 }
979 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700980 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700981
Winson Chungf314b0e2011-08-16 11:54:27 -0700982 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -0700984 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 int numCells = mCellCountX * mCellCountY;
986 int startIndex = page * numCells;
987 int endIndex = Math.min(startIndex + numCells, mApps.size());
Winson Chungc58497e2013-09-03 17:48:37 -0700988 AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700989
Winson Chung785d2eb2011-04-14 16:08:02 -0700990 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700991 ArrayList<Object> items = new ArrayList<Object>();
992 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700993 for (int i = startIndex; i < endIndex; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200994 AppInfo info = mApps.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
996 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800997 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700998 icon.setOnClickListener(this);
999 icon.setOnLongClickListener(this);
1000 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001001 icon.setOnKeyListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001002 icon.setOnFocusChangeListener(layout.mFocusHandlerView);
Winson Chung785d2eb2011-04-14 16:08:02 -07001003
1004 int index = i - startIndex;
1005 int x = index % mCellCountX;
1006 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001007 if (isRtl) {
1008 x = mCellCountX - x - 1;
1009 }
Winson Chungc58497e2013-09-03 17:48:37 -07001010 layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false);
Winson Chungb44b5242011-06-13 11:32:14 -07001011
1012 items.add(info);
1013 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001014 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001015
Michael Jurka47639b92013-01-14 12:42:27 +01001016 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001017 }
Winson Chungb44b5242011-06-13 11:32:14 -07001018
1019 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001020 * A helper to return the priority for loading of the specified widget page.
1021 */
1022 private int getWidgetPageLoadPriority(int page) {
1023 // If we are snapping to another page, use that index as the target page index
1024 int toPage = mCurrentPage;
1025 if (mNextPage > -1) {
1026 toPage = mNextPage;
1027 }
1028
1029 // We use the distance from the target page as an initial guess of priority, but if there
1030 // are no pages of higher priority than the page specified, then bump up the priority of
1031 // the specified page.
1032 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1033 int minPageDiff = Integer.MAX_VALUE;
1034 while (iter.hasNext()) {
1035 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001036 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001037 }
1038
1039 int rawPageDiff = Math.abs(page - toPage);
1040 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1041 }
1042 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001043 * Return the appropriate thread priority for loading for a given page (we give the current
1044 * page much higher priority)
1045 */
1046 private int getThreadPriorityForPage(int page) {
1047 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001048 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001049 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001050 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001051 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001052 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001053 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001054 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001055 }
1056 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001057 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001058 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001059 return Math.max(0, pageDiff * sPageSleepDelay);
1060 }
Winson Chungb44b5242011-06-13 11:32:14 -07001061 /**
1062 * Creates and executes a new AsyncTask to load a page of widget previews.
1063 */
1064 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001065 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001066
Winson Chungb44b5242011-06-13 11:32:14 -07001067 // Prune all tasks that are no longer needed
1068 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1069 while (iter.hasNext()) {
1070 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001071 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001072 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1073 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001074 task.cancel(false);
1075 iter.remove();
1076 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001077 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001078 }
1079 }
1080
Winson Chungf314b0e2011-08-16 11:54:27 -07001081 // 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 -07001082 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001083 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001084 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001085 @Override
1086 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001087 try {
Winson Chung09945932011-09-20 14:22:40 -07001088 try {
1089 Thread.sleep(sleepMs);
1090 } catch (Exception e) {}
1091 loadWidgetPreviewsInBackground(task, data);
1092 } finally {
1093 if (task.isCancelled()) {
1094 data.cleanup(true);
1095 }
1096 }
Winson Chungb44b5242011-06-13 11:32:14 -07001097 }
1098 },
1099 new AsyncTaskCallback() {
1100 @Override
1101 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001102 mRunningTasks.remove(task);
1103 if (task.isCancelled()) return;
1104 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -07001105 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001106 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001107 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -07001108
1109 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001110 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001111 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001112 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001113 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1114 mRunningTasks.add(t);
1115 }
Winson Chungb44b5242011-06-13 11:32:14 -07001116
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 /*
1118 * Widgets PagedView implementation
1119 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001120 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -07001121 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001122 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -07001123 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
1124 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -07001125
1126 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
1127 if (bg != null) {
1128 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
1129 layout.setBackground(bg);
1130 }
Winson Chung63257c12011-05-05 17:06:13 -07001131 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001132 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001133
Michael Jurka038f9d82011-11-03 13:50:45 -07001134 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001135 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001136
Adam Cohen4e243a22014-08-10 18:30:55 -07001137 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
1138
Winson Chungd2945262011-06-24 15:22:14 -07001139 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001140 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -07001141 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -07001142 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -07001143 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1144
Adam Cohen4e243a22014-08-10 18:30:55 -07001145 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001146
Winson Chunge4a647f2011-09-30 14:41:25 -07001147 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001148 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001149 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1150 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001151 }
1152
Winson Chunge4a647f2011-09-30 14:41:25 -07001153 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001154 layout.setColumnCount(layout.getCellCountX());
1155 for (int i = 0; i < items.size(); ++i) {
1156 Object rawInfo = items.get(i);
1157 PendingAddItemInfo createItemInfo = null;
1158 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1159 R.layout.apps_customize_widget, layout, false);
1160 if (rawInfo instanceof AppWidgetProviderInfo) {
1161 // Fill in the widget information
1162 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1163 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001164
1165 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001166 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001167 createItemInfo.spanX = spanXY[0];
1168 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001169 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001170 createItemInfo.minSpanX = minSpanXY[0];
1171 createItemInfo.minSpanY = minSpanXY[1];
1172
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001173 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001174 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001175 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001176 } else if (rawInfo instanceof ResolveInfo) {
1177 // Fill in the shortcuts information
1178 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001179 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001180 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1181 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1182 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001183 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001184 widget.setTag(createItemInfo);
1185 }
1186 widget.setOnClickListener(this);
1187 widget.setOnLongClickListener(this);
1188 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001189 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001190
1191 // Layout each widget
1192 int ix = i % mWidgetCountX;
1193 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001194
1195 if (ix > 0) {
1196 View border = widget.findViewById(R.id.left_border);
1197 border.setVisibility(View.VISIBLE);
1198 }
1199 if (ix < mWidgetCountX - 1) {
1200 View border = widget.findViewById(R.id.right_border);
1201 border.setVisibility(View.VISIBLE);
1202 }
1203
Winson Chunge4a647f2011-09-30 14:41:25 -07001204 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001205 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001206 GridLayout.spec(ix, GridLayout.TOP));
1207 lp.width = cellWidth;
1208 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001209 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001210 layout.addView(widget, lp);
1211 }
1212
Michael Jurka038f9d82011-11-03 13:50:45 -07001213 // wait until a call on onLayout to start loading, because
1214 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1215 // TODO: can we do a measure/layout immediately?
1216 layout.setOnLayoutListener(new Runnable() {
1217 public void run() {
1218 // Load the widget previews
1219 int maxPreviewWidth = cellWidth;
1220 int maxPreviewHeight = cellHeight;
1221 if (layout.getChildCount() > 0) {
1222 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1223 int[] maxSize = w.getPreviewSize();
1224 maxPreviewWidth = maxSize[0];
1225 maxPreviewHeight = maxSize[1];
1226 }
Michael Jurka05713af2013-01-23 12:39:24 +01001227
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001228 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001229 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001230 if (immediate) {
1231 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001232 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001233 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001234 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001235 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001236 if (mInTransition) {
1237 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1238 } else {
1239 prepareLoadWidgetPreviewsTask(page, items,
1240 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1241 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001242 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001243 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 }
1245 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001246 }
1247 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1248 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001249 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1250 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001251 if (task != null) {
1252 // Ensure that this task starts running at the correct priority
1253 task.syncThreadPriority();
1254 }
1255
1256 // Load each of the widget/shortcut previews
1257 ArrayList<Object> items = data.items;
1258 ArrayList<Bitmap> images = data.generatedImages;
1259 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001260 for (int i = 0; i < count; ++i) {
1261 if (task != null) {
1262 // Ensure we haven't been cancelled yet
1263 if (task.isCancelled()) break;
1264 // Before work on each item, ensure that this task is running at the correct
1265 // priority
1266 task.syncThreadPriority();
1267 }
1268
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001269 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001270 }
Winson Chungb44b5242011-06-13 11:32:14 -07001271 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001272
Winson Chung7bb37522013-10-28 11:07:57 -07001273 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1274 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001275 mDeferredSyncWidgetPageItems.add(data);
1276 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001277 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001278 try {
1279 int page = data.page;
1280 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001281
Michael Jurka39e5d172012-03-12 18:36:12 -07001282 ArrayList<Object> items = data.items;
1283 int count = items.size();
1284 for (int i = 0; i < count; ++i) {
1285 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1286 if (widget != null) {
1287 Bitmap preview = data.generatedImages.get(i);
1288 widget.applyPreview(new FastBitmapDrawable(preview), i);
1289 }
1290 }
Winson Chung68e4c642011-11-10 15:48:25 -08001291
Michael Jurka47639b92013-01-14 12:42:27 +01001292 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001293
1294 // Update all thread priorities
1295 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1296 while (iter.hasNext()) {
1297 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1298 int pageIndex = task.page;
1299 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1300 }
1301 } finally {
1302 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001303 }
Winson Chungb44b5242011-06-13 11:32:14 -07001304 }
Winson Chung46af2e82011-05-09 16:00:53 -07001305
Winson Chung785d2eb2011-04-14 16:08:02 -07001306 @Override
1307 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001308 disablePagedViewAnimations();
1309
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001311 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001312
Adam Cohen0cd3b642011-10-14 14:58:00 -07001313 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -07001314 if (mContentType == ContentType.Applications) {
1315 for (int i = 0; i < mNumAppsPages; ++i) {
1316 AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context);
1317 setupPage(layout);
1318 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1319 LayoutParams.MATCH_PARENT));
1320 }
1321 } else if (mContentType == ContentType.Widgets) {
1322 for (int j = 0; j < mNumWidgetPages; ++j) {
1323 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1324 mWidgetCountY);
1325 setupPage(layout);
1326 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1327 LayoutParams.MATCH_PARENT));
1328 }
1329 } else {
1330 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001331 }
1332
Winson Chungc58497e2013-09-03 17:48:37 -07001333 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001334 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001335
Winson Chung785d2eb2011-04-14 16:08:02 -07001336 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001337 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001338 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001339 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001340 } else {
1341 syncAppsPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 }
1343 }
1344
Adam Cohen22f823d2011-09-01 17:22:18 -07001345 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1346 // it is in the z-order. This is important to insure touch events are handled correctly.
1347 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001348 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001349 }
1350
Adam Cohenae4f1552011-10-20 00:15:42 -07001351 @Override
1352 protected int indexToPage(int index) {
1353 return getChildCount() - index - 1;
1354 }
1355
Adam Cohen22f823d2011-09-01 17:22:18 -07001356 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1357 @Override
1358 protected void screenScrolled(int screenCenter) {
1359 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001360 enableHwLayersOnVisiblePages();
1361 }
1362
1363 private void enableHwLayersOnVisiblePages() {
1364 final int screenCount = getChildCount();
1365
1366 getVisiblePages(mTempVisiblePagesRange);
1367 int leftScreen = mTempVisiblePagesRange[0];
1368 int rightScreen = mTempVisiblePagesRange[1];
1369 int forceDrawScreen = -1;
1370 if (leftScreen == rightScreen) {
1371 // make sure we're caching at least two pages always
1372 if (rightScreen < screenCount - 1) {
1373 rightScreen++;
1374 forceDrawScreen = rightScreen;
1375 } else if (leftScreen > 0) {
1376 leftScreen--;
1377 forceDrawScreen = leftScreen;
1378 }
1379 } else {
1380 forceDrawScreen = leftScreen + 1;
1381 }
1382
1383 for (int i = 0; i < screenCount; i++) {
1384 final View layout = (View) getPageAt(i);
1385 if (!(leftScreen <= i && i <= rightScreen &&
1386 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1387 layout.setLayerType(LAYER_TYPE_NONE, null);
1388 }
1389 }
1390
Michael Jurka47639b92013-01-14 12:42:27 +01001391 for (int i = 0; i < screenCount; i++) {
1392 final View layout = (View) getPageAt(i);
1393
1394 if (leftScreen <= i && i <= rightScreen &&
1395 (i == forceDrawScreen || shouldDrawChild(layout))) {
1396 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1397 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1398 }
1399 }
1400 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001401 }
1402
1403 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001404 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001405 }
1406
Winson Chung785d2eb2011-04-14 16:08:02 -07001407 /**
1408 * Used by the parent to get the content width to set the tab bar to
1409 * @return
1410 */
1411 public int getPageContentWidth() {
1412 return mContentWidth;
1413 }
1414
Winson Chungb26f3d62011-06-02 10:49:29 -07001415 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001416 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001417 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001418 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001419 // We reset the save index when we change pages so that it will be recalculated on next
1420 // rotation
1421 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001422 }
1423
Winson Chung785d2eb2011-04-14 16:08:02 -07001424 /*
1425 * AllAppsView implementation
1426 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 public void setup(Launcher launcher, DragController dragController) {
1428 mLauncher = launcher;
1429 mDragController = dragController;
1430 }
Winson Chung9802ac92012-06-08 16:01:58 -07001431
1432 /**
1433 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1434 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1435 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1436 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1437 */
1438 private void invalidateOnDataChange() {
1439 if (!isDataReady()) {
1440 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1441 // request a layout to trigger the page data when ready.
1442 requestLayout();
1443 } else {
1444 cancelAllTasks();
1445 invalidatePageData();
1446 }
1447 }
1448
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001449 public void setApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001450 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001451 mApps = list;
1452 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1453 updatePageCountsAndInvalidateData();
1454 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001455 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001456 private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 // We add it in place, in alphabetical order
1458 int count = list.size();
1459 for (int i = 0; i < count; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001460 AppInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001461 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001462 if (index < 0) {
1463 mApps.add(-(index + 1), info);
1464 }
1465 }
1466 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001467 public void addApps(ArrayList<AppInfo> list) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001468 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001469 addAppsWithoutInvalidate(list);
1470 updatePageCountsAndInvalidateData();
Adam Cohen947dc542013-06-06 22:43:33 -07001471 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001472 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001473 private int findAppByComponent(List<AppInfo> list, AppInfo item) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001474 ComponentName removeComponent = item.intent.getComponent();
1475 int length = list.size();
1476 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001477 AppInfo info = list.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001478 if (info.user.equals(item.user)
1479 && info.intent.getComponent().equals(removeComponent)) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001480 return i;
1481 }
1482 }
1483 return -1;
1484 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001485 private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001486 // loop through all the apps and remove apps that have the same component
1487 int length = list.size();
1488 for (int i = 0; i < length; ++i) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001489 AppInfo info = list.get(i);
Winson Chung785d2eb2011-04-14 16:08:02 -07001490 int removeIndex = findAppByComponent(mApps, info);
1491 if (removeIndex > -1) {
1492 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001493 }
1494 }
1495 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001496 public void removeApps(ArrayList<AppInfo> appInfos) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001497 if (!LauncherAppState.isDisableAllApps()) {
Winson Chung64359a52013-07-08 17:17:08 -07001498 removeAppsWithoutInvalidate(appInfos);
1499 updatePageCountsAndInvalidateData();
1500 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001501 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001502 public void updateApps(ArrayList<AppInfo> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001503 // We remove and re-add the updated applications list because it's properties may have
1504 // changed (ie. the title), and this will ensure that the items will be in their proper
1505 // place in the list.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001506 if (!LauncherAppState.isDisableAllApps()) {
Adam Cohen947dc542013-06-06 22:43:33 -07001507 removeAppsWithoutInvalidate(list);
1508 addAppsWithoutInvalidate(list);
1509 updatePageCountsAndInvalidateData();
1510 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001511 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001512
Winson Chung785d2eb2011-04-14 16:08:02 -07001513 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001514 // If we have reset, then we should not continue to restore the previous state
1515 mSaveInstanceStateItemIndex = -1;
1516
Adam Cohen6c5891a2014-07-09 23:53:15 -07001517 if (mContentType != ContentType.Applications) {
1518 setContentType(ContentType.Applications);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001519 }
Winson Chung649668f2012-01-10 13:07:16 -08001520
Adam Cohenb64d36e2011-10-17 21:48:02 -07001521 if (mCurrentPage != 0) {
1522 invalidatePageData(0);
1523 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001524 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001525
1526 private AppsCustomizeTabHost getTabHost() {
1527 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1528 }
1529
Winson Chung785d2eb2011-04-14 16:08:02 -07001530 public void dumpState() {
1531 // TODO: Dump information related to current list of Applications, Widgets, etc.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001532 AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
Adam Cohen0e56cc92012-05-11 15:57:05 -07001533 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001534 }
Adam Cohen4e844012011-11-09 13:48:04 -08001535
Winson Chung785d2eb2011-04-14 16:08:02 -07001536 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001537 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001538 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001539 for (Object i: list) {
1540 if (i instanceof AppWidgetProviderInfo) {
1541 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1542 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1543 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1544 + " initialLayout=" + info.initialLayout
1545 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1546 } else if (i instanceof ResolveInfo) {
1547 ResolveInfo info = (ResolveInfo) i;
1548 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1549 + info.icon);
1550 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001551 }
1552 }
Adam Cohen4e844012011-11-09 13:48:04 -08001553
Winson Chung785d2eb2011-04-14 16:08:02 -07001554 public void surrender() {
1555 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1556 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001557
1558 // Stop all background tasks
1559 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 }
Winson Chung007c6982011-06-14 13:27:53 -07001561
Winson Chunge4e50662012-01-23 14:45:13 -08001562 @Override
1563 public void iconPressed(PagedViewIcon icon) {
1564 // Reset the previously pressed icon and store a reference to the pressed icon so that
1565 // we can reset it on return to Launcher (in Launcher.onResume())
1566 if (mPressedIcon != null) {
1567 mPressedIcon.resetDrawableState();
1568 }
1569 mPressedIcon = icon;
1570 }
1571
1572 public void resetDrawableState() {
1573 if (mPressedIcon != null) {
1574 mPressedIcon.resetDrawableState();
1575 mPressedIcon = null;
1576 }
1577 }
Winson Chung68e4c642011-11-10 15:48:25 -08001578
Winson Chungb44b5242011-06-13 11:32:14 -07001579 /*
1580 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1581 * widget previews in the background with the AsyncTasks.
1582 */
Winson Chung68e4c642011-11-10 15:48:25 -08001583 final static int sLookBehindPageCount = 2;
1584 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001585 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001586 final int count = getChildCount();
1587 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1588 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1589 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001590 }
1591 protected int getAssociatedUpperPageBound(int page) {
1592 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001593 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1594 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1595 count - 1);
1596 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001597 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001598
Winson Chung6a0f57d2011-06-29 20:10:49 -07001599 protected String getCurrentPageDescription() {
1600 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1601 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001602 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001603
1604 if (mContentType == ContentType.Applications) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001605 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001606 count = mNumAppsPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001607 } else if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001608 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001609 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001610 } else {
1611 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001612 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001613
Michael Jurka8b805b12012-04-18 14:23:14 -07001614 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001615 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001616}