blob: 43a5259a690387104942fd3625e21b73f627ab5b [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010034import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Rect;
36import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070037import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070038import android.os.Build;
39import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070040import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080044import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.LayoutInflater;
46import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.R;
55import com.android.launcher3.DropTarget.DragObject;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080074 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070075 }
76
Michael Jurka038f9d82011-11-03 13:50:45 -070077 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Michael Jurka3f4e0702013-02-05 11:21:28 +010078 AsyncTaskCallback postR, WidgetPreviewLoader w) {
Winson Chungb44b5242011-06-13 11:32:14 -070079 page = p;
80 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070081 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070082 maxImageWidth = cw;
83 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070084 doInBackgroundCallback = bgR;
85 postExecuteCallback = postR;
Michael Jurka3f4e0702013-02-05 11:21:28 +010086 widgetPreviewLoader = w;
Winson Chungb44b5242011-06-13 11:32:14 -070087 }
Winson Chung09945932011-09-20 14:22:40 -070088 void cleanup(boolean cancelled) {
89 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070090 if (generatedImages != null) {
91 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010092 for (int i = 0; i < generatedImages.size(); i++) {
Michael Jurkaee8e99f2013-02-07 13:27:06 +010093 widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070094 }
95 }
96 generatedImages.clear();
97 }
98 }
Winson Chungb44b5242011-06-13 11:32:14 -070099 int page;
100 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700101 ArrayList<Bitmap> sourceImages;
102 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700103 int maxImageWidth;
104 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 AsyncTaskCallback doInBackgroundCallback;
106 AsyncTaskCallback postExecuteCallback;
Michael Jurka3f4e0702013-02-05 11:21:28 +0100107 WidgetPreviewLoader widgetPreviewLoader;
Winson Chungb44b5242011-06-13 11:32:14 -0700108}
Winson Chung4e076542011-06-23 13:04:10 -0700109
Winson Chungb44b5242011-06-13 11:32:14 -0700110/**
111 * A generic template for an async task used in AppsCustomize.
112 */
113class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700114 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700115 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700116 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700117 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700118 }
119 @Override
120 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
121 if (params.length != 1) return null;
122 // Load each of the widget previews in the background
123 params[0].doInBackgroundCallback.run(this, params[0]);
124 return params[0];
125 }
126 @Override
127 protected void onPostExecute(AsyncTaskPageData result) {
128 // All the widget previews are loaded, so we can just callback to inflate the page
129 result.postExecuteCallback.run(this, result);
130 }
131
132 void setThreadPriority(int p) {
133 threadPriority = p;
134 }
135 void syncThreadPriority() {
136 Process.setThreadPriority(threadPriority);
137 }
138
139 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700140 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700142 int threadPriority;
143}
Winson Chungb44b5242011-06-13 11:32:14 -0700144
145/**
146 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
147 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700148public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700149 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700150 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
151 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700152 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700153
154 /**
155 * The different content types that this paged view can show.
156 */
157 public enum ContentType {
158 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700159 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700160 }
161
162 // Refs
163 private Launcher mLauncher;
164 private DragController mDragController;
165 private final LayoutInflater mLayoutInflater;
166 private final PackageManager mPackageManager;
167
Winson Chung5afbf7b2011-07-25 11:53:08 -0700168 // Save and Restore
169 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800170 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700171
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700173 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700174 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700175
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800177 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 private int mClingFocusedX;
179 private int mClingFocusedY;
180
Winson Chung1ed747a2011-05-03 16:18:34 -0700181 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700182 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700183 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700184
185 // Dimens
186 private int mContentWidth;
Winson Chung6032e7e2011-11-08 15:47:17 -0800187 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700188 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700189 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700191 private int mNumAppsPages;
192 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700193
Adam Cohen22f823d2011-09-01 17:22:18 -0700194 // Relating to the scroll and overscroll effects
195 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700197 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700198 private static float TRANSITION_PIVOT = 0.65f;
199 private static float TRANSITION_MAX_ROTATION = 22;
200 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700201 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700202 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700203
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
Michael Jurkac402cd92013-05-20 15:49:32 +0200243 private boolean mInBulkBind;
244 private boolean mNeedToUpdatePageCountsAndInvalidateData;
245
Winson Chung785d2eb2011-04-14 16:08:02 -0700246 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
247 super(context, attrs);
248 mLayoutInflater = LayoutInflater.from(context);
249 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700250 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700251 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700252 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700253 mCanvas = new Canvas();
254 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700255
256 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800257 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
258 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
259 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700260 mWidgetWidthGap =
261 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
262 mWidgetHeightGap =
263 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700264 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
265 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700266 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
267 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700268 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700270
Winson Chung1ed747a2011-05-03 16:18:34 -0700271 // The padding on the non-matched dimension for the default widget preview icons
272 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700273 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700274
275 // Unless otherwise specified this view is important for accessibility.
276 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
277 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
278 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700279 }
280
281 @Override
282 protected void init() {
283 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700284 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700285
286 Context context = getContext();
287 Resources r = context.getResources();
288 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
289 }
290
Winson Chung5afbf7b2011-07-25 11:53:08 -0700291 /** Returns the item index of the center item on this page so that we can restore to this
292 * item index when we rotate. */
293 private int getMiddleComponentIndexOnCurrentPage() {
294 int i = -1;
295 if (getPageCount() > 0) {
296 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700297 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700298 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700299 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
300 int numItemsPerPage = mCellCountX * mCellCountY;
301 int childCount = childrenLayout.getChildCount();
302 if (childCount > 0) {
303 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700304 }
305 } else {
306 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700307 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700308 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
309 int childCount = layout.getChildCount();
310 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 i = numApps +
312 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
313 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700314 }
315 }
316 return i;
317 }
318
319 /** Get the index of the item to restore to if we need to restore the current page. */
320 int getSaveInstanceStateIndex() {
321 if (mSaveInstanceStateItemIndex == -1) {
322 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
323 }
324 return mSaveInstanceStateItemIndex;
325 }
326
327 /** Returns the page in the current orientation which is expected to contain the specified
328 * item index. */
329 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 if (index < 0) return 0;
331
332 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700333 int numItemsPerPage = mCellCountX * mCellCountY;
334 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700335 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700337 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
338 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700339 }
340
Winson Chung5afbf7b2011-07-25 11:53:08 -0700341 /** Restores the page for an item at the specified index */
342 void restorePageForIndex(int index) {
343 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 }
346
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700347 private void updatePageCounts() {
348 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
349 (float) (mWidgetCountX * mWidgetCountY));
350 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
351 }
352
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 protected void onDataReady(int width, int height) {
Michael Jurka3f4e0702013-02-05 11:21:28 +0100354 if (mWidgetPreviewLoader == null) {
355 mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher);
356 }
357
Winson Chungf0ea4d32011-06-06 14:27:16 -0700358 // Note that we transpose the counts in portrait so that we get a similar layout
359 boolean isLandscape = getResources().getConfiguration().orientation ==
360 Configuration.ORIENTATION_LANDSCAPE;
361 int maxCellCountX = Integer.MAX_VALUE;
362 int maxCellCountY = Integer.MAX_VALUE;
363 if (LauncherApplication.isScreenLarge()) {
364 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
365 LauncherModel.getCellCountY());
366 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
367 LauncherModel.getCellCountX());
368 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800369 if (mMaxAppCellCountX > -1) {
370 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
371 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200372 // Temp hack for now: only use the max cell count Y for widget layout
373 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800374 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200375 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800376 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377
378 // Now that the data is ready, we can calculate the content width, the number of cells to
379 // use for each page
380 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
381 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
382 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
383 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
384 mCellCountX = mWidgetSpacingLayout.getCellCountX();
385 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700386 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700387
Winson Chungdb1138b2011-06-30 14:39:35 -0700388 // Force a measure to update recalculate the gaps
389 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
390 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200391 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700392 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700394
Michael Jurkae326f182011-11-21 14:05:46 -0800395 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
396 final boolean hostIsTransitioning = host.isTransitioning();
397
Adam Cohen0cd3b642011-10-14 14:58:00 -0700398 // Restore the page
399 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800400 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700401
Winson Chung3f4e1422011-11-17 14:58:51 -0800402 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
403 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
404 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800405 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800406 post(new Runnable() {
407 @Override
408 public void run() {
409 showAllAppsCling();
410 }
411 });
412 }
413 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700414
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700416 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800417 mHasShownAllAppsCling = true;
418 // Calculate the position for the cling punch through
419 int[] offset = new int[2];
420 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
421 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
422 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800423 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800424 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
425 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800426 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800427 mLauncher.showFirstRunAllAppsCling(pos);
428 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700429 }
430
431 @Override
432 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
433 int width = MeasureSpec.getSize(widthMeasureSpec);
434 int height = MeasureSpec.getSize(heightMeasureSpec);
435 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700436 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700437 setDataIsReady();
438 setMeasuredDimension(width, height);
439 onDataReady(width, height);
440 }
441 }
442
443 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
444 }
445
Michael Jurkac402cd92013-05-20 15:49:32 +0200446 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700447 // Get the list of widgets and shortcuts
448 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200449 for (Object o : widgetsAndShortcuts) {
450 if (o instanceof AppWidgetProviderInfo) {
451 AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o;
452 widget.label = widget.label.trim();
453 if (widget.minWidth > 0 && widget.minHeight > 0) {
454 // Ensure that all widgets we show can be added on a workspace of this size
455 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
456 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
457 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
458 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
459 if (minSpanX <= LauncherModel.getCellCountX() &&
Adam Cohen336d4912012-04-13 17:57:11 -0700460 minSpanY <= LauncherModel.getCellCountY()) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200461 mWidgets.add(widget);
462 } else {
463 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
464 widget.minWidth + ", " + widget.minHeight + ")");
465 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700466 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200467 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
468 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700469 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800470 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200471 // just add shortcuts
472 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800473 }
474 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200475 updatePageCountsAndInvalidateData();
476 }
477
478 public void setBulkBind(boolean bulkBind) {
479 if (bulkBind) {
480 mInBulkBind = true;
481 } else {
482 mInBulkBind = false;
483 if (mNeedToUpdatePageCountsAndInvalidateData) {
484 updatePageCountsAndInvalidateData();
485 }
486 }
487 }
488
489 private void updatePageCountsAndInvalidateData() {
490 if (mInBulkBind) {
491 mNeedToUpdatePageCountsAndInvalidateData = true;
492 } else {
493 updatePageCounts();
494 invalidateOnDataChange();
495 mNeedToUpdatePageCountsAndInvalidateData = false;
496 }
Winson Chung4b576be2011-04-27 17:40:20 -0700497 }
498
499 @Override
500 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700501 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700502 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700503 mLauncher.getWorkspace().isSwitchingState()) return;
504
Winson Chung4b576be2011-04-27 17:40:20 -0700505 if (v instanceof PagedViewIcon) {
506 // Animate some feedback to the click
507 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800508
509 // Lock the drawable state to pressed until we return to Launcher
510 if (mPressedIcon != null) {
511 mPressedIcon.lockDrawableState();
512 }
Winson Chungc7450e32012-04-17 17:34:08 -0700513
Winson Chung18f41f82012-05-09 13:28:10 -0700514 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
515 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
516 // when Launcher resumes and we are still in AllApps.
517 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700518 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
519
Winson Chung4b576be2011-04-27 17:40:20 -0700520 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700521 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700522 if (mWidgetInstructionToast != null) {
523 mWidgetInstructionToast.cancel();
524 }
525 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
526 Toast.LENGTH_SHORT);
527 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700528
Winson Chungd2e87b32011-06-02 10:53:07 -0700529 // Create a little animation to show that the widget can move
530 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
531 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700532 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
533 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700534 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700535 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700536 tydAnim.setDuration(100);
537 bounce.play(tyuAnim).before(tydAnim);
538 bounce.setInterpolator(new AccelerateInterpolator());
539 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700540 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 }
542
Winson Chungc6f10b92011-11-14 11:39:07 -0800543 public boolean onKey(View v, int keyCode, KeyEvent event) {
544 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
545 }
546
Winson Chung785d2eb2011-04-14 16:08:02 -0700547 /*
548 * PagedViewWithDraggableItems implementation
549 */
550 @Override
551 protected void determineDraggingStart(android.view.MotionEvent ev) {
552 // Disable dragging by pulling an app down for now.
553 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700554
Winson Chung4b576be2011-04-27 17:40:20 -0700555 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700556 mLauncher.getWorkspace().onDragStartedWithItem(v);
557 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700558 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700559
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700560 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
561 Bundle options = null;
562 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
563 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700564 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
565 info.componentName, null);
566
567 float density = getResources().getDisplayMetrics().density;
568 int xPaddingDips = (int) ((padding.left + padding.right) / density);
569 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
570
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700571 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700572 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
573 mTmpRect.left - xPaddingDips);
574 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
575 mTmpRect.top - yPaddingDips);
576 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
577 mTmpRect.right - xPaddingDips);
578 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
579 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700580 }
581 return options;
582 }
583
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700584 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800585 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700586 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
587
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700589 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 return;
591 }
592
Adam Cohen21a170b2012-05-30 15:17:06 -0700593 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 mBindWidgetRunnable = new Runnable() {
595 @Override
596 public void run() {
597 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700598 // Options will be null for platforms with JB or lower, so this serves as an
599 // SDK level check.
600 if (options == null) {
601 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
602 mWidgetLoadingId, info.componentName)) {
603 mWidgetCleanupState = WIDGET_BOUND;
604 }
605 } else {
606 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
607 mWidgetLoadingId, info.componentName, options)) {
608 mWidgetCleanupState = WIDGET_BOUND;
609 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700610 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800611 }
612 };
613 post(mBindWidgetRunnable);
614
615 mInflateWidgetRunnable = new Runnable() {
616 @Override
617 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700618 if (mWidgetCleanupState != WIDGET_BOUND) {
619 return;
620 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 AppWidgetHostView hostView = mLauncher.
622 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 info.boundWidget = hostView;
624 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800625 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700626 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
627 info.spanY, info, false);
628
629 // We want the first widget layout to be the correct size. This will be important
630 // for width size reporting to the AppWidgetManager.
631 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
632 unScaledSize[1]);
633 lp.x = lp.y = 0;
634 lp.customPosition = true;
635 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800636 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 }
638 };
639 post(mInflateWidgetRunnable);
640 }
641
642 @Override
643 public void onShortPress(View v) {
644 // We are anticipating a long press, and we use this time to load bind and instantiate
645 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700646 if (mCreateWidgetInfo != null) {
647 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
648 cleanupWidgetPreloading(false);
649 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800650 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700651 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 }
653
Adam Cohen0e56cc92012-05-11 15:57:05 -0700654 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
655 if (!widgetWasAdded) {
656 // If the widget was not added, we may need to do further cleanup.
657 PendingAddWidgetInfo info = mCreateWidgetInfo;
658 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700659
660 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700661 // We never did any preloading, so just remove pending callbacks to do so
662 removeCallbacks(mBindWidgetRunnable);
663 removeCallbacks(mInflateWidgetRunnable);
664 } else if (mWidgetCleanupState == WIDGET_BOUND) {
665 // Delete the widget id which was allocated
666 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700667 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
668 }
669
670 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700671 removeCallbacks(mInflateWidgetRunnable);
672 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700673 // Delete the widget id which was allocated
674 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700675 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
676 }
677
Adam Cohen0e56cc92012-05-11 15:57:05 -0700678 // The widget was inflated and added to the DragLayer -- remove it.
679 AppWidgetHostView widget = info.boundWidget;
680 mLauncher.getDragLayer().removeView(widget);
681 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 }
683 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
684 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700685 mCreateWidgetInfo = null;
686 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 }
688
Adam Cohen7a326642012-02-22 12:03:22 -0800689 @Override
690 public void cleanUpShortPress(View v) {
691 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700692 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800693 }
694 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800695
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700696 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800697 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700698 // Get the widget preview as the drag representation
699 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700700 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700701
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700702 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
703 // we abort the drag.
704 if (image.getDrawable() == null) {
705 mDraggingWidget = false;
706 return false;
707 }
708
Winson Chung4b576be2011-04-27 17:40:20 -0700709 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800710 Bitmap preview;
711 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800712 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100713 Point previewPadding = null;
714
Winson Chung1ed747a2011-05-03 16:18:34 -0700715 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700716 // This can happen in some weird cases involving multi-touch. We can't start dragging
717 // the widget if this is null, so we break out.
718 if (mCreateWidgetInfo == null) {
719 return false;
720 }
721
Adam Cohen1b36dc32012-02-13 19:27:37 -0800722 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
723 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700724 int spanX = createItemInfo.spanX;
725 int spanY = createItemInfo.spanY;
726 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
727 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700728
Winson Chung72d59842012-02-22 13:51:36 -0800729 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
730 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700731 int maxWidth, maxHeight;
732 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
733 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800734
Michael Jurka05713af2013-01-23 12:39:24 +0100735 int[] previewSizeBeforeScale = new int[1];
736
737 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
738 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
739 maxWidth, maxHeight, null, previewSizeBeforeScale);
740
741 // Compare the size of the drag preview to the preview in the AppsCustomize tray
742 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
743 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
744 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
745
746 // The bitmap in the AppsCustomize tray is always the the same size, so there
747 // might be extra pixels around the preview itself - this accounts for that
748 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
749 int padding =
750 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
751 previewPadding = new Point(padding, 0);
752 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700753 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700754 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
755 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
756 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
757 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
758
Winson Chung1120e032011-11-22 16:11:31 -0800759 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700760 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100761 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700762 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700763 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700764 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700765 createItemInfo.spanX = createItemInfo.spanY = 1;
766 }
Winson Chung4b576be2011-04-27 17:40:20 -0700767
Michael Jurka8c3339b2012-06-14 16:18:21 -0700768 // Don't clip alpha values for the drag outline if we're using the default widget preview
769 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
770 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700771
Winson Chung1120e032011-11-22 16:11:31 -0800772 // Save the preview for the outline generation, then dim the preview
773 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
774 false);
Winson Chung1120e032011-11-22 16:11:31 -0800775
Winson Chung4b576be2011-04-27 17:40:20 -0700776 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700777 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700778 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800779 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100780 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800781 outline.recycle();
782 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700783 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700784 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800785
Winson Chung4b576be2011-04-27 17:40:20 -0700786 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700788 if (!super.beginDragging(v)) return false;
789
790 if (v instanceof PagedViewIcon) {
791 beginDraggingApplication(v);
792 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700793 if (!beginDraggingWidget(v)) {
794 return false;
795 }
Winson Chung4b576be2011-04-27 17:40:20 -0700796 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800797
798 // We delay entering spring-loaded mode slightly to make sure the UI
799 // thready is free of any work.
800 postDelayed(new Runnable() {
801 @Override
802 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800803 // We don't enter spring-loaded mode if the drag has been cancelled
804 if (mLauncher.getDragController().isDragging()) {
805 // Dismiss the cling
806 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800807
Adam Cohen1b36dc32012-02-13 19:27:37 -0800808 // Reset the alpha on the dragged icon before we drag
809 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800810
Adam Cohen1b36dc32012-02-13 19:27:37 -0800811 // Go into spring loaded mode (must happen before we startDrag())
812 mLauncher.enterSpringLoadedDragMode();
813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 }
Winson Chung72d59842012-02-22 13:51:36 -0800815 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800816
Winson Chung785d2eb2011-04-14 16:08:02 -0700817 return true;
818 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800819
Winson Chunga48487a2012-03-20 16:19:37 -0700820 /**
821 * Clean up after dragging.
822 *
823 * @param target where the item was dragged to (can be null if the item was flung)
824 */
825 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700826 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700827 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700828 // Exit spring loaded mode if we have not successfully dropped or have not handled the
829 // drop in Workspace
830 mLauncher.exitSpringLoadedDragMode();
831 }
Winson Chung4b919f82012-05-01 10:44:08 -0700832 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 }
834
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700836 public View getContent() {
837 return null;
838 }
839
840 @Override
841 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700842 mInTransition = true;
843 if (toWorkspace) {
844 cancelAllTasks();
845 }
846 }
847
848 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700849 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700850 }
851
852 @Override
853 public void onLauncherTransitionStep(Launcher l, float t) {
854 }
855
856 @Override
857 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
858 mInTransition = false;
859 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
860 onSyncWidgetPageItems(d);
861 }
862 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700863 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
864 r.run();
865 }
866 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700867 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700868 }
869
870 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700871 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
872 boolean success) {
873 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
874 if (isFlingToDelete) return;
875
876 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700877
878 // Display an error message if the drag failed due to there not being enough space on the
879 // target layout we were dropping on.
880 if (!success) {
881 boolean showOutOfSpaceMessage = false;
882 if (target instanceof Workspace) {
883 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
884 Workspace workspace = (Workspace) target;
885 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700886 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700887 if (layout != null) {
888 layout.calculateSpans(itemInfo);
889 showOutOfSpaceMessage =
890 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
891 }
892 }
Winson Chungfc79c802011-05-02 13:35:34 -0700893 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700894 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700895 }
Adam Cohen7a326642012-02-22 12:03:22 -0800896
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800897 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700898 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700899 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800900 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700901 }
902
Winson Chunga48487a2012-03-20 16:19:37 -0700903 @Override
904 public void onFlingToDeleteCompleted() {
905 // We just dismiss the drag when we fling, so cleanup here
906 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700907 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700908 mDraggingWidget = false;
909 }
910
911 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800912 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700913 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800914 }
915
Winson Chung7f0acdd2011-09-19 18:34:19 -0700916 @Override
917 protected void onDetachedFromWindow() {
918 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700919 cancelAllTasks();
920 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700921
Michael Jurkae326f182011-11-21 14:05:46 -0800922 public void clearAllWidgetPages() {
923 cancelAllTasks();
924 int count = getChildCount();
925 for (int i = 0; i < count; i++) {
926 View v = getPageAt(i);
927 if (v instanceof PagedViewGridLayout) {
928 ((PagedViewGridLayout) v).removeAllViewsOnPage();
929 mDirtyPageContent.set(i, true);
930 }
931 }
932 }
933
Adam Cohen0cd3b642011-10-14 14:58:00 -0700934 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700935 // Clean up all the async tasks
936 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
937 while (iter.hasNext()) {
938 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
939 task.cancel(false);
940 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700941 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700942
943 // We've already preallocated the views for the data to load into, so clear them as well
944 View v = getPageAt(task.page);
945 if (v instanceof PagedViewGridLayout) {
946 ((PagedViewGridLayout) v).removeAllViewsOnPage();
947 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700948 }
Winson Chung83687b12012-04-25 16:01:01 -0700949 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700950 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700951 }
952
Winson Chung785d2eb2011-04-14 16:08:02 -0700953 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700954 if (type == ContentType.Widgets) {
955 invalidatePageData(mNumAppsPages, true);
956 } else if (type == ContentType.Applications) {
957 invalidatePageData(0, true);
958 }
Winson Chungb44b5242011-06-13 11:32:14 -0700959 }
960
Adam Cohen0cd3b642011-10-14 14:58:00 -0700961 protected void snapToPage(int whichPage, int delta, int duration) {
962 super.snapToPage(whichPage, delta, duration);
963 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800964
965 // Update the thread priorities given the direction lookahead
966 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
967 while (iter.hasNext()) {
968 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700969 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800970 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
971 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
972 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
973 } else {
974 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
975 }
976 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700977 }
978
979 private void updateCurrentTab(int currentPage) {
980 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800981 if (tabHost != null) {
982 String tag = tabHost.getCurrentTabTag();
983 if (tag != null) {
984 if (currentPage >= mNumAppsPages &&
985 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
986 tabHost.setCurrentTabFromContent(ContentType.Widgets);
987 } else if (currentPage < mNumAppsPages &&
988 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
989 tabHost.setCurrentTabFromContent(ContentType.Applications);
990 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700991 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700992 }
993 }
994
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 /*
996 * Apps PagedView implementation
997 */
Winson Chung63257c12011-05-05 17:06:13 -0700998 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
999 int childCount = layout.getChildCount();
1000 for (int i = 0; i < childCount; ++i) {
1001 layout.getChildAt(i).setVisibility(visibility);
1002 }
1003 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001004 private void setupPage(PagedViewCellLayout layout) {
1005 layout.setCellCount(mCellCountX, mCellCountY);
1006 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
1007 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1008 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1009
Winson Chung63257c12011-05-05 17:06:13 -07001010 // Note: We force a measure here to get around the fact that when we do layout calculations
1011 // immediately after syncing, we don't have a proper width. That said, we already know the
1012 // expected page width, so we can actually optimize by hiding all the TextView-based
1013 // children that are expensive to measure, and let that happen naturally later.
1014 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -07001015 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001016 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -07001017 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -07001018 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -07001019 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07001020 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001021
Winson Chungf314b0e2011-08-16 11:54:27 -07001022 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 // ensure that we have the right number of items on the pages
Winson Chungfe1fe262013-04-01 16:52:31 -07001024 final boolean isRtl = isLayoutRtl();
Winson Chung785d2eb2011-04-14 16:08:02 -07001025 int numCells = mCellCountX * mCellCountY;
1026 int startIndex = page * numCells;
1027 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -07001028 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -07001029
Winson Chung785d2eb2011-04-14 16:08:02 -07001030 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001031 ArrayList<Object> items = new ArrayList<Object>();
1032 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001033 for (int i = startIndex; i < endIndex; ++i) {
1034 ApplicationInfo info = mApps.get(i);
1035 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1036 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001037 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001038 icon.setOnClickListener(this);
1039 icon.setOnLongClickListener(this);
1040 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001041 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001042
1043 int index = i - startIndex;
1044 int x = index % mCellCountX;
1045 int y = index / mCellCountX;
Winson Chungfe1fe262013-04-01 16:52:31 -07001046 if (isRtl) {
1047 x = mCellCountX - x - 1;
1048 }
Winson Chung6a70e9f2011-05-17 16:24:49 -07001049 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001050
1051 items.add(info);
1052 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001053 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001054
Michael Jurka47639b92013-01-14 12:42:27 +01001055 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001056 }
Winson Chungb44b5242011-06-13 11:32:14 -07001057
1058 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001059 * A helper to return the priority for loading of the specified widget page.
1060 */
1061 private int getWidgetPageLoadPriority(int page) {
1062 // If we are snapping to another page, use that index as the target page index
1063 int toPage = mCurrentPage;
1064 if (mNextPage > -1) {
1065 toPage = mNextPage;
1066 }
1067
1068 // We use the distance from the target page as an initial guess of priority, but if there
1069 // are no pages of higher priority than the page specified, then bump up the priority of
1070 // the specified page.
1071 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1072 int minPageDiff = Integer.MAX_VALUE;
1073 while (iter.hasNext()) {
1074 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001075 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001076 }
1077
1078 int rawPageDiff = Math.abs(page - toPage);
1079 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1080 }
1081 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001082 * Return the appropriate thread priority for loading for a given page (we give the current
1083 * page much higher priority)
1084 */
1085 private int getThreadPriorityForPage(int page) {
1086 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001087 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001088 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001089 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001090 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001091 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001092 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001093 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001094 }
1095 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001096 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001097 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001098 return Math.max(0, pageDiff * sPageSleepDelay);
1099 }
Winson Chungb44b5242011-06-13 11:32:14 -07001100 /**
1101 * Creates and executes a new AsyncTask to load a page of widget previews.
1102 */
1103 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001104 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001105
Winson Chungb44b5242011-06-13 11:32:14 -07001106 // Prune all tasks that are no longer needed
1107 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1108 while (iter.hasNext()) {
1109 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001110 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001111 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1112 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001113 task.cancel(false);
1114 iter.remove();
1115 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001116 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001117 }
1118 }
1119
Winson Chungf314b0e2011-08-16 11:54:27 -07001120 // 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 -07001121 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001122 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001123 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001124 @Override
1125 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001126 try {
Winson Chung09945932011-09-20 14:22:40 -07001127 try {
1128 Thread.sleep(sleepMs);
1129 } catch (Exception e) {}
1130 loadWidgetPreviewsInBackground(task, data);
1131 } finally {
1132 if (task.isCancelled()) {
1133 data.cleanup(true);
1134 }
1135 }
Winson Chungb44b5242011-06-13 11:32:14 -07001136 }
1137 },
1138 new AsyncTaskCallback() {
1139 @Override
1140 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001141 mRunningTasks.remove(task);
1142 if (task.isCancelled()) return;
1143 // do cleanup inside onSyncWidgetPageItems
1144 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001145 }
Michael Jurka3f4e0702013-02-05 11:21:28 +01001146 }, mWidgetPreviewLoader);
Winson Chungb44b5242011-06-13 11:32:14 -07001147
1148 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001149 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001150 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001151 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001152 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1153 mRunningTasks.add(t);
1154 }
Winson Chungb44b5242011-06-13 11:32:14 -07001155
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 /*
1157 * Widgets PagedView implementation
1158 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001159 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001160 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1161 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001162
1163 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001164 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001165 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1166 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001167 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001168 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001170
Michael Jurka038f9d82011-11-03 13:50:45 -07001171 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001172 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001173
Winson Chungd2945262011-06-24 15:22:14 -07001174 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001175 final ArrayList<Object> items = new ArrayList<Object>();
1176 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1177 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001178 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001179 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1180 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001181 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001182
Winson Chunge4a647f2011-09-30 14:41:25 -07001183 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001184 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001185 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1186 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001187 }
1188
Winson Chunge4a647f2011-09-30 14:41:25 -07001189 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001190 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001191 layout.setColumnCount(layout.getCellCountX());
1192 for (int i = 0; i < items.size(); ++i) {
1193 Object rawInfo = items.get(i);
1194 PendingAddItemInfo createItemInfo = null;
1195 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1196 R.layout.apps_customize_widget, layout, false);
1197 if (rawInfo instanceof AppWidgetProviderInfo) {
1198 // Fill in the widget information
1199 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1200 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001201
1202 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001203 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001204 createItemInfo.spanX = spanXY[0];
1205 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001206 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001207 createItemInfo.minSpanX = minSpanXY[0];
1208 createItemInfo.minSpanY = minSpanXY[1];
1209
Michael Jurka3f4e0702013-02-05 11:21:28 +01001210 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001211 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001212 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001213 } else if (rawInfo instanceof ResolveInfo) {
1214 // Fill in the shortcuts information
1215 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001216 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001217 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1218 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1219 info.activityInfo.name);
Michael Jurka3f4e0702013-02-05 11:21:28 +01001220 widget.applyFromResolveInfo(mPackageManager, info, mWidgetPreviewLoader);
Winson Chunge4a647f2011-09-30 14:41:25 -07001221 widget.setTag(createItemInfo);
1222 }
1223 widget.setOnClickListener(this);
1224 widget.setOnLongClickListener(this);
1225 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001226 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001227
1228 // Layout each widget
1229 int ix = i % mWidgetCountX;
1230 int iy = i / mWidgetCountX;
1231 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001232 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001233 GridLayout.spec(ix, GridLayout.TOP));
1234 lp.width = cellWidth;
1235 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001236 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001237 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1238 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1239 layout.addView(widget, lp);
1240 }
1241
Michael Jurka038f9d82011-11-03 13:50:45 -07001242 // wait until a call on onLayout to start loading, because
1243 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1244 // TODO: can we do a measure/layout immediately?
1245 layout.setOnLayoutListener(new Runnable() {
1246 public void run() {
1247 // Load the widget previews
1248 int maxPreviewWidth = cellWidth;
1249 int maxPreviewHeight = cellHeight;
1250 if (layout.getChildCount() > 0) {
1251 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1252 int[] maxSize = w.getPreviewSize();
1253 maxPreviewWidth = maxSize[0];
1254 maxPreviewHeight = maxSize[1];
1255 }
Michael Jurka05713af2013-01-23 12:39:24 +01001256
Michael Jurka3f4e0702013-02-05 11:21:28 +01001257 mWidgetPreviewLoader.setPreviewSize(
1258 maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout);
Michael Jurka038f9d82011-11-03 13:50:45 -07001259 if (immediate) {
1260 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
Michael Jurka3f4e0702013-02-05 11:21:28 +01001261 maxPreviewWidth, maxPreviewHeight, null, null, mWidgetPreviewLoader);
Michael Jurka038f9d82011-11-03 13:50:45 -07001262 loadWidgetPreviewsInBackground(null, data);
1263 onSyncWidgetPageItems(data);
1264 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001265 if (mInTransition) {
1266 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1267 } else {
1268 prepareLoadWidgetPreviewsTask(page, items,
1269 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1270 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001271 }
Michael Jurka3c69dec2013-02-06 13:43:54 +01001272 layout.setOnLayoutListener(null);
Michael Jurka038f9d82011-11-03 13:50:45 -07001273 }
1274 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001275 }
1276 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1277 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001278 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1279 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001280 if (task != null) {
1281 // Ensure that this task starts running at the correct priority
1282 task.syncThreadPriority();
1283 }
1284
1285 // Load each of the widget/shortcut previews
1286 ArrayList<Object> items = data.items;
1287 ArrayList<Bitmap> images = data.generatedImages;
1288 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001289 for (int i = 0; i < count; ++i) {
1290 if (task != null) {
1291 // Ensure we haven't been cancelled yet
1292 if (task.isCancelled()) break;
1293 // Before work on each item, ensure that this task is running at the correct
1294 // priority
1295 task.syncThreadPriority();
1296 }
1297
Michael Jurka05713af2013-01-23 12:39:24 +01001298 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001299 }
Winson Chungb44b5242011-06-13 11:32:14 -07001300 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001301
Winson Chungb44b5242011-06-13 11:32:14 -07001302 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001303 if (mInTransition) {
1304 mDeferredSyncWidgetPageItems.add(data);
1305 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001306 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001307 try {
1308 int page = data.page;
1309 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001310
Michael Jurka39e5d172012-03-12 18:36:12 -07001311 ArrayList<Object> items = data.items;
1312 int count = items.size();
1313 for (int i = 0; i < count; ++i) {
1314 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1315 if (widget != null) {
1316 Bitmap preview = data.generatedImages.get(i);
1317 widget.applyPreview(new FastBitmapDrawable(preview), i);
1318 }
1319 }
Winson Chung68e4c642011-11-10 15:48:25 -08001320
Michael Jurka47639b92013-01-14 12:42:27 +01001321 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001322
1323 // Update all thread priorities
1324 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1325 while (iter.hasNext()) {
1326 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1327 int pageIndex = task.page;
1328 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1329 }
1330 } finally {
1331 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001332 }
Winson Chungb44b5242011-06-13 11:32:14 -07001333 }
Winson Chung46af2e82011-05-09 16:00:53 -07001334
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 @Override
1336 public void syncPages() {
1337 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001338 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001339
Adam Cohen0cd3b642011-10-14 14:58:00 -07001340 Context context = getContext();
1341 for (int j = 0; j < mNumWidgetPages; ++j) {
1342 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1343 mWidgetCountY);
1344 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001345 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001346 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001347 }
1348
Adam Cohen0cd3b642011-10-14 14:58:00 -07001349 for (int i = 0; i < mNumAppsPages; ++i) {
1350 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1351 setupPage(layout);
1352 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001353 }
1354 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001355
Winson Chung785d2eb2011-04-14 16:08:02 -07001356 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001357 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001358 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001359 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001360 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001361 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 }
1363 }
1364
Adam Cohen22f823d2011-09-01 17:22:18 -07001365 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1366 // it is in the z-order. This is important to insure touch events are handled correctly.
1367 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001368 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001369 }
1370
Adam Cohenae4f1552011-10-20 00:15:42 -07001371 @Override
1372 protected int indexToPage(int index) {
1373 return getChildCount() - index - 1;
1374 }
1375
Adam Cohen22f823d2011-09-01 17:22:18 -07001376 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1377 @Override
1378 protected void screenScrolled(int screenCenter) {
Winson Chung52aee602013-01-30 12:01:02 -08001379 final boolean isRtl = isLayoutRtl();
Adam Cohen22f823d2011-09-01 17:22:18 -07001380 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001381
1382 for (int i = 0; i < getChildCount(); i++) {
1383 View v = getPageAt(i);
1384 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001385 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001386
Winson Chung52aee602013-01-30 12:01:02 -08001387 float interpolatedProgress;
1388 float translationX;
1389 float maxScrollProgress = Math.max(0, scrollProgress);
1390 float minScrollProgress = Math.min(0, scrollProgress);
1391
1392 if (isRtl) {
1393 translationX = maxScrollProgress * v.getMeasuredWidth();
1394 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(maxScrollProgress));
1395 } else {
1396 translationX = minScrollProgress * v.getMeasuredWidth();
1397 interpolatedProgress = mZInterpolator.getInterpolation(Math.abs(minScrollProgress));
1398 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001399 float scale = (1 - interpolatedProgress) +
1400 interpolatedProgress * TRANSITION_SCALE_FACTOR;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001401
Adam Cohen2591f6a2011-10-25 14:36:40 -07001402 float alpha;
Winson Chung52aee602013-01-30 12:01:02 -08001403 if (isRtl && (scrollProgress > 0)) {
1404 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(maxScrollProgress));
1405 } else if (!isRtl && (scrollProgress < 0)) {
1406 alpha = mAlphaInterpolator.getInterpolation(1 - Math.abs(scrollProgress));
Adam Cohen2591f6a2011-10-25 14:36:40 -07001407 } else {
Winson Chung52aee602013-01-30 12:01:02 -08001408 // On large screens we need to fade the page as it nears its leftmost position
Adam Cohen2591f6a2011-10-25 14:36:40 -07001409 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1410 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001411
1412 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1413 int pageWidth = v.getMeasuredWidth();
1414 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001415
1416 if (PERFORM_OVERSCROLL_ROTATION) {
Winson Chung52aee602013-01-30 12:01:02 -08001417 float xPivot = isRtl ? 1f - TRANSITION_PIVOT : TRANSITION_PIVOT;
1418 boolean isOverscrollingFirstPage = isRtl ? scrollProgress > 0 : scrollProgress < 0;
1419 boolean isOverscrollingLastPage = isRtl ? scrollProgress < 0 : scrollProgress > 0;
1420
1421 if (i == 0 && isOverscrollingFirstPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001422 // Overscroll to the left
Winson Chung52aee602013-01-30 12:01:02 -08001423 v.setPivotX(xPivot * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001424 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1425 scale = 1.0f;
1426 alpha = 1.0f;
1427 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001428 translationX = 0;
Winson Chung52aee602013-01-30 12:01:02 -08001429 } else if (i == getChildCount() - 1 && isOverscrollingLastPage) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001430 // Overscroll to the right
Winson Chung52aee602013-01-30 12:01:02 -08001431 v.setPivotX((1 - xPivot) * pageWidth);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001432 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1433 scale = 1.0f;
1434 alpha = 1.0f;
1435 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001436 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001437 } else {
1438 v.setPivotY(pageHeight / 2.0f);
1439 v.setPivotX(pageWidth / 2.0f);
1440 v.setRotationY(0f);
1441 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001442 }
1443
1444 v.setTranslationX(translationX);
1445 v.setScaleX(scale);
1446 v.setScaleY(scale);
1447 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001448
1449 // If the view has 0 alpha, we set it to be invisible so as to prevent
1450 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001451 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001452 v.setVisibility(INVISIBLE);
1453 } else if (v.getVisibility() != VISIBLE) {
1454 v.setVisibility(VISIBLE);
1455 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001456 }
1457 }
Michael Jurka47639b92013-01-14 12:42:27 +01001458
1459 enableHwLayersOnVisiblePages();
1460 }
1461
1462 private void enableHwLayersOnVisiblePages() {
1463 final int screenCount = getChildCount();
1464
1465 getVisiblePages(mTempVisiblePagesRange);
1466 int leftScreen = mTempVisiblePagesRange[0];
1467 int rightScreen = mTempVisiblePagesRange[1];
1468 int forceDrawScreen = -1;
1469 if (leftScreen == rightScreen) {
1470 // make sure we're caching at least two pages always
1471 if (rightScreen < screenCount - 1) {
1472 rightScreen++;
1473 forceDrawScreen = rightScreen;
1474 } else if (leftScreen > 0) {
1475 leftScreen--;
1476 forceDrawScreen = leftScreen;
1477 }
1478 } else {
1479 forceDrawScreen = leftScreen + 1;
1480 }
1481
1482 for (int i = 0; i < screenCount; i++) {
1483 final View layout = (View) getPageAt(i);
1484 if (!(leftScreen <= i && i <= rightScreen &&
1485 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1486 layout.setLayerType(LAYER_TYPE_NONE, null);
1487 }
1488 }
1489
Michael Jurka47639b92013-01-14 12:42:27 +01001490 for (int i = 0; i < screenCount; i++) {
1491 final View layout = (View) getPageAt(i);
1492
1493 if (leftScreen <= i && i <= rightScreen &&
1494 (i == forceDrawScreen || shouldDrawChild(layout))) {
1495 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1496 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1497 }
1498 }
1499 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001500 }
1501
1502 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001503 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001504 }
1505
Winson Chung785d2eb2011-04-14 16:08:02 -07001506 /**
1507 * Used by the parent to get the content width to set the tab bar to
1508 * @return
1509 */
1510 public int getPageContentWidth() {
1511 return mContentWidth;
1512 }
1513
Winson Chungb26f3d62011-06-02 10:49:29 -07001514 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001515 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001516 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001517 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001518 // We reset the save index when we change pages so that it will be recalculated on next
1519 // rotation
1520 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001521 }
1522
Winson Chung785d2eb2011-04-14 16:08:02 -07001523 /*
1524 * AllAppsView implementation
1525 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001526 public void setup(Launcher launcher, DragController dragController) {
1527 mLauncher = launcher;
1528 mDragController = dragController;
1529 }
Winson Chung9802ac92012-06-08 16:01:58 -07001530
1531 /**
1532 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1533 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1534 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1535 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1536 */
1537 private void invalidateOnDataChange() {
1538 if (!isDataReady()) {
1539 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1540 // request a layout to trigger the page data when ready.
1541 requestLayout();
1542 } else {
1543 cancelAllTasks();
1544 invalidatePageData();
1545 }
1546 }
1547
Winson Chung785d2eb2011-04-14 16:08:02 -07001548 public void setApps(ArrayList<ApplicationInfo> list) {
1549 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001550 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Michael Jurkac402cd92013-05-20 15:49:32 +02001551 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001552 }
1553 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1554 // We add it in place, in alphabetical order
1555 int count = list.size();
1556 for (int i = 0; i < count; ++i) {
1557 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001558 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001559 if (index < 0) {
1560 mApps.add(-(index + 1), info);
1561 }
1562 }
1563 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 public void addApps(ArrayList<ApplicationInfo> list) {
1565 addAppsWithoutInvalidate(list);
Michael Jurkac402cd92013-05-20 15:49:32 +02001566 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001567 }
1568 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1569 ComponentName removeComponent = item.intent.getComponent();
1570 int length = list.size();
1571 for (int i = 0; i < length; ++i) {
1572 ApplicationInfo info = list.get(i);
1573 if (info.intent.getComponent().equals(removeComponent)) {
1574 return i;
1575 }
1576 }
1577 return -1;
1578 }
1579 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1580 // loop through all the apps and remove apps that have the same component
1581 int length = list.size();
1582 for (int i = 0; i < length; ++i) {
1583 ApplicationInfo info = list.get(i);
1584 int removeIndex = findAppByComponent(mApps, info);
1585 if (removeIndex > -1) {
1586 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001587 }
1588 }
1589 }
Winson Chung83892cc2013-05-01 16:53:33 -07001590 public void removeApps(ArrayList<ApplicationInfo> appInfos) {
1591 removeAppsWithoutInvalidate(appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +02001592 updatePageCountsAndInvalidateData();
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);
Michael Jurkac402cd92013-05-20 15:49:32 +02001600 updatePageCountsAndInvalidateData();
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001602
Winson Chung785d2eb2011-04-14 16:08:02 -07001603 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001604 // If we have reset, then we should not continue to restore the previous state
1605 mSaveInstanceStateItemIndex = -1;
1606
Adam Cohenb64d36e2011-10-17 21:48:02 -07001607 AppsCustomizeTabHost tabHost = getTabHost();
1608 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001609 if (tag != null) {
1610 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1611 tabHost.setCurrentTabFromContent(ContentType.Applications);
1612 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001613 }
Winson Chung649668f2012-01-10 13:07:16 -08001614
Adam Cohenb64d36e2011-10-17 21:48:02 -07001615 if (mCurrentPage != 0) {
1616 invalidatePageData(0);
1617 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001619
1620 private AppsCustomizeTabHost getTabHost() {
1621 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1622 }
1623
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 public void dumpState() {
1625 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001626 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1627 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 }
Adam Cohen4e844012011-11-09 13:48:04 -08001629
Winson Chung785d2eb2011-04-14 16:08:02 -07001630 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001631 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001633 for (Object i: list) {
1634 if (i instanceof AppWidgetProviderInfo) {
1635 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1636 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1637 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1638 + " initialLayout=" + info.initialLayout
1639 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1640 } else if (i instanceof ResolveInfo) {
1641 ResolveInfo info = (ResolveInfo) i;
1642 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1643 + info.icon);
1644 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001645 }
1646 }
Adam Cohen4e844012011-11-09 13:48:04 -08001647
Winson Chung785d2eb2011-04-14 16:08:02 -07001648 public void surrender() {
1649 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1650 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001651
1652 // Stop all background tasks
1653 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001654 }
Winson Chung007c6982011-06-14 13:27:53 -07001655
Winson Chunge4e50662012-01-23 14:45:13 -08001656 @Override
1657 public void iconPressed(PagedViewIcon icon) {
1658 // Reset the previously pressed icon and store a reference to the pressed icon so that
1659 // we can reset it on return to Launcher (in Launcher.onResume())
1660 if (mPressedIcon != null) {
1661 mPressedIcon.resetDrawableState();
1662 }
1663 mPressedIcon = icon;
1664 }
1665
1666 public void resetDrawableState() {
1667 if (mPressedIcon != null) {
1668 mPressedIcon.resetDrawableState();
1669 mPressedIcon = null;
1670 }
1671 }
Winson Chung68e4c642011-11-10 15:48:25 -08001672
Winson Chungb44b5242011-06-13 11:32:14 -07001673 /*
1674 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1675 * widget previews in the background with the AsyncTasks.
1676 */
Winson Chung68e4c642011-11-10 15:48:25 -08001677 final static int sLookBehindPageCount = 2;
1678 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001679 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001680 final int count = getChildCount();
1681 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1682 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1683 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001684 }
1685 protected int getAssociatedUpperPageBound(int page) {
1686 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001687 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1688 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1689 count - 1);
1690 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001691 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001692
1693 @Override
1694 protected String getCurrentPageDescription() {
1695 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1696 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001697 int count = 0;
1698
Adam Cohen0cd3b642011-10-14 14:58:00 -07001699 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001700 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001701 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001702 } else {
1703 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001704 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001705 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001706 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001707
Michael Jurka8b805b12012-04-18 14:23:14 -07001708 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001709 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001710}