blob: 14b2429453d661a658027ae17d2883eb1e9a909c [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 Chung80baf5a2010-08-09 16:03:15 -0700292 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700293
294 // End the current choice mode so that we don't carry selections across tabs
295 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700296 }
297
298 @Override
299 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700300 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 }
302
303 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700304 public void onClick(View v) {
305 if (!v.isInTouchMode()) {
306 return;
307 }
308
Winson Chungd0d43012010-09-26 17:26:45 -0700309 // On certain pages, we allow single tap to mark items as selected so that they can be
310 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700311 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700312 switch (mCustomizationType) {
313 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700314 mChoiceModeTitleText = R.string.cab_widget_selection_text;
315 enterChoiceMode = true;
316 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700317 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700318 mChoiceModeTitleText = R.string.cab_app_selection_text;
319 enterChoiceMode = true;
320 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700321 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700322 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
323 enterChoiceMode = true;
324 break;
325 default:
326 break;
327 }
Winson Chungd0d43012010-09-26 17:26:45 -0700328
Michael Jurka3125d9d2010-09-27 11:30:20 -0700329 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700330 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700331
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700332 Workspace w = mLauncher.getWorkspace();
333 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
334 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700335
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700336 animateClickFeedback(v, new Runnable() {
337 @Override
338 public void run() {
339 mLauncher.addExternalItemToScreen(itemInfo, cl);
340 }
341 });
Winson Chungd0d43012010-09-26 17:26:45 -0700342 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700343 }
344
345 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700346 switch (mCustomizationType) {
347 case WallpaperCustomization:
348 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700349 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700350 animateClickFeedback(v, new Runnable() {
351 @Override
352 public void run() {
353 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700354 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700355 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
356 ComponentName name = new ComponentName(info.activityInfo.packageName,
357 info.activityInfo.name);
358 createWallpapersIntent.setComponent(name);
359 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700360 }
361 });
Winson Chungd0d43012010-09-26 17:26:45 -0700362 break;
363 default:
364 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700365 }
366 }
367
368 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700369 public boolean onLongClick(View v) {
370 if (!v.isInTouchMode()) {
371 return false;
372 }
373
Winson Chungd0d43012010-09-26 17:26:45 -0700374 // End the current choice mode before we start dragging anything
375 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
376 endChoiceMode();
377 }
378
379 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700380 switch (mCustomizationType) {
381 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700382 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700383 final LinearLayout l = (LinearLayout) v;
384 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700385 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
386 Bitmap.Config.ARGB_8888);
387 Canvas c = new Canvas(b);
388 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700389 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700390
Michael Jurka3e7c7632010-10-02 16:01:03 -0700391 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
392 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
393 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700394 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700395
396 // Cleanup the icon
397 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700398 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700399 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700400 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700401 mDragController.startDrag(
402 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700403 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700404 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700405 case ApplicationCustomization:
406 // Pick up the application for dropping
407 ApplicationInfo app = (ApplicationInfo) v.getTag();
408 app = new ApplicationInfo(app);
409
410 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700411 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700412 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700413 }
414 return false;
415 }
416
Winson Chunge3193b92010-09-10 11:44:42 -0700417 /**
418 * Pre-processes the layout of the different widget pages.
419 * @return the number of pages of widgets that we have
420 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700421 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700422 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700423
Winson Chunge3193b92010-09-10 11:44:42 -0700424 // create a new page for the first set of widgets
425 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700426 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700427 mWidgetPages.add(newPage);
428
429 // do this until we have no more widgets to lay out
430 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
431 final int widgetCount = mWidgetList.size();
432 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700433 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700434 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700435
Winson Chunge3193b92010-09-10 11:44:42 -0700436 // determine the size of the current widget
437 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
438 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700439
Winson Chunge3193b92010-09-10 11:44:42 -0700440 // create a new page if necessary
441 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
442 numCellsInRow = 0;
443 newPage = new ArrayList<AppWidgetProviderInfo>();
444 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700445 }
446
Winson Chunge3193b92010-09-10 11:44:42 -0700447 // add the item to the current page
448 newPage.add(info);
449 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700450 }
Winson Chunge3193b92010-09-10 11:44:42 -0700451
Winson Chung80baf5a2010-08-09 16:03:15 -0700452 return mWidgetPages.size();
453 }
454
Winson Chunge3193b92010-09-10 11:44:42 -0700455 /**
Winson Chung7da10252010-10-28 16:07:04 -0700456 * Helper function to draw a drawable to the specified canvas with the specified bounds.
457 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800458 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700459 if (bitmap != null) mCanvas.setBitmap(bitmap);
460 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800461 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700462 d.draw(mCanvas);
463 mCanvas.restore();
464 }
465
466 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800467 * This method will extract the preview image specified by the wallpaper source provider (if it
468 * exists) otherwise, it will try to generate a default image preview.
469 */
470 private Drawable getWallpaperPreview(ResolveInfo info) {
471 // To be implemented later: resolving the up-to-date wallpaper thumbnail
472
473 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
474 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
475 Resources resources = mLauncher.getResources();
476
477 // Create a new bitmap to hold the widget preview
478 int width = (int) (dim * sScaleFactor);
479 int height = (int) (dim * sScaleFactor);
480 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
481 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
482 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
483
484 // Draw the icon flush left
485 try {
486 final IconCache iconCache =
487 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
488 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
489 iconCache.getFullResIcon(info, mPackageManager), mContext));
490
491 final int iconSize = minDim / 2;
492 final int offset = iconSize / 4;
493 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
494 } catch (Resources.NotFoundException e) {
495 // if we can't find the icon, then just don't draw it
496 }
497
498 Drawable drawable = new FastBitmapDrawable(bitmap);
499 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
500 return drawable;
501 }
502
503 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700504 * This method will extract the preview image specified by the widget developer (if it exists),
505 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800506 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700507 */
Winson Chung94ba5b12010-11-08 17:17:47 -0800508 private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800509 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 String packageName = info.provider.getPackageName();
511 Drawable drawable = null;
512 if (info.previewImage != 0) {
513 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
514 if (drawable == null) {
515 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
516 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700517 }
518 }
519
520 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700521 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
522 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700523 if (drawable == null) {
524 Resources resources = mLauncher.getResources();
525
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700527 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
528 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700529 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
530 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
531 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700532
Winson Chung45e1d6e2010-11-09 17:19:49 -0800533 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700534 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700536 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700537 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700538 }
539 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700540 icon = resources.getDrawable(R.drawable.ic_launcher_application);
541 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700542
Winson Chunge3193b92010-09-10 11:44:42 -0700543 final int iconSize = minDim / 2;
544 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800545 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700546 } catch (Resources.NotFoundException e) {
547 // if we can't find the icon, then just don't draw it
548 }
549
Winson Chungb3347bb2010-08-19 14:51:28 -0700550 drawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700551 } else {
552 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800553 final float imageWidth = drawable.getIntrinsicWidth();
554 final float imageHeight = drawable.getIntrinsicHeight();
555 final float aspect = (float) imageWidth / imageHeight;
556 final int scaledWidth =
557 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
558 final int scaledHeight =
559 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700560 int width;
561 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800562 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700563 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800564 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700565 } else {
566 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800567 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700568 }
569
570 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
571 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
572
573 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700574 }
575 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
576 return drawable;
577 }
578
579 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700580 layout.setCellCount(mCellCountX, mCellCountY);
581 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
582 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700583 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700584 }
585
Winson Chunge3193b92010-09-10 11:44:42 -0700586 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700587 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700588 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
589
590 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800591 mMaxWallpaperWidth = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
Winson Chunge3193b92010-09-10 11:44:42 -0700592 }
593
Winson Chung80baf5a2010-08-09 16:03:15 -0700594 private void syncWidgetPages() {
595 if (mWidgetList == null) return;
596
Winson Chunge3193b92010-09-10 11:44:42 -0700597 // we need to repopulate with the LinearLayout layout for the widget pages
598 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700599 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700600 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800601 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700602 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700603 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
604 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700605
606 // Temporary change to prevent the last page from being too small (and items bleeding
607 // onto it). We can remove this once we properly fix the fading algorithm
608 if (i < numPages - 1) {
609 addView(layout, new LinearLayout.LayoutParams(
610 LinearLayout.LayoutParams.WRAP_CONTENT,
611 LinearLayout.LayoutParams.MATCH_PARENT));
612 } else {
613 addView(layout, new LinearLayout.LayoutParams(
614 LinearLayout.LayoutParams.MATCH_PARENT,
615 LinearLayout.LayoutParams.MATCH_PARENT));
616 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700617 }
618 }
619
620 private void syncWidgetPageItems(int page) {
621 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700622 LinearLayout layout = (LinearLayout) getChildAt(page);
623 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700624 final int count = list.size();
625 layout.removeAllViews();
626 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700627 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
628 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungd0d43012010-09-26 17:26:45 -0700629
Winson Chunge3193b92010-09-10 11:44:42 -0700630 LinearLayout l = (LinearLayout) mInflater.inflate(
631 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700632 l.setTag(createItemInfo);
633 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700634 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700635
Winson Chung94ba5b12010-11-08 17:17:47 -0800636 final Drawable icon = getWidgetPreview(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700637
638 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
639 final int hSpan = spans[0];
640 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700641
Winson Chungd0d43012010-09-26 17:26:45 -0700642 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700643 image.setMaxWidth(mMaxWidgetWidth);
644 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700645 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700646 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700647 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700648 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700649
650 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700651 }
652 }
653
Winson Chung45e1d6e2010-11-09 17:19:49 -0800654 private void syncWallpaperPages() {
655 if (mWallpaperList == null) return;
656
657 // We need to repopulate the LinearLayout for the wallpaper pages
658 removeAllViews();
659 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
660 mMaxWidgetsCellHSpan);
661 for (int i = 0; i < numPages; ++i) {
662 LinearLayout layout = new PagedViewExtendedLayout(getContext());
663 layout.setGravity(Gravity.CENTER_HORIZONTAL);
664 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
665 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
666
667 // Temporary change to prevent the last page from being too small (and items bleeding
668 // onto it). We can remove this once we properly fix the fading algorithm
669 if (i < numPages - 1) {
670 addView(layout, new LinearLayout.LayoutParams(
671 LinearLayout.LayoutParams.WRAP_CONTENT,
672 LinearLayout.LayoutParams.MATCH_PARENT));
673 } else {
674 addView(layout, new LinearLayout.LayoutParams(
675 LinearLayout.LayoutParams.MATCH_PARENT,
676 LinearLayout.LayoutParams.MATCH_PARENT));
677 }
678 }
679 }
680
681 private void syncWallpaperPageItems(int page) {
682 // Load the items on to the pages
683 LinearLayout layout = (LinearLayout) getChildAt(page);
684 layout.removeAllViews();
685 final int count = mWallpaperList.size();
686 for (int i = 0; i < count; ++i) {
687 final ResolveInfo info = mWallpaperList.get(i);
688
689 LinearLayout l = (LinearLayout) mInflater.inflate(
690 R.layout.customize_paged_view_wallpaper, layout, false);
691 l.setTag(info);
692 l.setOnClickListener(this);
693
694 final Drawable icon = getWallpaperPreview(info);
695
696 ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview);
697 image.setMaxWidth(mMaxWidgetWidth);
698 image.setImageDrawable(icon);
699 TextView name = (TextView) l.findViewById(R.id.wallpaper_name);
700 name.setText(info.loadLabel(mPackageManager));
701
702 layout.addView(l);
703 }
704 }
705
Winson Chung80baf5a2010-08-09 16:03:15 -0700706 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700707 // we need to repopulate with PagedViewCellLayouts
708 removeAllViews();
709
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700711 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700712 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700713 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
714 setupPage(layout);
715 addView(layout);
716 }
717 }
718
719 private void syncListPageItems(int page, List<ResolveInfo> list) {
720 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700721 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700722 int startIndex = page * numCells;
723 int endIndex = Math.min(startIndex + numCells, list.size());
724 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
725 // TODO: we can optimize by just re-applying to existing views
726 layout.removeAllViews();
727 for (int i = startIndex; i < endIndex; ++i) {
728 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700729 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
730
Winson Chung241c3b42010-08-25 16:53:03 -0700731 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
732 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700733 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
734 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700735 switch (mCustomizationType) {
736 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700737 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700738 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700739 case ShortcutCustomization:
740 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
741 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
742 info.activityInfo.name);
743 icon.setTag(createItemInfo);
744 icon.setOnClickListener(this);
745 icon.setOnLongClickListener(this);
746 break;
747 default:
748 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700749 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700750
751 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700752 final int x = index % mCellCountX;
753 final int y = index / mCellCountX;
754 setupPage(layout);
755 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
756 }
757 }
758
759 private void syncAppPages() {
760 if (mApps == null) return;
761
762 // We need to repopulate with PagedViewCellLayouts
763 removeAllViews();
764
765 // Ensure that we have the right number of pages
766 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
767 for (int i = 0; i < numPages; ++i) {
768 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
769 setupPage(layout);
770 addView(layout);
771 }
772 }
773
774 private void syncAppPageItems(int page) {
775 if (mApps == null) return;
776
777 // ensure that we have the right number of items on the pages
778 int numCells = mCellCountX * mCellCountY;
779 int startIndex = page * numCells;
780 int endIndex = Math.min(startIndex + numCells, mApps.size());
781 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
782 // TODO: we can optimize by just re-applying to existing views
783 layout.removeAllViews();
784 for (int i = startIndex; i < endIndex; ++i) {
785 final ApplicationInfo info = mApps.get(i);
786 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
787 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700788 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700789 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700790 icon.setOnLongClickListener(this);
791
792 final int index = i - startIndex;
793 final int x = index % mCellCountX;
794 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700795 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700796 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700797 }
798 }
799
Winson Chung80baf5a2010-08-09 16:03:15 -0700800 @Override
801 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700802 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 switch (mCustomizationType) {
804 case WidgetCustomization:
805 syncWidgetPages();
806 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700807 case ShortcutCustomization:
808 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700809 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700810 break;
811 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800812 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700813 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700814 case ApplicationCustomization:
815 syncAppPages();
816 centerPagedViewCellLayouts = false;
817 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700818 default:
819 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700820 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700821 break;
822 }
823
824 // only try and center the page if there is one page
825 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700826 if (centerPagedViewCellLayouts) {
827 if (childCount == 1) {
828 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
829 layout.enableCenteredContent(true);
830 } else {
831 for (int i = 0; i < childCount; ++i) {
832 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
833 layout.enableCenteredContent(false);
834 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700835 }
836 }
837
838 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700839 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700840 }
841
842 @Override
843 public void syncPageItems(int page) {
844 switch (mCustomizationType) {
845 case WidgetCustomization:
846 syncWidgetPageItems(page);
847 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700848 case ShortcutCustomization:
849 syncListPageItems(page, mShortcutList);
850 break;
851 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800852 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700853 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700854 case ApplicationCustomization:
855 syncAppPageItems(page);
856 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700857 }
858 }
Winson Chunge3193b92010-09-10 11:44:42 -0700859
Winson Chungd0d43012010-09-26 17:26:45 -0700860 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700861 protected int getAssociatedLowerPageBound(int page) {
862 return 0;
863 }
Winson Chungd0d43012010-09-26 17:26:45 -0700864 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700865 protected int getAssociatedUpperPageBound(int page) {
866 return getChildCount();
867 }
Winson Chungd0d43012010-09-26 17:26:45 -0700868
869 @Override
870 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700871 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700872 return true;
873 }
874
875 @Override
876 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
877 return true;
878 }
879
880 @Override
881 public void onDestroyActionMode(ActionMode mode) {
882 endChoiceMode();
883 }
884
885 @Override
886 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
887 return false;
888 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700889}