blob: 1763a00f6c4ab0b9118bf95b1c036f2e38b614c5 [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
Winson Chung5974adc2010-10-25 14:29:11 -070019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070023
24import android.appwidget.AppWidgetManager;
25import android.appwidget.AppWidgetProviderInfo;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070032import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070033import android.graphics.Bitmap;
Winson Chung5974adc2010-10-25 14:29:11 -070034import android.graphics.Bitmap.Config;
Winson Chung86f77532010-08-24 11:08:22 -070035import android.graphics.Canvas;
Winson Chung7da10252010-10-28 16:07:04 -070036import android.graphics.Color;
Winson Chung86f77532010-08-24 11:08:22 -070037import android.graphics.Rect;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.graphics.Region.Op;
Winson Chung80baf5a2010-08-09 16:03:15 -070039import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.util.AttributeSet;
41import android.util.Log;
Winson Chungd0d43012010-09-26 17:26:45 -070042import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070045import android.view.Menu;
46import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070047import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070048import android.widget.ImageView;
49import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.widget.TextView;
51
Winson Chung5974adc2010-10-25 14:29:11 -070052import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070053
54public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070055 implements View.OnLongClickListener, View.OnClickListener,
Winson Chungd0d43012010-09-26 17:26:45 -070056 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070057
58 public enum CustomizationType {
59 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070060 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070061 WallpaperCustomization,
62 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070063 }
64
65 private static final String TAG = "CustomizeWorkspace";
66 private static final boolean DEBUG = false;
67
68 private Launcher mLauncher;
69 private DragController mDragController;
70 private PackageManager mPackageManager;
71
72 private CustomizationType mCustomizationType;
73
Winson Chunge3193b92010-09-10 11:44:42 -070074 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
75 private PagedViewCellLayout mWorkspaceWidgetLayout;
76
77 // The mapping between the pages and the widgets that will be laid out on them
78 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
79
Winson Chung45e1d6e2010-11-09 17:19:49 -080080 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070081 private int mMaxWidgetWidth;
82
Winson Chung45e1d6e2010-11-09 17:19:49 -080083 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070084 private int mMaxWidgetsCellHSpan;
85
Winson Chung45e1d6e2010-11-09 17:19:49 -080086 // The size of the items on the wallpaper tab
87 private int mWallpaperCellHSpan;
88
89 // The max dimensions for the ImageView we use for displaying a wallpaper
90 private int mMaxWallpaperWidth;
91
Winson Chunge3193b92010-09-10 11:44:42 -070092 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070093 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -070094 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070095 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -070096 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -070097
Winson Chunge3193b92010-09-10 11:44:42 -070098 private static final int sMinWidgetCellHSpan = 2;
99 private static final int sMaxWidgetCellHSpan = 4;
100
Michael Jurka3125d9d2010-09-27 11:30:20 -0700101 private int mChoiceModeTitleText;
102
Winson Chunge3193b92010-09-10 11:44:42 -0700103 // The scale factor for widget previews inside the widget drawer
104 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700105
106 private final Canvas mCanvas = new Canvas();
107 private final LayoutInflater mInflater;
108
109 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700110 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700111 }
112
113 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700114 this(context, attrs, 0);
115 }
116
117 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
118 super(context, attrs, defStyle);
119
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700120 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800121 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
122 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700123 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
124 a.recycle();
125 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
126 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
127 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700128 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800129
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700131 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
132 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700133 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700134
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 setVisibility(View.GONE);
136 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700137 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700138 }
139
Winson Chung7da10252010-10-28 16:07:04 -0700140 @Override
141 protected void init() {
142 super.init();
143 mCenterPagesVertically = false;
144 }
145
Winson Chung80baf5a2010-08-09 16:03:15 -0700146 public void setLauncher(Launcher launcher) {
147 Context context = getContext();
148 mLauncher = launcher;
149 mPackageManager = context.getPackageManager();
150 }
151
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700152 /**
153 * Sets the list of applications that launcher has loaded.
154 */
155 public void setApps(ArrayList<ApplicationInfo> list) {
156 mApps = list;
157 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700158
159 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700160 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700161 }
162
163 /**
164 * Convenience function to add new items to the set of applications that were previously loaded.
165 * Called by both updateApps() and setApps().
166 */
167 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
168 // we add it in place, in alphabetical order
169 final int count = list.size();
170 for (int i = 0; i < count; ++i) {
171 final ApplicationInfo info = list.get(i);
172 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
173 if (index < 0) {
174 mApps.add(-(index + 1), info);
175 }
176 }
177 }
178
179 /**
180 * Adds new applications to the loaded list, and notifies the paged view to update itself.
181 */
182 public void addApps(ArrayList<ApplicationInfo> list) {
183 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700184
185 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700186 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700187 }
188
189 /**
190 * Convenience function to remove items to the set of applications that were previously loaded.
191 * Called by both updateApps() and removeApps().
192 */
193 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
194 // loop through all the apps and remove apps that have the same component
195 final int length = list.size();
196 for (int i = 0; i < length; ++i) {
197 final ApplicationInfo info = list.get(i);
198 int removeIndex = findAppByComponent(mApps, info);
199 if (removeIndex > -1) {
200 mApps.remove(removeIndex);
201 mPageViewIconCache.removeOutline(info);
202 }
203 }
204 }
205
206 /**
207 * Removes applications from the loaded list, and notifies the paged view to update itself.
208 */
209 public void removeApps(ArrayList<ApplicationInfo> list) {
210 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700211
212 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700213 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700214 }
215
216 /**
217 * Updates a set of applications from the loaded list, and notifies the paged view to update
218 * itself.
219 */
220 public void updateApps(ArrayList<ApplicationInfo> list) {
221 // We remove and re-add the updated applications list because it's properties may have
222 // changed (ie. the title), and this will ensure that the items will be in their proper
223 // place in the list.
224 removeAppsWithoutInvalidate(list);
225 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700226
227 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700228 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700229 }
230
231 /**
232 * Convenience function to find matching ApplicationInfos for removal.
233 */
234 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
235 ComponentName removeComponent = item.intent.getComponent();
236 final int length = list.size();
237 for (int i = 0; i < length; ++i) {
238 ApplicationInfo info = list.get(i);
239 if (info.intent.getComponent().equals(removeComponent)) {
240 return i;
241 }
242 }
243 return -1;
244 }
245
Winson Chung80baf5a2010-08-09 16:03:15 -0700246 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700247 // get the list of widgets
248 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
249 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
250 @Override
251 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
252 return object1.label.compareTo(object2.label);
253 }
254 });
255
256 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
257 @Override
258 public int compare(ResolveInfo object1, ResolveInfo object2) {
259 return object1.loadLabel(mPackageManager).toString().compareTo(
260 object2.loadLabel(mPackageManager).toString());
261 }
262 };
263
Winson Chung80baf5a2010-08-09 16:03:15 -0700264 // get the list of shortcuts
265 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
266 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
267 Collections.sort(mShortcutList, resolveInfoComparator);
268
Winson Chunge8878e32010-09-15 20:37:09 -0700269 // get the list of wallpapers
270 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
271 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
272 Collections.sort(mWallpaperList, resolveInfoComparator);
273
Winson Chung10fefb12010-11-01 11:57:06 -0700274 invalidatePageDataAndIconCache();
275 }
276
277 private void invalidatePageDataAndIconCache() {
278 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700279 mPageViewIconCache.clear();
280
Winson Chunge3193b92010-09-10 11:44:42 -0700281 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700282 invalidatePageData();
283 }
284
285 public void setDragController(DragController dragger) {
286 mDragController = dragger;
287 }
288
289 public void setCustomizationFilter(CustomizationType filterType) {
290 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700291 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800292 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700294
295 // End the current choice mode so that we don't carry selections across tabs
296 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700297 }
298
299 @Override
300 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700301 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700302 }
303
304 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700305 public void onClick(View v) {
306 if (!v.isInTouchMode()) {
307 return;
308 }
309
Winson Chungd0d43012010-09-26 17:26:45 -0700310 // On certain pages, we allow single tap to mark items as selected so that they can be
311 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700312 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700313 switch (mCustomizationType) {
314 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700315 mChoiceModeTitleText = R.string.cab_widget_selection_text;
316 enterChoiceMode = true;
317 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700318 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700319 mChoiceModeTitleText = R.string.cab_app_selection_text;
320 enterChoiceMode = true;
321 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700322 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700323 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
324 enterChoiceMode = true;
325 break;
326 default:
327 break;
328 }
Winson Chungd0d43012010-09-26 17:26:45 -0700329
Michael Jurka3125d9d2010-09-27 11:30:20 -0700330 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700331 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700332
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700333 Workspace w = mLauncher.getWorkspace();
334 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
335 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700336
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700337 animateClickFeedback(v, new Runnable() {
338 @Override
339 public void run() {
340 mLauncher.addExternalItemToScreen(itemInfo, cl);
341 }
342 });
Winson Chungd0d43012010-09-26 17:26:45 -0700343 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700344 }
345
346 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700347 switch (mCustomizationType) {
348 case WallpaperCustomization:
349 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700350 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700351 animateClickFeedback(v, new Runnable() {
352 @Override
353 public void run() {
354 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700355 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700356 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
357 ComponentName name = new ComponentName(info.activityInfo.packageName,
358 info.activityInfo.name);
359 createWallpapersIntent.setComponent(name);
360 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700361 }
362 });
Winson Chungd0d43012010-09-26 17:26:45 -0700363 break;
364 default:
365 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700366 }
367 }
368
369 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700370 public boolean onLongClick(View v) {
371 if (!v.isInTouchMode()) {
372 return false;
373 }
374
Winson Chungd0d43012010-09-26 17:26:45 -0700375 // End the current choice mode before we start dragging anything
376 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
377 endChoiceMode();
378 }
379
380 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700381 switch (mCustomizationType) {
382 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700383 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700384 final LinearLayout l = (LinearLayout) v;
385 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700386 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
387 Bitmap.Config.ARGB_8888);
388 Canvas c = new Canvas(b);
389 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700390 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700391
Michael Jurka3e7c7632010-10-02 16:01:03 -0700392 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
393 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
394 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700395 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700396
397 // Cleanup the icon
398 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700399 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700400 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700401 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700402 mDragController.startDrag(
403 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700404 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700406 case ApplicationCustomization:
407 // Pick up the application for dropping
408 ApplicationInfo app = (ApplicationInfo) v.getTag();
409 app = new ApplicationInfo(app);
410
411 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700412 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700413 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 }
415 return false;
416 }
417
Winson Chunge3193b92010-09-10 11:44:42 -0700418 /**
419 * Pre-processes the layout of the different widget pages.
420 * @return the number of pages of widgets that we have
421 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700422 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700423 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700424
Winson Chunge3193b92010-09-10 11:44:42 -0700425 // create a new page for the first set of widgets
426 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700428 mWidgetPages.add(newPage);
429
430 // do this until we have no more widgets to lay out
431 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
432 final int widgetCount = mWidgetList.size();
433 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700434 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700435 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700436
Winson Chunge3193b92010-09-10 11:44:42 -0700437 // determine the size of the current widget
438 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
439 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700440
Winson Chunge3193b92010-09-10 11:44:42 -0700441 // create a new page if necessary
442 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
443 numCellsInRow = 0;
444 newPage = new ArrayList<AppWidgetProviderInfo>();
445 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700446 }
447
Winson Chunge3193b92010-09-10 11:44:42 -0700448 // add the item to the current page
449 newPage.add(info);
450 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700451 }
Winson Chunge3193b92010-09-10 11:44:42 -0700452
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 return mWidgetPages.size();
454 }
455
Winson Chunge3193b92010-09-10 11:44:42 -0700456 /**
Winson Chung7da10252010-10-28 16:07:04 -0700457 * Helper function to draw a drawable to the specified canvas with the specified bounds.
458 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800459 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700460 if (bitmap != null) mCanvas.setBitmap(bitmap);
461 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800462 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700463 d.draw(mCanvas);
464 mCanvas.restore();
465 }
466
467 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800468 * This method will extract the preview image specified by the wallpaper source provider (if it
469 * exists) otherwise, it will try to generate a default image preview.
470 */
471 private Drawable getWallpaperPreview(ResolveInfo info) {
472 // To be implemented later: resolving the up-to-date wallpaper thumbnail
473
474 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
475 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
476 Resources resources = mLauncher.getResources();
477
478 // Create a new bitmap to hold the widget preview
479 int width = (int) (dim * sScaleFactor);
480 int height = (int) (dim * sScaleFactor);
481 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
482 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
483 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
484
485 // Draw the icon flush left
486 try {
487 final IconCache iconCache =
488 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
489 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
490 iconCache.getFullResIcon(info, mPackageManager), mContext));
491
492 final int iconSize = minDim / 2;
493 final int offset = iconSize / 4;
494 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
495 } catch (Resources.NotFoundException e) {
496 // if we can't find the icon, then just don't draw it
497 }
498
499 Drawable drawable = new FastBitmapDrawable(bitmap);
500 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
501 return drawable;
502 }
503
504 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700505 * This method will extract the preview image specified by the widget developer (if it exists),
506 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800507 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700508 */
Winson Chung94ba5b12010-11-08 17:17:47 -0800509 private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800510 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700511 String packageName = info.provider.getPackageName();
512 Drawable drawable = null;
513 if (info.previewImage != 0) {
514 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
515 if (drawable == null) {
516 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
517 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700518 }
519 }
520
521 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700522 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
523 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700524 if (drawable == null) {
525 Resources resources = mLauncher.getResources();
526
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700528 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
529 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700530 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
531 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
532 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700533
Winson Chung45e1d6e2010-11-09 17:19:49 -0800534 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700536 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700537 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700538 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700539 }
540 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700541 icon = resources.getDrawable(R.drawable.ic_launcher_application);
542 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700543
Winson Chunge3193b92010-09-10 11:44:42 -0700544 final int iconSize = minDim / 2;
545 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800546 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700547 } catch (Resources.NotFoundException e) {
548 // if we can't find the icon, then just don't draw it
549 }
550
Winson Chungb3347bb2010-08-19 14:51:28 -0700551 drawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700552 } else {
553 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800554 final float imageWidth = drawable.getIntrinsicWidth();
555 final float imageHeight = drawable.getIntrinsicHeight();
556 final float aspect = (float) imageWidth / imageHeight;
557 final int scaledWidth =
558 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
559 final int scaledHeight =
560 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700561 int width;
562 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800563 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700564 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800565 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700566 } else {
567 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800568 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700569 }
570
571 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
572 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
573
574 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700575 }
576 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
577 return drawable;
578 }
579
580 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700581 layout.setCellCount(mCellCountX, mCellCountY);
582 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
583 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700584 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700585 }
586
Winson Chunge3193b92010-09-10 11:44:42 -0700587 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700588 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700589 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
590
591 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800592 mMaxWallpaperWidth = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
Winson Chunge3193b92010-09-10 11:44:42 -0700593 }
594
Winson Chung80baf5a2010-08-09 16:03:15 -0700595 private void syncWidgetPages() {
596 if (mWidgetList == null) return;
597
Winson Chunge3193b92010-09-10 11:44:42 -0700598 // we need to repopulate with the LinearLayout layout for the widget pages
599 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700600 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700601 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800602 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700603 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700604 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
605 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700606
607 // Temporary change to prevent the last page from being too small (and items bleeding
608 // onto it). We can remove this once we properly fix the fading algorithm
609 if (i < numPages - 1) {
610 addView(layout, new LinearLayout.LayoutParams(
611 LinearLayout.LayoutParams.WRAP_CONTENT,
612 LinearLayout.LayoutParams.MATCH_PARENT));
613 } else {
614 addView(layout, new LinearLayout.LayoutParams(
615 LinearLayout.LayoutParams.MATCH_PARENT,
616 LinearLayout.LayoutParams.MATCH_PARENT));
617 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700618 }
619 }
620
621 private void syncWidgetPageItems(int page) {
622 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700623 LinearLayout layout = (LinearLayout) getChildAt(page);
624 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700625 final int count = list.size();
626 layout.removeAllViews();
627 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700628 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
629 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungd0d43012010-09-26 17:26:45 -0700630
Winson Chunge3193b92010-09-10 11:44:42 -0700631 LinearLayout l = (LinearLayout) mInflater.inflate(
632 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700633 l.setTag(createItemInfo);
634 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700635 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700636
Winson Chung94ba5b12010-11-08 17:17:47 -0800637 final Drawable icon = getWidgetPreview(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700638
639 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
640 final int hSpan = spans[0];
641 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700642
Winson Chungd0d43012010-09-26 17:26:45 -0700643 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700644 image.setMaxWidth(mMaxWidgetWidth);
645 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700646 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700647 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700648 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700649 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700650
651 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700652 }
653 }
654
Winson Chung45e1d6e2010-11-09 17:19:49 -0800655 private void syncWallpaperPages() {
656 if (mWallpaperList == null) return;
657
658 // We need to repopulate the LinearLayout for the wallpaper pages
659 removeAllViews();
660 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
661 mMaxWidgetsCellHSpan);
662 for (int i = 0; i < numPages; ++i) {
663 LinearLayout layout = new PagedViewExtendedLayout(getContext());
664 layout.setGravity(Gravity.CENTER_HORIZONTAL);
665 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
666 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
667
668 // Temporary change to prevent the last page from being too small (and items bleeding
669 // onto it). We can remove this once we properly fix the fading algorithm
670 if (i < numPages - 1) {
671 addView(layout, new LinearLayout.LayoutParams(
672 LinearLayout.LayoutParams.WRAP_CONTENT,
673 LinearLayout.LayoutParams.MATCH_PARENT));
674 } else {
675 addView(layout, new LinearLayout.LayoutParams(
676 LinearLayout.LayoutParams.MATCH_PARENT,
677 LinearLayout.LayoutParams.MATCH_PARENT));
678 }
679 }
680 }
681
682 private void syncWallpaperPageItems(int page) {
683 // Load the items on to the pages
684 LinearLayout layout = (LinearLayout) getChildAt(page);
685 layout.removeAllViews();
686 final int count = mWallpaperList.size();
687 for (int i = 0; i < count; ++i) {
688 final ResolveInfo info = mWallpaperList.get(i);
689
690 LinearLayout l = (LinearLayout) mInflater.inflate(
691 R.layout.customize_paged_view_wallpaper, layout, false);
692 l.setTag(info);
693 l.setOnClickListener(this);
694
695 final Drawable icon = getWallpaperPreview(info);
696
697 ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview);
698 image.setMaxWidth(mMaxWidgetWidth);
699 image.setImageDrawable(icon);
700 TextView name = (TextView) l.findViewById(R.id.wallpaper_name);
701 name.setText(info.loadLabel(mPackageManager));
702
703 layout.addView(l);
704 }
705 }
706
Winson Chung80baf5a2010-08-09 16:03:15 -0700707 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700708 // we need to repopulate with PagedViewCellLayouts
709 removeAllViews();
710
Winson Chung80baf5a2010-08-09 16:03:15 -0700711 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700712 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700713 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700714 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
715 setupPage(layout);
716 addView(layout);
717 }
718 }
719
720 private void syncListPageItems(int page, List<ResolveInfo> list) {
721 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700722 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 int startIndex = page * numCells;
724 int endIndex = Math.min(startIndex + numCells, list.size());
725 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
726 // TODO: we can optimize by just re-applying to existing views
727 layout.removeAllViews();
728 for (int i = startIndex; i < endIndex; ++i) {
729 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700730 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
731
Winson Chung241c3b42010-08-25 16:53:03 -0700732 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
733 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700734 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
735 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700736 switch (mCustomizationType) {
737 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700738 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700739 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700740 case ShortcutCustomization:
741 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
742 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
743 info.activityInfo.name);
744 icon.setTag(createItemInfo);
745 icon.setOnClickListener(this);
746 icon.setOnLongClickListener(this);
747 break;
748 default:
749 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700750 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700751
752 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700753 final int x = index % mCellCountX;
754 final int y = index / mCellCountX;
755 setupPage(layout);
756 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
757 }
758 }
759
760 private void syncAppPages() {
761 if (mApps == null) return;
762
763 // We need to repopulate with PagedViewCellLayouts
764 removeAllViews();
765
766 // Ensure that we have the right number of pages
767 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
768 for (int i = 0; i < numPages; ++i) {
769 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
770 setupPage(layout);
771 addView(layout);
772 }
773 }
774
775 private void syncAppPageItems(int page) {
776 if (mApps == null) return;
777
778 // ensure that we have the right number of items on the pages
779 int numCells = mCellCountX * mCellCountY;
780 int startIndex = page * numCells;
781 int endIndex = Math.min(startIndex + numCells, mApps.size());
782 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
783 // TODO: we can optimize by just re-applying to existing views
784 layout.removeAllViews();
785 for (int i = startIndex; i < endIndex; ++i) {
786 final ApplicationInfo info = mApps.get(i);
787 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
788 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700789 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700790 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700791 icon.setOnLongClickListener(this);
792
793 final int index = i - startIndex;
794 final int x = index % mCellCountX;
795 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700796 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700797 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700798 }
799 }
800
Winson Chung80baf5a2010-08-09 16:03:15 -0700801 @Override
802 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700803 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700804 switch (mCustomizationType) {
805 case WidgetCustomization:
806 syncWidgetPages();
807 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700808 case ShortcutCustomization:
809 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700810 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700811 break;
812 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800813 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700814 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700815 case ApplicationCustomization:
816 syncAppPages();
817 centerPagedViewCellLayouts = false;
818 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700819 default:
820 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700821 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 break;
823 }
824
825 // only try and center the page if there is one page
826 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700827 if (centerPagedViewCellLayouts) {
828 if (childCount == 1) {
829 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
830 layout.enableCenteredContent(true);
831 } else {
832 for (int i = 0; i < childCount; ++i) {
833 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
834 layout.enableCenteredContent(false);
835 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700836 }
837 }
838
839 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700840 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700841 }
842
843 @Override
844 public void syncPageItems(int page) {
845 switch (mCustomizationType) {
846 case WidgetCustomization:
847 syncWidgetPageItems(page);
848 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700849 case ShortcutCustomization:
850 syncListPageItems(page, mShortcutList);
851 break;
852 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800853 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700854 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700855 case ApplicationCustomization:
856 syncAppPageItems(page);
857 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700858 }
859 }
Winson Chunge3193b92010-09-10 11:44:42 -0700860
Winson Chungd0d43012010-09-26 17:26:45 -0700861 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700862 protected int getAssociatedLowerPageBound(int page) {
863 return 0;
864 }
Winson Chungd0d43012010-09-26 17:26:45 -0700865 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700866 protected int getAssociatedUpperPageBound(int page) {
867 return getChildCount();
868 }
Winson Chungd0d43012010-09-26 17:26:45 -0700869
870 @Override
871 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700872 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700873 return true;
874 }
875
876 @Override
877 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
878 return true;
879 }
880
881 @Override
882 public void onDestroyActionMode(ActionMode mode) {
883 endChoiceMode();
884 }
885
886 @Override
887 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
888 return false;
889 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700890}