blob: 3f2aa7036a3b98a3ed7fba9b5a2da09a1a631d25 [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 Chungfd3385f2011-06-15 19:51:24 -070041import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070043import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070044
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal290800b2015-03-05 11:33:33 -080046import com.android.launcher3.FocusHelper.PagedViewKeyListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Adam Cohenc0dcf592011-06-01 15:30:43 -070048
49import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.Iterator;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
Winson Chungb44b5242011-06-13 11:32:14 -070052/**
53 * A simple callback interface which also provides the results of the task.
54 */
55interface AsyncTaskCallback {
56 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
57}
Winson Chung4e076542011-06-23 13:04:10 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * The data needed to perform either of the custom AsyncTasks.
61 */
62class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070063 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080064 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070065 }
66
Michael Jurka038f9d82011-11-03 13:50:45 -070067 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010068 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070069 page = p;
70 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070071 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070072 maxImageWidth = cw;
73 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070074 doInBackgroundCallback = bgR;
75 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010076 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070077 }
Winson Chung09945932011-09-20 14:22:40 -070078 void cleanup(boolean cancelled) {
79 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070080 if (generatedImages != null) {
81 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010082 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010083 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070084 }
85 }
86 generatedImages.clear();
87 }
88 }
Winson Chungb44b5242011-06-13 11:32:14 -070089 int page;
90 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070091 ArrayList<Bitmap> sourceImages;
92 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -070093 int maxImageWidth;
94 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -070095 AsyncTaskCallback doInBackgroundCallback;
96 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +010097 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -070098}
Winson Chung4e076542011-06-23 13:04:10 -070099
Winson Chungb44b5242011-06-13 11:32:14 -0700100/**
101 * A generic template for an async task used in AppsCustomize.
102 */
103class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700104 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700105 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700106 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700107 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700108 }
109 @Override
110 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
111 if (params.length != 1) return null;
112 // Load each of the widget previews in the background
113 params[0].doInBackgroundCallback.run(this, params[0]);
114 return params[0];
115 }
116 @Override
117 protected void onPostExecute(AsyncTaskPageData result) {
118 // All the widget previews are loaded, so we can just callback to inflate the page
119 result.postExecuteCallback.run(this, result);
120 }
121
122 void setThreadPriority(int p) {
123 threadPriority = p;
124 }
125 void syncThreadPriority() {
126 Process.setThreadPriority(threadPriority);
127 }
128
129 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700130 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700131 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700132 int threadPriority;
133}
Winson Chungb44b5242011-06-13 11:32:14 -0700134
135/**
136 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
137 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700138public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Sunny Goyal290800b2015-03-05 11:33:33 -0800139 View.OnClickListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -0700140 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700141 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700142
Sunny Goyalff572272014-07-23 13:58:07 -0700143 private static Rect sTmpRect = new Rect();
144
Winson Chung785d2eb2011-04-14 16:08:02 -0700145 /**
146 * The different content types that this paged view can show.
147 */
148 public enum ContentType {
Winson Chung6a26e5b2011-05-26 14:36:06 -0700149 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700150 }
Winson Chungb745afb2015-03-02 11:51:23 -0800151 private ContentType mContentType = ContentType.Widgets;
Winson Chung785d2eb2011-04-14 16:08:02 -0700152
153 // Refs
154 private Launcher mLauncher;
155 private DragController mDragController;
156 private final LayoutInflater mLayoutInflater;
157 private final PackageManager mPackageManager;
158
Winson Chung5afbf7b2011-07-25 11:53:08 -0700159 // Save and Restore
160 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700161
Winson Chung785d2eb2011-04-14 16:08:02 -0700162 // Content
Winson Chungd2945262011-06-24 15:22:14 -0700163 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700164
165 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -0700166 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700167
168 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700169 private int mContentWidth, mContentHeight;
Winson Chung4b576be2011-04-27 17:40:20 -0700170 private int mWidgetCountX, mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700171 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700172
Winson Chungb44b5242011-06-13 11:32:14 -0700173 // Previews & outlines
174 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800175 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700176
Sunny Goyal290800b2015-03-05 11:33:33 -0800177 private final PagedViewKeyListener mKeyListener = new PagedViewKeyListener();
178
Adam Cohened66b2b2012-01-23 17:28:51 -0800179 private Runnable mInflateWidgetRunnable = null;
180 private Runnable mBindWidgetRunnable = null;
181 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700182 static final int WIDGET_PRELOAD_PENDING = 0;
183 static final int WIDGET_BOUND = 1;
184 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800185 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
186 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800187 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800188 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700189 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800190
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700191 private Toast mWidgetInstructionToast;
192
Michael Jurka39e5d172012-03-12 18:36:12 -0700193 // Deferral of loading widget previews during launcher transitions
194 private boolean mInTransition;
195 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
196 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700197 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
198 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700199
Michael Jurka05713af2013-01-23 12:39:24 +0100200 WidgetPreviewLoader mWidgetPreviewLoader;
201
Michael Jurkac402cd92013-05-20 15:49:32 +0200202 private boolean mInBulkBind;
203 private boolean mNeedToUpdatePageCountsAndInvalidateData;
204
Winson Chung785d2eb2011-04-14 16:08:02 -0700205 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
206 super(context, attrs);
207 mLayoutInflater = LayoutInflater.from(context);
208 mPackageManager = context.getPackageManager();
Winson Chungb745afb2015-03-02 11:51:23 -0800209 mWidgets = new ArrayList<>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400210 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb745afb2015-03-02 11:51:23 -0800211 mRunningTasks = new ArrayList<>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700212
213 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800214 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700215 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
216 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
217 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700218
Winson Chung1ed747a2011-05-03 16:18:34 -0700219 // The padding on the non-matched dimension for the default widget preview icons
220 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700221 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700222
223 // Unless otherwise specified this view is important for accessibility.
224 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
225 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
226 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700227 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700228 }
229
230 @Override
231 protected void init() {
232 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700233 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700234
235 Context context = getContext();
236 Resources r = context.getResources();
237 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
238 }
239
Winson Chungc58497e2013-09-03 17:48:37 -0700240 public void onFinishInflate() {
241 super.onFinishInflate();
242
243 LauncherAppState app = LauncherAppState.getInstance();
244 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
245 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
246 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
247 }
248
Winson Chung67ca7e42013-10-31 16:53:19 -0700249 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700250 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700251 }
252
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100253 WidgetPreviewLoader getWidgetPreviewLoader() {
254 if (mWidgetPreviewLoader == null) {
255 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
256 }
257 return mWidgetPreviewLoader;
258 }
259
Winson Chung5afbf7b2011-07-25 11:53:08 -0700260 /** Returns the item index of the center item on this page so that we can restore to this
261 * item index when we rotate. */
262 private int getMiddleComponentIndexOnCurrentPage() {
263 int i = -1;
264 if (getPageCount() > 0) {
265 int currentPage = getCurrentPage();
Winson Chungb745afb2015-03-02 11:51:23 -0800266 if (mContentType == ContentType.Widgets) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700267 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700268 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
269 int childCount = layout.getChildCount();
270 if (childCount > 0) {
Winson Chungb745afb2015-03-02 11:51:23 -0800271 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700272 }
Winson Chungc58497e2013-09-03 17:48:37 -0700273 } else {
274 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700275 }
276 }
277 return i;
278 }
279
280 /** Get the index of the item to restore to if we need to restore the current page. */
281 int getSaveInstanceStateIndex() {
282 if (mSaveInstanceStateItemIndex == -1) {
283 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
284 }
285 return mSaveInstanceStateItemIndex;
286 }
287
288 /** Returns the page in the current orientation which is expected to contain the specified
289 * item index. */
290 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700291 if (index < 0) return 0;
292
Winson Chungb745afb2015-03-02 11:51:23 -0800293 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
294 return index / numItemsPerPage;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700295 }
296
Winson Chung5afbf7b2011-07-25 11:53:08 -0700297 /** Restores the page for an item at the specified index */
298 void restorePageForIndex(int index) {
299 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700300 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700301 }
302
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700303 private void updatePageCounts() {
304 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
305 (float) (mWidgetCountX * mWidgetCountY));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700306 }
307
Winson Chungf0ea4d32011-06-06 14:27:16 -0700308 protected void onDataReady(int width, int height) {
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700309 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700310
Winson Chungdb1138b2011-06-30 14:39:35 -0700311 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700312 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
313 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700314
Adam Cohen6c5891a2014-07-09 23:53:15 -0700315 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800317 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800318 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700319
Adam Cohena00673c2014-08-14 12:57:28 -0700320 protected void onLayout(boolean changed, int l, int t, int r, int b) {
321 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700322
Winson Chungf0ea4d32011-06-06 14:27:16 -0700323 if (!isDataReady()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800324 if (!mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700325 post(new Runnable() {
326 // This code triggers requestLayout so must be posted outside of the
327 // layout pass.
328 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800329 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200330 setDataIsReady();
331 onDataReady(getMeasuredWidth(), getMeasuredHeight());
332 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700333 }
334 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 }
336 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700337 }
338
Michael Jurkac402cd92013-05-20 15:49:32 +0200339 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700340 LauncherAppState app = LauncherAppState.getInstance();
341 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
342
Winson Chung1ed747a2011-05-03 16:18:34 -0700343 // Get the list of widgets and shortcuts
344 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200345 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800346 if (o instanceof LauncherAppWidgetProviderInfo) {
347 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
348 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100349 continue;
350 }
Adam Cohen59400422014-03-05 18:07:04 -0800351
352 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200353 // Ensure that all widgets we show can be added on a workspace of this size
354 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
355 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
356 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
357 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700358 if (minSpanX <= (int) grid.numColumns &&
359 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200360 mWidgets.add(widget);
361 } else {
362 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
363 widget.minWidth + ", " + widget.minHeight + ")");
364 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700365 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200366 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
367 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700368 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800369 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200370 // just add shortcuts
371 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800372 }
373 }
Adam Cohen59400422014-03-05 18:07:04 -0800374
Michael Jurkac402cd92013-05-20 15:49:32 +0200375 updatePageCountsAndInvalidateData();
376 }
377
378 public void setBulkBind(boolean bulkBind) {
379 if (bulkBind) {
380 mInBulkBind = true;
381 } else {
382 mInBulkBind = false;
383 if (mNeedToUpdatePageCountsAndInvalidateData) {
384 updatePageCountsAndInvalidateData();
385 }
386 }
387 }
388
389 private void updatePageCountsAndInvalidateData() {
390 if (mInBulkBind) {
391 mNeedToUpdatePageCountsAndInvalidateData = true;
392 } else {
393 updatePageCounts();
394 invalidateOnDataChange();
395 mNeedToUpdatePageCountsAndInvalidateData = false;
396 }
Winson Chung4b576be2011-04-27 17:40:20 -0700397 }
398
399 @Override
400 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700401 // When we have exited all apps or are in transition, disregard clicks
Winson Chungb745afb2015-03-02 11:51:23 -0800402 if (!mLauncher.isWidgetsViewVisible()
Sunny Goyal508da152014-08-14 10:53:27 -0700403 || mLauncher.getWorkspace().isSwitchingState()
404 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700405
Sunny Goyal508da152014-08-14 10:53:27 -0700406 // Let the user know that they have to long press to add a widget
407 if (mWidgetInstructionToast != null) {
408 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700409 }
Sunny Goyal508da152014-08-14 10:53:27 -0700410 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
411 Toast.LENGTH_SHORT);
412 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700413 }
414
415 /*
416 * PagedViewWithDraggableItems implementation
417 */
418 @Override
419 protected void determineDraggingStart(android.view.MotionEvent ev) {
Winson Chung4b576be2011-04-27 17:40:20 -0700420 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700421
Sunny Goyalff572272014-07-23 13:58:07 -0700422 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700423 Bundle options = null;
424 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700425 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
426 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700427 info.componentName, null);
428
Sunny Goyalff572272014-07-23 13:58:07 -0700429 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700430 int xPaddingDips = (int) ((padding.left + padding.right) / density);
431 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
432
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700433 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700434 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700435 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700436 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700437 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700438 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700439 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700440 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700441 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700442 }
443 return options;
444 }
445
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700446 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800447 final LauncherAppWidgetProviderInfo pInfo = info.info;
448 final Bundle options = pInfo.isCustomWidget ? null :
449 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700450
Adam Cohened66b2b2012-01-23 17:28:51 -0800451 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700452 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800453 return;
454 }
455
Adam Cohen21a170b2012-05-30 15:17:06 -0700456 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800457 mBindWidgetRunnable = new Runnable() {
458 @Override
459 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800460 if (pInfo.isCustomWidget) {
461 mWidgetCleanupState = WIDGET_BOUND;
462 return;
463 }
464
Adam Cohened66b2b2012-01-23 17:28:51 -0800465 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700466 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
467 mWidgetLoadingId, pInfo, options)) {
468 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700469 }
Adam Cohen59400422014-03-05 18:07:04 -0800470
Adam Cohened66b2b2012-01-23 17:28:51 -0800471 }
472 };
473 post(mBindWidgetRunnable);
474
475 mInflateWidgetRunnable = new Runnable() {
476 @Override
477 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700478 if (mWidgetCleanupState != WIDGET_BOUND) {
479 return;
480 }
Adam Cohen59400422014-03-05 18:07:04 -0800481 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
482 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800483 info.boundWidget = hostView;
484 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800485 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700486 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
487 info.spanY, info, false);
488
489 // We want the first widget layout to be the correct size. This will be important
490 // for width size reporting to the AppWidgetManager.
491 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
492 unScaledSize[1]);
493 lp.x = lp.y = 0;
494 lp.customPosition = true;
495 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800496 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800497 }
498 };
499 post(mInflateWidgetRunnable);
500 }
501
502 @Override
503 public void onShortPress(View v) {
504 // We are anticipating a long press, and we use this time to load bind and instantiate
505 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700506 if (mCreateWidgetInfo != null) {
507 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
508 cleanupWidgetPreloading(false);
509 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800510 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700511 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800512 }
513
Adam Cohen0e56cc92012-05-11 15:57:05 -0700514 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
515 if (!widgetWasAdded) {
516 // If the widget was not added, we may need to do further cleanup.
517 PendingAddWidgetInfo info = mCreateWidgetInfo;
518 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700519
520 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700521 // We never did any preloading, so just remove pending callbacks to do so
522 removeCallbacks(mBindWidgetRunnable);
523 removeCallbacks(mInflateWidgetRunnable);
524 } else if (mWidgetCleanupState == WIDGET_BOUND) {
525 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800526 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700527 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
528 }
529
530 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700531 removeCallbacks(mInflateWidgetRunnable);
532 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700533 // 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
Adam Cohen0e56cc92012-05-11 15:57:05 -0700538 // The widget was inflated and added to the DragLayer -- remove it.
539 AppWidgetHostView widget = info.boundWidget;
540 mLauncher.getDragLayer().removeView(widget);
541 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800542 }
543 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
544 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700545 mCreateWidgetInfo = null;
546 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800547 }
548
Adam Cohen7a326642012-02-22 12:03:22 -0800549 @Override
550 public void cleanUpShortPress(View v) {
551 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700552 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800553 }
554 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800555
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700556 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800557 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700558 // Get the widget preview as the drag representation
559 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700560 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700561
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700562 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
563 // we abort the drag.
564 if (image.getDrawable() == null) {
565 mDraggingWidget = false;
566 return false;
567 }
568
Winson Chung4b576be2011-04-27 17:40:20 -0700569 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800570 Bitmap preview;
571 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800572 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100573 Point previewPadding = null;
574
Winson Chung1ed747a2011-05-03 16:18:34 -0700575 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700576 // This can happen in some weird cases involving multi-touch. We can't start dragging
577 // the widget if this is null, so we break out.
578 if (mCreateWidgetInfo == null) {
579 return false;
580 }
581
Adam Cohen1b36dc32012-02-13 19:27:37 -0800582 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
583 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700584 int spanX = createItemInfo.spanX;
585 int spanY = createItemInfo.spanY;
586 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
587 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700588
Winson Chung72d59842012-02-22 13:51:36 -0800589 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
590 float minScale = 1.25f;
Sunny Goyal4cad7532015-03-18 15:56:30 -0700591 int maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
Winson Chung72d59842012-02-22 13:51:36 -0800592
Michael Jurka05713af2013-01-23 12:39:24 +0100593 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700594 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
Sunny Goyal4cad7532015-03-18 15:56:30 -0700595 maxWidth, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100596
597 // Compare the size of the drag preview to the preview in the AppsCustomize tray
598 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100599 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100600 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
601
602 // The bitmap in the AppsCustomize tray is always the the same size, so there
603 // might be extra pixels around the preview itself - this accounts for that
604 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
605 int padding =
606 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
607 previewPadding = new Point(padding, 0);
608 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700609 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700610 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
611 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700612 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700613 createItemInfo.spanX = createItemInfo.spanY = 1;
614 }
Winson Chung4b576be2011-04-27 17:40:20 -0700615
Michael Jurka8c3339b2012-06-14 16:18:21 -0700616 // Don't clip alpha values for the drag outline if we're using the default widget preview
617 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
618 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700619
Winson Chung1120e032011-11-22 16:11:31 -0800620 // Save the preview for the outline generation, then dim the preview
621 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
622 false);
Winson Chung1120e032011-11-22 16:11:31 -0800623
Winson Chung4b576be2011-04-27 17:40:20 -0700624 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700625 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700626 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800627 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100628 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800629 outline.recycle();
630 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700631 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700632 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800633
Winson Chung4b576be2011-04-27 17:40:20 -0700634 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700636 if (!super.beginDragging(v)) return false;
637
Winson Chungb745afb2015-03-02 11:51:23 -0800638 if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700639 if (!beginDraggingWidget(v)) {
640 return false;
641 }
Winson Chungb745afb2015-03-02 11:51:23 -0800642 } else {
643 Log.e(TAG, "Unexpected dragging view: " + v);
Winson Chung4b576be2011-04-27 17:40:20 -0700644 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800645
646 // We delay entering spring-loaded mode slightly to make sure the UI
647 // thready is free of any work.
648 postDelayed(new Runnable() {
649 @Override
650 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800651 // We don't enter spring-loaded mode if the drag has been cancelled
652 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800653 // Go into spring loaded mode (must happen before we startDrag())
654 mLauncher.enterSpringLoadedDragMode();
655 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800656 }
Winson Chung72d59842012-02-22 13:51:36 -0800657 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800658
Winson Chung785d2eb2011-04-14 16:08:02 -0700659 return true;
660 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800661
Winson Chunga48487a2012-03-20 16:19:37 -0700662 /**
663 * Clean up after dragging.
664 *
665 * @param target where the item was dragged to (can be null if the item was flung)
666 */
667 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700668 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800669 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 // Exit spring loaded mode if we have not successfully dropped or have not handled the
671 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700672 mLauncher.exitSpringLoadedDragModeDelayed(true,
673 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700674 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700675 } else {
676 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 }
679
Winson Chung785d2eb2011-04-14 16:08:02 -0700680 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700681 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700682 if (getChildCount() > 0) {
683 return getChildAt(0);
684 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700685 return null;
686 }
687
688 @Override
689 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700690 mInTransition = true;
691 if (toWorkspace) {
692 cancelAllTasks();
693 }
694 }
695
696 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700697 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700698 }
699
700 @Override
701 public void onLauncherTransitionStep(Launcher l, float t) {
702 }
703
704 @Override
705 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
706 mInTransition = false;
707 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700708 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700709 }
710 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700711 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
712 r.run();
713 }
714 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700715 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700716 }
717
718 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700719 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
720 boolean success) {
721 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
722 if (isFlingToDelete) return;
723
724 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700725
726 // Display an error message if the drag failed due to there not being enough space on the
727 // target layout we were dropping on.
728 if (!success) {
729 boolean showOutOfSpaceMessage = false;
730 if (target instanceof Workspace) {
731 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
732 Workspace workspace = (Workspace) target;
733 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700734 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700735 if (layout != null) {
736 layout.calculateSpans(itemInfo);
737 showOutOfSpaceMessage =
738 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
739 }
740 }
Winson Chungfc79c802011-05-02 13:35:34 -0700741 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700742 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700743 }
Adam Cohen7a326642012-02-22 12:03:22 -0800744
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800745 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700746 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700747 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800748 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 }
750
Winson Chunga48487a2012-03-20 16:19:37 -0700751 @Override
752 public void onFlingToDeleteCompleted() {
753 // We just dismiss the drag when we fling, so cleanup here
754 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700755 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700756 mDraggingWidget = false;
757 }
758
759 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800760 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700761 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800762 }
763
Winson Chung7f0acdd2011-09-19 18:34:19 -0700764 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000765 public boolean supportsAppInfoDropTarget() {
766 return true;
767 }
768
769 @Override
770 public boolean supportsDeleteDropTarget() {
771 return false;
772 }
773
774 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800775 public float getIntrinsicIconScaleFactor() {
776 LauncherAppState app = LauncherAppState.getInstance();
777 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
778 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
779 }
780
781 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700782 protected void onDetachedFromWindow() {
783 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700784 cancelAllTasks();
785 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700786
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800787 @Override
788 public void trimMemory() {
789 super.trimMemory();
790 clearAllWidgetPages();
791 }
792
Michael Jurkae326f182011-11-21 14:05:46 -0800793 public void clearAllWidgetPages() {
794 cancelAllTasks();
795 int count = getChildCount();
796 for (int i = 0; i < count; i++) {
797 View v = getPageAt(i);
798 if (v instanceof PagedViewGridLayout) {
799 ((PagedViewGridLayout) v).removeAllViewsOnPage();
800 mDirtyPageContent.set(i, true);
801 }
802 }
803 }
804
Adam Cohen0cd3b642011-10-14 14:58:00 -0700805 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700806 // Clean up all the async tasks
807 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
808 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800809 AppsCustomizeAsyncTask task = iter.next();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700810 task.cancel(false);
811 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700812 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700813
814 // We've already preallocated the views for the data to load into, so clear them as well
815 View v = getPageAt(task.page);
816 if (v instanceof PagedViewGridLayout) {
817 ((PagedViewGridLayout) v).removeAllViewsOnPage();
818 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700819 }
Winson Chung83687b12012-04-25 16:01:01 -0700820 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700821 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700822 }
823
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200825 // Widgets appear to be cleared every time you leave, always force invalidate for them
826 if (mContentType != type || type == ContentType.Widgets) {
827 int page = (mContentType != type) ? 0 : getCurrentPage();
828 mContentType = type;
829 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700830 }
Winson Chungc58497e2013-09-03 17:48:37 -0700831 }
832
833 public ContentType getContentType() {
834 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700835 }
836
Adam Cohen0cd3b642011-10-14 14:58:00 -0700837 protected void snapToPage(int whichPage, int delta, int duration) {
838 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800839
840 // Update the thread priorities given the direction lookahead
841 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
842 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800843 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700844 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800845 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
846 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
847 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
848 } else {
849 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
850 }
851 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700852 }
853
Adam Cohen9bfdb762014-07-21 17:44:06 -0700854 public void setPageBackgroundsVisible(boolean visible) {
855 mPageBackgroundsVisible = visible;
856 int childCount = getChildCount();
857 for (int i = 0; i < childCount; ++i) {
858 Drawable bg = getChildAt(i).getBackground();
859 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700860 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700861 }
862 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700863 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700864
Winson Chungb44b5242011-06-13 11:32:14 -0700865 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800866 * A helper to return the priority for loading of the specified widget page.
867 */
868 private int getWidgetPageLoadPriority(int page) {
869 // If we are snapping to another page, use that index as the target page index
870 int toPage = mCurrentPage;
871 if (mNextPage > -1) {
872 toPage = mNextPage;
873 }
874
875 // We use the distance from the target page as an initial guess of priority, but if there
876 // are no pages of higher priority than the page specified, then bump up the priority of
877 // the specified page.
878 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
879 int minPageDiff = Integer.MAX_VALUE;
880 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800881 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700882 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800883 }
884
885 int rawPageDiff = Math.abs(page - toPage);
886 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
887 }
888 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700889 * Return the appropriate thread priority for loading for a given page (we give the current
890 * page much higher priority)
891 */
892 private int getThreadPriorityForPage(int page) {
893 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800894 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700895 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800896 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700897 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800898 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700899 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800900 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700901 }
902 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700903 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800904 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700905 return Math.max(0, pageDiff * sPageSleepDelay);
906 }
Winson Chungb44b5242011-06-13 11:32:14 -0700907 /**
908 * Creates and executes a new AsyncTask to load a page of widget previews.
909 */
910 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700911 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800912
Winson Chungb44b5242011-06-13 11:32:14 -0700913 // Prune all tasks that are no longer needed
914 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
915 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800916 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700917 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800918 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
919 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700920 task.cancel(false);
921 iter.remove();
922 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800923 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700924 }
925 }
926
Winson Chungf314b0e2011-08-16 11:54:27 -0700927 // 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 -0700928 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700929 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700930 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700931 @Override
932 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700933 try {
Winson Chung09945932011-09-20 14:22:40 -0700934 try {
935 Thread.sleep(sleepMs);
936 } catch (Exception e) {}
937 loadWidgetPreviewsInBackground(task, data);
938 } finally {
939 if (task.isCancelled()) {
940 data.cleanup(true);
941 }
942 }
Winson Chungb44b5242011-06-13 11:32:14 -0700943 }
944 },
945 new AsyncTaskCallback() {
946 @Override
947 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700948 mRunningTasks.remove(task);
949 if (task.isCancelled()) return;
950 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -0700951 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700952 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100953 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -0700954
955 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700956 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700957 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -0700958 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -0700959 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
960 mRunningTasks.add(t);
961 }
Winson Chungb44b5242011-06-13 11:32:14 -0700962
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 /*
964 * Widgets PagedView implementation
965 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700966 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -0700967 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700968 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -0700969 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
970 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700971
972 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
973 if (bg != null) {
974 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
975 layout.setBackground(bg);
976 }
Winson Chung63257c12011-05-05 17:06:13 -0700977 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700978 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700979
Michael Jurka038f9d82011-11-03 13:50:45 -0700980 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700981 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -0700982
Adam Cohen4e243a22014-08-10 18:30:55 -0700983 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
984
Winson Chungd2945262011-06-24 15:22:14 -0700985 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -0700986 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -0700987 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -0700988 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -0700989 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
990
Adam Cohen4e243a22014-08-10 18:30:55 -0700991 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700992
Winson Chunge4a647f2011-09-30 14:41:25 -0700993 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -0700994 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700995 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
996 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700997 }
998
Winson Chunge4a647f2011-09-30 14:41:25 -0700999 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001000 layout.setColumnCount(layout.getCellCountX());
1001 for (int i = 0; i < items.size(); ++i) {
1002 Object rawInfo = items.get(i);
1003 PendingAddItemInfo createItemInfo = null;
1004 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1005 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001006
1007 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001008 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001009 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1010 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001011
Adam Cohen59400422014-03-05 18:07:04 -08001012 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001013 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001014 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001015 } else if (rawInfo instanceof ResolveInfo) {
1016 // Fill in the shortcuts information
1017 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001018 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001019 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1020 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1021 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001022 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001023 widget.setTag(createItemInfo);
1024 }
Adam Cohen59400422014-03-05 18:07:04 -08001025
Winson Chunge4a647f2011-09-30 14:41:25 -07001026 widget.setOnClickListener(this);
1027 widget.setOnLongClickListener(this);
1028 widget.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -08001029 widget.setOnKeyListener(mKeyListener);
Winson Chunge4a647f2011-09-30 14:41:25 -07001030
1031 // Layout each widget
1032 int ix = i % mWidgetCountX;
1033 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001034
1035 if (ix > 0) {
1036 View border = widget.findViewById(R.id.left_border);
1037 border.setVisibility(View.VISIBLE);
1038 }
1039 if (ix < mWidgetCountX - 1) {
1040 View border = widget.findViewById(R.id.right_border);
1041 border.setVisibility(View.VISIBLE);
1042 }
1043
Winson Chunge4a647f2011-09-30 14:41:25 -07001044 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001045 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001046 GridLayout.spec(ix, GridLayout.TOP));
1047 lp.width = cellWidth;
1048 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001049 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001050 layout.addView(widget, lp);
1051 }
1052
Michael Jurka038f9d82011-11-03 13:50:45 -07001053 // wait until a call on onLayout to start loading, because
1054 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1055 // TODO: can we do a measure/layout immediately?
1056 layout.setOnLayoutListener(new Runnable() {
1057 public void run() {
1058 // Load the widget previews
1059 int maxPreviewWidth = cellWidth;
1060 int maxPreviewHeight = cellHeight;
1061 if (layout.getChildCount() > 0) {
1062 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1063 int[] maxSize = w.getPreviewSize();
1064 maxPreviewWidth = maxSize[0];
1065 maxPreviewHeight = maxSize[1];
1066 }
Michael Jurka05713af2013-01-23 12:39:24 +01001067
Sunny Goyal4cad7532015-03-18 15:56:30 -07001068 getWidgetPreviewLoader().setPreviewSize(maxPreviewWidth, maxPreviewHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001069 if (immediate) {
1070 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001071 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001072 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001073 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001074 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001075 if (mInTransition) {
1076 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1077 } else {
1078 prepareLoadWidgetPreviewsTask(page, items,
1079 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1080 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001081 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001082 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001083 }
1084 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001085 }
1086 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1087 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001088 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1089 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001090 if (task != null) {
1091 // Ensure that this task starts running at the correct priority
1092 task.syncThreadPriority();
1093 }
1094
1095 // Load each of the widget/shortcut previews
1096 ArrayList<Object> items = data.items;
1097 ArrayList<Bitmap> images = data.generatedImages;
1098 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001099 for (int i = 0; i < count; ++i) {
1100 if (task != null) {
1101 // Ensure we haven't been cancelled yet
1102 if (task.isCancelled()) break;
1103 // Before work on each item, ensure that this task is running at the correct
1104 // priority
1105 task.syncThreadPriority();
1106 }
1107
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001108 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001109 }
Winson Chungb44b5242011-06-13 11:32:14 -07001110 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001111
Winson Chung7bb37522013-10-28 11:07:57 -07001112 private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
1113 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001114 mDeferredSyncWidgetPageItems.add(data);
1115 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001117 try {
1118 int page = data.page;
1119 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001120
Michael Jurka39e5d172012-03-12 18:36:12 -07001121 ArrayList<Object> items = data.items;
1122 int count = items.size();
1123 for (int i = 0; i < count; ++i) {
1124 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1125 if (widget != null) {
1126 Bitmap preview = data.generatedImages.get(i);
1127 widget.applyPreview(new FastBitmapDrawable(preview), i);
1128 }
1129 }
Winson Chung68e4c642011-11-10 15:48:25 -08001130
Michael Jurka47639b92013-01-14 12:42:27 +01001131 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001132
1133 // Update all thread priorities
1134 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1135 while (iter.hasNext()) {
1136 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1137 int pageIndex = task.page;
1138 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1139 }
1140 } finally {
1141 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001142 }
Winson Chungb44b5242011-06-13 11:32:14 -07001143 }
Winson Chung46af2e82011-05-09 16:00:53 -07001144
Winson Chung785d2eb2011-04-14 16:08:02 -07001145 @Override
1146 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001147 disablePagedViewAnimations();
1148
Winson Chung785d2eb2011-04-14 16:08:02 -07001149 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001150 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001151
Adam Cohen0cd3b642011-10-14 14:58:00 -07001152 Context context = getContext();
Winson Chungb745afb2015-03-02 11:51:23 -08001153 if (mContentType == ContentType.Widgets) {
Winson Chungc58497e2013-09-03 17:48:37 -07001154 for (int j = 0; j < mNumWidgetPages; ++j) {
1155 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1156 mWidgetCountY);
1157 setupPage(layout);
1158 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1159 LayoutParams.MATCH_PARENT));
1160 }
1161 } else {
1162 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001163 }
1164
Winson Chungc58497e2013-09-03 17:48:37 -07001165 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001166 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001167
Winson Chung785d2eb2011-04-14 16:08:02 -07001168 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001169 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001170 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001171 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001172 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001173 Log.e(TAG, "Unexpected ContentType");
Winson Chung785d2eb2011-04-14 16:08:02 -07001174 }
1175 }
1176
Adam Cohen22f823d2011-09-01 17:22:18 -07001177 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1178 // it is in the z-order. This is important to insure touch events are handled correctly.
1179 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001180 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001181 }
1182
Adam Cohenae4f1552011-10-20 00:15:42 -07001183 @Override
1184 protected int indexToPage(int index) {
1185 return getChildCount() - index - 1;
1186 }
1187
Adam Cohen22f823d2011-09-01 17:22:18 -07001188 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1189 @Override
1190 protected void screenScrolled(int screenCenter) {
1191 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001192 enableHwLayersOnVisiblePages();
1193 }
1194
1195 private void enableHwLayersOnVisiblePages() {
1196 final int screenCount = getChildCount();
1197
1198 getVisiblePages(mTempVisiblePagesRange);
1199 int leftScreen = mTempVisiblePagesRange[0];
1200 int rightScreen = mTempVisiblePagesRange[1];
1201 int forceDrawScreen = -1;
1202 if (leftScreen == rightScreen) {
1203 // make sure we're caching at least two pages always
1204 if (rightScreen < screenCount - 1) {
1205 rightScreen++;
1206 forceDrawScreen = rightScreen;
1207 } else if (leftScreen > 0) {
1208 leftScreen--;
1209 forceDrawScreen = leftScreen;
1210 }
1211 } else {
1212 forceDrawScreen = leftScreen + 1;
1213 }
1214
1215 for (int i = 0; i < screenCount; i++) {
1216 final View layout = (View) getPageAt(i);
1217 if (!(leftScreen <= i && i <= rightScreen &&
1218 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1219 layout.setLayerType(LAYER_TYPE_NONE, null);
1220 }
1221 }
1222
Michael Jurka47639b92013-01-14 12:42:27 +01001223 for (int i = 0; i < screenCount; i++) {
1224 final View layout = (View) getPageAt(i);
1225
1226 if (leftScreen <= i && i <= rightScreen &&
1227 (i == forceDrawScreen || shouldDrawChild(layout))) {
1228 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1229 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1230 }
1231 }
1232 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001233 }
1234
1235 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001236 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001237 }
1238
Winson Chung785d2eb2011-04-14 16:08:02 -07001239 /**
1240 * Used by the parent to get the content width to set the tab bar to
1241 * @return
1242 */
1243 public int getPageContentWidth() {
1244 return mContentWidth;
1245 }
1246
Winson Chungb26f3d62011-06-02 10:49:29 -07001247 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001248 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001249 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001250 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001251 // We reset the save index when we change pages so that it will be recalculated on next
1252 // rotation
1253 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001254 }
1255
Winson Chung785d2eb2011-04-14 16:08:02 -07001256 /*
1257 * AllAppsView implementation
1258 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001259 public void setup(Launcher launcher, DragController dragController) {
1260 mLauncher = launcher;
1261 mDragController = dragController;
1262 }
Winson Chung9802ac92012-06-08 16:01:58 -07001263
1264 /**
Winson Chungb745afb2015-03-02 11:51:23 -08001265 * We should call thise method whenever the core data changes (mWidgets) so that we can
Winson Chung9802ac92012-06-08 16:01:58 -07001266 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1267 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1268 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1269 */
1270 private void invalidateOnDataChange() {
1271 if (!isDataReady()) {
1272 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1273 // request a layout to trigger the page data when ready.
1274 requestLayout();
1275 } else {
1276 cancelAllTasks();
1277 invalidatePageData();
1278 }
1279 }
1280
Winson Chung785d2eb2011-04-14 16:08:02 -07001281 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001282 // If we have reset, then we should not continue to restore the previous state
1283 mSaveInstanceStateItemIndex = -1;
1284
Winson Chungb745afb2015-03-02 11:51:23 -08001285 if (mContentType != ContentType.Widgets) {
1286 setContentType(ContentType.Widgets);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001287 }
Winson Chung649668f2012-01-10 13:07:16 -08001288
Adam Cohenb64d36e2011-10-17 21:48:02 -07001289 if (mCurrentPage != 0) {
1290 invalidatePageData(0);
1291 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001292 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001293
1294 private AppsCustomizeTabHost getTabHost() {
1295 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1296 }
1297
Winson Chung785d2eb2011-04-14 16:08:02 -07001298 public void dumpState() {
1299 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001300 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001301 }
Adam Cohen4e844012011-11-09 13:48:04 -08001302
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001304 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001306 for (Object i: list) {
1307 if (i instanceof AppWidgetProviderInfo) {
1308 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1309 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1310 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1311 + " initialLayout=" + info.initialLayout
1312 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1313 } else if (i instanceof ResolveInfo) {
1314 ResolveInfo info = (ResolveInfo) i;
1315 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1316 + info.icon);
1317 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 }
1319 }
Adam Cohen4e844012011-11-09 13:48:04 -08001320
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 public void surrender() {
1322 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1323 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001324
1325 // Stop all background tasks
1326 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001327 }
Winson Chung007c6982011-06-14 13:27:53 -07001328
Winson Chungb44b5242011-06-13 11:32:14 -07001329 /*
1330 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1331 * widget previews in the background with the AsyncTasks.
1332 */
Winson Chung68e4c642011-11-10 15:48:25 -08001333 final static int sLookBehindPageCount = 2;
1334 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001335 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001336 final int count = getChildCount();
1337 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1338 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1339 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001340 }
1341 protected int getAssociatedUpperPageBound(int page) {
1342 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001343 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1344 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1345 count - 1);
1346 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001347 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001348
Winson Chung6a0f57d2011-06-29 20:10:49 -07001349 protected String getCurrentPageDescription() {
1350 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1351 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001352 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001353
Winson Chungb745afb2015-03-02 11:51:23 -08001354 if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001355 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001356 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001357 } else {
1358 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001359 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001360
Michael Jurka8b805b12012-04-18 14:23:14 -07001361 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001362 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001363}