blob: a0e3528ece18213a413c6febc56c02584e65f02f [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) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800306 // Return early if this is not initiated from a touch
307 if (!v.isInTouchMode()) return;
308 // Return early if we are still animating the pages
309 if (mNextPage == INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700310
Winson Chungd0d43012010-09-26 17:26:45 -0700311 // On certain pages, we allow single tap to mark items as selected so that they can be
312 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700313 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700314 switch (mCustomizationType) {
315 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700316 mChoiceModeTitleText = R.string.cab_widget_selection_text;
317 enterChoiceMode = true;
318 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700319 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700320 mChoiceModeTitleText = R.string.cab_app_selection_text;
321 enterChoiceMode = true;
322 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700323 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700324 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
325 enterChoiceMode = true;
326 break;
327 default:
328 break;
329 }
Winson Chungd0d43012010-09-26 17:26:45 -0700330
Michael Jurka3125d9d2010-09-27 11:30:20 -0700331 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700332 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700333
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700334 Workspace w = mLauncher.getWorkspace();
335 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
336 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700337
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700338 animateClickFeedback(v, new Runnable() {
339 @Override
340 public void run() {
341 mLauncher.addExternalItemToScreen(itemInfo, cl);
342 }
343 });
Winson Chungd0d43012010-09-26 17:26:45 -0700344 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700345 }
346
347 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700348 switch (mCustomizationType) {
349 case WallpaperCustomization:
350 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700351 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700352 animateClickFeedback(v, new Runnable() {
353 @Override
354 public void run() {
355 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700356 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700357 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
358 ComponentName name = new ComponentName(info.activityInfo.packageName,
359 info.activityInfo.name);
360 createWallpapersIntent.setComponent(name);
361 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700362 }
363 });
Winson Chungd0d43012010-09-26 17:26:45 -0700364 break;
365 default:
366 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700367 }
368 }
369
370 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800372 // Return early if this is not initiated from a touch
373 if (!v.isInTouchMode()) return false;
374 // Return early if we are still animating the pages
375 if (mNextPage == INVALID_PAGE) return false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700376
Winson Chungd0d43012010-09-26 17:26:45 -0700377 // End the current choice mode before we start dragging anything
378 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
379 endChoiceMode();
380 }
381
382 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 switch (mCustomizationType) {
384 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700385 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700386 final LinearLayout l = (LinearLayout) v;
387 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700388 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
389 Bitmap.Config.ARGB_8888);
390 Canvas c = new Canvas(b);
391 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700392 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700393
Michael Jurka3e7c7632010-10-02 16:01:03 -0700394 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
395 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
396 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700397 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700398
399 // Cleanup the icon
400 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700401 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700402 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700403 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700404 mDragController.startDrag(
405 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700406 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700407 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700408 case ApplicationCustomization:
409 // Pick up the application for dropping
410 ApplicationInfo app = (ApplicationInfo) v.getTag();
411 app = new ApplicationInfo(app);
412
413 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700414 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700415 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700416 }
417 return false;
418 }
419
Winson Chunge3193b92010-09-10 11:44:42 -0700420 /**
421 * Pre-processes the layout of the different widget pages.
422 * @return the number of pages of widgets that we have
423 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700424 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700425 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700426
Winson Chunge3193b92010-09-10 11:44:42 -0700427 // create a new page for the first set of widgets
428 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700429 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700430 mWidgetPages.add(newPage);
431
432 // do this until we have no more widgets to lay out
433 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
434 final int widgetCount = mWidgetList.size();
435 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700436 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700437 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700438
Winson Chunge3193b92010-09-10 11:44:42 -0700439 // determine the size of the current widget
440 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
441 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700442
Winson Chunge3193b92010-09-10 11:44:42 -0700443 // create a new page if necessary
444 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
445 numCellsInRow = 0;
446 newPage = new ArrayList<AppWidgetProviderInfo>();
447 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700448 }
449
Winson Chunge3193b92010-09-10 11:44:42 -0700450 // add the item to the current page
451 newPage.add(info);
452 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 }
Winson Chunge3193b92010-09-10 11:44:42 -0700454
Winson Chung80baf5a2010-08-09 16:03:15 -0700455 return mWidgetPages.size();
456 }
457
Winson Chunge3193b92010-09-10 11:44:42 -0700458 /**
Winson Chung7da10252010-10-28 16:07:04 -0700459 * Helper function to draw a drawable to the specified canvas with the specified bounds.
460 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800461 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700462 if (bitmap != null) mCanvas.setBitmap(bitmap);
463 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800464 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700465 d.draw(mCanvas);
466 mCanvas.restore();
467 }
468
469 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800470 * This method will extract the preview image specified by the wallpaper source provider (if it
471 * exists) otherwise, it will try to generate a default image preview.
472 */
473 private Drawable getWallpaperPreview(ResolveInfo info) {
474 // To be implemented later: resolving the up-to-date wallpaper thumbnail
475
476 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
477 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
478 Resources resources = mLauncher.getResources();
479
480 // Create a new bitmap to hold the widget preview
481 int width = (int) (dim * sScaleFactor);
482 int height = (int) (dim * sScaleFactor);
483 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
484 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
485 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
486
487 // Draw the icon flush left
488 try {
489 final IconCache iconCache =
490 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
491 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
492 iconCache.getFullResIcon(info, mPackageManager), mContext));
493
494 final int iconSize = minDim / 2;
495 final int offset = iconSize / 4;
496 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
497 } catch (Resources.NotFoundException e) {
498 // if we can't find the icon, then just don't draw it
499 }
500
501 Drawable drawable = new FastBitmapDrawable(bitmap);
502 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
503 return drawable;
504 }
505
506 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700507 * This method will extract the preview image specified by the widget developer (if it exists),
508 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800509 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700510 */
Winson Chung94ba5b12010-11-08 17:17:47 -0800511 private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800512 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700513 String packageName = info.provider.getPackageName();
514 Drawable drawable = null;
515 if (info.previewImage != 0) {
516 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
517 if (drawable == null) {
518 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
519 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700520 }
521 }
522
523 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700524 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
525 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 if (drawable == null) {
527 Resources resources = mLauncher.getResources();
528
Winson Chung80baf5a2010-08-09 16:03:15 -0700529 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700530 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
531 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700532 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
533 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
534 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700535
Winson Chung45e1d6e2010-11-09 17:19:49 -0800536 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700537 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700538 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700539 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700540 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700541 }
542 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700543 icon = resources.getDrawable(R.drawable.ic_launcher_application);
544 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700545
Winson Chunge3193b92010-09-10 11:44:42 -0700546 final int iconSize = minDim / 2;
547 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800548 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700549 } catch (Resources.NotFoundException e) {
550 // if we can't find the icon, then just don't draw it
551 }
552
Winson Chungb3347bb2010-08-19 14:51:28 -0700553 drawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700554 } else {
555 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800556 final float imageWidth = drawable.getIntrinsicWidth();
557 final float imageHeight = drawable.getIntrinsicHeight();
558 final float aspect = (float) imageWidth / imageHeight;
559 final int scaledWidth =
560 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
561 final int scaledHeight =
562 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700563 int width;
564 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800565 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700566 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800567 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700568 } else {
569 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800570 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700571 }
572
573 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
574 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
575
576 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700577 }
578 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
579 return drawable;
580 }
581
582 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700583 layout.setCellCount(mCellCountX, mCellCountY);
584 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
585 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700586 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700587 }
588
Winson Chunge3193b92010-09-10 11:44:42 -0700589 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700590 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700591 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
592
593 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800594 mMaxWallpaperWidth = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
Winson Chunge3193b92010-09-10 11:44:42 -0700595 }
596
Winson Chung80baf5a2010-08-09 16:03:15 -0700597 private void syncWidgetPages() {
598 if (mWidgetList == null) return;
599
Winson Chunge3193b92010-09-10 11:44:42 -0700600 // we need to repopulate with the LinearLayout layout for the widget pages
601 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700603 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800604 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700605 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700606 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
607 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700608
Winson Chunge22a8e92010-11-12 13:40:58 -0800609 addView(layout, new LinearLayout.LayoutParams(
610 LinearLayout.LayoutParams.WRAP_CONTENT,
611 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 }
613 }
614
615 private void syncWidgetPageItems(int page) {
616 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700617 LinearLayout layout = (LinearLayout) getChildAt(page);
618 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700619 final int count = list.size();
620 layout.removeAllViews();
621 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700622 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
623 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungd0d43012010-09-26 17:26:45 -0700624
Winson Chunge3193b92010-09-10 11:44:42 -0700625 LinearLayout l = (LinearLayout) mInflater.inflate(
626 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700627 l.setTag(createItemInfo);
628 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700629 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700630
Winson Chung94ba5b12010-11-08 17:17:47 -0800631 final Drawable icon = getWidgetPreview(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700632
633 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
634 final int hSpan = spans[0];
635 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700636
Winson Chungd0d43012010-09-26 17:26:45 -0700637 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700638 image.setMaxWidth(mMaxWidgetWidth);
639 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700640 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700641 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700642 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700643 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700644
645 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700646 }
647 }
648
Winson Chung45e1d6e2010-11-09 17:19:49 -0800649 private void syncWallpaperPages() {
650 if (mWallpaperList == null) return;
651
652 // We need to repopulate the LinearLayout for the wallpaper pages
653 removeAllViews();
654 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
655 mMaxWidgetsCellHSpan);
656 for (int i = 0; i < numPages; ++i) {
657 LinearLayout layout = new PagedViewExtendedLayout(getContext());
658 layout.setGravity(Gravity.CENTER_HORIZONTAL);
659 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
660 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
661
Winson Chunge22a8e92010-11-12 13:40:58 -0800662 addView(layout, new LinearLayout.LayoutParams(
663 LinearLayout.LayoutParams.WRAP_CONTENT,
664 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800665 }
666 }
667
668 private void syncWallpaperPageItems(int page) {
669 // Load the items on to the pages
670 LinearLayout layout = (LinearLayout) getChildAt(page);
671 layout.removeAllViews();
672 final int count = mWallpaperList.size();
673 for (int i = 0; i < count; ++i) {
674 final ResolveInfo info = mWallpaperList.get(i);
675
676 LinearLayout l = (LinearLayout) mInflater.inflate(
677 R.layout.customize_paged_view_wallpaper, layout, false);
678 l.setTag(info);
679 l.setOnClickListener(this);
680
681 final Drawable icon = getWallpaperPreview(info);
682
683 ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview);
684 image.setMaxWidth(mMaxWidgetWidth);
685 image.setImageDrawable(icon);
686 TextView name = (TextView) l.findViewById(R.id.wallpaper_name);
687 name.setText(info.loadLabel(mPackageManager));
688
689 layout.addView(l);
690 }
691 }
692
Winson Chung80baf5a2010-08-09 16:03:15 -0700693 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700694 // we need to repopulate with PagedViewCellLayouts
695 removeAllViews();
696
Winson Chung80baf5a2010-08-09 16:03:15 -0700697 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700698 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700699 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700700 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
701 setupPage(layout);
702 addView(layout);
703 }
704 }
705
706 private void syncListPageItems(int page, List<ResolveInfo> list) {
707 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700708 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700709 int startIndex = page * numCells;
710 int endIndex = Math.min(startIndex + numCells, list.size());
711 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
712 // TODO: we can optimize by just re-applying to existing views
713 layout.removeAllViews();
714 for (int i = startIndex; i < endIndex; ++i) {
715 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700716 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
717
Winson Chung241c3b42010-08-25 16:53:03 -0700718 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
719 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700720 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
721 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700722 switch (mCustomizationType) {
723 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700724 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700725 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700726 case ShortcutCustomization:
727 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
728 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
729 info.activityInfo.name);
730 icon.setTag(createItemInfo);
731 icon.setOnClickListener(this);
732 icon.setOnLongClickListener(this);
733 break;
734 default:
735 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700736 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700737
738 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700739 final int x = index % mCellCountX;
740 final int y = index / mCellCountX;
741 setupPage(layout);
742 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
743 }
744 }
745
746 private void syncAppPages() {
747 if (mApps == null) return;
748
749 // We need to repopulate with PagedViewCellLayouts
750 removeAllViews();
751
752 // Ensure that we have the right number of pages
753 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
754 for (int i = 0; i < numPages; ++i) {
755 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
756 setupPage(layout);
757 addView(layout);
758 }
759 }
760
761 private void syncAppPageItems(int page) {
762 if (mApps == null) return;
763
764 // ensure that we have the right number of items on the pages
765 int numCells = mCellCountX * mCellCountY;
766 int startIndex = page * numCells;
767 int endIndex = Math.min(startIndex + numCells, mApps.size());
768 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
769 // TODO: we can optimize by just re-applying to existing views
770 layout.removeAllViews();
771 for (int i = startIndex; i < endIndex; ++i) {
772 final ApplicationInfo info = mApps.get(i);
773 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
774 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700775 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700776 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700777 icon.setOnLongClickListener(this);
778
779 final int index = i - startIndex;
780 final int x = index % mCellCountX;
781 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700782 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700783 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700784 }
785 }
786
Winson Chung80baf5a2010-08-09 16:03:15 -0700787 @Override
788 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700789 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700790 switch (mCustomizationType) {
791 case WidgetCustomization:
792 syncWidgetPages();
793 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700794 case ShortcutCustomization:
795 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700796 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700797 break;
798 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800799 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700800 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700801 case ApplicationCustomization:
802 syncAppPages();
803 centerPagedViewCellLayouts = false;
804 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700805 default:
806 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700807 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700808 break;
809 }
810
811 // only try and center the page if there is one page
812 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700813 if (centerPagedViewCellLayouts) {
814 if (childCount == 1) {
815 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
816 layout.enableCenteredContent(true);
817 } else {
818 for (int i = 0; i < childCount; ++i) {
819 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
820 layout.enableCenteredContent(false);
821 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 }
823 }
824
825 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700826 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700827 }
828
829 @Override
830 public void syncPageItems(int page) {
831 switch (mCustomizationType) {
832 case WidgetCustomization:
833 syncWidgetPageItems(page);
834 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700835 case ShortcutCustomization:
836 syncListPageItems(page, mShortcutList);
837 break;
838 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800839 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700840 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700841 case ApplicationCustomization:
842 syncAppPageItems(page);
843 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700844 }
845 }
Winson Chunge3193b92010-09-10 11:44:42 -0700846
Winson Chungd0d43012010-09-26 17:26:45 -0700847 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700848 protected int getAssociatedLowerPageBound(int page) {
849 return 0;
850 }
Winson Chungd0d43012010-09-26 17:26:45 -0700851 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700852 protected int getAssociatedUpperPageBound(int page) {
853 return getChildCount();
854 }
Winson Chungd0d43012010-09-26 17:26:45 -0700855
856 @Override
857 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700858 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700859 return true;
860 }
861
862 @Override
863 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
864 return true;
865 }
866
867 @Override
868 public void onDestroyActionMode(ActionMode mode) {
869 endChoiceMode();
870 }
871
872 @Override
873 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
874 return false;
875 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700876}