blob: ead258c8c6b2a198f06dd5da94fdb5c64ad4ea27 [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
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
23
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;
Winson Chunge3193b92010-09-10 11:44:42 -070031import android.content.res.Configuration;
Winson Chung80baf5a2010-08-09 16:03:15 -070032import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070033import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070034import android.graphics.Bitmap;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.graphics.Bitmap.Config;
Winson Chung86f77532010-08-24 11:08:22 -070036import android.graphics.Canvas;
37import 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;
40import android.provider.LiveFolders;
41import android.util.AttributeSet;
42import android.util.Log;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.view.LayoutInflater;
Winson Chunge3193b92010-09-10 11:44:42 -070045import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070047import android.view.ViewGroup;
48import android.widget.ImageView;
49import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.widget.TextView;
51
52import com.android.launcher.R;
53
54public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070055 implements View.OnLongClickListener, View.OnClickListener,
Winson Chung80baf5a2010-08-09 16:03:15 -070056 DragSource {
57
58 public enum CustomizationType {
59 WidgetCustomization,
60 FolderCustomization,
61 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070062 WallpaperCustomization,
63 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070064 }
65
Winson Chunge3193b92010-09-10 11:44:42 -070066 /**
67 * The linear layout used strictly for the widget tab of the customization tray
68 */
69 private class WidgetLayout extends LinearLayout {
70 public WidgetLayout(Context context) {
71 super(context);
72 }
73
74 @Override
75 public boolean onTouchEvent(MotionEvent event) {
76 // We eat up the touch events here, since the PagedView (which uses the same swiping
77 // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
78 // the user is scrolling between pages. This means that if the pages themselves don't
79 // handle touch events, it gets forwarded up to PagedView itself, and it's own
80 // onTouchEvent() handling will prevent further intercept touch events from being called
81 // (it's the same view in that case). This is not ideal, but to prevent more changes,
82 // we just always mark the touch event as handled.
83 return super.onTouchEvent(event) || true;
84 }
85 }
86
Winson Chung80baf5a2010-08-09 16:03:15 -070087 private static final String TAG = "CustomizeWorkspace";
88 private static final boolean DEBUG = false;
89
90 private Launcher mLauncher;
91 private DragController mDragController;
92 private PackageManager mPackageManager;
93
94 private CustomizationType mCustomizationType;
95
Winson Chunge3193b92010-09-10 11:44:42 -070096 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
97 private PagedViewCellLayout mWorkspaceWidgetLayout;
98
99 // The mapping between the pages and the widgets that will be laid out on them
100 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
101
102 // The max dimensions for the ImageView we use for displaying the widget
103 private int mMaxWidgetWidth;
104
105 // The max number of widget cells to take a "page" of widget
106 private int mMaxWidgetsCellHSpan;
107
108 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700109 private List<AppWidgetProviderInfo> mWidgetList;
110 private List<ResolveInfo> mFolderList;
111 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700112 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700113 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700114
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700115 private int mCellCountX;
116 private int mCellCountY;
117 private int mPageLayoutPaddingTop;
118 private int mPageLayoutPaddingBottom;
119 private int mPageLayoutPaddingLeft;
120 private int mPageLayoutPaddingRight;
Winson Chunge3193b92010-09-10 11:44:42 -0700121 private static final int sMinWidgetCellHSpan = 2;
122 private static final int sMaxWidgetCellHSpan = 4;
123
124 // The scale factor for widget previews inside the widget drawer
125 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700126
127 private final Canvas mCanvas = new Canvas();
128 private final LayoutInflater mInflater;
129
130 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700131 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700132 }
133
134 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700135 this(context, attrs, 0);
136 }
137
138 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
139 super(context, attrs, defStyle);
140
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700141 TypedArray a;
142 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700143 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700144 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
145 a.recycle();
146 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
147 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
148 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
149 mPageLayoutPaddingTop = a.getDimensionPixelSize(
150 R.styleable.PagedView_pageLayoutPaddingTop, 10);
151 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
152 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
153 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
154 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
155 mPageLayoutPaddingRight = a.getDimensionPixelSize(
156 R.styleable.PagedView_pageLayoutPaddingRight, 10);
157 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700159 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
160 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700162
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 setVisibility(View.GONE);
164 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700165 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700166 }
167
168 public void setLauncher(Launcher launcher) {
169 Context context = getContext();
170 mLauncher = launcher;
171 mPackageManager = context.getPackageManager();
172 }
173
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700174 /**
175 * Sets the list of applications that launcher has loaded.
176 */
177 public void setApps(ArrayList<ApplicationInfo> list) {
178 mApps = list;
179 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
180 mPageViewIconCache.clear();
181 invalidatePageData();
182 }
183
184 /**
185 * Convenience function to add new items to the set of applications that were previously loaded.
186 * Called by both updateApps() and setApps().
187 */
188 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
189 // we add it in place, in alphabetical order
190 final int count = list.size();
191 for (int i = 0; i < count; ++i) {
192 final ApplicationInfo info = list.get(i);
193 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
194 if (index < 0) {
195 mApps.add(-(index + 1), info);
196 }
197 }
198 }
199
200 /**
201 * Adds new applications to the loaded list, and notifies the paged view to update itself.
202 */
203 public void addApps(ArrayList<ApplicationInfo> list) {
204 addAppsWithoutInvalidate(list);
205 invalidatePageData();
206 }
207
208 /**
209 * Convenience function to remove items to the set of applications that were previously loaded.
210 * Called by both updateApps() and removeApps().
211 */
212 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
213 // loop through all the apps and remove apps that have the same component
214 final int length = list.size();
215 for (int i = 0; i < length; ++i) {
216 final ApplicationInfo info = list.get(i);
217 int removeIndex = findAppByComponent(mApps, info);
218 if (removeIndex > -1) {
219 mApps.remove(removeIndex);
220 mPageViewIconCache.removeOutline(info);
221 }
222 }
223 }
224
225 /**
226 * Removes applications from the loaded list, and notifies the paged view to update itself.
227 */
228 public void removeApps(ArrayList<ApplicationInfo> list) {
229 removeAppsWithoutInvalidate(list);
230 invalidatePageData();
231 }
232
233 /**
234 * Updates a set of applications from the loaded list, and notifies the paged view to update
235 * itself.
236 */
237 public void updateApps(ArrayList<ApplicationInfo> list) {
238 // We remove and re-add the updated applications list because it's properties may have
239 // changed (ie. the title), and this will ensure that the items will be in their proper
240 // place in the list.
241 removeAppsWithoutInvalidate(list);
242 addAppsWithoutInvalidate(list);
243 invalidatePageData();
244 }
245
246 /**
247 * Convenience function to find matching ApplicationInfos for removal.
248 */
249 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
250 ComponentName removeComponent = item.intent.getComponent();
251 final int length = list.size();
252 for (int i = 0; i < length; ++i) {
253 ApplicationInfo info = list.get(i);
254 if (info.intent.getComponent().equals(removeComponent)) {
255 return i;
256 }
257 }
258 return -1;
259 }
260
Winson Chung80baf5a2010-08-09 16:03:15 -0700261 public void update() {
262 Context context = getContext();
263
264 // get the list of widgets
265 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
266 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
267 @Override
268 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
269 return object1.label.compareTo(object2.label);
270 }
271 });
272
273 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
274 @Override
275 public int compare(ResolveInfo object1, ResolveInfo object2) {
276 return object1.loadLabel(mPackageManager).toString().compareTo(
277 object2.loadLabel(mPackageManager).toString());
278 }
279 };
280
281 // get the list of live folder intents
282 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
283 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
284
285 // manually create a separate entry for creating a folder in Launcher
286 ResolveInfo folder = new ResolveInfo();
287 folder.icon = R.drawable.ic_launcher_folder;
288 folder.labelRes = R.string.group_folder;
289 folder.resolvePackageName = context.getPackageName();
290 mFolderList.add(0, folder);
291 Collections.sort(mFolderList, resolveInfoComparator);
292
293 // get the list of shortcuts
294 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
295 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
296 Collections.sort(mShortcutList, resolveInfoComparator);
297
Winson Chunge8878e32010-09-15 20:37:09 -0700298 // get the list of wallpapers
299 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
300 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
301 Collections.sort(mWallpaperList, resolveInfoComparator);
302
Winson Chung241c3b42010-08-25 16:53:03 -0700303 // reset the icon cache
304 mPageViewIconCache.clear();
305
Winson Chunge3193b92010-09-10 11:44:42 -0700306 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 invalidatePageData();
308 }
309
310 public void setDragController(DragController dragger) {
311 mDragController = dragger;
312 }
313
314 public void setCustomizationFilter(CustomizationType filterType) {
315 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700316 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 invalidatePageData();
318 }
319
320 @Override
321 public void onDropCompleted(View target, boolean success) {
322 // do nothing
323 }
324
325 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700326 public void onClick(View v) {
327 if (!v.isInTouchMode()) {
328 return;
329 }
330
331 final View animView = v;
332 switch (mCustomizationType) {
333 case WallpaperCustomization:
334 // animate some feedback to the long press
335 animateClickFeedback(v, new Runnable() {
336 @Override
337 public void run() {
338 // add the shortcut
339 ResolveInfo info = (ResolveInfo) animView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700340 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
341 ComponentName name = new ComponentName(info.activityInfo.packageName,
342 info.activityInfo.name);
343 createWallpapersIntent.setComponent(name);
344 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700345 }
346 });
347 }
348 }
349
350 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 public boolean onLongClick(View v) {
352 if (!v.isInTouchMode()) {
353 return false;
354 }
355
356 final View animView = v;
357 switch (mCustomizationType) {
358 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700359 // Get the icon as the drag representation
360 final LinearLayout l = (LinearLayout) animView;
361 final Drawable icon = ((ImageView) l.findViewById(R.id.icon)).getDrawable();
362 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
363 Bitmap.Config.ARGB_8888);
364 Canvas c = new Canvas(b);
365 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700366
Winson Chung80baf5a2010-08-09 16:03:15 -0700367 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
368 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
369 dragInfo.minWidth = appWidgetInfo.minWidth;
370 dragInfo.minHeight = appWidgetInfo.minHeight;
Winson Chunge3193b92010-09-10 11:44:42 -0700371 mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null);
372
373 // Cleanup the icon
374 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700375 return true;
376 case FolderCustomization:
377 // animate some feedback to the long press
378 animateClickFeedback(v, new Runnable() {
379 @Override
380 public void run() {
381 // add the folder
382 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
383 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
384 if (resolveInfo.labelRes == R.string.group_folder) {
385 // Create app shortcuts is a special built-in case of shortcuts
386 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
387 getContext().getString(R.string.group_folder));
388 } else {
389 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
390 resolveInfo.activityInfo.name);
391 createFolderIntent.setComponent(name);
392 }
393 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
394 mLauncher.addLiveFolder(createFolderIntent);
395 }
396 });
397 return true;
398 case ShortcutCustomization:
399 // animate some feedback to the long press
400 animateClickFeedback(v, new Runnable() {
401 @Override
402 public void run() {
403 // add the shortcut
404 ResolveInfo info = (ResolveInfo) animView.getTag();
405 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
406 if (info.labelRes == R.string.group_applications) {
407 // Create app shortcuts is a special built-in case of shortcuts
408 createShortcutIntent.putExtra(
409 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
410 R.string.group_applications));
411 } else {
412 ComponentName name = new ComponentName(info.activityInfo.packageName,
413 info.activityInfo.name);
414 createShortcutIntent.setComponent(name);
415 }
416 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
417 mLauncher.processShortcut(createShortcutIntent);
418 }
419 });
420 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700421 case ApplicationCustomization:
422 // Pick up the application for dropping
423 ApplicationInfo app = (ApplicationInfo) v.getTag();
424 app = new ApplicationInfo(app);
425
426 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
427 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700428 }
429 return false;
430 }
431
Winson Chunge3193b92010-09-10 11:44:42 -0700432 /**
433 * Pre-processes the layout of the different widget pages.
434 * @return the number of pages of widgets that we have
435 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700436 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700437 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700438
Winson Chunge3193b92010-09-10 11:44:42 -0700439 // create a new page for the first set of widgets
440 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700441 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700442 mWidgetPages.add(newPage);
443
444 // do this until we have no more widgets to lay out
445 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
446 final int widgetCount = mWidgetList.size();
447 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700448 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700449 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700450
Winson Chunge3193b92010-09-10 11:44:42 -0700451 // determine the size of the current widget
452 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
453 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700454
Winson Chunge3193b92010-09-10 11:44:42 -0700455 // create a new page if necessary
456 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
457 numCellsInRow = 0;
458 newPage = new ArrayList<AppWidgetProviderInfo>();
459 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700460 }
461
Winson Chunge3193b92010-09-10 11:44:42 -0700462 // add the item to the current page
463 newPage.add(info);
464 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700465 }
Winson Chunge3193b92010-09-10 11:44:42 -0700466
Winson Chung80baf5a2010-08-09 16:03:15 -0700467 return mWidgetPages.size();
468 }
469
Winson Chunge3193b92010-09-10 11:44:42 -0700470 /**
471 * This method will extract the preview image specified by the widget developer (if it exists),
472 * otherwise, it will try to generate a default image preview with the widget's package icon.
473 * @return the drawable will be used and sized in the ImageView to represent the widget
474 */
475 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700476 PackageManager packageManager = mLauncher.getPackageManager();
477 String packageName = info.provider.getPackageName();
478 Drawable drawable = null;
479 if (info.previewImage != 0) {
480 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
481 if (drawable == null) {
482 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
483 + " for provider: " + info.provider);
484 } else {
485 return drawable;
486 }
487 }
488
489 // If we don't have a preview image, create a default one
490 if (drawable == null) {
491 Resources resources = mLauncher.getResources();
492
Winson Chung80baf5a2010-08-09 16:03:15 -0700493 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700494 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
495 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
496 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
497 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
499 mCanvas.setBitmap(bitmap);
500 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
501 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
502
503 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
504 background.setBounds(0, 0, width, height);
505 background.draw(mCanvas);
506
507 // Draw the icon vertically centered, flush left
508 try {
509 Rect tmpRect = new Rect();
510 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700511 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700512 icon = packageManager.getDrawable(packageName, info.icon, null);
513 } else {
514 icon = resources.getDrawable(R.drawable.ic_launcher_application);
515 }
516 background.getPadding(tmpRect);
517
Winson Chunge3193b92010-09-10 11:44:42 -0700518 final int iconSize = minDim / 2;
519 final int offset = iconSize / 4;
520 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700521 icon.draw(mCanvas);
522 } catch (Resources.NotFoundException e) {
523 // if we can't find the icon, then just don't draw it
524 }
525
Winson Chungb3347bb2010-08-19 14:51:28 -0700526 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 }
528 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
529 return drawable;
530 }
531
532 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700533 layout.setCellCount(mCellCountX, mCellCountY);
534 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
535 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700536 }
537
Winson Chunge3193b92010-09-10 11:44:42 -0700538 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700539 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700540 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
541
542 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
543 }
544
Winson Chung80baf5a2010-08-09 16:03:15 -0700545 private void syncWidgetPages() {
546 if (mWidgetList == null) return;
547
Winson Chunge3193b92010-09-10 11:44:42 -0700548 // we need to repopulate with the LinearLayout layout for the widget pages
549 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700551 for (int i = 0; i < numPages; ++i) {
552 LinearLayout layout = new WidgetLayout(getContext());
553 layout.setGravity(Gravity.CENTER_HORIZONTAL);
554
555 // Temporary change to prevent the last page from being too small (and items bleeding
556 // onto it). We can remove this once we properly fix the fading algorithm
557 if (i < numPages - 1) {
558 addView(layout, new LinearLayout.LayoutParams(
559 LinearLayout.LayoutParams.WRAP_CONTENT,
560 LinearLayout.LayoutParams.MATCH_PARENT));
561 } else {
562 addView(layout, new LinearLayout.LayoutParams(
563 LinearLayout.LayoutParams.MATCH_PARENT,
564 LinearLayout.LayoutParams.MATCH_PARENT));
565 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700566 }
567 }
568
569 private void syncWidgetPageItems(int page) {
570 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700571 LinearLayout layout = (LinearLayout) getChildAt(page);
572 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700573 final int count = list.size();
574 layout.removeAllViews();
575 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700576 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
577 LinearLayout l = (LinearLayout) mInflater.inflate(
578 R.layout.customize_paged_view_widget, layout, false);
579 l.setTag(info);
580 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700581
Winson Chunge3193b92010-09-10 11:44:42 -0700582 final Drawable icon = getWidgetIcon(info);
583 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
584 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
585
586 ImageView image = (ImageView) l.findViewById(R.id.icon);
587 image.setMaxWidth(mMaxWidgetWidth);
588 image.setImageDrawable(icon);
589 TextView name = (TextView) l.findViewById(R.id.name);
590 name.setText(info.label);
591 TextView dims = (TextView) l.findViewById(R.id.dims);
Winson Chung3a476782010-09-15 15:21:55 -0700592 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700593
594 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700595 }
596 }
597
598 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700599 // we need to repopulate with PagedViewCellLayouts
600 removeAllViews();
601
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700603 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700604 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700605 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
606 setupPage(layout);
607 addView(layout);
608 }
609 }
610
611 private void syncListPageItems(int page, List<ResolveInfo> list) {
612 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700613 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700614 int startIndex = page * numCells;
615 int endIndex = Math.min(startIndex + numCells, list.size());
616 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
617 // TODO: we can optimize by just re-applying to existing views
618 layout.removeAllViews();
619 for (int i = startIndex; i < endIndex; ++i) {
620 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700621 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
622 R.layout.customize_paged_view_item, layout, false);
623 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chunge8878e32010-09-15 20:37:09 -0700624 if (mCustomizationType == CustomizationType.WallpaperCustomization) {
625 icon.setOnClickListener(this);
626 } else {
627 icon.setOnLongClickListener(this);
628 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700629
630 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700631 final int x = index % mCellCountX;
632 final int y = index / mCellCountX;
633 setupPage(layout);
634 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
635 }
636 }
637
638 private void syncAppPages() {
639 if (mApps == null) return;
640
641 // We need to repopulate with PagedViewCellLayouts
642 removeAllViews();
643
644 // Ensure that we have the right number of pages
645 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
646 for (int i = 0; i < numPages; ++i) {
647 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
648 setupPage(layout);
649 addView(layout);
650 }
651 }
652
653 private void syncAppPageItems(int page) {
654 if (mApps == null) return;
655
656 // ensure that we have the right number of items on the pages
657 int numCells = mCellCountX * mCellCountY;
658 int startIndex = page * numCells;
659 int endIndex = Math.min(startIndex + numCells, mApps.size());
660 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
661 // TODO: we can optimize by just re-applying to existing views
662 layout.removeAllViews();
663 for (int i = startIndex; i < endIndex; ++i) {
664 final ApplicationInfo info = mApps.get(i);
665 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
666 R.layout.all_apps_paged_view_application, layout, false);
667 icon.applyFromApplicationInfo(info, mPageViewIconCache);
668 icon.setOnLongClickListener(this);
669
670 final int index = i - startIndex;
671 final int x = index % mCellCountX;
672 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700673 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700674 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700675 }
676 }
677
Winson Chung80baf5a2010-08-09 16:03:15 -0700678 @Override
679 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700680 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700681 switch (mCustomizationType) {
682 case WidgetCustomization:
683 syncWidgetPages();
684 break;
685 case FolderCustomization:
686 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700687 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700688 break;
689 case ShortcutCustomization:
690 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700691 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700692 break;
693 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700694 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700695 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700696 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700697 case ApplicationCustomization:
698 syncAppPages();
699 centerPagedViewCellLayouts = false;
700 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700701 default:
702 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700703 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 break;
705 }
706
707 // only try and center the page if there is one page
708 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700709 if (centerPagedViewCellLayouts) {
710 if (childCount == 1) {
711 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
712 layout.enableCenteredContent(true);
713 } else {
714 for (int i = 0; i < childCount; ++i) {
715 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
716 layout.enableCenteredContent(false);
717 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700718 }
719 }
720
721 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700722 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 }
724
725 @Override
726 public void syncPageItems(int page) {
727 switch (mCustomizationType) {
728 case WidgetCustomization:
729 syncWidgetPageItems(page);
730 break;
731 case FolderCustomization:
732 syncListPageItems(page, mFolderList);
733 break;
734 case ShortcutCustomization:
735 syncListPageItems(page, mShortcutList);
736 break;
737 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700738 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700739 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700740 case ApplicationCustomization:
741 syncAppPageItems(page);
742 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700743 }
744 }
Winson Chunge3193b92010-09-10 11:44:42 -0700745
746 protected int getAssociatedLowerPageBound(int page) {
747 return 0;
748 }
749 protected int getAssociatedUpperPageBound(int page) {
750 return getChildCount();
751 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700752}