blob: 5c91d35ccd6999c0ea07e8684127b7d6ee4f735b [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 Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Adam Cohened66b2b2012-01-23 17:28:51 -080022import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Adam Cohen4e844012011-11-09 13:48:04 -080034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070036import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080037import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070038import android.graphics.Paint;
Winson Chung785d2eb2011-04-14 16:08:02 -070039import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080040import android.graphics.RectF;
Peter Ng8db70002011-10-25 15:40:08 -070041import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070042import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070043import android.os.AsyncTask;
44import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.util.AttributeSet;
46import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070047import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080048import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070050import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070051import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070052import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070053import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070054import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070055import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070056import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070057import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
59import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import com.android.launcher2.DropTarget.DragObject;
61
62import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Iterator;
65import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070066
Winson Chungb44b5242011-06-13 11:32:14 -070067/**
68 * A simple callback interface which also provides the results of the task.
69 */
70interface AsyncTaskCallback {
71 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
72}
Winson Chung4e076542011-06-23 13:04:10 -070073
Winson Chungb44b5242011-06-13 11:32:14 -070074/**
75 * The data needed to perform either of the custom AsyncTasks.
76 */
77class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070078 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080079 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070080 }
81
Winson Chungb44b5242011-06-13 11:32:14 -070082 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
83 AsyncTaskCallback postR) {
84 page = p;
85 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070086 sourceImages = si;
87 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070088 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070089 doInBackgroundCallback = bgR;
90 postExecuteCallback = postR;
91 }
Michael Jurka038f9d82011-11-03 13:50:45 -070092 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070093 AsyncTaskCallback postR) {
94 page = p;
95 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070096 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070097 maxImageWidth = cw;
98 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -070099 doInBackgroundCallback = bgR;
100 postExecuteCallback = postR;
101 }
Winson Chung09945932011-09-20 14:22:40 -0700102 void cleanup(boolean cancelled) {
103 // Clean up any references to source/generated bitmaps
104 if (sourceImages != null) {
105 if (cancelled) {
106 for (Bitmap b : sourceImages) {
107 b.recycle();
108 }
109 }
110 sourceImages.clear();
111 }
112 if (generatedImages != null) {
113 if (cancelled) {
114 for (Bitmap b : generatedImages) {
115 b.recycle();
116 }
117 }
118 generatedImages.clear();
119 }
120 }
Winson Chungb44b5242011-06-13 11:32:14 -0700121 int page;
122 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700123 ArrayList<Bitmap> sourceImages;
124 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700125 int maxImageWidth;
126 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700127 AsyncTaskCallback doInBackgroundCallback;
128 AsyncTaskCallback postExecuteCallback;
129}
Winson Chung4e076542011-06-23 13:04:10 -0700130
Winson Chungb44b5242011-06-13 11:32:14 -0700131/**
132 * A generic template for an async task used in AppsCustomize.
133 */
134class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700135 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700136 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700137 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700138 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 }
140 @Override
141 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
142 if (params.length != 1) return null;
143 // Load each of the widget previews in the background
144 params[0].doInBackgroundCallback.run(this, params[0]);
145 return params[0];
146 }
147 @Override
148 protected void onPostExecute(AsyncTaskPageData result) {
149 // All the widget previews are loaded, so we can just callback to inflate the page
150 result.postExecuteCallback.run(this, result);
151 }
152
153 void setThreadPriority(int p) {
154 threadPriority = p;
155 }
156 void syncThreadPriority() {
157 Process.setThreadPriority(threadPriority);
158 }
159
160 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700161 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700162 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int threadPriority;
164}
Winson Chungb44b5242011-06-13 11:32:14 -0700165
166/**
167 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
168 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700169public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800170 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700171 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
172 LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700173 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -0700174
175 /**
176 * The different content types that this paged view can show.
177 */
178 public enum ContentType {
179 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700180 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700181 }
182
183 // Refs
184 private Launcher mLauncher;
185 private DragController mDragController;
186 private final LayoutInflater mLayoutInflater;
187 private final PackageManager mPackageManager;
188
Winson Chung5afbf7b2011-07-25 11:53:08 -0700189 // Save and Restore
190 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800191 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700192
Winson Chung785d2eb2011-04-14 16:08:02 -0700193 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700195 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700196
Winson Chung7d7541e2011-09-16 20:14:36 -0700197 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800198 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700199 private int mClingFocusedX;
200 private int mClingFocusedY;
201
Winson Chung1ed747a2011-05-03 16:18:34 -0700202 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700203 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700204 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700205 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700206
207 // Dimens
208 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700209 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800210 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700211 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700212 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700213 private final int mWidgetPreviewIconPaddedDimension;
214 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700215 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700216 private int mNumAppsPages;
217 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700218
Adam Cohen22f823d2011-09-01 17:22:18 -0700219 // Relating to the scroll and overscroll effects
220 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700221 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700222 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700223 private static float TRANSITION_PIVOT = 0.65f;
224 private static float TRANSITION_MAX_ROTATION = 22;
225 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700226 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700227 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700228
Winson Chungb44b5242011-06-13 11:32:14 -0700229 // Previews & outlines
230 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800231 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700232
Adam Cohened66b2b2012-01-23 17:28:51 -0800233 private Runnable mInflateWidgetRunnable = null;
234 private Runnable mBindWidgetRunnable = null;
235 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
236 static final int WIDGET_BOUND = 0;
237 static final int WIDGET_INFLATED = 1;
238 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
239 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800240 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800241 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800242
Michael Jurka39e5d172012-03-12 18:36:12 -0700243 // Deferral of loading widget previews during launcher transitions
244 private boolean mInTransition;
245 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
246 new ArrayList<AsyncTaskPageData>();
247
Winson Chung785d2eb2011-04-14 16:08:02 -0700248 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
249 super(context, attrs);
250 mLayoutInflater = LayoutInflater.from(context);
251 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700252 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700254 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700255 mCanvas = new Canvas();
256 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700257
258 // Save the default widget preview background
259 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700260 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700261 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700262
Winson Chung6032e7e2011-11-08 15:47:17 -0800263 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
264 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
265 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 mWidgetWidthGap =
267 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
268 mWidgetHeightGap =
269 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700270 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
271 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700272 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
273 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700274 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700275 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700276
Winson Chung1ed747a2011-05-03 16:18:34 -0700277 // The padding on the non-matched dimension for the default widget preview icons
278 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700279 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700280 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700281 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700282 }
283
284 @Override
285 protected void init() {
286 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700287 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700288
289 Context context = getContext();
290 Resources r = context.getResources();
291 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
292 }
293
Winson Chungf0ea4d32011-06-06 14:27:16 -0700294 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700295 protected void onUnhandledTap(MotionEvent ev) {
296 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700297 // Dismiss AppsCustomize if we tap
298 mLauncher.showWorkspace(true);
299 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700300 }
301
Winson Chung5afbf7b2011-07-25 11:53:08 -0700302 /** Returns the item index of the center item on this page so that we can restore to this
303 * item index when we rotate. */
304 private int getMiddleComponentIndexOnCurrentPage() {
305 int i = -1;
306 if (getPageCount() > 0) {
307 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700308 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700309 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700310 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
311 int numItemsPerPage = mCellCountX * mCellCountY;
312 int childCount = childrenLayout.getChildCount();
313 if (childCount > 0) {
314 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700315 }
316 } else {
317 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700318 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700319 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
320 int childCount = layout.getChildCount();
321 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700322 i = numApps +
323 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
324 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700325 }
326 }
327 return i;
328 }
329
330 /** Get the index of the item to restore to if we need to restore the current page. */
331 int getSaveInstanceStateIndex() {
332 if (mSaveInstanceStateItemIndex == -1) {
333 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
334 }
335 return mSaveInstanceStateItemIndex;
336 }
337
338 /** Returns the page in the current orientation which is expected to contain the specified
339 * item index. */
340 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700341 if (index < 0) return 0;
342
343 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700344 int numItemsPerPage = mCellCountX * mCellCountY;
345 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700346 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700347 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700348 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
349 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700350 }
351
Winson Chungf0ea4d32011-06-06 14:27:16 -0700352 /**
353 * This differs from isDataReady as this is the test done if isDataReady is not set.
354 */
355 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700356 // We only do this test once, and we default to the Applications page, so we only really
357 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700358 // TODO: What if one of them is validly empty
359 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 }
361
Winson Chung5afbf7b2011-07-25 11:53:08 -0700362 /** Restores the page for an item at the specified index */
363 void restorePageForIndex(int index) {
364 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700365 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700366 }
367
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700368 private void updatePageCounts() {
369 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
370 (float) (mWidgetCountX * mWidgetCountY));
371 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
372 }
373
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 protected void onDataReady(int width, int height) {
375 // Note that we transpose the counts in portrait so that we get a similar layout
376 boolean isLandscape = getResources().getConfiguration().orientation ==
377 Configuration.ORIENTATION_LANDSCAPE;
378 int maxCellCountX = Integer.MAX_VALUE;
379 int maxCellCountY = Integer.MAX_VALUE;
380 if (LauncherApplication.isScreenLarge()) {
381 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
382 LauncherModel.getCellCountY());
383 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
384 LauncherModel.getCellCountX());
385 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800386 if (mMaxAppCellCountX > -1) {
387 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
388 }
389 if (mMaxAppCellCountY > -1) {
390 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
391 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700392
393 // Now that the data is ready, we can calculate the content width, the number of cells to
394 // use for each page
395 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
396 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
397 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
398 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
399 mCellCountX = mWidgetSpacingLayout.getCellCountX();
400 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700401 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700402
Winson Chungdb1138b2011-06-30 14:39:35 -0700403 // Force a measure to update recalculate the gaps
404 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
405 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
406 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700407 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700408
Michael Jurkae326f182011-11-21 14:05:46 -0800409 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
410 final boolean hostIsTransitioning = host.isTransitioning();
411
Adam Cohen0cd3b642011-10-14 14:58:00 -0700412 // Restore the page
413 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800414 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700415
Winson Chung3f4e1422011-11-17 14:58:51 -0800416 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
417 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
418 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800419 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800420 post(new Runnable() {
421 @Override
422 public void run() {
423 showAllAppsCling();
424 }
425 });
426 }
427 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700428
Winson Chung3f4e1422011-11-17 14:58:51 -0800429 void showAllAppsCling() {
430 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
431 mHasShownAllAppsCling = true;
432 // Calculate the position for the cling punch through
433 int[] offset = new int[2];
434 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
435 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
436 // PagedViews are centered horizontally but top aligned
437 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
438 offset[0];
439 pos[1] += offset[1];
440 mLauncher.showFirstRunAllAppsCling(pos);
441 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 }
443
444 @Override
445 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
446 int width = MeasureSpec.getSize(widthMeasureSpec);
447 int height = MeasureSpec.getSize(heightMeasureSpec);
448 if (!isDataReady()) {
449 if (testDataReady()) {
450 setDataIsReady();
451 setMeasuredDimension(width, height);
452 onDataReady(width, height);
453 }
454 }
455
456 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
457 }
458
Winson Chung785d2eb2011-04-14 16:08:02 -0700459 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700460 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
461 // by a broadcast receiver, and in order for it to work correctly, we need to know that
462 // the AppWidgetService has already received and processed the same broadcast. Since there
463 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
464 // we should have a more precise way of ensuring the AppWidgetService is up to date.
465 postDelayed(new Runnable() {
466 public void run() {
467 updatePackages();
468 }
469 }, 500);
470 }
471
472 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700473 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700474 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700475 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700476 List<AppWidgetProviderInfo> widgets =
477 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700478 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
479 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800480 for (AppWidgetProviderInfo widget : widgets) {
481 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700482 // Ensure that all widgets we show can be added on a workspace of this size
Adam Cohen2f093b62012-04-30 18:59:53 -0700483 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
484 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
Winson Chunga5c96362012-04-12 14:04:41 -0700485 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
486 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700487 if (minSpanX <= LauncherModel.getCellCountX() &&
488 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700489 mWidgets.add(widget);
490 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800491 } else {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700492 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
Michael Jurkadbc1f652011-11-10 17:02:56 -0800493 widget.minWidth + ", " + widget.minHeight + ")");
494 }
495 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700496 mWidgets.addAll(shortcuts);
497 Collections.sort(mWidgets,
498 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700499 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700500
Winson Chung875de7e2011-06-28 14:25:17 -0700501 if (wasEmpty) {
502 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
503 // a layout to do this test and invalidate the page data when ready.
504 if (testDataReady()) requestLayout();
505 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700506 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700507 invalidatePageData();
508 }
Winson Chung4b576be2011-04-27 17:40:20 -0700509 }
510
511 @Override
512 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700513 // When we have exited all apps or are in transition, disregard clicks
514 if (!mLauncher.isAllAppsCustomizeOpen() ||
515 mLauncher.getWorkspace().isSwitchingState()) return;
516
Winson Chung4b576be2011-04-27 17:40:20 -0700517 if (v instanceof PagedViewIcon) {
518 // Animate some feedback to the click
519 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800520
521 // Lock the drawable state to pressed until we return to Launcher
522 if (mPressedIcon != null) {
523 mPressedIcon.lockDrawableState();
524 }
Winson Chungc7450e32012-04-17 17:34:08 -0700525
Winson Chung18f41f82012-05-09 13:28:10 -0700526 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
527 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
528 // when Launcher resumes and we are still in AllApps.
529 mLauncher.updateWallpaperVisibility(true);
Winson Chungc7450e32012-04-17 17:34:08 -0700530 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
531
Winson Chung4b576be2011-04-27 17:40:20 -0700532 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700533 // Let the user know that they have to long press to add a widget
534 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
535 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700536
Winson Chungd2e87b32011-06-02 10:53:07 -0700537 // Create a little animation to show that the widget can move
538 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
539 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
540 AnimatorSet bounce = new AnimatorSet();
541 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
542 tyuAnim.setDuration(125);
543 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
544 tydAnim.setDuration(100);
545 bounce.play(tyuAnim).before(tydAnim);
546 bounce.setInterpolator(new AccelerateInterpolator());
547 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700548 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 }
550
Winson Chungc6f10b92011-11-14 11:39:07 -0800551 public boolean onKey(View v, int keyCode, KeyEvent event) {
552 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
553 }
554
Winson Chung785d2eb2011-04-14 16:08:02 -0700555 /*
556 * PagedViewWithDraggableItems implementation
557 */
558 @Override
559 protected void determineDraggingStart(android.view.MotionEvent ev) {
560 // Disable dragging by pulling an app down for now.
561 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700562
Winson Chung4b576be2011-04-27 17:40:20 -0700563 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700564 mLauncher.getWorkspace().onDragStartedWithItem(v);
565 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700566 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700567
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700568 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800569 final AppWidgetProviderInfo pInfo = info.info;
570 if (pInfo.configure != null) {
571 return;
572 }
573
574 mBindWidgetRunnable = new Runnable() {
575 @Override
576 public void run() {
577 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700578 if (AppWidgetManager.getInstance(mLauncher)
579 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
580 mWidgetCleanupState = WIDGET_BOUND;
581 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800582 }
583 };
584 post(mBindWidgetRunnable);
585
586 mInflateWidgetRunnable = new Runnable() {
587 @Override
588 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 AppWidgetHostView hostView = mLauncher.
590 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 info.boundWidget = hostView;
592 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800593 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700594 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
595 info.spanY, info, false);
596
597 // We want the first widget layout to be the correct size. This will be important
598 // for width size reporting to the AppWidgetManager.
599 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
600 unScaledSize[1]);
601 lp.x = lp.y = 0;
602 lp.customPosition = true;
603 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800604 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 }
606 };
607 post(mInflateWidgetRunnable);
608 }
609
610 @Override
611 public void onShortPress(View v) {
612 // We are anticipating a long press, and we use this time to load bind and instantiate
613 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700614 if (mCreateWidgetInfo != null) {
615 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
616 cleanupWidgetPreloading(false);
617 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800618 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700619 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800620 }
621
Adam Cohen0e56cc92012-05-11 15:57:05 -0700622 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
623 if (!widgetWasAdded) {
624 // If the widget was not added, we may need to do further cleanup.
625 PendingAddWidgetInfo info = mCreateWidgetInfo;
626 mCreateWidgetInfo = null;
627 // First step was to allocate a widget id, revert that.
628 if ((mWidgetCleanupState == WIDGET_BOUND || mWidgetCleanupState == WIDGET_INFLATED) &&
629 mWidgetLoadingId != -1) {
630 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
631 }
632 if (mWidgetCleanupState == WIDGET_BOUND) {
633 // We never actually inflated the widget, so remove the callback to do so.
634 removeCallbacks(mInflateWidgetRunnable);
635 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
636 // The widget was inflated and added to the DragLayer -- remove it.
637 AppWidgetHostView widget = info.boundWidget;
638 mLauncher.getDragLayer().removeView(widget);
639 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 }
641 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
642 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700643 mCreateWidgetInfo = null;
644 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800645 }
646
Adam Cohen7a326642012-02-22 12:03:22 -0800647 @Override
648 public void cleanUpShortPress(View v) {
649 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700650 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800651 }
652 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800653
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700654 private boolean beginDraggingWidget(View v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800655 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700656 // Get the widget preview as the drag representation
657 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700658 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700659
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700660 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
661 // we abort the drag.
662 if (image.getDrawable() == null) {
663 mDraggingWidget = false;
664 return false;
665 }
666
Winson Chung4b576be2011-04-27 17:40:20 -0700667 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800668 Bitmap preview;
669 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800670 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700671 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800672 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
673 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700674 int spanX = createItemInfo.spanX;
675 int spanY = createItemInfo.spanY;
676 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
677 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700678
Winson Chung72d59842012-02-22 13:51:36 -0800679 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
680 float minScale = 1.25f;
681 int minWidth, minHeight;
682 minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
683 minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800684 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohen1f362702012-04-04 14:58:12 -0700685 createWidgetInfo.icon, spanX, spanY, minWidth, minHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800686
687 // Determine the image view drawable scale relative to the preview
688 float[] mv = new float[9];
689 Matrix m = new Matrix();
690 m.setRectToRect(
691 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
692 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
693 (float) previewDrawable.getIntrinsicHeight()),
694 Matrix.ScaleToFit.START);
695 m.getValues(mv);
696 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700697 } else {
698 // Workaround for the fact that we don't keep the original ResolveInfo associated with
699 // the shortcut around. To get the icon, we just render the preview image (which has
700 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800701 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700702 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800703 Drawable d = image.getDrawable();
704 mCanvas.setBitmap(preview);
Michael Jurka1202b332012-05-14 19:33:37 -0700705 mCanvas.translate((mWidgetPreviewIconPaddedDimension - d.getIntrinsicWidth()) / 2,
706 (mWidgetPreviewIconPaddedDimension - d.getIntrinsicHeight()) / 2);
Winson Chung1120e032011-11-22 16:11:31 -0800707 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700708 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700709 createItemInfo.spanX = createItemInfo.spanY = 1;
710 }
Winson Chung4b576be2011-04-27 17:40:20 -0700711
Peter Ng8db70002011-10-25 15:40:08 -0700712 // We use a custom alpha clip table for the default widget previews
713 Paint alphaClipPaint = null;
714 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700715 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700716 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
717 alphaClipPaint = new Paint();
718 alphaClipPaint.setMaskFilter(alphaClipTable);
719 }
720 }
721
Winson Chung1120e032011-11-22 16:11:31 -0800722 // Save the preview for the outline generation, then dim the preview
723 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
724 false);
Winson Chung1120e032011-11-22 16:11:31 -0800725
Winson Chung4b576be2011-04-27 17:40:20 -0700726 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800727 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700728 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800729 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
730 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800731 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800732 outline.recycle();
733 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700734 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700735 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800736
Winson Chung4b576be2011-04-27 17:40:20 -0700737 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700739 if (!super.beginDragging(v)) return false;
740
741 if (v instanceof PagedViewIcon) {
742 beginDraggingApplication(v);
743 } else if (v instanceof PagedViewWidget) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700744 if (!beginDraggingWidget(v)) {
745 return false;
746 }
Winson Chung4b576be2011-04-27 17:40:20 -0700747 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800748
749 // We delay entering spring-loaded mode slightly to make sure the UI
750 // thready is free of any work.
751 postDelayed(new Runnable() {
752 @Override
753 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800754 // We don't enter spring-loaded mode if the drag has been cancelled
755 if (mLauncher.getDragController().isDragging()) {
756 // Dismiss the cling
757 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800758
Adam Cohen1b36dc32012-02-13 19:27:37 -0800759 // Reset the alpha on the dragged icon before we drag
760 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800761
Adam Cohen1b36dc32012-02-13 19:27:37 -0800762 // Go into spring loaded mode (must happen before we startDrag())
763 mLauncher.enterSpringLoadedDragMode();
764 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 }
Winson Chung72d59842012-02-22 13:51:36 -0800766 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800767
Winson Chung785d2eb2011-04-14 16:08:02 -0700768 return true;
769 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800770
Winson Chunga48487a2012-03-20 16:19:37 -0700771 /**
772 * Clean up after dragging.
773 *
774 * @param target where the item was dragged to (can be null if the item was flung)
775 */
776 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700777 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700778 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700779 // Exit spring loaded mode if we have not successfully dropped or have not handled the
780 // drop in Workspace
781 mLauncher.exitSpringLoadedDragMode();
782 }
Winson Chung4b919f82012-05-01 10:44:08 -0700783 mLauncher.unlockScreenOrientation(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700784 }
785
Winson Chung785d2eb2011-04-14 16:08:02 -0700786 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700787 public View getContent() {
788 return null;
789 }
790
791 @Override
792 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700793 mInTransition = true;
794 if (toWorkspace) {
795 cancelAllTasks();
796 }
797 }
798
799 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700800 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700801 }
802
803 @Override
804 public void onLauncherTransitionStep(Launcher l, float t) {
805 }
806
807 @Override
808 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
809 mInTransition = false;
810 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
811 onSyncWidgetPageItems(d);
812 }
813 mDeferredSyncWidgetPageItems.clear();
814 }
815
816 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700817 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
818 boolean success) {
819 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
820 if (isFlingToDelete) return;
821
822 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700823
824 // Display an error message if the drag failed due to there not being enough space on the
825 // target layout we were dropping on.
826 if (!success) {
827 boolean showOutOfSpaceMessage = false;
828 if (target instanceof Workspace) {
829 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
830 Workspace workspace = (Workspace) target;
831 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700832 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700833 if (layout != null) {
834 layout.calculateSpans(itemInfo);
835 showOutOfSpaceMessage =
836 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
837 }
838 }
Winson Chungfc79c802011-05-02 13:35:34 -0700839 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700840 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700841 }
Adam Cohen7a326642012-02-22 12:03:22 -0800842
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800843 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700844 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700845 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800846 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700847 }
848
Winson Chunga48487a2012-03-20 16:19:37 -0700849 @Override
850 public void onFlingToDeleteCompleted() {
851 // We just dismiss the drag when we fling, so cleanup here
852 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700853 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700854 mDraggingWidget = false;
855 }
856
857 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800858 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700859 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800860 }
861
Winson Chung7f0acdd2011-09-19 18:34:19 -0700862 @Override
863 protected void onDetachedFromWindow() {
864 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700865 cancelAllTasks();
866 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700867
Michael Jurkae326f182011-11-21 14:05:46 -0800868 public void clearAllWidgetPages() {
869 cancelAllTasks();
870 int count = getChildCount();
871 for (int i = 0; i < count; i++) {
872 View v = getPageAt(i);
873 if (v instanceof PagedViewGridLayout) {
874 ((PagedViewGridLayout) v).removeAllViewsOnPage();
875 mDirtyPageContent.set(i, true);
876 }
877 }
878 }
879
Adam Cohen0cd3b642011-10-14 14:58:00 -0700880 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700881 // Clean up all the async tasks
882 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
883 while (iter.hasNext()) {
884 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
885 task.cancel(false);
886 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700887 mDirtyPageContent.set(task.page, true);
Winson Chung7f0acdd2011-09-19 18:34:19 -0700888 }
Winson Chung83687b12012-04-25 16:01:01 -0700889 mDeferredSyncWidgetPageItems.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700890 }
891
Winson Chung785d2eb2011-04-14 16:08:02 -0700892 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700893 if (type == ContentType.Widgets) {
894 invalidatePageData(mNumAppsPages, true);
895 } else if (type == ContentType.Applications) {
896 invalidatePageData(0, true);
897 }
Winson Chungb44b5242011-06-13 11:32:14 -0700898 }
899
Adam Cohen0cd3b642011-10-14 14:58:00 -0700900 protected void snapToPage(int whichPage, int delta, int duration) {
901 super.snapToPage(whichPage, delta, duration);
902 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800903
904 // Update the thread priorities given the direction lookahead
905 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
906 while (iter.hasNext()) {
907 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700908 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800909 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
910 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
911 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
912 } else {
913 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
914 }
915 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700916 }
917
918 private void updateCurrentTab(int currentPage) {
919 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800920 if (tabHost != null) {
921 String tag = tabHost.getCurrentTabTag();
922 if (tag != null) {
923 if (currentPage >= mNumAppsPages &&
924 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
925 tabHost.setCurrentTabFromContent(ContentType.Widgets);
926 } else if (currentPage < mNumAppsPages &&
927 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
928 tabHost.setCurrentTabFromContent(ContentType.Applications);
929 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700930 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700931 }
932 }
933
Winson Chung785d2eb2011-04-14 16:08:02 -0700934 /*
935 * Apps PagedView implementation
936 */
Winson Chung63257c12011-05-05 17:06:13 -0700937 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
938 int childCount = layout.getChildCount();
939 for (int i = 0; i < childCount; ++i) {
940 layout.getChildAt(i).setVisibility(visibility);
941 }
942 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700943 private void setupPage(PagedViewCellLayout layout) {
944 layout.setCellCount(mCellCountX, mCellCountY);
945 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
946 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
947 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
948
Winson Chung63257c12011-05-05 17:06:13 -0700949 // Note: We force a measure here to get around the fact that when we do layout calculations
950 // immediately after syncing, we don't have a proper width. That said, we already know the
951 // expected page width, so we can actually optimize by hiding all the TextView-based
952 // children that are expensive to measure, and let that happen naturally later.
953 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700954 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700955 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700956 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700957 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700958 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700959 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700960
Winson Chungf314b0e2011-08-16 11:54:27 -0700961 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700962 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 int numCells = mCellCountX * mCellCountY;
964 int startIndex = page * numCells;
965 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700966 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700967
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700969 ArrayList<Object> items = new ArrayList<Object>();
970 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 for (int i = startIndex; i < endIndex; ++i) {
972 ApplicationInfo info = mApps.get(i);
973 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
974 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800975 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 icon.setOnClickListener(this);
977 icon.setOnLongClickListener(this);
978 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800979 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700980
981 int index = i - startIndex;
982 int x = index % mCellCountX;
983 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700984 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700985
986 items.add(info);
987 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700989
Winson Chungf0ea4d32011-06-06 14:27:16 -0700990 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 }
Winson Chungb44b5242011-06-13 11:32:14 -0700992
993 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800994 * A helper to return the priority for loading of the specified widget page.
995 */
996 private int getWidgetPageLoadPriority(int page) {
997 // If we are snapping to another page, use that index as the target page index
998 int toPage = mCurrentPage;
999 if (mNextPage > -1) {
1000 toPage = mNextPage;
1001 }
1002
1003 // We use the distance from the target page as an initial guess of priority, but if there
1004 // are no pages of higher priority than the page specified, then bump up the priority of
1005 // the specified page.
1006 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1007 int minPageDiff = Integer.MAX_VALUE;
1008 while (iter.hasNext()) {
1009 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001010 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -08001011 }
1012
1013 int rawPageDiff = Math.abs(page - toPage);
1014 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
1015 }
1016 /**
Winson Chungb44b5242011-06-13 11:32:14 -07001017 * Return the appropriate thread priority for loading for a given page (we give the current
1018 * page much higher priority)
1019 */
1020 private int getThreadPriorityForPage(int page) {
1021 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -08001022 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001023 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -08001024 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -07001025 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001026 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001027 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001028 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001029 }
1030 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001031 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001032 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001033 return Math.max(0, pageDiff * sPageSleepDelay);
1034 }
Winson Chungb44b5242011-06-13 11:32:14 -07001035 /**
1036 * Creates and executes a new AsyncTask to load a page of widget previews.
1037 */
1038 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001039 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001040
Winson Chungb44b5242011-06-13 11:32:14 -07001041 // Prune all tasks that are no longer needed
1042 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1043 while (iter.hasNext()) {
1044 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001045 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001046 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1047 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001048 task.cancel(false);
1049 iter.remove();
1050 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001051 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001052 }
1053 }
1054
Winson Chungf314b0e2011-08-16 11:54:27 -07001055 // 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 -07001056 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001057 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001058 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001059 @Override
1060 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001061 try {
Winson Chung09945932011-09-20 14:22:40 -07001062 try {
1063 Thread.sleep(sleepMs);
1064 } catch (Exception e) {}
1065 loadWidgetPreviewsInBackground(task, data);
1066 } finally {
1067 if (task.isCancelled()) {
1068 data.cleanup(true);
1069 }
1070 }
Winson Chungb44b5242011-06-13 11:32:14 -07001071 }
1072 },
1073 new AsyncTaskCallback() {
1074 @Override
1075 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001076 mRunningTasks.remove(task);
1077 if (task.isCancelled()) return;
1078 // do cleanup inside onSyncWidgetPageItems
1079 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001080 }
Winson Chung09945932011-09-20 14:22:40 -07001081 });
Winson Chungb44b5242011-06-13 11:32:14 -07001082
1083 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001084 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001085 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001086 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001087 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1088 mRunningTasks.add(t);
1089 }
Winson Chungb44b5242011-06-13 11:32:14 -07001090
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 /*
1092 * Widgets PagedView implementation
1093 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001094 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001095 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1096 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001097
1098 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001099 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001100 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1101 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001102 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001103 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001104 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001105
Winson Chung5fc72b32011-10-11 17:53:58 -07001106 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
1107 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -07001108 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001109
Winson Chung5fc72b32011-10-11 17:53:58 -07001110 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
1111 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -07001112 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001113 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001114 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001115 Rect oldBounds = d.copyBounds();
1116 d.setBounds(x, y, x + w, y + h);
1117 d.draw(c);
1118 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001119 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001120 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001122 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001123 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001124 int offset = 0;
1125 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001126 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001127
Winson Chung1ed747a2011-05-03 16:18:34 -07001128 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001129 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001130 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001131 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001132 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001133
Michael Jurka038f9d82011-11-03 13:50:45 -07001134 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1135 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001136 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001137 String packageName = provider.getPackageName();
1138 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1139 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001140
Michael Jurka038f9d82011-11-03 13:50:45 -07001141 Drawable drawable = null;
1142 if (previewImage != 0) {
1143 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1144 if (drawable == null) {
Adam Cohen0e56cc92012-05-11 15:57:05 -07001145 Log.w(TAG, "Can't load widget preview drawable 0x" +
Michael Jurka038f9d82011-11-03 13:50:45 -07001146 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001147 }
1148 }
1149
Michael Jurka038f9d82011-11-03 13:50:45 -07001150 int bitmapWidth;
1151 int bitmapHeight;
1152 boolean widgetPreviewExists = (drawable != null);
1153 if (widgetPreviewExists) {
1154 bitmapWidth = drawable.getIntrinsicWidth();
1155 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001156 } else {
Michael Jurkac7e52f52012-03-26 06:20:31 -07001157 if (cellHSpan < 1) cellHSpan = 1;
1158 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurka038f9d82011-11-03 13:50:45 -07001159 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001160 // TODO: This actually uses the apps customize cell layout params, where as we make want
1161 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001162 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1163 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001164 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001165 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001166 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001167 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001168 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001169 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001170 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001171 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001172 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001173 }
1174
1175 float scale = 1f;
1176 if (bitmapWidth > maxWidth) {
1177 scale = maxWidth / (float) bitmapWidth;
1178 }
1179 if (bitmapHeight * scale > maxHeight) {
1180 scale = maxHeight / (float) bitmapHeight;
1181 }
1182 if (scale != 1f) {
1183 bitmapWidth = (int) (scale * bitmapWidth);
1184 bitmapHeight = (int) (scale * bitmapHeight);
1185 }
1186
1187 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1188
1189 if (widgetPreviewExists) {
1190 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1191 } else {
1192 // Generate a preview image if we couldn't load one
1193 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1194 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1195 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001196 if (cellHSpan != 1 || cellVSpan != 1) {
1197 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1198 bitmapHeight);
1199 }
Winson Chung4b576be2011-04-27 17:40:20 -07001200
1201 // Draw the icon in the top left corner
1202 try {
1203 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001204 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1205 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001206 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001207 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001208 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1209
Peter Ng8db70002011-10-25 15:40:08 -07001210 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1211 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001212 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001213 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001214 }
Winson Chungb44b5242011-06-13 11:32:14 -07001215 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001216 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001217
Michael Jurka038f9d82011-11-03 13:50:45 -07001218 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001219 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001220
Winson Chungd2945262011-06-24 15:22:14 -07001221 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001222 final ArrayList<Object> items = new ArrayList<Object>();
1223 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1224 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001225 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001226 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1227 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001228 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001229
Winson Chunge4a647f2011-09-30 14:41:25 -07001230 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001231 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001232 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1233 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001234 }
1235
Winson Chunge4a647f2011-09-30 14:41:25 -07001236 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001237 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001238 layout.setColumnCount(layout.getCellCountX());
1239 for (int i = 0; i < items.size(); ++i) {
1240 Object rawInfo = items.get(i);
1241 PendingAddItemInfo createItemInfo = null;
1242 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1243 R.layout.apps_customize_widget, layout, false);
1244 if (rawInfo instanceof AppWidgetProviderInfo) {
1245 // Fill in the widget information
1246 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1247 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001248
1249 // Determine the widget spans and min resize spans.
Adam Cohen2f093b62012-04-30 18:59:53 -07001250 int[] spanXY = Launcher.getSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001251 createItemInfo.spanX = spanXY[0];
1252 createItemInfo.spanY = spanXY[1];
Adam Cohen2f093b62012-04-30 18:59:53 -07001253 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info);
Adam Cohen1f362702012-04-04 14:58:12 -07001254 createItemInfo.minSpanX = minSpanXY[0];
1255 createItemInfo.minSpanY = minSpanXY[1];
1256
1257 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001258 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001259 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001260 } else if (rawInfo instanceof ResolveInfo) {
1261 // Fill in the shortcuts information
1262 ResolveInfo info = (ResolveInfo) rawInfo;
1263 createItemInfo = new PendingAddItemInfo();
1264 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1265 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1266 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001267 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001268 widget.setTag(createItemInfo);
1269 }
1270 widget.setOnClickListener(this);
1271 widget.setOnLongClickListener(this);
1272 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001273 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001274
1275 // Layout each widget
1276 int ix = i % mWidgetCountX;
1277 int iy = i / mWidgetCountX;
1278 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1279 GridLayout.spec(iy, GridLayout.LEFT),
1280 GridLayout.spec(ix, GridLayout.TOP));
1281 lp.width = cellWidth;
1282 lp.height = cellHeight;
1283 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1284 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1285 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1286 layout.addView(widget, lp);
1287 }
1288
Michael Jurka038f9d82011-11-03 13:50:45 -07001289 // wait until a call on onLayout to start loading, because
1290 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1291 // TODO: can we do a measure/layout immediately?
1292 layout.setOnLayoutListener(new Runnable() {
1293 public void run() {
1294 // Load the widget previews
1295 int maxPreviewWidth = cellWidth;
1296 int maxPreviewHeight = cellHeight;
1297 if (layout.getChildCount() > 0) {
1298 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1299 int[] maxSize = w.getPreviewSize();
1300 maxPreviewWidth = maxSize[0];
1301 maxPreviewHeight = maxSize[1];
1302 }
1303 if (immediate) {
1304 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1305 maxPreviewWidth, maxPreviewHeight, null, null);
1306 loadWidgetPreviewsInBackground(null, data);
1307 onSyncWidgetPageItems(data);
1308 } else {
1309 prepareLoadWidgetPreviewsTask(page, items,
1310 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1311 }
1312 }
1313 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001314 }
1315 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1316 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001317 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1318 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001319 if (task != null) {
1320 // Ensure that this task starts running at the correct priority
1321 task.syncThreadPriority();
1322 }
1323
1324 // Load each of the widget/shortcut previews
1325 ArrayList<Object> items = data.items;
1326 ArrayList<Bitmap> images = data.generatedImages;
1327 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001328 for (int i = 0; i < count; ++i) {
1329 if (task != null) {
1330 // Ensure we haven't been cancelled yet
1331 if (task.isCancelled()) break;
1332 // Before work on each item, ensure that this task is running at the correct
1333 // priority
1334 task.syncThreadPriority();
1335 }
1336
1337 Object rawInfo = items.get(i);
1338 if (rawInfo instanceof AppWidgetProviderInfo) {
1339 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohen2f093b62012-04-30 18:59:53 -07001340 int[] cellSpans = Launcher.getSpanForWidget(mLauncher, info);
Winson Chung72d59842012-02-22 13:51:36 -08001341
1342 int maxWidth = Math.min(data.maxImageWidth,
1343 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1344 int maxHeight = Math.min(data.maxImageHeight,
1345 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001346 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001347 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001348 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001349 } else if (rawInfo instanceof ResolveInfo) {
1350 // Fill in the shortcuts information
1351 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001352 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001353 }
1354 }
Winson Chungb44b5242011-06-13 11:32:14 -07001355 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001356
Winson Chungb44b5242011-06-13 11:32:14 -07001357 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001358 if (mInTransition) {
1359 mDeferredSyncWidgetPageItems.add(data);
1360 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001362 try {
1363 int page = data.page;
1364 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001365
Michael Jurka39e5d172012-03-12 18:36:12 -07001366 ArrayList<Object> items = data.items;
1367 int count = items.size();
1368 for (int i = 0; i < count; ++i) {
1369 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1370 if (widget != null) {
1371 Bitmap preview = data.generatedImages.get(i);
1372 widget.applyPreview(new FastBitmapDrawable(preview), i);
1373 }
1374 }
Winson Chung68e4c642011-11-10 15:48:25 -08001375
Michael Jurka39e5d172012-03-12 18:36:12 -07001376 layout.createHardwareLayer();
1377 invalidate();
1378
1379 // Update all thread priorities
1380 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1381 while (iter.hasNext()) {
1382 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1383 int pageIndex = task.page;
1384 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1385 }
1386 } finally {
1387 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001388 }
Winson Chungb44b5242011-06-13 11:32:14 -07001389 }
Winson Chung46af2e82011-05-09 16:00:53 -07001390
Winson Chung785d2eb2011-04-14 16:08:02 -07001391 @Override
1392 public void syncPages() {
1393 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001394 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001395
Adam Cohen0cd3b642011-10-14 14:58:00 -07001396 Context context = getContext();
1397 for (int j = 0; j < mNumWidgetPages; ++j) {
1398 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1399 mWidgetCountY);
1400 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001401 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001402 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001403 }
1404
Adam Cohen0cd3b642011-10-14 14:58:00 -07001405 for (int i = 0; i < mNumAppsPages; ++i) {
1406 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1407 setupPage(layout);
1408 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001409 }
1410 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001411
Winson Chung785d2eb2011-04-14 16:08:02 -07001412 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001413 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001414 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001415 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001416 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001417 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001418 }
1419 }
1420
Adam Cohen22f823d2011-09-01 17:22:18 -07001421 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1422 // it is in the z-order. This is important to insure touch events are handled correctly.
1423 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001424 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001425 }
1426
Adam Cohenae4f1552011-10-20 00:15:42 -07001427 @Override
1428 protected int indexToPage(int index) {
1429 return getChildCount() - index - 1;
1430 }
1431
Adam Cohen22f823d2011-09-01 17:22:18 -07001432 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1433 @Override
1434 protected void screenScrolled(int screenCenter) {
1435 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001436
1437 for (int i = 0; i < getChildCount(); i++) {
1438 View v = getPageAt(i);
1439 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001440 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001441
1442 float interpolatedProgress =
1443 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1444 float scale = (1 - interpolatedProgress) +
1445 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1446 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001447
Adam Cohen2591f6a2011-10-25 14:36:40 -07001448 float alpha;
1449
Winson Chungd167e2a2012-04-26 13:13:01 -07001450 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001451 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001452 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001453 } else {
1454 // On large screens we need to fade the page as it nears its leftmost position
1455 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1456 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001457
1458 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1459 int pageWidth = v.getMeasuredWidth();
1460 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001461
1462 if (PERFORM_OVERSCROLL_ROTATION) {
1463 if (i == 0 && scrollProgress < 0) {
1464 // Overscroll to the left
1465 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1466 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1467 scale = 1.0f;
1468 alpha = 1.0f;
1469 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001470 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001471 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1472 // Overscroll to the right
1473 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1474 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1475 scale = 1.0f;
1476 alpha = 1.0f;
1477 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001478 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001479 } else {
1480 v.setPivotY(pageHeight / 2.0f);
1481 v.setPivotX(pageWidth / 2.0f);
1482 v.setRotationY(0f);
1483 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001484 }
1485
1486 v.setTranslationX(translationX);
1487 v.setScaleX(scale);
1488 v.setScaleY(scale);
1489 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001490
1491 // If the view has 0 alpha, we set it to be invisible so as to prevent
1492 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001493 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001494 v.setVisibility(INVISIBLE);
1495 } else if (v.getVisibility() != VISIBLE) {
1496 v.setVisibility(VISIBLE);
1497 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001498 }
1499 }
1500 }
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();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001517
1518 // 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 */
1526 @Override
1527 public void setup(Launcher launcher, DragController dragController) {
1528 mLauncher = launcher;
1529 mDragController = dragController;
1530 }
1531 @Override
1532 public void zoom(float zoom, boolean animate) {
1533 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1534 }
1535 @Override
1536 public boolean isVisible() {
1537 return (getVisibility() == VISIBLE);
1538 }
1539 @Override
1540 public boolean isAnimating() {
1541 return false;
1542 }
1543 @Override
1544 public void setApps(ArrayList<ApplicationInfo> list) {
1545 mApps = list;
1546 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001547 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001548
Winson Chung875de7e2011-06-28 14:25:17 -07001549 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1550 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001551 if (testDataReady()) requestLayout();
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);
1558 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1559 if (index < 0) {
1560 mApps.add(-(index + 1), info);
1561 }
1562 }
1563 }
1564 @Override
1565 public void addApps(ArrayList<ApplicationInfo> list) {
1566 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001567 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 invalidatePageData();
1569 }
1570 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1571 ComponentName removeComponent = item.intent.getComponent();
1572 int length = list.size();
1573 for (int i = 0; i < length; ++i) {
1574 ApplicationInfo info = list.get(i);
1575 if (info.intent.getComponent().equals(removeComponent)) {
1576 return i;
1577 }
1578 }
1579 return -1;
1580 }
1581 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1582 // loop through all the apps and remove apps that have the same component
1583 int length = list.size();
1584 for (int i = 0; i < length; ++i) {
1585 ApplicationInfo info = list.get(i);
1586 int removeIndex = findAppByComponent(mApps, info);
1587 if (removeIndex > -1) {
1588 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001589 }
1590 }
1591 }
1592 @Override
1593 public void removeApps(ArrayList<ApplicationInfo> list) {
1594 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001595 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 invalidatePageData();
1597 }
1598 @Override
1599 public void updateApps(ArrayList<ApplicationInfo> list) {
1600 // We remove and re-add the updated applications list because it's properties may have
1601 // changed (ie. the title), and this will ensure that the items will be in their proper
1602 // place in the list.
1603 removeAppsWithoutInvalidate(list);
1604 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001605 updatePageCounts();
1606
Winson Chung785d2eb2011-04-14 16:08:02 -07001607 invalidatePageData();
1608 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001609
Winson Chung785d2eb2011-04-14 16:08:02 -07001610 @Override
1611 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001612 // If we have reset, then we should not continue to restore the previous state
1613 mSaveInstanceStateItemIndex = -1;
1614
Adam Cohenb64d36e2011-10-17 21:48:02 -07001615 AppsCustomizeTabHost tabHost = getTabHost();
1616 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001617 if (tag != null) {
1618 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1619 tabHost.setCurrentTabFromContent(ContentType.Applications);
1620 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001621 }
Winson Chung649668f2012-01-10 13:07:16 -08001622
Adam Cohenb64d36e2011-10-17 21:48:02 -07001623 if (mCurrentPage != 0) {
1624 invalidatePageData(0);
1625 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001626 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001627
1628 private AppsCustomizeTabHost getTabHost() {
1629 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1630 }
1631
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 @Override
1633 public void dumpState() {
1634 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -07001635 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
1636 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -07001637 }
Adam Cohen4e844012011-11-09 13:48:04 -08001638
Winson Chung785d2eb2011-04-14 16:08:02 -07001639 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001640 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001641 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001642 for (Object i: list) {
1643 if (i instanceof AppWidgetProviderInfo) {
1644 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1645 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1646 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1647 + " initialLayout=" + info.initialLayout
1648 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1649 } else if (i instanceof ResolveInfo) {
1650 ResolveInfo info = (ResolveInfo) i;
1651 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1652 + info.icon);
1653 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001654 }
1655 }
Adam Cohen4e844012011-11-09 13:48:04 -08001656
Winson Chung785d2eb2011-04-14 16:08:02 -07001657 @Override
1658 public void surrender() {
1659 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1660 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001661
1662 // Stop all background tasks
1663 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001664 }
Winson Chung007c6982011-06-14 13:27:53 -07001665
Winson Chunge4e50662012-01-23 14:45:13 -08001666 @Override
1667 public void iconPressed(PagedViewIcon icon) {
1668 // Reset the previously pressed icon and store a reference to the pressed icon so that
1669 // we can reset it on return to Launcher (in Launcher.onResume())
1670 if (mPressedIcon != null) {
1671 mPressedIcon.resetDrawableState();
1672 }
1673 mPressedIcon = icon;
1674 }
1675
1676 public void resetDrawableState() {
1677 if (mPressedIcon != null) {
1678 mPressedIcon.resetDrawableState();
1679 mPressedIcon = null;
1680 }
1681 }
Winson Chung68e4c642011-11-10 15:48:25 -08001682
Winson Chungb44b5242011-06-13 11:32:14 -07001683 /*
1684 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1685 * widget previews in the background with the AsyncTasks.
1686 */
Winson Chung68e4c642011-11-10 15:48:25 -08001687 final static int sLookBehindPageCount = 2;
1688 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001689 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001690 final int count = getChildCount();
1691 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1692 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1693 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001694 }
1695 protected int getAssociatedUpperPageBound(int page) {
1696 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001697 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1698 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1699 count - 1);
1700 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001701 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001702
1703 @Override
1704 protected String getCurrentPageDescription() {
1705 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1706 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001707 int count = 0;
1708
Adam Cohen0cd3b642011-10-14 14:58:00 -07001709 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001710 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001711 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001712 } else {
1713 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001714 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001715 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001716 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001717
Michael Jurka8b805b12012-04-18 14:23:14 -07001718 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001719 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001720}