blob: bfc2db02c4f9a0a512ba9fc3a62e35e1c941eb1a [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chungd2e87b32011-06-02 10:53:07 -070020import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080021import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070029import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.content.res.Resources;
31import android.content.res.TypedArray;
32import android.graphics.Bitmap;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.graphics.Canvas;
Michael Jurka05713af2013-01-23 12:39:24 +010034import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Rect;
36import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070037import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070038import android.os.Build;
39import android.os.Bundle;
Winson Chungb44b5242011-06-13 11:32:14 -070040import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080044import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.LayoutInflater;
46import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070050import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070052import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
54import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import com.android.launcher2.DropTarget.DragObject;
56
57import java.util.ArrayList;
58import java.util.Collections;
59import java.util.Iterator;
60import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070061
Winson Chungb44b5242011-06-13 11:32:14 -070062/**
63 * A simple callback interface which also provides the results of the task.
64 */
65interface AsyncTaskCallback {
66 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
67}
Winson Chung4e076542011-06-23 13:04:10 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * The data needed to perform either of the custom AsyncTasks.
71 */
72class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070073 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080074 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070075 }
76
Michael Jurka038f9d82011-11-03 13:50:45 -070077 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070078 AsyncTaskCallback postR) {
79 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;
86 }
Winson Chung09945932011-09-20 14:22:40 -070087 void cleanup(boolean cancelled) {
88 // Clean up any references to source/generated bitmaps
Winson Chung09945932011-09-20 14:22:40 -070089 if (generatedImages != null) {
90 if (cancelled) {
Michael Jurka05713af2013-01-23 12:39:24 +010091 for (int i = 0; i < generatedImages.size(); i++) {
92 WidgetPreviewLoader.releaseBitmap(items.get(i), generatedImages.get(i));
Winson Chung09945932011-09-20 14:22:40 -070093 }
94 }
95 generatedImages.clear();
96 }
97 }
Winson Chungb44b5242011-06-13 11:32:14 -070098 int page;
99 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700100 ArrayList<Bitmap> sourceImages;
101 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700102 int maxImageWidth;
103 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700104 AsyncTaskCallback doInBackgroundCallback;
105 AsyncTaskCallback postExecuteCallback;
106}
Winson Chung4e076542011-06-23 13:04:10 -0700107
Winson Chungb44b5242011-06-13 11:32:14 -0700108/**
109 * A generic template for an async task used in AppsCustomize.
110 */
111class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700112 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700113 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700114 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700115 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700116 }
117 @Override
118 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
119 if (params.length != 1) return null;
120 // Load each of the widget previews in the background
121 params[0].doInBackgroundCallback.run(this, params[0]);
122 return params[0];
123 }
124 @Override
125 protected void onPostExecute(AsyncTaskPageData result) {
126 // All the widget previews are loaded, so we can just callback to inflate the page
127 result.postExecuteCallback.run(this, result);
128 }
129
130 void setThreadPriority(int p) {
131 threadPriority = p;
132 }
133 void syncThreadPriority() {
134 Process.setThreadPriority(threadPriority);
135 }
136
137 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700138 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 int threadPriority;
141}
Winson Chungb44b5242011-06-13 11:32:14 -0700142
143/**
144 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
145 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700146public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chungcd810732012-06-18 16:45:43 -0700147 View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700148 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
149 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700150 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700151
152 /**
153 * The different content types that this paged view can show.
154 */
155 public enum ContentType {
156 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700157 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700158 }
159
160 // Refs
161 private Launcher mLauncher;
162 private DragController mDragController;
163 private final LayoutInflater mLayoutInflater;
164 private final PackageManager mPackageManager;
165
Winson Chung5afbf7b2011-07-25 11:53:08 -0700166 // Save and Restore
167 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800168 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700169
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700171 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700172 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700173
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800175 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private int mClingFocusedX;
177 private int mClingFocusedY;
178
Winson Chung1ed747a2011-05-03 16:18:34 -0700179 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700180 private Canvas mCanvas;
Winson Chung4dbea792011-05-05 14:21:32 -0700181 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700182
183 // Dimens
184 private int mContentWidth;
Winson Chung6032e7e2011-11-08 15:47:17 -0800185 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700186 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700187 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700189 private int mNumAppsPages;
190 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700191
Adam Cohen22f823d2011-09-01 17:22:18 -0700192 // Relating to the scroll and overscroll effects
193 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700194 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700196 private static float TRANSITION_PIVOT = 0.65f;
197 private static float TRANSITION_MAX_ROTATION = 22;
198 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700199 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700200 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700201
Winson Chungb44b5242011-06-13 11:32:14 -0700202 // Previews & outlines
203 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800204 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700205
Adam Cohened66b2b2012-01-23 17:28:51 -0800206 private Runnable mInflateWidgetRunnable = null;
207 private Runnable mBindWidgetRunnable = null;
208 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -0700209 static final int WIDGET_PRELOAD_PENDING = 0;
210 static final int WIDGET_BOUND = 1;
211 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -0800212 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
213 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800214 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800215 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800216
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700217 private Toast mWidgetInstructionToast;
218
Michael Jurka39e5d172012-03-12 18:36:12 -0700219 // Deferral of loading widget previews during launcher transitions
220 private boolean mInTransition;
221 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
222 new ArrayList<AsyncTaskPageData>();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700223 private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks =
224 new ArrayList<Runnable>();
Michael Jurka39e5d172012-03-12 18:36:12 -0700225
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700226 private Rect mTmpRect = new Rect();
227
Michael Jurkadac85912012-05-18 15:04:49 -0700228 // Used for drawing shortcut previews
229 BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache();
230 PaintCache mCachedShortcutPreviewPaint = new PaintCache();
231 CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache();
232
233 // Used for drawing widget previews
234 CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache();
235 RectCache mCachedAppWidgetPreviewSrcRect = new RectCache();
236 RectCache mCachedAppWidgetPreviewDestRect = new RectCache();
237 PaintCache mCachedAppWidgetPreviewPaint = new PaintCache();
238
Michael Jurka05713af2013-01-23 12:39:24 +0100239 WidgetPreviewLoader mWidgetPreviewLoader;
240
Winson Chung785d2eb2011-04-14 16:08:02 -0700241 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
242 super(context, attrs);
243 mLayoutInflater = LayoutInflater.from(context);
244 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700245 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700246 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700247 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700248 mCanvas = new Canvas();
249 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700250
251 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800252 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
253 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
254 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 mWidgetWidthGap =
256 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
257 mWidgetHeightGap =
258 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700259 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
260 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700261 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
262 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700263 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700265
Winson Chung1ed747a2011-05-03 16:18:34 -0700266 // The padding on the non-matched dimension for the default widget preview icons
267 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700268 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700269
270 // Unless otherwise specified this view is important for accessibility.
271 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
272 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
273 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700274 }
275
276 @Override
277 protected void init() {
278 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700279 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700280
281 Context context = getContext();
282 Resources r = context.getResources();
283 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
284 }
285
Winson Chung5afbf7b2011-07-25 11:53:08 -0700286 /** Returns the item index of the center item on this page so that we can restore to this
287 * item index when we rotate. */
288 private int getMiddleComponentIndexOnCurrentPage() {
289 int i = -1;
290 if (getPageCount() > 0) {
291 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700292 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700293 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700294 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
295 int numItemsPerPage = mCellCountX * mCellCountY;
296 int childCount = childrenLayout.getChildCount();
297 if (childCount > 0) {
298 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700299 }
300 } else {
301 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700302 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700303 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
304 int childCount = layout.getChildCount();
305 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700306 i = numApps +
307 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
308 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700309 }
310 }
311 return i;
312 }
313
314 /** Get the index of the item to restore to if we need to restore the current page. */
315 int getSaveInstanceStateIndex() {
316 if (mSaveInstanceStateItemIndex == -1) {
317 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
318 }
319 return mSaveInstanceStateItemIndex;
320 }
321
322 /** Returns the page in the current orientation which is expected to contain the specified
323 * item index. */
324 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700325 if (index < 0) return 0;
326
327 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700328 int numItemsPerPage = mCellCountX * mCellCountY;
329 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700330 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700331 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700332 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
333 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700334 }
335
Winson Chung5afbf7b2011-07-25 11:53:08 -0700336 /** Restores the page for an item at the specified index */
337 void restorePageForIndex(int index) {
338 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700339 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700340 }
341
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700342 private void updatePageCounts() {
343 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
344 (float) (mWidgetCountX * mWidgetCountY));
345 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
346 }
347
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 protected void onDataReady(int width, int height) {
349 // Note that we transpose the counts in portrait so that we get a similar layout
350 boolean isLandscape = getResources().getConfiguration().orientation ==
351 Configuration.ORIENTATION_LANDSCAPE;
352 int maxCellCountX = Integer.MAX_VALUE;
353 int maxCellCountY = Integer.MAX_VALUE;
354 if (LauncherApplication.isScreenLarge()) {
355 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
356 LauncherModel.getCellCountY());
357 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
358 LauncherModel.getCellCountX());
359 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800360 if (mMaxAppCellCountX > -1) {
361 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
362 }
Michael Jurka244ebcd2012-10-08 17:19:32 +0200363 // Temp hack for now: only use the max cell count Y for widget layout
364 int maxWidgetCellCountY = maxCellCountY;
Winson Chung6032e7e2011-11-08 15:47:17 -0800365 if (mMaxAppCellCountY > -1) {
Michael Jurka244ebcd2012-10-08 17:19:32 +0200366 maxWidgetCellCountY = Math.min(maxWidgetCellCountY, mMaxAppCellCountY);
Winson Chung6032e7e2011-11-08 15:47:17 -0800367 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700368
369 // Now that the data is ready, we can calculate the content width, the number of cells to
370 // use for each page
371 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
372 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
373 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
374 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
375 mCellCountX = mWidgetSpacingLayout.getCellCountX();
376 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700377 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700378
Winson Chungdb1138b2011-06-30 14:39:35 -0700379 // Force a measure to update recalculate the gaps
380 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
381 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Michael Jurka244ebcd2012-10-08 17:19:32 +0200382 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxWidgetCellCountY);
Winson Chungdb1138b2011-06-30 14:39:35 -0700383 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700384 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700385
Michael Jurkae326f182011-11-21 14:05:46 -0800386 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
387 final boolean hostIsTransitioning = host.isTransitioning();
388
Adam Cohen0cd3b642011-10-14 14:58:00 -0700389 // Restore the page
390 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800391 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700392
Winson Chung3f4e1422011-11-17 14:58:51 -0800393 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
394 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
395 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800396 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800397 post(new Runnable() {
398 @Override
399 public void run() {
400 showAllAppsCling();
401 }
402 });
403 }
404 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700405
Winson Chung3f4e1422011-11-17 14:58:51 -0800406 void showAllAppsCling() {
Winson Chung9802ac92012-06-08 16:01:58 -0700407 if (!mHasShownAllAppsCling && isDataReady()) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800408 mHasShownAllAppsCling = true;
409 // Calculate the position for the cling punch through
410 int[] offset = new int[2];
411 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
412 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
413 // PagedViews are centered horizontally but top aligned
Winson Chung7819abd2012-11-29 14:29:38 -0800414 // Note we have to shift the items up now that Launcher sits under the status bar
Winson Chung3f4e1422011-11-17 14:58:51 -0800415 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
416 offset[0];
Winson Chung7819abd2012-11-29 14:29:38 -0800417 pos[1] += offset[1] - mLauncher.getDragLayer().getPaddingTop();
Winson Chung3f4e1422011-11-17 14:58:51 -0800418 mLauncher.showFirstRunAllAppsCling(pos);
419 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700420 }
421
422 @Override
423 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
424 int width = MeasureSpec.getSize(widthMeasureSpec);
425 int height = MeasureSpec.getSize(heightMeasureSpec);
426 if (!isDataReady()) {
Winson Chung9802ac92012-06-08 16:01:58 -0700427 if (!mApps.isEmpty() && !mWidgets.isEmpty()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700428 setDataIsReady();
429 setMeasuredDimension(width, height);
430 onDataReady(width, height);
431 }
432 }
433
434 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
435 }
436
Winson Chung785d2eb2011-04-14 16:08:02 -0700437 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700438 // Get the list of widgets and shortcuts
439 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 List<AppWidgetProviderInfo> widgets =
441 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
443 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800444 for (AppWidgetProviderInfo widget : widgets) {
445 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700446 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700447 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
448 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700449 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
450 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700451 if (minSpanX <= LauncherModel.getCellCountX() &&
452 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700453 mWidgets.add(widget);
Winson Chungfd39d8e2012-06-05 10:12:48 -0700454 } else {
455 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
456 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700457 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800458 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700459 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800460 widget.minWidth + ", " + widget.minHeight + ")");
461 }
462 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700463 mWidgets.addAll(shortcuts);
464 Collections.sort(mWidgets,
465 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700466 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -0700467 invalidateOnDataChange();
Winson Chung4b576be2011-04-27 17:40:20 -0700468 }
469
470 @Override
471 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700472 // When we have exited all apps or are in transition, disregard clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700473 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700474 mLauncher.getWorkspace().isSwitchingState()) return;
475
Winson Chung4b576be2011-04-27 17:40:20 -0700476 if (v instanceof PagedViewIcon) {
477 // Animate some feedback to the click
478 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800479
480 // Lock the drawable state to pressed until we return to Launcher
481 if (mPressedIcon != null) {
482 mPressedIcon.lockDrawableState();
483 }
Winson Chungc7450e32012-04-17 17:34:08 -0700484
Winson Chung18f41f82012-05-09 13:28:10 -0700485 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
486 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
487 // when Launcher resumes and we are still in AllApps.
488 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700489 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
490
Winson Chung4b576be2011-04-27 17:40:20 -0700491 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700492 // Let the user know that they have to long press to add a widget
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700493 if (mWidgetInstructionToast != null) {
494 mWidgetInstructionToast.cancel();
495 }
496 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
497 Toast.LENGTH_SHORT);
498 mWidgetInstructionToast.show();
Winson Chung46af2e82011-05-09 16:00:53 -0700499
Winson Chungd2e87b32011-06-02 10:53:07 -0700500 // Create a little animation to show that the widget can move
501 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
502 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700503 AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet();
504 ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY);
Winson Chungd2e87b32011-06-02 10:53:07 -0700505 tyuAnim.setDuration(125);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700506 ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f);
Winson Chungd2e87b32011-06-02 10:53:07 -0700507 tydAnim.setDuration(100);
508 bounce.play(tyuAnim).before(tydAnim);
509 bounce.setInterpolator(new AccelerateInterpolator());
510 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700511 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700512 }
513
Winson Chungc6f10b92011-11-14 11:39:07 -0800514 public boolean onKey(View v, int keyCode, KeyEvent event) {
515 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
516 }
517
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 /*
519 * PagedViewWithDraggableItems implementation
520 */
521 @Override
522 protected void determineDraggingStart(android.view.MotionEvent ev) {
523 // Disable dragging by pulling an app down for now.
524 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700525
Winson Chung4b576be2011-04-27 17:40:20 -0700526 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700527 mLauncher.getWorkspace().onDragStartedWithItem(v);
528 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700529 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700530
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700531 Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
532 Bundle options = null;
533 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
534 AppWidgetResizeFrame.getWidgetSizeRanges(mLauncher, info.spanX, info.spanY, mTmpRect);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700535 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(mLauncher,
536 info.componentName, null);
537
538 float density = getResources().getDisplayMetrics().density;
539 int xPaddingDips = (int) ((padding.left + padding.right) / density);
540 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
541
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700542 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700543 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
544 mTmpRect.left - xPaddingDips);
545 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
546 mTmpRect.top - yPaddingDips);
547 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
548 mTmpRect.right - xPaddingDips);
549 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
550 mTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700551 }
552 return options;
553 }
554
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700555 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800556 final AppWidgetProviderInfo pInfo = info.info;
Adam Cohendd70d662012-10-04 16:53:44 -0700557 final Bundle options = getDefaultOptionsForWidget(mLauncher, info);
558
Adam Cohened66b2b2012-01-23 17:28:51 -0800559 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700560 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800561 return;
562 }
563
Adam Cohen21a170b2012-05-30 15:17:06 -0700564 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800565 mBindWidgetRunnable = new Runnable() {
566 @Override
567 public void run() {
568 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700569 // Options will be null for platforms with JB or lower, so this serves as an
570 // SDK level check.
571 if (options == null) {
572 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
573 mWidgetLoadingId, info.componentName)) {
574 mWidgetCleanupState = WIDGET_BOUND;
575 }
576 } else {
577 if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
578 mWidgetLoadingId, info.componentName, options)) {
579 mWidgetCleanupState = WIDGET_BOUND;
580 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800582 }
583 };
584 post(mBindWidgetRunnable);
585
586 mInflateWidgetRunnable = new Runnable() {
587 @Override
588 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700589 if (mWidgetCleanupState != WIDGET_BOUND) {
590 return;
591 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700592 AppWidgetHostView hostView = mLauncher.
593 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 info.boundWidget = hostView;
595 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800596 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700597 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
598 info.spanY, info, false);
599
600 // We want the first widget layout to be the correct size. This will be important
601 // for width size reporting to the AppWidgetManager.
602 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
603 unScaledSize[1]);
604 lp.x = lp.y = 0;
605 lp.customPosition = true;
606 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800607 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 }
609 };
610 post(mInflateWidgetRunnable);
611 }
612
613 @Override
614 public void onShortPress(View v) {
615 // We are anticipating a long press, and we use this time to load bind and instantiate
616 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700617 if (mCreateWidgetInfo != null) {
618 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
619 cleanupWidgetPreloading(false);
620 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800621 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700622 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 }
624
Adam Cohen0e56cc92012-05-11 15:57:05 -0700625 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
626 if (!widgetWasAdded) {
627 // If the widget was not added, we may need to do further cleanup.
628 PendingAddWidgetInfo info = mCreateWidgetInfo;
629 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700630
631 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700632 // We never did any preloading, so just remove pending callbacks to do so
633 removeCallbacks(mBindWidgetRunnable);
634 removeCallbacks(mInflateWidgetRunnable);
635 } else if (mWidgetCleanupState == WIDGET_BOUND) {
636 // Delete the widget id which was allocated
637 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700638 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
639 }
640
641 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700642 removeCallbacks(mInflateWidgetRunnable);
643 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700644 // Delete the widget id which was allocated
645 if (mWidgetLoadingId != -1) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700646 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
647 }
648
Adam Cohen0e56cc92012-05-11 15:57:05 -0700649 // The widget was inflated and added to the DragLayer -- remove it.
650 AppWidgetHostView widget = info.boundWidget;
651 mLauncher.getDragLayer().removeView(widget);
652 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 }
654 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
655 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700656 mCreateWidgetInfo = null;
657 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 }
659
Adam Cohen7a326642012-02-22 12:03:22 -0800660 @Override
661 public void cleanUpShortPress(View v) {
662 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700663 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800664 }
665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700667 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800668 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700669 // Get the widget preview as the drag representation
670 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700671 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700672
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700673 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
674 // we abort the drag.
675 if (image.getDrawable() == null) {
676 mDraggingWidget = false;
677 return false;
678 }
679
Winson Chung4b576be2011-04-27 17:40:20 -0700680 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800681 Bitmap preview;
682 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800683 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100684 Point previewPadding = null;
685
Winson Chung1ed747a2011-05-03 16:18:34 -0700686 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700687 // This can happen in some weird cases involving multi-touch. We can't start dragging
688 // the widget if this is null, so we break out.
689 if (mCreateWidgetInfo == null) {
690 return false;
691 }
692
Adam Cohen1b36dc32012-02-13 19:27:37 -0800693 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
694 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700695 int spanX = createItemInfo.spanX;
696 int spanY = createItemInfo.spanY;
697 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
698 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700699
Winson Chung72d59842012-02-22 13:51:36 -0800700 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
701 float minScale = 1.25f;
Michael Jurkadac85912012-05-18 15:04:49 -0700702 int maxWidth, maxHeight;
703 maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
704 maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung72d59842012-02-22 13:51:36 -0800705
Michael Jurka05713af2013-01-23 12:39:24 +0100706 int[] previewSizeBeforeScale = new int[1];
707
708 preview = mWidgetPreviewLoader.generateWidgetPreview(createWidgetInfo.componentName,
709 createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY,
710 maxWidth, maxHeight, null, previewSizeBeforeScale);
711
712 // Compare the size of the drag preview to the preview in the AppsCustomize tray
713 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
714 mWidgetPreviewLoader.maxWidthForWidgetPreview(spanX));
715 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
716
717 // The bitmap in the AppsCustomize tray is always the the same size, so there
718 // might be extra pixels around the preview itself - this accounts for that
719 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
720 int padding =
721 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
722 previewPadding = new Point(padding, 0);
723 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700724 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700725 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
726 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
727 preview = Bitmap.createBitmap(icon.getIntrinsicWidth(),
728 icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
729
Winson Chung1120e032011-11-22 16:11:31 -0800730 mCanvas.setBitmap(preview);
Michael Jurka4ca39222012-05-15 17:18:34 -0700731 mCanvas.save();
Michael Jurka05713af2013-01-23 12:39:24 +0100732 WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0,
Michael Jurkadac85912012-05-18 15:04:49 -0700733 icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
Michael Jurka4ca39222012-05-15 17:18:34 -0700734 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700735 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700736 createItemInfo.spanX = createItemInfo.spanY = 1;
737 }
Winson Chung4b576be2011-04-27 17:40:20 -0700738
Michael Jurka8c3339b2012-06-14 16:18:21 -0700739 // Don't clip alpha values for the drag outline if we're using the default widget preview
740 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
741 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700742
Winson Chung1120e032011-11-22 16:11:31 -0800743 // Save the preview for the outline generation, then dim the preview
744 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
745 false);
Winson Chung1120e032011-11-22 16:11:31 -0800746
Winson Chung4b576be2011-04-27 17:40:20 -0700747 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700748 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700749 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800750 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100751 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800752 outline.recycle();
753 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700754 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700755 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800756
Winson Chung4b576be2011-04-27 17:40:20 -0700757 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800758 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700759 if (!super.beginDragging(v)) return false;
760
761 if (v instanceof PagedViewIcon) {
762 beginDraggingApplication(v);
763 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700764 if (!beginDraggingWidget(v)) {
765 return false;
766 }
Winson Chung4b576be2011-04-27 17:40:20 -0700767 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800768
769 // We delay entering spring-loaded mode slightly to make sure the UI
770 // thready is free of any work.
771 postDelayed(new Runnable() {
772 @Override
773 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800774 // We don't enter spring-loaded mode if the drag has been cancelled
775 if (mLauncher.getDragController().isDragging()) {
776 // Dismiss the cling
777 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800778
Adam Cohen1b36dc32012-02-13 19:27:37 -0800779 // Reset the alpha on the dragged icon before we drag
780 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800781
Adam Cohen1b36dc32012-02-13 19:27:37 -0800782 // Go into spring loaded mode (must happen before we startDrag())
783 mLauncher.enterSpringLoadedDragMode();
784 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 }
Winson Chung72d59842012-02-22 13:51:36 -0800786 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800787
Winson Chung785d2eb2011-04-14 16:08:02 -0700788 return true;
789 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800790
Winson Chunga48487a2012-03-20 16:19:37 -0700791 /**
792 * Clean up after dragging.
793 *
794 * @param target where the item was dragged to (can be null if the item was flung)
795 */
796 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700797 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700798 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700799 // Exit spring loaded mode if we have not successfully dropped or have not handled the
800 // drop in Workspace
801 mLauncher.exitSpringLoadedDragMode();
802 }
Winson Chung4b919f82012-05-01 10:44:08 -0700803 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700804 }
805
Winson Chung785d2eb2011-04-14 16:08:02 -0700806 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700807 public View getContent() {
808 return null;
809 }
810
811 @Override
812 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700813 mInTransition = true;
814 if (toWorkspace) {
815 cancelAllTasks();
816 }
817 }
818
819 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700820 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700821 }
822
823 @Override
824 public void onLauncherTransitionStep(Launcher l, float t) {
825 }
826
827 @Override
828 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
829 mInTransition = false;
830 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
831 onSyncWidgetPageItems(d);
832 }
833 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700834 for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) {
835 r.run();
836 }
837 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700838 mForceDrawAllChildrenNextFrame = !toWorkspace;
Michael Jurka39e5d172012-03-12 18:36:12 -0700839 }
840
841 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700842 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
843 boolean success) {
844 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
845 if (isFlingToDelete) return;
846
847 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700848
849 // Display an error message if the drag failed due to there not being enough space on the
850 // target layout we were dropping on.
851 if (!success) {
852 boolean showOutOfSpaceMessage = false;
853 if (target instanceof Workspace) {
854 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
855 Workspace workspace = (Workspace) target;
856 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700857 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700858 if (layout != null) {
859 layout.calculateSpans(itemInfo);
860 showOutOfSpaceMessage =
861 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
862 }
863 }
Winson Chungfc79c802011-05-02 13:35:34 -0700864 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700865 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700866 }
Adam Cohen7a326642012-02-22 12:03:22 -0800867
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800868 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700869 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700870 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800871 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700872 }
873
Winson Chunga48487a2012-03-20 16:19:37 -0700874 @Override
875 public void onFlingToDeleteCompleted() {
876 // We just dismiss the drag when we fling, so cleanup here
877 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700878 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700879 mDraggingWidget = false;
880 }
881
882 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800883 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700884 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800885 }
886
Winson Chung7f0acdd2011-09-19 18:34:19 -0700887 @Override
888 protected void onDetachedFromWindow() {
889 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700890 cancelAllTasks();
891 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700892
Michael Jurkae326f182011-11-21 14:05:46 -0800893 public void clearAllWidgetPages() {
894 cancelAllTasks();
895 int count = getChildCount();
896 for (int i = 0; i < count; i++) {
897 View v = getPageAt(i);
898 if (v instanceof PagedViewGridLayout) {
899 ((PagedViewGridLayout) v).removeAllViewsOnPage();
900 mDirtyPageContent.set(i, true);
901 }
902 }
903 }
904
Adam Cohen0cd3b642011-10-14 14:58:00 -0700905 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700906 // Clean up all the async tasks
907 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
908 while (iter.hasNext()) {
909 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
910 task.cancel(false);
911 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700912 mDirtyPageContent.set(task.page, true);
Winson Chung7ce99852012-05-24 17:34:08 -0700913
914 // We've already preallocated the views for the data to load into, so clear them as well
915 View v = getPageAt(task.page);
916 if (v instanceof PagedViewGridLayout) {
917 ((PagedViewGridLayout) v).removeAllViewsOnPage();
918 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700919 }
Winson Chung83687b12012-04-25 16:01:01 -0700920 mDeferredSyncWidgetPageItems.clear();
Michael Jurkaf6a96902012-06-06 11:48:13 -0700921 mDeferredPrepareLoadWidgetPreviewsTasks.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700922 }
923
Winson Chung785d2eb2011-04-14 16:08:02 -0700924 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700925 if (type == ContentType.Widgets) {
926 invalidatePageData(mNumAppsPages, true);
927 } else if (type == ContentType.Applications) {
928 invalidatePageData(0, true);
929 }
Winson Chungb44b5242011-06-13 11:32:14 -0700930 }
931
Adam Cohen0cd3b642011-10-14 14:58:00 -0700932 protected void snapToPage(int whichPage, int delta, int duration) {
933 super.snapToPage(whichPage, delta, duration);
934 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800935
936 // Update the thread priorities given the direction lookahead
937 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
938 while (iter.hasNext()) {
939 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700940 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800941 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
942 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
943 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
944 } else {
945 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
946 }
947 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700948 }
949
950 private void updateCurrentTab(int currentPage) {
951 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800952 if (tabHost != null) {
953 String tag = tabHost.getCurrentTabTag();
954 if (tag != null) {
955 if (currentPage >= mNumAppsPages &&
956 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
957 tabHost.setCurrentTabFromContent(ContentType.Widgets);
958 } else if (currentPage < mNumAppsPages &&
959 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
960 tabHost.setCurrentTabFromContent(ContentType.Applications);
961 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700962 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700963 }
964 }
965
Winson Chung785d2eb2011-04-14 16:08:02 -0700966 /*
967 * Apps PagedView implementation
968 */
Winson Chung63257c12011-05-05 17:06:13 -0700969 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
970 int childCount = layout.getChildCount();
971 for (int i = 0; i < childCount; ++i) {
972 layout.getChildAt(i).setVisibility(visibility);
973 }
974 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 private void setupPage(PagedViewCellLayout layout) {
976 layout.setCellCount(mCellCountX, mCellCountY);
977 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
978 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
979 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
980
Winson Chung63257c12011-05-05 17:06:13 -0700981 // Note: We force a measure here to get around the fact that when we do layout calculations
982 // immediately after syncing, we don't have a proper width. That said, we already know the
983 // expected page width, so we can actually optimize by hiding all the TextView-based
984 // children that are expensive to measure, and let that happen naturally later.
985 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700986 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700988 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700989 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700990 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700992
Winson Chungf314b0e2011-08-16 11:54:27 -0700993 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 int numCells = mCellCountX * mCellCountY;
996 int startIndex = page * numCells;
997 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700998 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700999
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -07001001 ArrayList<Object> items = new ArrayList<Object>();
1002 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -07001003 for (int i = startIndex; i < endIndex; ++i) {
1004 ApplicationInfo info = mApps.get(i);
1005 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
1006 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -08001007 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001008 icon.setOnClickListener(this);
1009 icon.setOnLongClickListener(this);
1010 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001011 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -07001012
1013 int index = i - startIndex;
1014 int x = index % mCellCountX;
1015 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -07001016 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -07001017
1018 items.add(info);
1019 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -07001020 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001021
Michael Jurka47639b92013-01-14 12:42:27 +01001022 enableHwLayersOnVisiblePages();
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 }
Winson Chungb44b5242011-06-13 11:32:14 -07001024
1025 /**
Winson Chung68e4c642011-11-10 15:48:25 -08001026 * A helper to return the priority for loading of the specified widget page.
1027 */
1028 private int getWidgetPageLoadPriority(int page) {
1029 // If we are snapping to another page, use that index as the target page index
1030 int toPage = mCurrentPage;
1031 if (mNextPage > -1) {
1032 toPage = mNextPage;
1033 }
1034
1035 // We use the distance from the target page as an initial guess of priority, but if there
1036 // are no pages of higher priority than the page specified, then bump up the priority of
1037 // the specified page.
1038 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1039 int minPageDiff = Integer.MAX_VALUE;
1040 while (iter.hasNext()) {
1041 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001042 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001043 }
1044
1045 int rawPageDiff = Math.abs(page - toPage);
1046 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1047 }
1048 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001049 * Return the appropriate thread priority for loading for a given page (we give the current
1050 * page much higher priority)
1051 */
1052 private int getThreadPriorityForPage(int page) {
1053 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001054 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001055 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001056 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001057 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001058 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001059 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001060 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001061 }
1062 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001063 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001064 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001065 return Math.max(0, pageDiff * sPageSleepDelay);
1066 }
Winson Chungb44b5242011-06-13 11:32:14 -07001067 /**
1068 * Creates and executes a new AsyncTask to load a page of widget previews.
1069 */
1070 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001071 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001072
Winson Chungb44b5242011-06-13 11:32:14 -07001073 // Prune all tasks that are no longer needed
1074 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1075 while (iter.hasNext()) {
1076 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001077 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001078 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1079 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001080 task.cancel(false);
1081 iter.remove();
1082 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001083 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001084 }
1085 }
1086
Winson Chungf314b0e2011-08-16 11:54:27 -07001087 // 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 -07001088 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001089 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001090 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001091 @Override
1092 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001093 try {
Winson Chung09945932011-09-20 14:22:40 -07001094 try {
1095 Thread.sleep(sleepMs);
1096 } catch (Exception e) {}
1097 loadWidgetPreviewsInBackground(task, data);
1098 } finally {
1099 if (task.isCancelled()) {
1100 data.cleanup(true);
1101 }
1102 }
Winson Chungb44b5242011-06-13 11:32:14 -07001103 }
1104 },
1105 new AsyncTaskCallback() {
1106 @Override
1107 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001108 mRunningTasks.remove(task);
1109 if (task.isCancelled()) return;
1110 // do cleanup inside onSyncWidgetPageItems
1111 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001112 }
Winson Chung09945932011-09-20 14:22:40 -07001113 });
Winson Chungb44b5242011-06-13 11:32:14 -07001114
1115 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001116 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001117 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001118 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001119 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1120 mRunningTasks.add(t);
1121 }
Winson Chungb44b5242011-06-13 11:32:14 -07001122
Winson Chung785d2eb2011-04-14 16:08:02 -07001123 /*
1124 * Widgets PagedView implementation
1125 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001126 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001127 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1128 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001129
1130 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001131 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001132 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1133 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001135 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001137
Michael Jurka038f9d82011-11-03 13:50:45 -07001138 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001139 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001140
Winson Chungd2945262011-06-24 15:22:14 -07001141 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001142 final ArrayList<Object> items = new ArrayList<Object>();
1143 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1144 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001145 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001146 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1147 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001148 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001149
Winson Chunge4a647f2011-09-30 14:41:25 -07001150 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001151 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001152 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1153 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001154 }
1155
Winson Chunge4a647f2011-09-30 14:41:25 -07001156 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001157 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001158 layout.setColumnCount(layout.getCellCountX());
1159 for (int i = 0; i < items.size(); ++i) {
1160 Object rawInfo = items.get(i);
1161 PendingAddItemInfo createItemInfo = null;
1162 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1163 R.layout.apps_customize_widget, layout, false);
1164 if (rawInfo instanceof AppWidgetProviderInfo) {
1165 // Fill in the widget information
1166 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1167 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001168
1169 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001170 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001171 createItemInfo.spanX = spanXY[0];
1172 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001173 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001174 createItemInfo.minSpanX = minSpanXY[0];
1175 createItemInfo.minSpanY = minSpanXY[1];
1176
1177 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001178 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001179 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001180 } else if (rawInfo instanceof ResolveInfo) {
1181 // Fill in the shortcuts information
1182 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -07001183 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -07001184 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1185 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1186 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001187 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001188 widget.setTag(createItemInfo);
1189 }
1190 widget.setOnClickListener(this);
1191 widget.setOnLongClickListener(this);
1192 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001193 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001194
1195 // Layout each widget
1196 int ix = i % mWidgetCountX;
1197 int iy = i / mWidgetCountX;
1198 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001199 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -07001200 GridLayout.spec(ix, GridLayout.TOP));
1201 lp.width = cellWidth;
1202 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -08001203 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -07001204 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1205 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1206 layout.addView(widget, lp);
1207 }
1208
Michael Jurka038f9d82011-11-03 13:50:45 -07001209 // wait until a call on onLayout to start loading, because
1210 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1211 // TODO: can we do a measure/layout immediately?
1212 layout.setOnLayoutListener(new Runnable() {
1213 public void run() {
1214 // Load the widget previews
1215 int maxPreviewWidth = cellWidth;
1216 int maxPreviewHeight = cellHeight;
1217 if (layout.getChildCount() > 0) {
1218 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1219 int[] maxSize = w.getPreviewSize();
1220 maxPreviewWidth = maxSize[0];
1221 maxPreviewHeight = maxSize[1];
1222 }
Michael Jurka05713af2013-01-23 12:39:24 +01001223
1224 if (mWidgetPreviewLoader == null) {
1225 mWidgetPreviewLoader = new WidgetPreviewLoader(
1226 maxPreviewWidth, maxPreviewHeight, mLauncher, mWidgetSpacingLayout);
1227 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001228 if (immediate) {
1229 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1230 maxPreviewWidth, maxPreviewHeight, null, null);
1231 loadWidgetPreviewsInBackground(null, data);
1232 onSyncWidgetPageItems(data);
1233 } else {
Michael Jurkaf6a96902012-06-06 11:48:13 -07001234 if (mInTransition) {
1235 mDeferredPrepareLoadWidgetPreviewsTasks.add(this);
1236 } else {
1237 prepareLoadWidgetPreviewsTask(page, items,
1238 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1239 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001240 }
1241 }
1242 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001243 }
1244 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1245 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001246 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1247 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001248 if (task != null) {
1249 // Ensure that this task starts running at the correct priority
1250 task.syncThreadPriority();
1251 }
1252
1253 // Load each of the widget/shortcut previews
1254 ArrayList<Object> items = data.items;
1255 ArrayList<Bitmap> images = data.generatedImages;
1256 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001257 for (int i = 0; i < count; ++i) {
1258 if (task != null) {
1259 // Ensure we haven't been cancelled yet
1260 if (task.isCancelled()) break;
1261 // Before work on each item, ensure that this task is running at the correct
1262 // priority
1263 task.syncThreadPriority();
1264 }
1265
Michael Jurka05713af2013-01-23 12:39:24 +01001266 images.add(mWidgetPreviewLoader.getPreview(items.get(i)));
Winson Chungf314b0e2011-08-16 11:54:27 -07001267 }
Winson Chungb44b5242011-06-13 11:32:14 -07001268 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001269
Winson Chungb44b5242011-06-13 11:32:14 -07001270 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001271 if (mInTransition) {
1272 mDeferredSyncWidgetPageItems.add(data);
1273 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001274 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001275 try {
1276 int page = data.page;
1277 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001278
Michael Jurka39e5d172012-03-12 18:36:12 -07001279 ArrayList<Object> items = data.items;
1280 int count = items.size();
1281 for (int i = 0; i < count; ++i) {
1282 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1283 if (widget != null) {
1284 Bitmap preview = data.generatedImages.get(i);
1285 widget.applyPreview(new FastBitmapDrawable(preview), i);
1286 }
1287 }
Winson Chung68e4c642011-11-10 15:48:25 -08001288
Michael Jurka47639b92013-01-14 12:42:27 +01001289 enableHwLayersOnVisiblePages();
Michael Jurka39e5d172012-03-12 18:36:12 -07001290
1291 // Update all thread priorities
1292 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1293 while (iter.hasNext()) {
1294 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1295 int pageIndex = task.page;
1296 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1297 }
1298 } finally {
1299 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001300 }
Winson Chungb44b5242011-06-13 11:32:14 -07001301 }
Winson Chung46af2e82011-05-09 16:00:53 -07001302
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 @Override
1304 public void syncPages() {
1305 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001306 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001307
Adam Cohen0cd3b642011-10-14 14:58:00 -07001308 Context context = getContext();
1309 for (int j = 0; j < mNumWidgetPages; ++j) {
1310 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1311 mWidgetCountY);
1312 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001313 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001314 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001315 }
1316
Adam Cohen0cd3b642011-10-14 14:58:00 -07001317 for (int i = 0; i < mNumAppsPages; ++i) {
1318 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1319 setupPage(layout);
1320 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 }
1322 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001323
Winson Chung785d2eb2011-04-14 16:08:02 -07001324 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001325 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001326 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001327 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001328 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001329 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 }
1331 }
1332
Adam Cohen22f823d2011-09-01 17:22:18 -07001333 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1334 // it is in the z-order. This is important to insure touch events are handled correctly.
1335 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001336 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001337 }
1338
Adam Cohenae4f1552011-10-20 00:15:42 -07001339 @Override
1340 protected int indexToPage(int index) {
1341 return getChildCount() - index - 1;
1342 }
1343
Adam Cohen22f823d2011-09-01 17:22:18 -07001344 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1345 @Override
1346 protected void screenScrolled(int screenCenter) {
1347 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001348
1349 for (int i = 0; i < getChildCount(); i++) {
1350 View v = getPageAt(i);
1351 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001352 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001353
1354 float interpolatedProgress =
1355 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1356 float scale = (1 - interpolatedProgress) +
1357 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1358 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001359
Adam Cohen2591f6a2011-10-25 14:36:40 -07001360 float alpha;
1361
Winson Chungd167e2a2012-04-26 13:13:01 -07001362 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001363 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001364 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001365 } else {
1366 // On large screens we need to fade the page as it nears its leftmost position
1367 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1368 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001369
1370 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1371 int pageWidth = v.getMeasuredWidth();
1372 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001373
1374 if (PERFORM_OVERSCROLL_ROTATION) {
1375 if (i == 0 && scrollProgress < 0) {
1376 // Overscroll to the left
1377 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1378 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1379 scale = 1.0f;
1380 alpha = 1.0f;
1381 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001382 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001383 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1384 // Overscroll to the right
1385 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1386 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1387 scale = 1.0f;
1388 alpha = 1.0f;
1389 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001390 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001391 } else {
1392 v.setPivotY(pageHeight / 2.0f);
1393 v.setPivotX(pageWidth / 2.0f);
1394 v.setRotationY(0f);
1395 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001396 }
1397
1398 v.setTranslationX(translationX);
1399 v.setScaleX(scale);
1400 v.setScaleY(scale);
1401 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001402
1403 // If the view has 0 alpha, we set it to be invisible so as to prevent
1404 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001405 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001406 v.setVisibility(INVISIBLE);
1407 } else if (v.getVisibility() != VISIBLE) {
1408 v.setVisibility(VISIBLE);
1409 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001410 }
1411 }
Michael Jurka47639b92013-01-14 12:42:27 +01001412
1413 enableHwLayersOnVisiblePages();
1414 }
1415
1416 private void enableHwLayersOnVisiblePages() {
1417 final int screenCount = getChildCount();
1418
1419 getVisiblePages(mTempVisiblePagesRange);
1420 int leftScreen = mTempVisiblePagesRange[0];
1421 int rightScreen = mTempVisiblePagesRange[1];
1422 int forceDrawScreen = -1;
1423 if (leftScreen == rightScreen) {
1424 // make sure we're caching at least two pages always
1425 if (rightScreen < screenCount - 1) {
1426 rightScreen++;
1427 forceDrawScreen = rightScreen;
1428 } else if (leftScreen > 0) {
1429 leftScreen--;
1430 forceDrawScreen = leftScreen;
1431 }
1432 } else {
1433 forceDrawScreen = leftScreen + 1;
1434 }
1435
1436 for (int i = 0; i < screenCount; i++) {
1437 final View layout = (View) getPageAt(i);
1438 if (!(leftScreen <= i && i <= rightScreen &&
1439 (i == forceDrawScreen || shouldDrawChild(layout)))) {
1440 layout.setLayerType(LAYER_TYPE_NONE, null);
1441 }
1442 }
1443
Michael Jurka47639b92013-01-14 12:42:27 +01001444 for (int i = 0; i < screenCount; i++) {
1445 final View layout = (View) getPageAt(i);
1446
1447 if (leftScreen <= i && i <= rightScreen &&
1448 (i == forceDrawScreen || shouldDrawChild(layout))) {
1449 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
1450 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
1451 }
1452 }
1453 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001454 }
1455
1456 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001457 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 }
1459
Winson Chung785d2eb2011-04-14 16:08:02 -07001460 /**
1461 * Used by the parent to get the content width to set the tab bar to
1462 * @return
1463 */
1464 public int getPageContentWidth() {
1465 return mContentWidth;
1466 }
1467
Winson Chungb26f3d62011-06-02 10:49:29 -07001468 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001469 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001470 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -07001471 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -07001472 // We reset the save index when we change pages so that it will be recalculated on next
1473 // rotation
1474 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001475 }
1476
Winson Chung785d2eb2011-04-14 16:08:02 -07001477 /*
1478 * AllAppsView implementation
1479 */
Winson Chung785d2eb2011-04-14 16:08:02 -07001480 public void setup(Launcher launcher, DragController dragController) {
1481 mLauncher = launcher;
1482 mDragController = dragController;
1483 }
Winson Chung9802ac92012-06-08 16:01:58 -07001484
1485 /**
1486 * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can
1487 * appropriately determine when to invalidate the PagedView page data. In cases where the data
1488 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
1489 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
1490 */
1491 private void invalidateOnDataChange() {
1492 if (!isDataReady()) {
1493 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1494 // request a layout to trigger the page data when ready.
1495 requestLayout();
1496 } else {
1497 cancelAllTasks();
1498 invalidatePageData();
1499 }
1500 }
1501
Winson Chung785d2eb2011-04-14 16:08:02 -07001502 public void setApps(ArrayList<ApplicationInfo> list) {
1503 mApps = list;
Winson Chung11904872012-09-17 16:58:46 -07001504 Collections.sort(mApps, LauncherModel.getAppNameComparator());
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001505 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001506 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001507 }
1508 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1509 // We add it in place, in alphabetical order
1510 int count = list.size();
1511 for (int i = 0; i < count; ++i) {
1512 ApplicationInfo info = list.get(i);
Winson Chung11904872012-09-17 16:58:46 -07001513 int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator());
Winson Chung785d2eb2011-04-14 16:08:02 -07001514 if (index < 0) {
1515 mApps.add(-(index + 1), info);
1516 }
1517 }
1518 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001519 public void addApps(ArrayList<ApplicationInfo> list) {
1520 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001521 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001522 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001523 }
1524 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1525 ComponentName removeComponent = item.intent.getComponent();
1526 int length = list.size();
1527 for (int i = 0; i < length; ++i) {
1528 ApplicationInfo info = list.get(i);
1529 if (info.intent.getComponent().equals(removeComponent)) {
1530 return i;
1531 }
1532 }
1533 return -1;
1534 }
Winson Chungcd810732012-06-18 16:45:43 -07001535 private int findAppByPackage(List<ApplicationInfo> list, String packageName) {
1536 int length = list.size();
1537 for (int i = 0; i < length; ++i) {
1538 ApplicationInfo info = list.get(i);
1539 if (ItemInfo.getPackageName(info.intent).equals(packageName)) {
1540 return i;
1541 }
1542 }
1543 return -1;
1544 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001545 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1546 // loop through all the apps and remove apps that have the same component
1547 int length = list.size();
1548 for (int i = 0; i < length; ++i) {
1549 ApplicationInfo info = list.get(i);
1550 int removeIndex = findAppByComponent(mApps, info);
1551 if (removeIndex > -1) {
1552 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001553 }
1554 }
1555 }
Winson Chungcd810732012-06-18 16:45:43 -07001556 private void removeAppsWithPackageNameWithoutInvalidate(ArrayList<String> packageNames) {
1557 // loop through all the package names and remove apps that have the same package name
1558 for (String pn : packageNames) {
1559 int removeIndex = findAppByPackage(mApps, pn);
1560 while (removeIndex > -1) {
1561 mApps.remove(removeIndex);
1562 removeIndex = findAppByPackage(mApps, pn);
1563 }
1564 }
1565 }
1566 public void removeApps(ArrayList<String> packageNames) {
1567 removeAppsWithPackageNameWithoutInvalidate(packageNames);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001568 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001569 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001571 public void updateApps(ArrayList<ApplicationInfo> list) {
1572 // We remove and re-add the updated applications list because it's properties may have
1573 // changed (ie. the title), and this will ensure that the items will be in their proper
1574 // place in the list.
1575 removeAppsWithoutInvalidate(list);
1576 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001577 updatePageCounts();
Winson Chung9802ac92012-06-08 16:01:58 -07001578 invalidateOnDataChange();
Winson Chung785d2eb2011-04-14 16:08:02 -07001579 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001580
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001582 // If we have reset, then we should not continue to restore the previous state
1583 mSaveInstanceStateItemIndex = -1;
1584
Adam Cohenb64d36e2011-10-17 21:48:02 -07001585 AppsCustomizeTabHost tabHost = getTabHost();
1586 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001587 if (tag != null) {
1588 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1589 tabHost.setCurrentTabFromContent(ContentType.Applications);
1590 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001591 }
Winson Chung649668f2012-01-10 13:07:16 -08001592
Adam Cohenb64d36e2011-10-17 21:48:02 -07001593 if (mCurrentPage != 0) {
1594 invalidatePageData(0);
1595 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001597
1598 private AppsCustomizeTabHost getTabHost() {
1599 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1600 }
1601
Winson Chung785d2eb2011-04-14 16:08:02 -07001602 public void dumpState() {
1603 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001604 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1605 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001606 }
Adam Cohen4e844012011-11-09 13:48:04 -08001607
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001609 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001610 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001611 for (Object i: list) {
1612 if (i instanceof AppWidgetProviderInfo) {
1613 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1614 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1615 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1616 + " initialLayout=" + info.initialLayout
1617 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1618 } else if (i instanceof ResolveInfo) {
1619 ResolveInfo info = (ResolveInfo) i;
1620 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1621 + info.icon);
1622 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 }
1624 }
Adam Cohen4e844012011-11-09 13:48:04 -08001625
Winson Chung785d2eb2011-04-14 16:08:02 -07001626 public void surrender() {
1627 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1628 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001629
1630 // Stop all background tasks
1631 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 }
Winson Chung007c6982011-06-14 13:27:53 -07001633
Winson Chunge4e50662012-01-23 14:45:13 -08001634 @Override
1635 public void iconPressed(PagedViewIcon icon) {
1636 // Reset the previously pressed icon and store a reference to the pressed icon so that
1637 // we can reset it on return to Launcher (in Launcher.onResume())
1638 if (mPressedIcon != null) {
1639 mPressedIcon.resetDrawableState();
1640 }
1641 mPressedIcon = icon;
1642 }
1643
1644 public void resetDrawableState() {
1645 if (mPressedIcon != null) {
1646 mPressedIcon.resetDrawableState();
1647 mPressedIcon = null;
1648 }
1649 }
Winson Chung68e4c642011-11-10 15:48:25 -08001650
Winson Chungb44b5242011-06-13 11:32:14 -07001651 /*
1652 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1653 * widget previews in the background with the AsyncTasks.
1654 */
Winson Chung68e4c642011-11-10 15:48:25 -08001655 final static int sLookBehindPageCount = 2;
1656 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001657 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001658 final int count = getChildCount();
1659 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1660 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1661 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001662 }
1663 protected int getAssociatedUpperPageBound(int page) {
1664 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001665 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1666 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1667 count - 1);
1668 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001669 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001670
1671 @Override
1672 protected String getCurrentPageDescription() {
1673 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1674 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001675 int count = 0;
1676
Adam Cohen0cd3b642011-10-14 14:58:00 -07001677 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001678 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001679 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001680 } else {
1681 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001682 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001683 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001684 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001685
Michael Jurka8b805b12012-04-18 14:23:14 -07001686 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001687 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001688}