blob: c98e885750bab8e4e307ac0748131b9e805ca24c [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 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
Adam Cohen120980b2010-12-08 11:05:37 -080019import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080020import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080024import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070025import android.appwidget.AppWidgetManager;
26import android.appwidget.AppWidgetProviderInfo;
27import android.content.ComponentName;
28import android.content.Context;
29import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080030import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070034import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080035import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070036import android.graphics.Bitmap;
Winson Chungcd4bc492010-12-09 18:52:32 -080037import android.graphics.Canvas;
Patrick Dubroy5f445422011-02-18 14:35:21 -080038import android.graphics.Rect;
Winson Chung1908d072011-02-24 18:09:44 -080039import android.graphics.RectF;
Patrick Dubroy5f445422011-02-18 14:35:21 -080040import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070041import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070042import android.util.AttributeSet;
43import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080044import android.util.Slog;
45import android.util.TypedValue;
46import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070047import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070048import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070049import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070050import android.view.Menu;
51import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070052import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080053import android.view.animation.DecelerateInterpolator;
Patrick Dubroy5f445422011-02-18 14:35:21 -080054import android.view.animation.LinearInterpolator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080055import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070056import android.widget.ImageView;
57import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080058import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070059
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import com.android.launcher.R;
61import com.android.launcher2.DropTarget.DragObject;
62
63import org.xmlpull.v1.XmlPullParser;
64
Michael Jurka72b079e2010-12-10 01:03:53 -080065import java.util.ArrayList;
66import java.util.Collections;
67import java.util.Comparator;
68import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070069
Adam Cohen7b9d3a62010-12-07 21:49:34 -080070
Michael Jurka72b079e2010-12-10 01:03:53 -080071public class CustomizePagedView extends PagedViewWithDraggableItems
72 implements View.OnClickListener, DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070073
74 public enum CustomizationType {
75 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070076 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070077 WallpaperCustomization,
78 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070079 }
80
81 private static final String TAG = "CustomizeWorkspace";
Winson Chung80baf5a2010-08-09 16:03:15 -070082
83 private Launcher mLauncher;
84 private DragController mDragController;
85 private PackageManager mPackageManager;
86
87 private CustomizationType mCustomizationType;
88
Winson Chunge3193b92010-09-10 11:44:42 -070089 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
90 private PagedViewCellLayout mWorkspaceWidgetLayout;
91
92 // The mapping between the pages and the widgets that will be laid out on them
93 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
94
Winson Chung1908d072011-02-24 18:09:44 -080095 // This is used if we want to set a min width on pages so that things inside them left align to
96 // a fixed size
97 private int mMinPageWidth;
98
Winson Chung45e1d6e2010-11-09 17:19:49 -080099 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -0700100 private int mMaxWidgetWidth;
101
Winson Chung45e1d6e2010-11-09 17:19:49 -0800102 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -0700103 private int mMaxWidgetsCellHSpan;
104
Winson Chung45e1d6e2010-11-09 17:19:49 -0800105 // The size of the items on the wallpaper tab
106 private int mWallpaperCellHSpan;
107
Winson Chung78bd53c2010-12-09 13:50:24 -0800108 // The max number of wallpaper cells to take a "page" of wallpaper items
109 private int mMaxWallpaperCellHSpan;
110
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700111 // The maximum number of rows in a paged view.
112 private int mMaxCellCountY;
113
Winson Chunge3193b92010-09-10 11:44:42 -0700114 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700115 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700116 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700117 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700118 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700119
Winson Chunge3193b92010-09-10 11:44:42 -0700120 private static final int sMinWidgetCellHSpan = 2;
121 private static final int sMaxWidgetCellHSpan = 4;
122
Michael Jurka3125d9d2010-09-27 11:30:20 -0700123 private int mChoiceModeTitleText;
124
Winson Chunge3193b92010-09-10 11:44:42 -0700125 // The scale factor for widget previews inside the widget drawer
126 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700127
128 private final Canvas mCanvas = new Canvas();
129 private final LayoutInflater mInflater;
130
Michael Jurka4c6016f2011-05-17 18:21:03 -0700131 private boolean mFirstMeasure = true;
132
Adam Cohen120980b2010-12-08 11:05:37 -0800133 private final float mTmpFloatPos[] = new float[2];
134 private final float ANIMATION_SCALE = 0.5f;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800135
136 // The duration of the translation animation that occurs during you drag and drop
137 private final int TRANSLATE_ANIM_DURATION = 400;
138
139 // The duration of the scale & alpha animation that occurs during drag and drop
140 private final int DROP_ANIM_DURATION = 200;
141
Adam Cohen120980b2010-12-08 11:05:37 -0800142 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800143
144 // The Bitmap used to generate the drag view
145 private Bitmap mDragBitmap;
146
147 private int[] mDragViewOrigin = new int[2];
Adam Cohen120980b2010-12-08 11:05:37 -0800148
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700149 private int mPageContentWidth = -1;
150 private int mPageContentHeight = -1;
151
152 private AllAppsPagedView mAllAppsPagedView;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800153
Michael Jurka87b14902011-05-25 22:13:09 -0700154 private boolean mWaitingToInitPages = true;
155
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700157 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 }
159
160 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700161 this(context, attrs, 0);
162 }
163
164 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
165 super(context, attrs, defStyle);
166
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700167 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800168 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
169 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800170 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700171 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
172 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700175 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
176 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700178
Michael Jurka7426c422010-11-11 15:23:47 -0800179 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800180 setDragSlopeThreshold(
181 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800182
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700183 mMaxCellCountY = r.getInteger(R.integer.customization_drawer_contents_maxCellCountY);
184
Winson Chung80baf5a2010-08-09 16:03:15 -0700185 setVisibility(View.GONE);
186 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700187 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700188 }
189
Winson Chung7da10252010-10-28 16:07:04 -0700190 @Override
191 protected void init() {
192 super.init();
193 mCenterPagesVertically = false;
194 }
195
Michael Jurka4c6016f2011-05-17 18:21:03 -0700196 @Override
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700197 protected void onMeasure(int widthSpec, int heightSpec) {
198 // Base the size of this control on the size of the All Apps view.
199 final int cellCountX = mAllAppsPagedView.getCellCountX();
200 final int cellCountY = Math.min(mAllAppsPagedView.getCellCountY(), mMaxCellCountY);
Michael Jurka4c6016f2011-05-17 18:21:03 -0700201
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700202 if (cellCountX != mCellCountX || cellCountY != mCellCountY) {
203 mCellCountX = cellCountX;
204 mCellCountY = cellCountY;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700205
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700206 // Create a dummy page and set it up to determine our size.
207 // The size is based on the app shortcuts tab having the same dimensions as All Apps.
Michael Jurka4c6016f2011-05-17 18:21:03 -0700208 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
209 setupPage(layout);
210 mPageContentWidth = layout.getContentWidth();
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700211 mPageContentHeight = layout.getContentHeight();
Michael Jurka7bcda142011-05-18 15:31:55 -0700212 mMinPageWidth = layout.getWidthBeforeFirstLayout();
Michael Jurka983e3fd2011-05-26 17:10:29 -0700213 postInvalidatePageData(true);
Michael Jurka4c6016f2011-05-17 18:21:03 -0700214 }
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700215 if (mPageContentHeight > 0) {
216 // Lock our height to the size of the page content
217 final int h = mPageContentHeight + mPageLayoutPaddingTop + mPageLayoutPaddingBottom;
218 heightSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
219 }
220 super.onMeasure(widthSpec, heightSpec);
221 mFirstMeasure = false;
222 }
223
224 @Override
225 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurka87b14902011-05-25 22:13:09 -0700226 if (mWaitingToInitPages) {
227 mWaitingToInitPages = false;
Michael Jurka983e3fd2011-05-26 17:10:29 -0700228 postInvalidatePageData(false);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700229 }
230 super.onLayout(changed, left, top, right, bottom);
231 mFirstLayout = false;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700232 }
233
Winson Chung80baf5a2010-08-09 16:03:15 -0700234 public void setLauncher(Launcher launcher) {
235 Context context = getContext();
236 mLauncher = launcher;
237 mPackageManager = context.getPackageManager();
238 }
239
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700240 public void setAllAppsPagedView(AllAppsPagedView view) {
241 mAllAppsPagedView = view;
242 }
243
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700244 /**
245 * Sets the list of applications that launcher has loaded.
246 */
247 public void setApps(ArrayList<ApplicationInfo> list) {
248 mApps = list;
249 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700250
251 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800252 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700253 }
254
255 /**
256 * Convenience function to add new items to the set of applications that were previously loaded.
257 * Called by both updateApps() and setApps().
258 */
259 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
260 // we add it in place, in alphabetical order
261 final int count = list.size();
262 for (int i = 0; i < count; ++i) {
263 final ApplicationInfo info = list.get(i);
264 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
265 if (index < 0) {
266 mApps.add(-(index + 1), info);
267 }
268 }
269 }
270
271 /**
272 * Adds new applications to the loaded list, and notifies the paged view to update itself.
273 */
274 public void addApps(ArrayList<ApplicationInfo> list) {
275 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700276
277 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800278 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700279 }
280
281 /**
282 * Convenience function to remove items to the set of applications that were previously loaded.
283 * Called by both updateApps() and removeApps().
284 */
285 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
286 // loop through all the apps and remove apps that have the same component
287 final int length = list.size();
288 for (int i = 0; i < length; ++i) {
289 final ApplicationInfo info = list.get(i);
290 int removeIndex = findAppByComponent(mApps, info);
291 if (removeIndex > -1) {
292 mApps.remove(removeIndex);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700293 }
294 }
295 }
296
297 /**
298 * Removes applications from the loaded list, and notifies the paged view to update itself.
299 */
300 public void removeApps(ArrayList<ApplicationInfo> list) {
301 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700302
303 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800304 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700305 }
306
307 /**
308 * Updates a set of applications from the loaded list, and notifies the paged view to update
309 * itself.
310 */
311 public void updateApps(ArrayList<ApplicationInfo> list) {
312 // We remove and re-add the updated applications list because it's properties may have
313 // changed (ie. the title), and this will ensure that the items will be in their proper
314 // place in the list.
315 removeAppsWithoutInvalidate(list);
316 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700317
318 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800319 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700320 }
321
322 /**
323 * Convenience function to find matching ApplicationInfos for removal.
324 */
325 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
326 ComponentName removeComponent = item.intent.getComponent();
327 final int length = list.size();
328 for (int i = 0; i < length; ++i) {
329 ApplicationInfo info = list.get(i);
330 if (info.intent.getComponent().equals(removeComponent)) {
331 return i;
332 }
333 }
334 return -1;
335 }
336
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700338 // get the list of widgets
339 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
340 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
341 @Override
342 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
343 return object1.label.compareTo(object2.label);
344 }
345 });
346
Winson Chung4dbea792011-05-05 14:21:32 -0700347 LauncherModel.ShortcutNameComparator resolveInfoComparator =
348 new LauncherModel.ShortcutNameComparator(mPackageManager);
Winson Chung80baf5a2010-08-09 16:03:15 -0700349
Winson Chung80baf5a2010-08-09 16:03:15 -0700350 // get the list of shortcuts
351 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
352 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
353 Collections.sort(mShortcutList, resolveInfoComparator);
354
Winson Chunge8878e32010-09-15 20:37:09 -0700355 // get the list of wallpapers
356 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800357 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
358 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700359 Collections.sort(mWallpaperList, resolveInfoComparator);
360
Winson Chung04998342011-01-05 13:54:43 -0800361 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
362 retainShortcutList.addAll(mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700363 invalidatePageData();
364 }
365
366 public void setDragController(DragController dragger) {
367 mDragController = dragger;
368 }
369
370 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800371 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800372 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800373 if (getChildCount() > 0) {
374 setCurrentPage(0);
375 updateCurrentPageScroll();
376 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700377
Winson Chunga12a2502010-12-20 14:41:35 -0800378 // End the current choice mode so that we don't carry selections across tabs
379 endChoiceMode();
380 }
381 }
382
383 public CustomizationType getCustomizationFilter() {
384 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 }
386
Patrick Dubroy5f445422011-02-18 14:35:21 -0800387 /**
388 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
Patrick Dubroy5f445422011-02-18 14:35:21 -0800389 */
390 private void resetCheckedItem(boolean animated) {
Patrick Dubroy6f133422011-02-24 12:16:12 -0800391 final Checkable checkable = getSingleCheckedGrandchild();
392 if (checkable != null) {
393 if (checkable instanceof PagedViewWidget) {
394 ((PagedViewWidget) checkable).setChecked(false, animated);
395 } else {
396 ((PagedViewIcon) checkable).setChecked(false, animated);
397 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800398 }
399 }
400
Adam Cohenc0dcf592011-06-01 15:30:43 -0700401 public void onDropCompleted(View target, DragObject d, boolean success) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800402 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
403
404 // Create a view, identical to the drag view, that is only used for animating the
405 // item onto the home screen (or back to its original position, if the drop failed).
Patrick Dubroy6f133422011-02-24 12:16:12 -0800406 final int[] pos = mDragController.getDragView().getPosition(null);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800407 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
408 animView.setVisibility(View.VISIBLE);
409
410 if (success) {
411 resetCheckedItem(true);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700412 animateDropOntoScreen(animView, (ItemInfo) d.dragInfo, DROP_ANIM_DURATION, 0);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800413 } else {
414 // Animate the icon/widget back to its original position
415 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
416 public void run() {
417 resetCheckedItem(false);
418 dragLayer.removeView(animView);
419 }
420 });
421 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800422 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800423 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800424 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700425 }
426
427 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800428 public void onDragViewVisible() {
429 }
430
Patrick Dubroy5f445422011-02-18 14:35:21 -0800431 /**
432 * Animates the given item onto the center of a home screen, and then scales the item to
433 * look as though it's disappearing onto that screen.
434 */
Adam Cohen120980b2010-12-08 11:05:37 -0800435 private void animateItemOntoScreen(View dragView,
436 final CellLayout layout, final ItemInfo info) {
437 mTmpFloatPos[0] = layout.getWidth() / 2;
438 mTmpFloatPos[1] = layout.getHeight() / 2;
439 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
440
Adam Cohen120980b2010-12-08 11:05:37 -0800441 int dragViewWidth = dragView.getMeasuredWidth();
442 int dragViewHeight = dragView.getMeasuredHeight();
443 float heightOffset = 0;
444 float widthOffset = 0;
445
446 if (dragView instanceof ImageView) {
447 Drawable d = ((ImageView) dragView).getDrawable();
448 int width = d.getIntrinsicWidth();
449 int height = d.getIntrinsicHeight();
450
451 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
452 float f = (dragViewWidth / (width * 1.0f));
453 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
454 } else {
455 float f = (dragViewHeight / (height * 1.0f));
456 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
457 }
458 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800459 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
460 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800461
Patrick Dubroy5f445422011-02-18 14:35:21 -0800462 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
463 final View dragCopy = dragLayer.createDragView(dragView);
464 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800465
Patrick Dubroy5f445422011-02-18 14:35:21 -0800466 // Translate the item to the center of the appropriate home screen
467 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800468
Patrick Dubroy5f445422011-02-18 14:35:21 -0800469 // The drop-onto-screen animation begins a bit later, but ends at the same time.
470 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
471
472 // Scale down the icon and fade out the alpha
473 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
474 }
Adam Cohen120980b2010-12-08 11:05:37 -0800475
Patrick Dubroy5f445422011-02-18 14:35:21 -0800476 /**
477 * Animation which scales the view down and animates its alpha, making it appear to disappear
478 * onto a home screen.
479 */
480 private void animateDropOntoScreen(
481 final View view, final ItemInfo info, int duration, int delay) {
482 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
483 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
484
485 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800486 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
487 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
488 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800489 anim.setInterpolator(new LinearInterpolator());
490 if (delay > 0) {
491 anim.setStartDelay(delay);
492 }
493 anim.setDuration(duration);
494 anim.addListener(new AnimatorListenerAdapter() {
495 public void onAnimationEnd(Animator animation) {
496 dragLayer.removeView(view);
497 mLauncher.addExternalItemToScreen(info, layout);
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800498 info.dropPos = null;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800499 }
500 });
501 anim.start();
502 }
Adam Cohen120980b2010-12-08 11:05:37 -0800503
Patrick Dubroy5f445422011-02-18 14:35:21 -0800504 /**
505 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
506 */
507 private void animateIntoPosition(
508 View view, float toX, float toY, final Runnable endRunnable) {
509 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
510 PropertyValuesHolder.ofFloat("x", toX),
511 PropertyValuesHolder.ofFloat("y", toY));
512 anim.setInterpolator(mQuintEaseOutInterpolator);
513 anim.setDuration(TRANSLATE_ANIM_DURATION);
514 if (endRunnable != null) {
515 anim.addListener(new AnimatorListenerAdapter() {
516 @Override
517 public void onAnimationEnd(Animator animation) {
518 endRunnable.run();
519 }
520 });
521 }
522 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800523 }
524
Patrick Dubroya669d792010-11-23 14:40:33 -0800525 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800526 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800527 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800528 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700529
Winson Chungd0d43012010-09-26 17:26:45 -0700530 // On certain pages, we allow single tap to mark items as selected so that they can be
531 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700532 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700533 switch (mCustomizationType) {
534 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700535 mChoiceModeTitleText = R.string.cab_widget_selection_text;
536 enterChoiceMode = true;
537 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700538 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700539 mChoiceModeTitleText = R.string.cab_app_selection_text;
540 enterChoiceMode = true;
541 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700542 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700543 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
544 enterChoiceMode = true;
545 break;
546 default:
547 break;
548 }
Winson Chungd0d43012010-09-26 17:26:45 -0700549
Michael Jurka3125d9d2010-09-27 11:30:20 -0700550 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700551 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700552
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700553 Workspace w = mLauncher.getWorkspace();
554 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
555 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800556 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700557
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700558 animateClickFeedback(v, new Runnable() {
559 @Override
560 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800561 cl.calculateSpans(itemInfo);
562 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
563 animateItemOntoScreen(dragView, cl, itemInfo);
564 } else {
565 mLauncher.showOutOfSpaceMessage();
566 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700567 }
568 });
Winson Chungd0d43012010-09-26 17:26:45 -0700569 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700570 }
571
572 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700573 switch (mCustomizationType) {
574 case WallpaperCustomization:
575 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700576 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700577 animateClickFeedback(v, new Runnable() {
578 @Override
579 public void run() {
580 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700581 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700582 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
583 ComponentName name = new ComponentName(info.activityInfo.packageName,
584 info.activityInfo.name);
585 createWallpapersIntent.setComponent(name);
586 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700587 }
588 });
Winson Chungd0d43012010-09-26 17:26:45 -0700589 break;
590 default:
591 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700592 }
593 }
594
Winson Chung1908d072011-02-24 18:09:44 -0800595 private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800596 final Rect bounds = d.getBounds();
597 final int w = bounds.width();
598 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800599 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800600 renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800601 return b;
602 }
603
Adam Cohen120980b2010-12-08 11:05:37 -0800604 private View getDragView(View v) {
605 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
606 v.findViewById(R.id.widget_preview) : v;
607 }
608
Michael Jurka72b079e2010-12-10 01:03:53 -0800609 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800610 if (!v.isInTouchMode()) return false;
611 if (!super.beginDragging(v)) return false;
612
Winson Chungd0d43012010-09-26 17:26:45 -0700613 // End the current choice mode before we start dragging anything
614 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
615 endChoiceMode();
616 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800617 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800618 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800619 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700620 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800621 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800622 if (v instanceof PagedViewWidget) {
623 // Get the widget preview as the drag representation
624 final LinearLayout l = (LinearLayout) v;
625 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800626
627 // Calculate how much to scale the drag preview
628 RectF tmpScaleRect = new RectF(0,0,1,1);
629 i.getImageMatrix().mapRect(tmpScaleRect);
630
631 mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
632 tmpScaleRect.bottom);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800633 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800634 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700635
Patrick Dubroy5f445422011-02-18 14:35:21 -0800636 int[] spanXY = CellLayout.rectToCell(getResources(),
637 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800638 createWidgetInfo.spanX = spanXY[0];
639 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800640 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
641 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
642 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800643 result = true;
644 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800645 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800646 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800647 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800648 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800649 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800650 // get icon (top compound drawable, index is 1)
651 final TextView tv = (TextView) v;
652 final Drawable icon = tv.getCompoundDrawables()[1];
Winson Chung1908d072011-02-24 18:09:44 -0800653 mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700654
Patrick Dubroy5f445422011-02-18 14:35:21 -0800655 Object dragInfo = v.getTag();
656 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
657 // TODO: Not sure why we have to copy this
658 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
659 }
660 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
661
662 // Calculate where to place the drag view in order to align the icon pixels with
663 // the original view.
664 v.getLocationOnScreen(mDragViewOrigin);
665 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
666 mDragViewOrigin[1] += v.getPaddingTop();
667
668 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
669 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800670 result = true;
671 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800672 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700673 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800674 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800675
676 // We toggle the checked state _after_ we create the view for the drag in case toggling the
677 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800678 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800679 // In preparation for drag, we always reset the checked grand children regardless of
680 // what choice mode we are in
681 resetCheckedGrandchildren();
682
683 // Toggle the selection on the dragged app
684 Checkable checkable = (Checkable) v;
685
686 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
687 // of the drag of the item. (The fade-in will occur when all checked states are
688 // disabled when dragging ends)
689 checkable.toggle();
690 }
691
692 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700693 }
694
Winson Chunge3193b92010-09-10 11:44:42 -0700695 /**
696 * Pre-processes the layout of the different widget pages.
697 * @return the number of pages of widgets that we have
698 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700699 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700700 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700701
Winson Chunge3193b92010-09-10 11:44:42 -0700702 // create a new page for the first set of widgets
703 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700705 mWidgetPages.add(newPage);
706
707 // do this until we have no more widgets to lay out
708 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
709 final int widgetCount = mWidgetList.size();
710 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700711 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700712 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700713
Winson Chunge3193b92010-09-10 11:44:42 -0700714 // determine the size of the current widget
715 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
716 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700717
Winson Chunge3193b92010-09-10 11:44:42 -0700718 // create a new page if necessary
719 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
720 numCellsInRow = 0;
721 newPage = new ArrayList<AppWidgetProviderInfo>();
722 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 }
724
Winson Chunge3193b92010-09-10 11:44:42 -0700725 // add the item to the current page
726 newPage.add(info);
727 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700728 }
Winson Chunge3193b92010-09-10 11:44:42 -0700729
Winson Chung80baf5a2010-08-09 16:03:15 -0700730 return mWidgetPages.size();
731 }
732
Winson Chunge3193b92010-09-10 11:44:42 -0700733 /**
Winson Chung7da10252010-10-28 16:07:04 -0700734 * Helper function to draw a drawable to the specified canvas with the specified bounds.
735 */
Winson Chung1908d072011-02-24 18:09:44 -0800736 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
737 float scaleX, float scaleY) {
Winson Chung7da10252010-10-28 16:07:04 -0700738 if (bitmap != null) mCanvas.setBitmap(bitmap);
739 mCanvas.save();
Winson Chung1908d072011-02-24 18:09:44 -0800740 mCanvas.scale(scaleX, scaleY);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800741 final Rect oldBounds = d.copyBounds();
742 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700743 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800744 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700745 mCanvas.restore();
746 }
747
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800748 /*
749 * This method fetches an xml file specified in the manifest identified by
750 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
751 * an image which will be used as the wallpaper preview for an activity
752 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
753 * in the customize drawer.
754 */
755 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800756 ActivityInfo activityInfo = ri.activityInfo;
757 XmlResourceParser parser = null;
758 try {
759 parser = activityInfo.loadXmlMetaData(mPackageManager,
760 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
761 if (parser == null) {
762 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
763 + "wallpaper provider '" + component + '\'');
764 return null;
765 }
766
767 AttributeSet attrs = Xml.asAttributeSet(parser);
768
769 int type;
770 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
771 && type != XmlPullParser.START_TAG) {
772 // drain whitespace, comments, etc.
773 }
774
775 String nodeName = parser.getName();
776 if (!"wallpaper-preview".equals(nodeName)) {
777 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
778 + "wallpaper provider '" + component + '\'');
779 return null;
780 }
781
782 // If metaData was null, we would have returned earlier when getting
783 // the parser No need to do the check here
784 Resources res = mPackageManager.getResourcesForApplication(
785 activityInfo.applicationInfo);
786
787 TypedArray sa = res.obtainAttributes(attrs,
788 com.android.internal.R.styleable.WallpaperPreviewInfo);
789
790 TypedValue value = sa.peekValue(
791 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
792 if (value == null) return null;
793
794 return res.getDrawable(value.resourceId);
795 } catch (Exception e) {
796 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
797 return null;
798 } finally {
799 if (parser != null) parser.close();
800 }
801 }
802
Winson Chung7da10252010-10-28 16:07:04 -0700803 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800804 * This method will extract the preview image specified by the wallpaper source provider (if it
805 * exists) otherwise, it will try to generate a default image preview.
806 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800807 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800808 // To be implemented later: resolving the up-to-date wallpaper thumbnail
809
810 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
811 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
812 Resources resources = mLauncher.getResources();
813
814 // Create a new bitmap to hold the widget preview
815 int width = (int) (dim * sScaleFactor);
816 int height = (int) (dim * sScaleFactor);
817 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800818
819 Drawable background = parseWallpaperPreviewXml(
820 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
821 boolean foundCustomDrawable = background != null;
822
823 if (!foundCustomDrawable) {
824 background = resources.getDrawable(R.drawable.default_widget_preview);
825 }
826
Winson Chung1908d072011-02-24 18:09:44 -0800827 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800828
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800829 // If we don't have a custom icon, we use the app icon on the default background
830 if (!foundCustomDrawable) {
831 try {
832 final IconCache iconCache =
833 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
834 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
835 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800836
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800837 final int iconSize = minDim / 2;
838 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800839 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800840 } catch (Resources.NotFoundException e) {
841 // if we can't find the icon, then just don't draw it
842 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800843 }
844
Winson Chung29d6fea2010-12-01 15:47:31 -0800845 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800846 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
847 return drawable;
848 }
849
850 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700851 * This method will extract the preview image specified by the widget developer (if it exists),
852 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800853 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700854 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800855 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800856 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700857 String packageName = info.provider.getPackageName();
858 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800859 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700860 if (info.previewImage != 0) {
861 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
862 if (drawable == null) {
863 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
864 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700865 }
866 }
867
868 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700869 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
870 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700871 if (drawable == null) {
872 Resources resources = mLauncher.getResources();
873
Winson Chung80baf5a2010-08-09 16:03:15 -0700874 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700875 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
876 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700877 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
878 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800879 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700880
Winson Chung45e1d6e2010-11-09 17:19:49 -0800881 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700882 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700883 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700884 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700886 }
887 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700888 icon = resources.getDrawable(R.drawable.ic_launcher_application);
889 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700890
Winson Chunge3193b92010-09-10 11:44:42 -0700891 final int iconSize = minDim / 2;
892 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800893 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700894 } catch (Resources.NotFoundException e) {
895 // if we can't find the icon, then just don't draw it
896 }
897
Winson Chung29d6fea2010-12-01 15:47:31 -0800898 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700899 } else {
900 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800901 final float imageWidth = drawable.getIntrinsicWidth();
902 final float imageHeight = drawable.getIntrinsicHeight();
903 final float aspect = (float) imageWidth / imageHeight;
904 final int scaledWidth =
905 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
906 final int scaledHeight =
907 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700908 int width;
909 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800910 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700911 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800912 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700913 } else {
914 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800915 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700916 }
917
918 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800919 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung7da10252010-10-28 16:07:04 -0700920
Winson Chung29d6fea2010-12-01 15:47:31 -0800921 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700922 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800923 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
924 newDrawable.getIntrinsicHeight());
925 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700926 }
927
928 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700929 layout.setCellCount(mCellCountX, mCellCountY);
930 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
931 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700932 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 }
934
Winson Chunge3193b92010-09-10 11:44:42 -0700935 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700936 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700937 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
938
939 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
940 }
941
Winson Chung80baf5a2010-08-09 16:03:15 -0700942 private void syncWidgetPages() {
Winson Chungb44b5242011-06-13 11:32:14 -0700943 /*
Winson Chung80baf5a2010-08-09 16:03:15 -0700944 if (mWidgetList == null) return;
945
Winson Chunge3193b92010-09-10 11:44:42 -0700946 // we need to repopulate with the LinearLayout layout for the widget pages
947 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700948 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700949 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800950 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700951 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700952 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
953 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700954
Winson Chunge22a8e92010-11-12 13:40:58 -0800955 addView(layout, new LinearLayout.LayoutParams(
956 LinearLayout.LayoutParams.WRAP_CONTENT,
957 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700958 }
Winson Chungb44b5242011-06-13 11:32:14 -0700959 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700960 }
961
962 private void syncWidgetPageItems(int page) {
Winson Chungb44b5242011-06-13 11:32:14 -0700963 /*
Winson Chung80baf5a2010-08-09 16:03:15 -0700964 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700965 LinearLayout layout = (LinearLayout) getChildAt(page);
966 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700967 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800968 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700969 layout.removeAllViews();
970 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700971 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
972 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800973 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
974 info.minHeight, null);
975 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -0700976 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -0700977
Winson Chung29d6fea2010-12-01 15:47:31 -0800978 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700979 R.layout.customize_paged_view_widget, layout, false);
Winson Chung1908d072011-02-24 18:09:44 -0800980
Winson Chung04998342011-01-05 13:54:43 -0800981 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -0700982 null, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -0700983 l.setTag(createItemInfo);
984 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800985 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700986 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700987
Winson Chunge3193b92010-09-10 11:44:42 -0700988 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700989 }
Winson Chungb44b5242011-06-13 11:32:14 -0700990 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700991 }
992
Winson Chung45e1d6e2010-11-09 17:19:49 -0800993 private void syncWallpaperPages() {
Winson Chungb44b5242011-06-13 11:32:14 -0700994 /*
Winson Chung45e1d6e2010-11-09 17:19:49 -0800995 if (mWallpaperList == null) return;
996
997 // We need to repopulate the LinearLayout for the wallpaper pages
998 removeAllViews();
999 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -08001000 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001001 for (int i = 0; i < numPages; ++i) {
1002 LinearLayout layout = new PagedViewExtendedLayout(getContext());
1003 layout.setGravity(Gravity.CENTER_HORIZONTAL);
1004 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1005 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1006
Winson Chunge22a8e92010-11-12 13:40:58 -08001007 addView(layout, new LinearLayout.LayoutParams(
1008 LinearLayout.LayoutParams.WRAP_CONTENT,
1009 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -08001010 }
Winson Chungb44b5242011-06-13 11:32:14 -07001011 */
Winson Chung45e1d6e2010-11-09 17:19:49 -08001012 }
1013
1014 private void syncWallpaperPageItems(int page) {
Winson Chungb44b5242011-06-13 11:32:14 -07001015 /*
Winson Chung45e1d6e2010-11-09 17:19:49 -08001016 // Load the items on to the pages
1017 LinearLayout layout = (LinearLayout) getChildAt(page);
1018 layout.removeAllViews();
1019 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -08001020 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -08001021 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -08001022 final int startIndex = page * numItemsPerPage;
1023 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
1024 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001025 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -08001026 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -07001027 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001028
Winson Chung29d6fea2010-12-01 15:47:31 -08001029 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -08001030 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001031 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
Winson Chungb44b5242011-06-13 11:32:14 -07001032 null, createHolographicOutlines);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001033 l.setTag(info);
1034 l.setOnClickListener(this);
1035
Winson Chung45e1d6e2010-11-09 17:19:49 -08001036 layout.addView(l);
1037 }
Winson Chungb44b5242011-06-13 11:32:14 -07001038 */
Winson Chung45e1d6e2010-11-09 17:19:49 -08001039 }
1040
Winson Chung80baf5a2010-08-09 16:03:15 -07001041 private void syncListPages(List<ResolveInfo> list) {
Winson Chungb44b5242011-06-13 11:32:14 -07001042 /*
Winson Chunge3193b92010-09-10 11:44:42 -07001043 // we need to repopulate with PagedViewCellLayouts
1044 removeAllViews();
1045
Winson Chung80baf5a2010-08-09 16:03:15 -07001046 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001047 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -07001048 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001049 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1050 setupPage(layout);
1051 addView(layout);
1052 }
Winson Chungb44b5242011-06-13 11:32:14 -07001053 */
Winson Chung80baf5a2010-08-09 16:03:15 -07001054 }
1055
1056 private void syncListPageItems(int page, List<ResolveInfo> list) {
Winson Chungb44b5242011-06-13 11:32:14 -07001057 /*
Winson Chung80baf5a2010-08-09 16:03:15 -07001058 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001059 final int numPages = getPageCount();
1060 final int numCells = mCellCountX * mCellCountY;
1061 final int startIndex = page * numCells;
1062 final int endIndex = Math.min(startIndex + numCells, list.size());
1063 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001064 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001065 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -07001066 for (int i = startIndex; i < endIndex; ++i) {
1067 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -07001068 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
Winson Chungdd259022011-05-10 15:59:42 -07001069 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -07001070
Winson Chung241c3b42010-08-25 16:53:03 -07001071 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1072 R.layout.customize_paged_view_item, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -07001073 icon.applyFromResolveInfo(info, mPackageManager, null,
Winson Chung04998342011-01-05 13:54:43 -08001074 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
Winson Chung63257c12011-05-05 17:06:13 -07001075 createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001076 switch (mCustomizationType) {
1077 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001078 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001079 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001080 case ShortcutCustomization:
1081 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1082 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1083 info.activityInfo.name);
1084 icon.setTag(createItemInfo);
1085 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001086 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001087 icon.setOnLongClickListener(this);
1088 break;
1089 default:
1090 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001091 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001092
1093 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001094 final int x = index % mCellCountX;
1095 final int y = index / mCellCountX;
1096 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001097 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001098 }
Winson Chungb44b5242011-06-13 11:32:14 -07001099 */
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001100 }
1101
1102 private void syncAppPages() {
Winson Chungb44b5242011-06-13 11:32:14 -07001103 /*
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001104 if (mApps == null) return;
1105
1106 // We need to repopulate with PagedViewCellLayouts
1107 removeAllViews();
1108
1109 // Ensure that we have the right number of pages
1110 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1111 for (int i = 0; i < numPages; ++i) {
1112 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1113 setupPage(layout);
1114 addView(layout);
1115 }
Winson Chungb44b5242011-06-13 11:32:14 -07001116 */
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001117 }
1118
1119 private void syncAppPageItems(int page) {
Winson Chungb44b5242011-06-13 11:32:14 -07001120 /*
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001121 if (mApps == null) return;
1122
1123 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001124 final int numPages = getPageCount();
1125 final int numCells = mCellCountX * mCellCountY;
1126 final int startIndex = page * numCells;
1127 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1128 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001129 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001130 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001131 for (int i = startIndex; i < endIndex; ++i) {
1132 final ApplicationInfo info = mApps.get(i);
Winson Chungdd259022011-05-10 15:59:42 -07001133 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001134 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1135 R.layout.all_apps_paged_view_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -07001136 icon.applyFromApplicationInfo(
Winson Chungb44b5242011-06-13 11:32:14 -07001137 info, null, true, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001138 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001139 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001140 icon.setOnLongClickListener(this);
1141
1142 final int index = i - startIndex;
1143 final int x = index % mCellCountX;
1144 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001145 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001146 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001147 }
Winson Chungb44b5242011-06-13 11:32:14 -07001148 */
Winson Chung80baf5a2010-08-09 16:03:15 -07001149 }
1150
Winson Chung80baf5a2010-08-09 16:03:15 -07001151 @Override
Michael Jurka87b14902011-05-25 22:13:09 -07001152 protected void invalidatePageData() {
1153 if (mWaitingToInitPages || mCellCountX <= 0 || mCellCountY <= 0) {
Michael Jurka4c6016f2011-05-17 18:21:03 -07001154 // We don't know our size yet, which means we haven't calculated cell count x/y;
1155 // onMeasure will call us once we figure out our size
1156 return;
1157 }
Michael Jurka87b14902011-05-25 22:13:09 -07001158 super.invalidatePageData();
1159 }
1160
1161 @Override
1162 public void syncPages() {
Winson Chungb44b5242011-06-13 11:32:14 -07001163 /*
Winson Chung1908d072011-02-24 18:09:44 -08001164 boolean enforceMinimumPagedWidths = false;
Winson Chunge3193b92010-09-10 11:44:42 -07001165 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001166 switch (mCustomizationType) {
1167 case WidgetCustomization:
1168 syncWidgetPages();
Winson Chung1908d072011-02-24 18:09:44 -08001169 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001170 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001171 case ShortcutCustomization:
1172 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001173 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001174 break;
1175 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001176 syncWallpaperPages();
Winson Chung1908d072011-02-24 18:09:44 -08001177 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001178 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001179 case ApplicationCustomization:
1180 syncAppPages();
1181 centerPagedViewCellLayouts = false;
1182 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001183 default:
1184 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001185 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001186 break;
1187 }
1188
1189 // only try and center the page if there is one page
1190 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001191 if (centerPagedViewCellLayouts) {
1192 if (childCount == 1) {
1193 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1194 layout.enableCenteredContent(true);
1195 } else {
1196 for (int i = 0; i < childCount; ++i) {
1197 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1198 layout.enableCenteredContent(false);
1199 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001200 }
1201 }
1202
Winson Chung1908d072011-02-24 18:09:44 -08001203 // Set a min page width for PagedView layout if we have more than a single page
Winson Chung34b23d52011-03-18 11:29:34 -07001204 if (enforceMinimumPagedWidths && childCount > 1) {
1205 setMinimumWidthOverride(mMinPageWidth);
1206 } else {
1207 resetMinimumWidthOverride();
Winson Chung1908d072011-02-24 18:09:44 -08001208 }
1209
1210 // Bound the current page index
Winson Chung03929772011-02-23 17:07:10 -08001211 requestLayout();
1212 post(new Runnable() {
1213 @Override
1214 public void run() {
1215 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1216 forceUpdateAdjacentPagesAlpha();
1217 }
1218 });
Winson Chungb44b5242011-06-13 11:32:14 -07001219 */
Winson Chung80baf5a2010-08-09 16:03:15 -07001220 }
1221
1222 @Override
1223 public void syncPageItems(int page) {
Winson Chungb44b5242011-06-13 11:32:14 -07001224 /*
Winson Chung80baf5a2010-08-09 16:03:15 -07001225 switch (mCustomizationType) {
1226 case WidgetCustomization:
1227 syncWidgetPageItems(page);
1228 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001229 case ShortcutCustomization:
1230 syncListPageItems(page, mShortcutList);
1231 break;
1232 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001233 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001234 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001235 case ApplicationCustomization:
1236 syncAppPageItems(page);
1237 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001238 }
Winson Chungb44b5242011-06-13 11:32:14 -07001239 */
Winson Chung80baf5a2010-08-09 16:03:15 -07001240 }
Winson Chunge3193b92010-09-10 11:44:42 -07001241
Michael Jurka7ef959b2011-02-23 11:48:32 -08001242 int getPageContentWidth() {
1243 return mPageContentWidth;
1244 }
1245
Winson Chungd0d43012010-09-26 17:26:45 -07001246 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001247 protected int getAssociatedLowerPageBound(int page) {
1248 return 0;
1249 }
Winson Chungd0d43012010-09-26 17:26:45 -07001250 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001251 protected int getAssociatedUpperPageBound(int page) {
1252 return getChildCount();
1253 }
Winson Chungd0d43012010-09-26 17:26:45 -07001254
1255 @Override
1256 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001257 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001258 return true;
1259 }
1260
1261 @Override
1262 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1263 return true;
1264 }
1265
1266 @Override
1267 public void onDestroyActionMode(ActionMode mode) {
1268 endChoiceMode();
1269 }
1270
1271 @Override
1272 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1273 return false;
1274 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001275}