blob: d3779c45a90d3ef5b923d93908c7bd1807d45d26 [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 Jurkaea2daff2011-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 Dubroy1a009332011-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 Jurkaea2daff2011-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 Dubroy1a009332011-05-23 16:15:09 -0700149 private int mPageContentWidth = -1;
150 private int mPageContentHeight = -1;
151
152 private AllAppsPagedView mAllAppsPagedView;
Michael Jurka12ac0d62011-02-23 11:48:32 -0800153
Michael Jurka87b14902011-05-25 22:13:09 -0700154 private boolean mWaitingToInitPages = true;
155
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700157 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 }
159
160 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700161 this(context, attrs, 0);
162 }
163
164 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
165 super(context, attrs, defStyle);
166
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700167 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800168 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
169 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800170 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700171 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
172 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700175 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
176 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700178
Michael Jurka7426c422010-11-11 15:23:47 -0800179 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800180 setDragSlopeThreshold(
181 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800182
Patrick Dubroy1a009332011-05-23 16:15:09 -0700183 mMaxCellCountY = r.getInteger(R.integer.customization_drawer_contents_maxCellCountY);
184
Winson Chung80baf5a2010-08-09 16:03:15 -0700185 setVisibility(View.GONE);
186 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700187 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700188 }
189
Winson Chung7da10252010-10-28 16:07:04 -0700190 @Override
191 protected void init() {
192 super.init();
193 mCenterPagesVertically = false;
194 }
195
Michael Jurkaea2daff2011-05-17 18:21:03 -0700196 @Override
Patrick Dubroy1a009332011-05-23 16:15:09 -0700197 protected void onMeasure(int widthSpec, int heightSpec) {
198 // Base the size of this control on the size of the All Apps view.
199 final int cellCountX = mAllAppsPagedView.getCellCountX();
200 final int cellCountY = Math.min(mAllAppsPagedView.getCellCountY(), mMaxCellCountY);
Michael Jurkaea2daff2011-05-17 18:21:03 -0700201
Patrick Dubroy1a009332011-05-23 16:15:09 -0700202 if (cellCountX != mCellCountX || cellCountY != mCellCountY) {
203 mCellCountX = cellCountX;
204 mCellCountY = cellCountY;
Michael Jurkaea2daff2011-05-17 18:21:03 -0700205
Patrick Dubroy1a009332011-05-23 16:15:09 -0700206 // Create a dummy page and set it up to determine our size.
207 // The size is based on the app shortcuts tab having the same dimensions as All Apps.
Michael Jurkaea2daff2011-05-17 18:21:03 -0700208 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
209 setupPage(layout);
210 mPageContentWidth = layout.getContentWidth();
Patrick Dubroy1a009332011-05-23 16:15:09 -0700211 mPageContentHeight = layout.getContentHeight();
Michael Jurkaae785512011-05-18 15:31:55 -0700212 mMinPageWidth = layout.getWidthBeforeFirstLayout();
Michael Jurka983e3fd2011-05-26 17:10:29 -0700213 postInvalidatePageData(true);
Michael Jurkaea2daff2011-05-17 18:21:03 -0700214 }
Patrick Dubroy1a009332011-05-23 16:15:09 -0700215 if (mPageContentHeight > 0) {
216 // Lock our height to the size of the page content
217 final int h = mPageContentHeight + mPageLayoutPaddingTop + mPageLayoutPaddingBottom;
218 heightSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
219 }
220 super.onMeasure(widthSpec, heightSpec);
221 mFirstMeasure = false;
222 }
223
224 @Override
225 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurka87b14902011-05-25 22:13:09 -0700226 if (mWaitingToInitPages) {
227 mWaitingToInitPages = false;
Michael Jurka983e3fd2011-05-26 17:10:29 -0700228 postInvalidatePageData(false);
Patrick Dubroy1a009332011-05-23 16:15:09 -0700229 }
230 super.onLayout(changed, left, top, right, bottom);
231 mFirstLayout = false;
Michael Jurkaea2daff2011-05-17 18:21:03 -0700232 }
233
Winson Chung80baf5a2010-08-09 16:03:15 -0700234 public void setLauncher(Launcher launcher) {
235 Context context = getContext();
236 mLauncher = launcher;
237 mPackageManager = context.getPackageManager();
238 }
239
Patrick Dubroy1a009332011-05-23 16:15:09 -0700240 public void setAllAppsPagedView(AllAppsPagedView view) {
241 mAllAppsPagedView = view;
242 }
243
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700244 /**
245 * Sets the list of applications that launcher has loaded.
246 */
247 public void setApps(ArrayList<ApplicationInfo> list) {
248 mApps = list;
249 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700250
251 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800252 mPageViewIconCache.retainAllApps(list);
253 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700254 }
255
256 /**
257 * Convenience function to add new items to the set of applications that were previously loaded.
258 * Called by both updateApps() and setApps().
259 */
260 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
261 // we add it in place, in alphabetical order
262 final int count = list.size();
263 for (int i = 0; i < count; ++i) {
264 final ApplicationInfo info = list.get(i);
265 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
266 if (index < 0) {
267 mApps.add(-(index + 1), info);
268 }
269 }
270 }
271
272 /**
273 * Adds new applications to the loaded list, and notifies the paged view to update itself.
274 */
275 public void addApps(ArrayList<ApplicationInfo> list) {
276 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700277
278 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800279 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700280 }
281
282 /**
283 * Convenience function to remove items to the set of applications that were previously loaded.
284 * Called by both updateApps() and removeApps().
285 */
286 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
287 // loop through all the apps and remove apps that have the same component
288 final int length = list.size();
289 for (int i = 0; i < length; ++i) {
290 final ApplicationInfo info = list.get(i);
291 int removeIndex = findAppByComponent(mApps, info);
292 if (removeIndex > -1) {
293 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800294 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700295 }
296 }
297 }
298
299 /**
300 * Removes applications from the loaded list, and notifies the paged view to update itself.
301 */
302 public void removeApps(ArrayList<ApplicationInfo> list) {
303 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700304
305 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800306 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700307 }
308
309 /**
310 * Updates a set of applications from the loaded list, and notifies the paged view to update
311 * itself.
312 */
313 public void updateApps(ArrayList<ApplicationInfo> list) {
314 // We remove and re-add the updated applications list because it's properties may have
315 // changed (ie. the title), and this will ensure that the items will be in their proper
316 // place in the list.
317 removeAppsWithoutInvalidate(list);
318 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700319
320 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800321 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700322 }
323
324 /**
325 * Convenience function to find matching ApplicationInfos for removal.
326 */
327 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
328 ComponentName removeComponent = item.intent.getComponent();
329 final int length = list.size();
330 for (int i = 0; i < length; ++i) {
331 ApplicationInfo info = list.get(i);
332 if (info.intent.getComponent().equals(removeComponent)) {
333 return i;
334 }
335 }
336 return -1;
337 }
338
Winson Chung80baf5a2010-08-09 16:03:15 -0700339 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 // get the list of widgets
341 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
342 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
343 @Override
344 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
345 return object1.label.compareTo(object2.label);
346 }
347 });
348
349 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
350 @Override
351 public int compare(ResolveInfo object1, ResolveInfo object2) {
352 return object1.loadLabel(mPackageManager).toString().compareTo(
353 object2.loadLabel(mPackageManager).toString());
354 }
355 };
356
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 // get the list of shortcuts
358 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
359 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
360 Collections.sort(mShortcutList, resolveInfoComparator);
361
Winson Chunge8878e32010-09-15 20:37:09 -0700362 // get the list of wallpapers
363 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800364 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
365 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700366 Collections.sort(mWallpaperList, resolveInfoComparator);
367
Winson Chung04998342011-01-05 13:54:43 -0800368 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
369 retainShortcutList.addAll(mWallpaperList);
370 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
371 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700372 invalidatePageData();
373 }
374
375 public void setDragController(DragController dragger) {
376 mDragController = dragger;
377 }
378
379 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800380 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800381 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800382 if (getChildCount() > 0) {
383 setCurrentPage(0);
384 updateCurrentPageScroll();
385 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700386
Winson Chunga12a2502010-12-20 14:41:35 -0800387 // End the current choice mode so that we don't carry selections across tabs
388 endChoiceMode();
389 }
390 }
391
392 public CustomizationType getCustomizationFilter() {
393 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700394 }
395
Patrick Dubroy5f445422011-02-18 14:35:21 -0800396 /**
397 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
Patrick Dubroy5f445422011-02-18 14:35:21 -0800398 */
399 private void resetCheckedItem(boolean animated) {
Patrick Dubroy6f133422011-02-24 12:16:12 -0800400 final Checkable checkable = getSingleCheckedGrandchild();
401 if (checkable != null) {
402 if (checkable instanceof PagedViewWidget) {
403 ((PagedViewWidget) checkable).setChecked(false, animated);
404 } else {
405 ((PagedViewIcon) checkable).setChecked(false, animated);
406 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800407 }
408 }
409
410 public void onDropCompleted(View target, Object dragInfo, boolean success) {
411 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
412
413 // Create a view, identical to the drag view, that is only used for animating the
414 // item onto the home screen (or back to its original position, if the drop failed).
Patrick Dubroy6f133422011-02-24 12:16:12 -0800415 final int[] pos = mDragController.getDragView().getPosition(null);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800416 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
417 animView.setVisibility(View.VISIBLE);
418
419 if (success) {
420 resetCheckedItem(true);
421 animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0);
422 } else {
423 // Animate the icon/widget back to its original position
424 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
425 public void run() {
426 resetCheckedItem(false);
427 dragLayer.removeView(animView);
428 }
429 });
430 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800431 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800432 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800433 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700434 }
435
436 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800437 public void onDragViewVisible() {
438 }
439
Patrick Dubroy5f445422011-02-18 14:35:21 -0800440 /**
441 * Animates the given item onto the center of a home screen, and then scales the item to
442 * look as though it's disappearing onto that screen.
443 */
Adam Cohen120980b2010-12-08 11:05:37 -0800444 private void animateItemOntoScreen(View dragView,
445 final CellLayout layout, final ItemInfo info) {
446 mTmpFloatPos[0] = layout.getWidth() / 2;
447 mTmpFloatPos[1] = layout.getHeight() / 2;
448 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
449
Adam Cohen120980b2010-12-08 11:05:37 -0800450 int dragViewWidth = dragView.getMeasuredWidth();
451 int dragViewHeight = dragView.getMeasuredHeight();
452 float heightOffset = 0;
453 float widthOffset = 0;
454
455 if (dragView instanceof ImageView) {
456 Drawable d = ((ImageView) dragView).getDrawable();
457 int width = d.getIntrinsicWidth();
458 int height = d.getIntrinsicHeight();
459
460 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
461 float f = (dragViewWidth / (width * 1.0f));
462 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
463 } else {
464 float f = (dragViewHeight / (height * 1.0f));
465 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
466 }
467 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800468 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
469 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800470
Patrick Dubroy5f445422011-02-18 14:35:21 -0800471 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
472 final View dragCopy = dragLayer.createDragView(dragView);
473 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800474
Patrick Dubroy5f445422011-02-18 14:35:21 -0800475 // Translate the item to the center of the appropriate home screen
476 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800477
Patrick Dubroy5f445422011-02-18 14:35:21 -0800478 // The drop-onto-screen animation begins a bit later, but ends at the same time.
479 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
480
481 // Scale down the icon and fade out the alpha
482 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
483 }
Adam Cohen120980b2010-12-08 11:05:37 -0800484
Patrick Dubroy5f445422011-02-18 14:35:21 -0800485 /**
486 * Animation which scales the view down and animates its alpha, making it appear to disappear
487 * onto a home screen.
488 */
489 private void animateDropOntoScreen(
490 final View view, final ItemInfo info, int duration, int delay) {
491 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
492 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
493
494 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800495 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
496 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
497 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800498 anim.setInterpolator(new LinearInterpolator());
499 if (delay > 0) {
500 anim.setStartDelay(delay);
501 }
502 anim.setDuration(duration);
503 anim.addListener(new AnimatorListenerAdapter() {
504 public void onAnimationEnd(Animator animation) {
505 dragLayer.removeView(view);
506 mLauncher.addExternalItemToScreen(info, layout);
Patrick Dubroybbaa75c2011-03-08 18:47:40 -0800507 info.dropPos = null;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800508 }
509 });
510 anim.start();
511 }
Adam Cohen120980b2010-12-08 11:05:37 -0800512
Patrick Dubroy5f445422011-02-18 14:35:21 -0800513 /**
514 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
515 */
516 private void animateIntoPosition(
517 View view, float toX, float toY, final Runnable endRunnable) {
518 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
519 PropertyValuesHolder.ofFloat("x", toX),
520 PropertyValuesHolder.ofFloat("y", toY));
521 anim.setInterpolator(mQuintEaseOutInterpolator);
522 anim.setDuration(TRANSLATE_ANIM_DURATION);
523 if (endRunnable != null) {
524 anim.addListener(new AnimatorListenerAdapter() {
525 @Override
526 public void onAnimationEnd(Animator animation) {
527 endRunnable.run();
528 }
529 });
530 }
531 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800532 }
533
Patrick Dubroya669d792010-11-23 14:40:33 -0800534 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800535 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800536 // Return early if this is not initiated from a touch
537 if (!v.isInTouchMode()) return;
538 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800539 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700540
Winson Chungd0d43012010-09-26 17:26:45 -0700541 // On certain pages, we allow single tap to mark items as selected so that they can be
542 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700543 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700544 switch (mCustomizationType) {
545 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700546 mChoiceModeTitleText = R.string.cab_widget_selection_text;
547 enterChoiceMode = true;
548 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700549 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700550 mChoiceModeTitleText = R.string.cab_app_selection_text;
551 enterChoiceMode = true;
552 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700553 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700554 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
555 enterChoiceMode = true;
556 break;
557 default:
558 break;
559 }
Winson Chungd0d43012010-09-26 17:26:45 -0700560
Michael Jurka3125d9d2010-09-27 11:30:20 -0700561 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700562 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700563
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700564 Workspace w = mLauncher.getWorkspace();
565 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
566 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800567 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700568
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700569 animateClickFeedback(v, new Runnable() {
570 @Override
571 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800572 cl.calculateSpans(itemInfo);
573 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
574 animateItemOntoScreen(dragView, cl, itemInfo);
575 } else {
576 mLauncher.showOutOfSpaceMessage();
577 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700578 }
579 });
Winson Chungd0d43012010-09-26 17:26:45 -0700580 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700581 }
582
583 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700584 switch (mCustomizationType) {
585 case WallpaperCustomization:
586 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700587 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700588 animateClickFeedback(v, new Runnable() {
589 @Override
590 public void run() {
591 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700592 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700593 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
594 ComponentName name = new ComponentName(info.activityInfo.packageName,
595 info.activityInfo.name);
596 createWallpapersIntent.setComponent(name);
597 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700598 }
599 });
Winson Chungd0d43012010-09-26 17:26:45 -0700600 break;
601 default:
602 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700603 }
604 }
605
Winson Chung1908d072011-02-24 18:09:44 -0800606 private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800607 final Rect bounds = d.getBounds();
608 final int w = bounds.width();
609 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800610 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800611 renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800612 return b;
613 }
614
Adam Cohen120980b2010-12-08 11:05:37 -0800615 private View getDragView(View v) {
616 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
617 v.findViewById(R.id.widget_preview) : v;
618 }
619
Michael Jurka72b079e2010-12-10 01:03:53 -0800620 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800621 if (!v.isInTouchMode()) return false;
622 if (!super.beginDragging(v)) return false;
623
Winson Chungd0d43012010-09-26 17:26:45 -0700624 // End the current choice mode before we start dragging anything
625 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
626 endChoiceMode();
627 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800628 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800629 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800630 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700631 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800632 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800633 if (v instanceof PagedViewWidget) {
634 // Get the widget preview as the drag representation
635 final LinearLayout l = (LinearLayout) v;
636 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800637
638 // Calculate how much to scale the drag preview
639 RectF tmpScaleRect = new RectF(0,0,1,1);
640 i.getImageMatrix().mapRect(tmpScaleRect);
641
642 mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right,
643 tmpScaleRect.bottom);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800644 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800645 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700646
Patrick Dubroy5f445422011-02-18 14:35:21 -0800647 int[] spanXY = CellLayout.rectToCell(getResources(),
648 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800649 createWidgetInfo.spanX = spanXY[0];
650 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800651 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
652 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
653 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800654 result = true;
655 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800656 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800657 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800658 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800659 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800660 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800661 // get icon (top compound drawable, index is 1)
662 final TextView tv = (TextView) v;
663 final Drawable icon = tv.getCompoundDrawables()[1];
Winson Chung1908d072011-02-24 18:09:44 -0800664 mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700665
Patrick Dubroy5f445422011-02-18 14:35:21 -0800666 Object dragInfo = v.getTag();
667 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
668 // TODO: Not sure why we have to copy this
669 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
670 }
671 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
672
673 // Calculate where to place the drag view in order to align the icon pixels with
674 // the original view.
675 v.getLocationOnScreen(mDragViewOrigin);
676 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
677 mDragViewOrigin[1] += v.getPaddingTop();
678
679 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
680 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800681 result = true;
682 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800683 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700684 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800685 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800686
687 // We toggle the checked state _after_ we create the view for the drag in case toggling the
688 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800689 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800690 // In preparation for drag, we always reset the checked grand children regardless of
691 // what choice mode we are in
692 resetCheckedGrandchildren();
693
694 // Toggle the selection on the dragged app
695 Checkable checkable = (Checkable) v;
696
697 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
698 // of the drag of the item. (The fade-in will occur when all checked states are
699 // disabled when dragging ends)
700 checkable.toggle();
701 }
702
703 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 }
705
Winson Chunge3193b92010-09-10 11:44:42 -0700706 /**
707 * Pre-processes the layout of the different widget pages.
708 * @return the number of pages of widgets that we have
709 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700711 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700712
Winson Chunge3193b92010-09-10 11:44:42 -0700713 // create a new page for the first set of widgets
714 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700715 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700716 mWidgetPages.add(newPage);
717
718 // do this until we have no more widgets to lay out
719 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
720 final int widgetCount = mWidgetList.size();
721 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700722 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700723 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700724
Winson Chunge3193b92010-09-10 11:44:42 -0700725 // determine the size of the current widget
726 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
727 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700728
Winson Chunge3193b92010-09-10 11:44:42 -0700729 // create a new page if necessary
730 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
731 numCellsInRow = 0;
732 newPage = new ArrayList<AppWidgetProviderInfo>();
733 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 }
735
Winson Chunge3193b92010-09-10 11:44:42 -0700736 // add the item to the current page
737 newPage.add(info);
738 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700739 }
Winson Chunge3193b92010-09-10 11:44:42 -0700740
Winson Chung80baf5a2010-08-09 16:03:15 -0700741 return mWidgetPages.size();
742 }
743
Winson Chunge3193b92010-09-10 11:44:42 -0700744 /**
Winson Chung7da10252010-10-28 16:07:04 -0700745 * Helper function to draw a drawable to the specified canvas with the specified bounds.
746 */
Winson Chung1908d072011-02-24 18:09:44 -0800747 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
748 float scaleX, float scaleY) {
Winson Chung7da10252010-10-28 16:07:04 -0700749 if (bitmap != null) mCanvas.setBitmap(bitmap);
750 mCanvas.save();
Winson Chung1908d072011-02-24 18:09:44 -0800751 mCanvas.scale(scaleX, scaleY);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800752 final Rect oldBounds = d.copyBounds();
753 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700754 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800755 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700756 mCanvas.restore();
757 }
758
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800759 /*
760 * This method fetches an xml file specified in the manifest identified by
761 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
762 * an image which will be used as the wallpaper preview for an activity
763 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
764 * in the customize drawer.
765 */
766 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800767 ActivityInfo activityInfo = ri.activityInfo;
768 XmlResourceParser parser = null;
769 try {
770 parser = activityInfo.loadXmlMetaData(mPackageManager,
771 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
772 if (parser == null) {
773 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
774 + "wallpaper provider '" + component + '\'');
775 return null;
776 }
777
778 AttributeSet attrs = Xml.asAttributeSet(parser);
779
780 int type;
781 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
782 && type != XmlPullParser.START_TAG) {
783 // drain whitespace, comments, etc.
784 }
785
786 String nodeName = parser.getName();
787 if (!"wallpaper-preview".equals(nodeName)) {
788 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
789 + "wallpaper provider '" + component + '\'');
790 return null;
791 }
792
793 // If metaData was null, we would have returned earlier when getting
794 // the parser No need to do the check here
795 Resources res = mPackageManager.getResourcesForApplication(
796 activityInfo.applicationInfo);
797
798 TypedArray sa = res.obtainAttributes(attrs,
799 com.android.internal.R.styleable.WallpaperPreviewInfo);
800
801 TypedValue value = sa.peekValue(
802 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
803 if (value == null) return null;
804
805 return res.getDrawable(value.resourceId);
806 } catch (Exception e) {
807 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
808 return null;
809 } finally {
810 if (parser != null) parser.close();
811 }
812 }
813
Winson Chung7da10252010-10-28 16:07:04 -0700814 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800815 * This method will extract the preview image specified by the wallpaper source provider (if it
816 * exists) otherwise, it will try to generate a default image preview.
817 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800818 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800819 // To be implemented later: resolving the up-to-date wallpaper thumbnail
820
821 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
822 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
823 Resources resources = mLauncher.getResources();
824
825 // Create a new bitmap to hold the widget preview
826 int width = (int) (dim * sScaleFactor);
827 int height = (int) (dim * sScaleFactor);
828 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800829
830 Drawable background = parseWallpaperPreviewXml(
831 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
832 boolean foundCustomDrawable = background != null;
833
834 if (!foundCustomDrawable) {
835 background = resources.getDrawable(R.drawable.default_widget_preview);
836 }
837
Winson Chung1908d072011-02-24 18:09:44 -0800838 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800839
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800840 // If we don't have a custom icon, we use the app icon on the default background
841 if (!foundCustomDrawable) {
842 try {
843 final IconCache iconCache =
844 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
845 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
846 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800847
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800848 final int iconSize = minDim / 2;
849 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800850 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800851 } catch (Resources.NotFoundException e) {
852 // if we can't find the icon, then just don't draw it
853 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800854 }
855
Winson Chung29d6fea2010-12-01 15:47:31 -0800856 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800857 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
858 return drawable;
859 }
860
861 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700862 * This method will extract the preview image specified by the widget developer (if it exists),
863 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800864 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700865 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800866 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800867 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700868 String packageName = info.provider.getPackageName();
869 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800870 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700871 if (info.previewImage != 0) {
872 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
873 if (drawable == null) {
874 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
875 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700876 }
877 }
878
879 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700880 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
881 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700882 if (drawable == null) {
883 Resources resources = mLauncher.getResources();
884
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700886 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
887 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700888 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
889 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
Winson Chung1908d072011-02-24 18:09:44 -0800890 renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700891
Winson Chung45e1d6e2010-11-09 17:19:49 -0800892 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700893 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700894 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700895 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700896 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700897 }
898 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700899 icon = resources.getDrawable(R.drawable.ic_launcher_application);
900 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700901
Winson Chunge3193b92010-09-10 11:44:42 -0700902 final int iconSize = minDim / 2;
903 final int offset = iconSize / 4;
Winson Chung1908d072011-02-24 18:09:44 -0800904 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 } catch (Resources.NotFoundException e) {
906 // if we can't find the icon, then just don't draw it
907 }
908
Winson Chung29d6fea2010-12-01 15:47:31 -0800909 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700910 } else {
911 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800912 final float imageWidth = drawable.getIntrinsicWidth();
913 final float imageHeight = drawable.getIntrinsicHeight();
914 final float aspect = (float) imageWidth / imageHeight;
915 final int scaledWidth =
916 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
917 final int scaledHeight =
918 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700919 int width;
920 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800921 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700922 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800923 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700924 } else {
925 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800926 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700927 }
928
929 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Winson Chung1908d072011-02-24 18:09:44 -0800930 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f);
Winson Chung7da10252010-10-28 16:07:04 -0700931
Winson Chung29d6fea2010-12-01 15:47:31 -0800932 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800934 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
935 newDrawable.getIntrinsicHeight());
936 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700937 }
938
939 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700940 layout.setCellCount(mCellCountX, mCellCountY);
941 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
942 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700943 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700944 }
945
Winson Chunge3193b92010-09-10 11:44:42 -0700946 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700947 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700948 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
949
950 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
951 }
952
Winson Chung80baf5a2010-08-09 16:03:15 -0700953 private void syncWidgetPages() {
954 if (mWidgetList == null) return;
955
Winson Chunge3193b92010-09-10 11:44:42 -0700956 // we need to repopulate with the LinearLayout layout for the widget pages
957 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700958 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700959 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800960 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700961 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700962 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
963 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700964
Winson Chunge22a8e92010-11-12 13:40:58 -0800965 addView(layout, new LinearLayout.LayoutParams(
966 LinearLayout.LayoutParams.WRAP_CONTENT,
967 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 }
969 }
970
971 private void syncWidgetPageItems(int page) {
972 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700973 LinearLayout layout = (LinearLayout) getChildAt(page);
974 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800976 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700977 layout.removeAllViews();
978 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700979 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
980 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800981 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
982 info.minHeight, null);
983 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700984
Winson Chung29d6fea2010-12-01 15:47:31 -0800985 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700986 R.layout.customize_paged_view_widget, layout, false);
Winson Chung1908d072011-02-24 18:09:44 -0800987
Winson Chung04998342011-01-05 13:54:43 -0800988 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
989 mPageViewIconCache, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700990 l.setTag(createItemInfo);
991 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800992 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700993 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700994
Winson Chunge3193b92010-09-10 11:44:42 -0700995 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700996 }
997 }
998
Winson Chung45e1d6e2010-11-09 17:19:49 -0800999 private void syncWallpaperPages() {
1000 if (mWallpaperList == null) return;
1001
1002 // We need to repopulate the LinearLayout for the wallpaper pages
1003 removeAllViews();
1004 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -08001005 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001006 for (int i = 0; i < numPages; ++i) {
1007 LinearLayout layout = new PagedViewExtendedLayout(getContext());
1008 layout.setGravity(Gravity.CENTER_HORIZONTAL);
1009 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
1010 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
1011
Winson Chunge22a8e92010-11-12 13:40:58 -08001012 addView(layout, new LinearLayout.LayoutParams(
1013 LinearLayout.LayoutParams.WRAP_CONTENT,
1014 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -08001015 }
1016 }
1017
1018 private void syncWallpaperPageItems(int page) {
1019 // Load the items on to the pages
1020 LinearLayout layout = (LinearLayout) getChildAt(page);
1021 layout.removeAllViews();
1022 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -08001023 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -08001024 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -08001025 final int startIndex = page * numItemsPerPage;
1026 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
1027 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -08001028 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -08001029 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -08001030
Winson Chung29d6fea2010-12-01 15:47:31 -08001031 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -08001032 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001033 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
1034 mPageViewIconCache, (numPages > 1));
Winson Chung45e1d6e2010-11-09 17:19:49 -08001035 l.setTag(info);
1036 l.setOnClickListener(this);
1037
Winson Chung45e1d6e2010-11-09 17:19:49 -08001038 layout.addView(l);
1039 }
1040 }
1041
Winson Chung80baf5a2010-08-09 16:03:15 -07001042 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -07001043 // we need to repopulate with PagedViewCellLayouts
1044 removeAllViews();
1045
Winson Chung80baf5a2010-08-09 16:03:15 -07001046 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001047 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -07001048 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -07001049 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1050 setupPage(layout);
1051 addView(layout);
1052 }
1053 }
1054
1055 private void syncListPageItems(int page, List<ResolveInfo> list) {
1056 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001057 final int numPages = getPageCount();
1058 final int numCells = mCellCountX * mCellCountY;
1059 final int startIndex = page * numCells;
1060 final int endIndex = Math.min(startIndex + numCells, list.size());
1061 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001062 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001063 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -07001064 for (int i = startIndex; i < endIndex; ++i) {
1065 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -07001066 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
1067
Winson Chung241c3b42010-08-25 16:53:03 -07001068 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1069 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001070 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -08001071 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
1072 (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -07001073 switch (mCustomizationType) {
1074 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001075 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001076 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001077 case ShortcutCustomization:
1078 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1079 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1080 info.activityInfo.name);
1081 icon.setTag(createItemInfo);
1082 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001083 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001084 icon.setOnLongClickListener(this);
1085 break;
1086 default:
1087 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001088 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001089
1090 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001091 final int x = index % mCellCountX;
1092 final int y = index / mCellCountX;
1093 setupPage(layout);
1094 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
1095 }
1096 }
1097
1098 private void syncAppPages() {
1099 if (mApps == null) return;
1100
1101 // We need to repopulate with PagedViewCellLayouts
1102 removeAllViews();
1103
1104 // Ensure that we have the right number of pages
1105 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1106 for (int i = 0; i < numPages; ++i) {
1107 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1108 setupPage(layout);
1109 addView(layout);
1110 }
1111 }
1112
1113 private void syncAppPageItems(int page) {
1114 if (mApps == null) return;
1115
1116 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001117 final int numPages = getPageCount();
1118 final int numCells = mCellCountX * mCellCountY;
1119 final int startIndex = page * numCells;
1120 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1121 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001122 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001123 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001124 for (int i = startIndex; i < endIndex; ++i) {
1125 final ApplicationInfo info = mApps.get(i);
1126 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1127 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001128 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -07001129 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001130 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001131 icon.setOnLongClickListener(this);
1132
1133 final int index = i - startIndex;
1134 final int x = index % mCellCountX;
1135 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001136 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001137 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001138 }
1139 }
1140
Winson Chung80baf5a2010-08-09 16:03:15 -07001141 @Override
Michael Jurka87b14902011-05-25 22:13:09 -07001142 protected void invalidatePageData() {
1143 if (mWaitingToInitPages || mCellCountX <= 0 || mCellCountY <= 0) {
Michael Jurkaea2daff2011-05-17 18:21:03 -07001144 // We don't know our size yet, which means we haven't calculated cell count x/y;
1145 // onMeasure will call us once we figure out our size
1146 return;
1147 }
Michael Jurka87b14902011-05-25 22:13:09 -07001148 super.invalidatePageData();
1149 }
1150
1151 @Override
1152 public void syncPages() {
Winson Chung1908d072011-02-24 18:09:44 -08001153 boolean enforceMinimumPagedWidths = false;
Winson Chunge3193b92010-09-10 11:44:42 -07001154 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001155 switch (mCustomizationType) {
1156 case WidgetCustomization:
1157 syncWidgetPages();
Winson Chung1908d072011-02-24 18:09:44 -08001158 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001159 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001160 case ShortcutCustomization:
1161 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001162 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001163 break;
1164 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001165 syncWallpaperPages();
Winson Chung1908d072011-02-24 18:09:44 -08001166 enforceMinimumPagedWidths = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001167 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001168 case ApplicationCustomization:
1169 syncAppPages();
1170 centerPagedViewCellLayouts = false;
1171 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001172 default:
1173 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001174 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001175 break;
1176 }
1177
1178 // only try and center the page if there is one page
1179 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001180 if (centerPagedViewCellLayouts) {
1181 if (childCount == 1) {
1182 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1183 layout.enableCenteredContent(true);
1184 } else {
1185 for (int i = 0; i < childCount; ++i) {
1186 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1187 layout.enableCenteredContent(false);
1188 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001189 }
1190 }
1191
Winson Chung1908d072011-02-24 18:09:44 -08001192 // Set a min page width for PagedView layout if we have more than a single page
1193 if (enforceMinimumPagedWidths) {
1194 setMinimumWidthOverride((childCount > 1) ? mMinPageWidth : 0);
1195 }
1196
1197 // Bound the current page index
Winson Chung03929772011-02-23 17:07:10 -08001198 requestLayout();
1199 post(new Runnable() {
1200 @Override
1201 public void run() {
1202 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1203 forceUpdateAdjacentPagesAlpha();
1204 }
1205 });
Winson Chung80baf5a2010-08-09 16:03:15 -07001206 }
1207
1208 @Override
1209 public void syncPageItems(int page) {
1210 switch (mCustomizationType) {
1211 case WidgetCustomization:
1212 syncWidgetPageItems(page);
1213 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001214 case ShortcutCustomization:
1215 syncListPageItems(page, mShortcutList);
1216 break;
1217 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001218 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001219 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001220 case ApplicationCustomization:
1221 syncAppPageItems(page);
1222 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001223 }
1224 }
Winson Chunge3193b92010-09-10 11:44:42 -07001225
Michael Jurka12ac0d62011-02-23 11:48:32 -08001226 int getPageContentWidth() {
1227 return mPageContentWidth;
1228 }
1229
Winson Chungd0d43012010-09-26 17:26:45 -07001230 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001231 protected int getAssociatedLowerPageBound(int page) {
1232 return 0;
1233 }
Winson Chungd0d43012010-09-26 17:26:45 -07001234 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001235 protected int getAssociatedUpperPageBound(int page) {
1236 return getChildCount();
1237 }
Winson Chungd0d43012010-09-26 17:26:45 -07001238
1239 @Override
1240 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001241 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001242 return true;
1243 }
1244
1245 @Override
1246 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1247 return true;
1248 }
1249
1250 @Override
1251 public void onDestroyActionMode(ActionMode mode) {
1252 endChoiceMode();
1253 }
1254
1255 @Override
1256 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1257 return false;
1258 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001259}