blob: cdae80c45ef46d938a25a6ec8f1b394d0f67e6f2 [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
Michael Jurka72b079e2010-12-10 01:03:53 -080019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
Adam Cohen7b9d3a62010-12-07 21:49:34 -080021import org.xmlpull.v1.XmlPullParser;
22
Adam Cohen120980b2010-12-08 11:05:37 -080023import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080024import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
27import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080028import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070029import android.appwidget.AppWidgetManager;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080039import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.graphics.Bitmap;
Winson Chungcd4bc492010-12-09 18:52:32 -080041import android.graphics.Canvas;
Patrick Dubroy5f445422011-02-18 14:35:21 -080042import android.graphics.Rect;
Winson Chung1908d072011-02-24 18:09:44 -080043import android.graphics.RectF;
Patrick Dubroy5f445422011-02-18 14:35:21 -080044import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.util.AttributeSet;
47import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080048import android.util.Slog;
49import android.util.TypedValue;
50import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070051import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070052import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070053import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070054import android.view.Menu;
55import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070056import android.view.View;
Michael Jurka4c6016f2011-05-17 18:21:03 -070057import android.view.View.MeasureSpec;
Adam Cohen120980b2010-12-08 11:05:37 -080058import android.view.animation.DecelerateInterpolator;
Patrick Dubroy5f445422011-02-18 14:35:21 -080059import android.view.animation.LinearInterpolator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080060import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070061import android.widget.ImageView;
62import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080063import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070064
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
Winson Chunge3193b92010-09-10 11:44:42 -0700111 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700113 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700114 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700115 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700116
Winson Chunge3193b92010-09-10 11:44:42 -0700117 private static final int sMinWidgetCellHSpan = 2;
118 private static final int sMaxWidgetCellHSpan = 4;
119
Michael Jurka3125d9d2010-09-27 11:30:20 -0700120 private int mChoiceModeTitleText;
121
Winson Chunge3193b92010-09-10 11:44:42 -0700122 // The scale factor for widget previews inside the widget drawer
123 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700124
125 private final Canvas mCanvas = new Canvas();
126 private final LayoutInflater mInflater;
127
Michael Jurka4c6016f2011-05-17 18:21:03 -0700128 private boolean mFirstMeasure = true;
129
Adam Cohen120980b2010-12-08 11:05:37 -0800130 private final float mTmpFloatPos[] = new float[2];
131 private final float ANIMATION_SCALE = 0.5f;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800132
133 // The duration of the translation animation that occurs during you drag and drop
134 private final int TRANSLATE_ANIM_DURATION = 400;
135
136 // The duration of the scale & alpha animation that occurs during drag and drop
137 private final int DROP_ANIM_DURATION = 200;
138
Adam Cohen120980b2010-12-08 11:05:37 -0800139 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800140
141 // The Bitmap used to generate the drag view
142 private Bitmap mDragBitmap;
143
144 private int[] mDragViewOrigin = new int[2];
Adam Cohen120980b2010-12-08 11:05:37 -0800145
Michael Jurka7ef959b2011-02-23 11:48:32 -0800146 private int mPageContentWidth;
147
Winson Chung80baf5a2010-08-09 16:03:15 -0700148 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700149 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700150 }
151
152 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700153 this(context, attrs, 0);
154 }
155
156 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
157 super(context, attrs, defStyle);
158
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700159 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800160 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
161 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800162 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700163 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
164 a.recycle();
165 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
166 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
167 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700168 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700171 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
172 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700174
Michael Jurka7426c422010-11-11 15:23:47 -0800175 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800176 setDragSlopeThreshold(
177 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800178
Michael Jurka7ef959b2011-02-23 11:48:32 -0800179 // Create a dummy page and set it up to find out the content width (used by our parent)
180 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
181 setupPage(layout);
182 mPageContentWidth = layout.getContentWidth();
Michael Jurka0413dfa2011-04-05 16:52:32 -0700183 mMinPageWidth = layout.getWidthBeforeFirstLayout();
Michael Jurka7ef959b2011-02-23 11:48:32 -0800184
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
197 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
198 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
199
200 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
201 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
202
203 if (mFirstMeasure) {
204 mFirstMeasure = false;
205
206 // TODO: actually calculate mCellCountX/mCellCountY as some function of
207 // widthSize and heightSize
208 //mCellCountX = ?
209 //mCellCountY = ?
210
211 // Since mCellCountX/mCellCountY changed, we need to update the pages
212 invalidatePageData();
213
214 // Create a dummy page and set it up to find out the content width (used by our parent)
215 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
216 setupPage(layout);
217 mPageContentWidth = layout.getContentWidth();
218 }
219 }
220
Winson Chung80baf5a2010-08-09 16:03:15 -0700221 public void setLauncher(Launcher launcher) {
222 Context context = getContext();
223 mLauncher = launcher;
224 mPackageManager = context.getPackageManager();
225 }
226
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700227 /**
228 * Sets the list of applications that launcher has loaded.
229 */
230 public void setApps(ArrayList<ApplicationInfo> list) {
231 mApps = list;
232 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700233
234 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800235 mPageViewIconCache.retainAllApps(list);
236 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700237 }
238
239 /**
240 * Convenience function to add new items to the set of applications that were previously loaded.
241 * Called by both updateApps() and setApps().
242 */
243 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
244 // we add it in place, in alphabetical order
245 final int count = list.size();
246 for (int i = 0; i < count; ++i) {
247 final ApplicationInfo info = list.get(i);
248 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
249 if (index < 0) {
250 mApps.add(-(index + 1), info);
251 }
252 }
253 }
254
255 /**
256 * Adds new applications to the loaded list, and notifies the paged view to update itself.
257 */
258 public void addApps(ArrayList<ApplicationInfo> list) {
259 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700260
261 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800262 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700263 }
264
265 /**
266 * Convenience function to remove items to the set of applications that were previously loaded.
267 * Called by both updateApps() and removeApps().
268 */
269 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
270 // loop through all the apps and remove apps that have the same component
271 final int length = list.size();
272 for (int i = 0; i < length; ++i) {
273 final ApplicationInfo info = list.get(i);
274 int removeIndex = findAppByComponent(mApps, info);
275 if (removeIndex > -1) {
276 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800277 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700278 }
279 }
280 }
281
282 /**
283 * Removes applications from the loaded list, and notifies the paged view to update itself.
284 */
285 public void removeApps(ArrayList<ApplicationInfo> list) {
286 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700287
288 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800289 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700290 }
291
292 /**
293 * Updates a set of applications from the loaded list, and notifies the paged view to update
294 * itself.
295 */
296 public void updateApps(ArrayList<ApplicationInfo> list) {
297 // We remove and re-add the updated applications list because it's properties may have
298 // changed (ie. the title), and this will ensure that the items will be in their proper
299 // place in the list.
300 removeAppsWithoutInvalidate(list);
301 addAppsWithoutInvalidate(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 * Convenience function to find matching ApplicationInfos for removal.
309 */
310 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
311 ComponentName removeComponent = item.intent.getComponent();
312 final int length = list.size();
313 for (int i = 0; i < length; ++i) {
314 ApplicationInfo info = list.get(i);
315 if (info.intent.getComponent().equals(removeComponent)) {
316 return i;
317 }
318 }
319 return -1;
320 }
321
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 // get the list of widgets
324 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
325 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
326 @Override
327 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
328 return object1.label.compareTo(object2.label);
329 }
330 });
331
Winson Chung4dbea792011-05-05 14:21:32 -0700332 LauncherModel.ShortcutNameComparator resolveInfoComparator =
333 new LauncherModel.ShortcutNameComparator(mPackageManager);
Winson Chung80baf5a2010-08-09 16:03:15 -0700334
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 // get the list of shortcuts
336 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
337 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
338 Collections.sort(mShortcutList, resolveInfoComparator);
339
Winson Chunge8878e32010-09-15 20:37:09 -0700340 // get the list of wallpapers
341 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800342 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
343 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700344 Collections.sort(mWallpaperList, resolveInfoComparator);
345
Winson Chung04998342011-01-05 13:54:43 -0800346 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
347 retainShortcutList.addAll(mWallpaperList);
348 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
349 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700350 invalidatePageData();
351 }
352
353 public void setDragController(DragController dragger) {
354 mDragController = dragger;
355 }
356
357 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800358 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800359 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800360 if (getChildCount() > 0) {
361 setCurrentPage(0);
362 updateCurrentPageScroll();
363 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700364
Winson Chunga12a2502010-12-20 14:41:35 -0800365 // End the current choice mode so that we don't carry selections across tabs
366 endChoiceMode();
367 }
368 }
369
370 public CustomizationType getCustomizationFilter() {
371 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700372 }
373
Patrick Dubroy5f445422011-02-18 14:35:21 -0800374 /**
375 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
Patrick Dubroy5f445422011-02-18 14:35:21 -0800376 */
377 private void resetCheckedItem(boolean animated) {
Patrick Dubroy6f133422011-02-24 12:16:12 -0800378 final Checkable checkable = getSingleCheckedGrandchild();
379 if (checkable != null) {
380 if (checkable instanceof PagedViewWidget) {
381 ((PagedViewWidget) checkable).setChecked(false, animated);
382 } else {
383 ((PagedViewIcon) checkable).setChecked(false, animated);
384 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800385 }
386 }
387
388 public void onDropCompleted(View target, Object dragInfo, boolean success) {
389 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
390
391 // Create a view, identical to the drag view, that is only used for animating the
392 // item onto the home screen (or back to its original position, if the drop failed).
Patrick Dubroy6f133422011-02-24 12:16:12 -0800393 final int[] pos = mDragController.getDragView().getPosition(null);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800394 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
395 animView.setVisibility(View.VISIBLE);
396
397 if (success) {
398 resetCheckedItem(true);
399 animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0);
400 } else {
401 // Animate the icon/widget back to its original position
402 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
403 public void run() {
404 resetCheckedItem(false);
405 dragLayer.removeView(animView);
406 }
407 });
408 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800409 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800410 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800411 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700412 }
413
414 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800415 public void onDragViewVisible() {
416 }
417
Patrick Dubroy5f445422011-02-18 14:35:21 -0800418 /**
419 * Animates the given item onto the center of a home screen, and then scales the item to
420 * look as though it's disappearing onto that screen.
421 */
Adam Cohen120980b2010-12-08 11:05:37 -0800422 private void animateItemOntoScreen(View dragView,
423 final CellLayout layout, final ItemInfo info) {
424 mTmpFloatPos[0] = layout.getWidth() / 2;
425 mTmpFloatPos[1] = layout.getHeight() / 2;
426 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
427
Adam Cohen120980b2010-12-08 11:05:37 -0800428 int dragViewWidth = dragView.getMeasuredWidth();
429 int dragViewHeight = dragView.getMeasuredHeight();
430 float heightOffset = 0;
431 float widthOffset = 0;
432
433 if (dragView instanceof ImageView) {
434 Drawable d = ((ImageView) dragView).getDrawable();
435 int width = d.getIntrinsicWidth();
436 int height = d.getIntrinsicHeight();
437
438 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
439 float f = (dragViewWidth / (width * 1.0f));
440 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
441 } else {
442 float f = (dragViewHeight / (height * 1.0f));
443 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
444 }
445 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800446 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
447 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800448
Patrick Dubroy5f445422011-02-18 14:35:21 -0800449 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
450 final View dragCopy = dragLayer.createDragView(dragView);
451 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800452
Patrick Dubroy5f445422011-02-18 14:35:21 -0800453 // Translate the item to the center of the appropriate home screen
454 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800455
Patrick Dubroy5f445422011-02-18 14:35:21 -0800456 // The drop-onto-screen animation begins a bit later, but ends at the same time.
457 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
458
459 // Scale down the icon and fade out the alpha
460 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
461 }
Adam Cohen120980b2010-12-08 11:05:37 -0800462
Patrick Dubroy5f445422011-02-18 14:35:21 -0800463 /**
464 * Animation which scales the view down and animates its alpha, making it appear to disappear
465 * onto a home screen.
466 */
467 private void animateDropOntoScreen(
468 final View view, final ItemInfo info, int duration, int delay) {
469 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
470 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
471
472 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800473 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
474 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
475 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800476 anim.setInterpolator(new LinearInterpolator());
477 if (delay > 0) {
478 anim.setStartDelay(delay);
479 }
480 anim.setDuration(duration);
481 anim.addListener(new AnimatorListenerAdapter() {
482 public void onAnimationEnd(Animator animation) {
483 dragLayer.removeView(view);
484 mLauncher.addExternalItemToScreen(info, layout);
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800485 info.dropPos = null;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800486 }
487 });
488 anim.start();
489 }
Adam Cohen120980b2010-12-08 11:05:37 -0800490
Patrick Dubroy5f445422011-02-18 14:35:21 -0800491 /**
492 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
493 */
494 private void animateIntoPosition(
495 View view, float toX, float toY, final Runnable endRunnable) {
496 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
497 PropertyValuesHolder.ofFloat("x", toX),
498 PropertyValuesHolder.ofFloat("y", toY));
499 anim.setInterpolator(mQuintEaseOutInterpolator);
500 anim.setDuration(TRANSLATE_ANIM_DURATION);
501 if (endRunnable != null) {
502 anim.addListener(new AnimatorListenerAdapter() {
503 @Override
504 public void onAnimationEnd(Animator animation) {
505 endRunnable.run();
506 }
507 });
508 }
509 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800510 }
511
Patrick Dubroya669d792010-11-23 14:40:33 -0800512 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800513 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800514 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800515 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700516
Winson Chungd0d43012010-09-26 17:26:45 -0700517 // On certain pages, we allow single tap to mark items as selected so that they can be
518 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700519 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700520 switch (mCustomizationType) {
521 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700522 mChoiceModeTitleText = R.string.cab_widget_selection_text;
523 enterChoiceMode = true;
524 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700525 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700526 mChoiceModeTitleText = R.string.cab_app_selection_text;
527 enterChoiceMode = true;
528 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700529 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700530 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
531 enterChoiceMode = true;
532 break;
533 default:
534 break;
535 }
Winson Chungd0d43012010-09-26 17:26:45 -0700536
Michael Jurka3125d9d2010-09-27 11:30:20 -0700537 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700538 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700539
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700540 Workspace w = mLauncher.getWorkspace();
541 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
542 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800543 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700544
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700545 animateClickFeedback(v, new Runnable() {
546 @Override
547 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800548 cl.calculateSpans(itemInfo);
549 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
550 animateItemOntoScreen(dragView, cl, itemInfo);
551 } else {
552 mLauncher.showOutOfSpaceMessage();
553 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700554 }
555 });
Winson Chungd0d43012010-09-26 17:26:45 -0700556 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700557 }
558
559 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700560 switch (mCustomizationType) {
561 case WallpaperCustomization:
562 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700563 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700564 animateClickFeedback(v, new Runnable() {
565 @Override
566 public void run() {
567 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700568 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700569 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
570 ComponentName name = new ComponentName(info.activityInfo.packageName,
571 info.activityInfo.name);
572 createWallpapersIntent.setComponent(name);
573 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700574 }
575 });
Winson Chungd0d43012010-09-26 17:26:45 -0700576 break;
577 default:
578 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700579 }
580 }
581
Winson Chung1908d072011-02-24 18:09:44 -0800582 private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800583 final Rect bounds = d.getBounds();
584 final int w = bounds.width();
585 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800586 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800587 renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800588 return b;
589 }
590
Adam Cohen120980b2010-12-08 11:05:37 -0800591 private View getDragView(View v) {
592 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
593 v.findViewById(R.id.widget_preview) : v;
594 }
595
Michael Jurka72b079e2010-12-10 01:03:53 -0800596 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800597 if (!v.isInTouchMode()) return false;
598 if (!super.beginDragging(v)) return false;
599
Winson Chungd0d43012010-09-26 17:26:45 -0700600 // End the current choice mode before we start dragging anything
601 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
602 endChoiceMode();
603 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800604 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800605 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800606 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700607 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800608 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800609 if (v instanceof PagedViewWidget) {
610 // Get the widget preview as the drag representation
611 final LinearLayout l = (LinearLayout) v;
612 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800613
614 // Calculate how much to scale the drag preview
615 RectF tmpScaleRect = new RectF(0,0,1,1);
616 i.getImageMatrix().mapRect(tmpScaleRect);
617
618 mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
619 tmpScaleRect.bottom);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800620 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800621 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700622
Patrick Dubroy5f445422011-02-18 14:35:21 -0800623 int[] spanXY = CellLayout.rectToCell(getResources(),
624 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800625 createWidgetInfo.spanX = spanXY[0];
626 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800627 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
628 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
629 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800630 result = true;
631 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800632 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800633 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800634 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800635 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800636 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800637 // get icon (top compound drawable, index is 1)
638 final TextView tv = (TextView) v;
639 final Drawable icon = tv.getCompoundDrawables()[1];
Winson Chung1908d072011-02-24 18:09:44 -0800640 mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700641
Patrick Dubroy5f445422011-02-18 14:35:21 -0800642 Object dragInfo = v.getTag();
643 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
644 // TODO: Not sure why we have to copy this
645 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
646 }
647 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
648
649 // Calculate where to place the drag view in order to align the icon pixels with
650 // the original view.
651 v.getLocationOnScreen(mDragViewOrigin);
652 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
653 mDragViewOrigin[1] += v.getPaddingTop();
654
655 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
656 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800657 result = true;
658 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800659 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700660 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800661 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800662
663 // We toggle the checked state _after_ we create the view for the drag in case toggling the
664 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800665 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800666 // In preparation for drag, we always reset the checked grand children regardless of
667 // what choice mode we are in
668 resetCheckedGrandchildren();
669
670 // Toggle the selection on the dragged app
671 Checkable checkable = (Checkable) v;
672
673 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
674 // of the drag of the item. (The fade-in will occur when all checked states are
675 // disabled when dragging ends)
676 checkable.toggle();
677 }
678
679 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700680 }
681
Winson Chunge3193b92010-09-10 11:44:42 -0700682 /**
683 * Pre-processes the layout of the different widget pages.
684 * @return the number of pages of widgets that we have
685 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700687 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700688
Winson Chunge3193b92010-09-10 11:44:42 -0700689 // create a new page for the first set of widgets
690 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700691 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700692 mWidgetPages.add(newPage);
693
694 // do this until we have no more widgets to lay out
695 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
696 final int widgetCount = mWidgetList.size();
697 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700698 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700699 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700700
Winson Chunge3193b92010-09-10 11:44:42 -0700701 // determine the size of the current widget
702 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
703 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700704
Winson Chunge3193b92010-09-10 11:44:42 -0700705 // create a new page if necessary
706 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
707 numCellsInRow = 0;
708 newPage = new ArrayList<AppWidgetProviderInfo>();
709 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 }
711
Winson Chunge3193b92010-09-10 11:44:42 -0700712 // add the item to the current page
713 newPage.add(info);
714 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700715 }
Winson Chunge3193b92010-09-10 11:44:42 -0700716
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 return mWidgetPages.size();
718 }
719
Winson Chunge3193b92010-09-10 11:44:42 -0700720 /**
Winson Chung7da10252010-10-28 16:07:04 -0700721 * Helper function to draw a drawable to the specified canvas with the specified bounds.
722 */
Winson Chung1908d072011-02-24 18:09:44 -0800723 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
724 float scaleX, float scaleY) {
Winson Chung7da10252010-10-28 16:07:04 -0700725 if (bitmap != null) mCanvas.setBitmap(bitmap);
726 mCanvas.save();
Winson Chung1908d072011-02-24 18:09:44 -0800727 mCanvas.scale(scaleX, scaleY);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800728 final Rect oldBounds = d.copyBounds();
729 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700730 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800731 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700732 mCanvas.restore();
733 }
734
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800735 /*
736 * This method fetches an xml file specified in the manifest identified by
737 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
738 * an image which will be used as the wallpaper preview for an activity
739 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
740 * in the customize drawer.
741 */
742 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800743 ActivityInfo activityInfo = ri.activityInfo;
744 XmlResourceParser parser = null;
745 try {
746 parser = activityInfo.loadXmlMetaData(mPackageManager,
747 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
748 if (parser == null) {
749 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
750 + "wallpaper provider '" + component + '\'');
751 return null;
752 }
753
754 AttributeSet attrs = Xml.asAttributeSet(parser);
755
756 int type;
757 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
758 && type != XmlPullParser.START_TAG) {
759 // drain whitespace, comments, etc.
760 }
761
762 String nodeName = parser.getName();
763 if (!"wallpaper-preview".equals(nodeName)) {
764 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
765 + "wallpaper provider '" + component + '\'');
766 return null;
767 }
768
769 // If metaData was null, we would have returned earlier when getting
770 // the parser No need to do the check here
771 Resources res = mPackageManager.getResourcesForApplication(
772 activityInfo.applicationInfo);
773
774 TypedArray sa = res.obtainAttributes(attrs,
775 com.android.internal.R.styleable.WallpaperPreviewInfo);
776
777 TypedValue value = sa.peekValue(
778 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
779 if (value == null) return null;
780
781 return res.getDrawable(value.resourceId);
782 } catch (Exception e) {
783 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
784 return null;
785 } finally {
786 if (parser != null) parser.close();
787 }
788 }
789
Winson Chung7da10252010-10-28 16:07:04 -0700790 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800791 * This method will extract the preview image specified by the wallpaper source provider (if it
792 * exists) otherwise, it will try to generate a default image preview.
793 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800794 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800795 // To be implemented later: resolving the up-to-date wallpaper thumbnail
796
797 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
798 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
799 Resources resources = mLauncher.getResources();
800
801 // Create a new bitmap to hold the widget preview
802 int width = (int) (dim * sScaleFactor);
803 int height = (int) (dim * sScaleFactor);
804 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800805
806 Drawable background = parseWallpaperPreviewXml(
807 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
808 boolean foundCustomDrawable = background != null;
809
810 if (!foundCustomDrawable) {
811 background = resources.getDrawable(R.drawable.default_widget_preview);
812 }
813
Winson Chung1908d072011-02-24 18:09:44 -0800814 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800815
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800816 // If we don't have a custom icon, we use the app icon on the default background
817 if (!foundCustomDrawable) {
818 try {
819 final IconCache iconCache =
820 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
821 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
822 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800823
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800824 final int iconSize = minDim / 2;
825 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800826 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800827 } catch (Resources.NotFoundException e) {
828 // if we can't find the icon, then just don't draw it
829 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800830 }
831
Winson Chung29d6fea2010-12-01 15:47:31 -0800832 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800833 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
834 return drawable;
835 }
836
837 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700838 * This method will extract the preview image specified by the widget developer (if it exists),
839 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800840 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700841 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800842 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800843 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700844 String packageName = info.provider.getPackageName();
845 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800846 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700847 if (info.previewImage != 0) {
848 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
849 if (drawable == null) {
850 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
851 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700852 }
853 }
854
855 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700856 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
857 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700858 if (drawable == null) {
859 Resources resources = mLauncher.getResources();
860
Winson Chung80baf5a2010-08-09 16:03:15 -0700861 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700862 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
863 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700864 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
865 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800866 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700867
Winson Chung45e1d6e2010-11-09 17:19:49 -0800868 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700869 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700871 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700872 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700873 }
874 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700875 icon = resources.getDrawable(R.drawable.ic_launcher_application);
876 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700877
Winson Chunge3193b92010-09-10 11:44:42 -0700878 final int iconSize = minDim / 2;
879 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800880 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700881 } catch (Resources.NotFoundException e) {
882 // if we can't find the icon, then just don't draw it
883 }
884
Winson Chung29d6fea2010-12-01 15:47:31 -0800885 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700886 } else {
887 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800888 final float imageWidth = drawable.getIntrinsicWidth();
889 final float imageHeight = drawable.getIntrinsicHeight();
890 final float aspect = (float) imageWidth / imageHeight;
891 final int scaledWidth =
892 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
893 final int scaledHeight =
894 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700895 int width;
896 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800897 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700898 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800899 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700900 } else {
901 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800902 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700903 }
904
905 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800906 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung7da10252010-10-28 16:07:04 -0700907
Winson Chung29d6fea2010-12-01 15:47:31 -0800908 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700909 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800910 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
911 newDrawable.getIntrinsicHeight());
912 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700913 }
914
915 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700916 layout.setCellCount(mCellCountX, mCellCountY);
917 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
918 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700919 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700920 }
921
Winson Chunge3193b92010-09-10 11:44:42 -0700922 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700923 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700924 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
925
926 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
927 }
928
Winson Chung80baf5a2010-08-09 16:03:15 -0700929 private void syncWidgetPages() {
930 if (mWidgetList == null) return;
931
Winson Chunge3193b92010-09-10 11:44:42 -0700932 // we need to repopulate with the LinearLayout layout for the widget pages
933 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700934 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700935 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800936 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700937 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700938 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
939 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700940
Winson Chunge22a8e92010-11-12 13:40:58 -0800941 addView(layout, new LinearLayout.LayoutParams(
942 LinearLayout.LayoutParams.WRAP_CONTENT,
943 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700944 }
945 }
946
947 private void syncWidgetPageItems(int page) {
948 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700949 LinearLayout layout = (LinearLayout) getChildAt(page);
950 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700951 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800952 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700953 layout.removeAllViews();
954 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700955 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
956 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800957 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
958 info.minHeight, null);
959 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -0700960 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -0700961
Winson Chung29d6fea2010-12-01 15:47:31 -0800962 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700963 R.layout.customize_paged_view_widget, layout, false);
Winson Chung1908d072011-02-24 18:09:44 -0800964
Winson Chung04998342011-01-05 13:54:43 -0800965 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
Winson Chung63257c12011-05-05 17:06:13 -0700966 mPageViewIconCache, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -0700967 l.setTag(createItemInfo);
968 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800969 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700970 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700971
Winson Chunge3193b92010-09-10 11:44:42 -0700972 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700973 }
974 }
975
Winson Chung45e1d6e2010-11-09 17:19:49 -0800976 private void syncWallpaperPages() {
977 if (mWallpaperList == null) return;
978
979 // We need to repopulate the LinearLayout for the wallpaper pages
980 removeAllViews();
981 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800982 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800983 for (int i = 0; i < numPages; ++i) {
984 LinearLayout layout = new PagedViewExtendedLayout(getContext());
985 layout.setGravity(Gravity.CENTER_HORIZONTAL);
986 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
987 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
988
Winson Chunge22a8e92010-11-12 13:40:58 -0800989 addView(layout, new LinearLayout.LayoutParams(
990 LinearLayout.LayoutParams.WRAP_CONTENT,
991 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800992 }
993 }
994
995 private void syncWallpaperPageItems(int page) {
996 // Load the items on to the pages
997 LinearLayout layout = (LinearLayout) getChildAt(page);
998 layout.removeAllViews();
999 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -08001000 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -08001001 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -08001002 final int startIndex = page * numItemsPerPage;
1003 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
1004 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001005 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -08001006 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -07001007 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001008
Winson Chung29d6fea2010-12-01 15:47:31 -08001009 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -08001010 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001011 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
Winson Chung63257c12011-05-05 17:06:13 -07001012 mPageViewIconCache, createHolographicOutlines);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001013 l.setTag(info);
1014 l.setOnClickListener(this);
1015
Winson Chung45e1d6e2010-11-09 17:19:49 -08001016 layout.addView(l);
1017 }
1018 }
1019
Winson Chung80baf5a2010-08-09 16:03:15 -07001020 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -07001021 // we need to repopulate with PagedViewCellLayouts
1022 removeAllViews();
1023
Winson Chung80baf5a2010-08-09 16:03:15 -07001024 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001025 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -07001026 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001027 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1028 setupPage(layout);
1029 addView(layout);
1030 }
1031 }
1032
1033 private void syncListPageItems(int page, List<ResolveInfo> list) {
1034 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001035 final int numPages = getPageCount();
1036 final int numCells = mCellCountX * mCellCountY;
1037 final int startIndex = page * numCells;
1038 final int endIndex = Math.min(startIndex + numCells, list.size());
1039 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001040 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001041 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -07001042 for (int i = startIndex; i < endIndex; ++i) {
1043 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -07001044 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
Winson Chungdd259022011-05-10 15:59:42 -07001045 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -07001046
Winson Chung241c3b42010-08-25 16:53:03 -07001047 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1048 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001049 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -08001050 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
Winson Chung63257c12011-05-05 17:06:13 -07001051 createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001052 switch (mCustomizationType) {
1053 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001054 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001055 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001056 case ShortcutCustomization:
1057 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1058 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1059 info.activityInfo.name);
1060 icon.setTag(createItemInfo);
1061 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001062 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001063 icon.setOnLongClickListener(this);
1064 break;
1065 default:
1066 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001067 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001068
1069 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001070 final int x = index % mCellCountX;
1071 final int y = index / mCellCountX;
1072 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001073 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001074 }
1075 }
1076
1077 private void syncAppPages() {
1078 if (mApps == null) return;
1079
1080 // We need to repopulate with PagedViewCellLayouts
1081 removeAllViews();
1082
1083 // Ensure that we have the right number of pages
1084 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1085 for (int i = 0; i < numPages; ++i) {
1086 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1087 setupPage(layout);
1088 addView(layout);
1089 }
1090 }
1091
1092 private void syncAppPageItems(int page) {
1093 if (mApps == null) return;
1094
1095 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001096 final int numPages = getPageCount();
1097 final int numCells = mCellCountX * mCellCountY;
1098 final int startIndex = page * numCells;
1099 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1100 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001101 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001102 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001103 for (int i = startIndex; i < endIndex; ++i) {
1104 final ApplicationInfo info = mApps.get(i);
Winson Chungdd259022011-05-10 15:59:42 -07001105 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001106 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1107 R.layout.all_apps_paged_view_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -07001108 icon.applyFromApplicationInfo(
Winson Chung63257c12011-05-05 17:06:13 -07001109 info, mPageViewIconCache, true, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001110 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001111 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001112 icon.setOnLongClickListener(this);
1113
1114 final int index = i - startIndex;
1115 final int x = index % mCellCountX;
1116 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001117 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001118 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001119 }
1120 }
1121
Winson Chung80baf5a2010-08-09 16:03:15 -07001122 @Override
1123 public void syncPages() {
Michael Jurka4c6016f2011-05-17 18:21:03 -07001124 if (mFirstMeasure) {
1125 // We don't know our size yet, which means we haven't calculated cell count x/y;
1126 // onMeasure will call us once we figure out our size
1127 return;
1128 }
Winson Chung1908d072011-02-24 18:09:44 -08001129 boolean enforceMinimumPagedWidths = false;
Winson Chunge3193b92010-09-10 11:44:42 -07001130 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001131 switch (mCustomizationType) {
1132 case WidgetCustomization:
1133 syncWidgetPages();
Winson Chung1908d072011-02-24 18:09:44 -08001134 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001135 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001136 case ShortcutCustomization:
1137 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001138 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001139 break;
1140 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001141 syncWallpaperPages();
Winson Chung1908d072011-02-24 18:09:44 -08001142 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001143 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001144 case ApplicationCustomization:
1145 syncAppPages();
1146 centerPagedViewCellLayouts = false;
1147 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001148 default:
1149 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001150 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001151 break;
1152 }
1153
1154 // only try and center the page if there is one page
1155 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001156 if (centerPagedViewCellLayouts) {
1157 if (childCount == 1) {
1158 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1159 layout.enableCenteredContent(true);
1160 } else {
1161 for (int i = 0; i < childCount; ++i) {
1162 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1163 layout.enableCenteredContent(false);
1164 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001165 }
1166 }
1167
Winson Chung1908d072011-02-24 18:09:44 -08001168 // Set a min page width for PagedView layout if we have more than a single page
Winson Chung34b23d52011-03-18 11:29:34 -07001169 if (enforceMinimumPagedWidths && childCount > 1) {
1170 setMinimumWidthOverride(mMinPageWidth);
1171 } else {
1172 resetMinimumWidthOverride();
Winson Chung1908d072011-02-24 18:09:44 -08001173 }
1174
1175 // Bound the current page index
Winson Chung03929772011-02-23 17:07:10 -08001176 requestLayout();
1177 post(new Runnable() {
1178 @Override
1179 public void run() {
1180 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1181 forceUpdateAdjacentPagesAlpha();
1182 }
1183 });
Winson Chung80baf5a2010-08-09 16:03:15 -07001184 }
1185
1186 @Override
1187 public void syncPageItems(int page) {
1188 switch (mCustomizationType) {
1189 case WidgetCustomization:
1190 syncWidgetPageItems(page);
1191 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001192 case ShortcutCustomization:
1193 syncListPageItems(page, mShortcutList);
1194 break;
1195 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001196 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001197 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001198 case ApplicationCustomization:
1199 syncAppPageItems(page);
1200 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001201 }
1202 }
Winson Chunge3193b92010-09-10 11:44:42 -07001203
Michael Jurka7ef959b2011-02-23 11:48:32 -08001204 int getPageContentWidth() {
1205 return mPageContentWidth;
1206 }
1207
Winson Chungd0d43012010-09-26 17:26:45 -07001208 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001209 protected int getAssociatedLowerPageBound(int page) {
1210 return 0;
1211 }
Winson Chungd0d43012010-09-26 17:26:45 -07001212 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001213 protected int getAssociatedUpperPageBound(int page) {
1214 return getChildCount();
1215 }
Winson Chungd0d43012010-09-26 17:26:45 -07001216
1217 @Override
1218 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001219 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001220 return true;
1221 }
1222
1223 @Override
1224 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1225 return true;
1226 }
1227
1228 @Override
1229 public void onDestroyActionMode(ActionMode mode) {
1230 endChoiceMode();
1231 }
1232
1233 @Override
1234 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1235 return false;
1236 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001237}