blob: 68ec535d8d3099726b62f4571decca90d1ae7599 [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
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 setVisibility(View.GONE);
180 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700181 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700182 }
183
Winson Chung7da10252010-10-28 16:07:04 -0700184 @Override
185 protected void init() {
186 super.init();
187 mCenterPagesVertically = false;
188 }
189
Michael Jurka4c6016f2011-05-17 18:21:03 -0700190 @Override
191 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
192 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
193
194 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
195 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
196
197 if (mFirstMeasure) {
198 mFirstMeasure = false;
199
200 // TODO: actually calculate mCellCountX/mCellCountY as some function of
201 // widthSize and heightSize
202 //mCellCountX = ?
203 //mCellCountY = ?
204
205 // Since mCellCountX/mCellCountY changed, we need to update the pages
206 invalidatePageData();
207
208 // Create a dummy page and set it up to find out the content width (used by our parent)
209 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
210 setupPage(layout);
211 mPageContentWidth = layout.getContentWidth();
Michael Jurka7bcda142011-05-18 15:31:55 -0700212 mMinPageWidth = layout.getWidthBeforeFirstLayout();
Michael Jurka4c6016f2011-05-17 18:21:03 -0700213 }
214 }
215
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 public void setLauncher(Launcher launcher) {
217 Context context = getContext();
218 mLauncher = launcher;
219 mPackageManager = context.getPackageManager();
220 }
221
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700222 /**
223 * Sets the list of applications that launcher has loaded.
224 */
225 public void setApps(ArrayList<ApplicationInfo> list) {
226 mApps = list;
227 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700228
229 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800230 mPageViewIconCache.retainAllApps(list);
231 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700232 }
233
234 /**
235 * Convenience function to add new items to the set of applications that were previously loaded.
236 * Called by both updateApps() and setApps().
237 */
238 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
239 // we add it in place, in alphabetical order
240 final int count = list.size();
241 for (int i = 0; i < count; ++i) {
242 final ApplicationInfo info = list.get(i);
243 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
244 if (index < 0) {
245 mApps.add(-(index + 1), info);
246 }
247 }
248 }
249
250 /**
251 * Adds new applications to the loaded list, and notifies the paged view to update itself.
252 */
253 public void addApps(ArrayList<ApplicationInfo> list) {
254 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700255
256 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800257 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700258 }
259
260 /**
261 * Convenience function to remove items to the set of applications that were previously loaded.
262 * Called by both updateApps() and removeApps().
263 */
264 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
265 // loop through all the apps and remove apps that have the same component
266 final int length = list.size();
267 for (int i = 0; i < length; ++i) {
268 final ApplicationInfo info = list.get(i);
269 int removeIndex = findAppByComponent(mApps, info);
270 if (removeIndex > -1) {
271 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800272 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700273 }
274 }
275 }
276
277 /**
278 * Removes applications from the loaded list, and notifies the paged view to update itself.
279 */
280 public void removeApps(ArrayList<ApplicationInfo> list) {
281 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700282
283 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800284 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700285 }
286
287 /**
288 * Updates a set of applications from the loaded list, and notifies the paged view to update
289 * itself.
290 */
291 public void updateApps(ArrayList<ApplicationInfo> list) {
292 // We remove and re-add the updated applications list because it's properties may have
293 // changed (ie. the title), and this will ensure that the items will be in their proper
294 // place in the list.
295 removeAppsWithoutInvalidate(list);
296 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700297
298 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800299 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700300 }
301
302 /**
303 * Convenience function to find matching ApplicationInfos for removal.
304 */
305 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
306 ComponentName removeComponent = item.intent.getComponent();
307 final int length = list.size();
308 for (int i = 0; i < length; ++i) {
309 ApplicationInfo info = list.get(i);
310 if (info.intent.getComponent().equals(removeComponent)) {
311 return i;
312 }
313 }
314 return -1;
315 }
316
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700318 // get the list of widgets
319 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
320 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
321 @Override
322 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
323 return object1.label.compareTo(object2.label);
324 }
325 });
326
Winson Chung4dbea792011-05-05 14:21:32 -0700327 LauncherModel.ShortcutNameComparator resolveInfoComparator =
328 new LauncherModel.ShortcutNameComparator(mPackageManager);
Winson Chung80baf5a2010-08-09 16:03:15 -0700329
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 // get the list of shortcuts
331 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
332 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
333 Collections.sort(mShortcutList, resolveInfoComparator);
334
Winson Chunge8878e32010-09-15 20:37:09 -0700335 // get the list of wallpapers
336 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800337 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
338 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700339 Collections.sort(mWallpaperList, resolveInfoComparator);
340
Winson Chung04998342011-01-05 13:54:43 -0800341 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
342 retainShortcutList.addAll(mWallpaperList);
343 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
344 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700345 invalidatePageData();
346 }
347
348 public void setDragController(DragController dragger) {
349 mDragController = dragger;
350 }
351
352 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800353 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800354 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800355 if (getChildCount() > 0) {
356 setCurrentPage(0);
357 updateCurrentPageScroll();
358 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700359
Winson Chunga12a2502010-12-20 14:41:35 -0800360 // End the current choice mode so that we don't carry selections across tabs
361 endChoiceMode();
362 }
363 }
364
365 public CustomizationType getCustomizationFilter() {
366 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700367 }
368
Patrick Dubroy5f445422011-02-18 14:35:21 -0800369 /**
370 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
Patrick Dubroy5f445422011-02-18 14:35:21 -0800371 */
372 private void resetCheckedItem(boolean animated) {
Patrick Dubroy6f133422011-02-24 12:16:12 -0800373 final Checkable checkable = getSingleCheckedGrandchild();
374 if (checkable != null) {
375 if (checkable instanceof PagedViewWidget) {
376 ((PagedViewWidget) checkable).setChecked(false, animated);
377 } else {
378 ((PagedViewIcon) checkable).setChecked(false, animated);
379 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800380 }
381 }
382
383 public void onDropCompleted(View target, Object dragInfo, boolean success) {
384 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
385
386 // Create a view, identical to the drag view, that is only used for animating the
387 // item onto the home screen (or back to its original position, if the drop failed).
Patrick Dubroy6f133422011-02-24 12:16:12 -0800388 final int[] pos = mDragController.getDragView().getPosition(null);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800389 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
390 animView.setVisibility(View.VISIBLE);
391
392 if (success) {
393 resetCheckedItem(true);
394 animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0);
395 } else {
396 // Animate the icon/widget back to its original position
397 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
398 public void run() {
399 resetCheckedItem(false);
400 dragLayer.removeView(animView);
401 }
402 });
403 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800404 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800405 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800406 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700407 }
408
409 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800410 public void onDragViewVisible() {
411 }
412
Patrick Dubroy5f445422011-02-18 14:35:21 -0800413 /**
414 * Animates the given item onto the center of a home screen, and then scales the item to
415 * look as though it's disappearing onto that screen.
416 */
Adam Cohen120980b2010-12-08 11:05:37 -0800417 private void animateItemOntoScreen(View dragView,
418 final CellLayout layout, final ItemInfo info) {
419 mTmpFloatPos[0] = layout.getWidth() / 2;
420 mTmpFloatPos[1] = layout.getHeight() / 2;
421 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
422
Adam Cohen120980b2010-12-08 11:05:37 -0800423 int dragViewWidth = dragView.getMeasuredWidth();
424 int dragViewHeight = dragView.getMeasuredHeight();
425 float heightOffset = 0;
426 float widthOffset = 0;
427
428 if (dragView instanceof ImageView) {
429 Drawable d = ((ImageView) dragView).getDrawable();
430 int width = d.getIntrinsicWidth();
431 int height = d.getIntrinsicHeight();
432
433 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
434 float f = (dragViewWidth / (width * 1.0f));
435 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
436 } else {
437 float f = (dragViewHeight / (height * 1.0f));
438 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
439 }
440 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800441 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
442 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800443
Patrick Dubroy5f445422011-02-18 14:35:21 -0800444 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
445 final View dragCopy = dragLayer.createDragView(dragView);
446 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800447
Patrick Dubroy5f445422011-02-18 14:35:21 -0800448 // Translate the item to the center of the appropriate home screen
449 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800450
Patrick Dubroy5f445422011-02-18 14:35:21 -0800451 // The drop-onto-screen animation begins a bit later, but ends at the same time.
452 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
453
454 // Scale down the icon and fade out the alpha
455 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
456 }
Adam Cohen120980b2010-12-08 11:05:37 -0800457
Patrick Dubroy5f445422011-02-18 14:35:21 -0800458 /**
459 * Animation which scales the view down and animates its alpha, making it appear to disappear
460 * onto a home screen.
461 */
462 private void animateDropOntoScreen(
463 final View view, final ItemInfo info, int duration, int delay) {
464 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
465 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
466
467 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800468 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
469 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
470 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800471 anim.setInterpolator(new LinearInterpolator());
472 if (delay > 0) {
473 anim.setStartDelay(delay);
474 }
475 anim.setDuration(duration);
476 anim.addListener(new AnimatorListenerAdapter() {
477 public void onAnimationEnd(Animator animation) {
478 dragLayer.removeView(view);
479 mLauncher.addExternalItemToScreen(info, layout);
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800480 info.dropPos = null;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800481 }
482 });
483 anim.start();
484 }
Adam Cohen120980b2010-12-08 11:05:37 -0800485
Patrick Dubroy5f445422011-02-18 14:35:21 -0800486 /**
487 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
488 */
489 private void animateIntoPosition(
490 View view, float toX, float toY, final Runnable endRunnable) {
491 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
492 PropertyValuesHolder.ofFloat("x", toX),
493 PropertyValuesHolder.ofFloat("y", toY));
494 anim.setInterpolator(mQuintEaseOutInterpolator);
495 anim.setDuration(TRANSLATE_ANIM_DURATION);
496 if (endRunnable != null) {
497 anim.addListener(new AnimatorListenerAdapter() {
498 @Override
499 public void onAnimationEnd(Animator animation) {
500 endRunnable.run();
501 }
502 });
503 }
504 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800505 }
506
Patrick Dubroya669d792010-11-23 14:40:33 -0800507 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800508 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800509 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800510 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700511
Winson Chungd0d43012010-09-26 17:26:45 -0700512 // On certain pages, we allow single tap to mark items as selected so that they can be
513 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700514 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700515 switch (mCustomizationType) {
516 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700517 mChoiceModeTitleText = R.string.cab_widget_selection_text;
518 enterChoiceMode = true;
519 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700520 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700521 mChoiceModeTitleText = R.string.cab_app_selection_text;
522 enterChoiceMode = true;
523 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700524 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700525 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
526 enterChoiceMode = true;
527 break;
528 default:
529 break;
530 }
Winson Chungd0d43012010-09-26 17:26:45 -0700531
Michael Jurka3125d9d2010-09-27 11:30:20 -0700532 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700533 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700534
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700535 Workspace w = mLauncher.getWorkspace();
536 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
537 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800538 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700539
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700540 animateClickFeedback(v, new Runnable() {
541 @Override
542 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800543 cl.calculateSpans(itemInfo);
544 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
545 animateItemOntoScreen(dragView, cl, itemInfo);
546 } else {
547 mLauncher.showOutOfSpaceMessage();
548 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700549 }
550 });
Winson Chungd0d43012010-09-26 17:26:45 -0700551 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700552 }
553
554 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700555 switch (mCustomizationType) {
556 case WallpaperCustomization:
557 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700558 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700559 animateClickFeedback(v, new Runnable() {
560 @Override
561 public void run() {
562 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700563 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700564 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
565 ComponentName name = new ComponentName(info.activityInfo.packageName,
566 info.activityInfo.name);
567 createWallpapersIntent.setComponent(name);
568 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700569 }
570 });
Winson Chungd0d43012010-09-26 17:26:45 -0700571 break;
572 default:
573 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700574 }
575 }
576
Winson Chung1908d072011-02-24 18:09:44 -0800577 private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800578 final Rect bounds = d.getBounds();
579 final int w = bounds.width();
580 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800581 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800582 renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800583 return b;
584 }
585
Adam Cohen120980b2010-12-08 11:05:37 -0800586 private View getDragView(View v) {
587 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
588 v.findViewById(R.id.widget_preview) : v;
589 }
590
Michael Jurka72b079e2010-12-10 01:03:53 -0800591 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800592 if (!v.isInTouchMode()) return false;
593 if (!super.beginDragging(v)) return false;
594
Winson Chungd0d43012010-09-26 17:26:45 -0700595 // End the current choice mode before we start dragging anything
596 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
597 endChoiceMode();
598 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800599 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800600 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800601 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800603 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800604 if (v instanceof PagedViewWidget) {
605 // Get the widget preview as the drag representation
606 final LinearLayout l = (LinearLayout) v;
607 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800608
609 // Calculate how much to scale the drag preview
610 RectF tmpScaleRect = new RectF(0,0,1,1);
611 i.getImageMatrix().mapRect(tmpScaleRect);
612
613 mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
614 tmpScaleRect.bottom);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800615 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800616 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700617
Patrick Dubroy5f445422011-02-18 14:35:21 -0800618 int[] spanXY = CellLayout.rectToCell(getResources(),
619 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800620 createWidgetInfo.spanX = spanXY[0];
621 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800622 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
623 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
624 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800625 result = true;
626 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800627 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800628 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800629 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800630 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800631 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800632 // get icon (top compound drawable, index is 1)
633 final TextView tv = (TextView) v;
634 final Drawable icon = tv.getCompoundDrawables()[1];
Winson Chung1908d072011-02-24 18:09:44 -0800635 mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700636
Patrick Dubroy5f445422011-02-18 14:35:21 -0800637 Object dragInfo = v.getTag();
638 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
639 // TODO: Not sure why we have to copy this
640 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
641 }
642 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
643
644 // Calculate where to place the drag view in order to align the icon pixels with
645 // the original view.
646 v.getLocationOnScreen(mDragViewOrigin);
647 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
648 mDragViewOrigin[1] += v.getPaddingTop();
649
650 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
651 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800652 result = true;
653 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800654 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700655 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800656 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800657
658 // We toggle the checked state _after_ we create the view for the drag in case toggling the
659 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800660 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800661 // In preparation for drag, we always reset the checked grand children regardless of
662 // what choice mode we are in
663 resetCheckedGrandchildren();
664
665 // Toggle the selection on the dragged app
666 Checkable checkable = (Checkable) v;
667
668 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
669 // of the drag of the item. (The fade-in will occur when all checked states are
670 // disabled when dragging ends)
671 checkable.toggle();
672 }
673
674 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700675 }
676
Winson Chunge3193b92010-09-10 11:44:42 -0700677 /**
678 * Pre-processes the layout of the different widget pages.
679 * @return the number of pages of widgets that we have
680 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700681 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700682 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700683
Winson Chunge3193b92010-09-10 11:44:42 -0700684 // create a new page for the first set of widgets
685 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700687 mWidgetPages.add(newPage);
688
689 // do this until we have no more widgets to lay out
690 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
691 final int widgetCount = mWidgetList.size();
692 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700693 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700694 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700695
Winson Chunge3193b92010-09-10 11:44:42 -0700696 // determine the size of the current widget
697 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
698 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700699
Winson Chunge3193b92010-09-10 11:44:42 -0700700 // create a new page if necessary
701 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
702 numCellsInRow = 0;
703 newPage = new ArrayList<AppWidgetProviderInfo>();
704 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700705 }
706
Winson Chunge3193b92010-09-10 11:44:42 -0700707 // add the item to the current page
708 newPage.add(info);
709 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 }
Winson Chunge3193b92010-09-10 11:44:42 -0700711
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 return mWidgetPages.size();
713 }
714
Winson Chunge3193b92010-09-10 11:44:42 -0700715 /**
Winson Chung7da10252010-10-28 16:07:04 -0700716 * Helper function to draw a drawable to the specified canvas with the specified bounds.
717 */
Winson Chung1908d072011-02-24 18:09:44 -0800718 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
719 float scaleX, float scaleY) {
Winson Chung7da10252010-10-28 16:07:04 -0700720 if (bitmap != null) mCanvas.setBitmap(bitmap);
721 mCanvas.save();
Winson Chung1908d072011-02-24 18:09:44 -0800722 mCanvas.scale(scaleX, scaleY);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800723 final Rect oldBounds = d.copyBounds();
724 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700725 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800726 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700727 mCanvas.restore();
728 }
729
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800730 /*
731 * This method fetches an xml file specified in the manifest identified by
732 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
733 * an image which will be used as the wallpaper preview for an activity
734 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
735 * in the customize drawer.
736 */
737 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800738 ActivityInfo activityInfo = ri.activityInfo;
739 XmlResourceParser parser = null;
740 try {
741 parser = activityInfo.loadXmlMetaData(mPackageManager,
742 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
743 if (parser == null) {
744 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
745 + "wallpaper provider '" + component + '\'');
746 return null;
747 }
748
749 AttributeSet attrs = Xml.asAttributeSet(parser);
750
751 int type;
752 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
753 && type != XmlPullParser.START_TAG) {
754 // drain whitespace, comments, etc.
755 }
756
757 String nodeName = parser.getName();
758 if (!"wallpaper-preview".equals(nodeName)) {
759 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
760 + "wallpaper provider '" + component + '\'');
761 return null;
762 }
763
764 // If metaData was null, we would have returned earlier when getting
765 // the parser No need to do the check here
766 Resources res = mPackageManager.getResourcesForApplication(
767 activityInfo.applicationInfo);
768
769 TypedArray sa = res.obtainAttributes(attrs,
770 com.android.internal.R.styleable.WallpaperPreviewInfo);
771
772 TypedValue value = sa.peekValue(
773 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
774 if (value == null) return null;
775
776 return res.getDrawable(value.resourceId);
777 } catch (Exception e) {
778 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
779 return null;
780 } finally {
781 if (parser != null) parser.close();
782 }
783 }
784
Winson Chung7da10252010-10-28 16:07:04 -0700785 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800786 * This method will extract the preview image specified by the wallpaper source provider (if it
787 * exists) otherwise, it will try to generate a default image preview.
788 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800789 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800790 // To be implemented later: resolving the up-to-date wallpaper thumbnail
791
792 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
793 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
794 Resources resources = mLauncher.getResources();
795
796 // Create a new bitmap to hold the widget preview
797 int width = (int) (dim * sScaleFactor);
798 int height = (int) (dim * sScaleFactor);
799 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800800
801 Drawable background = parseWallpaperPreviewXml(
802 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
803 boolean foundCustomDrawable = background != null;
804
805 if (!foundCustomDrawable) {
806 background = resources.getDrawable(R.drawable.default_widget_preview);
807 }
808
Winson Chung1908d072011-02-24 18:09:44 -0800809 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800810
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800811 // If we don't have a custom icon, we use the app icon on the default background
812 if (!foundCustomDrawable) {
813 try {
814 final IconCache iconCache =
815 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
816 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
817 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800818
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800819 final int iconSize = minDim / 2;
820 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800821 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800822 } catch (Resources.NotFoundException e) {
823 // if we can't find the icon, then just don't draw it
824 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800825 }
826
Winson Chung29d6fea2010-12-01 15:47:31 -0800827 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800828 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
829 return drawable;
830 }
831
832 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700833 * This method will extract the preview image specified by the widget developer (if it exists),
834 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800835 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700836 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800837 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800838 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700839 String packageName = info.provider.getPackageName();
840 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800841 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700842 if (info.previewImage != 0) {
843 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
844 if (drawable == null) {
845 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
846 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700847 }
848 }
849
850 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700851 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
852 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700853 if (drawable == null) {
854 Resources resources = mLauncher.getResources();
855
Winson Chung80baf5a2010-08-09 16:03:15 -0700856 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700857 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
858 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700859 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
860 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800861 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700862
Winson Chung45e1d6e2010-11-09 17:19:49 -0800863 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700864 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700865 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700866 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700867 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700868 }
869 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 icon = resources.getDrawable(R.drawable.ic_launcher_application);
871 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700872
Winson Chunge3193b92010-09-10 11:44:42 -0700873 final int iconSize = minDim / 2;
874 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800875 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700876 } catch (Resources.NotFoundException e) {
877 // if we can't find the icon, then just don't draw it
878 }
879
Winson Chung29d6fea2010-12-01 15:47:31 -0800880 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700881 } else {
882 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800883 final float imageWidth = drawable.getIntrinsicWidth();
884 final float imageHeight = drawable.getIntrinsicHeight();
885 final float aspect = (float) imageWidth / imageHeight;
886 final int scaledWidth =
887 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
888 final int scaledHeight =
889 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700890 int width;
891 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800892 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700893 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800894 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700895 } else {
896 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800897 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700898 }
899
900 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800901 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung7da10252010-10-28 16:07:04 -0700902
Winson Chung29d6fea2010-12-01 15:47:31 -0800903 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700904 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800905 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
906 newDrawable.getIntrinsicHeight());
907 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700908 }
909
910 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700911 layout.setCellCount(mCellCountX, mCellCountY);
912 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
913 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700914 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700915 }
916
Winson Chunge3193b92010-09-10 11:44:42 -0700917 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700918 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700919 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
920
921 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
922 }
923
Winson Chung80baf5a2010-08-09 16:03:15 -0700924 private void syncWidgetPages() {
925 if (mWidgetList == null) return;
926
Winson Chunge3193b92010-09-10 11:44:42 -0700927 // we need to repopulate with the LinearLayout layout for the widget pages
928 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700929 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700930 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800931 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700932 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700933 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
934 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700935
Winson Chunge22a8e92010-11-12 13:40:58 -0800936 addView(layout, new LinearLayout.LayoutParams(
937 LinearLayout.LayoutParams.WRAP_CONTENT,
938 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700939 }
940 }
941
942 private void syncWidgetPageItems(int page) {
943 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700944 LinearLayout layout = (LinearLayout) getChildAt(page);
945 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700946 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800947 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700948 layout.removeAllViews();
949 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700950 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
951 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800952 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
953 info.minHeight, null);
954 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -0700955 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -0700956
Winson Chung29d6fea2010-12-01 15:47:31 -0800957 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700958 R.layout.customize_paged_view_widget, layout, false);
Winson Chung1908d072011-02-24 18:09:44 -0800959
Winson Chung04998342011-01-05 13:54:43 -0800960 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
Winson Chung63257c12011-05-05 17:06:13 -0700961 mPageViewIconCache, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -0700962 l.setTag(createItemInfo);
963 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800964 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700965 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700966
Winson Chunge3193b92010-09-10 11:44:42 -0700967 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 }
969 }
970
Winson Chung45e1d6e2010-11-09 17:19:49 -0800971 private void syncWallpaperPages() {
972 if (mWallpaperList == null) return;
973
974 // We need to repopulate the LinearLayout for the wallpaper pages
975 removeAllViews();
976 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800977 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800978 for (int i = 0; i < numPages; ++i) {
979 LinearLayout layout = new PagedViewExtendedLayout(getContext());
980 layout.setGravity(Gravity.CENTER_HORIZONTAL);
981 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
982 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
983
Winson Chunge22a8e92010-11-12 13:40:58 -0800984 addView(layout, new LinearLayout.LayoutParams(
985 LinearLayout.LayoutParams.WRAP_CONTENT,
986 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800987 }
988 }
989
990 private void syncWallpaperPageItems(int page) {
991 // Load the items on to the pages
992 LinearLayout layout = (LinearLayout) getChildAt(page);
993 layout.removeAllViews();
994 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -0800995 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -0800996 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800997 final int startIndex = page * numItemsPerPage;
998 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
999 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001000 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -08001001 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -07001002 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001003
Winson Chung29d6fea2010-12-01 15:47:31 -08001004 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -08001005 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001006 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
Winson Chung63257c12011-05-05 17:06:13 -07001007 mPageViewIconCache, createHolographicOutlines);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001008 l.setTag(info);
1009 l.setOnClickListener(this);
1010
Winson Chung45e1d6e2010-11-09 17:19:49 -08001011 layout.addView(l);
1012 }
1013 }
1014
Winson Chung80baf5a2010-08-09 16:03:15 -07001015 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -07001016 // we need to repopulate with PagedViewCellLayouts
1017 removeAllViews();
1018
Winson Chung80baf5a2010-08-09 16:03:15 -07001019 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001020 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -07001021 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001022 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1023 setupPage(layout);
1024 addView(layout);
1025 }
1026 }
1027
1028 private void syncListPageItems(int page, List<ResolveInfo> list) {
1029 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001030 final int numPages = getPageCount();
1031 final int numCells = mCellCountX * mCellCountY;
1032 final int startIndex = page * numCells;
1033 final int endIndex = Math.min(startIndex + numCells, list.size());
1034 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001035 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001036 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -07001037 for (int i = startIndex; i < endIndex; ++i) {
1038 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -07001039 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
Winson Chungdd259022011-05-10 15:59:42 -07001040 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -07001041
Winson Chung241c3b42010-08-25 16:53:03 -07001042 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1043 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001044 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -08001045 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
Winson Chung63257c12011-05-05 17:06:13 -07001046 createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001047 switch (mCustomizationType) {
1048 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001049 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001050 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001051 case ShortcutCustomization:
1052 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1053 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1054 info.activityInfo.name);
1055 icon.setTag(createItemInfo);
1056 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001057 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001058 icon.setOnLongClickListener(this);
1059 break;
1060 default:
1061 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001062 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001063
1064 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001065 final int x = index % mCellCountX;
1066 final int y = index / mCellCountX;
1067 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001068 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001069 }
1070 }
1071
1072 private void syncAppPages() {
1073 if (mApps == null) return;
1074
1075 // We need to repopulate with PagedViewCellLayouts
1076 removeAllViews();
1077
1078 // Ensure that we have the right number of pages
1079 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1080 for (int i = 0; i < numPages; ++i) {
1081 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1082 setupPage(layout);
1083 addView(layout);
1084 }
1085 }
1086
1087 private void syncAppPageItems(int page) {
1088 if (mApps == null) return;
1089
1090 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001091 final int numPages = getPageCount();
1092 final int numCells = mCellCountX * mCellCountY;
1093 final int startIndex = page * numCells;
1094 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1095 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001096 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001097 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001098 for (int i = startIndex; i < endIndex; ++i) {
1099 final ApplicationInfo info = mApps.get(i);
Winson Chungdd259022011-05-10 15:59:42 -07001100 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001101 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1102 R.layout.all_apps_paged_view_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -07001103 icon.applyFromApplicationInfo(
Winson Chung63257c12011-05-05 17:06:13 -07001104 info, mPageViewIconCache, true, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001105 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001106 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001107 icon.setOnLongClickListener(this);
1108
1109 final int index = i - startIndex;
1110 final int x = index % mCellCountX;
1111 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001112 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001113 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001114 }
1115 }
1116
Winson Chung80baf5a2010-08-09 16:03:15 -07001117 @Override
1118 public void syncPages() {
Michael Jurka4c6016f2011-05-17 18:21:03 -07001119 if (mFirstMeasure) {
1120 // We don't know our size yet, which means we haven't calculated cell count x/y;
1121 // onMeasure will call us once we figure out our size
1122 return;
1123 }
Winson Chung1908d072011-02-24 18:09:44 -08001124 boolean enforceMinimumPagedWidths = false;
Winson Chunge3193b92010-09-10 11:44:42 -07001125 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001126 switch (mCustomizationType) {
1127 case WidgetCustomization:
1128 syncWidgetPages();
Winson Chung1908d072011-02-24 18:09:44 -08001129 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001130 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001131 case ShortcutCustomization:
1132 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001133 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001134 break;
1135 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001136 syncWallpaperPages();
Winson Chung1908d072011-02-24 18:09:44 -08001137 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001138 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001139 case ApplicationCustomization:
1140 syncAppPages();
1141 centerPagedViewCellLayouts = false;
1142 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001143 default:
1144 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001145 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001146 break;
1147 }
1148
1149 // only try and center the page if there is one page
1150 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001151 if (centerPagedViewCellLayouts) {
1152 if (childCount == 1) {
1153 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1154 layout.enableCenteredContent(true);
1155 } else {
1156 for (int i = 0; i < childCount; ++i) {
1157 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1158 layout.enableCenteredContent(false);
1159 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001160 }
1161 }
1162
Winson Chung1908d072011-02-24 18:09:44 -08001163 // Set a min page width for PagedView layout if we have more than a single page
Winson Chung34b23d52011-03-18 11:29:34 -07001164 if (enforceMinimumPagedWidths && childCount > 1) {
1165 setMinimumWidthOverride(mMinPageWidth);
1166 } else {
1167 resetMinimumWidthOverride();
Winson Chung1908d072011-02-24 18:09:44 -08001168 }
1169
1170 // Bound the current page index
Winson Chung03929772011-02-23 17:07:10 -08001171 requestLayout();
1172 post(new Runnable() {
1173 @Override
1174 public void run() {
1175 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1176 forceUpdateAdjacentPagesAlpha();
1177 }
1178 });
Winson Chung80baf5a2010-08-09 16:03:15 -07001179 }
1180
1181 @Override
1182 public void syncPageItems(int page) {
1183 switch (mCustomizationType) {
1184 case WidgetCustomization:
1185 syncWidgetPageItems(page);
1186 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001187 case ShortcutCustomization:
1188 syncListPageItems(page, mShortcutList);
1189 break;
1190 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001191 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001192 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001193 case ApplicationCustomization:
1194 syncAppPageItems(page);
1195 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001196 }
1197 }
Winson Chunge3193b92010-09-10 11:44:42 -07001198
Michael Jurka7ef959b2011-02-23 11:48:32 -08001199 int getPageContentWidth() {
1200 return mPageContentWidth;
1201 }
1202
Winson Chungd0d43012010-09-26 17:26:45 -07001203 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001204 protected int getAssociatedLowerPageBound(int page) {
1205 return 0;
1206 }
Winson Chungd0d43012010-09-26 17:26:45 -07001207 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001208 protected int getAssociatedUpperPageBound(int page) {
1209 return getChildCount();
1210 }
Winson Chungd0d43012010-09-26 17:26:45 -07001211
1212 @Override
1213 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001214 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001215 return true;
1216 }
1217
1218 @Override
1219 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1220 return true;
1221 }
1222
1223 @Override
1224 public void onDestroyActionMode(ActionMode mode) {
1225 endChoiceMode();
1226 }
1227
1228 @Override
1229 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1230 return false;
1231 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001232}