blob: a4a9ea9dade0368572cbe1fa5acdfb3c99144ab5 [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
17package com.android.launcher2;
18
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
54import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import com.android.launcher2.DropTarget.DragObject;
56
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
Winson Chungb44b5242011-06-13 11:32:14 -0700204 // Previews & outlines
205 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800206 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700207
Adam Cohened66b2b2012-01-23 17:28:51 -0800208 private Runnable mInflateWidgetRunnable = null;
209 private Runnable mBindWidgetRunnable = null;
210 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700211 static final int WIDGET_PRELOAD_PENDING = 0;
212 static final int WIDGET_BOUND = 1;
213 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800214 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
215 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800216 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800217 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800218
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700219 private Toast mWidgetInstructionToast;
220
Michael Jurka39e5d172012-03-12 18:36:12 -0700221 // Deferral of loading widget previews during launcher transitions
222 private boolean mInTransition;
223 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
224 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700225 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
226 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700227
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700228 private Rect mTmpRect = new Rect();
229
Michael Jurkadac85912012-05-18 15:04:49 -0700230 // Used for drawing shortcut previews
231 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
232 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
233 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
234
235 // Used for drawing widget previews
236 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
237 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
238 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
239 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
240
Michael Jurka05713af2013-01-23 12:39:24 +0100241 WidgetPreviewLoader mWidgetPreviewLoader;
242
Winson Chung785d2eb2011-04-14 16:08:02 -0700243 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
244 super(context, attrs);
245 mLayoutInflater = LayoutInflater.from(context);
246 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700247 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700248 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700249 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700250 mCanvas = new Canvas();
251 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700252
253 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800254 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
255 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
256 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700257 mWidgetWidthGap =
258 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
259 mWidgetHeightGap =
260 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700261 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
262 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
264 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700265 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700267
Winson Chung1ed747a2011-05-03 16:18:34 -0700268 // The padding on the non-matched dimension for the default widget preview icons
269 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700270 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700271
272 // Unless otherwise specified this view is important for accessibility.
273 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
274 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
275 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700276 }
277
278 @Override
279 protected void init() {
280 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700281 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700282
283 Context context = getContext();
284 Resources r = context.getResources();
285 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
286 }
287
Winson Chung5afbf7b2011-07-25 11:53:08 -0700288 /** Returns the item index of the center item on this page so that we can restore to this
289 * item index when we rotate. */
290 private int getMiddleComponentIndexOnCurrentPage() {
291 int i = -1;
292 if (getPageCount() > 0) {
293 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700294 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700295 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700296 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
297 int numItemsPerPage = mCellCountX * mCellCountY;
298 int childCount = childrenLayout.getChildCount();
299 if (childCount > 0) {
300 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700301 }
302 } else {
303 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700304 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700305 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
306 int childCount = layout.getChildCount();
307 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700308 i = numApps +
309 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
310 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 }
312 }
313 return i;
314 }
315
316 /** Get the index of the item to restore to if we need to restore the current page. */
317 int getSaveInstanceStateIndex() {
318 if (mSaveInstanceStateItemIndex == -1) {
319 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
320 }
321 return mSaveInstanceStateItemIndex;
322 }
323
324 /** Returns the page in the current orientation which is expected to contain the specified
325 * item index. */
326 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700327 if (index < 0) return 0;
328
329 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700330 int numItemsPerPage = mCellCountX * mCellCountY;
331 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700334 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
335 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 }
337
Winson Chung5afbf7b2011-07-25 11:53:08 -0700338 /** Restores the page for an item at the specified index */
339 void restorePageForIndex(int index) {
340 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700341 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700342 }
343
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700344 private void updatePageCounts() {
345 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
346 (float) (mWidgetCountX * mWidgetCountY));
347 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
348 }
349
Winson Chungf0ea4d32011-06-06 14:27:16 -0700350 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100351 if (mWidgetPreviewLoader == null) {
352 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
353 }
354
Winson Chungf0ea4d32011-06-06 14:27:16 -0700355 // Note that we transpose the counts in portrait so that we get a similar layout
356 boolean isLandscape = getResources().getConfiguration().orientation ==
357 Configuration.ORIENTATION_LANDSCAPE;
358 int maxCellCountX = Integer.MAX_VALUE;
359 int maxCellCountY = Integer.MAX_VALUE;
360 if (LauncherApplication.isScreenLarge()) {
361 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
362 LauncherModel.getCellCountY());
363 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
364 LauncherModel.getCellCountX());
365 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800366 if (mMaxAppCellCountX > -1) {
367 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
368 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200369 // Temp hack for now: only use the max cell count Y for widget layout
370 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800371 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200372 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800373 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374
375 // Now that the data is ready, we can calculate the content width, the number of cells to
376 // use for each page
377 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
378 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
379 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
380 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
381 mCellCountX = mWidgetSpacingLayout.getCellCountX();
382 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700383 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700384
Winson Chungdb1138b2011-06-30 14:39:35 -0700385 // Force a measure to update recalculate the gaps
386 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
387 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200388 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700389 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700390 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700391
Michael Jurkae326f182011-11-21 14:05:46 -0800392 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
393 final boolean hostIsTransitioning = host.isTransitioning();
394
Adam Cohen0cd3b642011-10-14 14:58:00 -0700395 // Restore the page
396 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800397 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700398
Winson Chung3f4e1422011-11-17 14:58:51 -0800399 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
400 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
401 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800402 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800403 post(new Runnable() {
404 @Override
405 public void run() {
406 showAllAppsCling();
407 }
408 });
409 }
410 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700411
Winson Chung3f4e1422011-11-17 14:58:51 -0800412 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700413 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800414 mHasShownAllAppsCling = true;
415 // Calculate the position for the cling punch through
416 int[] offset = new int[2];
417 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
418 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
419 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800420 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800421 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
422 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800423 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800424 mLauncher.showFirstRunAllAppsCling(pos);
425 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700426 }
427
428 @Override
429 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
430 int width = MeasureSpec.getSize(widthMeasureSpec);
431 int height = MeasureSpec.getSize(heightMeasureSpec);
432 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700433 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700434 setDataIsReady();
435 setMeasuredDimension(width, height);
436 onDataReady(width, height);
437 }
438 }
439
440 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
441 }
442
Winson Chung785d2eb2011-04-14 16:08:02 -0700443 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700444 // Get the list of widgets and shortcuts
445 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 List<AppWidgetProviderInfo> widgets =
447 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700448 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
449 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800450 for (AppWidgetProviderInfo widget : widgets) {
451 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700452 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700453 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
454 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700455 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
456 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700457 if (minSpanX <= LauncherModel.getCellCountX() &&
458 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700459 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700460 } else {
461 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
462 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700463 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800464 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700465 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800466 widget.minWidth + ", " + widget.minHeight + ")");
467 }
468 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700469 mWidgets.addAll(shortcuts);
470 Collections.sort(mWidgets,
471 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700472 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700473 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700474 }
475
476 @Override
477 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700478 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700479 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700480 mLauncher.getWorkspace().isSwitchingState()) return;
481
Winson Chung4b576be2011-04-27 17:40:20 -0700482 if (v instanceof PagedViewIcon) {
483 // Animate some feedback to the click
484 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800485
486 // Lock the drawable state to pressed until we return to Launcher
487 if (mPressedIcon != null) {
488 mPressedIcon.lockDrawableState();
489 }
Winson Chungc7450e32012-04-17 17:34:08 -0700490
Winson Chung18f41f82012-05-09 13:28:10 -0700491 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
492 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
493 // when Launcher resumes and we are still in AllApps.
494 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700495 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
496
Winson Chung4b576be2011-04-27 17:40:20 -0700497 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700498 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700499 if (mWidgetInstructionToast != null) {
500 mWidgetInstructionToast.cancel();
501 }
502 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
503 Toast.LENGTH_SHORT);
504 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700505
Winson Chungd2e87b32011-06-02 10:53:07 -0700506 // Create a little animation to show that the widget can move
507 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
508 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700509 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
510 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700511 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700512 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700513 tydAnim.setDuration(100);
514 bounce.play(tyuAnim).before(tydAnim);
515 bounce.setInterpolator(new AccelerateInterpolator());
516 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700517 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 }
519
Winson Chungc6f10b92011-11-14 11:39:07 -0800520 public boolean onKey(View v, int keyCode, KeyEvent event) {
521 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
522 }
523
Winson Chung785d2eb2011-04-14 16:08:02 -0700524 /*
525 * PagedViewWithDraggableItems implementation
526 */
527 @Override
528 protected void determineDraggingStart(android.view.MotionEvent ev) {
529 // Disable dragging by pulling an app down for now.
530 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700531
Winson Chung4b576be2011-04-27 17:40:20 -0700532 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700533 mLauncher.getWorkspace().onDragStartedWithItem(v);
534 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700535 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700536
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700537 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
538 Bundle options = null;
539 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
540 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700541 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
542 info.componentName, null);
543
544 float density = getResources().getDisplayMetrics().density;
545 int xPaddingDips = (int) ((padding.left + padding.right) / density);
546 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
547
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700548 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700549 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
550 mTmpRect.left - xPaddingDips);
551 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
552 mTmpRect.top - yPaddingDips);
553 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
554 mTmpRect.right - xPaddingDips);
555 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
556 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700557 }
558 return options;
559 }
560
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700561 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800562 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700563 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
564
Adam Cohened66b2b2012-01-23 17:28:51 -0800565 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700566 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800567 return;
568 }
569
Adam Cohen21a170b2012-05-30 15:17:06 -0700570 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800571 mBindWidgetRunnable = new Runnable() {
572 @Override
573 public void run() {
574 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700575 // Options will be null for platforms with JB or lower, so this serves as an
576 // SDK level check.
577 if (options == null) {
578 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
579 mWidgetLoadingId, info.componentName)) {
580 mWidgetCleanupState = WIDGET_BOUND;
581 }
582 } else {
583 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
584 mWidgetLoadingId, info.componentName, options)) {
585 mWidgetCleanupState = WIDGET_BOUND;
586 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 }
589 };
590 post(mBindWidgetRunnable);
591
592 mInflateWidgetRunnable = new Runnable() {
593 @Override
594 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700595 if (mWidgetCleanupState != WIDGET_BOUND) {
596 return;
597 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 AppWidgetHostView hostView = mLauncher.
599 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 info.boundWidget = hostView;
601 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800602 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700603 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
604 info.spanY, info, false);
605
606 // We want the first widget layout to be the correct size. This will be important
607 // for width size reporting to the AppWidgetManager.
608 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
609 unScaledSize[1]);
610 lp.x = lp.y = 0;
611 lp.customPosition = true;
612 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800613 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800614 }
615 };
616 post(mInflateWidgetRunnable);
617 }
618
619 @Override
620 public void onShortPress(View v) {
621 // We are anticipating a long press, and we use this time to load bind and instantiate
622 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700623 if (mCreateWidgetInfo != null) {
624 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
625 cleanupWidgetPreloading(false);
626 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800627 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700628 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800629 }
630
Adam Cohen0e56cc92012-05-11 15:57:05 -0700631 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
632 if (!widgetWasAdded) {
633 // If the widget was not added, we may need to do further cleanup.
634 PendingAddWidgetInfo info = mCreateWidgetInfo;
635 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700636
637 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700638 // We never did any preloading, so just remove pending callbacks to do so
639 removeCallbacks(mBindWidgetRunnable);
640 removeCallbacks(mInflateWidgetRunnable);
641 } else if (mWidgetCleanupState == WIDGET_BOUND) {
642 // Delete the widget id which was allocated
643 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700644 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
645 }
646
647 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700648 removeCallbacks(mInflateWidgetRunnable);
649 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700650 // Delete the widget id which was allocated
651 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700652 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
653 }
654
Adam Cohen0e56cc92012-05-11 15:57:05 -0700655 // The widget was inflated and added to the DragLayer -- remove it.
656 AppWidgetHostView widget = info.boundWidget;
657 mLauncher.getDragLayer().removeView(widget);
658 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 }
660 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
661 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700662 mCreateWidgetInfo = null;
663 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800664 }
665
Adam Cohen7a326642012-02-22 12:03:22 -0800666 @Override
667 public void cleanUpShortPress(View v) {
668 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700669 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800670 }
671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700673 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800674 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700675 // Get the widget preview as the drag representation
676 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700677 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700678
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700679 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
680 // we abort the drag.
681 if (image.getDrawable() == null) {
682 mDraggingWidget = false;
683 return false;
684 }
685
Winson Chung4b576be2011-04-27 17:40:20 -0700686 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800687 Bitmap preview;
688 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800689 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100690 Point previewPadding = null;
691
Winson Chung1ed747a2011-05-03 16:18:34 -0700692 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700693 // This can happen in some weird cases involving multi-touch. We can't start dragging
694 // the widget if this is null, so we break out.
695 if (mCreateWidgetInfo == null) {
696 return false;
697 }
698
Adam Cohen1b36dc32012-02-13 19:27:37 -0800699 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
700 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700701 int spanX = createItemInfo.spanX;
702 int spanY = createItemInfo.spanY;
703 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
704 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700705
Winson Chung72d59842012-02-22 13:51:36 -0800706 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
707 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700708 int maxWidth, maxHeight;
709 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
710 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800711
Michael Jurka05713af2013-01-23 12:39:24 +0100712 int[] previewSizeBeforeScale = new int[1];
713
714 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
715 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
716 maxWidth, maxHeight, null, previewSizeBeforeScale);
717
718 // Compare the size of the drag preview to the preview in the AppsCustomize tray
719 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
720 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
721 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
722
723 // The bitmap in the AppsCustomize tray is always the the same size, so there
724 // might be extra pixels around the preview itself - this accounts for that
725 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
726 int padding =
727 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
728 previewPadding = new Point(padding, 0);
729 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700730 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700731 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
732 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
733 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
734 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
735
Winson Chung1120e032011-11-22 16:11:31 -0800736 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700737 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100738 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700739 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700740 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700741 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700742 createItemInfo.spanX = createItemInfo.spanY = 1;
743 }
Winson Chung4b576be2011-04-27 17:40:20 -0700744
Michael Jurka8c3339b2012-06-14 16:18:21 -0700745 // Don't clip alpha values for the drag outline if we're using the default widget preview
746 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
747 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700748
Winson Chung1120e032011-11-22 16:11:31 -0800749 // Save the preview for the outline generation, then dim the preview
750 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
751 false);
Winson Chung1120e032011-11-22 16:11:31 -0800752
Winson Chung4b576be2011-04-27 17:40:20 -0700753 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700754 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700755 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800756 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100757 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800758 outline.recycle();
759 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700760 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700761 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800762
Winson Chung4b576be2011-04-27 17:40:20 -0700763 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700765 if (!super.beginDragging(v)) return false;
766
767 if (v instanceof PagedViewIcon) {
768 beginDraggingApplication(v);
769 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700770 if (!beginDraggingWidget(v)) {
771 return false;
772 }
Winson Chung4b576be2011-04-27 17:40:20 -0700773 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800774
775 // We delay entering spring-loaded mode slightly to make sure the UI
776 // thready is free of any work.
777 postDelayed(new Runnable() {
778 @Override
779 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800780 // We don't enter spring-loaded mode if the drag has been cancelled
781 if (mLauncher.getDragController().isDragging()) {
782 // Dismiss the cling
783 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800784
Adam Cohen1b36dc32012-02-13 19:27:37 -0800785 // Reset the alpha on the dragged icon before we drag
786 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800787
Adam Cohen1b36dc32012-02-13 19:27:37 -0800788 // Go into spring loaded mode (must happen before we startDrag())
789 mLauncher.enterSpringLoadedDragMode();
790 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 }
Winson Chung72d59842012-02-22 13:51:36 -0800792 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800793
Winson Chung785d2eb2011-04-14 16:08:02 -0700794 return true;
795 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800796
Winson Chunga48487a2012-03-20 16:19:37 -0700797 /**
798 * Clean up after dragging.
799 *
800 * @param target where the item was dragged to (can be null if the item was flung)
801 */
802 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700803 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700804 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700805 // Exit spring loaded mode if we have not successfully dropped or have not handled the
806 // drop in Workspace
807 mLauncher.exitSpringLoadedDragMode();
808 }
Winson Chung4b919f82012-05-01 10:44:08 -0700809 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700810 }
811
Winson Chung785d2eb2011-04-14 16:08:02 -0700812 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700813 public View getContent() {
814 return null;
815 }
816
817 @Override
818 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700819 mInTransition = true;
820 if (toWorkspace) {
821 cancelAllTasks();
822 }
823 }
824
825 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700826 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700827 }
828
829 @Override
830 public void onLauncherTransitionStep(Launcher l, float t) {
831 }
832
833 @Override
834 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
835 mInTransition = false;
836 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
837 onSyncWidgetPageItems(d);
838 }
839 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700840 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
841 r.run();
842 }
843 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700844 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700845 }
846
847 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700848 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
849 boolean success) {
850 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
851 if (isFlingToDelete) return;
852
853 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700854
855 // Display an error message if the drag failed due to there not being enough space on the
856 // target layout we were dropping on.
857 if (!success) {
858 boolean showOutOfSpaceMessage = false;
859 if (target instanceof Workspace) {
860 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
861 Workspace workspace = (Workspace) target;
862 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700863 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700864 if (layout != null) {
865 layout.calculateSpans(itemInfo);
866 showOutOfSpaceMessage =
867 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
868 }
869 }
Winson Chungfc79c802011-05-02 13:35:34 -0700870 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700871 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700872 }
Adam Cohen7a326642012-02-22 12:03:22 -0800873
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800874 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700875 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700876 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800877 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700878 }
879
Winson Chunga48487a2012-03-20 16:19:37 -0700880 @Override
881 public void onFlingToDeleteCompleted() {
882 // We just dismiss the drag when we fling, so cleanup here
883 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700884 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700885 mDraggingWidget = false;
886 }
887
888 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800889 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700890 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800891 }
892
Winson Chung7f0acdd2011-09-19 18:34:19 -0700893 @Override
894 protected void onDetachedFromWindow() {
895 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700896 cancelAllTasks();
897 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700898
Michael Jurkae326f182011-11-21 14:05:46 -0800899 public void clearAllWidgetPages() {
900 cancelAllTasks();
901 int count = getChildCount();
902 for (int i = 0; i < count; i++) {
903 View v = getPageAt(i);
904 if (v instanceof PagedViewGridLayout) {
905 ((PagedViewGridLayout) v).removeAllViewsOnPage();
906 mDirtyPageContent.set(i, true);
907 }
908 }
909 }
910
Adam Cohen0cd3b642011-10-14 14:58:00 -0700911 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700912 // Clean up all the async tasks
913 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
914 while (iter.hasNext()) {
915 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
916 task.cancel(false);
917 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700918 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700919
920 // We've already preallocated the views for the data to load into, so clear them as well
921 View v = getPageAt(task.page);
922 if (v instanceof PagedViewGridLayout) {
923 ((PagedViewGridLayout) v).removeAllViewsOnPage();
924 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700925 }
Winson Chung83687b12012-04-25 16:01:01 -0700926 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700927 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700928 }
929
Winson Chung785d2eb2011-04-14 16:08:02 -0700930 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700931 if (type == ContentType.Widgets) {
932 invalidatePageData(mNumAppsPages, true);
933 } else if (type == ContentType.Applications) {
934 invalidatePageData(0, true);
935 }
Winson Chungb44b5242011-06-13 11:32:14 -0700936 }
937
Adam Cohen0cd3b642011-10-14 14:58:00 -0700938 protected void snapToPage(int whichPage, int delta, int duration) {
939 super.snapToPage(whichPage, delta, duration);
940 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800941
942 // Update the thread priorities given the direction lookahead
943 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
944 while (iter.hasNext()) {
945 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700946 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800947 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
948 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
949 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
950 } else {
951 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
952 }
953 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700954 }
955
956 private void updateCurrentTab(int currentPage) {
957 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800958 if (tabHost != null) {
959 String tag = tabHost.getCurrentTabTag();
960 if (tag != null) {
961 if (currentPage >= mNumAppsPages &&
962 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
963 tabHost.setCurrentTabFromContent(ContentType.Widgets);
964 } else if (currentPage < mNumAppsPages &&
965 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
966 tabHost.setCurrentTabFromContent(ContentType.Applications);
967 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700968 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700969 }
970 }
971
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 /*
973 * Apps PagedView implementation
974 */
Winson Chung63257c12011-05-05 17:06:13 -0700975 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
976 int childCount = layout.getChildCount();
977 for (int i = 0; i < childCount; ++i) {
978 layout.getChildAt(i).setVisibility(visibility);
979 }
980 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700981 private void setupPage(PagedViewCellLayout layout) {
982 layout.setCellCount(mCellCountX, mCellCountY);
983 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
984 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
985 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
986
Winson Chung63257c12011-05-05 17:06:13 -0700987 // Note: We force a measure here to get around the fact that when we do layout calculations
988 // immediately after syncing, we don't have a proper width. That said, we already know the
989 // expected page width, so we can actually optimize by hiding all the TextView-based
990 // children that are expensive to measure, and let that happen naturally later.
991 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700992 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700993 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700994 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700996 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700998
Winson Chungf314b0e2011-08-16 11:54:27 -0700999 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001001 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 int numCells = mCellCountX * mCellCountY;
1003 int startIndex = page * numCells;
1004 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001005 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001006
Winson Chung785d2eb2011-04-14 16:08:02 -07001007 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001008 ArrayList<Object> items = new ArrayList<Object>();
1009 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001010 for (int i = startIndex; i < endIndex; ++i) {
1011 ApplicationInfo info = mApps.get(i);
1012 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1013 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001014 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001015 icon.setOnClickListener(this);
1016 icon.setOnLongClickListener(this);
1017 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001018 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001019
1020 int index = i - startIndex;
1021 int x = index % mCellCountX;
1022 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001023 if (isRtl) {
1024 x = mCellCountX - x - 1;
1025 }
Winson Chung6a70e9f2011-05-17 16:24:49 -07001026 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001027
1028 items.add(info);
1029 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001031
Michael Jurka47639b92013-01-14 12:42:27 +01001032 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001033 }
Winson Chungb44b5242011-06-13 11:32:14 -07001034
1035 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001036 * A helper to return the priority for loading of the specified widget page.
1037 */
1038 private int getWidgetPageLoadPriority(int page) {
1039 // If we are snapping to another page, use that index as the target page index
1040 int toPage = mCurrentPage;
1041 if (mNextPage > -1) {
1042 toPage = mNextPage;
1043 }
1044
1045 // We use the distance from the target page as an initial guess of priority, but if there
1046 // are no pages of higher priority than the page specified, then bump up the priority of
1047 // the specified page.
1048 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1049 int minPageDiff = Integer.MAX_VALUE;
1050 while (iter.hasNext()) {
1051 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001052 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001053 }
1054
1055 int rawPageDiff = Math.abs(page - toPage);
1056 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1057 }
1058 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001059 * Return the appropriate thread priority for loading for a given page (we give the current
1060 * page much higher priority)
1061 */
1062 private int getThreadPriorityForPage(int page) {
1063 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001064 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001065 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001066 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001067 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001068 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001069 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001070 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001071 }
1072 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001073 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001074 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001075 return Math.max(0, pageDiff * sPageSleepDelay);
1076 }
Winson Chungb44b5242011-06-13 11:32:14 -07001077 /**
1078 * Creates and executes a new AsyncTask to load a page of widget previews.
1079 */
1080 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001081 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001082
Winson Chungb44b5242011-06-13 11:32:14 -07001083 // Prune all tasks that are no longer needed
1084 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1085 while (iter.hasNext()) {
1086 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001087 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001088 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1089 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001090 task.cancel(false);
1091 iter.remove();
1092 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001093 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001094 }
1095 }
1096
Winson Chungf314b0e2011-08-16 11:54:27 -07001097 // 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 -07001098 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001099 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001100 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001101 @Override
1102 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001103 try {
Winson Chung09945932011-09-20 14:22:40 -07001104 try {
1105 Thread.sleep(sleepMs);
1106 } catch (Exception e) {}
1107 loadWidgetPreviewsInBackground(task, data);
1108 } finally {
1109 if (task.isCancelled()) {
1110 data.cleanup(true);
1111 }
1112 }
Winson Chungb44b5242011-06-13 11:32:14 -07001113 }
1114 },
1115 new AsyncTaskCallback() {
1116 @Override
1117 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001118 mRunningTasks.remove(task);
1119 if (task.isCancelled()) return;
1120 // do cleanup inside onSyncWidgetPageItems
1121 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001122 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001123 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001124
1125 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001126 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001127 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001128 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001129 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1130 mRunningTasks.add(t);
1131 }
Winson Chungb44b5242011-06-13 11:32:14 -07001132
Winson Chung785d2eb2011-04-14 16:08:02 -07001133 /*
1134 * Widgets PagedView implementation
1135 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001136 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1138 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001139
1140 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001141 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001142 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1143 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001145 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001146 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001147
Michael Jurka038f9d82011-11-03 13:50:45 -07001148 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001149 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001150
Winson Chungd2945262011-06-24 15:22:14 -07001151 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001152 final ArrayList<Object> items = new ArrayList<Object>();
1153 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1154 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001155 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001156 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1157 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001158 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001159
Winson Chunge4a647f2011-09-30 14:41:25 -07001160 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001161 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001162 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1163 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001164 }
1165
Winson Chunge4a647f2011-09-30 14:41:25 -07001166 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001167 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001168 layout.setColumnCount(layout.getCellCountX());
1169 for (int i = 0; i < items.size(); ++i) {
1170 Object rawInfo = items.get(i);
1171 PendingAddItemInfo createItemInfo = null;
1172 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1173 R.layout.apps_customize_widget, layout, false);
1174 if (rawInfo instanceof AppWidgetProviderInfo) {
1175 // Fill in the widget information
1176 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1177 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001178
1179 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001180 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001181 createItemInfo.spanX = spanXY[0];
1182 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001183 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001184 createItemInfo.minSpanX = minSpanXY[0];
1185 createItemInfo.minSpanY = minSpanXY[1];
1186
Michael Jurka3f4e0702013-02-05 11:21:28 +01001187 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001188 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001189 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001190 } else if (rawInfo instanceof ResolveInfo) {
1191 // Fill in the shortcuts information
1192 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001193 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001194 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1195 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1196 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001197 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001198 widget.setTag(createItemInfo);
1199 }
1200 widget.setOnClickListener(this);
1201 widget.setOnLongClickListener(this);
1202 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001203 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001204
1205 // Layout each widget
1206 int ix = i % mWidgetCountX;
1207 int iy = i / mWidgetCountX;
1208 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001209 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001210 GridLayout.spec(ix, GridLayout.TOP));
1211 lp.width = cellWidth;
1212 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001213 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001214 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1215 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1216 layout.addView(widget, lp);
1217 }
1218
Michael Jurka038f9d82011-11-03 13:50:45 -07001219 // wait until a call on onLayout to start loading, because
1220 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1221 // TODO: can we do a measure/layout immediately?
1222 layout.setOnLayoutListener(new Runnable() {
1223 public void run() {
1224 // Load the widget previews
1225 int maxPreviewWidth = cellWidth;
1226 int maxPreviewHeight = cellHeight;
1227 if (layout.getChildCount() > 0) {
1228 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1229 int[] maxSize = w.getPreviewSize();
1230 maxPreviewWidth = maxSize[0];
1231 maxPreviewHeight = maxSize[1];
1232 }
Michael Jurka05713af2013-01-23 12:39:24 +01001233
Michael Jurka3f4e0702013-02-05 11:21:28 +01001234 mWidgetPreviewLoader.setPreviewSize(
1235 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001236 if (immediate) {
1237 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001238 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001239 loadWidgetPreviewsInBackground(null, data);
1240 onSyncWidgetPageItems(data);
1241 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001242 if (mInTransition) {
1243 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1244 } else {
1245 prepareLoadWidgetPreviewsTask(page, items,
1246 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1247 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001248 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001249 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001250 }
1251 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001252 }
1253 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1254 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001255 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1256 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001257 if (task != null) {
1258 // Ensure that this task starts running at the correct priority
1259 task.syncThreadPriority();
1260 }
1261
1262 // Load each of the widget/shortcut previews
1263 ArrayList<Object> items = data.items;
1264 ArrayList<Bitmap> images = data.generatedImages;
1265 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001266 for (int i = 0; i < count; ++i) {
1267 if (task != null) {
1268 // Ensure we haven't been cancelled yet
1269 if (task.isCancelled()) break;
1270 // Before work on each item, ensure that this task is running at the correct
1271 // priority
1272 task.syncThreadPriority();
1273 }
1274
Michael Jurka05713af2013-01-23 12:39:24 +01001275 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001276 }
Winson Chungb44b5242011-06-13 11:32:14 -07001277 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001278
Winson Chungb44b5242011-06-13 11:32:14 -07001279 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001280 if (mInTransition) {
1281 mDeferredSyncWidgetPageItems.add(data);
1282 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001283 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001284 try {
1285 int page = data.page;
1286 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001287
Michael Jurka39e5d172012-03-12 18:36:12 -07001288 ArrayList<Object> items = data.items;
1289 int count = items.size();
1290 for (int i = 0; i < count; ++i) {
1291 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1292 if (widget != null) {
1293 Bitmap preview = data.generatedImages.get(i);
1294 widget.applyPreview(new FastBitmapDrawable(preview), i);
1295 }
1296 }
Winson Chung68e4c642011-11-10 15:48:25 -08001297
Michael Jurka47639b92013-01-14 12:42:27 +01001298 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001299
1300 // Update all thread priorities
1301 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1302 while (iter.hasNext()) {
1303 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1304 int pageIndex = task.page;
1305 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1306 }
1307 } finally {
1308 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001309 }
Winson Chungb44b5242011-06-13 11:32:14 -07001310 }
Winson Chung46af2e82011-05-09 16:00:53 -07001311
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 @Override
1313 public void syncPages() {
1314 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001315 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001316
Adam Cohen0cd3b642011-10-14 14:58:00 -07001317 Context context = getContext();
1318 for (int j = 0; j < mNumWidgetPages; ++j) {
1319 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1320 mWidgetCountY);
1321 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001322 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001323 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001324 }
1325
Adam Cohen0cd3b642011-10-14 14:58:00 -07001326 for (int i = 0; i < mNumAppsPages; ++i) {
1327 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1328 setupPage(layout);
1329 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 }
1331 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001332
Winson Chung785d2eb2011-04-14 16:08:02 -07001333 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001334 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001335 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001336 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001337 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001338 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001339 }
1340 }
1341
Adam Cohen22f823d2011-09-01 17:22:18 -07001342 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1343 // it is in the z-order. This is important to insure touch events are handled correctly.
1344 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001345 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001346 }
1347
Adam Cohenae4f1552011-10-20 00:15:42 -07001348 @Override
1349 protected int indexToPage(int index) {
1350 return getChildCount() - index - 1;
1351 }
1352
Adam Cohen22f823d2011-09-01 17:22:18 -07001353 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1354 @Override
1355 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001356 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001357 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001358
1359 for (int i = 0; i < getChildCount(); i++) {
1360 View v = getPageAt(i);
1361 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001362 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001363
Winson Chung52aee602013-01-30 12:01:02 -08001364 float interpolatedProgress;
1365 float translationX;
1366 float maxScrollProgress = Math.max(0, scrollProgress);
1367 float minScrollProgress = Math.min(0, scrollProgress);
1368
1369 if (isRtl) {
1370 translationX = maxScrollProgress * v.getMeasuredWidth();
1371 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1372 } else {
1373 translationX = minScrollProgress * v.getMeasuredWidth();
1374 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1375 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001376 float scale = (1 - interpolatedProgress) +
1377 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001378
Adam Cohen2591f6a2011-10-25 14:36:40 -07001379 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001380 if (isRtl && (scrollProgress > 0)) {
1381 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1382 } else if (!isRtl && (scrollProgress < 0)) {
1383 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001384 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001385 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001386 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1387 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001388
1389 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1390 int pageWidth = v.getMeasuredWidth();
1391 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001392
1393 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001394 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1395 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1396 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1397
1398 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001399 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001400 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001401 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1402 scale = 1.0f;
1403 alpha = 1.0f;
1404 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001405 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001406 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001407 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001408 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001409 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1410 scale = 1.0f;
1411 alpha = 1.0f;
1412 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001413 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001414 } else {
1415 v.setPivotY(pageHeight / 2.0f);
1416 v.setPivotX(pageWidth / 2.0f);
1417 v.setRotationY(0f);
1418 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001419 }
1420
1421 v.setTranslationX(translationX);
1422 v.setScaleX(scale);
1423 v.setScaleY(scale);
1424 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001425
1426 // If the view has 0 alpha, we set it to be invisible so as to prevent
1427 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001428 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001429 v.setVisibility(INVISIBLE);
1430 } else if (v.getVisibility() != VISIBLE) {
1431 v.setVisibility(VISIBLE);
1432 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001433 }
1434 }
Michael Jurka47639b92013-01-14 12:42:27 +01001435
1436 enableHwLayersOnVisiblePages();
1437 }
1438
1439 private void enableHwLayersOnVisiblePages() {
1440 final int screenCount = getChildCount();
1441
1442 getVisiblePages(mTempVisiblePagesRange);
1443 int leftScreen = mTempVisiblePagesRange[0];
1444 int rightScreen = mTempVisiblePagesRange[1];
1445 int forceDrawScreen = -1;
1446 if (leftScreen == rightScreen) {
1447 // make sure we're caching at least two pages always
1448 if (rightScreen < screenCount - 1) {
1449 rightScreen++;
1450 forceDrawScreen = rightScreen;
1451 } else if (leftScreen > 0) {
1452 leftScreen--;
1453 forceDrawScreen = leftScreen;
1454 }
1455 } else {
1456 forceDrawScreen = leftScreen + 1;
1457 }
1458
1459 for (int i = 0; i < screenCount; i++) {
1460 final View layout = (View) getPageAt(i);
1461 if (!(leftScreen <= i && i <= rightScreen &&
1462 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1463 layout.setLayerType(LAYER_TYPE_NONE, null);
1464 }
1465 }
1466
Michael Jurka47639b92013-01-14 12:42:27 +01001467 for (int i = 0; i < screenCount; i++) {
1468 final View layout = (View) getPageAt(i);
1469
1470 if (leftScreen <= i && i <= rightScreen &&
1471 (i == forceDrawScreen || shouldDrawChild(layout))) {
1472 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1473 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1474 }
1475 }
1476 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001477 }
1478
1479 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001480 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001481 }
1482
Winson Chung785d2eb2011-04-14 16:08:02 -07001483 /**
1484 * Used by the parent to get the content width to set the tab bar to
1485 * @return
1486 */
1487 public int getPageContentWidth() {
1488 return mContentWidth;
1489 }
1490
Winson Chungb26f3d62011-06-02 10:49:29 -07001491 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001492 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001493 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001494 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001495 // We reset the save index when we change pages so that it will be recalculated on next
1496 // rotation
1497 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001498 }
1499
Winson Chung785d2eb2011-04-14 16:08:02 -07001500 /*
1501 * AllAppsView implementation
1502 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001503 public void setup(Launcher launcher, DragController dragController) {
1504 mLauncher = launcher;
1505 mDragController = dragController;
1506 }
Winson Chung9802ac92012-06-08 16:01:58 -07001507
1508 /**
1509 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1510 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1511 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1512 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1513 */
1514 private void invalidateOnDataChange() {
1515 if (!isDataReady()) {
1516 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1517 // request a layout to trigger the page data when ready.
1518 requestLayout();
1519 } else {
1520 cancelAllTasks();
1521 invalidatePageData();
1522 }
1523 }
1524
Winson Chung785d2eb2011-04-14 16:08:02 -07001525 public void setApps(ArrayList<ApplicationInfo> list) {
1526 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001527 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001528 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001529 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001530 }
1531 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1532 // We add it in place, in alphabetical order
1533 int count = list.size();
1534 for (int i = 0; i < count; ++i) {
1535 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001536 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001537 if (index < 0) {
1538 mApps.add(-(index + 1), info);
1539 }
1540 }
1541 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001542 public void addApps(ArrayList<ApplicationInfo> list) {
1543 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001544 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001545 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001546 }
1547 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1548 ComponentName removeComponent = item.intent.getComponent();
1549 int length = list.size();
1550 for (int i = 0; i < length; ++i) {
1551 ApplicationInfo info = list.get(i);
1552 if (info.intent.getComponent().equals(removeComponent)) {
1553 return i;
1554 }
1555 }
1556 return -1;
1557 }
Winson Chungcd810732012-06-18 16:45:43 -07001558 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1559 int length = list.size();
1560 for (int i = 0; i < length; ++i) {
1561 ApplicationInfo info = list.get(i);
1562 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1563 return i;
1564 }
1565 }
1566 return -1;
1567 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1569 // loop through all the apps and remove apps that have the same component
1570 int length = list.size();
1571 for (int i = 0; i < length; ++i) {
1572 ApplicationInfo info = list.get(i);
1573 int removeIndex = findAppByComponent(mApps, info);
1574 if (removeIndex > -1) {
1575 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001576 }
1577 }
1578 }
Winson Chungcd810732012-06-18 16:45:43 -07001579 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1580 // loop through all the package names and remove apps that have the same package name
1581 for (String pn : packageNames) {
1582 int removeIndex = findAppByPackage(mApps, pn);
1583 while (removeIndex > -1) {
1584 mApps.remove(removeIndex);
1585 removeIndex = findAppByPackage(mApps, pn);
1586 }
1587 }
1588 }
1589 public void removeApps(ArrayList<String> packageNames) {
1590 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001591 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001592 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001593 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001594 public void updateApps(ArrayList<ApplicationInfo> list) {
1595 // We remove and re-add the updated applications list because it's properties may have
1596 // changed (ie. the title), and this will ensure that the items will be in their proper
1597 // place in the list.
1598 removeAppsWithoutInvalidate(list);
1599 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001600 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001601 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001602 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001603
Winson Chung785d2eb2011-04-14 16:08:02 -07001604 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001605 // If we have reset, then we should not continue to restore the previous state
1606 mSaveInstanceStateItemIndex = -1;
1607
Adam Cohenb64d36e2011-10-17 21:48:02 -07001608 AppsCustomizeTabHost tabHost = getTabHost();
1609 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001610 if (tag != null) {
1611 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1612 tabHost.setCurrentTabFromContent(ContentType.Applications);
1613 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001614 }
Winson Chung649668f2012-01-10 13:07:16 -08001615
Adam Cohenb64d36e2011-10-17 21:48:02 -07001616 if (mCurrentPage != 0) {
1617 invalidatePageData(0);
1618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001620
1621 private AppsCustomizeTabHost getTabHost() {
1622 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1623 }
1624
Winson Chung785d2eb2011-04-14 16:08:02 -07001625 public void dumpState() {
1626 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001627 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1628 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001629 }
Adam Cohen4e844012011-11-09 13:48:04 -08001630
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001632 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001633 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001634 for (Object i: list) {
1635 if (i instanceof AppWidgetProviderInfo) {
1636 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1637 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1638 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1639 + " initialLayout=" + info.initialLayout
1640 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1641 } else if (i instanceof ResolveInfo) {
1642 ResolveInfo info = (ResolveInfo) i;
1643 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1644 + info.icon);
1645 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001646 }
1647 }
Adam Cohen4e844012011-11-09 13:48:04 -08001648
Winson Chung785d2eb2011-04-14 16:08:02 -07001649 public void surrender() {
1650 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1651 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001652
1653 // Stop all background tasks
1654 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 }
Winson Chung007c6982011-06-14 13:27:53 -07001656
Winson Chunge4e50662012-01-23 14:45:13 -08001657 @Override
1658 public void iconPressed(PagedViewIcon icon) {
1659 // Reset the previously pressed icon and store a reference to the pressed icon so that
1660 // we can reset it on return to Launcher (in Launcher.onResume())
1661 if (mPressedIcon != null) {
1662 mPressedIcon.resetDrawableState();
1663 }
1664 mPressedIcon = icon;
1665 }
1666
1667 public void resetDrawableState() {
1668 if (mPressedIcon != null) {
1669 mPressedIcon.resetDrawableState();
1670 mPressedIcon = null;
1671 }
1672 }
Winson Chung68e4c642011-11-10 15:48:25 -08001673
Winson Chungb44b5242011-06-13 11:32:14 -07001674 /*
1675 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1676 * widget previews in the background with the AsyncTasks.
1677 */
Winson Chung68e4c642011-11-10 15:48:25 -08001678 final static int sLookBehindPageCount = 2;
1679 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001680 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001681 final int count = getChildCount();
1682 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1683 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1684 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001685 }
1686 protected int getAssociatedUpperPageBound(int page) {
1687 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001688 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1689 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1690 count - 1);
1691 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001692 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001693
1694 @Override
1695 protected String getCurrentPageDescription() {
1696 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1697 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001698 int count = 0;
1699
Adam Cohen0cd3b642011-10-14 14:58:00 -07001700 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001701 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001702 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001703 } else {
1704 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001705 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001706 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001707 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001708
Michael Jurka8b805b12012-04-18 14:23:14 -07001709 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001710 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001711}