blob: 3fcff726f12c4cf2195ee24451b954cdb4acc97a [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 Chung72d59842012-02-22 13:51:36 -080035import android.graphics.BitmapFactory;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.graphics.Canvas;
Peter Ng8db70002011-10-25 15:40:08 -070037import android.graphics.MaskFilter;
Winson Chung72d59842012-02-22 13:51:36 -080038import android.graphics.Matrix;
Peter Ng8db70002011-10-25 15:40:08 -070039import android.graphics.Paint;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.graphics.Rect;
Winson Chung72d59842012-02-22 13:51:36 -080041import android.graphics.RectF;
Peter Ng8db70002011-10-25 15:40:08 -070042import android.graphics.TableMaskFilter;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070044import android.os.AsyncTask;
45import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070046import android.util.AttributeSet;
47import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070048import android.view.Gravity;
Winson Chungc6f10b92011-11-14 11:39:07 -080049import android.view.KeyEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070051import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070052import android.view.View;
Adam Cohen4e844012011-11-09 13:48:04 -080053import android.view.ViewConfiguration;
Winson Chung63257c12011-05-05 17:06:13 -070054import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070055import android.view.animation.AccelerateInterpolator;
Adam Cohen2591f6a2011-10-25 14:36:40 -070056import android.view.animation.DecelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070057import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070058import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070059import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070060
61import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import com.android.launcher2.DropTarget.DragObject;
63
64import java.util.ArrayList;
65import java.util.Collections;
66import java.util.Iterator;
67import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070068
Winson Chungb44b5242011-06-13 11:32:14 -070069/**
70 * A simple callback interface which also provides the results of the task.
71 */
72interface AsyncTaskCallback {
73 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
74}
Winson Chung4e076542011-06-23 13:04:10 -070075
Winson Chungb44b5242011-06-13 11:32:14 -070076/**
77 * The data needed to perform either of the custom AsyncTasks.
78 */
79class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070080 enum Type {
Michael Jurka82369a12012-01-12 08:08:38 -080081 LoadWidgetPreviewData
Winson Chung875de7e2011-06-28 14:25:17 -070082 }
83
Winson Chungb44b5242011-06-13 11:32:14 -070084 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
85 AsyncTaskCallback postR) {
86 page = p;
87 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070088 sourceImages = si;
89 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070090 maxImageWidth = maxImageHeight = -1;
Winson Chungb44b5242011-06-13 11:32:14 -070091 doInBackgroundCallback = bgR;
92 postExecuteCallback = postR;
93 }
Michael Jurka038f9d82011-11-03 13:50:45 -070094 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070095 AsyncTaskCallback postR) {
96 page = p;
97 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070098 generatedImages = new ArrayList<Bitmap>();
Michael Jurka038f9d82011-11-03 13:50:45 -070099 maxImageWidth = cw;
100 maxImageHeight = ch;
Winson Chungb44b5242011-06-13 11:32:14 -0700101 doInBackgroundCallback = bgR;
102 postExecuteCallback = postR;
103 }
Winson Chung09945932011-09-20 14:22:40 -0700104 void cleanup(boolean cancelled) {
105 // Clean up any references to source/generated bitmaps
106 if (sourceImages != null) {
107 if (cancelled) {
108 for (Bitmap b : sourceImages) {
109 b.recycle();
110 }
111 }
112 sourceImages.clear();
113 }
114 if (generatedImages != null) {
115 if (cancelled) {
116 for (Bitmap b : generatedImages) {
117 b.recycle();
118 }
119 }
120 generatedImages.clear();
121 }
122 }
Winson Chungb44b5242011-06-13 11:32:14 -0700123 int page;
124 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700125 ArrayList<Bitmap> sourceImages;
126 ArrayList<Bitmap> generatedImages;
Michael Jurka038f9d82011-11-03 13:50:45 -0700127 int maxImageWidth;
128 int maxImageHeight;
Winson Chungb44b5242011-06-13 11:32:14 -0700129 AsyncTaskCallback doInBackgroundCallback;
130 AsyncTaskCallback postExecuteCallback;
131}
Winson Chung4e076542011-06-23 13:04:10 -0700132
Winson Chungb44b5242011-06-13 11:32:14 -0700133/**
134 * A generic template for an async task used in AppsCustomize.
135 */
136class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700137 AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700138 page = p;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700140 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 }
142 @Override
143 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
144 if (params.length != 1) return null;
145 // Load each of the widget previews in the background
146 params[0].doInBackgroundCallback.run(this, params[0]);
147 return params[0];
148 }
149 @Override
150 protected void onPostExecute(AsyncTaskPageData result) {
151 // All the widget previews are loaded, so we can just callback to inflate the page
152 result.postExecuteCallback.run(this, result);
153 }
154
155 void setThreadPriority(int p) {
156 threadPriority = p;
157 }
158 void syncThreadPriority() {
159 Process.setThreadPriority(threadPriority);
160 }
161
162 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700163 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700164 int page;
Winson Chungb44b5242011-06-13 11:32:14 -0700165 int threadPriority;
166}
Winson Chungb44b5242011-06-13 11:32:14 -0700167
168/**
169 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
170 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700171public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Winson Chunge4e50662012-01-23 14:45:13 -0800172 AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource,
Adam Cohened66b2b2012-01-23 17:28:51 -0800173 PagedViewIcon.PressedCallback, PagedViewWidget.ShortPressListener {
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
Winson Chung785d2eb2011-04-14 16:08:02 -0700244 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
245 super(context, attrs);
246 mLayoutInflater = LayoutInflater.from(context);
247 mPackageManager = context.getPackageManager();
Winson Chung785d2eb2011-04-14 16:08:02 -0700248 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700249 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700250 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700251 mCanvas = new Canvas();
252 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700253
254 // Save the default widget preview background
255 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700256 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700257 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700258
Winson Chung6032e7e2011-11-08 15:47:17 -0800259 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
260 mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1);
261 mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262 mWidgetWidthGap =
263 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
264 mWidgetHeightGap =
265 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700266 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
267 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
Winson Chung7d7541e2011-09-16 20:14:36 -0700268 mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0);
269 mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700270 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700271 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700272
Winson Chung1ed747a2011-05-03 16:18:34 -0700273 // The padding on the non-matched dimension for the default widget preview icons
274 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700275 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700276 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Adam Cohen2591f6a2011-10-25 14:36:40 -0700277 mFadeInAdjacentScreens = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700278 }
279
280 @Override
281 protected void init() {
282 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700283 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700284
285 Context context = getContext();
286 Resources r = context.getResources();
287 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
288 }
289
Winson Chungf0ea4d32011-06-06 14:27:16 -0700290 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700291 protected void onUnhandledTap(MotionEvent ev) {
292 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700293 // Dismiss AppsCustomize if we tap
294 mLauncher.showWorkspace(true);
295 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700296 }
297
Winson Chung5afbf7b2011-07-25 11:53:08 -0700298 /** Returns the item index of the center item on this page so that we can restore to this
299 * item index when we rotate. */
300 private int getMiddleComponentIndexOnCurrentPage() {
301 int i = -1;
302 if (getPageCount() > 0) {
303 int currentPage = getCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700304 if (currentPage < mNumAppsPages) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700305 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700306 PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout();
307 int numItemsPerPage = mCellCountX * mCellCountY;
308 int childCount = childrenLayout.getChildCount();
309 if (childCount > 0) {
310 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700311 }
312 } else {
313 int numApps = mApps.size();
Adam Cohen22f823d2011-09-01 17:22:18 -0700314 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700315 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
316 int childCount = layout.getChildCount();
317 if (childCount > 0) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700318 i = numApps +
319 ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2);
320 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700321 }
322 }
323 return i;
324 }
325
326 /** Get the index of the item to restore to if we need to restore the current page. */
327 int getSaveInstanceStateIndex() {
328 if (mSaveInstanceStateItemIndex == -1) {
329 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
330 }
331 return mSaveInstanceStateItemIndex;
332 }
333
334 /** Returns the page in the current orientation which is expected to contain the specified
335 * item index. */
336 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700337 if (index < 0) return 0;
338
339 if (index < mApps.size()) {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700340 int numItemsPerPage = mCellCountX * mCellCountY;
341 return (index / numItemsPerPage);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700342 } else {
Winson Chung5afbf7b2011-07-25 11:53:08 -0700343 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700344 return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage);
345 }
Winson Chung5afbf7b2011-07-25 11:53:08 -0700346 }
347
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 /**
349 * This differs from isDataReady as this is the test done if isDataReady is not set.
350 */
351 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700352 // We only do this test once, and we default to the Applications page, so we only really
353 // have to wait for there to be apps.
Adam Cohen0cd3b642011-10-14 14:58:00 -0700354 // TODO: What if one of them is validly empty
355 return !mApps.isEmpty() && !mWidgets.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700356 }
357
Winson Chung5afbf7b2011-07-25 11:53:08 -0700358 /** Restores the page for an item at the specified index */
359 void restorePageForIndex(int index) {
360 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700361 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700362 }
363
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700364 private void updatePageCounts() {
365 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
366 (float) (mWidgetCountX * mWidgetCountY));
367 mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
368 }
369
Winson Chungf0ea4d32011-06-06 14:27:16 -0700370 protected void onDataReady(int width, int height) {
371 // Note that we transpose the counts in portrait so that we get a similar layout
372 boolean isLandscape = getResources().getConfiguration().orientation ==
373 Configuration.ORIENTATION_LANDSCAPE;
374 int maxCellCountX = Integer.MAX_VALUE;
375 int maxCellCountY = Integer.MAX_VALUE;
376 if (LauncherApplication.isScreenLarge()) {
377 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
378 LauncherModel.getCellCountY());
379 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
380 LauncherModel.getCellCountX());
381 }
Winson Chung6032e7e2011-11-08 15:47:17 -0800382 if (mMaxAppCellCountX > -1) {
383 maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX);
384 }
385 if (mMaxAppCellCountY > -1) {
386 maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY);
387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700388
389 // Now that the data is ready, we can calculate the content width, the number of cells to
390 // use for each page
391 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
392 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
393 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
394 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
395 mCellCountX = mWidgetSpacingLayout.getCellCountX();
396 mCellCountY = mWidgetSpacingLayout.getCellCountY();
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700397 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700398
Winson Chungdb1138b2011-06-30 14:39:35 -0700399 // Force a measure to update recalculate the gaps
400 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
401 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
402 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700403 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700404
Michael Jurkae326f182011-11-21 14:05:46 -0800405 AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
406 final boolean hostIsTransitioning = host.isTransitioning();
407
Adam Cohen0cd3b642011-10-14 14:58:00 -0700408 // Restore the page
409 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800410 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung7d7541e2011-09-16 20:14:36 -0700411
Winson Chung3f4e1422011-11-17 14:58:51 -0800412 // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
413 // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
414 // returned while animating)
Michael Jurkae326f182011-11-21 14:05:46 -0800415 if (!hostIsTransitioning) {
Winson Chung3f4e1422011-11-17 14:58:51 -0800416 post(new Runnable() {
417 @Override
418 public void run() {
419 showAllAppsCling();
420 }
421 });
422 }
423 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700424
Winson Chung3f4e1422011-11-17 14:58:51 -0800425 void showAllAppsCling() {
426 if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) {
427 mHasShownAllAppsCling = true;
428 // Calculate the position for the cling punch through
429 int[] offset = new int[2];
430 int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY);
431 mLauncher.getDragLayer().getLocationInDragLayer(this, offset);
432 // PagedViews are centered horizontally but top aligned
433 pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 +
434 offset[0];
435 pos[1] += offset[1];
436 mLauncher.showFirstRunAllAppsCling(pos);
437 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700438 }
439
440 @Override
441 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
442 int width = MeasureSpec.getSize(widthMeasureSpec);
443 int height = MeasureSpec.getSize(heightMeasureSpec);
444 if (!isDataReady()) {
445 if (testDataReady()) {
446 setDataIsReady();
447 setMeasuredDimension(width, height);
448 onDataReady(width, height);
449 }
450 }
451
452 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
453 }
454
Winson Chung785d2eb2011-04-14 16:08:02 -0700455 public void onPackagesUpdated() {
Adam Cohenb0fa3522011-08-17 16:10:46 -0700456 // TODO: this isn't ideal, but we actually need to delay here. This call is triggered
457 // by a broadcast receiver, and in order for it to work correctly, we need to know that
458 // the AppWidgetService has already received and processed the same broadcast. Since there
459 // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally,
460 // we should have a more precise way of ensuring the AppWidgetService is up to date.
461 postDelayed(new Runnable() {
462 public void run() {
463 updatePackages();
464 }
465 }, 500);
466 }
467
468 public void updatePackages() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700469 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700470 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700471 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 List<AppWidgetProviderInfo> widgets =
473 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700474 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
475 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800476 for (AppWidgetProviderInfo widget : widgets) {
477 if (widget.minWidth > 0 && widget.minHeight > 0) {
478 mWidgets.add(widget);
479 } else {
480 Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" +
481 widget.minWidth + ", " + widget.minHeight + ")");
482 }
483 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700484 mWidgets.addAll(shortcuts);
485 Collections.sort(mWidgets,
486 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700487 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -0700488
Winson Chung875de7e2011-06-28 14:25:17 -0700489 if (wasEmpty) {
490 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
491 // a layout to do this test and invalidate the page data when ready.
492 if (testDataReady()) requestLayout();
493 } else {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700494 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -0700495 invalidatePageData();
496 }
Winson Chung4b576be2011-04-27 17:40:20 -0700497 }
498
499 @Override
500 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700501 // When we have exited all apps or are in transition, disregard clicks
502 if (!mLauncher.isAllAppsCustomizeOpen() ||
503 mLauncher.getWorkspace().isSwitchingState()) return;
504
Winson Chung4b576be2011-04-27 17:40:20 -0700505 if (v instanceof PagedViewIcon) {
506 // Animate some feedback to the click
507 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
Michael Jurkae3517d02012-01-12 07:46:24 -0800508 mLauncher.startActivitySafely(appInfo.intent, appInfo);
Winson Chung3b187b82012-01-30 15:11:08 -0800509
510 // Lock the drawable state to pressed until we return to Launcher
511 if (mPressedIcon != null) {
512 mPressedIcon.lockDrawableState();
513 }
Winson Chung4b576be2011-04-27 17:40:20 -0700514 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700515 // Let the user know that they have to long press to add a widget
516 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
517 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700518
Winson Chungd2e87b32011-06-02 10:53:07 -0700519 // Create a little animation to show that the widget can move
520 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
521 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
522 AnimatorSet bounce = new AnimatorSet();
523 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
524 tyuAnim.setDuration(125);
525 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
526 tydAnim.setDuration(100);
527 bounce.play(tyuAnim).before(tydAnim);
528 bounce.setInterpolator(new AccelerateInterpolator());
529 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700530 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700531 }
532
Winson Chungc6f10b92011-11-14 11:39:07 -0800533 public boolean onKey(View v, int keyCode, KeyEvent event) {
534 return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event);
535 }
536
Winson Chung785d2eb2011-04-14 16:08:02 -0700537 /*
538 * PagedViewWithDraggableItems implementation
539 */
540 @Override
541 protected void determineDraggingStart(android.view.MotionEvent ev) {
542 // Disable dragging by pulling an app down for now.
543 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700544
Winson Chung4b576be2011-04-27 17:40:20 -0700545 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700546 mLauncher.getWorkspace().onDragStartedWithItem(v);
547 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700548 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700549
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 private void loadWidgetInBackground(final PendingAddWidgetInfo info) {
551 final AppWidgetProviderInfo pInfo = info.info;
552 if (pInfo.configure != null) {
553 return;
554 }
555
556 mBindWidgetRunnable = new Runnable() {
557 @Override
558 public void run() {
559 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
560 AppWidgetManager.getInstance(mLauncher).bindAppWidgetId(mWidgetLoadingId,
561 info.componentName);
562 mWidgetCleanupState = WIDGET_BOUND;
563 }
564 };
565 post(mBindWidgetRunnable);
566
567 mInflateWidgetRunnable = new Runnable() {
568 @Override
569 public void run() {
570 AppWidgetHostView hostView =
571 mLauncher.getAppWidgetHost().createView(mContext, mWidgetLoadingId, pInfo);
572 info.boundWidget = hostView;
573 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800574 hostView.setVisibility(INVISIBLE);
575 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 }
577 };
578 post(mInflateWidgetRunnable);
579 }
580
581 @Override
582 public void onShortPress(View v) {
583 // We are anticipating a long press, and we use this time to load bind and instantiate
584 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen1b36dc32012-02-13 19:27:37 -0800585 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
586 loadWidgetInBackground(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800587 }
588
Adam Cohen7a326642012-02-22 12:03:22 -0800589 private void cleanupWidgetPreloading() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800590 PendingAddWidgetInfo info = mCreateWidgetInfo;
591 mCreateWidgetInfo = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800592 if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
593 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
594 }
595 if (mWidgetCleanupState == WIDGET_BOUND) {
596 removeCallbacks(mInflateWidgetRunnable);
597 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
598 AppWidgetHostView widget = info.boundWidget;
599 int widgetId = widget.getAppWidgetId();
600 mLauncher.getAppWidgetHost().deleteAppWidgetId(widgetId);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800601 mLauncher.getDragLayer().removeView(widget);
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 }
603 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
604 mWidgetLoadingId = -1;
605 }
606
Adam Cohen7a326642012-02-22 12:03:22 -0800607 @Override
608 public void cleanUpShortPress(View v) {
609 if (!mDraggingWidget) {
610 cleanupWidgetPreloading();
611 }
612 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800613
Adam Cohen7a326642012-02-22 12:03:22 -0800614 private void beginDraggingWidget(View v) {
615 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700616 // Get the widget preview as the drag representation
617 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700618 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700619
620 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800621 Bitmap preview;
622 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800623 float scale = 1f;
Winson Chung1ed747a2011-05-03 16:18:34 -0700624 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800625 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
626 createItemInfo = createWidgetInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -0700627 int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800628 int[] size = mLauncher.getWorkspace().estimateItemSize(spanXY[0],
629 spanXY[1], createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700630 createItemInfo.spanX = spanXY[0];
631 createItemInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -0800632 int[] minSpanXY = mLauncher.getMinSpanForWidget(createWidgetInfo, null);
633 createWidgetInfo.minSpanX = minSpanXY[0];
634 createWidgetInfo.minSpanY = minSpanXY[1];
Winson Chung1ed747a2011-05-03 16:18:34 -0700635
Winson Chung72d59842012-02-22 13:51:36 -0800636 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
637 float minScale = 1.25f;
638 int minWidth, minHeight;
639 minWidth = Math.max((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
640 minHeight = Math.max((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]);
Winson Chung1120e032011-11-22 16:11:31 -0800641 preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage,
Winson Chung72d59842012-02-22 13:51:36 -0800642 createWidgetInfo.icon, spanXY[0], spanXY[1], minWidth, minHeight);
643
644 // Determine the image view drawable scale relative to the preview
645 float[] mv = new float[9];
646 Matrix m = new Matrix();
647 m.setRectToRect(
648 new RectF(0f, 0f, (float) preview.getWidth(), (float) preview.getHeight()),
649 new RectF(0f, 0f, (float) previewDrawable.getIntrinsicWidth(),
650 (float) previewDrawable.getIntrinsicHeight()),
651 Matrix.ScaleToFit.START);
652 m.getValues(mv);
653 scale = (float) mv[0];
Winson Chung1ed747a2011-05-03 16:18:34 -0700654 } else {
655 // Workaround for the fact that we don't keep the original ResolveInfo associated with
656 // the shortcut around. To get the icon, we just render the preview image (which has
657 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
Winson Chung1120e032011-11-22 16:11:31 -0800658 preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
Winson Chung1ed747a2011-05-03 16:18:34 -0700659 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chung1120e032011-11-22 16:11:31 -0800660 Drawable d = image.getDrawable();
661 mCanvas.setBitmap(preview);
662 d.draw(mCanvas);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700663 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700664 createItemInfo.spanX = createItemInfo.spanY = 1;
665 }
Winson Chung4b576be2011-04-27 17:40:20 -0700666
Peter Ng8db70002011-10-25 15:40:08 -0700667 // We use a custom alpha clip table for the default widget previews
668 Paint alphaClipPaint = null;
669 if (createItemInfo instanceof PendingAddWidgetInfo) {
Michael Jurka038f9d82011-11-03 13:50:45 -0700670 if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) {
Peter Ng8db70002011-10-25 15:40:08 -0700671 MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255);
672 alphaClipPaint = new Paint();
673 alphaClipPaint.setMaskFilter(alphaClipTable);
674 }
675 }
676
Winson Chung1120e032011-11-22 16:11:31 -0800677 // Save the preview for the outline generation, then dim the preview
678 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
679 false);
Winson Chung1120e032011-11-22 16:11:31 -0800680
Winson Chung4b576be2011-04-27 17:40:20 -0700681 // Start the drag
Winson Chung1120e032011-11-22 16:11:31 -0800682 alphaClipPaint = null;
Adam Cohen446e9402011-09-15 18:21:21 -0700683 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung1120e032011-11-22 16:11:31 -0800684 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint);
685 mDragController.startDrag(image, preview, this, createItemInfo,
Winson Chung72d59842012-02-22 13:51:36 -0800686 DragController.DRAG_ACTION_COPY, null, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800687 outline.recycle();
688 preview.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700689 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800690
Winson Chung4b576be2011-04-27 17:40:20 -0700691 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700693 if (!super.beginDragging(v)) return false;
694
695 if (v instanceof PagedViewIcon) {
696 beginDraggingApplication(v);
697 } else if (v instanceof PagedViewWidget) {
698 beginDraggingWidget(v);
699 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800700
701 // We delay entering spring-loaded mode slightly to make sure the UI
702 // thready is free of any work.
703 postDelayed(new Runnable() {
704 @Override
705 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800706 // We don't enter spring-loaded mode if the drag has been cancelled
707 if (mLauncher.getDragController().isDragging()) {
708 // Dismiss the cling
709 mLauncher.dismissAllAppsCling(null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800710
Adam Cohen1b36dc32012-02-13 19:27:37 -0800711 // Reset the alpha on the dragged icon before we drag
712 resetDrawableState();
Adam Cohened66b2b2012-01-23 17:28:51 -0800713
Adam Cohen1b36dc32012-02-13 19:27:37 -0800714 // Go into spring loaded mode (must happen before we startDrag())
715 mLauncher.enterSpringLoadedDragMode();
716 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 }
Winson Chung72d59842012-02-22 13:51:36 -0800718 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800719
Winson Chung785d2eb2011-04-14 16:08:02 -0700720 return true;
721 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800722
Winson Chung557d6ed2011-07-08 15:34:52 -0700723 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700725 if (!success || (target != mLauncher.getWorkspace() &&
726 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 // Exit spring loaded mode if we have not successfully dropped or have not handled the
728 // drop in Workspace
729 mLauncher.exitSpringLoadedDragMode();
730 }
Adam Cohen446e9402011-09-15 18:21:21 -0700731 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chung785d2eb2011-04-14 16:08:02 -0700732 }
733
Winson Chung785d2eb2011-04-14 16:08:02 -0700734 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700735 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700736 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700737
738 // Display an error message if the drag failed due to there not being enough space on the
739 // target layout we were dropping on.
740 if (!success) {
741 boolean showOutOfSpaceMessage = false;
742 if (target instanceof Workspace) {
743 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
744 Workspace workspace = (Workspace) target;
745 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700746 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700747 if (layout != null) {
748 layout.calculateSpans(itemInfo);
749 showOutOfSpaceMessage =
750 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
751 }
752 }
Winson Chungfc79c802011-05-02 13:35:34 -0700753 if (showOutOfSpaceMessage) {
754 mLauncher.showOutOfSpaceMessage();
755 }
Adam Cohen7a326642012-02-22 12:03:22 -0800756
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800757 d.deferDragViewCleanupPostAnimation = false;
Adam Cohen7a326642012-02-22 12:03:22 -0800758 cleanupWidgetPreloading();
Winson Chungfc79c802011-05-02 13:35:34 -0700759 }
Adam Cohen7a326642012-02-22 12:03:22 -0800760 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700761 }
762
Winson Chung7f0acdd2011-09-19 18:34:19 -0700763 @Override
764 protected void onDetachedFromWindow() {
765 super.onDetachedFromWindow();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700766 cancelAllTasks();
767 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700768
Michael Jurkae326f182011-11-21 14:05:46 -0800769 public void clearAllWidgetPages() {
770 cancelAllTasks();
771 int count = getChildCount();
772 for (int i = 0; i < count; i++) {
773 View v = getPageAt(i);
774 if (v instanceof PagedViewGridLayout) {
775 ((PagedViewGridLayout) v).removeAllViewsOnPage();
776 mDirtyPageContent.set(i, true);
777 }
778 }
779 }
780
Adam Cohen0cd3b642011-10-14 14:58:00 -0700781 private void cancelAllTasks() {
Winson Chung7f0acdd2011-09-19 18:34:19 -0700782 // Clean up all the async tasks
783 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
784 while (iter.hasNext()) {
785 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
786 task.cancel(false);
787 iter.remove();
788 }
789 }
790
Winson Chung785d2eb2011-04-14 16:08:02 -0700791 public void setContentType(ContentType type) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700792 if (type == ContentType.Widgets) {
793 invalidatePageData(mNumAppsPages, true);
794 } else if (type == ContentType.Applications) {
795 invalidatePageData(0, true);
796 }
Winson Chungb44b5242011-06-13 11:32:14 -0700797 }
798
Adam Cohen0cd3b642011-10-14 14:58:00 -0700799 protected void snapToPage(int whichPage, int delta, int duration) {
800 super.snapToPage(whichPage, delta, duration);
801 updateCurrentTab(whichPage);
Winson Chung68e4c642011-11-10 15:48:25 -0800802
803 // Update the thread priorities given the direction lookahead
804 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
805 while (iter.hasNext()) {
806 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
807 int pageIndex = task.page + mNumAppsPages;
808 if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) ||
809 (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) {
810 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
811 } else {
812 task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
813 }
814 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700815 }
816
817 private void updateCurrentTab(int currentPage) {
818 AppsCustomizeTabHost tabHost = getTabHost();
Winson Chungc6f10b92011-11-14 11:39:07 -0800819 if (tabHost != null) {
820 String tag = tabHost.getCurrentTabTag();
821 if (tag != null) {
822 if (currentPage >= mNumAppsPages &&
823 !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) {
824 tabHost.setCurrentTabFromContent(ContentType.Widgets);
825 } else if (currentPage < mNumAppsPages &&
826 !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
827 tabHost.setCurrentTabFromContent(ContentType.Applications);
828 }
Winson Chung6a8103c2011-10-21 11:08:32 -0700829 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700830 }
831 }
832
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 /*
834 * Apps PagedView implementation
835 */
Winson Chung63257c12011-05-05 17:06:13 -0700836 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
837 int childCount = layout.getChildCount();
838 for (int i = 0; i < childCount; ++i) {
839 layout.getChildAt(i).setVisibility(visibility);
840 }
841 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700842 private void setupPage(PagedViewCellLayout layout) {
843 layout.setCellCount(mCellCountX, mCellCountY);
844 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
845 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
846 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
847
Winson Chung63257c12011-05-05 17:06:13 -0700848 // Note: We force a measure here to get around the fact that when we do layout calculations
849 // immediately after syncing, we don't have a proper width. That said, we already know the
850 // expected page width, so we can actually optimize by hiding all the TextView-based
851 // children that are expensive to measure, and let that happen naturally later.
852 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700853 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700854 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700855 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700856 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700857 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700858 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700859
Winson Chungf314b0e2011-08-16 11:54:27 -0700860 public void syncAppsPageItems(int page, boolean immediate) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700861 // ensure that we have the right number of items on the pages
Winson Chung785d2eb2011-04-14 16:08:02 -0700862 int numCells = mCellCountX * mCellCountY;
863 int startIndex = page * numCells;
864 int endIndex = Math.min(startIndex + numCells, mApps.size());
Adam Cohen22f823d2011-09-01 17:22:18 -0700865 PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700866
Winson Chung785d2eb2011-04-14 16:08:02 -0700867 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700868 ArrayList<Object> items = new ArrayList<Object>();
869 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700870 for (int i = startIndex; i < endIndex; ++i) {
871 ApplicationInfo info = mApps.get(i);
872 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
873 R.layout.apps_customize_application, layout, false);
Winson Chunge4e50662012-01-23 14:45:13 -0800874 icon.applyFromApplicationInfo(info, true, this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700875 icon.setOnClickListener(this);
876 icon.setOnLongClickListener(this);
877 icon.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -0800878 icon.setOnKeyListener(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700879
880 int index = i - startIndex;
881 int x = index % mCellCountX;
882 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700883 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700884
885 items.add(info);
886 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700887 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700888
Winson Chungf0ea4d32011-06-06 14:27:16 -0700889 layout.createHardwareLayers();
Winson Chung785d2eb2011-04-14 16:08:02 -0700890 }
Winson Chungb44b5242011-06-13 11:32:14 -0700891
892 /**
Winson Chung68e4c642011-11-10 15:48:25 -0800893 * A helper to return the priority for loading of the specified widget page.
894 */
895 private int getWidgetPageLoadPriority(int page) {
896 // If we are snapping to another page, use that index as the target page index
897 int toPage = mCurrentPage;
898 if (mNextPage > -1) {
899 toPage = mNextPage;
900 }
901
902 // We use the distance from the target page as an initial guess of priority, but if there
903 // are no pages of higher priority than the page specified, then bump up the priority of
904 // the specified page.
905 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
906 int minPageDiff = Integer.MAX_VALUE;
907 while (iter.hasNext()) {
908 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
909 minPageDiff = Math.abs(task.page + mNumAppsPages - toPage);
910 }
911
912 int rawPageDiff = Math.abs(page - toPage);
913 return rawPageDiff - Math.min(rawPageDiff, minPageDiff);
914 }
915 /**
Winson Chungb44b5242011-06-13 11:32:14 -0700916 * Return the appropriate thread priority for loading for a given page (we give the current
917 * page much higher priority)
918 */
919 private int getThreadPriorityForPage(int page) {
920 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
Winson Chung68e4c642011-11-10 15:48:25 -0800921 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700922 if (pageDiff <= 0) {
Winson Chung68e4c642011-11-10 15:48:25 -0800923 return Process.THREAD_PRIORITY_LESS_FAVORABLE;
Winson Chungb44b5242011-06-13 11:32:14 -0700924 } else if (pageDiff <= 1) {
Winson Chung68e4c642011-11-10 15:48:25 -0800925 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700926 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800927 return Process.THREAD_PRIORITY_LOWEST;
Winson Chungb44b5242011-06-13 11:32:14 -0700928 }
929 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700930 private int getSleepForPage(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -0800931 int pageDiff = getWidgetPageLoadPriority(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700932 return Math.max(0, pageDiff * sPageSleepDelay);
933 }
Winson Chungb44b5242011-06-13 11:32:14 -0700934 /**
935 * Creates and executes a new AsyncTask to load a page of widget previews.
936 */
937 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700938 int cellWidth, int cellHeight, int cellCountX) {
Winson Chung68e4c642011-11-10 15:48:25 -0800939
Winson Chungb44b5242011-06-13 11:32:14 -0700940 // Prune all tasks that are no longer needed
941 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
942 while (iter.hasNext()) {
943 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
Winson Chung68e4c642011-11-10 15:48:25 -0800944 int taskPage = task.page + mNumAppsPages;
945 if (taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
946 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700947 task.cancel(false);
948 iter.remove();
949 } else {
Winson Chung68e4c642011-11-10 15:48:25 -0800950 task.setThreadPriority(getThreadPriorityForPage(taskPage));
Winson Chungb44b5242011-06-13 11:32:14 -0700951 }
952 }
953
Winson Chungf314b0e2011-08-16 11:54:27 -0700954 // We introduce a slight delay to order the loading of side pages so that we don't thrash
Adam Cohen0cd3b642011-10-14 14:58:00 -0700955 final int sleepMs = getSleepForPage(page + mNumAppsPages);
Winson Chungb44b5242011-06-13 11:32:14 -0700956 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Michael Jurka038f9d82011-11-03 13:50:45 -0700957 new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700958 @Override
959 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chungf314b0e2011-08-16 11:54:27 -0700960 try {
Winson Chung09945932011-09-20 14:22:40 -0700961 try {
962 Thread.sleep(sleepMs);
963 } catch (Exception e) {}
964 loadWidgetPreviewsInBackground(task, data);
965 } finally {
966 if (task.isCancelled()) {
967 data.cleanup(true);
968 }
969 }
Winson Chungb44b5242011-06-13 11:32:14 -0700970 }
971 },
972 new AsyncTaskCallback() {
973 @Override
974 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
Winson Chung09945932011-09-20 14:22:40 -0700975 try {
976 mRunningTasks.remove(task);
977 if (task.isCancelled()) return;
Winson Chung09945932011-09-20 14:22:40 -0700978 onSyncWidgetPageItems(data);
979 } finally {
980 data.cleanup(task.isCancelled());
981 }
Winson Chungb44b5242011-06-13 11:32:14 -0700982 }
Winson Chung09945932011-09-20 14:22:40 -0700983 });
Winson Chungb44b5242011-06-13 11:32:14 -0700984
985 // Ensure that the task is appropriately prioritized and runs in parallel
Adam Cohen0cd3b642011-10-14 14:58:00 -0700986 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page,
Winson Chung875de7e2011-06-28 14:25:17 -0700987 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chung68e4c642011-11-10 15:48:25 -0800988 t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages));
Winson Chungb44b5242011-06-13 11:32:14 -0700989 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
990 mRunningTasks.add(t);
991 }
Winson Chungb44b5242011-06-13 11:32:14 -0700992
Winson Chung785d2eb2011-04-14 16:08:02 -0700993 /*
994 * Widgets PagedView implementation
995 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700996 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
998 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700999
1000 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -07001001 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -07001002 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
1003 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -07001004 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -07001005 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -07001006 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001007
Winson Chung5fc72b32011-10-11 17:53:58 -07001008 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
1009 renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF);
Winson Chung70fc4382011-08-08 15:31:33 -07001010 }
Michael Jurka92f3d462011-11-22 21:02:29 -08001011
Winson Chung5fc72b32011-10-11 17:53:58 -07001012 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
1013 float scale, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -07001014 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -07001015 Canvas c = new Canvas(bitmap);
Winson Chung5fc72b32011-10-11 17:53:58 -07001016 c.scale(scale, scale);
Winson Chung201bc822011-06-20 15:41:53 -07001017 Rect oldBounds = d.copyBounds();
1018 d.setBounds(x, y, x + w, y + h);
1019 d.draw(c);
1020 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -07001021 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -07001022 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001024 private Bitmap getShortcutPreview(ResolveInfo info) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001025 // Render the background
Peter Ng8db70002011-10-25 15:40:08 -07001026 int offset = 0;
1027 int bitmapSize = mAppIconSize;
Winson Chung5fc72b32011-10-11 17:53:58 -07001028 Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888);
Winson Chung5fc72b32011-10-11 17:53:58 -07001029
Winson Chung1ed747a2011-05-03 16:18:34 -07001030 // Render the icon
Winson Chung0b9fcf52011-10-31 13:05:15 -07001031 Drawable icon = mIconCache.getFullResIcon(info);
Winson Chung5fc72b32011-10-11 17:53:58 -07001032 renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize);
Winson Chungb44b5242011-06-13 11:32:14 -07001033 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -07001034 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001035
Michael Jurka038f9d82011-11-03 13:50:45 -07001036 private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId,
1037 int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) {
Winson Chung4b576be2011-04-27 17:40:20 -07001038 // Load the preview image if possible
Michael Jurka038f9d82011-11-03 13:50:45 -07001039 String packageName = provider.getPackageName();
1040 if (maxWidth < 0) maxWidth = Integer.MAX_VALUE;
1041 if (maxHeight < 0) maxHeight = Integer.MAX_VALUE;
Winson Chung4b576be2011-04-27 17:40:20 -07001042
Michael Jurka038f9d82011-11-03 13:50:45 -07001043 Drawable drawable = null;
1044 if (previewImage != 0) {
1045 drawable = mPackageManager.getDrawable(packageName, previewImage, null);
1046 if (drawable == null) {
1047 Log.w(LOG_TAG, "Can't load widget preview drawable 0x" +
1048 Integer.toHexString(previewImage) + " for provider: " + provider);
Winson Chung4b576be2011-04-27 17:40:20 -07001049 }
1050 }
1051
Michael Jurka038f9d82011-11-03 13:50:45 -07001052 int bitmapWidth;
1053 int bitmapHeight;
1054 boolean widgetPreviewExists = (drawable != null);
1055 if (widgetPreviewExists) {
1056 bitmapWidth = drawable.getIntrinsicWidth();
1057 bitmapHeight = drawable.getIntrinsicHeight();
Michael Jurka038f9d82011-11-03 13:50:45 -07001058 } else {
1059 // Determine the size of the bitmap for the preview image we will generate
Winson Chung5fc72b32011-10-11 17:53:58 -07001060 // TODO: This actually uses the apps customize cell layout params, where as we make want
1061 // the Workspace params for more accuracy.
Michael Jurka038f9d82011-11-03 13:50:45 -07001062 bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
1063 bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
Winson Chung273c1022011-07-11 13:40:52 -07001064 if (cellHSpan == cellVSpan) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001065 // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1
Michael Jurka038f9d82011-11-03 13:50:45 -07001066 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
Winson Chung5fc72b32011-10-11 17:53:58 -07001067 if (cellHSpan <= 1) {
Peter Ng8db70002011-10-25 15:40:08 -07001068 bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001069 } else {
Peter Ng8db70002011-10-25 15:40:08 -07001070 bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset;
Winson Chung5fc72b32011-10-11 17:53:58 -07001071 }
Winson Chung1ed747a2011-05-03 16:18:34 -07001072 }
Michael Jurka038f9d82011-11-03 13:50:45 -07001073 }
1074
1075 float scale = 1f;
1076 if (bitmapWidth > maxWidth) {
1077 scale = maxWidth / (float) bitmapWidth;
1078 }
1079 if (bitmapHeight * scale > maxHeight) {
1080 scale = maxHeight / (float) bitmapHeight;
1081 }
1082 if (scale != 1f) {
1083 bitmapWidth = (int) (scale * bitmapWidth);
1084 bitmapHeight = (int) (scale * bitmapHeight);
1085 }
1086
1087 Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
1088
1089 if (widgetPreviewExists) {
1090 renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
1091 } else {
1092 // Generate a preview image if we couldn't load one
1093 int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
1094 int smallestSide = Math.min(bitmapWidth, bitmapHeight);
1095 float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);
Peter Ng8db70002011-10-25 15:40:08 -07001096 if (cellHSpan != 1 || cellVSpan != 1) {
1097 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth,
1098 bitmapHeight);
1099 }
Winson Chung4b576be2011-04-27 17:40:20 -07001100
1101 // Draw the icon in the top left corner
1102 try {
1103 Drawable icon = null;
Peter Ng8db70002011-10-25 15:40:08 -07001104 int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2);
1105 int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2);
Michael Jurka038f9d82011-11-03 13:50:45 -07001106 if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
Michael Jurka31234d82011-11-10 15:15:15 -08001107 Resources resources = mLauncher.getResources();
Winson Chung4b576be2011-04-27 17:40:20 -07001108 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
1109
Peter Ng8db70002011-10-25 15:40:08 -07001110 renderDrawableToBitmap(icon, preview, hoffset, yoffset,
1111 (int) (mAppIconSize * iconScale),
Winson Chung5fc72b32011-10-11 17:53:58 -07001112 (int) (mAppIconSize * iconScale));
Winson Chung4b576be2011-04-27 17:40:20 -07001113 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -07001114 }
Winson Chungb44b5242011-06-13 11:32:14 -07001115 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001117
Michael Jurka038f9d82011-11-03 13:50:45 -07001118 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001119 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -07001120
Winson Chungd2945262011-06-24 15:22:14 -07001121 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -07001122 final ArrayList<Object> items = new ArrayList<Object>();
1123 int contentWidth = mWidgetSpacingLayout.getContentWidth();
1124 final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001125 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Michael Jurka038f9d82011-11-03 13:50:45 -07001126 int contentHeight = mWidgetSpacingLayout.getContentHeight();
1127 final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001128 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -07001129
Winson Chunge4a647f2011-09-30 14:41:25 -07001130 // Prepare the set of widgets to load previews for in the background
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001131 int offset = page * numItemsPerPage;
1132 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
1133 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -07001134 }
1135
Winson Chunge4a647f2011-09-30 14:41:25 -07001136 // Prepopulate the pages with the other widget info, and fill in the previews later
Michael Jurka038f9d82011-11-03 13:50:45 -07001137 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chunge4a647f2011-09-30 14:41:25 -07001138 layout.setColumnCount(layout.getCellCountX());
1139 for (int i = 0; i < items.size(); ++i) {
1140 Object rawInfo = items.get(i);
1141 PendingAddItemInfo createItemInfo = null;
1142 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
1143 R.layout.apps_customize_widget, layout, false);
1144 if (rawInfo instanceof AppWidgetProviderInfo) {
1145 // Fill in the widget information
1146 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
1147 createItemInfo = new PendingAddWidgetInfo(info, null, null);
1148 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Michael Jurka82369a12012-01-12 08:08:38 -08001149 widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans);
Winson Chunge4a647f2011-09-30 14:41:25 -07001150 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001151 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001152 } else if (rawInfo instanceof ResolveInfo) {
1153 // Fill in the shortcuts information
1154 ResolveInfo info = (ResolveInfo) rawInfo;
1155 createItemInfo = new PendingAddItemInfo();
1156 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1157 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1158 info.activityInfo.name);
Michael Jurka82369a12012-01-12 08:08:38 -08001159 widget.applyFromResolveInfo(mPackageManager, info);
Winson Chunge4a647f2011-09-30 14:41:25 -07001160 widget.setTag(createItemInfo);
1161 }
1162 widget.setOnClickListener(this);
1163 widget.setOnLongClickListener(this);
1164 widget.setOnTouchListener(this);
Winson Chungc6f10b92011-11-14 11:39:07 -08001165 widget.setOnKeyListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -07001166
1167 // Layout each widget
1168 int ix = i % mWidgetCountX;
1169 int iy = i / mWidgetCountX;
1170 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
1171 GridLayout.spec(iy, GridLayout.LEFT),
1172 GridLayout.spec(ix, GridLayout.TOP));
1173 lp.width = cellWidth;
1174 lp.height = cellHeight;
1175 lp.setGravity(Gravity.TOP | Gravity.LEFT);
1176 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
1177 if (iy > 0) lp.topMargin = mWidgetHeightGap;
1178 layout.addView(widget, lp);
1179 }
1180
Michael Jurka038f9d82011-11-03 13:50:45 -07001181 // wait until a call on onLayout to start loading, because
1182 // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out
1183 // TODO: can we do a measure/layout immediately?
1184 layout.setOnLayoutListener(new Runnable() {
1185 public void run() {
1186 // Load the widget previews
1187 int maxPreviewWidth = cellWidth;
1188 int maxPreviewHeight = cellHeight;
1189 if (layout.getChildCount() > 0) {
1190 PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0);
1191 int[] maxSize = w.getPreviewSize();
1192 maxPreviewWidth = maxSize[0];
1193 maxPreviewHeight = maxSize[1];
1194 }
1195 if (immediate) {
1196 AsyncTaskPageData data = new AsyncTaskPageData(page, items,
1197 maxPreviewWidth, maxPreviewHeight, null, null);
1198 loadWidgetPreviewsInBackground(null, data);
1199 onSyncWidgetPageItems(data);
1200 } else {
1201 prepareLoadWidgetPreviewsTask(page, items,
1202 maxPreviewWidth, maxPreviewHeight, mWidgetCountX);
1203 }
1204 }
1205 });
Winson Chungf314b0e2011-08-16 11:54:27 -07001206 }
1207 private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task,
1208 AsyncTaskPageData data) {
Winson Chung68e4c642011-11-10 15:48:25 -08001209 // loadWidgetPreviewsInBackground can be called without a task to load a set of widget
1210 // previews synchronously
Winson Chungf314b0e2011-08-16 11:54:27 -07001211 if (task != null) {
1212 // Ensure that this task starts running at the correct priority
1213 task.syncThreadPriority();
1214 }
1215
1216 // Load each of the widget/shortcut previews
1217 ArrayList<Object> items = data.items;
1218 ArrayList<Bitmap> images = data.generatedImages;
1219 int count = items.size();
Winson Chungf314b0e2011-08-16 11:54:27 -07001220 for (int i = 0; i < count; ++i) {
1221 if (task != null) {
1222 // Ensure we haven't been cancelled yet
1223 if (task.isCancelled()) break;
1224 // Before work on each item, ensure that this task is running at the correct
1225 // priority
1226 task.syncThreadPriority();
1227 }
1228
1229 Object rawInfo = items.get(i);
1230 if (rawInfo instanceof AppWidgetProviderInfo) {
1231 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
Adam Cohenf814aa02011-09-01 13:48:05 -07001232 int[] cellSpans = mLauncher.getSpanForWidget(info, null);
Winson Chung72d59842012-02-22 13:51:36 -08001233
1234 int maxWidth = Math.min(data.maxImageWidth,
1235 mWidgetSpacingLayout.estimateCellWidth(cellSpans[0]));
1236 int maxHeight = Math.min(data.maxImageHeight,
1237 mWidgetSpacingLayout.estimateCellHeight(cellSpans[1]));
Michael Jurka038f9d82011-11-03 13:50:45 -07001238 Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon,
Winson Chung72d59842012-02-22 13:51:36 -08001239 cellSpans[0], cellSpans[1], maxWidth, maxHeight);
Michael Jurka038f9d82011-11-03 13:50:45 -07001240 images.add(b);
Winson Chungf314b0e2011-08-16 11:54:27 -07001241 } else if (rawInfo instanceof ResolveInfo) {
1242 // Fill in the shortcuts information
1243 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurka038f9d82011-11-03 13:50:45 -07001244 images.add(getShortcutPreview(info));
Winson Chungf314b0e2011-08-16 11:54:27 -07001245 }
1246 }
Winson Chungb44b5242011-06-13 11:32:14 -07001247 }
1248 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
1249 int page = data.page;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001250 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages);
Winson Chung785d2eb2011-04-14 16:08:02 -07001251
Winson Chungb44b5242011-06-13 11:32:14 -07001252 ArrayList<Object> items = data.items;
1253 int count = items.size();
Winson Chungb44b5242011-06-13 11:32:14 -07001254 for (int i = 0; i < count; ++i) {
Winson Chunge4a647f2011-09-30 14:41:25 -07001255 PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i);
1256 if (widget != null) {
Winson Chung5fc72b32011-10-11 17:53:58 -07001257 Bitmap preview = data.generatedImages.get(i);
Michael Jurka038f9d82011-11-03 13:50:45 -07001258 widget.applyPreview(new FastBitmapDrawable(preview), i);
Winson Chung1ed747a2011-05-03 16:18:34 -07001259 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001260 }
Winson Chungb44b5242011-06-13 11:32:14 -07001261
Winson Chung68e4c642011-11-10 15:48:25 -08001262 layout.createHardwareLayer();
Winson Chungb44b5242011-06-13 11:32:14 -07001263 invalidate();
Winson Chung68e4c642011-11-10 15:48:25 -08001264
Winson Chung68e4c642011-11-10 15:48:25 -08001265 // Update all thread priorities
1266 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
1267 while (iter.hasNext()) {
1268 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
1269 int pageIndex = task.page + mNumAppsPages;
1270 task.setThreadPriority(getThreadPriorityForPage(pageIndex));
1271 }
Winson Chungb44b5242011-06-13 11:32:14 -07001272 }
Winson Chung46af2e82011-05-09 16:00:53 -07001273
Winson Chung785d2eb2011-04-14 16:08:02 -07001274 @Override
1275 public void syncPages() {
1276 removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -07001277 cancelAllTasks();
Winson Chung875de7e2011-06-28 14:25:17 -07001278
Adam Cohen0cd3b642011-10-14 14:58:00 -07001279 Context context = getContext();
1280 for (int j = 0; j < mNumWidgetPages; ++j) {
1281 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
1282 mWidgetCountY);
1283 setupPage(layout);
1284 addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT,
1285 LayoutParams.MATCH_PARENT));
Winson Chung875de7e2011-06-28 14:25:17 -07001286 }
1287
Adam Cohen0cd3b642011-10-14 14:58:00 -07001288 for (int i = 0; i < mNumAppsPages; ++i) {
1289 PagedViewCellLayout layout = new PagedViewCellLayout(context);
1290 setupPage(layout);
1291 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07001292 }
1293 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001294
Winson Chung785d2eb2011-04-14 16:08:02 -07001295 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07001296 public void syncPageItems(int page, boolean immediate) {
Adam Cohen0cd3b642011-10-14 14:58:00 -07001297 if (page < mNumAppsPages) {
Winson Chungf314b0e2011-08-16 11:54:27 -07001298 syncAppsPageItems(page, immediate);
Adam Cohen0cd3b642011-10-14 14:58:00 -07001299 } else {
1300 syncWidgetPageItems(page - mNumAppsPages, immediate);
Winson Chung785d2eb2011-04-14 16:08:02 -07001301 }
1302 }
1303
Adam Cohen22f823d2011-09-01 17:22:18 -07001304 // We want our pages to be z-ordered such that the further a page is to the left, the higher
1305 // it is in the z-order. This is important to insure touch events are handled correctly.
1306 View getPageAt(int index) {
1307 return getChildAt(getChildCount() - index - 1);
1308 }
1309
Adam Cohenae4f1552011-10-20 00:15:42 -07001310 @Override
1311 protected int indexToPage(int index) {
1312 return getChildCount() - index - 1;
1313 }
1314
Adam Cohen22f823d2011-09-01 17:22:18 -07001315 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
1316 @Override
1317 protected void screenScrolled(int screenCenter) {
1318 super.screenScrolled(screenCenter);
Adam Cohen22f823d2011-09-01 17:22:18 -07001319
1320 for (int i = 0; i < getChildCount(); i++) {
1321 View v = getPageAt(i);
1322 if (v != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001323 float scrollProgress = getScrollProgress(screenCenter, v, i);
Adam Cohen22f823d2011-09-01 17:22:18 -07001324
1325 float interpolatedProgress =
1326 mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0)));
1327 float scale = (1 - interpolatedProgress) +
1328 interpolatedProgress * TRANSITION_SCALE_FACTOR;
1329 float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001330
Adam Cohen2591f6a2011-10-25 14:36:40 -07001331 float alpha;
1332
1333 if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) {
1334 alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation(
Adam Cohenb5ba0972011-09-07 18:02:31 -07001335 1 - Math.abs(scrollProgress)) : 1.0f;
Adam Cohen2591f6a2011-10-25 14:36:40 -07001336 } else {
1337 // On large screens we need to fade the page as it nears its leftmost position
1338 alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress);
1339 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001340
1341 v.setCameraDistance(mDensity * CAMERA_DISTANCE);
1342 int pageWidth = v.getMeasuredWidth();
1343 int pageHeight = v.getMeasuredHeight();
Adam Cohenb5ba0972011-09-07 18:02:31 -07001344
1345 if (PERFORM_OVERSCROLL_ROTATION) {
1346 if (i == 0 && scrollProgress < 0) {
1347 // Overscroll to the left
1348 v.setPivotX(TRANSITION_PIVOT * pageWidth);
1349 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1350 scale = 1.0f;
1351 alpha = 1.0f;
1352 // On the first page, we don't want the page to have any lateral motion
Adam Cohenebea84d2011-11-09 17:20:41 -08001353 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001354 } else if (i == getChildCount() - 1 && scrollProgress > 0) {
1355 // Overscroll to the right
1356 v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth);
1357 v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress);
1358 scale = 1.0f;
1359 alpha = 1.0f;
1360 // On the last page, we don't want the page to have any lateral motion.
Adam Cohenebea84d2011-11-09 17:20:41 -08001361 translationX = 0;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001362 } else {
1363 v.setPivotY(pageHeight / 2.0f);
1364 v.setPivotX(pageWidth / 2.0f);
1365 v.setRotationY(0f);
1366 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001367 }
1368
1369 v.setTranslationX(translationX);
1370 v.setScaleX(scale);
1371 v.setScaleY(scale);
1372 v.setAlpha(alpha);
Adam Cohen4e844012011-11-09 13:48:04 -08001373
1374 // If the view has 0 alpha, we set it to be invisible so as to prevent
1375 // it from accepting touches
1376 if (alpha < ViewConfiguration.ALPHA_THRESHOLD) {
1377 v.setVisibility(INVISIBLE);
1378 } else if (v.getVisibility() != VISIBLE) {
1379 v.setVisibility(VISIBLE);
1380 }
Adam Cohen22f823d2011-09-01 17:22:18 -07001381 }
1382 }
1383 }
1384
1385 protected void overScroll(float amount) {
Adam Cohencff6af82011-09-13 14:51:53 -07001386 acceleratedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -07001387 }
1388
Winson Chung785d2eb2011-04-14 16:08:02 -07001389 /**
1390 * Used by the parent to get the content width to set the tab bar to
1391 * @return
1392 */
1393 public int getPageContentWidth() {
1394 return mContentWidth;
1395 }
1396
Winson Chungb26f3d62011-06-02 10:49:29 -07001397 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -07001398 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -07001399 super.onPageEndMoving();
Winson Chung5afbf7b2011-07-25 11:53:08 -07001400
1401 // We reset the save index when we change pages so that it will be recalculated on next
1402 // rotation
1403 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -07001404 }
1405
Winson Chung785d2eb2011-04-14 16:08:02 -07001406 /*
1407 * AllAppsView implementation
1408 */
1409 @Override
1410 public void setup(Launcher launcher, DragController dragController) {
1411 mLauncher = launcher;
1412 mDragController = dragController;
1413 }
1414 @Override
1415 public void zoom(float zoom, boolean animate) {
1416 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1417 }
1418 @Override
1419 public boolean isVisible() {
1420 return (getVisibility() == VISIBLE);
1421 }
1422 @Override
1423 public boolean isAnimating() {
1424 return false;
1425 }
1426 @Override
1427 public void setApps(ArrayList<ApplicationInfo> list) {
1428 mApps = list;
1429 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001430 updatePageCounts();
Winson Chungf0ea4d32011-06-06 14:27:16 -07001431
Winson Chung875de7e2011-06-28 14:25:17 -07001432 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1433 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001435 }
1436 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1437 // We add it in place, in alphabetical order
1438 int count = list.size();
1439 for (int i = 0; i < count; ++i) {
1440 ApplicationInfo info = list.get(i);
1441 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1442 if (index < 0) {
1443 mApps.add(-(index + 1), info);
1444 }
1445 }
1446 }
1447 @Override
1448 public void addApps(ArrayList<ApplicationInfo> list) {
1449 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001450 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001451 invalidatePageData();
1452 }
1453 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1454 ComponentName removeComponent = item.intent.getComponent();
1455 int length = list.size();
1456 for (int i = 0; i < length; ++i) {
1457 ApplicationInfo info = list.get(i);
1458 if (info.intent.getComponent().equals(removeComponent)) {
1459 return i;
1460 }
1461 }
1462 return -1;
1463 }
1464 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1465 // loop through all the apps and remove apps that have the same component
1466 int length = list.size();
1467 for (int i = 0; i < length; ++i) {
1468 ApplicationInfo info = list.get(i);
1469 int removeIndex = findAppByComponent(mApps, info);
1470 if (removeIndex > -1) {
1471 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001472 }
1473 }
1474 }
1475 @Override
1476 public void removeApps(ArrayList<ApplicationInfo> list) {
1477 removeAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001478 updatePageCounts();
Winson Chung785d2eb2011-04-14 16:08:02 -07001479 invalidatePageData();
1480 }
1481 @Override
1482 public void updateApps(ArrayList<ApplicationInfo> list) {
1483 // We remove and re-add the updated applications list because it's properties may have
1484 // changed (ie. the title), and this will ensure that the items will be in their proper
1485 // place in the list.
1486 removeAppsWithoutInvalidate(list);
1487 addAppsWithoutInvalidate(list);
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001488 updatePageCounts();
1489
Winson Chung785d2eb2011-04-14 16:08:02 -07001490 invalidatePageData();
1491 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001492
Winson Chung785d2eb2011-04-14 16:08:02 -07001493 @Override
1494 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -08001495 // If we have reset, then we should not continue to restore the previous state
1496 mSaveInstanceStateItemIndex = -1;
1497
Adam Cohenb64d36e2011-10-17 21:48:02 -07001498 AppsCustomizeTabHost tabHost = getTabHost();
1499 String tag = tabHost.getCurrentTabTag();
Winson Chung6a8103c2011-10-21 11:08:32 -07001500 if (tag != null) {
1501 if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) {
1502 tabHost.setCurrentTabFromContent(ContentType.Applications);
1503 }
Adam Cohenb64d36e2011-10-17 21:48:02 -07001504 }
Winson Chung649668f2012-01-10 13:07:16 -08001505
Adam Cohenb64d36e2011-10-17 21:48:02 -07001506 if (mCurrentPage != 0) {
1507 invalidatePageData(0);
1508 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001509 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001510
1511 private AppsCustomizeTabHost getTabHost() {
1512 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
1513 }
1514
Winson Chung785d2eb2011-04-14 16:08:02 -07001515 @Override
1516 public void dumpState() {
1517 // TODO: Dump information related to current list of Applications, Widgets, etc.
1518 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1519 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1520 }
Adam Cohen4e844012011-11-09 13:48:04 -08001521
Winson Chung785d2eb2011-04-14 16:08:02 -07001522 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001523 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001524 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001525 for (Object i: list) {
1526 if (i instanceof AppWidgetProviderInfo) {
1527 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1528 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1529 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1530 + " initialLayout=" + info.initialLayout
1531 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1532 } else if (i instanceof ResolveInfo) {
1533 ResolveInfo info = (ResolveInfo) i;
1534 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1535 + info.icon);
1536 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001537 }
1538 }
Adam Cohen4e844012011-11-09 13:48:04 -08001539
Winson Chung785d2eb2011-04-14 16:08:02 -07001540 @Override
1541 public void surrender() {
1542 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1543 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001544
1545 // Stop all background tasks
1546 cancelAllTasks();
Winson Chung785d2eb2011-04-14 16:08:02 -07001547 }
Winson Chung007c6982011-06-14 13:27:53 -07001548
Winson Chunge4e50662012-01-23 14:45:13 -08001549 @Override
1550 public void iconPressed(PagedViewIcon icon) {
1551 // Reset the previously pressed icon and store a reference to the pressed icon so that
1552 // we can reset it on return to Launcher (in Launcher.onResume())
1553 if (mPressedIcon != null) {
1554 mPressedIcon.resetDrawableState();
1555 }
1556 mPressedIcon = icon;
1557 }
1558
1559 public void resetDrawableState() {
1560 if (mPressedIcon != null) {
1561 mPressedIcon.resetDrawableState();
1562 mPressedIcon = null;
1563 }
1564 }
Winson Chung68e4c642011-11-10 15:48:25 -08001565
Winson Chungb44b5242011-06-13 11:32:14 -07001566 /*
1567 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1568 * widget previews in the background with the AsyncTasks.
1569 */
Winson Chung68e4c642011-11-10 15:48:25 -08001570 final static int sLookBehindPageCount = 2;
1571 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001572 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001573 final int count = getChildCount();
1574 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1575 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1576 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001577 }
1578 protected int getAssociatedUpperPageBound(int page) {
1579 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001580 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1581 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1582 count - 1);
1583 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001584 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001585
1586 @Override
1587 protected String getCurrentPageDescription() {
1588 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1589 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001590 int count = 0;
1591
Adam Cohen0cd3b642011-10-14 14:58:00 -07001592 if (page < mNumAppsPages) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001593 stringId = R.string.apps_customize_apps_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001594 count = mNumAppsPages;
Adam Cohen0cd3b642011-10-14 14:58:00 -07001595 } else {
1596 page -= mNumAppsPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001597 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001598 count = mNumWidgetPages;
Winson Chung6a0f57d2011-06-29 20:10:49 -07001599 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001600
Adam Cohend3357b12011-10-18 14:58:11 -07001601 return String.format(mContext.getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001602 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001603}