blob: 4958f03e0ff01a514c3f7abcfff539b4513375e6 [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;
Adam Cohen4e844012011-11-09 13:48:04 -080052import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070053import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070054import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070055import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070056import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070057import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070058import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070059
60import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070061import com.android.launcher2.DropTarget.DragObject;
62
63import java.util.ArrayList;
64import java.util.Collections;
65import java.util.Iterator;
66import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070067
Winson Chungb44b5242011-06-13 11:32:14 -070068/**
69 * A simple callback interface which also provides the results of the task.
70 */
71interface AsyncTaskCallback {
72 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
73}
Winson Chung4e076542011-06-23 13:04:10 -070074
Winson Chungb44b5242011-06-13 11:32:14 -070075/**
76 * The data needed to perform either of the custom AsyncTasks.
77 */
78class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070079 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080080 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070081 }
82
Winson Chungb44b5242011-06-13 11:32:14 -070083 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
84 AsyncTaskCallback postR) {
85 page = p;
86 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070087 sourceImages = si;
88 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070089 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070090 doInBackgroundCallback = bgR;
91 postExecuteCallback = postR;
92 }
Michael Jurka038f9d82011-11-03 13:50:45 -070093 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070094 AsyncTaskCallback postR) {
95 page = p;
96 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070097 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070098 maxImageWidth = cw;
99 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700100 doInBackgroundCallback = bgR;
101 postExecuteCallback = postR;
102 }
Winson Chung09945932011-09-20 14:22:40 -0700103 void cleanup(boolean cancelled) {
104 // Clean up any references to source/generated bitmaps
105 if (sourceImages != null) {
106 if (cancelled) {
107 for (Bitmap b : sourceImages) {
108 b.recycle();
109 }
110 }
111 sourceImages.clear();
112 }
113 if (generatedImages != null) {
114 if (cancelled) {
115 for (Bitmap b : generatedImages) {
116 b.recycle();
117 }
118 }
119 generatedImages.clear();
120 }
121 }
Winson Chungb44b5242011-06-13 11:32:14 -0700122 int page;
123 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700124 ArrayList<Bitmap> sourceImages;
125 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700126 int maxImageWidth;
127 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700128 AsyncTaskCallback doInBackgroundCallback;
129 AsyncTaskCallback postExecuteCallback;
130}
Winson Chung4e076542011-06-23 13:04:10 -0700131
Winson Chungb44b5242011-06-13 11:32:14 -0700132/**
133 * A generic template for an async task used in AppsCustomize.
134 */
135class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700136 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700137 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700138 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700139 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 }
141 @Override
142 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
143 if (params.length != 1) return null;
144 // Load each of the widget previews in the background
145 params[0].doInBackgroundCallback.run(this, params[0]);
146 return params[0];
147 }
148 @Override
149 protected void onPostExecute(AsyncTaskPageData result) {
150 // All the widget previews are loaded, so we can just callback to inflate the page
151 result.postExecuteCallback.run(this, result);
152 }
153
154 void setThreadPriority(int p) {
155 threadPriority = p;
156 }
157 void syncThreadPriority() {
158 Process.setThreadPriority(threadPriority);
159 }
160
161 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700162 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700163 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700164 int threadPriority;
165}
Winson Chungb44b5242011-06-13 11:32:14 -0700166
167/**
168 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
169 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800171 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Michael Jurka39e5d172012-03-12 18:36:12 -0700172 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener,
173 LauncherTransitionable {
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 static final String LOG_TAG = "AppsCustomizePagedView";
175
176 /**
177 * The different content types that this paged view can show.
178 */
179 public enum ContentType {
180 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700181 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700182 }
183
184 // Refs
185 private Launcher mLauncher;
186 private DragController mDragController;
187 private final LayoutInflater mLayoutInflater;
188 private final PackageManager mPackageManager;
189
Winson Chung5afbf7b2011-07-25 11:53:08 -0700190 // Save and Restore
191 private int mSaveInstanceStateItemIndex = -1;
Winson Chunge4e50662012-01-23 14:45:13 -0800192 private PagedViewIcon mPressedIcon;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700193
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 // Content
Winson Chung785d2eb2011-04-14 16:08:02 -0700195 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700196 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700197
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 // Cling
Winson Chung3f4e1422011-11-17 14:58:51 -0800199 private boolean mHasShownAllAppsCling;
Winson Chung7d7541e2011-09-16 20:14:36 -0700200 private int mClingFocusedX;
201 private int mClingFocusedY;
202
Winson Chung1ed747a2011-05-03 16:18:34 -0700203 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700204 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700205 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700206 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700207
208 // Dimens
209 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700210 private int mAppIconSize;
Winson Chung6032e7e2011-11-08 15:47:17 -0800211 private int mMaxAppCellCountX, mMaxAppCellCountY;
Winson Chung4b576be2011-04-27 17:40:20 -0700212 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700213 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700214 private final int mWidgetPreviewIconPaddedDimension;
215 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700216 private PagedViewCellLayout mWidgetSpacingLayout;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700217 private int mNumAppsPages;
218 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -0700219
Adam Cohen22f823d2011-09-01 17:22:18 -0700220 // Relating to the scroll and overscroll effects
221 Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f);
Adam Cohencff6af82011-09-13 14:51:53 -0700222 private static float CAMERA_DISTANCE = 6500;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700223 private static float TRANSITION_SCALE_FACTOR = 0.74f;
Adam Cohencff6af82011-09-13 14:51:53 -0700224 private static float TRANSITION_PIVOT = 0.65f;
225 private static float TRANSITION_MAX_ROTATION = 22;
226 private static final boolean PERFORM_OVERSCROLL_ROTATION = true;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700227 private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f);
Adam Cohen2591f6a2011-10-25 14:36:40 -0700228 private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4);
Adam Cohen22f823d2011-09-01 17:22:18 -0700229
Winson Chungb44b5242011-06-13 11:32:14 -0700230 // Previews & outlines
231 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
Winson Chung68e4c642011-11-10 15:48:25 -0800232 private static final int sPageSleepDelay = 200;
Winson Chung4b576be2011-04-27 17:40:20 -0700233
Adam Cohened66b2b2012-01-23 17:28:51 -0800234 private Runnable mInflateWidgetRunnable = null;
235 private Runnable mBindWidgetRunnable = null;
236 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
237 static final int WIDGET_BOUND = 0;
238 static final int WIDGET_INFLATED = 1;
239 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
240 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -0800241 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800242 private boolean mDraggingWidget = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800243
Michael Jurka39e5d172012-03-12 18:36:12 -0700244 // Deferral of loading widget previews during launcher transitions
245 private boolean mInTransition;
246 private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems =
247 new ArrayList<AsyncTaskPageData>();
248
Winson Chung785d2eb2011-04-14 16:08:02 -0700249 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
250 super(context, attrs);
251 mLayoutInflater = LayoutInflater.from(context);
252 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700253 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700254 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700255 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700256 mCanvas = new Canvas();
257 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700258
259 // Save the default widget preview background
260 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700261 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700262 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700263
Winson Chung6032e7e2011-11-08 15:47:17 -0800264 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
265 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
266 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700267 mWidgetWidthGap =
268 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
269 mWidgetHeightGap =
270 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700271 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
272 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700273 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
274 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700275 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700277
Winson Chung1ed747a2011-05-03 16:18:34 -0700278 // The padding on the non-matched dimension for the default widget preview icons
279 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700280 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700281 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700282 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700283 }
284
285 @Override
286 protected void init() {
287 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700288 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700289
290 Context context = getContext();
291 Resources r = context.getResources();
292 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
293 }
294
Winson Chungf0ea4d32011-06-06 14:27:16 -0700295 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700296 protected void onUnhandledTap(MotionEvent ev) {
297 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700298 // Dismiss AppsCustomize if we tap
299 mLauncher.showWorkspace(true);
300 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700301 }
302
Winson Chung5afbf7b2011-07-25 11:53:08 -0700303 /** Returns the item index of the center item on this page so that we can restore to this
304 * item index when we rotate. */
305 private int getMiddleComponentIndexOnCurrentPage() {
306 int i = -1;
307 if (getPageCount() > 0) {
308 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700309 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700310 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700311 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
312 int numItemsPerPage = mCellCountX * mCellCountY;
313 int childCount = childrenLayout.getChildCount();
314 if (childCount > 0) {
315 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700316 }
317 } else {
318 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700319 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700320 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
321 int childCount = layout.getChildCount();
322 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700323 i = numApps +
324 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
325 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700326 }
327 }
328 return i;
329 }
330
331 /** Get the index of the item to restore to if we need to restore the current page. */
332 int getSaveInstanceStateIndex() {
333 if (mSaveInstanceStateItemIndex == -1) {
334 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
335 }
336 return mSaveInstanceStateItemIndex;
337 }
338
339 /** Returns the page in the current orientation which is expected to contain the specified
340 * item index. */
341 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 if (index < 0) return 0;
343
344 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700345 int numItemsPerPage = mCellCountX * mCellCountY;
346 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700347 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700348 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700349 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
350 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700351 }
352
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 /**
354 * This differs from isDataReady as this is the test done if isDataReady is not set.
355 */
356 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700357 // We only do this test once, and we default to the Applications page, so we only really
358 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700359 // TODO: What if one of them is validly empty
360 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700361 }
362
Winson Chung5afbf7b2011-07-25 11:53:08 -0700363 /** Restores the page for an item at the specified index */
364 void restorePageForIndex(int index) {
365 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700366 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700367 }
368
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700369 private void updatePageCounts() {
370 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
371 (float) (mWidgetCountX * mWidgetCountY));
372 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
373 }
374
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 protected void onDataReady(int width, int height) {
376 // Note that we transpose the counts in portrait so that we get a similar layout
377 boolean isLandscape = getResources().getConfiguration().orientation ==
378 Configuration.ORIENTATION_LANDSCAPE;
379 int maxCellCountX = Integer.MAX_VALUE;
380 int maxCellCountY = Integer.MAX_VALUE;
381 if (LauncherApplication.isScreenLarge()) {
382 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
383 LauncherModel.getCellCountY());
384 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
385 LauncherModel.getCellCountX());
386 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800387 if (mMaxAppCellCountX > -1) {
388 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
389 }
390 if (mMaxAppCellCountY > -1) {
391 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
392 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393
394 // Now that the data is ready, we can calculate the content width, the number of cells to
395 // use for each page
396 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
397 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
398 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
399 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
400 mCellCountX = mWidgetSpacingLayout.getCellCountX();
401 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700402 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700403
Winson Chungdb1138b2011-06-30 14:39:35 -0700404 // Force a measure to update recalculate the gaps
405 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
406 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
407 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700408 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700409
Michael Jurkae326f182011-11-21 14:05:46 -0800410 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
411 final boolean hostIsTransitioning = host.isTransitioning();
412
Adam Cohen0cd3b642011-10-14 14:58:00 -0700413 // Restore the page
414 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800415 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700416
Winson Chung3f4e1422011-11-17 14:58:51 -0800417 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
418 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
419 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800420 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800421 post(new Runnable() {
422 @Override
423 public void run() {
424 showAllAppsCling();
425 }
426 });
427 }
428 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700429
Winson Chung3f4e1422011-11-17 14:58:51 -0800430 void showAllAppsCling() {
431 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
432 mHasShownAllAppsCling = true;
433 // Calculate the position for the cling punch through
434 int[] offset = new int[2];
435 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
436 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
437 // PagedViews are centered horizontally but top aligned
438 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
439 offset[0];
440 pos[1] += offset[1];
441 mLauncher.showFirstRunAllAppsCling(pos);
442 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
444
445 @Override
446 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
447 int width = MeasureSpec.getSize(widthMeasureSpec);
448 int height = MeasureSpec.getSize(heightMeasureSpec);
449 if (!isDataReady()) {
450 if (testDataReady()) {
451 setDataIsReady();
452 setMeasuredDimension(width, height);
453 onDataReady(width, height);
454 }
455 }
456
457 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
458 }
459
Winson Chung785d2eb2011-04-14 16:08:02 -0700460 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700461 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
462 // by a broadcast receiver, and in order for it to work correctly, we need to know that
463 // the AppWidgetService has already received and processed the same broadcast. Since there
464 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
465 // we should have a more precise way of ensuring the AppWidgetService is up to date.
466 postDelayed(new Runnable() {
467 public void run() {
468 updatePackages();
469 }
470 }, 500);
471 }
472
473 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700474 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700475 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700476 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700477 List<AppWidgetProviderInfo> widgets =
478 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700479 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
480 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800481 for (AppWidgetProviderInfo widget : widgets) {
482 if (widget.minWidth > 0 && widget.minHeight > 0) {
Winson Chunga5c96362012-04-12 14:04:41 -0700483 // Ensure that all widgets we show can be added on a workspace of this size
484 int[] spanXY = mLauncher.getSpanForWidget(widget);
485 int[] minSpanXY = mLauncher.getMinSpanForWidget(widget);
486 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
487 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Adam Cohen336d4912012-04-13 17:57:11 -0700488 if (minSpanX <= LauncherModel.getCellCountX() &&
489 minSpanY <= LauncherModel.getCellCountY()) {
Winson Chunga5c96362012-04-12 14:04:41 -0700490 mWidgets.add(widget);
491 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800492 } else {
493 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
494 widget.minWidth + ", " + widget.minHeight + ")");
495 }
496 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700497 mWidgets.addAll(shortcuts);
498 Collections.sort(mWidgets,
499 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700500 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700501
Winson Chung875de7e2011-06-28 14:25:17 -0700502 if (wasEmpty) {
503 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
504 // a layout to do this test and invalidate the page data when ready.
505 if (testDataReady()) requestLayout();
506 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700507 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700508 invalidatePageData();
509 }
Winson Chung4b576be2011-04-27 17:40:20 -0700510 }
511
512 @Override
513 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700514 // When we have exited all apps or are in transition, disregard clicks
515 if (!mLauncher.isAllAppsCustomizeOpen() ||
516 mLauncher.getWorkspace().isSwitchingState()) return;
517
Winson Chung4b576be2011-04-27 17:40:20 -0700518 if (v instanceof PagedViewIcon) {
519 // Animate some feedback to the click
520 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Winson Chung3b187b82012-01-30 15:11:08 -0800521
522 // Lock the drawable state to pressed until we return to Launcher
523 if (mPressedIcon != null) {
524 mPressedIcon.lockDrawableState();
525 }
Winson Chungc7450e32012-04-17 17:34:08 -0700526
527 // NOTE: we need to re-enable the wallpaper visibility if we want correct transitions
528 // between items that are launched from the workspace and all apps. It will be disabled
529 // correctly the next time the window is visible in AppsCustomizeTabHost.
530 mLauncher.updateWallpaperVisibility(true);
531 mLauncher.startActivitySafely(v, appInfo.intent, appInfo);
532
Winson Chung4b576be2011-04-27 17:40:20 -0700533 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700534 // Let the user know that they have to long press to add a widget
535 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
536 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700537
Winson Chungd2e87b32011-06-02 10:53:07 -0700538 // Create a little animation to show that the widget can move
539 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
540 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
541 AnimatorSet bounce = new AnimatorSet();
542 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
543 tyuAnim.setDuration(125);
544 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
545 tydAnim.setDuration(100);
546 bounce.play(tyuAnim).before(tydAnim);
547 bounce.setInterpolator(new AccelerateInterpolator());
548 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700549 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700550 }
551
Winson Chungc6f10b92011-11-14 11:39:07 -0800552 public boolean onKey(View v, int keyCode, KeyEvent event) {
553 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
554 }
555
Winson Chung785d2eb2011-04-14 16:08:02 -0700556 /*
557 * PagedViewWithDraggableItems implementation
558 */
559 @Override
560 protected void determineDraggingStart(android.view.MotionEvent ev) {
561 // Disable dragging by pulling an app down for now.
562 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700563
Winson Chung4b576be2011-04-27 17:40:20 -0700564 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700565 mLauncher.getWorkspace().onDragStartedWithItem(v);
566 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700567 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700568
Adam Cohened66b2b2012-01-23 17:28:51 -0800569 private void loadWidgetInBackground(final PendingAddWidgetInfo info) {
570 final AppWidgetProviderInfo pInfo = info.info;
571 if (pInfo.configure != null) {
572 return;
573 }
574
575 mBindWidgetRunnable = new Runnable() {
576 @Override
577 public void run() {
578 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 if (AppWidgetManager.getInstance(mLauncher)
580 .bindAppWidgetIdIfAllowed(mWidgetLoadingId, info.componentName)) {
581 mWidgetCleanupState = WIDGET_BOUND;
582 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 }
584 };
585 post(mBindWidgetRunnable);
586
587 mInflateWidgetRunnable = new Runnable() {
588 @Override
589 public void run() {
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 AppWidgetHostView hostView = mLauncher.
591 getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 info.boundWidget = hostView;
593 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800594 hostView.setVisibility(INVISIBLE);
Adam Cohen1f362702012-04-04 14:58:12 -0700595 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX,
596 info.spanY, info, false);
597
598 // We want the first widget layout to be the correct size. This will be important
599 // for width size reporting to the AppWidgetManager.
600 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
601 unScaledSize[1]);
602 lp.x = lp.y = 0;
603 lp.customPosition = true;
604 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800605 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 }
607 };
608 post(mInflateWidgetRunnable);
609 }
610
611 @Override
612 public void onShortPress(View v) {
613 // We are anticipating a long press, and we use this time to load bind and instantiate
614 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen1b36dc32012-02-13 19:27:37 -0800615 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
616 loadWidgetInBackground(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 }
618
Adam Cohen7a326642012-02-22 12:03:22 -0800619 private void cleanupWidgetPreloading() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800620 PendingAddWidgetInfo info = mCreateWidgetInfo;
621 mCreateWidgetInfo = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
623 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
624 }
625 if (mWidgetCleanupState == WIDGET_BOUND) {
626 removeCallbacks(mInflateWidgetRunnable);
627 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
628 AppWidgetHostView widget = info.boundWidget;
629 int widgetId = widget.getAppWidgetId();
630 mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800631 mLauncher.getDragLayer().removeView(widget);
Adam Cohened66b2b2012-01-23 17:28:51 -0800632 }
633 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
634 mWidgetLoadingId = -1;
635 }
636
Adam Cohen7a326642012-02-22 12:03:22 -0800637 @Override
638 public void cleanUpShortPress(View v) {
639 if (!mDraggingWidget) {
640 cleanupWidgetPreloading();
641 }
642 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800643
Adam Cohen7a326642012-02-22 12:03:22 -0800644 private void beginDraggingWidget(View v) {
645 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700646 // Get the widget preview as the drag representation
647 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700648 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700649
650 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800651 Bitmap preview;
652 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800653 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700654 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800655 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
656 createItemInfo = createWidgetInfo;
Adam Cohen1f362702012-04-04 14:58:12 -0700657 int spanX = createItemInfo.spanX;
658 int spanY = createItemInfo.spanY;
659 int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY,
660 createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700661
Winson Chung72d59842012-02-22 13:51:36 -0800662 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
663 float minScale = 1.25f;
664 int minWidth, minHeight;
665 minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
666 minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800667 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Adam Cohen1f362702012-04-04 14:58:12 -0700668 createWidgetInfo.icon, spanX, spanY, minWidth, minHeight);
Winson Chung72d59842012-02-22 13:51:36 -0800669
670 // Determine the image view drawable scale relative to the preview
671 float[] mv = new float[9];
672 Matrix m = new Matrix();
673 m.setRectToRect(
674 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
675 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
676 (float) previewDrawable.getIntrinsicHeight()),
677 Matrix.ScaleToFit.START);
678 m.getValues(mv);
679 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700680 } else {
681 // Workaround for the fact that we don't keep the original ResolveInfo associated with
682 // the shortcut around. To get the icon, we just render the preview image (which has
683 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800684 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700685 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800686 Drawable d = image.getDrawable();
687 mCanvas.setBitmap(preview);
688 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700689 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700690 createItemInfo.spanX = createItemInfo.spanY = 1;
691 }
Winson Chung4b576be2011-04-27 17:40:20 -0700692
Peter Ng8db70002011-10-25 15:40:08 -0700693 // We use a custom alpha clip table for the default widget previews
694 Paint alphaClipPaint = null;
695 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700696 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700697 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
698 alphaClipPaint = new Paint();
699 alphaClipPaint.setMaskFilter(alphaClipTable);
700 }
701 }
702
Winson Chung1120e032011-11-22 16:11:31 -0800703 // Save the preview for the outline generation, then dim the preview
704 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
705 false);
Winson Chung1120e032011-11-22 16:11:31 -0800706
Winson Chung4b576be2011-04-27 17:40:20 -0700707 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800708 alphaClipPaint = null;
Winson Chung641d71d2012-04-26 15:58:01 -0700709 mLauncher.lockScreenOrientation();
Winson Chung1120e032011-11-22 16:11:31 -0800710 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
711 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800712 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800713 outline.recycle();
714 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700715 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800716
Winson Chung4b576be2011-04-27 17:40:20 -0700717 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700719 if (!super.beginDragging(v)) return false;
720
721 if (v instanceof PagedViewIcon) {
722 beginDraggingApplication(v);
723 } else if (v instanceof PagedViewWidget) {
724 beginDraggingWidget(v);
725 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800726
727 // We delay entering spring-loaded mode slightly to make sure the UI
728 // thready is free of any work.
729 postDelayed(new Runnable() {
730 @Override
731 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800732 // We don't enter spring-loaded mode if the drag has been cancelled
733 if (mLauncher.getDragController().isDragging()) {
734 // Dismiss the cling
735 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800736
Adam Cohen1b36dc32012-02-13 19:27:37 -0800737 // Reset the alpha on the dragged icon before we drag
738 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800739
Adam Cohen1b36dc32012-02-13 19:27:37 -0800740 // Go into spring loaded mode (must happen before we startDrag())
741 mLauncher.enterSpringLoadedDragMode();
742 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 }
Winson Chung72d59842012-02-22 13:51:36 -0800744 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800745
Winson Chung785d2eb2011-04-14 16:08:02 -0700746 return true;
747 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800748
Winson Chunga48487a2012-03-20 16:19:37 -0700749 /**
750 * Clean up after dragging.
751 *
752 * @param target where the item was dragged to (can be null if the item was flung)
753 */
754 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700755 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Adam Cohend4d7aa52011-07-19 21:47:37 -0700756 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700757 // Exit spring loaded mode if we have not successfully dropped or have not handled the
758 // drop in Workspace
759 mLauncher.exitSpringLoadedDragMode();
760 }
Winson Chung641d71d2012-04-26 15:58:01 -0700761 mLauncher.unlockScreenOrientation();
Winson Chung785d2eb2011-04-14 16:08:02 -0700762 }
763
Winson Chung785d2eb2011-04-14 16:08:02 -0700764 @Override
Michael Jurka39e5d172012-03-12 18:36:12 -0700765 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
766 mInTransition = true;
767 if (toWorkspace) {
768 cancelAllTasks();
769 }
770 }
771
772 @Override
773 public View getContent() {
774 return null;
775 }
776
777 @Override
778 public void onLauncherTransitionStep(Launcher l, float t) {
779 }
780
781 @Override
782 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
783 mInTransition = false;
784 for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) {
785 onSyncWidgetPageItems(d);
786 }
787 mDeferredSyncWidgetPageItems.clear();
788 }
789
790 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700791 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
792 boolean success) {
793 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
794 if (isFlingToDelete) return;
795
796 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700797
798 // Display an error message if the drag failed due to there not being enough space on the
799 // target layout we were dropping on.
800 if (!success) {
801 boolean showOutOfSpaceMessage = false;
802 if (target instanceof Workspace) {
803 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
804 Workspace workspace = (Workspace) target;
805 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700806 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700807 if (layout != null) {
808 layout.calculateSpans(itemInfo);
809 showOutOfSpaceMessage =
810 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
811 }
812 }
Winson Chungfc79c802011-05-02 13:35:34 -0700813 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700814 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700815 }
Adam Cohen7a326642012-02-22 12:03:22 -0800816
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800817 d.deferDragViewCleanupPostAnimation = false;
Adam Cohen7a326642012-02-22 12:03:22 -0800818 cleanupWidgetPreloading();
Winson Chungfc79c802011-05-02 13:35:34 -0700819 }
Adam Cohen7a326642012-02-22 12:03:22 -0800820 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 }
822
Winson Chunga48487a2012-03-20 16:19:37 -0700823 @Override
824 public void onFlingToDeleteCompleted() {
825 // We just dismiss the drag when we fling, so cleanup here
826 endDragging(null, true, true);
827 cleanupWidgetPreloading();
828 mDraggingWidget = false;
829 }
830
831 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800832 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700833 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800834 }
835
Winson Chung7f0acdd2011-09-19 18:34:19 -0700836 @Override
837 protected void onDetachedFromWindow() {
838 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700839 cancelAllTasks();
840 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700841
Michael Jurkae326f182011-11-21 14:05:46 -0800842 public void clearAllWidgetPages() {
843 cancelAllTasks();
844 int count = getChildCount();
845 for (int i = 0; i < count; i++) {
846 View v = getPageAt(i);
847 if (v instanceof PagedViewGridLayout) {
848 ((PagedViewGridLayout) v).removeAllViewsOnPage();
849 mDirtyPageContent.set(i, true);
850 }
851 }
852 }
853
Adam Cohen0cd3b642011-10-14 14:58:00 -0700854 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700855 // Clean up all the async tasks
856 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
857 while (iter.hasNext()) {
858 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
859 task.cancel(false);
860 iter.remove();
Michael Jurka39e5d172012-03-12 18:36:12 -0700861 mDirtyPageContent.set(task.page, true);
Winson Chung7f0acdd2011-09-19 18:34:19 -0700862 }
Winson Chung83687b12012-04-25 16:01:01 -0700863 mDeferredSyncWidgetPageItems.clear();
Winson Chung7f0acdd2011-09-19 18:34:19 -0700864 }
865
Winson Chung785d2eb2011-04-14 16:08:02 -0700866 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700867 if (type == ContentType.Widgets) {
868 invalidatePageData(mNumAppsPages, true);
869 } else if (type == ContentType.Applications) {
870 invalidatePageData(0, true);
871 }
Winson Chungb44b5242011-06-13 11:32:14 -0700872 }
873
Adam Cohen0cd3b642011-10-14 14:58:00 -0700874 protected void snapToPage(int whichPage, int delta, int duration) {
875 super.snapToPage(whichPage, delta, duration);
876 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800877
878 // Update the thread priorities given the direction lookahead
879 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
880 while (iter.hasNext()) {
881 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700882 int pageIndex = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -0800883 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
884 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
885 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
886 } else {
887 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
888 }
889 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700890 }
891
892 private void updateCurrentTab(int currentPage) {
893 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800894 if (tabHost != null) {
895 String tag = tabHost.getCurrentTabTag();
896 if (tag != null) {
897 if (currentPage >= mNumAppsPages &&
898 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
899 tabHost.setCurrentTabFromContent(ContentType.Widgets);
900 } else if (currentPage < mNumAppsPages &&
901 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
902 tabHost.setCurrentTabFromContent(ContentType.Applications);
903 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700904 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700905 }
906 }
907
Winson Chung785d2eb2011-04-14 16:08:02 -0700908 /*
909 * Apps PagedView implementation
910 */
Winson Chung63257c12011-05-05 17:06:13 -0700911 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
912 int childCount = layout.getChildCount();
913 for (int i = 0; i < childCount; ++i) {
914 layout.getChildAt(i).setVisibility(visibility);
915 }
916 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700917 private void setupPage(PagedViewCellLayout layout) {
918 layout.setCellCount(mCellCountX, mCellCountY);
919 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
920 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
921 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
922
Winson Chung63257c12011-05-05 17:06:13 -0700923 // Note: We force a measure here to get around the fact that when we do layout calculations
924 // immediately after syncing, we don't have a proper width. That said, we already know the
925 // expected page width, so we can actually optimize by hiding all the TextView-based
926 // children that are expensive to measure, and let that happen naturally later.
927 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700928 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700929 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700930 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700931 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700932 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700933 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700934
Winson Chungf314b0e2011-08-16 11:54:27 -0700935 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700936 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700937 int numCells = mCellCountX * mCellCountY;
938 int startIndex = page * numCells;
939 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700940 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700941
Winson Chung785d2eb2011-04-14 16:08:02 -0700942 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700943 ArrayList<Object> items = new ArrayList<Object>();
944 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700945 for (int i = startIndex; i < endIndex; ++i) {
946 ApplicationInfo info = mApps.get(i);
947 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
948 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800949 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700950 icon.setOnClickListener(this);
951 icon.setOnLongClickListener(this);
952 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800953 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700954
955 int index = i - startIndex;
956 int x = index % mCellCountX;
957 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700958 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700959
960 items.add(info);
961 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700962 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700963
Winson Chungf0ea4d32011-06-06 14:27:16 -0700964 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700965 }
Winson Chungb44b5242011-06-13 11:32:14 -0700966
967 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800968 * A helper to return the priority for loading of the specified widget page.
969 */
970 private int getWidgetPageLoadPriority(int page) {
971 // If we are snapping to another page, use that index as the target page index
972 int toPage = mCurrentPage;
973 if (mNextPage > -1) {
974 toPage = mNextPage;
975 }
976
977 // We use the distance from the target page as an initial guess of priority, but if there
978 // are no pages of higher priority than the page specified, then bump up the priority of
979 // the specified page.
980 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
981 int minPageDiff = Integer.MAX_VALUE;
982 while (iter.hasNext()) {
983 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -0700984 minPageDiff = Math.abs(task.page - toPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800985 }
986
987 int rawPageDiff = Math.abs(page - toPage);
988 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
989 }
990 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700991 * Return the appropriate thread priority for loading for a given page (we give the current
992 * page much higher priority)
993 */
994 private int getThreadPriorityForPage(int page) {
995 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800996 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700997 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800998 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700999 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -08001000 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001001 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001002 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -07001003 }
1004 }
Winson Chungf314b0e2011-08-16 11:54:27 -07001005 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001006 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -07001007 return Math.max(0, pageDiff * sPageSleepDelay);
1008 }
Winson Chungb44b5242011-06-13 11:32:14 -07001009 /**
1010 * Creates and executes a new AsyncTask to load a page of widget previews.
1011 */
1012 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -07001013 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -08001014
Winson Chungb44b5242011-06-13 11:32:14 -07001015 // Prune all tasks that are no longer needed
1016 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1017 while (iter.hasNext()) {
1018 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Michael Jurka39e5d172012-03-12 18:36:12 -07001019 int taskPage = task.page;
Winson Chung68e4c642011-11-10 15:48:25 -08001020 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
1021 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -07001022 task.cancel(false);
1023 iter.remove();
1024 } else {
Winson Chung68e4c642011-11-10 15:48:25 -08001025 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -07001026 }
1027 }
1028
Winson Chungf314b0e2011-08-16 11:54:27 -07001029 // 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 -07001030 final int sleepMs = getSleepForPage(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001031 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -07001032 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -07001033 @Override
1034 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001035 try {
Winson Chung09945932011-09-20 14:22:40 -07001036 try {
1037 Thread.sleep(sleepMs);
1038 } catch (Exception e) {}
1039 loadWidgetPreviewsInBackground(task, data);
1040 } finally {
1041 if (task.isCancelled()) {
1042 data.cleanup(true);
1043 }
1044 }
Winson Chungb44b5242011-06-13 11:32:14 -07001045 }
1046 },
1047 new AsyncTaskCallback() {
1048 @Override
1049 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001050 mRunningTasks.remove(task);
1051 if (task.isCancelled()) return;
1052 // do cleanup inside onSyncWidgetPageItems
1053 onSyncWidgetPageItems(data);
Winson Chungb44b5242011-06-13 11:32:14 -07001054 }
Winson Chung09945932011-09-20 14:22:40 -07001055 });
Winson Chungb44b5242011-06-13 11:32:14 -07001056
1057 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -07001058 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -07001059 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Michael Jurka39e5d172012-03-12 18:36:12 -07001060 t.setThreadPriority(getThreadPriorityForPage(page));
Winson Chungb44b5242011-06-13 11:32:14 -07001061 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
1062 mRunningTasks.add(t);
1063 }
Winson Chungb44b5242011-06-13 11:32:14 -07001064
Winson Chung785d2eb2011-04-14 16:08:02 -07001065 /*
1066 * Widgets PagedView implementation
1067 */
Winson Chung4e6a9762011-05-09 11:56:34 -07001068 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001069 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1070 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -07001071
1072 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001073 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001074 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1075 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001076 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001077 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001078 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001079
Winson Chung5fc72b32011-10-11 17:53:58 -07001080 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
1081 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -07001082 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001083
Winson Chung5fc72b32011-10-11 17:53:58 -07001084 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
1085 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -07001086 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001087 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001088 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001089 Rect oldBounds = d.copyBounds();
1090 d.setBounds(x, y, x + w, y + h);
1091 d.draw(c);
1092 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001093 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001094 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001095 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001096 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001097 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001098 int offset = 0;
1099 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001100 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001101
Winson Chung1ed747a2011-05-03 16:18:34 -07001102 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001103 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001104 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001105 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001106 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001107
Michael Jurka038f9d82011-11-03 13:50:45 -07001108 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1109 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001110 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001111 String packageName = provider.getPackageName();
1112 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1113 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001114
Michael Jurka038f9d82011-11-03 13:50:45 -07001115 Drawable drawable = null;
1116 if (previewImage != 0) {
1117 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1118 if (drawable == null) {
1119 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1120 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001121 }
1122 }
1123
Michael Jurka038f9d82011-11-03 13:50:45 -07001124 int bitmapWidth;
1125 int bitmapHeight;
1126 boolean widgetPreviewExists = (drawable != null);
1127 if (widgetPreviewExists) {
1128 bitmapWidth = drawable.getIntrinsicWidth();
1129 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001130 } else {
Michael Jurkac7e52f52012-03-26 06:20:31 -07001131 if (cellHSpan < 1) cellHSpan = 1;
1132 if (cellVSpan < 1) cellVSpan = 1;
Michael Jurka038f9d82011-11-03 13:50:45 -07001133 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001134 // TODO: This actually uses the apps customize cell layout params, where as we make want
1135 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001136 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1137 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001138 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001139 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001140 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001141 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001142 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001143 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001144 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001145 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001146 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001147 }
1148
1149 float scale = 1f;
1150 if (bitmapWidth > maxWidth) {
1151 scale = maxWidth / (float) bitmapWidth;
1152 }
1153 if (bitmapHeight * scale > maxHeight) {
1154 scale = maxHeight / (float) bitmapHeight;
1155 }
1156 if (scale != 1f) {
1157 bitmapWidth = (int) (scale * bitmapWidth);
1158 bitmapHeight = (int) (scale * bitmapHeight);
1159 }
1160
1161 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1162
1163 if (widgetPreviewExists) {
1164 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1165 } else {
1166 // Generate a preview image if we couldn't load one
1167 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1168 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1169 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001170 if (cellHSpan != 1 || cellVSpan != 1) {
1171 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1172 bitmapHeight);
1173 }
Winson Chung4b576be2011-04-27 17:40:20 -07001174
1175 // Draw the icon in the top left corner
1176 try {
1177 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001178 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1179 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001180 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001181 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001182 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1183
Peter Ng8db70002011-10-25 15:40:08 -07001184 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1185 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001186 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001187 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001188 }
Winson Chungb44b5242011-06-13 11:32:14 -07001189 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001190 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001191
Michael Jurka038f9d82011-11-03 13:50:45 -07001192 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001193 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001194
Winson Chungd2945262011-06-24 15:22:14 -07001195 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001196 final ArrayList<Object> items = new ArrayList<Object>();
1197 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1198 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001199 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001200 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1201 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001202 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001203
Winson Chunge4a647f2011-09-30 14:41:25 -07001204 // Prepare the set of widgets to load previews for in the background
Michael Jurka39e5d172012-03-12 18:36:12 -07001205 int offset = (page - mNumAppsPages) * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001206 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1207 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001208 }
1209
Winson Chunge4a647f2011-09-30 14:41:25 -07001210 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka39e5d172012-03-12 18:36:12 -07001211 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chunge4a647f2011-09-30 14:41:25 -07001212 layout.setColumnCount(layout.getCellCountX());
1213 for (int i = 0; i < items.size(); ++i) {
1214 Object rawInfo = items.get(i);
1215 PendingAddItemInfo createItemInfo = null;
1216 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1217 R.layout.apps_customize_widget, layout, false);
1218 if (rawInfo instanceof AppWidgetProviderInfo) {
1219 // Fill in the widget information
1220 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1221 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Adam Cohen1f362702012-04-04 14:58:12 -07001222
1223 // Determine the widget spans and min resize spans.
Winson Chunga5c96362012-04-12 14:04:41 -07001224 int[] spanXY = mLauncher.getSpanForWidget(info);
Adam Cohen1f362702012-04-04 14:58:12 -07001225 createItemInfo.spanX = spanXY[0];
1226 createItemInfo.spanY = spanXY[1];
Winson Chunga5c96362012-04-12 14:04:41 -07001227 int[] minSpanXY = mLauncher.getMinSpanForWidget(info);
Adam Cohen1f362702012-04-04 14:58:12 -07001228 createItemInfo.minSpanX = minSpanXY[0];
1229 createItemInfo.minSpanY = minSpanXY[1];
1230
1231 widget.applyFromAppWidgetProviderInfo(info, -1, spanXY);
Winson Chunge4a647f2011-09-30 14:41:25 -07001232 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001233 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001234 } else if (rawInfo instanceof ResolveInfo) {
1235 // Fill in the shortcuts information
1236 ResolveInfo info = (ResolveInfo) rawInfo;
1237 createItemInfo = new PendingAddItemInfo();
1238 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1239 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1240 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001241 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001242 widget.setTag(createItemInfo);
1243 }
1244 widget.setOnClickListener(this);
1245 widget.setOnLongClickListener(this);
1246 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001247 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001248
1249 // Layout each widget
1250 int ix = i % mWidgetCountX;
1251 int iy = i / mWidgetCountX;
1252 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1253 GridLayout.spec(iy, GridLayout.LEFT),
1254 GridLayout.spec(ix, GridLayout.TOP));
1255 lp.width = cellWidth;
1256 lp.height = cellHeight;
1257 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1258 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1259 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1260 layout.addView(widget, lp);
1261 }
1262
Michael Jurka038f9d82011-11-03 13:50:45 -07001263 // wait until a call on onLayout to start loading, because
1264 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1265 // TODO: can we do a measure/layout immediately?
1266 layout.setOnLayoutListener(new Runnable() {
1267 public void run() {
1268 // Load the widget previews
1269 int maxPreviewWidth = cellWidth;
1270 int maxPreviewHeight = cellHeight;
1271 if (layout.getChildCount() > 0) {
1272 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1273 int[] maxSize = w.getPreviewSize();
1274 maxPreviewWidth = maxSize[0];
1275 maxPreviewHeight = maxSize[1];
1276 }
1277 if (immediate) {
1278 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1279 maxPreviewWidth, maxPreviewHeight, null, null);
1280 loadWidgetPreviewsInBackground(null, data);
1281 onSyncWidgetPageItems(data);
1282 } else {
1283 prepareLoadWidgetPreviewsTask(page, items,
1284 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1285 }
1286 }
1287 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001288 }
1289 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1290 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001291 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1292 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001293 if (task != null) {
1294 // Ensure that this task starts running at the correct priority
1295 task.syncThreadPriority();
1296 }
1297
1298 // Load each of the widget/shortcut previews
1299 ArrayList<Object> items = data.items;
1300 ArrayList<Bitmap> images = data.generatedImages;
1301 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001302 for (int i = 0; i < count; ++i) {
1303 if (task != null) {
1304 // Ensure we haven't been cancelled yet
1305 if (task.isCancelled()) break;
1306 // Before work on each item, ensure that this task is running at the correct
1307 // priority
1308 task.syncThreadPriority();
1309 }
1310
1311 Object rawInfo = items.get(i);
1312 if (rawInfo instanceof AppWidgetProviderInfo) {
1313 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Winson Chunga5c96362012-04-12 14:04:41 -07001314 int[] cellSpans = mLauncher.getSpanForWidget(info);
Winson Chung72d59842012-02-22 13:51:36 -08001315
1316 int maxWidth = Math.min(data.maxImageWidth,
1317 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1318 int maxHeight = Math.min(data.maxImageHeight,
1319 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001320 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001321 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001322 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001323 } else if (rawInfo instanceof ResolveInfo) {
1324 // Fill in the shortcuts information
1325 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001326 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001327 }
1328 }
Winson Chungb44b5242011-06-13 11:32:14 -07001329 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001330
Winson Chungb44b5242011-06-13 11:32:14 -07001331 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001332 if (mInTransition) {
1333 mDeferredSyncWidgetPageItems.add(data);
1334 return;
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 }
Michael Jurka39e5d172012-03-12 18:36:12 -07001336 try {
1337 int page = data.page;
1338 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -07001339
Michael Jurka39e5d172012-03-12 18:36:12 -07001340 ArrayList<Object> items = data.items;
1341 int count = items.size();
1342 for (int i = 0; i < count; ++i) {
1343 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1344 if (widget != null) {
1345 Bitmap preview = data.generatedImages.get(i);
1346 widget.applyPreview(new FastBitmapDrawable(preview), i);
1347 }
1348 }
Winson Chung68e4c642011-11-10 15:48:25 -08001349
Michael Jurka39e5d172012-03-12 18:36:12 -07001350 layout.createHardwareLayer();
1351 invalidate();
1352
1353 // Update all thread priorities
1354 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1355 while (iter.hasNext()) {
1356 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1357 int pageIndex = task.page;
1358 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1359 }
1360 } finally {
1361 data.cleanup(false);
Winson Chung68e4c642011-11-10 15:48:25 -08001362 }
Winson Chungb44b5242011-06-13 11:32:14 -07001363 }
Winson Chung46af2e82011-05-09 16:00:53 -07001364
Winson Chung785d2eb2011-04-14 16:08:02 -07001365 @Override
1366 public void syncPages() {
1367 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001368 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001369
Adam Cohen0cd3b642011-10-14 14:58:00 -07001370 Context context = getContext();
1371 for (int j = 0; j < mNumWidgetPages; ++j) {
1372 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1373 mWidgetCountY);
1374 setupPage(layout);
Michael Jurka39e5d172012-03-12 18:36:12 -07001375 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
Adam Cohen0cd3b642011-10-14 14:58:00 -07001376 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001377 }
1378
Adam Cohen0cd3b642011-10-14 14:58:00 -07001379 for (int i = 0; i < mNumAppsPages; ++i) {
1380 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1381 setupPage(layout);
1382 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001383 }
1384 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001385
Winson Chung785d2eb2011-04-14 16:08:02 -07001386 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001387 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001388 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001389 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001390 } else {
Michael Jurka39e5d172012-03-12 18:36:12 -07001391 syncWidgetPageItems(page, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001392 }
1393 }
1394
Adam Cohen22f823d2011-09-01 17:22:18 -07001395 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1396 // it is in the z-order. This is important to insure touch events are handled correctly.
1397 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -07001398 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -07001399 }
1400
Adam Cohenae4f1552011-10-20 00:15:42 -07001401 @Override
1402 protected int indexToPage(int index) {
1403 return getChildCount() - index - 1;
1404 }
1405
Adam Cohen22f823d2011-09-01 17:22:18 -07001406 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1407 @Override
1408 protected void screenScrolled(int screenCenter) {
1409 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001410
1411 for (int i = 0; i < getChildCount(); i++) {
1412 View v = getPageAt(i);
1413 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001414 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001415
1416 float interpolatedProgress =
1417 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1418 float scale = (1 - interpolatedProgress) +
1419 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1420 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001421
Adam Cohen2591f6a2011-10-25 14:36:40 -07001422 float alpha;
1423
Winson Chungd167e2a2012-04-26 13:13:01 -07001424 if (scrollProgress < 0) {
Adam Cohen2591f6a2011-10-25 14:36:40 -07001425 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001426 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001427 } else {
1428 // On large screens we need to fade the page as it nears its leftmost position
1429 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1430 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001431
1432 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1433 int pageWidth = v.getMeasuredWidth();
1434 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001435
1436 if (PERFORM_OVERSCROLL_ROTATION) {
1437 if (i == 0 && scrollProgress < 0) {
1438 // Overscroll to the left
1439 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1440 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1441 scale = 1.0f;
1442 alpha = 1.0f;
1443 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001444 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001445 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1446 // Overscroll to the right
1447 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1448 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1449 scale = 1.0f;
1450 alpha = 1.0f;
1451 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001452 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001453 } else {
1454 v.setPivotY(pageHeight / 2.0f);
1455 v.setPivotX(pageWidth / 2.0f);
1456 v.setRotationY(0f);
1457 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001458 }
1459
1460 v.setTranslationX(translationX);
1461 v.setScaleX(scale);
1462 v.setScaleY(scale);
1463 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001464
1465 // If the view has 0 alpha, we set it to be invisible so as to prevent
1466 // it from accepting touches
Michael Jurka8b805b12012-04-18 14:23:14 -07001467 if (alpha == 0) {
Adam Cohen4e844012011-11-09 13:48:04 -08001468 v.setVisibility(INVISIBLE);
1469 } else if (v.getVisibility() != VISIBLE) {
1470 v.setVisibility(VISIBLE);
1471 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001472 }
1473 }
1474 }
1475
1476 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001477 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001478 }
1479
Winson Chung785d2eb2011-04-14 16:08:02 -07001480 /**
1481 * Used by the parent to get the content width to set the tab bar to
1482 * @return
1483 */
1484 public int getPageContentWidth() {
1485 return mContentWidth;
1486 }
1487
Winson Chungb26f3d62011-06-02 10:49:29 -07001488 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001489 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001490 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001491
1492 // We reset the save index when we change pages so that it will be recalculated on next
1493 // rotation
1494 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001495 }
1496
Winson Chung785d2eb2011-04-14 16:08:02 -07001497 /*
1498 * AllAppsView implementation
1499 */
1500 @Override
1501 public void setup(Launcher launcher, DragController dragController) {
1502 mLauncher = launcher;
1503 mDragController = dragController;
1504 }
1505 @Override
1506 public void zoom(float zoom, boolean animate) {
1507 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1508 }
1509 @Override
1510 public boolean isVisible() {
1511 return (getVisibility() == VISIBLE);
1512 }
1513 @Override
1514 public boolean isAnimating() {
1515 return false;
1516 }
1517 @Override
1518 public void setApps(ArrayList<ApplicationInfo> list) {
1519 mApps = list;
1520 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001521 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001522
Winson Chung875de7e2011-06-28 14:25:17 -07001523 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1524 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001525 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001526 }
1527 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1528 // We add it in place, in alphabetical order
1529 int count = list.size();
1530 for (int i = 0; i < count; ++i) {
1531 ApplicationInfo info = list.get(i);
1532 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1533 if (index < 0) {
1534 mApps.add(-(index + 1), info);
1535 }
1536 }
1537 }
1538 @Override
1539 public void addApps(ArrayList<ApplicationInfo> list) {
1540 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001541 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001542 invalidatePageData();
1543 }
1544 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1545 ComponentName removeComponent = item.intent.getComponent();
1546 int length = list.size();
1547 for (int i = 0; i < length; ++i) {
1548 ApplicationInfo info = list.get(i);
1549 if (info.intent.getComponent().equals(removeComponent)) {
1550 return i;
1551 }
1552 }
1553 return -1;
1554 }
1555 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1556 // loop through all the apps and remove apps that have the same component
1557 int length = list.size();
1558 for (int i = 0; i < length; ++i) {
1559 ApplicationInfo info = list.get(i);
1560 int removeIndex = findAppByComponent(mApps, info);
1561 if (removeIndex > -1) {
1562 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001563 }
1564 }
1565 }
1566 @Override
1567 public void removeApps(ArrayList<ApplicationInfo> list) {
1568 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001569 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 invalidatePageData();
1571 }
1572 @Override
1573 public void updateApps(ArrayList<ApplicationInfo> list) {
1574 // We remove and re-add the updated applications list because it's properties may have
1575 // changed (ie. the title), and this will ensure that the items will be in their proper
1576 // place in the list.
1577 removeAppsWithoutInvalidate(list);
1578 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001579 updatePageCounts();
1580
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 invalidatePageData();
1582 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001583
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 @Override
1585 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001586 // If we have reset, then we should not continue to restore the previous state
1587 mSaveInstanceStateItemIndex = -1;
1588
Adam Cohenb64d36e2011-10-17 21:48:02 -07001589 AppsCustomizeTabHost tabHost = getTabHost();
1590 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001591 if (tag != null) {
1592 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1593 tabHost.setCurrentTabFromContent(ContentType.Applications);
1594 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001595 }
Winson Chung649668f2012-01-10 13:07:16 -08001596
Adam Cohenb64d36e2011-10-17 21:48:02 -07001597 if (mCurrentPage != 0) {
1598 invalidatePageData(0);
1599 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001601
1602 private AppsCustomizeTabHost getTabHost() {
1603 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1604 }
1605
Winson Chung785d2eb2011-04-14 16:08:02 -07001606 @Override
1607 public void dumpState() {
1608 // TODO: Dump information related to current list of Applications, Widgets, etc.
1609 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1610 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1611 }
Adam Cohen4e844012011-11-09 13:48:04 -08001612
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001614 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001616 for (Object i: list) {
1617 if (i instanceof AppWidgetProviderInfo) {
1618 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1619 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1620 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1621 + " initialLayout=" + info.initialLayout
1622 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1623 } else if (i instanceof ResolveInfo) {
1624 ResolveInfo info = (ResolveInfo) i;
1625 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1626 + info.icon);
1627 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 }
1629 }
Adam Cohen4e844012011-11-09 13:48:04 -08001630
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 @Override
1632 public void surrender() {
1633 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1634 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001635
1636 // Stop all background tasks
1637 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001638 }
Winson Chung007c6982011-06-14 13:27:53 -07001639
Winson Chunge4e50662012-01-23 14:45:13 -08001640 @Override
1641 public void iconPressed(PagedViewIcon icon) {
1642 // Reset the previously pressed icon and store a reference to the pressed icon so that
1643 // we can reset it on return to Launcher (in Launcher.onResume())
1644 if (mPressedIcon != null) {
1645 mPressedIcon.resetDrawableState();
1646 }
1647 mPressedIcon = icon;
1648 }
1649
1650 public void resetDrawableState() {
1651 if (mPressedIcon != null) {
1652 mPressedIcon.resetDrawableState();
1653 mPressedIcon = null;
1654 }
1655 }
Winson Chung68e4c642011-11-10 15:48:25 -08001656
Winson Chungb44b5242011-06-13 11:32:14 -07001657 /*
1658 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1659 * widget previews in the background with the AsyncTasks.
1660 */
Winson Chung68e4c642011-11-10 15:48:25 -08001661 final static int sLookBehindPageCount = 2;
1662 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001663 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001664 final int count = getChildCount();
1665 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1666 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1667 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001668 }
1669 protected int getAssociatedUpperPageBound(int page) {
1670 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001671 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1672 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1673 count - 1);
1674 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001675 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001676
1677 @Override
1678 protected String getCurrentPageDescription() {
1679 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1680 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001681 int count = 0;
1682
Adam Cohen0cd3b642011-10-14 14:58:00 -07001683 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001684 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001685 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001686 } else {
1687 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001688 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001689 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001690 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001691
Michael Jurka8b805b12012-04-18 14:23:14 -07001692 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001693 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001694}