blob: 1955547c8c83618f67caafaf6d39716d11ab7d3a [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 Cohen091440a2015-03-18 14:16:05 -070048import com.android.launcher3.util.Thunk;
Adam Cohenc0dcf592011-06-01 15:30:43 -070049
50import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.Iterator;
Winson Chung785d2eb2011-04-14 16:08:02 -070052
Winson Chungb44b5242011-06-13 11:32:14 -070053/**
54 * A simple callback interface which also provides the results of the task.
55 */
56interface AsyncTaskCallback {
57 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
58}
Winson Chung4e076542011-06-23 13:04:10 -070059
Winson Chungb44b5242011-06-13 11:32:14 -070060/**
61 * The data needed to perform either of the custom AsyncTasks.
62 */
63class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070064 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080065 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070066 }
67
Michael Jurka038f9d82011-11-03 13:50:45 -070068 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010069 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070070 page = p;
71 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070072 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070073 maxImageWidth = cw;
74 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070075 doInBackgroundCallback = bgR;
76 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010077 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070078 }
Winson Chung09945932011-09-20 14:22:40 -070079 void cleanup(boolean cancelled) {
80 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070081 if (generatedImages != null) {
82 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010083 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010084 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070085 }
86 }
87 generatedImages.clear();
88 }
89 }
Winson Chungb44b5242011-06-13 11:32:14 -070090 int page;
91 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070092 ArrayList<Bitmap> sourceImages;
93 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -070094 int maxImageWidth;
95 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -070096 AsyncTaskCallback doInBackgroundCallback;
97 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +010098 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -070099}
Winson Chung4e076542011-06-23 13:04:10 -0700100
Winson Chungb44b5242011-06-13 11:32:14 -0700101/**
102 * A generic template for an async task used in AppsCustomize.
103 */
104class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700105 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700106 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700107 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700108 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700109 }
110 @Override
111 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
112 if (params.length != 1) return null;
113 // Load each of the widget previews in the background
114 params[0].doInBackgroundCallback.run(this, params[0]);
115 return params[0];
116 }
117 @Override
118 protected void onPostExecute(AsyncTaskPageData result) {
119 // All the widget previews are loaded, so we can just callback to inflate the page
120 result.postExecuteCallback.run(this, result);
121 }
122
123 void setThreadPriority(int p) {
124 threadPriority = p;
125 }
126 void syncThreadPriority() {
127 Process.setThreadPriority(threadPriority);
128 }
129
130 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700131 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700132 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700133 int threadPriority;
134}
Winson Chungb44b5242011-06-13 11:32:14 -0700135
136/**
137 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
138 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700139public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Sunny Goyal290800b2015-03-05 11:33:33 -0800140 View.OnClickListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -0700141 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700142 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700143
Sunny Goyalff572272014-07-23 13:58:07 -0700144 private static Rect sTmpRect = new Rect();
145
Winson Chung785d2eb2011-04-14 16:08:02 -0700146 /**
147 * The different content types that this paged view can show.
148 */
149 public enum ContentType {
Winson Chung6a26e5b2011-05-26 14:36:06 -0700150 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700151 }
Winson Chungb745afb2015-03-02 11:51:23 -0800152 private ContentType mContentType = ContentType.Widgets;
Winson Chung785d2eb2011-04-14 16:08:02 -0700153
154 // Refs
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk Launcher mLauncher;
Winson Chung785d2eb2011-04-14 16:08:02 -0700156 private DragController mDragController;
157 private final LayoutInflater mLayoutInflater;
158 private final PackageManager mPackageManager;
159
Winson Chung5afbf7b2011-07-25 11:53:08 -0700160 // Save and Restore
161 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700162
Winson Chung785d2eb2011-04-14 16:08:02 -0700163 // Content
Winson Chungd2945262011-06-24 15:22:14 -0700164 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700165
166 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -0700167 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700168
169 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -0700170 private int mContentWidth, mContentHeight;
Adam Cohen091440a2015-03-18 14:16:05 -0700171 @Thunk int mWidgetCountX, mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700172 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700173
Winson Chungb44b5242011-06-13 11:32:14 -0700174 // Previews & outlines
175 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800176 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700177
Sunny Goyal290800b2015-03-05 11:33:33 -0800178 private final PagedViewKeyListener mKeyListener = new PagedViewKeyListener();
179
Adam Cohened66b2b2012-01-23 17:28:51 -0800180 private Runnable mInflateWidgetRunnable = null;
181 private Runnable mBindWidgetRunnable = null;
182 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700183 static final int WIDGET_PRELOAD_PENDING = 0;
184 static final int WIDGET_BOUND = 1;
185 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800186 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
187 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800188 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800189 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700190 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800191
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700192 private Toast mWidgetInstructionToast;
193
Michael Jurka39e5d172012-03-12 18:36:12 -0700194 // Deferral of loading widget previews during launcher transitions
Adam Cohen091440a2015-03-18 14:16:05 -0700195 @Thunk boolean mInTransition;
Michael Jurka39e5d172012-03-12 18:36:12 -0700196 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
197 new ArrayList<AsyncTaskPageData>();
Adam Cohen091440a2015-03-18 14:16:05 -0700198 @Thunk ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
Michael Jurkaf6a96902012-06-06 11:48:13 -0700199 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700200
Michael Jurka05713af2013-01-23 12:39:24 +0100201 WidgetPreviewLoader mWidgetPreviewLoader;
202
Michael Jurkac402cd92013-05-20 15:49:32 +0200203 private boolean mInBulkBind;
204 private boolean mNeedToUpdatePageCountsAndInvalidateData;
205
Winson Chung785d2eb2011-04-14 16:08:02 -0700206 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
207 super(context, attrs);
208 mLayoutInflater = LayoutInflater.from(context);
209 mPackageManager = context.getPackageManager();
Winson Chungb745afb2015-03-02 11:51:23 -0800210 mWidgets = new ArrayList<>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400211 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb745afb2015-03-02 11:51:23 -0800212 mRunningTasks = new ArrayList<>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700213
214 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800215 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700216 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
217 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
218 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700219
Winson Chung1ed747a2011-05-03 16:18:34 -0700220 // The padding on the non-matched dimension for the default widget preview icons
221 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700222 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700223
224 // Unless otherwise specified this view is important for accessibility.
225 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
226 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
227 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700228 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700229 }
230
231 @Override
232 protected void init() {
233 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700234 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700235
236 Context context = getContext();
237 Resources r = context.getResources();
238 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
239 }
240
Winson Chungc58497e2013-09-03 17:48:37 -0700241 public void onFinishInflate() {
242 super.onFinishInflate();
243
244 LauncherAppState app = LauncherAppState.getInstance();
245 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
246 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
247 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
248 }
249
Winson Chung67ca7e42013-10-31 16:53:19 -0700250 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700251 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700252 }
253
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100254 WidgetPreviewLoader getWidgetPreviewLoader() {
255 if (mWidgetPreviewLoader == null) {
256 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
257 }
258 return mWidgetPreviewLoader;
259 }
260
Winson Chung5afbf7b2011-07-25 11:53:08 -0700261 /** Returns the item index of the center item on this page so that we can restore to this
262 * item index when we rotate. */
263 private int getMiddleComponentIndexOnCurrentPage() {
264 int i = -1;
265 if (getPageCount() > 0) {
266 int currentPage = getCurrentPage();
Winson Chungb745afb2015-03-02 11:51:23 -0800267 if (mContentType == ContentType.Widgets) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700268 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700269 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
270 int childCount = layout.getChildCount();
271 if (childCount > 0) {
Winson Chungb745afb2015-03-02 11:51:23 -0800272 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700273 }
Winson Chungc58497e2013-09-03 17:48:37 -0700274 } else {
275 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700276 }
277 }
278 return i;
279 }
280
281 /** Get the index of the item to restore to if we need to restore the current page. */
282 int getSaveInstanceStateIndex() {
283 if (mSaveInstanceStateItemIndex == -1) {
284 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
285 }
286 return mSaveInstanceStateItemIndex;
287 }
288
289 /** Returns the page in the current orientation which is expected to contain the specified
290 * item index. */
291 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700292 if (index < 0) return 0;
293
Winson Chungb745afb2015-03-02 11:51:23 -0800294 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
295 return index / numItemsPerPage;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700296 }
297
Winson Chung5afbf7b2011-07-25 11:53:08 -0700298 /** Restores the page for an item at the specified index */
299 void restorePageForIndex(int index) {
300 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700301 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700302 }
303
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700304 private void updatePageCounts() {
305 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
306 (float) (mWidgetCountX * mWidgetCountY));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700307 }
308
Winson Chungf0ea4d32011-06-06 14:27:16 -0700309 protected void onDataReady(int width, int height) {
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700310 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700311
Winson Chungdb1138b2011-06-30 14:39:35 -0700312 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700313 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
314 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700315
Adam Cohen6c5891a2014-07-09 23:53:15 -0700316 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700317 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800318 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800319 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700320
Adam Cohena00673c2014-08-14 12:57:28 -0700321 protected void onLayout(boolean changed, int l, int t, int r, int b) {
322 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700323
Winson Chungf0ea4d32011-06-06 14:27:16 -0700324 if (!isDataReady()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800325 if (!mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700326 post(new Runnable() {
327 // This code triggers requestLayout so must be posted outside of the
328 // layout pass.
329 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800330 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200331 setDataIsReady();
332 onDataReady(getMeasuredWidth(), getMeasuredHeight());
333 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700334 }
335 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700336 }
337 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700338 }
339
Michael Jurkac402cd92013-05-20 15:49:32 +0200340 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700341 LauncherAppState app = LauncherAppState.getInstance();
342 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
343
Winson Chung1ed747a2011-05-03 16:18:34 -0700344 // Get the list of widgets and shortcuts
345 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200346 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800347 if (o instanceof LauncherAppWidgetProviderInfo) {
348 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
349 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100350 continue;
351 }
Adam Cohen59400422014-03-05 18:07:04 -0800352
353 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200354 // Ensure that all widgets we show can be added on a workspace of this size
355 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
356 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
357 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
358 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700359 if (minSpanX <= (int) grid.numColumns &&
360 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200361 mWidgets.add(widget);
362 } else {
363 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
364 widget.minWidth + ", " + widget.minHeight + ")");
365 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700366 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200367 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
368 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700369 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800370 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200371 // just add shortcuts
372 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800373 }
374 }
Adam Cohen59400422014-03-05 18:07:04 -0800375
Michael Jurkac402cd92013-05-20 15:49:32 +0200376 updatePageCountsAndInvalidateData();
377 }
378
379 public void setBulkBind(boolean bulkBind) {
380 if (bulkBind) {
381 mInBulkBind = true;
382 } else {
383 mInBulkBind = false;
384 if (mNeedToUpdatePageCountsAndInvalidateData) {
385 updatePageCountsAndInvalidateData();
386 }
387 }
388 }
389
390 private void updatePageCountsAndInvalidateData() {
391 if (mInBulkBind) {
392 mNeedToUpdatePageCountsAndInvalidateData = true;
393 } else {
394 updatePageCounts();
395 invalidateOnDataChange();
396 mNeedToUpdatePageCountsAndInvalidateData = false;
397 }
Winson Chung4b576be2011-04-27 17:40:20 -0700398 }
399
400 @Override
401 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700402 // When we have exited all apps or are in transition, disregard clicks
Winson Chungb745afb2015-03-02 11:51:23 -0800403 if (!mLauncher.isWidgetsViewVisible()
Sunny Goyal508da152014-08-14 10:53:27 -0700404 || mLauncher.getWorkspace().isSwitchingState()
405 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700406
Sunny Goyal508da152014-08-14 10:53:27 -0700407 // Let the user know that they have to long press to add a widget
408 if (mWidgetInstructionToast != null) {
409 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700410 }
Sunny Goyal508da152014-08-14 10:53:27 -0700411 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
412 Toast.LENGTH_SHORT);
413 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700414 }
415
416 /*
417 * PagedViewWithDraggableItems implementation
418 */
419 @Override
420 protected void determineDraggingStart(android.view.MotionEvent ev) {
Winson Chung4b576be2011-04-27 17:40:20 -0700421 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700422
Sunny Goyalff572272014-07-23 13:58:07 -0700423 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700424 Bundle options = null;
425 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700426 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
427 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700428 info.componentName, null);
429
Sunny Goyalff572272014-07-23 13:58:07 -0700430 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700431 int xPaddingDips = (int) ((padding.left + padding.right) / density);
432 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
433
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700434 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700435 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700436 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700437 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700438 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700439 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700440 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700441 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700442 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700443 }
444 return options;
445 }
446
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700447 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800448 final LauncherAppWidgetProviderInfo pInfo = info.info;
449 final Bundle options = pInfo.isCustomWidget ? null :
450 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700451
Adam Cohened66b2b2012-01-23 17:28:51 -0800452 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700453 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800454 return;
455 }
456
Adam Cohen21a170b2012-05-30 15:17:06 -0700457 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800458 mBindWidgetRunnable = new Runnable() {
459 @Override
460 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800461 if (pInfo.isCustomWidget) {
462 mWidgetCleanupState = WIDGET_BOUND;
463 return;
464 }
465
Adam Cohened66b2b2012-01-23 17:28:51 -0800466 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700467 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
468 mWidgetLoadingId, pInfo, options)) {
469 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700470 }
Adam Cohen59400422014-03-05 18:07:04 -0800471
Adam Cohened66b2b2012-01-23 17:28:51 -0800472 }
473 };
474 post(mBindWidgetRunnable);
475
476 mInflateWidgetRunnable = new Runnable() {
477 @Override
478 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700479 if (mWidgetCleanupState != WIDGET_BOUND) {
480 return;
481 }
Adam Cohen59400422014-03-05 18:07:04 -0800482 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
483 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800484 info.boundWidget = hostView;
485 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800486 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700487 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
488 info.spanY, info, false);
489
490 // We want the first widget layout to be the correct size. This will be important
491 // for width size reporting to the AppWidgetManager.
492 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
493 unScaledSize[1]);
494 lp.x = lp.y = 0;
495 lp.customPosition = true;
496 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800497 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800498 }
499 };
500 post(mInflateWidgetRunnable);
501 }
502
503 @Override
504 public void onShortPress(View v) {
505 // We are anticipating a long press, and we use this time to load bind and instantiate
506 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700507 if (mCreateWidgetInfo != null) {
508 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
509 cleanupWidgetPreloading(false);
510 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800511 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700512 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800513 }
514
Adam Cohen0e56cc92012-05-11 15:57:05 -0700515 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
516 if (!widgetWasAdded) {
517 // If the widget was not added, we may need to do further cleanup.
518 PendingAddWidgetInfo info = mCreateWidgetInfo;
519 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700520
521 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700522 // We never did any preloading, so just remove pending callbacks to do so
523 removeCallbacks(mBindWidgetRunnable);
524 removeCallbacks(mInflateWidgetRunnable);
525 } else if (mWidgetCleanupState == WIDGET_BOUND) {
526 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800527 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700528 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
529 }
530
531 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700532 removeCallbacks(mInflateWidgetRunnable);
533 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700534 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800535 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700536 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
537 }
538
Adam Cohen0e56cc92012-05-11 15:57:05 -0700539 // The widget was inflated and added to the DragLayer -- remove it.
540 AppWidgetHostView widget = info.boundWidget;
541 mLauncher.getDragLayer().removeView(widget);
542 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800543 }
544 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
545 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700546 mCreateWidgetInfo = null;
547 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 }
549
Adam Cohen7a326642012-02-22 12:03:22 -0800550 @Override
551 public void cleanUpShortPress(View v) {
552 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700553 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800554 }
555 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800556
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700557 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800558 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700559 // Get the widget preview as the drag representation
560 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700561 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700562
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700563 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
564 // we abort the drag.
565 if (image.getDrawable() == null) {
566 mDraggingWidget = false;
567 return false;
568 }
569
Winson Chung4b576be2011-04-27 17:40:20 -0700570 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800571 Bitmap preview;
572 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800573 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100574 Point previewPadding = null;
575
Winson Chung1ed747a2011-05-03 16:18:34 -0700576 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700577 // This can happen in some weird cases involving multi-touch. We can't start dragging
578 // the widget if this is null, so we break out.
579 if (mCreateWidgetInfo == null) {
580 return false;
581 }
582
Adam Cohen1b36dc32012-02-13 19:27:37 -0800583 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
584 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700585 int spanX = createItemInfo.spanX;
586 int spanY = createItemInfo.spanY;
587 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
588 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700589
Winson Chung72d59842012-02-22 13:51:36 -0800590 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
591 float minScale = 1.25f;
Sunny Goyal4cad7532015-03-18 15:56:30 -0700592 int maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
Winson Chung72d59842012-02-22 13:51:36 -0800593
Michael Jurka05713af2013-01-23 12:39:24 +0100594 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700595 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
Sunny Goyal4cad7532015-03-18 15:56:30 -0700596 maxWidth, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100597
598 // Compare the size of the drag preview to the preview in the AppsCustomize tray
599 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100600 getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX));
Michael Jurka05713af2013-01-23 12:39:24 +0100601 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
602
603 // The bitmap in the AppsCustomize tray is always the the same size, so there
604 // might be extra pixels around the preview itself - this accounts for that
605 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
606 int padding =
607 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
608 previewPadding = new Point(padding, 0);
609 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700610 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700611 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
612 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700613 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700614 createItemInfo.spanX = createItemInfo.spanY = 1;
615 }
Winson Chung4b576be2011-04-27 17:40:20 -0700616
Michael Jurka8c3339b2012-06-14 16:18:21 -0700617 // Don't clip alpha values for the drag outline if we're using the default widget preview
618 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
619 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700620
Winson Chung1120e032011-11-22 16:11:31 -0800621 // Save the preview for the outline generation, then dim the preview
622 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
623 false);
Winson Chung1120e032011-11-22 16:11:31 -0800624
Winson Chung4b576be2011-04-27 17:40:20 -0700625 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700626 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700627 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800628 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100629 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800630 outline.recycle();
631 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700632 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700633 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800634
Winson Chung4b576be2011-04-27 17:40:20 -0700635 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700637 if (!super.beginDragging(v)) return false;
638
Winson Chungb745afb2015-03-02 11:51:23 -0800639 if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700640 if (!beginDraggingWidget(v)) {
641 return false;
642 }
Winson Chungb745afb2015-03-02 11:51:23 -0800643 } else {
644 Log.e(TAG, "Unexpected dragging view: " + v);
Winson Chung4b576be2011-04-27 17:40:20 -0700645 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800646
647 // We delay entering spring-loaded mode slightly to make sure the UI
648 // thready is free of any work.
649 postDelayed(new Runnable() {
650 @Override
651 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800652 // We don't enter spring-loaded mode if the drag has been cancelled
653 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800654 // Go into spring loaded mode (must happen before we startDrag())
655 mLauncher.enterSpringLoadedDragMode();
656 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 }
Winson Chung72d59842012-02-22 13:51:36 -0800658 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800659
Winson Chung785d2eb2011-04-14 16:08:02 -0700660 return true;
661 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800662
Winson Chunga48487a2012-03-20 16:19:37 -0700663 /**
664 * Clean up after dragging.
665 *
666 * @param target where the item was dragged to (can be null if the item was flung)
667 */
668 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700669 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800670 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 // Exit spring loaded mode if we have not successfully dropped or have not handled the
672 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700673 mLauncher.exitSpringLoadedDragModeDelayed(true,
674 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700675 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700676 } else {
677 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 }
680
Winson Chung785d2eb2011-04-14 16:08:02 -0700681 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700682 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700683 if (getChildCount() > 0) {
684 return getChildAt(0);
685 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700686 return null;
687 }
688
689 @Override
690 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700691 mInTransition = true;
692 if (toWorkspace) {
693 cancelAllTasks();
694 }
695 }
696
697 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700698 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700699 }
700
701 @Override
702 public void onLauncherTransitionStep(Launcher l, float t) {
703 }
704
705 @Override
706 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
707 mInTransition = false;
708 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
Winson Chung7bb37522013-10-28 11:07:57 -0700709 onSyncWidgetPageItems(d, false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700710 }
711 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700712 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
713 r.run();
714 }
715 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700716 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700717 }
718
719 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700720 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
721 boolean success) {
722 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
723 if (isFlingToDelete) return;
724
725 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700726
727 // Display an error message if the drag failed due to there not being enough space on the
728 // target layout we were dropping on.
729 if (!success) {
730 boolean showOutOfSpaceMessage = false;
731 if (target instanceof Workspace) {
732 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
733 Workspace workspace = (Workspace) target;
734 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700735 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700736 if (layout != null) {
737 layout.calculateSpans(itemInfo);
738 showOutOfSpaceMessage =
739 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
740 }
741 }
Winson Chungfc79c802011-05-02 13:35:34 -0700742 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700743 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700744 }
Adam Cohen7a326642012-02-22 12:03:22 -0800745
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800746 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700747 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700748 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800749 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700750 }
751
Winson Chunga48487a2012-03-20 16:19:37 -0700752 @Override
753 public void onFlingToDeleteCompleted() {
754 // We just dismiss the drag when we fling, so cleanup here
755 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700756 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700757 mDraggingWidget = false;
758 }
759
760 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800761 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700762 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800763 }
764
Winson Chung7f0acdd2011-09-19 18:34:19 -0700765 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000766 public boolean supportsAppInfoDropTarget() {
767 return true;
768 }
769
770 @Override
771 public boolean supportsDeleteDropTarget() {
772 return false;
773 }
774
775 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800776 public float getIntrinsicIconScaleFactor() {
777 LauncherAppState app = LauncherAppState.getInstance();
778 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
779 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
780 }
781
782 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700783 protected void onDetachedFromWindow() {
784 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700785 cancelAllTasks();
786 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700787
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800788 @Override
789 public void trimMemory() {
790 super.trimMemory();
791 clearAllWidgetPages();
792 }
793
Michael Jurkae326f182011-11-21 14:05:46 -0800794 public void clearAllWidgetPages() {
795 cancelAllTasks();
796 int count = getChildCount();
797 for (int i = 0; i < count; i++) {
798 View v = getPageAt(i);
799 if (v instanceof PagedViewGridLayout) {
800 ((PagedViewGridLayout) v).removeAllViewsOnPage();
801 mDirtyPageContent.set(i, true);
802 }
803 }
804 }
805
Adam Cohen0cd3b642011-10-14 14:58:00 -0700806 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700807 // Clean up all the async tasks
808 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
809 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800810 AppsCustomizeAsyncTask task = iter.next();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700811 task.cancel(false);
812 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700813 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700814
815 // We've already preallocated the views for the data to load into, so clear them as well
816 View v = getPageAt(task.page);
817 if (v instanceof PagedViewGridLayout) {
818 ((PagedViewGridLayout) v).removeAllViewsOnPage();
819 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700820 }
Winson Chung83687b12012-04-25 16:01:01 -0700821 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700822 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700823 }
824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200826 // Widgets appear to be cleared every time you leave, always force invalidate for them
827 if (mContentType != type || type == ContentType.Widgets) {
828 int page = (mContentType != type) ? 0 : getCurrentPage();
829 mContentType = type;
830 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700831 }
Winson Chungc58497e2013-09-03 17:48:37 -0700832 }
833
834 public ContentType getContentType() {
835 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700836 }
837
Adam Cohen0cd3b642011-10-14 14:58:00 -0700838 protected void snapToPage(int whichPage, int delta, int duration) {
839 super.snapToPage(whichPage, delta, duration);
Winson Chung68e4c642011-11-10 15:48:25 -0800840
841 // Update the thread priorities given the direction lookahead
842 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
843 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800844 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700845 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800846 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
847 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
848 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
849 } else {
850 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
851 }
852 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700853 }
854
Adam Cohen9bfdb762014-07-21 17:44:06 -0700855 public void setPageBackgroundsVisible(boolean visible) {
856 mPageBackgroundsVisible = visible;
857 int childCount = getChildCount();
858 for (int i = 0; i < childCount; ++i) {
859 Drawable bg = getChildAt(i).getBackground();
860 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700861 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700862 }
863 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700864 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700865
Winson Chungb44b5242011-06-13 11:32:14 -0700866 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800867 * A helper to return the priority for loading of the specified widget page.
868 */
869 private int getWidgetPageLoadPriority(int page) {
870 // If we are snapping to another page, use that index as the target page index
871 int toPage = mCurrentPage;
872 if (mNextPage > -1) {
873 toPage = mNextPage;
874 }
875
876 // We use the distance from the target page as an initial guess of priority, but if there
877 // are no pages of higher priority than the page specified, then bump up the priority of
878 // the specified page.
879 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
880 int minPageDiff = Integer.MAX_VALUE;
881 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800882 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700883 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800884 }
885
886 int rawPageDiff = Math.abs(page - toPage);
887 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
888 }
889 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700890 * Return the appropriate thread priority for loading for a given page (we give the current
891 * page much higher priority)
892 */
893 private int getThreadPriorityForPage(int page) {
894 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800895 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700896 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800897 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700898 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800899 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700900 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800901 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700902 }
903 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700904 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800905 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700906 return Math.max(0, pageDiff * sPageSleepDelay);
907 }
Winson Chungb44b5242011-06-13 11:32:14 -0700908 /**
909 * Creates and executes a new AsyncTask to load a page of widget previews.
910 */
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700912 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800913
Winson Chungb44b5242011-06-13 11:32:14 -0700914 // Prune all tasks that are no longer needed
915 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
916 while (iter.hasNext()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800917 AppsCustomizeAsyncTask task = iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700918 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800919 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
920 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700921 task.cancel(false);
922 iter.remove();
923 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800924 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700925 }
926 }
927
Winson Chungf314b0e2011-08-16 11:54:27 -0700928 // 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 -0700929 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700930 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700931 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700932 @Override
933 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700934 try {
Winson Chung09945932011-09-20 14:22:40 -0700935 try {
936 Thread.sleep(sleepMs);
937 } catch (Exception e) {}
938 loadWidgetPreviewsInBackground(task, data);
939 } finally {
940 if (task.isCancelled()) {
941 data.cleanup(true);
942 }
943 }
Winson Chungb44b5242011-06-13 11:32:14 -0700944 }
945 },
946 new AsyncTaskCallback() {
947 @Override
948 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700949 mRunningTasks.remove(task);
950 if (task.isCancelled()) return;
951 // do cleanup inside onSyncWidgetPageItems
Winson Chung7bb37522013-10-28 11:07:57 -0700952 onSyncWidgetPageItems(data, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700953 }
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100954 }, getWidgetPreviewLoader());
Winson Chungb44b5242011-06-13 11:32:14 -0700955
956 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700957 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700958 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -0700959 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -0700960 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
961 mRunningTasks.add(t);
962 }
Winson Chungb44b5242011-06-13 11:32:14 -0700963
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 /*
965 * Widgets PagedView implementation
966 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700967 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -0700968 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700969 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -0700970 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
971 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700972
973 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
974 if (bg != null) {
975 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
976 layout.setBackground(bg);
977 }
Winson Chung63257c12011-05-05 17:06:13 -0700978 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700979 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700980
Michael Jurka038f9d82011-11-03 13:50:45 -0700981 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700982 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -0700983
Adam Cohen4e243a22014-08-10 18:30:55 -0700984 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
985
Winson Chungd2945262011-06-24 15:22:14 -0700986 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -0700987 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -0700988 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -0700989 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -0700990 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
991
Adam Cohen4e243a22014-08-10 18:30:55 -0700992 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700993
Winson Chunge4a647f2011-09-30 14:41:25 -0700994 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -0700995 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700996 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
997 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700998 }
999
Winson Chunge4a647f2011-09-30 14:41:25 -07001000 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -07001001 layout.setColumnCount(layout.getCellCountX());
1002 for (int i = 0; i < items.size(); ++i) {
1003 Object rawInfo = items.get(i);
1004 PendingAddItemInfo createItemInfo = null;
1005 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1006 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -08001007
1008 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001009 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -08001010 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
1011 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001012
Adam Cohen59400422014-03-05 18:07:04 -08001013 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001014 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001015 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001016 } else if (rawInfo instanceof ResolveInfo) {
1017 // Fill in the shortcuts information
1018 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001019 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001020 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1021 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1022 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001023 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -07001024 widget.setTag(createItemInfo);
1025 }
Adam Cohen59400422014-03-05 18:07:04 -08001026
Winson Chunge4a647f2011-09-30 14:41:25 -07001027 widget.setOnClickListener(this);
1028 widget.setOnLongClickListener(this);
1029 widget.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -08001030 widget.setOnKeyListener(mKeyListener);
Winson Chunge4a647f2011-09-30 14:41:25 -07001031
1032 // Layout each widget
1033 int ix = i % mWidgetCountX;
1034 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -07001035
1036 if (ix > 0) {
1037 View border = widget.findViewById(R.id.left_border);
1038 border.setVisibility(View.VISIBLE);
1039 }
1040 if (ix < mWidgetCountX - 1) {
1041 View border = widget.findViewById(R.id.right_border);
1042 border.setVisibility(View.VISIBLE);
1043 }
1044
Winson Chunge4a647f2011-09-30 14:41:25 -07001045 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001046 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001047 GridLayout.spec(ix, GridLayout.TOP));
1048 lp.width = cellWidth;
1049 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001050 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001051 layout.addView(widget, lp);
1052 }
1053
Michael Jurka038f9d82011-11-03 13:50:45 -07001054 // wait until a call on onLayout to start loading, because
1055 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1056 // TODO: can we do a measure/layout immediately?
1057 layout.setOnLayoutListener(new Runnable() {
1058 public void run() {
1059 // Load the widget previews
1060 int maxPreviewWidth = cellWidth;
1061 int maxPreviewHeight = cellHeight;
1062 if (layout.getChildCount() > 0) {
1063 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1064 int[] maxSize = w.getPreviewSize();
1065 maxPreviewWidth = maxSize[0];
1066 maxPreviewHeight = maxSize[1];
1067 }
Michael Jurka05713af2013-01-23 12:39:24 +01001068
Sunny Goyal4cad7532015-03-18 15:56:30 -07001069 getWidgetPreviewLoader().setPreviewSize(maxPreviewWidth, maxPreviewHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001070 if (immediate) {
1071 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001072 maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader());
Michael Jurka038f9d82011-11-03 13:50:45 -07001073 loadWidgetPreviewsInBackground(null, data);
Winson Chung7bb37522013-10-28 11:07:57 -07001074 onSyncWidgetPageItems(data, immediate);
Michael Jurka038f9d82011-11-03 13:50:45 -07001075 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001076 if (mInTransition) {
1077 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1078 } else {
1079 prepareLoadWidgetPreviewsTask(page, items,
1080 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1081 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001082 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001083 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001084 }
1085 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001086 }
Adam Cohen091440a2015-03-18 14:16:05 -07001087 @Thunk void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
Winson Chungf314b0e2011-08-16 11:54:27 -07001088 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001089 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1090 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001091 if (task != null) {
1092 // Ensure that this task starts running at the correct priority
1093 task.syncThreadPriority();
1094 }
1095
1096 // Load each of the widget/shortcut previews
1097 ArrayList<Object> items = data.items;
1098 ArrayList<Bitmap> images = data.generatedImages;
1099 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001100 for (int i = 0; i < count; ++i) {
1101 if (task != null) {
1102 // Ensure we haven't been cancelled yet
1103 if (task.isCancelled()) break;
1104 // Before work on each item, ensure that this task is running at the correct
1105 // priority
1106 task.syncThreadPriority();
1107 }
1108
Michael Jurka9c5cc5a2014-01-09 14:59:22 +01001109 images.add(getWidgetPreviewLoader().getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001110 }
Winson Chungb44b5242011-06-13 11:32:14 -07001111 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001112
Adam Cohen091440a2015-03-18 14:16:05 -07001113 @Thunk void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) {
Winson Chung7bb37522013-10-28 11:07:57 -07001114 if (!immediatelySyncItems && mInTransition) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001115 mDeferredSyncWidgetPageItems.add(data);
1116 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001118 try {
1119 int page = data.page;
1120 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001121
Michael Jurka39e5d172012-03-12 18:36:12 -07001122 ArrayList<Object> items = data.items;
1123 int count = items.size();
1124 for (int i = 0; i < count; ++i) {
1125 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1126 if (widget != null) {
1127 Bitmap preview = data.generatedImages.get(i);
1128 widget.applyPreview(new FastBitmapDrawable(preview), i);
1129 }
1130 }
Winson Chung68e4c642011-11-10 15:48:25 -08001131
Michael Jurka47639b92013-01-14 12:42:27 +01001132 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001133
1134 // Update all thread priorities
1135 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1136 while (iter.hasNext()) {
1137 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1138 int pageIndex = task.page;
1139 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1140 }
1141 } finally {
1142 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001143 }
Winson Chungb44b5242011-06-13 11:32:14 -07001144 }
Winson Chung46af2e82011-05-09 16:00:53 -07001145
Winson Chung785d2eb2011-04-14 16:08:02 -07001146 @Override
1147 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -07001148 disablePagedViewAnimations();
1149
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001151 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001152
Adam Cohen0cd3b642011-10-14 14:58:00 -07001153 Context context = getContext();
Winson Chungb745afb2015-03-02 11:51:23 -08001154 if (mContentType == ContentType.Widgets) {
Winson Chungc58497e2013-09-03 17:48:37 -07001155 for (int j = 0; j < mNumWidgetPages; ++j) {
1156 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1157 mWidgetCountY);
1158 setupPage(layout);
1159 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
1160 LayoutParams.MATCH_PARENT));
1161 }
1162 } else {
1163 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -07001164 }
1165
Winson Chungc58497e2013-09-03 17:48:37 -07001166 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -07001167 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001168
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001170 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -07001171 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001172 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -07001173 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08001174 Log.e(TAG, "Unexpected ContentType");
Winson Chung785d2eb2011-04-14 16:08:02 -07001175 }
1176 }
1177
Adam Cohen22f823d2011-09-01 17:22:18 -07001178 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1179 // it is in the z-order. This is important to insure touch events are handled correctly.
1180 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001181 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001182 }
1183
Adam Cohenae4f1552011-10-20 00:15:42 -07001184 @Override
1185 protected int indexToPage(int index) {
1186 return getChildCount() - index - 1;
1187 }
1188
Adam Cohen22f823d2011-09-01 17:22:18 -07001189 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1190 @Override
1191 protected void screenScrolled(int screenCenter) {
1192 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +01001193 enableHwLayersOnVisiblePages();
1194 }
1195
1196 private void enableHwLayersOnVisiblePages() {
1197 final int screenCount = getChildCount();
1198
1199 getVisiblePages(mTempVisiblePagesRange);
1200 int leftScreen = mTempVisiblePagesRange[0];
1201 int rightScreen = mTempVisiblePagesRange[1];
1202 int forceDrawScreen = -1;
1203 if (leftScreen == rightScreen) {
1204 // make sure we're caching at least two pages always
1205 if (rightScreen < screenCount - 1) {
1206 rightScreen++;
1207 forceDrawScreen = rightScreen;
1208 } else if (leftScreen > 0) {
1209 leftScreen--;
1210 forceDrawScreen = leftScreen;
1211 }
1212 } else {
1213 forceDrawScreen = leftScreen + 1;
1214 }
1215
1216 for (int i = 0; i < screenCount; i++) {
1217 final View layout = (View) getPageAt(i);
1218 if (!(leftScreen <= i && i <= rightScreen &&
1219 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1220 layout.setLayerType(LAYER_TYPE_NONE, null);
1221 }
1222 }
1223
Michael Jurka47639b92013-01-14 12:42:27 +01001224 for (int i = 0; i < screenCount; i++) {
1225 final View layout = (View) getPageAt(i);
1226
1227 if (leftScreen <= i && i <= rightScreen &&
1228 (i == forceDrawScreen || shouldDrawChild(layout))) {
1229 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1230 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1231 }
1232 }
1233 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001234 }
1235
1236 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -07001237 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001238 }
1239
Winson Chung785d2eb2011-04-14 16:08:02 -07001240 /**
1241 * Used by the parent to get the content width to set the tab bar to
1242 * @return
1243 */
1244 public int getPageContentWidth() {
1245 return mContentWidth;
1246 }
1247
Winson Chungb26f3d62011-06-02 10:49:29 -07001248 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001249 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001250 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001251 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001252 // We reset the save index when we change pages so that it will be recalculated on next
1253 // rotation
1254 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001255 }
1256
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 /*
1258 * AllAppsView implementation
1259 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001260 public void setup(Launcher launcher, DragController dragController) {
1261 mLauncher = launcher;
1262 mDragController = dragController;
1263 }
Winson Chung9802ac92012-06-08 16:01:58 -07001264
1265 /**
Winson Chungb745afb2015-03-02 11:51:23 -08001266 * We should call thise method whenever the core data changes (mWidgets) so that we can
Winson Chung9802ac92012-06-08 16:01:58 -07001267 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1268 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1269 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1270 */
1271 private void invalidateOnDataChange() {
1272 if (!isDataReady()) {
1273 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1274 // request a layout to trigger the page data when ready.
1275 requestLayout();
1276 } else {
1277 cancelAllTasks();
1278 invalidatePageData();
1279 }
1280 }
1281
Winson Chung785d2eb2011-04-14 16:08:02 -07001282 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001283 // If we have reset, then we should not continue to restore the previous state
1284 mSaveInstanceStateItemIndex = -1;
1285
Winson Chungb745afb2015-03-02 11:51:23 -08001286 if (mContentType != ContentType.Widgets) {
1287 setContentType(ContentType.Widgets);
Adam Cohenb64d36e2011-10-17 21:48:02 -07001288 }
Winson Chung649668f2012-01-10 13:07:16 -08001289
Adam Cohenb64d36e2011-10-17 21:48:02 -07001290 if (mCurrentPage != 0) {
1291 invalidatePageData(0);
1292 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001293 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001294
1295 private AppsCustomizeTabHost getTabHost() {
1296 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1297 }
1298
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 public void dumpState() {
1300 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001301 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001302 }
Adam Cohen4e844012011-11-09 13:48:04 -08001303
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001305 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001306 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001307 for (Object i: list) {
1308 if (i instanceof AppWidgetProviderInfo) {
1309 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1310 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1311 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1312 + " initialLayout=" + info.initialLayout
1313 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1314 } else if (i instanceof ResolveInfo) {
1315 ResolveInfo info = (ResolveInfo) i;
1316 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1317 + info.icon);
1318 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001319 }
1320 }
Adam Cohen4e844012011-11-09 13:48:04 -08001321
Winson Chung785d2eb2011-04-14 16:08:02 -07001322 public void surrender() {
1323 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1324 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001325
1326 // Stop all background tasks
1327 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001328 }
Winson Chung007c6982011-06-14 13:27:53 -07001329
Winson Chungb44b5242011-06-13 11:32:14 -07001330 /*
1331 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1332 * widget previews in the background with the AsyncTasks.
1333 */
Winson Chung68e4c642011-11-10 15:48:25 -08001334 final static int sLookBehindPageCount = 2;
1335 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001336 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001337 final int count = getChildCount();
1338 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1339 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1340 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001341 }
1342 protected int getAssociatedUpperPageBound(int page) {
1343 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001344 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1345 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1346 count - 1);
1347 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001348 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001349
Winson Chung6a0f57d2011-06-29 20:10:49 -07001350 protected String getCurrentPageDescription() {
1351 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1352 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001353 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001354
Winson Chungb745afb2015-03-02 11:51:23 -08001355 if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001356 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001357 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001358 } else {
1359 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001360 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001361
Michael Jurka8b805b12012-04-18 14:23:14 -07001362 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001363 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001364}