blob: be0dd8aaf7f65c836a2aae820bfc9be6702b9c1b [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010034import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Rect;
36import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070037import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070038import android.os.Build;
39import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070040import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080044import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.LayoutInflater;
46import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.R;
55import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080074 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070075 }
76
Michael Jurka038f9d82011-11-03 13:50:45 -070077 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010078 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070079 page = p;
80 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070081 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070082 maxImageWidth = cw;
83 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070084 doInBackgroundCallback = bgR;
85 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010086 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070087 }
Winson Chung09945932011-09-20 14:22:40 -070088 void cleanup(boolean cancelled) {
89 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070090 if (generatedImages != null) {
91 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010092 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010093 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070094 }
95 }
96 generatedImages.clear();
97 }
98 }
Winson Chungb44b5242011-06-13 11:32:14 -070099 int page;
100 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700101 ArrayList<Bitmap> sourceImages;
102 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700103 int maxImageWidth;
104 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 AsyncTaskCallback doInBackgroundCallback;
106 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100107 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700108}
Winson Chung4e076542011-06-23 13:04:10 -0700109
Winson Chungb44b5242011-06-13 11:32:14 -0700110/**
111 * A generic template for an async task used in AppsCustomize.
112 */
113class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700114 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700115 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700116 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700117 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700118 }
119 @Override
120 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
121 if (params.length != 1) return null;
122 // Load each of the widget previews in the background
123 params[0].doInBackgroundCallback.run(this, params[0]);
124 return params[0];
125 }
126 @Override
127 protected void onPostExecute(AsyncTaskPageData result) {
128 // All the widget previews are loaded, so we can just callback to inflate the page
129 result.postExecuteCallback.run(this, result);
130 }
131
132 void setThreadPriority(int p) {
133 threadPriority = p;
134 }
135 void syncThreadPriority() {
136 Process.setThreadPriority(threadPriority);
137 }
138
139 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700140 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 int threadPriority;
143}
Winson Chungb44b5242011-06-13 11:32:14 -0700144
145/**
146 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
147 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700148public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700149 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700150 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
151 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700152 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700153
154 /**
155 * The different content types that this paged view can show.
156 */
157 public enum ContentType {
158 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700159 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700160 }
161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800170 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700173 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700174 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700175
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800177 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 private int mClingFocusedX;
179 private int mClingFocusedY;
180
Winson Chung1ed747a2011-05-03 16:18:34 -0700181 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700182 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700183 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700184
185 // Dimens
186 private int mContentWidth;
Winson Chung6032e7e2011-11-08 15:47:17 -0800187 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700188 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700189 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700191 private int mNumAppsPages;
192 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700193
Adam Cohen22f823d2011-09-01 17:22:18 -0700194 // Relating to the scroll and overscroll effects
195 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700197 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700198 private static float TRANSITION_PIVOT = 0.65f;
199 private static float TRANSITION_MAX_ROTATION = 22;
200 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700201 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700202 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700203
Adam Cohen947dc542013-06-06 22:43:33 -0700204 public static boolean DISABLE_ALL_APPS = true;
205
Winson Chungb44b5242011-06-13 11:32:14 -0700206 // Previews & outlines
207 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800208 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700209
Adam Cohened66b2b2012-01-23 17:28:51 -0800210 private Runnable mInflateWidgetRunnable = null;
211 private Runnable mBindWidgetRunnable = null;
212 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700213 static final int WIDGET_PRELOAD_PENDING = 0;
214 static final int WIDGET_BOUND = 1;
215 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800216 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
217 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800218 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800219 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800220
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700221 private Toast mWidgetInstructionToast;
222
Michael Jurka39e5d172012-03-12 18:36:12 -0700223 // Deferral of loading widget previews during launcher transitions
224 private boolean mInTransition;
225 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
226 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700227 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
228 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700229
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700230 private Rect mTmpRect = new Rect();
231
Michael Jurkadac85912012-05-18 15:04:49 -0700232 // Used for drawing shortcut previews
233 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
234 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
235 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
236
237 // Used for drawing widget previews
238 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
239 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
240 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
241 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
242
Michael Jurka05713af2013-01-23 12:39:24 +0100243 WidgetPreviewLoader mWidgetPreviewLoader;
244
Michael Jurkac402cd92013-05-20 15:49:32 +0200245 private boolean mInBulkBind;
246 private boolean mNeedToUpdatePageCountsAndInvalidateData;
247
Winson Chung785d2eb2011-04-14 16:08:02 -0700248 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
249 super(context, attrs);
250 mLayoutInflater = LayoutInflater.from(context);
251 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700252 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253 mWidgets = new ArrayList<Object>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400254 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700255 mCanvas = new Canvas();
256 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700257
258 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800259 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
260 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
261 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262 mWidgetWidthGap =
263 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
264 mWidgetHeightGap =
265 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700266 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
267 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700268 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
269 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700270 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700271 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700272
Winson Chung1ed747a2011-05-03 16:18:34 -0700273 // The padding on the non-matched dimension for the default widget preview icons
274 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700275 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700276
277 // Unless otherwise specified this view is important for accessibility.
278 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
279 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
280 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700281 }
282
283 @Override
284 protected void init() {
285 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700286 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700287
288 Context context = getContext();
289 Resources r = context.getResources();
290 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
291 }
292
Winson Chung5afbf7b2011-07-25 11:53:08 -0700293 /** Returns the item index of the center item on this page so that we can restore to this
294 * item index when we rotate. */
295 private int getMiddleComponentIndexOnCurrentPage() {
296 int i = -1;
297 if (getPageCount() > 0) {
298 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700299 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700300 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700301 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
302 int numItemsPerPage = mCellCountX * mCellCountY;
303 int childCount = childrenLayout.getChildCount();
304 if (childCount > 0) {
305 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700306 }
307 } else {
308 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700309 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700310 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
311 int childCount = layout.getChildCount();
312 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700313 i = numApps +
314 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
315 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700316 }
317 }
318 return i;
319 }
320
321 /** Get the index of the item to restore to if we need to restore the current page. */
322 int getSaveInstanceStateIndex() {
323 if (mSaveInstanceStateItemIndex == -1) {
324 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
325 }
326 return mSaveInstanceStateItemIndex;
327 }
328
329 /** Returns the page in the current orientation which is expected to contain the specified
330 * item index. */
331 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 if (index < 0) return 0;
333
334 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700335 int numItemsPerPage = mCellCountX * mCellCountY;
336 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700337 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700338 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700339 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
340 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 }
342
Winson Chung5afbf7b2011-07-25 11:53:08 -0700343 /** Restores the page for an item at the specified index */
344 void restorePageForIndex(int index) {
345 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700346 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700347 }
348
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700349 private void updatePageCounts() {
350 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
351 (float) (mWidgetCountX * mWidgetCountY));
352 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
353 }
354
Winson Chungf0ea4d32011-06-06 14:27:16 -0700355 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100356 if (mWidgetPreviewLoader == null) {
357 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
358 }
359
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 // Note that we transpose the counts in portrait so that we get a similar layout
361 boolean isLandscape = getResources().getConfiguration().orientation ==
362 Configuration.ORIENTATION_LANDSCAPE;
363 int maxCellCountX = Integer.MAX_VALUE;
364 int maxCellCountY = Integer.MAX_VALUE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400365 if (LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700366 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
367 LauncherModel.getCellCountY());
368 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
369 LauncherModel.getCellCountX());
370 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800371 if (mMaxAppCellCountX > -1) {
372 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
373 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200374 // Temp hack for now: only use the max cell count Y for widget layout
375 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800376 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200377 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800378 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700379
380 // Now that the data is ready, we can calculate the content width, the number of cells to
381 // use for each page
382 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
383 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
384 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
385 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
386 mCellCountX = mWidgetSpacingLayout.getCellCountX();
387 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700388 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700389
Winson Chungdb1138b2011-06-30 14:39:35 -0700390 // Force a measure to update recalculate the gaps
391 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
392 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200393 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700394 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700395 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700396
Michael Jurkae326f182011-11-21 14:05:46 -0800397 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
398 final boolean hostIsTransitioning = host.isTransitioning();
399
Adam Cohen0cd3b642011-10-14 14:58:00 -0700400 // Restore the page
401 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800402 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700403
Winson Chung3f4e1422011-11-17 14:58:51 -0800404 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
405 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
406 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800407 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800408 post(new Runnable() {
409 @Override
410 public void run() {
411 showAllAppsCling();
412 }
413 });
414 }
415 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700416
Winson Chung3f4e1422011-11-17 14:58:51 -0800417 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700418 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800419 mHasShownAllAppsCling = true;
420 // Calculate the position for the cling punch through
421 int[] offset = new int[2];
422 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
423 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
424 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800425 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800426 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
427 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800428 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800429 mLauncher.showFirstRunAllAppsCling(pos);
430 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700431 }
432
433 @Override
434 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
435 int width = MeasureSpec.getSize(widthMeasureSpec);
436 int height = MeasureSpec.getSize(heightMeasureSpec);
437 if (!isDataReady()) {
Adam Cohen947dc542013-06-06 22:43:33 -0700438 if ((DISABLE_ALL_APPS || !mApps.isEmpty()) && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700439 setDataIsReady();
440 setMeasuredDimension(width, height);
441 onDataReady(width, height);
442 }
443 }
444
445 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
446 }
447
Michael Jurkac402cd92013-05-20 15:49:32 +0200448 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700449 // Get the list of widgets and shortcuts
450 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200451 for (Object o : widgetsAndShortcuts) {
452 if (o instanceof AppWidgetProviderInfo) {
453 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
454 widget.label = widget.label.trim();
455 if (widget.minWidth > 0 && widget.minHeight > 0) {
456 // Ensure that all widgets we show can be added on a workspace of this size
457 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
458 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
459 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
460 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
461 if (minSpanX <= LauncherModel.getCellCountX() &&
Adam Cohen336d4912012-04-13 17:57:11 -0700462 minSpanY <= LauncherModel.getCellCountY()) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200463 mWidgets.add(widget);
464 } else {
465 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
466 widget.minWidth + ", " + widget.minHeight + ")");
467 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700468 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200469 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
470 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700471 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800472 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200473 // just add shortcuts
474 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800475 }
476 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200477 updatePageCountsAndInvalidateData();
478 }
479
480 public void setBulkBind(boolean bulkBind) {
481 if (bulkBind) {
482 mInBulkBind = true;
483 } else {
484 mInBulkBind = false;
485 if (mNeedToUpdatePageCountsAndInvalidateData) {
486 updatePageCountsAndInvalidateData();
487 }
488 }
489 }
490
491 private void updatePageCountsAndInvalidateData() {
492 if (mInBulkBind) {
493 mNeedToUpdatePageCountsAndInvalidateData = true;
494 } else {
495 updatePageCounts();
496 invalidateOnDataChange();
497 mNeedToUpdatePageCountsAndInvalidateData = false;
498 }
Winson Chung4b576be2011-04-27 17:40:20 -0700499 }
500
501 @Override
502 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700503 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700504 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700505 mLauncher.getWorkspace().isSwitchingState()) return;
506
Winson Chung4b576be2011-04-27 17:40:20 -0700507 if (v instanceof PagedViewIcon) {
508 // Animate some feedback to the click
509 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800510
511 // Lock the drawable state to pressed until we return to Launcher
512 if (mPressedIcon != null) {
513 mPressedIcon.lockDrawableState();
514 }
Winson Chungc7450e32012-04-17 17:34:08 -0700515
Winson Chung18f41f82012-05-09 13:28:10 -0700516 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
517 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
518 // when Launcher resumes and we are still in AllApps.
519 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700520 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
521
Winson Chung4b576be2011-04-27 17:40:20 -0700522 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700523 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700524 if (mWidgetInstructionToast != null) {
525 mWidgetInstructionToast.cancel();
526 }
527 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
528 Toast.LENGTH_SHORT);
529 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700530
Winson Chungd2e87b32011-06-02 10:53:07 -0700531 // Create a little animation to show that the widget can move
532 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
533 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700534 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
535 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700536 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700537 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700538 tydAnim.setDuration(100);
539 bounce.play(tyuAnim).before(tydAnim);
540 bounce.setInterpolator(new AccelerateInterpolator());
541 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700542 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700543 }
544
Winson Chungc6f10b92011-11-14 11:39:07 -0800545 public boolean onKey(View v, int keyCode, KeyEvent event) {
546 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
547 }
548
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 /*
550 * PagedViewWithDraggableItems implementation
551 */
552 @Override
553 protected void determineDraggingStart(android.view.MotionEvent ev) {
554 // Disable dragging by pulling an app down for now.
555 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700556
Winson Chung4b576be2011-04-27 17:40:20 -0700557 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700558 mLauncher.getWorkspace().onDragStartedWithItem(v);
559 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700560 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700561
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700562 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
563 Bundle options = null;
564 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
565 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700566 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
567 info.componentName, null);
568
569 float density = getResources().getDisplayMetrics().density;
570 int xPaddingDips = (int) ((padding.left + padding.right) / density);
571 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
572
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700573 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700574 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
575 mTmpRect.left - xPaddingDips);
576 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
577 mTmpRect.top - yPaddingDips);
578 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
579 mTmpRect.right - xPaddingDips);
580 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
581 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700582 }
583 return options;
584 }
585
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700586 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800587 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700588 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
589
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700591 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 return;
593 }
594
Adam Cohen21a170b2012-05-30 15:17:06 -0700595 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800596 mBindWidgetRunnable = new Runnable() {
597 @Override
598 public void run() {
599 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700600 // Options will be null for platforms with JB or lower, so this serves as an
601 // SDK level check.
602 if (options == null) {
603 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
604 mWidgetLoadingId, info.componentName)) {
605 mWidgetCleanupState = WIDGET_BOUND;
606 }
607 } else {
608 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
609 mWidgetLoadingId, info.componentName, options)) {
610 mWidgetCleanupState = WIDGET_BOUND;
611 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700612 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800613 }
614 };
615 post(mBindWidgetRunnable);
616
617 mInflateWidgetRunnable = new Runnable() {
618 @Override
619 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700620 if (mWidgetCleanupState != WIDGET_BOUND) {
621 return;
622 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 AppWidgetHostView hostView = mLauncher.
624 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800625 info.boundWidget = hostView;
626 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800627 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700628 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
629 info.spanY, info, false);
630
631 // We want the first widget layout to be the correct size. This will be important
632 // for width size reporting to the AppWidgetManager.
633 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
634 unScaledSize[1]);
635 lp.x = lp.y = 0;
636 lp.customPosition = true;
637 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800638 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 }
640 };
641 post(mInflateWidgetRunnable);
642 }
643
644 @Override
645 public void onShortPress(View v) {
646 // We are anticipating a long press, and we use this time to load bind and instantiate
647 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700648 if (mCreateWidgetInfo != null) {
649 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
650 cleanupWidgetPreloading(false);
651 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800652 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700653 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800654 }
655
Adam Cohen0e56cc92012-05-11 15:57:05 -0700656 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
657 if (!widgetWasAdded) {
658 // If the widget was not added, we may need to do further cleanup.
659 PendingAddWidgetInfo info = mCreateWidgetInfo;
660 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700661
662 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700663 // We never did any preloading, so just remove pending callbacks to do so
664 removeCallbacks(mBindWidgetRunnable);
665 removeCallbacks(mInflateWidgetRunnable);
666 } else if (mWidgetCleanupState == WIDGET_BOUND) {
667 // Delete the widget id which was allocated
668 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700669 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
670 }
671
672 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700673 removeCallbacks(mInflateWidgetRunnable);
674 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700675 // Delete the widget id which was allocated
676 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700677 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
678 }
679
Adam Cohen0e56cc92012-05-11 15:57:05 -0700680 // The widget was inflated and added to the DragLayer -- remove it.
681 AppWidgetHostView widget = info.boundWidget;
682 mLauncher.getDragLayer().removeView(widget);
683 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 }
685 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
686 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700687 mCreateWidgetInfo = null;
688 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 }
690
Adam Cohen7a326642012-02-22 12:03:22 -0800691 @Override
692 public void cleanUpShortPress(View v) {
693 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700694 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800695 }
696 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800697
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700698 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800699 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700700 // Get the widget preview as the drag representation
701 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700702 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700703
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700704 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
705 // we abort the drag.
706 if (image.getDrawable() == null) {
707 mDraggingWidget = false;
708 return false;
709 }
710
Winson Chung4b576be2011-04-27 17:40:20 -0700711 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800712 Bitmap preview;
713 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800714 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100715 Point previewPadding = null;
716
Winson Chung1ed747a2011-05-03 16:18:34 -0700717 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700718 // This can happen in some weird cases involving multi-touch. We can't start dragging
719 // the widget if this is null, so we break out.
720 if (mCreateWidgetInfo == null) {
721 return false;
722 }
723
Adam Cohen1b36dc32012-02-13 19:27:37 -0800724 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
725 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700726 int spanX = createItemInfo.spanX;
727 int spanY = createItemInfo.spanY;
728 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
729 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700730
Winson Chung72d59842012-02-22 13:51:36 -0800731 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
732 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700733 int maxWidth, maxHeight;
734 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
735 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800736
Michael Jurka05713af2013-01-23 12:39:24 +0100737 int[] previewSizeBeforeScale = new int[1];
738
739 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
740 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
741 maxWidth, maxHeight, null, previewSizeBeforeScale);
742
743 // Compare the size of the drag preview to the preview in the AppsCustomize tray
744 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
745 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
746 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
747
748 // The bitmap in the AppsCustomize tray is always the the same size, so there
749 // might be extra pixels around the preview itself - this accounts for that
750 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
751 int padding =
752 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
753 previewPadding = new Point(padding, 0);
754 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700755 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700756 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
757 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
758 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
759 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
760
Winson Chung1120e032011-11-22 16:11:31 -0800761 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700762 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100763 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700764 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700765 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700766 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700767 createItemInfo.spanX = createItemInfo.spanY = 1;
768 }
Winson Chung4b576be2011-04-27 17:40:20 -0700769
Michael Jurka8c3339b2012-06-14 16:18:21 -0700770 // Don't clip alpha values for the drag outline if we're using the default widget preview
771 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
772 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700773
Winson Chung1120e032011-11-22 16:11:31 -0800774 // Save the preview for the outline generation, then dim the preview
775 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
776 false);
Winson Chung1120e032011-11-22 16:11:31 -0800777
Winson Chung4b576be2011-04-27 17:40:20 -0700778 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700779 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700780 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800781 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100782 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800783 outline.recycle();
784 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700785 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700786 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800787
Winson Chung4b576be2011-04-27 17:40:20 -0700788 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700790 if (!super.beginDragging(v)) return false;
791
792 if (v instanceof PagedViewIcon) {
793 beginDraggingApplication(v);
794 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700795 if (!beginDraggingWidget(v)) {
796 return false;
797 }
Winson Chung4b576be2011-04-27 17:40:20 -0700798 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800799
800 // We delay entering spring-loaded mode slightly to make sure the UI
801 // thready is free of any work.
802 postDelayed(new Runnable() {
803 @Override
804 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800805 // We don't enter spring-loaded mode if the drag has been cancelled
806 if (mLauncher.getDragController().isDragging()) {
807 // Dismiss the cling
808 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800809
Adam Cohen1b36dc32012-02-13 19:27:37 -0800810 // Reset the alpha on the dragged icon before we drag
811 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800812
Adam Cohen1b36dc32012-02-13 19:27:37 -0800813 // Go into spring loaded mode (must happen before we startDrag())
814 mLauncher.enterSpringLoadedDragMode();
815 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 }
Winson Chung72d59842012-02-22 13:51:36 -0800817 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800818
Winson Chung785d2eb2011-04-14 16:08:02 -0700819 return true;
820 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800821
Winson Chunga48487a2012-03-20 16:19:37 -0700822 /**
823 * Clean up after dragging.
824 *
825 * @param target where the item was dragged to (can be null if the item was flung)
826 */
827 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700828 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700829 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700830 // Exit spring loaded mode if we have not successfully dropped or have not handled the
831 // drop in Workspace
832 mLauncher.exitSpringLoadedDragMode();
833 }
Winson Chung4b919f82012-05-01 10:44:08 -0700834 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 }
836
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700838 public View getContent() {
839 return null;
840 }
841
842 @Override
843 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700844 mInTransition = true;
845 if (toWorkspace) {
846 cancelAllTasks();
847 }
848 }
849
850 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700851 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700852 }
853
854 @Override
855 public void onLauncherTransitionStep(Launcher l, float t) {
856 }
857
858 @Override
859 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
860 mInTransition = false;
861 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
862 onSyncWidgetPageItems(d);
863 }
864 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700865 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
866 r.run();
867 }
868 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700869 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700870 }
871
872 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700873 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
874 boolean success) {
875 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
876 if (isFlingToDelete) return;
877
878 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700879
880 // Display an error message if the drag failed due to there not being enough space on the
881 // target layout we were dropping on.
882 if (!success) {
883 boolean showOutOfSpaceMessage = false;
884 if (target instanceof Workspace) {
885 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
886 Workspace workspace = (Workspace) target;
887 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700888 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700889 if (layout != null) {
890 layout.calculateSpans(itemInfo);
891 showOutOfSpaceMessage =
892 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
893 }
894 }
Winson Chungfc79c802011-05-02 13:35:34 -0700895 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700896 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700897 }
Adam Cohen7a326642012-02-22 12:03:22 -0800898
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800899 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700900 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700901 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800902 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700903 }
904
Winson Chunga48487a2012-03-20 16:19:37 -0700905 @Override
906 public void onFlingToDeleteCompleted() {
907 // We just dismiss the drag when we fling, so cleanup here
908 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700909 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700910 mDraggingWidget = false;
911 }
912
913 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800914 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700915 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800916 }
917
Winson Chung7f0acdd2011-09-19 18:34:19 -0700918 @Override
919 protected void onDetachedFromWindow() {
920 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700921 cancelAllTasks();
922 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700923
Michael Jurkae326f182011-11-21 14:05:46 -0800924 public void clearAllWidgetPages() {
925 cancelAllTasks();
926 int count = getChildCount();
927 for (int i = 0; i < count; i++) {
928 View v = getPageAt(i);
929 if (v instanceof PagedViewGridLayout) {
930 ((PagedViewGridLayout) v).removeAllViewsOnPage();
931 mDirtyPageContent.set(i, true);
932 }
933 }
934 }
935
Adam Cohen0cd3b642011-10-14 14:58:00 -0700936 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700937 // Clean up all the async tasks
938 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
939 while (iter.hasNext()) {
940 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
941 task.cancel(false);
942 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700943 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700944
945 // We've already preallocated the views for the data to load into, so clear them as well
946 View v = getPageAt(task.page);
947 if (v instanceof PagedViewGridLayout) {
948 ((PagedViewGridLayout) v).removeAllViewsOnPage();
949 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700950 }
Winson Chung83687b12012-04-25 16:01:01 -0700951 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700952 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700953 }
954
Winson Chung785d2eb2011-04-14 16:08:02 -0700955 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700956 if (type == ContentType.Widgets) {
957 invalidatePageData(mNumAppsPages, true);
958 } else if (type == ContentType.Applications) {
959 invalidatePageData(0, true);
960 }
Winson Chungb44b5242011-06-13 11:32:14 -0700961 }
962
Adam Cohen0cd3b642011-10-14 14:58:00 -0700963 protected void snapToPage(int whichPage, int delta, int duration) {
964 super.snapToPage(whichPage, delta, duration);
965 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800966
967 // Update the thread priorities given the direction lookahead
968 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
969 while (iter.hasNext()) {
970 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700971 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800972 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
973 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
974 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
975 } else {
976 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
977 }
978 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700979 }
980
981 private void updateCurrentTab(int currentPage) {
982 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800983 if (tabHost != null) {
984 String tag = tabHost.getCurrentTabTag();
985 if (tag != null) {
986 if (currentPage >= mNumAppsPages &&
987 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
988 tabHost.setCurrentTabFromContent(ContentType.Widgets);
989 } else if (currentPage < mNumAppsPages &&
990 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
991 tabHost.setCurrentTabFromContent(ContentType.Applications);
992 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700993 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700994 }
995 }
996
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 /*
998 * Apps PagedView implementation
999 */
Winson Chung63257c12011-05-05 17:06:13 -07001000 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
1001 int childCount = layout.getChildCount();
1002 for (int i = 0; i < childCount; ++i) {
1003 layout.getChildAt(i).setVisibility(visibility);
1004 }
1005 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001006 private void setupPage(PagedViewCellLayout layout) {
1007 layout.setCellCount(mCellCountX, mCellCountY);
1008 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1009 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1010 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1011
Winson Chung63257c12011-05-05 17:06:13 -07001012 // Note: We force a measure here to get around the fact that when we do layout calculations
1013 // immediately after syncing, we don't have a proper width. That said, we already know the
1014 // expected page width, so we can actually optimize by hiding all the TextView-based
1015 // children that are expensive to measure, and let that happen naturally later.
1016 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001017 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001018 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001019 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001020 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001021 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001022 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001023
Winson Chungf314b0e2011-08-16 11:54:27 -07001024 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001026 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 int numCells = mCellCountX * mCellCountY;
1028 int startIndex = page * numCells;
1029 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001030 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001031
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001033 ArrayList<Object> items = new ArrayList<Object>();
1034 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 for (int i = startIndex; i < endIndex; ++i) {
1036 ApplicationInfo info = mApps.get(i);
1037 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1038 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001039 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 icon.setOnClickListener(this);
1041 icon.setOnLongClickListener(this);
1042 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001043 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001044
1045 int index = i - startIndex;
1046 int x = index % mCellCountX;
1047 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001048 if (isRtl) {
1049 x = mCellCountX - x - 1;
1050 }
Winson Chung6a70e9f2011-05-17 16:24:49 -07001051 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001052
1053 items.add(info);
1054 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001056
Michael Jurka47639b92013-01-14 12:42:27 +01001057 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001058 }
Winson Chungb44b5242011-06-13 11:32:14 -07001059
1060 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001061 * A helper to return the priority for loading of the specified widget page.
1062 */
1063 private int getWidgetPageLoadPriority(int page) {
1064 // If we are snapping to another page, use that index as the target page index
1065 int toPage = mCurrentPage;
1066 if (mNextPage > -1) {
1067 toPage = mNextPage;
1068 }
1069
1070 // We use the distance from the target page as an initial guess of priority, but if there
1071 // are no pages of higher priority than the page specified, then bump up the priority of
1072 // the specified page.
1073 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1074 int minPageDiff = Integer.MAX_VALUE;
1075 while (iter.hasNext()) {
1076 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001077 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001078 }
1079
1080 int rawPageDiff = Math.abs(page - toPage);
1081 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1082 }
1083 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001084 * Return the appropriate thread priority for loading for a given page (we give the current
1085 * page much higher priority)
1086 */
1087 private int getThreadPriorityForPage(int page) {
1088 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001089 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001090 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001091 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001092 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001093 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001094 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001095 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001096 }
1097 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001098 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001099 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001100 return Math.max(0, pageDiff * sPageSleepDelay);
1101 }
Winson Chungb44b5242011-06-13 11:32:14 -07001102 /**
1103 * Creates and executes a new AsyncTask to load a page of widget previews.
1104 */
1105 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001106 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001107
Winson Chungb44b5242011-06-13 11:32:14 -07001108 // Prune all tasks that are no longer needed
1109 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1110 while (iter.hasNext()) {
1111 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001112 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001113 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1114 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001115 task.cancel(false);
1116 iter.remove();
1117 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001118 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001119 }
1120 }
1121
Winson Chungf314b0e2011-08-16 11:54:27 -07001122 // 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 -07001123 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001124 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001125 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001126 @Override
1127 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001128 try {
Winson Chung09945932011-09-20 14:22:40 -07001129 try {
1130 Thread.sleep(sleepMs);
1131 } catch (Exception e) {}
1132 loadWidgetPreviewsInBackground(task, data);
1133 } finally {
1134 if (task.isCancelled()) {
1135 data.cleanup(true);
1136 }
1137 }
Winson Chungb44b5242011-06-13 11:32:14 -07001138 }
1139 },
1140 new AsyncTaskCallback() {
1141 @Override
1142 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001143 mRunningTasks.remove(task);
1144 if (task.isCancelled()) return;
1145 // do cleanup inside onSyncWidgetPageItems
1146 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001147 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001148 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001149
1150 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001151 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001152 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001153 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001154 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1155 mRunningTasks.add(t);
1156 }
Winson Chungb44b5242011-06-13 11:32:14 -07001157
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 /*
1159 * Widgets PagedView implementation
1160 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001161 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001162 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1163 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001164
1165 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001166 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001167 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1168 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001170 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001171 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001172
Michael Jurka038f9d82011-11-03 13:50:45 -07001173 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001174 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001175
Winson Chungd2945262011-06-24 15:22:14 -07001176 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001177 final ArrayList<Object> items = new ArrayList<Object>();
1178 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1179 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001180 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1182 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001183 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001184
Winson Chunge4a647f2011-09-30 14:41:25 -07001185 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001186 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001187 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1188 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001189 }
1190
Winson Chunge4a647f2011-09-30 14:41:25 -07001191 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001192 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001193 layout.setColumnCount(layout.getCellCountX());
1194 for (int i = 0; i < items.size(); ++i) {
1195 Object rawInfo = items.get(i);
1196 PendingAddItemInfo createItemInfo = null;
1197 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1198 R.layout.apps_customize_widget, layout, false);
1199 if (rawInfo instanceof AppWidgetProviderInfo) {
1200 // Fill in the widget information
1201 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1202 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001203
1204 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001205 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001206 createItemInfo.spanX = spanXY[0];
1207 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001208 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001209 createItemInfo.minSpanX = minSpanXY[0];
1210 createItemInfo.minSpanY = minSpanXY[1];
1211
Michael Jurka3f4e0702013-02-05 11:21:28 +01001212 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001213 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001214 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001215 } else if (rawInfo instanceof ResolveInfo) {
1216 // Fill in the shortcuts information
1217 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001218 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001219 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1220 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1221 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001222 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001223 widget.setTag(createItemInfo);
1224 }
1225 widget.setOnClickListener(this);
1226 widget.setOnLongClickListener(this);
1227 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001228 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001229
1230 // Layout each widget
1231 int ix = i % mWidgetCountX;
1232 int iy = i / mWidgetCountX;
1233 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001234 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001235 GridLayout.spec(ix, GridLayout.TOP));
1236 lp.width = cellWidth;
1237 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001238 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001239 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1240 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1241 layout.addView(widget, lp);
1242 }
1243
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 // wait until a call on onLayout to start loading, because
1245 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1246 // TODO: can we do a measure/layout immediately?
1247 layout.setOnLayoutListener(new Runnable() {
1248 public void run() {
1249 // Load the widget previews
1250 int maxPreviewWidth = cellWidth;
1251 int maxPreviewHeight = cellHeight;
1252 if (layout.getChildCount() > 0) {
1253 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1254 int[] maxSize = w.getPreviewSize();
1255 maxPreviewWidth = maxSize[0];
1256 maxPreviewHeight = maxSize[1];
1257 }
Michael Jurka05713af2013-01-23 12:39:24 +01001258
Michael Jurka3f4e0702013-02-05 11:21:28 +01001259 mWidgetPreviewLoader.setPreviewSize(
1260 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001261 if (immediate) {
1262 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001263 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001264 loadWidgetPreviewsInBackground(null, data);
1265 onSyncWidgetPageItems(data);
1266 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001267 if (mInTransition) {
1268 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1269 } else {
1270 prepareLoadWidgetPreviewsTask(page, items,
1271 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1272 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001273 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001274 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001275 }
1276 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001277 }
1278 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1279 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001280 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1281 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001282 if (task != null) {
1283 // Ensure that this task starts running at the correct priority
1284 task.syncThreadPriority();
1285 }
1286
1287 // Load each of the widget/shortcut previews
1288 ArrayList<Object> items = data.items;
1289 ArrayList<Bitmap> images = data.generatedImages;
1290 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001291 for (int i = 0; i < count; ++i) {
1292 if (task != null) {
1293 // Ensure we haven't been cancelled yet
1294 if (task.isCancelled()) break;
1295 // Before work on each item, ensure that this task is running at the correct
1296 // priority
1297 task.syncThreadPriority();
1298 }
1299
Michael Jurka05713af2013-01-23 12:39:24 +01001300 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001301 }
Winson Chungb44b5242011-06-13 11:32:14 -07001302 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001303
Winson Chungb44b5242011-06-13 11:32:14 -07001304 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001305 if (mInTransition) {
1306 mDeferredSyncWidgetPageItems.add(data);
1307 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001308 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001309 try {
1310 int page = data.page;
1311 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001312
Michael Jurka39e5d172012-03-12 18:36:12 -07001313 ArrayList<Object> items = data.items;
1314 int count = items.size();
1315 for (int i = 0; i < count; ++i) {
1316 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1317 if (widget != null) {
1318 Bitmap preview = data.generatedImages.get(i);
1319 widget.applyPreview(new FastBitmapDrawable(preview), i);
1320 }
1321 }
Winson Chung68e4c642011-11-10 15:48:25 -08001322
Michael Jurka47639b92013-01-14 12:42:27 +01001323 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001324
1325 // Update all thread priorities
1326 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1327 while (iter.hasNext()) {
1328 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1329 int pageIndex = task.page;
1330 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1331 }
1332 } finally {
1333 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001334 }
Winson Chungb44b5242011-06-13 11:32:14 -07001335 }
Winson Chung46af2e82011-05-09 16:00:53 -07001336
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 @Override
1338 public void syncPages() {
1339 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001340 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001341
Adam Cohen0cd3b642011-10-14 14:58:00 -07001342 Context context = getContext();
1343 for (int j = 0; j < mNumWidgetPages; ++j) {
1344 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1345 mWidgetCountY);
1346 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001347 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001348 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001349 }
1350
Adam Cohen0cd3b642011-10-14 14:58:00 -07001351 for (int i = 0; i < mNumAppsPages; ++i) {
1352 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1353 setupPage(layout);
1354 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001355 }
1356 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001357
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001359 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001360 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001361 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001362 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001363 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001364 }
1365 }
1366
Adam Cohen22f823d2011-09-01 17:22:18 -07001367 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1368 // it is in the z-order. This is important to insure touch events are handled correctly.
1369 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001370 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001371 }
1372
Adam Cohenae4f1552011-10-20 00:15:42 -07001373 @Override
1374 protected int indexToPage(int index) {
1375 return getChildCount() - index - 1;
1376 }
1377
Adam Cohen22f823d2011-09-01 17:22:18 -07001378 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1379 @Override
1380 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001381 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001382 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001383
1384 for (int i = 0; i < getChildCount(); i++) {
1385 View v = getPageAt(i);
1386 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001387 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001388
Winson Chung52aee602013-01-30 12:01:02 -08001389 float interpolatedProgress;
1390 float translationX;
1391 float maxScrollProgress = Math.max(0, scrollProgress);
1392 float minScrollProgress = Math.min(0, scrollProgress);
1393
1394 if (isRtl) {
1395 translationX = maxScrollProgress * v.getMeasuredWidth();
1396 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1397 } else {
1398 translationX = minScrollProgress * v.getMeasuredWidth();
1399 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1400 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001401 float scale = (1 - interpolatedProgress) +
1402 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001403
Adam Cohen2591f6a2011-10-25 14:36:40 -07001404 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001405 if (isRtl && (scrollProgress > 0)) {
1406 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1407 } else if (!isRtl && (scrollProgress < 0)) {
1408 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001409 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001410 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001411 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1412 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001413
1414 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1415 int pageWidth = v.getMeasuredWidth();
1416 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001417
1418 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001419 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1420 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1421 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1422
1423 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001424 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001425 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001426 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1427 scale = 1.0f;
1428 alpha = 1.0f;
1429 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001430 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001431 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001432 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001433 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001434 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1435 scale = 1.0f;
1436 alpha = 1.0f;
1437 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001438 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001439 } else {
1440 v.setPivotY(pageHeight / 2.0f);
1441 v.setPivotX(pageWidth / 2.0f);
1442 v.setRotationY(0f);
1443 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001444 }
1445
1446 v.setTranslationX(translationX);
1447 v.setScaleX(scale);
1448 v.setScaleY(scale);
1449 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001450
1451 // If the view has 0 alpha, we set it to be invisible so as to prevent
1452 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001453 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001454 v.setVisibility(INVISIBLE);
1455 } else if (v.getVisibility() != VISIBLE) {
1456 v.setVisibility(VISIBLE);
1457 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 }
1459 }
Michael Jurka47639b92013-01-14 12:42:27 +01001460
1461 enableHwLayersOnVisiblePages();
1462 }
1463
1464 private void enableHwLayersOnVisiblePages() {
1465 final int screenCount = getChildCount();
1466
1467 getVisiblePages(mTempVisiblePagesRange);
1468 int leftScreen = mTempVisiblePagesRange[0];
1469 int rightScreen = mTempVisiblePagesRange[1];
1470 int forceDrawScreen = -1;
1471 if (leftScreen == rightScreen) {
1472 // make sure we're caching at least two pages always
1473 if (rightScreen < screenCount - 1) {
1474 rightScreen++;
1475 forceDrawScreen = rightScreen;
1476 } else if (leftScreen > 0) {
1477 leftScreen--;
1478 forceDrawScreen = leftScreen;
1479 }
1480 } else {
1481 forceDrawScreen = leftScreen + 1;
1482 }
1483
1484 for (int i = 0; i < screenCount; i++) {
1485 final View layout = (View) getPageAt(i);
1486 if (!(leftScreen <= i && i <= rightScreen &&
1487 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1488 layout.setLayerType(LAYER_TYPE_NONE, null);
1489 }
1490 }
1491
Michael Jurka47639b92013-01-14 12:42:27 +01001492 for (int i = 0; i < screenCount; i++) {
1493 final View layout = (View) getPageAt(i);
1494
1495 if (leftScreen <= i && i <= rightScreen &&
1496 (i == forceDrawScreen || shouldDrawChild(layout))) {
1497 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1498 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1499 }
1500 }
1501 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001502 }
1503
1504 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001505 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001506 }
1507
Winson Chung785d2eb2011-04-14 16:08:02 -07001508 /**
1509 * Used by the parent to get the content width to set the tab bar to
1510 * @return
1511 */
1512 public int getPageContentWidth() {
1513 return mContentWidth;
1514 }
1515
Winson Chungb26f3d62011-06-02 10:49:29 -07001516 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001517 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001518 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001519 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001520 // We reset the save index when we change pages so that it will be recalculated on next
1521 // rotation
1522 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001523 }
1524
Winson Chung785d2eb2011-04-14 16:08:02 -07001525 /*
1526 * AllAppsView implementation
1527 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001528 public void setup(Launcher launcher, DragController dragController) {
1529 mLauncher = launcher;
1530 mDragController = dragController;
1531 }
Winson Chung9802ac92012-06-08 16:01:58 -07001532
1533 /**
1534 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1535 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1536 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1537 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1538 */
1539 private void invalidateOnDataChange() {
1540 if (!isDataReady()) {
1541 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1542 // request a layout to trigger the page data when ready.
1543 requestLayout();
1544 } else {
1545 cancelAllTasks();
1546 invalidatePageData();
1547 }
1548 }
1549
Winson Chung785d2eb2011-04-14 16:08:02 -07001550 public void setApps(ArrayList<ApplicationInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001551 if (!DISABLE_ALL_APPS) {
1552 mApps = list;
1553 Collections.sort(mApps, LauncherModel.getAppNameComparator());
1554 updatePageCountsAndInvalidateData();
1555 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001556 }
1557 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1558 // We add it in place, in alphabetical order
1559 int count = list.size();
1560 for (int i = 0; i < count; ++i) {
1561 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001562 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001563 if (index < 0) {
1564 mApps.add(-(index + 1), info);
1565 }
1566 }
1567 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 public void addApps(ArrayList<ApplicationInfo> list) {
Adam Cohen947dc542013-06-06 22:43:33 -07001569 if (!DISABLE_ALL_APPS) {
1570 addAppsWithoutInvalidate(list);
1571 updatePageCountsAndInvalidateData();
1572 } else {
1573 // TODO: Maybe put them somewhere else?
Adam Cohene25af792013-06-06 23:08:25 -07001574 mLauncher.getHotseat().addAppsToAllAppsFolder(list);
Adam Cohen947dc542013-06-06 22:43:33 -07001575 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001576 }
1577 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1578 ComponentName removeComponent = item.intent.getComponent();
1579 int length = list.size();
1580 for (int i = 0; i < length; ++i) {
1581 ApplicationInfo info = list.get(i);
1582 if (info.intent.getComponent().equals(removeComponent)) {
1583 return i;
1584 }
1585 }
1586 return -1;
1587 }
1588 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1589 // loop through all the apps and remove apps that have the same component
1590 int length = list.size();
1591 for (int i = 0; i < length; ++i) {
1592 ApplicationInfo info = list.get(i);
1593 int removeIndex = findAppByComponent(mApps, info);
1594 if (removeIndex > -1) {
1595 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 }
1597 }
1598 }
Winson Chung83892cc2013-05-01 16:53:33 -07001599 public void removeApps(ArrayList<ApplicationInfo> appInfos) {
1600 removeAppsWithoutInvalidate(appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +02001601 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001602 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001603 public void updateApps(ArrayList<ApplicationInfo> list) {
1604 // We remove and re-add the updated applications list because it's properties may have
1605 // changed (ie. the title), and this will ensure that the items will be in their proper
1606 // place in the list.
Adam Cohen947dc542013-06-06 22:43:33 -07001607 if (!DISABLE_ALL_APPS) {
1608 removeAppsWithoutInvalidate(list);
1609 addAppsWithoutInvalidate(list);
1610 updatePageCountsAndInvalidateData();
1611 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001612 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001613
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001615 // If we have reset, then we should not continue to restore the previous state
1616 mSaveInstanceStateItemIndex = -1;
1617
Adam Cohenb64d36e2011-10-17 21:48:02 -07001618 AppsCustomizeTabHost tabHost = getTabHost();
1619 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001620 if (tag != null) {
1621 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1622 tabHost.setCurrentTabFromContent(ContentType.Applications);
1623 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001624 }
Winson Chung649668f2012-01-10 13:07:16 -08001625
Adam Cohenb64d36e2011-10-17 21:48:02 -07001626 if (mCurrentPage != 0) {
1627 invalidatePageData(0);
1628 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001629 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001630
1631 private AppsCustomizeTabHost getTabHost() {
1632 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1633 }
1634
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 public void dumpState() {
1636 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001637 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1638 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001639 }
Adam Cohen4e844012011-11-09 13:48:04 -08001640
Winson Chung785d2eb2011-04-14 16:08:02 -07001641 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001642 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001643 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001644 for (Object i: list) {
1645 if (i instanceof AppWidgetProviderInfo) {
1646 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1647 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1648 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1649 + " initialLayout=" + info.initialLayout
1650 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1651 } else if (i instanceof ResolveInfo) {
1652 ResolveInfo info = (ResolveInfo) i;
1653 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1654 + info.icon);
1655 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001656 }
1657 }
Adam Cohen4e844012011-11-09 13:48:04 -08001658
Winson Chung785d2eb2011-04-14 16:08:02 -07001659 public void surrender() {
1660 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1661 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001662
1663 // Stop all background tasks
1664 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001665 }
Winson Chung007c6982011-06-14 13:27:53 -07001666
Winson Chunge4e50662012-01-23 14:45:13 -08001667 @Override
1668 public void iconPressed(PagedViewIcon icon) {
1669 // Reset the previously pressed icon and store a reference to the pressed icon so that
1670 // we can reset it on return to Launcher (in Launcher.onResume())
1671 if (mPressedIcon != null) {
1672 mPressedIcon.resetDrawableState();
1673 }
1674 mPressedIcon = icon;
1675 }
1676
1677 public void resetDrawableState() {
1678 if (mPressedIcon != null) {
1679 mPressedIcon.resetDrawableState();
1680 mPressedIcon = null;
1681 }
1682 }
Winson Chung68e4c642011-11-10 15:48:25 -08001683
Winson Chungb44b5242011-06-13 11:32:14 -07001684 /*
1685 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1686 * widget previews in the background with the AsyncTasks.
1687 */
Winson Chung68e4c642011-11-10 15:48:25 -08001688 final static int sLookBehindPageCount = 2;
1689 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001690 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001691 final int count = getChildCount();
1692 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1693 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1694 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001695 }
1696 protected int getAssociatedUpperPageBound(int page) {
1697 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001698 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1699 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1700 count - 1);
1701 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001702 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001703
1704 @Override
1705 protected String getCurrentPageDescription() {
1706 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1707 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001708 int count = 0;
1709
Adam Cohen0cd3b642011-10-14 14:58:00 -07001710 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001711 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001712 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001713 } else {
1714 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001715 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001716 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001717 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001718
Michael Jurka8b805b12012-04-18 14:23:14 -07001719 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001720 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001721}