blob: bf368125f1a67809b7ba2c1f75c52db1d73abbe6 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Adam Cohened66b2b2012-01-23 17:28:51 -080019import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
22import android.content.ComponentName;
23import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Michael Jurka05713af2013-01-23 12:39:24 +010029import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.graphics.Rect;
31import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070032import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070033import android.os.Build;
34import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070035import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.util.AttributeSet;
37import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070038import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.view.LayoutInflater;
40import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070041import android.view.ViewGroup;
Winson Chungfd3385f2011-06-15 19:51:24 -070042import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070044import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070045
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal290800b2015-03-05 11:33:33 -080047import com.android.launcher3.FocusHelper.PagedViewKeyListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Adam Cohenc0dcf592011-06-01 15:30:43 -070049
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.Iterator;
53import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070054
Winson Chungb44b5242011-06-13 11:32:14 -070055/**
56 * A simple callback interface which also provides the results of the task.
57 */
58interface AsyncTaskCallback {
59 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
60}
Winson Chung4e076542011-06-23 13:04:10 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * The data needed to perform either of the custom AsyncTasks.
64 */
65class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070066 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080067 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070068 }
69
Michael Jurka038f9d82011-11-03 13:50:45 -070070 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010071 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070072 page = p;
73 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070074 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070075 maxImageWidth = cw;
76 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070077 doInBackgroundCallback = bgR;
78 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010079 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070080 }
Winson Chung09945932011-09-20 14:22:40 -070081 void cleanup(boolean cancelled) {
82 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070083 if (generatedImages != null) {
84 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010085 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010086 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070087 }
88 }
89 generatedImages.clear();
90 }
91 }
Winson Chungb44b5242011-06-13 11:32:14 -070092 int page;
93 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070094 ArrayList<Bitmap> sourceImages;
95 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -070096 int maxImageWidth;
97 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -070098 AsyncTaskCallback doInBackgroundCallback;
99 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100100 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700101}
Winson Chung4e076542011-06-23 13:04:10 -0700102
Winson Chungb44b5242011-06-13 11:32:14 -0700103/**
104 * A generic template for an async task used in AppsCustomize.
105 */
106class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700107 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700108 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700109 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700110 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700111 }
112 @Override
113 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
114 if (params.length != 1) return null;
115 // Load each of the widget previews in the background
116 params[0].doInBackgroundCallback.run(this, params[0]);
117 return params[0];
118 }
119 @Override
120 protected void onPostExecute(AsyncTaskPageData result) {
121 // All the widget previews are loaded, so we can just callback to inflate the page
122 result.postExecuteCallback.run(this, result);
123 }
124
125 void setThreadPriority(int p) {
126 threadPriority = p;
127 }
128 void syncThreadPriority() {
129 Process.setThreadPriority(threadPriority);
130 }
131
132 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700133 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700134 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700135 int threadPriority;
136}
Winson Chungb44b5242011-06-13 11:32:14 -0700137
138/**
139 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
140 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700141public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Sunny Goyal290800b2015-03-05 11:33:33 -0800142 View.OnClickListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -0700143 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700144 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700145
Sunny Goyalff572272014-07-23 13:58:07 -0700146 private static Rect sTmpRect = new Rect();
147
Winson Chung785d2eb2011-04-14 16:08:02 -0700148 /**
149 * The different content types that this paged view can show.
150 */
151 public enum ContentType {
Winson Chung6a26e5b2011-05-26 14:36:06 -0700152 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700153 }
Winson Chungb745afb2015-03-02 11:51:23 -0800154 private ContentType mContentType = ContentType.Widgets;
Winson Chung785d2eb2011-04-14 16:08:02 -0700155
156 // Refs
157 private Launcher mLauncher;
158 private DragController mDragController;
159 private final LayoutInflater mLayoutInflater;
160 private final PackageManager mPackageManager;
161
Winson Chung5afbf7b2011-07-25 11:53:08 -0700162 // Save and Restore
163 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700164
Winson Chung785d2eb2011-04-14 16:08:02 -0700165 // Content
Winson Chungd2945262011-06-24 15:22:14 -0700166 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700167
168 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -0700169 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700170
171 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700172 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700173 private int mWidgetCountX, mWidgetCountY;
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700175 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700176
Winson Chungb44b5242011-06-13 11:32:14 -0700177 // Previews & outlines
178 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800179 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700180
Sunny Goyal290800b2015-03-05 11:33:33 -0800181 private final PagedViewKeyListener mKeyListener = new PagedViewKeyListener();
182
Adam Cohened66b2b2012-01-23 17:28:51 -0800183 private Runnable mInflateWidgetRunnable = null;
184 private Runnable mBindWidgetRunnable = null;
185 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700186 static final int WIDGET_PRELOAD_PENDING = 0;
187 static final int WIDGET_BOUND = 1;
188 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800189 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
190 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800191 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800192 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700193 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800194
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700195 private Toast mWidgetInstructionToast;
196
Michael Jurka39e5d172012-03-12 18:36:12 -0700197 // Deferral of loading widget previews during launcher transitions
198 private boolean mInTransition;
199 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
200 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700201 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
202 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700203
Michael Jurka05713af2013-01-23 12:39:24 +0100204 WidgetPreviewLoader mWidgetPreviewLoader;
205
Michael Jurkac402cd92013-05-20 15:49:32 +0200206 private boolean mInBulkBind;
207 private boolean mNeedToUpdatePageCountsAndInvalidateData;
208
Winson Chung785d2eb2011-04-14 16:08:02 -0700209 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
210 super(context, attrs);
211 mLayoutInflater = LayoutInflater.from(context);
212 mPackageManager = context.getPackageManager();
Winson Chungb745afb2015-03-02 11:51:23 -0800213 mWidgets = new ArrayList<>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400214 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb745afb2015-03-02 11:51:23 -0800215 mRunningTasks = new ArrayList<>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700216
217 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800218 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700219 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
220 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
221 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700222 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700223
Winson Chung1ed747a2011-05-03 16:18:34 -0700224 // The padding on the non-matched dimension for the default widget preview icons
225 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700226 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700227
228 // Unless otherwise specified this view is important for accessibility.
229 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
230 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
231 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700232 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700233 }
234
235 @Override
236 protected void init() {
237 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700238 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700239
240 Context context = getContext();
241 Resources r = context.getResources();
242 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
243 }
244
Winson Chungc58497e2013-09-03 17:48:37 -0700245 public void onFinishInflate() {
246 super.onFinishInflate();
247
248 LauncherAppState app = LauncherAppState.getInstance();
249 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
250 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
251 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
252 }
253
Winson Chung67ca7e42013-10-31 16:53:19 -0700254 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700255 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700256 }
257
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100258 WidgetPreviewLoader getWidgetPreviewLoader() {
259 if (mWidgetPreviewLoader == null) {
260 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
261 }
262 return mWidgetPreviewLoader;
263 }
264
Winson Chung5afbf7b2011-07-25 11:53:08 -0700265 /** Returns the item index of the center item on this page so that we can restore to this
266 * item index when we rotate. */
267 private int getMiddleComponentIndexOnCurrentPage() {
268 int i = -1;
269 if (getPageCount() > 0) {
270 int currentPage = getCurrentPage();
Winson Chungb745afb2015-03-02 11:51:23 -0800271 if (mContentType == ContentType.Widgets) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700272 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700273 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
274 int childCount = layout.getChildCount();
275 if (childCount > 0) {
Winson Chungb745afb2015-03-02 11:51:23 -0800276 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700277 }
Winson Chungc58497e2013-09-03 17:48:37 -0700278 } else {
279 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700280 }
281 }
282 return i;
283 }
284
285 /** Get the index of the item to restore to if we need to restore the current page. */
286 int getSaveInstanceStateIndex() {
287 if (mSaveInstanceStateItemIndex == -1) {
288 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
289 }
290 return mSaveInstanceStateItemIndex;
291 }
292
293 /** Returns the page in the current orientation which is expected to contain the specified
294 * item index. */
295 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700296 if (index < 0) return 0;
297
Winson Chungb745afb2015-03-02 11:51:23 -0800298 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
299 return index / numItemsPerPage;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700300 }
301
Winson Chung5afbf7b2011-07-25 11:53:08 -0700302 /** Restores the page for an item at the specified index */
303 void restorePageForIndex(int index) {
304 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700305 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700306 }
307
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700308 private void updatePageCounts() {
309 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
310 (float) (mWidgetCountX * mWidgetCountY));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700311 }
312
Winson Chungf0ea4d32011-06-06 14:27:16 -0700313 protected void onDataReady(int width, int height) {
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700314 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700315
Winson Chungdb1138b2011-06-30 14:39:35 -0700316 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700317 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
318 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
319 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
320 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Winson Chungdb1138b2011-06-30 14:39:35 -0700321 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700322
Adam Cohen6c5891a2014-07-09 23:53:15 -0700323 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700324 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800325 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800326 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700327
Adam Cohena00673c2014-08-14 12:57:28 -0700328 protected void onLayout(boolean changed, int l, int t, int r, int b) {
329 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700330
Winson Chungf0ea4d32011-06-06 14:27:16 -0700331 if (!isDataReady()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800332 if (!mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700333 post(new Runnable() {
334 // This code triggers requestLayout so must be posted outside of the
335 // layout pass.
336 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800337 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200338 setDataIsReady();
339 onDataReady(getMeasuredWidth(), getMeasuredHeight());
340 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700341 }
342 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700343 }
344 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700345 }
346
Michael Jurkac402cd92013-05-20 15:49:32 +0200347 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700348 LauncherAppState app = LauncherAppState.getInstance();
349 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
350
Winson Chung1ed747a2011-05-03 16:18:34 -0700351 // Get the list of widgets and shortcuts
352 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200353 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800354 if (o instanceof LauncherAppWidgetProviderInfo) {
355 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
356 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100357 continue;
358 }
Adam Cohen59400422014-03-05 18:07:04 -0800359
360 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200361 // Ensure that all widgets we show can be added on a workspace of this size
362 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
363 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
364 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
365 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700366 if (minSpanX <= (int) grid.numColumns &&
367 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200368 mWidgets.add(widget);
369 } else {
370 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
371 widget.minWidth + ", " + widget.minHeight + ")");
372 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700373 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200374 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
375 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700376 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800377 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200378 // just add shortcuts
379 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800380 }
381 }
Adam Cohen59400422014-03-05 18:07:04 -0800382
Michael Jurkac402cd92013-05-20 15:49:32 +0200383 updatePageCountsAndInvalidateData();
384 }
385
386 public void setBulkBind(boolean bulkBind) {
387 if (bulkBind) {
388 mInBulkBind = true;
389 } else {
390 mInBulkBind = false;
391 if (mNeedToUpdatePageCountsAndInvalidateData) {
392 updatePageCountsAndInvalidateData();
393 }
394 }
395 }
396
397 private void updatePageCountsAndInvalidateData() {
398 if (mInBulkBind) {
399 mNeedToUpdatePageCountsAndInvalidateData = true;
400 } else {
401 updatePageCounts();
402 invalidateOnDataChange();
403 mNeedToUpdatePageCountsAndInvalidateData = false;
404 }
Winson Chung4b576be2011-04-27 17:40:20 -0700405 }
406
407 @Override
408 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700409 // When we have exited all apps or are in transition, disregard clicks
Winson Chungb745afb2015-03-02 11:51:23 -0800410 if (!mLauncher.isWidgetsViewVisible()
Sunny Goyal508da152014-08-14 10:53:27 -0700411 || mLauncher.getWorkspace().isSwitchingState()
412 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700413
Sunny Goyal508da152014-08-14 10:53:27 -0700414 // Let the user know that they have to long press to add a widget
415 if (mWidgetInstructionToast != null) {
416 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700417 }
Sunny Goyal508da152014-08-14 10:53:27 -0700418 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
419 Toast.LENGTH_SHORT);
420 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700421 }
422
423 /*
424 * PagedViewWithDraggableItems implementation
425 */
426 @Override
427 protected void determineDraggingStart(android.view.MotionEvent ev) {
Winson Chung4b576be2011-04-27 17:40:20 -0700428 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700429
Sunny Goyalff572272014-07-23 13:58:07 -0700430 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700431 Bundle options = null;
432 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700433 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
434 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700435 info.componentName, null);
436
Sunny Goyalff572272014-07-23 13:58:07 -0700437 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700438 int xPaddingDips = (int) ((padding.left + padding.right) / density);
439 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
440
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700441 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700442 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700443 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700444 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700445 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700446 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700447 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700448 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700449 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700450 }
451 return options;
452 }
453
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700454 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800455 final LauncherAppWidgetProviderInfo pInfo = info.info;
456 final Bundle options = pInfo.isCustomWidget ? null :
457 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700458
Adam Cohened66b2b2012-01-23 17:28:51 -0800459 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700460 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800461 return;
462 }
463
Adam Cohen21a170b2012-05-30 15:17:06 -0700464 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800465 mBindWidgetRunnable = new Runnable() {
466 @Override
467 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800468 if (pInfo.isCustomWidget) {
469 mWidgetCleanupState = WIDGET_BOUND;
470 return;
471 }
472
Adam Cohened66b2b2012-01-23 17:28:51 -0800473 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700474 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
475 mWidgetLoadingId, pInfo, options)) {
476 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700477 }
Adam Cohen59400422014-03-05 18:07:04 -0800478
Adam Cohened66b2b2012-01-23 17:28:51 -0800479 }
480 };
481 post(mBindWidgetRunnable);
482
483 mInflateWidgetRunnable = new Runnable() {
484 @Override
485 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700486 if (mWidgetCleanupState != WIDGET_BOUND) {
487 return;
488 }
Adam Cohen59400422014-03-05 18:07:04 -0800489 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
490 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800491 info.boundWidget = hostView;
492 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800493 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700494 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
495 info.spanY, info, false);
496
497 // We want the first widget layout to be the correct size. This will be important
498 // for width size reporting to the AppWidgetManager.
499 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
500 unScaledSize[1]);
501 lp.x = lp.y = 0;
502 lp.customPosition = true;
503 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800504 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800505 }
506 };
507 post(mInflateWidgetRunnable);
508 }
509
510 @Override
511 public void onShortPress(View v) {
512 // We are anticipating a long press, and we use this time to load bind and instantiate
513 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700514 if (mCreateWidgetInfo != null) {
515 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
516 cleanupWidgetPreloading(false);
517 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800518 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700519 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800520 }
521
Adam Cohen0e56cc92012-05-11 15:57:05 -0700522 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
523 if (!widgetWasAdded) {
524 // If the widget was not added, we may need to do further cleanup.
525 PendingAddWidgetInfo info = mCreateWidgetInfo;
526 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700527
528 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700529 // We never did any preloading, so just remove pending callbacks to do so
530 removeCallbacks(mBindWidgetRunnable);
531 removeCallbacks(mInflateWidgetRunnable);
532 } else if (mWidgetCleanupState == WIDGET_BOUND) {
533 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800534 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700535 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
536 }
537
538 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700539 removeCallbacks(mInflateWidgetRunnable);
540 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700541 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800542 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700543 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
544 }
545
Adam Cohen0e56cc92012-05-11 15:57:05 -0700546 // The widget was inflated and added to the DragLayer -- remove it.
547 AppWidgetHostView widget = info.boundWidget;
548 mLauncher.getDragLayer().removeView(widget);
549 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 }
551 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
552 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700553 mCreateWidgetInfo = null;
554 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800555 }
556
Adam Cohen7a326642012-02-22 12:03:22 -0800557 @Override
558 public void cleanUpShortPress(View v) {
559 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700560 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800561 }
562 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800563
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700564 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800565 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700566 // Get the widget preview as the drag representation
567 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700568 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700569
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700570 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
571 // we abort the drag.
572 if (image.getDrawable() == null) {
573 mDraggingWidget = false;
574 return false;
575 }
576
Winson Chung4b576be2011-04-27 17:40:20 -0700577 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800578 Bitmap preview;
579 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800580 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100581 Point previewPadding = null;
582
Winson Chung1ed747a2011-05-03 16:18:34 -0700583 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700584 // This can happen in some weird cases involving multi-touch. We can't start dragging
585 // the widget if this is null, so we break out.
586 if (mCreateWidgetInfo == null) {
587 return false;
588 }
589
Adam Cohen1b36dc32012-02-13 19:27:37 -0800590 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
591 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700592 int spanX = createItemInfo.spanX;
593 int spanY = createItemInfo.spanY;
594 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
595 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700596
Winson Chung72d59842012-02-22 13:51:36 -0800597 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
598 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700599 int maxWidth, maxHeight;
600 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
601 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800602
Michael Jurka05713af2013-01-23 12:39:24 +0100603 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700604 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
605 spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100606
607 // Compare the size of the drag preview to the preview in the AppsCustomize tray
608 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100609 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100610 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
611
612 // The bitmap in the AppsCustomize tray is always the the same size, so there
613 // might be extra pixels around the preview itself - this accounts for that
614 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
615 int padding =
616 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
617 previewPadding = new Point(padding, 0);
618 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700619 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700620 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
621 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700622 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700623 createItemInfo.spanX = createItemInfo.spanY = 1;
624 }
Winson Chung4b576be2011-04-27 17:40:20 -0700625
Michael Jurka8c3339b2012-06-14 16:18:21 -0700626 // Don't clip alpha values for the drag outline if we're using the default widget preview
627 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
628 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700629
Winson Chung1120e032011-11-22 16:11:31 -0800630 // Save the preview for the outline generation, then dim the preview
631 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
632 false);
Winson Chung1120e032011-11-22 16:11:31 -0800633
Winson Chung4b576be2011-04-27 17:40:20 -0700634 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700635 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700636 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800637 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100638 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800639 outline.recycle();
640 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700641 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700642 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800643
Winson Chung4b576be2011-04-27 17:40:20 -0700644 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800645 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700646 if (!super.beginDragging(v)) return false;
647
Winson Chungb745afb2015-03-02 11:51:23 -0800648 if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700649 if (!beginDraggingWidget(v)) {
650 return false;
651 }
Winson Chungb745afb2015-03-02 11:51:23 -0800652 } else {
653 Log.e(TAG, "Unexpected dragging view: " + v);
Winson Chung4b576be2011-04-27 17:40:20 -0700654 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800655
656 // We delay entering spring-loaded mode slightly to make sure the UI
657 // thready is free of any work.
658 postDelayed(new Runnable() {
659 @Override
660 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800661 // We don't enter spring-loaded mode if the drag has been cancelled
662 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800663 // Go into spring loaded mode (must happen before we startDrag())
664 mLauncher.enterSpringLoadedDragMode();
665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 }
Winson Chung72d59842012-02-22 13:51:36 -0800667 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800668
Winson Chung785d2eb2011-04-14 16:08:02 -0700669 return true;
670 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800671
Winson Chunga48487a2012-03-20 16:19:37 -0700672 /**
673 * Clean up after dragging.
674 *
675 * @param target where the item was dragged to (can be null if the item was flung)
676 */
677 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700678 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800679 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 // Exit spring loaded mode if we have not successfully dropped or have not handled the
681 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700682 mLauncher.exitSpringLoadedDragModeDelayed(true,
683 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700684 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700685 } else {
686 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700687 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700688 }
689
Winson Chung785d2eb2011-04-14 16:08:02 -0700690 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700691 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700692 if (getChildCount() > 0) {
693 return getChildAt(0);
694 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700695 return null;
696 }
697
698 @Override
699 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700700 mInTransition = true;
701 if (toWorkspace) {
702 cancelAllTasks();
703 }
704 }
705
706 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700707 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700708 }
709
710 @Override
711 public void onLauncherTransitionStep(Launcher l, float t) {
712 }
713
714 @Override
715 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
716 mInTransition = false;
717 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700718 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700719 }
720 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700721 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
722 r.run();
723 }
724 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700725 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700726 }
727
728 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700729 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
730 boolean success) {
731 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
732 if (isFlingToDelete) return;
733
734 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700735
736 // Display an error message if the drag failed due to there not being enough space on the
737 // target layout we were dropping on.
738 if (!success) {
739 boolean showOutOfSpaceMessage = false;
740 if (target instanceof Workspace) {
741 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
742 Workspace workspace = (Workspace) target;
743 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700744 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700745 if (layout != null) {
746 layout.calculateSpans(itemInfo);
747 showOutOfSpaceMessage =
748 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
749 }
750 }
Winson Chungfc79c802011-05-02 13:35:34 -0700751 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700752 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700753 }
Adam Cohen7a326642012-02-22 12:03:22 -0800754
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800755 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700756 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700757 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800758 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700759 }
760
Winson Chunga48487a2012-03-20 16:19:37 -0700761 @Override
762 public void onFlingToDeleteCompleted() {
763 // We just dismiss the drag when we fling, so cleanup here
764 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700765 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700766 mDraggingWidget = false;
767 }
768
769 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800770 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700771 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800772 }
773
Winson Chung7f0acdd2011-09-19 18:34:19 -0700774 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000775 public boolean supportsAppInfoDropTarget() {
776 return true;
777 }
778
779 @Override
780 public boolean supportsDeleteDropTarget() {
781 return false;
782 }
783
784 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800785 public float getIntrinsicIconScaleFactor() {
786 LauncherAppState app = LauncherAppState.getInstance();
787 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
788 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
789 }
790
791 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700792 protected void onDetachedFromWindow() {
793 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700794 cancelAllTasks();
795 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700796
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800797 @Override
798 public void trimMemory() {
799 super.trimMemory();
800 clearAllWidgetPages();
801 }
802
Michael Jurkae326f182011-11-21 14:05:46 -0800803 public void clearAllWidgetPages() {
804 cancelAllTasks();
805 int count = getChildCount();
806 for (int i = 0; i < count; i++) {
807 View v = getPageAt(i);
808 if (v instanceof PagedViewGridLayout) {
809 ((PagedViewGridLayout) v).removeAllViewsOnPage();
810 mDirtyPageContent.set(i, true);
811 }
812 }
813 }
814
Adam Cohen0cd3b642011-10-14 14:58:00 -0700815 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700816 // Clean up all the async tasks
817 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
818 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800819 AppsCustomizeAsyncTask task = iter.next();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700820 task.cancel(false);
821 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700822 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700823
824 // We've already preallocated the views for the data to load into, so clear them as well
825 View v = getPageAt(task.page);
826 if (v instanceof PagedViewGridLayout) {
827 ((PagedViewGridLayout) v).removeAllViewsOnPage();
828 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700829 }
Winson Chung83687b12012-04-25 16:01:01 -0700830 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700831 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700832 }
833
Winson Chung785d2eb2011-04-14 16:08:02 -0700834 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200835 // Widgets appear to be cleared every time you leave, always force invalidate for them
836 if (mContentType != type || type == ContentType.Widgets) {
837 int page = (mContentType != type) ? 0 : getCurrentPage();
838 mContentType = type;
839 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700840 }
Winson Chungc58497e2013-09-03 17:48:37 -0700841 }
842
843 public ContentType getContentType() {
844 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700845 }
846
Adam Cohen0cd3b642011-10-14 14:58:00 -0700847 protected void snapToPage(int whichPage, int delta, int duration) {
848 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800849
850 // Update the thread priorities given the direction lookahead
851 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
852 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800853 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700854 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800855 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
856 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
857 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
858 } else {
859 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
860 }
861 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700862 }
863
Adam Cohen9bfdb762014-07-21 17:44:06 -0700864 public void setPageBackgroundsVisible(boolean visible) {
865 mPageBackgroundsVisible = visible;
866 int childCount = getChildCount();
867 for (int i = 0; i < childCount; ++i) {
868 Drawable bg = getChildAt(i).getBackground();
869 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700870 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700871 }
872 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700873 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700874
Winson Chungb44b5242011-06-13 11:32:14 -0700875 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800876 * A helper to return the priority for loading of the specified widget page.
877 */
878 private int getWidgetPageLoadPriority(int page) {
879 // If we are snapping to another page, use that index as the target page index
880 int toPage = mCurrentPage;
881 if (mNextPage > -1) {
882 toPage = mNextPage;
883 }
884
885 // We use the distance from the target page as an initial guess of priority, but if there
886 // are no pages of higher priority than the page specified, then bump up the priority of
887 // the specified page.
888 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
889 int minPageDiff = Integer.MAX_VALUE;
890 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800891 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700892 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800893 }
894
895 int rawPageDiff = Math.abs(page - toPage);
896 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
897 }
898 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700899 * Return the appropriate thread priority for loading for a given page (we give the current
900 * page much higher priority)
901 */
902 private int getThreadPriorityForPage(int page) {
903 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800904 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700905 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800906 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700907 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800908 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700909 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800910 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700911 }
912 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700913 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800914 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700915 return Math.max(0, pageDiff * sPageSleepDelay);
916 }
Winson Chungb44b5242011-06-13 11:32:14 -0700917 /**
918 * Creates and executes a new AsyncTask to load a page of widget previews.
919 */
920 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700921 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800922
Winson Chungb44b5242011-06-13 11:32:14 -0700923 // Prune all tasks that are no longer needed
924 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
925 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800926 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700927 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800928 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
929 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700930 task.cancel(false);
931 iter.remove();
932 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800933 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700934 }
935 }
936
Winson Chungf314b0e2011-08-16 11:54:27 -0700937 // 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 -0700938 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700939 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700940 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700941 @Override
942 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700943 try {
Winson Chung09945932011-09-20 14:22:40 -0700944 try {
945 Thread.sleep(sleepMs);
946 } catch (Exception e) {}
947 loadWidgetPreviewsInBackground(task, data);
948 } finally {
949 if (task.isCancelled()) {
950 data.cleanup(true);
951 }
952 }
Winson Chungb44b5242011-06-13 11:32:14 -0700953 }
954 },
955 new AsyncTaskCallback() {
956 @Override
957 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700958 mRunningTasks.remove(task);
959 if (task.isCancelled()) return;
960 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -0700961 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700962 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100963 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -0700964
965 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700966 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700967 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -0700968 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -0700969 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
970 mRunningTasks.add(t);
971 }
Winson Chungb44b5242011-06-13 11:32:14 -0700972
Winson Chung785d2eb2011-04-14 16:08:02 -0700973 /*
974 * Widgets PagedView implementation
975 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700976 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -0700977 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700978 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -0700979 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
980 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700981
982 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
983 if (bg != null) {
984 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
985 layout.setBackground(bg);
986 }
Winson Chung63257c12011-05-05 17:06:13 -0700987 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700989
Michael Jurka038f9d82011-11-03 13:50:45 -0700990 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700991 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -0700992
Adam Cohen4e243a22014-08-10 18:30:55 -0700993 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
994
Winson Chungd2945262011-06-24 15:22:14 -0700995 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -0700996 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -0700997 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -0700998 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -0700999 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
1000
Adam Cohen4e243a22014-08-10 18:30:55 -07001001 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -07001002
Winson Chunge4a647f2011-09-30 14:41:25 -07001003 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -07001004 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001005 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1006 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001007 }
1008
Winson Chunge4a647f2011-09-30 14:41:25 -07001009 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001010 layout.setColumnCount(layout.getCellCountX());
1011 for (int i = 0; i < items.size(); ++i) {
1012 Object rawInfo = items.get(i);
1013 PendingAddItemInfo createItemInfo = null;
1014 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1015 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001016
1017 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001018 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001019 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1020 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001021
Adam Cohen59400422014-03-05 18:07:04 -08001022 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001023 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001024 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001025 } else if (rawInfo instanceof ResolveInfo) {
1026 // Fill in the shortcuts information
1027 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001028 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001029 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1030 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1031 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001032 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001033 widget.setTag(createItemInfo);
1034 }
Adam Cohen59400422014-03-05 18:07:04 -08001035
Winson Chunge4a647f2011-09-30 14:41:25 -07001036 widget.setOnClickListener(this);
1037 widget.setOnLongClickListener(this);
1038 widget.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -08001039 widget.setOnKeyListener(mKeyListener);
Winson Chunge4a647f2011-09-30 14:41:25 -07001040
1041 // Layout each widget
1042 int ix = i % mWidgetCountX;
1043 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001044
1045 if (ix > 0) {
1046 View border = widget.findViewById(R.id.left_border);
1047 border.setVisibility(View.VISIBLE);
1048 }
1049 if (ix < mWidgetCountX - 1) {
1050 View border = widget.findViewById(R.id.right_border);
1051 border.setVisibility(View.VISIBLE);
1052 }
1053
Winson Chunge4a647f2011-09-30 14:41:25 -07001054 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001055 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001056 GridLayout.spec(ix, GridLayout.TOP));
1057 lp.width = cellWidth;
1058 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001059 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001060 layout.addView(widget, lp);
1061 }
1062
Michael Jurka038f9d82011-11-03 13:50:45 -07001063 // wait until a call on onLayout to start loading, because
1064 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1065 // TODO: can we do a measure/layout immediately?
1066 layout.setOnLayoutListener(new Runnable() {
1067 public void run() {
1068 // Load the widget previews
1069 int maxPreviewWidth = cellWidth;
1070 int maxPreviewHeight = cellHeight;
1071 if (layout.getChildCount() > 0) {
1072 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1073 int[] maxSize = w.getPreviewSize();
1074 maxPreviewWidth = maxSize[0];
1075 maxPreviewHeight = maxSize[1];
1076 }
Michael Jurka05713af2013-01-23 12:39:24 +01001077
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001078 getWidgetPreviewLoader().setPreviewSize(
Michael Jurka3f4e0702013-02-05 11:21:28 +01001079 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001080 if (immediate) {
1081 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001082 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001083 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001084 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001085 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001086 if (mInTransition) {
1087 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1088 } else {
1089 prepareLoadWidgetPreviewsTask(page, items,
1090 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1091 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001092 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001093 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001094 }
1095 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001096 }
1097 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1098 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001099 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1100 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001101 if (task != null) {
1102 // Ensure that this task starts running at the correct priority
1103 task.syncThreadPriority();
1104 }
1105
1106 // Load each of the widget/shortcut previews
1107 ArrayList<Object> items = data.items;
1108 ArrayList<Bitmap> images = data.generatedImages;
1109 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001110 for (int i = 0; i < count; ++i) {
1111 if (task != null) {
1112 // Ensure we haven't been cancelled yet
1113 if (task.isCancelled()) break;
1114 // Before work on each item, ensure that this task is running at the correct
1115 // priority
1116 task.syncThreadPriority();
1117 }
1118
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001119 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001120 }
Winson Chungb44b5242011-06-13 11:32:14 -07001121 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001122
Winson Chung7bb37522013-10-28 11:07:57 -07001123 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1124 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001125 mDeferredSyncWidgetPageItems.add(data);
1126 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001127 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001128 try {
1129 int page = data.page;
1130 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001131
Michael Jurka39e5d172012-03-12 18:36:12 -07001132 ArrayList<Object> items = data.items;
1133 int count = items.size();
1134 for (int i = 0; i < count; ++i) {
1135 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1136 if (widget != null) {
1137 Bitmap preview = data.generatedImages.get(i);
1138 widget.applyPreview(new FastBitmapDrawable(preview), i);
1139 }
1140 }
Winson Chung68e4c642011-11-10 15:48:25 -08001141
Michael Jurka47639b92013-01-14 12:42:27 +01001142 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001143
1144 // Update all thread priorities
1145 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1146 while (iter.hasNext()) {
1147 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1148 int pageIndex = task.page;
1149 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1150 }
1151 } finally {
1152 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001153 }
Winson Chungb44b5242011-06-13 11:32:14 -07001154 }
Winson Chung46af2e82011-05-09 16:00:53 -07001155
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 @Override
1157 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001158 disablePagedViewAnimations();
1159
Winson Chung785d2eb2011-04-14 16:08:02 -07001160 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001161 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001162
Adam Cohen0cd3b642011-10-14 14:58:00 -07001163 Context context = getContext();
Winson Chungb745afb2015-03-02 11:51:23 -08001164 if (mContentType == ContentType.Widgets) {
Winson Chungc58497e2013-09-03 17:48:37 -07001165 for (int j = 0; j < mNumWidgetPages; ++j) {
1166 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1167 mWidgetCountY);
1168 setupPage(layout);
1169 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1170 LayoutParams.MATCH_PARENT));
1171 }
1172 } else {
1173 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001174 }
1175
Winson Chungc58497e2013-09-03 17:48:37 -07001176 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001177 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001178
Winson Chung785d2eb2011-04-14 16:08:02 -07001179 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001180 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001181 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001182 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001183 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001184 Log.e(TAG, "Unexpected ContentType");
Winson Chung785d2eb2011-04-14 16:08:02 -07001185 }
1186 }
1187
Adam Cohen22f823d2011-09-01 17:22:18 -07001188 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1189 // it is in the z-order. This is important to insure touch events are handled correctly.
1190 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001191 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001192 }
1193
Adam Cohenae4f1552011-10-20 00:15:42 -07001194 @Override
1195 protected int indexToPage(int index) {
1196 return getChildCount() - index - 1;
1197 }
1198
Adam Cohen22f823d2011-09-01 17:22:18 -07001199 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1200 @Override
1201 protected void screenScrolled(int screenCenter) {
1202 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001203 enableHwLayersOnVisiblePages();
1204 }
1205
1206 private void enableHwLayersOnVisiblePages() {
1207 final int screenCount = getChildCount();
1208
1209 getVisiblePages(mTempVisiblePagesRange);
1210 int leftScreen = mTempVisiblePagesRange[0];
1211 int rightScreen = mTempVisiblePagesRange[1];
1212 int forceDrawScreen = -1;
1213 if (leftScreen == rightScreen) {
1214 // make sure we're caching at least two pages always
1215 if (rightScreen < screenCount - 1) {
1216 rightScreen++;
1217 forceDrawScreen = rightScreen;
1218 } else if (leftScreen > 0) {
1219 leftScreen--;
1220 forceDrawScreen = leftScreen;
1221 }
1222 } else {
1223 forceDrawScreen = leftScreen + 1;
1224 }
1225
1226 for (int i = 0; i < screenCount; i++) {
1227 final View layout = (View) getPageAt(i);
1228 if (!(leftScreen <= i && i <= rightScreen &&
1229 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1230 layout.setLayerType(LAYER_TYPE_NONE, null);
1231 }
1232 }
1233
Michael Jurka47639b92013-01-14 12:42:27 +01001234 for (int i = 0; i < screenCount; i++) {
1235 final View layout = (View) getPageAt(i);
1236
1237 if (leftScreen <= i && i <= rightScreen &&
1238 (i == forceDrawScreen || shouldDrawChild(layout))) {
1239 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1240 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1241 }
1242 }
1243 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001244 }
1245
1246 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001247 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001248 }
1249
Winson Chung785d2eb2011-04-14 16:08:02 -07001250 /**
1251 * Used by the parent to get the content width to set the tab bar to
1252 * @return
1253 */
1254 public int getPageContentWidth() {
1255 return mContentWidth;
1256 }
1257
Winson Chungb26f3d62011-06-02 10:49:29 -07001258 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001259 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001260 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001261 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001262 // We reset the save index when we change pages so that it will be recalculated on next
1263 // rotation
1264 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001265 }
1266
Winson Chung785d2eb2011-04-14 16:08:02 -07001267 /*
1268 * AllAppsView implementation
1269 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 public void setup(Launcher launcher, DragController dragController) {
1271 mLauncher = launcher;
1272 mDragController = dragController;
1273 }
Winson Chung9802ac92012-06-08 16:01:58 -07001274
1275 /**
Winson Chungb745afb2015-03-02 11:51:23 -08001276 * We should call thise method whenever the core data changes (mWidgets) so that we can
Winson Chung9802ac92012-06-08 16:01:58 -07001277 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1278 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1279 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1280 */
1281 private void invalidateOnDataChange() {
1282 if (!isDataReady()) {
1283 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1284 // request a layout to trigger the page data when ready.
1285 requestLayout();
1286 } else {
1287 cancelAllTasks();
1288 invalidatePageData();
1289 }
1290 }
1291
Winson Chung785d2eb2011-04-14 16:08:02 -07001292 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001293 // If we have reset, then we should not continue to restore the previous state
1294 mSaveInstanceStateItemIndex = -1;
1295
Winson Chungb745afb2015-03-02 11:51:23 -08001296 if (mContentType != ContentType.Widgets) {
1297 setContentType(ContentType.Widgets);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001298 }
Winson Chung649668f2012-01-10 13:07:16 -08001299
Adam Cohenb64d36e2011-10-17 21:48:02 -07001300 if (mCurrentPage != 0) {
1301 invalidatePageData(0);
1302 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001304
1305 private AppsCustomizeTabHost getTabHost() {
1306 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1307 }
1308
Winson Chung785d2eb2011-04-14 16:08:02 -07001309 public void dumpState() {
1310 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001311 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 }
Adam Cohen4e844012011-11-09 13:48:04 -08001313
Winson Chung785d2eb2011-04-14 16:08:02 -07001314 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001315 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001316 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001317 for (Object i: list) {
1318 if (i instanceof AppWidgetProviderInfo) {
1319 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1320 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1321 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1322 + " initialLayout=" + info.initialLayout
1323 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1324 } else if (i instanceof ResolveInfo) {
1325 ResolveInfo info = (ResolveInfo) i;
1326 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1327 + info.icon);
1328 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001329 }
1330 }
Adam Cohen4e844012011-11-09 13:48:04 -08001331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 public void surrender() {
1333 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1334 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001335
1336 // Stop all background tasks
1337 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 }
Winson Chung007c6982011-06-14 13:27:53 -07001339
Winson Chungb44b5242011-06-13 11:32:14 -07001340 /*
1341 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1342 * widget previews in the background with the AsyncTasks.
1343 */
Winson Chung68e4c642011-11-10 15:48:25 -08001344 final static int sLookBehindPageCount = 2;
1345 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001346 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001347 final int count = getChildCount();
1348 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1349 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1350 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001351 }
1352 protected int getAssociatedUpperPageBound(int page) {
1353 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001354 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1355 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1356 count - 1);
1357 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001358 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001359
Winson Chung6a0f57d2011-06-29 20:10:49 -07001360 protected String getCurrentPageDescription() {
1361 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1362 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001363 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001364
Winson Chungb745afb2015-03-02 11:51:23 -08001365 if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001366 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001367 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001368 } else {
1369 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001370 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001371
Michael Jurka8b805b12012-04-18 14:23:14 -07001372 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001373 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001374}