blob: 5ac261f100b6b3a9a6639aafd6b2802292a14578 [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
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700111 // The maximum number of rows in a paged view.
112 private int mMaxCellCountY;
113
Winson Chunge3193b92010-09-10 11:44:42 -0700114 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700115 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700116 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700117 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700118 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700119
Winson Chunge3193b92010-09-10 11:44:42 -0700120 private static final int sMinWidgetCellHSpan = 2;
121 private static final int sMaxWidgetCellHSpan = 4;
122
Michael Jurka3125d9d2010-09-27 11:30:20 -0700123 private int mChoiceModeTitleText;
124
Winson Chunge3193b92010-09-10 11:44:42 -0700125 // The scale factor for widget previews inside the widget drawer
126 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700127
128 private final Canvas mCanvas = new Canvas();
129 private final LayoutInflater mInflater;
130
Michael Jurka4c6016f2011-05-17 18:21:03 -0700131 private boolean mFirstMeasure = true;
132
Adam Cohen120980b2010-12-08 11:05:37 -0800133 private final float mTmpFloatPos[] = new float[2];
134 private final float ANIMATION_SCALE = 0.5f;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800135
136 // The duration of the translation animation that occurs during you drag and drop
137 private final int TRANSLATE_ANIM_DURATION = 400;
138
139 // The duration of the scale & alpha animation that occurs during drag and drop
140 private final int DROP_ANIM_DURATION = 200;
141
Adam Cohen120980b2010-12-08 11:05:37 -0800142 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800143
144 // The Bitmap used to generate the drag view
145 private Bitmap mDragBitmap;
146
147 private int[] mDragViewOrigin = new int[2];
Adam Cohen120980b2010-12-08 11:05:37 -0800148
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700149 private int mPageContentWidth = -1;
150 private int mPageContentHeight = -1;
151
152 private AllAppsPagedView mAllAppsPagedView;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800153
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700155 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 }
157
158 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700159 this(context, attrs, 0);
160 }
161
162 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
163 super(context, attrs, defStyle);
164
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700165 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800166 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
167 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800168 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700169 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
170 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800171
Winson Chung80baf5a2010-08-09 16:03:15 -0700172 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700173 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
174 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700176
Michael Jurka7426c422010-11-11 15:23:47 -0800177 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800178 setDragSlopeThreshold(
179 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800180
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700181 mMaxCellCountY = r.getInteger(R.integer.customization_drawer_contents_maxCellCountY);
182
Winson Chung80baf5a2010-08-09 16:03:15 -0700183 setVisibility(View.GONE);
184 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700185 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700186 }
187
Winson Chung7da10252010-10-28 16:07:04 -0700188 @Override
189 protected void init() {
190 super.init();
191 mCenterPagesVertically = false;
192 }
193
Michael Jurka4c6016f2011-05-17 18:21:03 -0700194 @Override
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700195 protected void onMeasure(int widthSpec, int heightSpec) {
196 // Base the size of this control on the size of the All Apps view.
197 final int cellCountX = mAllAppsPagedView.getCellCountX();
198 final int cellCountY = Math.min(mAllAppsPagedView.getCellCountY(), mMaxCellCountY);
Michael Jurka4c6016f2011-05-17 18:21:03 -0700199
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700200 if (cellCountX != mCellCountX || cellCountY != mCellCountY) {
201 mCellCountX = cellCountX;
202 mCellCountY = cellCountY;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700203
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700204 // Create a dummy page and set it up to determine our size.
205 // The size is based on the app shortcuts tab having the same dimensions as All Apps.
Michael Jurka4c6016f2011-05-17 18:21:03 -0700206 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
207 setupPage(layout);
208 mPageContentWidth = layout.getContentWidth();
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700209 mPageContentHeight = layout.getContentHeight();
Michael Jurka7bcda142011-05-18 15:31:55 -0700210 mMinPageWidth = layout.getWidthBeforeFirstLayout();
Michael Jurka4c6016f2011-05-17 18:21:03 -0700211 }
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700212 if (mPageContentHeight > 0) {
213 // Lock our height to the size of the page content
214 final int h = mPageContentHeight + mPageLayoutPaddingTop + mPageLayoutPaddingBottom;
215 heightSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
216 }
217 super.onMeasure(widthSpec, heightSpec);
218 mFirstMeasure = false;
219 }
220
221 @Override
222 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
223 if (mFirstLayout) {
224 invalidatePageData();
225
226 // invalidatePageData() is what causes the child pages to be created. We need the
227 // children to be measured before layout, so force a new measure here.
228 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
229 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
230 }
231 super.onLayout(changed, left, top, right, bottom);
232 mFirstLayout = false;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700233 }
234
Winson Chung80baf5a2010-08-09 16:03:15 -0700235 public void setLauncher(Launcher launcher) {
236 Context context = getContext();
237 mLauncher = launcher;
238 mPackageManager = context.getPackageManager();
239 }
240
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700241 public void setAllAppsPagedView(AllAppsPagedView view) {
242 mAllAppsPagedView = view;
243 }
244
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700245 /**
246 * Sets the list of applications that launcher has loaded.
247 */
248 public void setApps(ArrayList<ApplicationInfo> list) {
249 mApps = list;
250 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700251
252 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800253 mPageViewIconCache.retainAllApps(list);
254 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700255 }
256
257 /**
258 * Convenience function to add new items to the set of applications that were previously loaded.
259 * Called by both updateApps() and setApps().
260 */
261 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
262 // we add it in place, in alphabetical order
263 final int count = list.size();
264 for (int i = 0; i < count; ++i) {
265 final ApplicationInfo info = list.get(i);
266 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
267 if (index < 0) {
268 mApps.add(-(index + 1), info);
269 }
270 }
271 }
272
273 /**
274 * Adds new applications to the loaded list, and notifies the paged view to update itself.
275 */
276 public void addApps(ArrayList<ApplicationInfo> list) {
277 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700278
279 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800280 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700281 }
282
283 /**
284 * Convenience function to remove items to the set of applications that were previously loaded.
285 * Called by both updateApps() and removeApps().
286 */
287 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
288 // loop through all the apps and remove apps that have the same component
289 final int length = list.size();
290 for (int i = 0; i < length; ++i) {
291 final ApplicationInfo info = list.get(i);
292 int removeIndex = findAppByComponent(mApps, info);
293 if (removeIndex > -1) {
294 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800295 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700296 }
297 }
298 }
299
300 /**
301 * Removes applications from the loaded list, and notifies the paged view to update itself.
302 */
303 public void removeApps(ArrayList<ApplicationInfo> list) {
304 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700305
306 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800307 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700308 }
309
310 /**
311 * Updates a set of applications from the loaded list, and notifies the paged view to update
312 * itself.
313 */
314 public void updateApps(ArrayList<ApplicationInfo> list) {
315 // We remove and re-add the updated applications list because it's properties may have
316 // changed (ie. the title), and this will ensure that the items will be in their proper
317 // place in the list.
318 removeAppsWithoutInvalidate(list);
319 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700320
321 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800322 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700323 }
324
325 /**
326 * Convenience function to find matching ApplicationInfos for removal.
327 */
328 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
329 ComponentName removeComponent = item.intent.getComponent();
330 final int length = list.size();
331 for (int i = 0; i < length; ++i) {
332 ApplicationInfo info = list.get(i);
333 if (info.intent.getComponent().equals(removeComponent)) {
334 return i;
335 }
336 }
337 return -1;
338 }
339
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 // get the list of widgets
342 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
343 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
344 @Override
345 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
346 return object1.label.compareTo(object2.label);
347 }
348 });
349
Winson Chung4dbea792011-05-05 14:21:32 -0700350 LauncherModel.ShortcutNameComparator resolveInfoComparator =
351 new LauncherModel.ShortcutNameComparator(mPackageManager);
Winson Chung80baf5a2010-08-09 16:03:15 -0700352
Winson Chung80baf5a2010-08-09 16:03:15 -0700353 // get the list of shortcuts
354 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
355 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
356 Collections.sort(mShortcutList, resolveInfoComparator);
357
Winson Chunge8878e32010-09-15 20:37:09 -0700358 // get the list of wallpapers
359 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800360 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
361 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700362 Collections.sort(mWallpaperList, resolveInfoComparator);
363
Winson Chung04998342011-01-05 13:54:43 -0800364 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
365 retainShortcutList.addAll(mWallpaperList);
366 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
367 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700368 invalidatePageData();
369 }
370
371 public void setDragController(DragController dragger) {
372 mDragController = dragger;
373 }
374
375 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800376 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800377 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800378 if (getChildCount() > 0) {
379 setCurrentPage(0);
380 updateCurrentPageScroll();
381 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700382
Winson Chunga12a2502010-12-20 14:41:35 -0800383 // End the current choice mode so that we don't carry selections across tabs
384 endChoiceMode();
385 }
386 }
387
388 public CustomizationType getCustomizationFilter() {
389 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700390 }
391
Patrick Dubroy5f445422011-02-18 14:35:21 -0800392 /**
393 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
Patrick Dubroy5f445422011-02-18 14:35:21 -0800394 */
395 private void resetCheckedItem(boolean animated) {
Patrick Dubroy6f133422011-02-24 12:16:12 -0800396 final Checkable checkable = getSingleCheckedGrandchild();
397 if (checkable != null) {
398 if (checkable instanceof PagedViewWidget) {
399 ((PagedViewWidget) checkable).setChecked(false, animated);
400 } else {
401 ((PagedViewIcon) checkable).setChecked(false, animated);
402 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800403 }
404 }
405
406 public void onDropCompleted(View target, Object dragInfo, boolean success) {
407 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
408
409 // Create a view, identical to the drag view, that is only used for animating the
410 // item onto the home screen (or back to its original position, if the drop failed).
Patrick Dubroy6f133422011-02-24 12:16:12 -0800411 final int[] pos = mDragController.getDragView().getPosition(null);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800412 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
413 animView.setVisibility(View.VISIBLE);
414
415 if (success) {
416 resetCheckedItem(true);
417 animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0);
418 } else {
419 // Animate the icon/widget back to its original position
420 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
421 public void run() {
422 resetCheckedItem(false);
423 dragLayer.removeView(animView);
424 }
425 });
426 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800427 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800428 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800429 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700430 }
431
432 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800433 public void onDragViewVisible() {
434 }
435
Patrick Dubroy5f445422011-02-18 14:35:21 -0800436 /**
437 * Animates the given item onto the center of a home screen, and then scales the item to
438 * look as though it's disappearing onto that screen.
439 */
Adam Cohen120980b2010-12-08 11:05:37 -0800440 private void animateItemOntoScreen(View dragView,
441 final CellLayout layout, final ItemInfo info) {
442 mTmpFloatPos[0] = layout.getWidth() / 2;
443 mTmpFloatPos[1] = layout.getHeight() / 2;
444 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
445
Adam Cohen120980b2010-12-08 11:05:37 -0800446 int dragViewWidth = dragView.getMeasuredWidth();
447 int dragViewHeight = dragView.getMeasuredHeight();
448 float heightOffset = 0;
449 float widthOffset = 0;
450
451 if (dragView instanceof ImageView) {
452 Drawable d = ((ImageView) dragView).getDrawable();
453 int width = d.getIntrinsicWidth();
454 int height = d.getIntrinsicHeight();
455
456 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
457 float f = (dragViewWidth / (width * 1.0f));
458 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
459 } else {
460 float f = (dragViewHeight / (height * 1.0f));
461 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
462 }
463 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800464 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
465 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800466
Patrick Dubroy5f445422011-02-18 14:35:21 -0800467 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
468 final View dragCopy = dragLayer.createDragView(dragView);
469 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800470
Patrick Dubroy5f445422011-02-18 14:35:21 -0800471 // Translate the item to the center of the appropriate home screen
472 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800473
Patrick Dubroy5f445422011-02-18 14:35:21 -0800474 // The drop-onto-screen animation begins a bit later, but ends at the same time.
475 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
476
477 // Scale down the icon and fade out the alpha
478 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
479 }
Adam Cohen120980b2010-12-08 11:05:37 -0800480
Patrick Dubroy5f445422011-02-18 14:35:21 -0800481 /**
482 * Animation which scales the view down and animates its alpha, making it appear to disappear
483 * onto a home screen.
484 */
485 private void animateDropOntoScreen(
486 final View view, final ItemInfo info, int duration, int delay) {
487 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
488 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
489
490 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800491 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
492 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
493 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800494 anim.setInterpolator(new LinearInterpolator());
495 if (delay > 0) {
496 anim.setStartDelay(delay);
497 }
498 anim.setDuration(duration);
499 anim.addListener(new AnimatorListenerAdapter() {
500 public void onAnimationEnd(Animator animation) {
501 dragLayer.removeView(view);
502 mLauncher.addExternalItemToScreen(info, layout);
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800503 info.dropPos = null;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800504 }
505 });
506 anim.start();
507 }
Adam Cohen120980b2010-12-08 11:05:37 -0800508
Patrick Dubroy5f445422011-02-18 14:35:21 -0800509 /**
510 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
511 */
512 private void animateIntoPosition(
513 View view, float toX, float toY, final Runnable endRunnable) {
514 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
515 PropertyValuesHolder.ofFloat("x", toX),
516 PropertyValuesHolder.ofFloat("y", toY));
517 anim.setInterpolator(mQuintEaseOutInterpolator);
518 anim.setDuration(TRANSLATE_ANIM_DURATION);
519 if (endRunnable != null) {
520 anim.addListener(new AnimatorListenerAdapter() {
521 @Override
522 public void onAnimationEnd(Animator animation) {
523 endRunnable.run();
524 }
525 });
526 }
527 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800528 }
529
Patrick Dubroya669d792010-11-23 14:40:33 -0800530 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800531 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800532 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800533 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700534
Winson Chungd0d43012010-09-26 17:26:45 -0700535 // On certain pages, we allow single tap to mark items as selected so that they can be
536 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700537 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700538 switch (mCustomizationType) {
539 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700540 mChoiceModeTitleText = R.string.cab_widget_selection_text;
541 enterChoiceMode = true;
542 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700543 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700544 mChoiceModeTitleText = R.string.cab_app_selection_text;
545 enterChoiceMode = true;
546 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700547 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700548 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
549 enterChoiceMode = true;
550 break;
551 default:
552 break;
553 }
Winson Chungd0d43012010-09-26 17:26:45 -0700554
Michael Jurka3125d9d2010-09-27 11:30:20 -0700555 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700556 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700557
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700558 Workspace w = mLauncher.getWorkspace();
559 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
560 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800561 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700562
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700563 animateClickFeedback(v, new Runnable() {
564 @Override
565 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800566 cl.calculateSpans(itemInfo);
567 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
568 animateItemOntoScreen(dragView, cl, itemInfo);
569 } else {
570 mLauncher.showOutOfSpaceMessage();
571 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700572 }
573 });
Winson Chungd0d43012010-09-26 17:26:45 -0700574 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700575 }
576
577 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700578 switch (mCustomizationType) {
579 case WallpaperCustomization:
580 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700581 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700582 animateClickFeedback(v, new Runnable() {
583 @Override
584 public void run() {
585 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700586 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700587 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
588 ComponentName name = new ComponentName(info.activityInfo.packageName,
589 info.activityInfo.name);
590 createWallpapersIntent.setComponent(name);
591 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700592 }
593 });
Winson Chungd0d43012010-09-26 17:26:45 -0700594 break;
595 default:
596 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700597 }
598 }
599
Winson Chung1908d072011-02-24 18:09:44 -0800600 private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800601 final Rect bounds = d.getBounds();
602 final int w = bounds.width();
603 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800604 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800605 renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800606 return b;
607 }
608
Adam Cohen120980b2010-12-08 11:05:37 -0800609 private View getDragView(View v) {
610 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
611 v.findViewById(R.id.widget_preview) : v;
612 }
613
Michael Jurka72b079e2010-12-10 01:03:53 -0800614 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800615 if (!v.isInTouchMode()) return false;
616 if (!super.beginDragging(v)) return false;
617
Winson Chungd0d43012010-09-26 17:26:45 -0700618 // End the current choice mode before we start dragging anything
619 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
620 endChoiceMode();
621 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800622 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800623 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800624 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700625 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800626 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800627 if (v instanceof PagedViewWidget) {
628 // Get the widget preview as the drag representation
629 final LinearLayout l = (LinearLayout) v;
630 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800631
632 // Calculate how much to scale the drag preview
633 RectF tmpScaleRect = new RectF(0,0,1,1);
634 i.getImageMatrix().mapRect(tmpScaleRect);
635
636 mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
637 tmpScaleRect.bottom);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800638 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800639 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700640
Patrick Dubroy5f445422011-02-18 14:35:21 -0800641 int[] spanXY = CellLayout.rectToCell(getResources(),
642 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800643 createWidgetInfo.spanX = spanXY[0];
644 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800645 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
646 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
647 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800648 result = true;
649 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800650 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800651 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800652 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800653 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800654 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800655 // get icon (top compound drawable, index is 1)
656 final TextView tv = (TextView) v;
657 final Drawable icon = tv.getCompoundDrawables()[1];
Winson Chung1908d072011-02-24 18:09:44 -0800658 mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700659
Patrick Dubroy5f445422011-02-18 14:35:21 -0800660 Object dragInfo = v.getTag();
661 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
662 // TODO: Not sure why we have to copy this
663 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
664 }
665 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
666
667 // Calculate where to place the drag view in order to align the icon pixels with
668 // the original view.
669 v.getLocationOnScreen(mDragViewOrigin);
670 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
671 mDragViewOrigin[1] += v.getPaddingTop();
672
673 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
674 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800675 result = true;
676 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800677 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700678 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800679 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800680
681 // We toggle the checked state _after_ we create the view for the drag in case toggling the
682 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800683 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800684 // In preparation for drag, we always reset the checked grand children regardless of
685 // what choice mode we are in
686 resetCheckedGrandchildren();
687
688 // Toggle the selection on the dragged app
689 Checkable checkable = (Checkable) v;
690
691 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
692 // of the drag of the item. (The fade-in will occur when all checked states are
693 // disabled when dragging ends)
694 checkable.toggle();
695 }
696
697 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700698 }
699
Winson Chunge3193b92010-09-10 11:44:42 -0700700 /**
701 * Pre-processes the layout of the different widget pages.
702 * @return the number of pages of widgets that we have
703 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700705 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700706
Winson Chunge3193b92010-09-10 11:44:42 -0700707 // create a new page for the first set of widgets
708 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700709 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700710 mWidgetPages.add(newPage);
711
712 // do this until we have no more widgets to lay out
713 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
714 final int widgetCount = mWidgetList.size();
715 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700716 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700717 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700718
Winson Chunge3193b92010-09-10 11:44:42 -0700719 // determine the size of the current widget
720 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
721 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700722
Winson Chunge3193b92010-09-10 11:44:42 -0700723 // create a new page if necessary
724 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
725 numCellsInRow = 0;
726 newPage = new ArrayList<AppWidgetProviderInfo>();
727 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700728 }
729
Winson Chunge3193b92010-09-10 11:44:42 -0700730 // add the item to the current page
731 newPage.add(info);
732 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700733 }
Winson Chunge3193b92010-09-10 11:44:42 -0700734
Winson Chung80baf5a2010-08-09 16:03:15 -0700735 return mWidgetPages.size();
736 }
737
Winson Chunge3193b92010-09-10 11:44:42 -0700738 /**
Winson Chung7da10252010-10-28 16:07:04 -0700739 * Helper function to draw a drawable to the specified canvas with the specified bounds.
740 */
Winson Chung1908d072011-02-24 18:09:44 -0800741 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
742 float scaleX, float scaleY) {
Winson Chung7da10252010-10-28 16:07:04 -0700743 if (bitmap != null) mCanvas.setBitmap(bitmap);
744 mCanvas.save();
Winson Chung1908d072011-02-24 18:09:44 -0800745 mCanvas.scale(scaleX, scaleY);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800746 final Rect oldBounds = d.copyBounds();
747 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700748 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800749 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700750 mCanvas.restore();
751 }
752
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800753 /*
754 * This method fetches an xml file specified in the manifest identified by
755 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
756 * an image which will be used as the wallpaper preview for an activity
757 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
758 * in the customize drawer.
759 */
760 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800761 ActivityInfo activityInfo = ri.activityInfo;
762 XmlResourceParser parser = null;
763 try {
764 parser = activityInfo.loadXmlMetaData(mPackageManager,
765 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
766 if (parser == null) {
767 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
768 + "wallpaper provider '" + component + '\'');
769 return null;
770 }
771
772 AttributeSet attrs = Xml.asAttributeSet(parser);
773
774 int type;
775 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
776 && type != XmlPullParser.START_TAG) {
777 // drain whitespace, comments, etc.
778 }
779
780 String nodeName = parser.getName();
781 if (!"wallpaper-preview".equals(nodeName)) {
782 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
783 + "wallpaper provider '" + component + '\'');
784 return null;
785 }
786
787 // If metaData was null, we would have returned earlier when getting
788 // the parser No need to do the check here
789 Resources res = mPackageManager.getResourcesForApplication(
790 activityInfo.applicationInfo);
791
792 TypedArray sa = res.obtainAttributes(attrs,
793 com.android.internal.R.styleable.WallpaperPreviewInfo);
794
795 TypedValue value = sa.peekValue(
796 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
797 if (value == null) return null;
798
799 return res.getDrawable(value.resourceId);
800 } catch (Exception e) {
801 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
802 return null;
803 } finally {
804 if (parser != null) parser.close();
805 }
806 }
807
Winson Chung7da10252010-10-28 16:07:04 -0700808 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800809 * This method will extract the preview image specified by the wallpaper source provider (if it
810 * exists) otherwise, it will try to generate a default image preview.
811 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800812 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800813 // To be implemented later: resolving the up-to-date wallpaper thumbnail
814
815 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
816 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
817 Resources resources = mLauncher.getResources();
818
819 // Create a new bitmap to hold the widget preview
820 int width = (int) (dim * sScaleFactor);
821 int height = (int) (dim * sScaleFactor);
822 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800823
824 Drawable background = parseWallpaperPreviewXml(
825 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
826 boolean foundCustomDrawable = background != null;
827
828 if (!foundCustomDrawable) {
829 background = resources.getDrawable(R.drawable.default_widget_preview);
830 }
831
Winson Chung1908d072011-02-24 18:09:44 -0800832 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800833
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800834 // If we don't have a custom icon, we use the app icon on the default background
835 if (!foundCustomDrawable) {
836 try {
837 final IconCache iconCache =
838 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
839 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
840 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800841
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800842 final int iconSize = minDim / 2;
843 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800844 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800845 } catch (Resources.NotFoundException e) {
846 // if we can't find the icon, then just don't draw it
847 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800848 }
849
Winson Chung29d6fea2010-12-01 15:47:31 -0800850 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800851 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
852 return drawable;
853 }
854
855 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700856 * This method will extract the preview image specified by the widget developer (if it exists),
857 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800858 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700859 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800860 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800861 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700862 String packageName = info.provider.getPackageName();
863 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800864 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700865 if (info.previewImage != 0) {
866 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
867 if (drawable == null) {
868 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
869 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 }
871 }
872
873 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700874 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
875 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700876 if (drawable == null) {
877 Resources resources = mLauncher.getResources();
878
Winson Chung80baf5a2010-08-09 16:03:15 -0700879 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700880 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
881 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700882 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
883 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800884 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700885
Winson Chung45e1d6e2010-11-09 17:19:49 -0800886 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700887 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700888 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700889 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700890 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700891 }
892 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700893 icon = resources.getDrawable(R.drawable.ic_launcher_application);
894 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700895
Winson Chunge3193b92010-09-10 11:44:42 -0700896 final int iconSize = minDim / 2;
897 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800898 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700899 } catch (Resources.NotFoundException e) {
900 // if we can't find the icon, then just don't draw it
901 }
902
Winson Chung29d6fea2010-12-01 15:47:31 -0800903 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700904 } else {
905 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800906 final float imageWidth = drawable.getIntrinsicWidth();
907 final float imageHeight = drawable.getIntrinsicHeight();
908 final float aspect = (float) imageWidth / imageHeight;
909 final int scaledWidth =
910 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
911 final int scaledHeight =
912 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700913 int width;
914 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800915 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700916 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800917 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700918 } else {
919 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800920 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700921 }
922
923 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800924 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung7da10252010-10-28 16:07:04 -0700925
Winson Chung29d6fea2010-12-01 15:47:31 -0800926 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700927 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800928 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
929 newDrawable.getIntrinsicHeight());
930 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700931 }
932
933 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700934 layout.setCellCount(mCellCountX, mCellCountY);
935 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
936 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700937 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700938 }
939
Winson Chunge3193b92010-09-10 11:44:42 -0700940 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700941 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700942 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
943
944 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
945 }
946
Winson Chung80baf5a2010-08-09 16:03:15 -0700947 private void syncWidgetPages() {
948 if (mWidgetList == null) return;
949
Winson Chunge3193b92010-09-10 11:44:42 -0700950 // we need to repopulate with the LinearLayout layout for the widget pages
951 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700952 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700953 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800954 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700955 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700956 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
957 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700958
Winson Chunge22a8e92010-11-12 13:40:58 -0800959 addView(layout, new LinearLayout.LayoutParams(
960 LinearLayout.LayoutParams.WRAP_CONTENT,
961 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700962 }
963 }
964
965 private void syncWidgetPageItems(int page) {
966 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700967 LinearLayout layout = (LinearLayout) getChildAt(page);
968 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700969 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800970 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 layout.removeAllViews();
972 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700973 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
974 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800975 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
976 info.minHeight, null);
977 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -0700978 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -0700979
Winson Chung29d6fea2010-12-01 15:47:31 -0800980 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700981 R.layout.customize_paged_view_widget, layout, false);
Winson Chung1908d072011-02-24 18:09:44 -0800982
Winson Chung04998342011-01-05 13:54:43 -0800983 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
Winson Chung63257c12011-05-05 17:06:13 -0700984 mPageViewIconCache, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -0700985 l.setTag(createItemInfo);
986 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800987 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700988 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700989
Winson Chunge3193b92010-09-10 11:44:42 -0700990 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700991 }
992 }
993
Winson Chung45e1d6e2010-11-09 17:19:49 -0800994 private void syncWallpaperPages() {
995 if (mWallpaperList == null) return;
996
997 // We need to repopulate the LinearLayout for the wallpaper pages
998 removeAllViews();
999 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -08001000 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001001 for (int i = 0; i < numPages; ++i) {
1002 LinearLayout layout = new PagedViewExtendedLayout(getContext());
1003 layout.setGravity(Gravity.CENTER_HORIZONTAL);
1004 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1005 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1006
Winson Chunge22a8e92010-11-12 13:40:58 -08001007 addView(layout, new LinearLayout.LayoutParams(
1008 LinearLayout.LayoutParams.WRAP_CONTENT,
1009 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -08001010 }
1011 }
1012
1013 private void syncWallpaperPageItems(int page) {
1014 // Load the items on to the pages
1015 LinearLayout layout = (LinearLayout) getChildAt(page);
1016 layout.removeAllViews();
1017 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -08001018 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -08001019 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -08001020 final int startIndex = page * numItemsPerPage;
1021 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
1022 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001023 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -08001024 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chungdd259022011-05-10 15:59:42 -07001025 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001026
Winson Chung29d6fea2010-12-01 15:47:31 -08001027 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -08001028 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001029 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
Winson Chung63257c12011-05-05 17:06:13 -07001030 mPageViewIconCache, createHolographicOutlines);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001031 l.setTag(info);
1032 l.setOnClickListener(this);
1033
Winson Chung45e1d6e2010-11-09 17:19:49 -08001034 layout.addView(l);
1035 }
1036 }
1037
Winson Chung80baf5a2010-08-09 16:03:15 -07001038 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -07001039 // we need to repopulate with PagedViewCellLayouts
1040 removeAllViews();
1041
Winson Chung80baf5a2010-08-09 16:03:15 -07001042 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001043 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -07001044 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001045 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1046 setupPage(layout);
1047 addView(layout);
1048 }
1049 }
1050
1051 private void syncListPageItems(int page, List<ResolveInfo> list) {
1052 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001053 final int numPages = getPageCount();
1054 final int numCells = mCellCountX * mCellCountY;
1055 final int startIndex = page * numCells;
1056 final int endIndex = Math.min(startIndex + numCells, list.size());
1057 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001058 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001059 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -07001060 for (int i = startIndex; i < endIndex; ++i) {
1061 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -07001062 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
Winson Chungdd259022011-05-10 15:59:42 -07001063 final boolean createHolographicOutlines = (numPages > 1);
Winson Chungd0d43012010-09-26 17:26:45 -07001064
Winson Chung241c3b42010-08-25 16:53:03 -07001065 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1066 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001067 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -08001068 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
Winson Chung63257c12011-05-05 17:06:13 -07001069 createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001070 switch (mCustomizationType) {
1071 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001072 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001073 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001074 case ShortcutCustomization:
1075 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1076 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1077 info.activityInfo.name);
1078 icon.setTag(createItemInfo);
1079 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001080 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001081 icon.setOnLongClickListener(this);
1082 break;
1083 default:
1084 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001085 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001086
1087 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001088 final int x = index % mCellCountX;
1089 final int y = index / mCellCountX;
1090 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001091 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001092 }
1093 }
1094
1095 private void syncAppPages() {
1096 if (mApps == null) return;
1097
1098 // We need to repopulate with PagedViewCellLayouts
1099 removeAllViews();
1100
1101 // Ensure that we have the right number of pages
1102 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1103 for (int i = 0; i < numPages; ++i) {
1104 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1105 setupPage(layout);
1106 addView(layout);
1107 }
1108 }
1109
1110 private void syncAppPageItems(int page) {
1111 if (mApps == null) return;
1112
1113 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001114 final int numPages = getPageCount();
1115 final int numCells = mCellCountX * mCellCountY;
1116 final int startIndex = page * numCells;
1117 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1118 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001119 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001120 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001121 for (int i = startIndex; i < endIndex; ++i) {
1122 final ApplicationInfo info = mApps.get(i);
Winson Chungdd259022011-05-10 15:59:42 -07001123 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001124 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1125 R.layout.all_apps_paged_view_application, layout, false);
Michael Jurkab9b8ce92011-05-05 15:05:07 -07001126 icon.applyFromApplicationInfo(
Winson Chung63257c12011-05-05 17:06:13 -07001127 info, mPageViewIconCache, true, createHolographicOutlines);
Winson Chungd0d43012010-09-26 17:26:45 -07001128 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001129 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001130 icon.setOnLongClickListener(this);
1131
1132 final int index = i - startIndex;
1133 final int x = index % mCellCountX;
1134 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001135 setupPage(layout);
Winson Chung6a70e9f2011-05-17 16:24:49 -07001136 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001137 }
1138 }
1139
Winson Chung80baf5a2010-08-09 16:03:15 -07001140 @Override
1141 public void syncPages() {
Michael Jurka4c6016f2011-05-17 18:21:03 -07001142 if (mFirstMeasure) {
1143 // We don't know our size yet, which means we haven't calculated cell count x/y;
1144 // onMeasure will call us once we figure out our size
1145 return;
1146 }
Winson Chung1908d072011-02-24 18:09:44 -08001147 boolean enforceMinimumPagedWidths = false;
Winson Chunge3193b92010-09-10 11:44:42 -07001148 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001149 switch (mCustomizationType) {
1150 case WidgetCustomization:
1151 syncWidgetPages();
Winson Chung1908d072011-02-24 18:09:44 -08001152 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001153 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001154 case ShortcutCustomization:
1155 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001156 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001157 break;
1158 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001159 syncWallpaperPages();
Winson Chung1908d072011-02-24 18:09:44 -08001160 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001161 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001162 case ApplicationCustomization:
1163 syncAppPages();
1164 centerPagedViewCellLayouts = false;
1165 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001166 default:
1167 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001168 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001169 break;
1170 }
1171
1172 // only try and center the page if there is one page
1173 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001174 if (centerPagedViewCellLayouts) {
1175 if (childCount == 1) {
1176 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1177 layout.enableCenteredContent(true);
1178 } else {
1179 for (int i = 0; i < childCount; ++i) {
1180 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1181 layout.enableCenteredContent(false);
1182 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001183 }
1184 }
1185
Winson Chung1908d072011-02-24 18:09:44 -08001186 // Set a min page width for PagedView layout if we have more than a single page
Winson Chung34b23d52011-03-18 11:29:34 -07001187 if (enforceMinimumPagedWidths && childCount > 1) {
1188 setMinimumWidthOverride(mMinPageWidth);
1189 } else {
1190 resetMinimumWidthOverride();
Winson Chung1908d072011-02-24 18:09:44 -08001191 }
1192
1193 // Bound the current page index
Winson Chung03929772011-02-23 17:07:10 -08001194 requestLayout();
1195 post(new Runnable() {
1196 @Override
1197 public void run() {
1198 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1199 forceUpdateAdjacentPagesAlpha();
1200 }
1201 });
Winson Chung80baf5a2010-08-09 16:03:15 -07001202 }
1203
1204 @Override
1205 public void syncPageItems(int page) {
1206 switch (mCustomizationType) {
1207 case WidgetCustomization:
1208 syncWidgetPageItems(page);
1209 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001210 case ShortcutCustomization:
1211 syncListPageItems(page, mShortcutList);
1212 break;
1213 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001214 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001215 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001216 case ApplicationCustomization:
1217 syncAppPageItems(page);
1218 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001219 }
1220 }
Winson Chunge3193b92010-09-10 11:44:42 -07001221
Michael Jurka7ef959b2011-02-23 11:48:32 -08001222 int getPageContentWidth() {
1223 return mPageContentWidth;
1224 }
1225
Winson Chungd0d43012010-09-26 17:26:45 -07001226 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001227 protected int getAssociatedLowerPageBound(int page) {
1228 return 0;
1229 }
Winson Chungd0d43012010-09-26 17:26:45 -07001230 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001231 protected int getAssociatedUpperPageBound(int page) {
1232 return getChildCount();
1233 }
Winson Chungd0d43012010-09-26 17:26:45 -07001234
1235 @Override
1236 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001237 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001238 return true;
1239 }
1240
1241 @Override
1242 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1243 return true;
1244 }
1245
1246 @Override
1247 public void onDestroyActionMode(ActionMode mode) {
1248 endChoiceMode();
1249 }
1250
1251 @Override
1252 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1253 return false;
1254 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001255}