blob: 79b3e6f21ab9a48d379b9262465f5becc5c6055d [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Michael Jurka0280c3b2010-09-17 15:00:07 -070019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
21import android.appwidget.AppWidgetManager;
22import android.appwidget.AppWidgetProviderInfo;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070029import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070030import android.graphics.Bitmap;
Winson Chung86f77532010-08-24 11:08:22 -070031import android.graphics.Canvas;
32import android.graphics.Rect;
Michael Jurka0280c3b2010-09-17 15:00:07 -070033import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070034import android.graphics.Region.Op;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.graphics.drawable.Drawable;
36import android.provider.LiveFolders;
37import android.util.AttributeSet;
38import android.util.Log;
Winson Chunge3193b92010-09-10 11:44:42 -070039import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.view.LayoutInflater;
Winson Chunge3193b92010-09-10 11:44:42 -070041import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070042import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.widget.ImageView;
44import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.widget.TextView;
46
Michael Jurka0280c3b2010-09-17 15:00:07 -070047import java.util.ArrayList;
48import java.util.Collections;
49import java.util.Comparator;
50import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070051
52public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070053 implements View.OnLongClickListener, View.OnClickListener,
Winson Chung80baf5a2010-08-09 16:03:15 -070054 DragSource {
55
56 public enum CustomizationType {
57 WidgetCustomization,
58 FolderCustomization,
59 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070060 WallpaperCustomization,
61 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070062 }
63
Winson Chunge3193b92010-09-10 11:44:42 -070064 /**
65 * The linear layout used strictly for the widget tab of the customization tray
66 */
67 private class WidgetLayout extends LinearLayout {
68 public WidgetLayout(Context context) {
69 super(context);
70 }
71
72 @Override
73 public boolean onTouchEvent(MotionEvent event) {
74 // We eat up the touch events here, since the PagedView (which uses the same swiping
75 // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
76 // the user is scrolling between pages. This means that if the pages themselves don't
77 // handle touch events, it gets forwarded up to PagedView itself, and it's own
78 // onTouchEvent() handling will prevent further intercept touch events from being called
79 // (it's the same view in that case). This is not ideal, but to prevent more changes,
80 // we just always mark the touch event as handled.
81 return super.onTouchEvent(event) || true;
82 }
83 }
84
Winson Chung80baf5a2010-08-09 16:03:15 -070085 private static final String TAG = "CustomizeWorkspace";
86 private static final boolean DEBUG = false;
87
88 private Launcher mLauncher;
89 private DragController mDragController;
90 private PackageManager mPackageManager;
91
92 private CustomizationType mCustomizationType;
93
Winson Chunge3193b92010-09-10 11:44:42 -070094 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
95 private PagedViewCellLayout mWorkspaceWidgetLayout;
96
97 // The mapping between the pages and the widgets that will be laid out on them
98 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
99
100 // The max dimensions for the ImageView we use for displaying the widget
101 private int mMaxWidgetWidth;
102
103 // The max number of widget cells to take a "page" of widget
104 private int mMaxWidgetsCellHSpan;
105
106 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 private List<AppWidgetProviderInfo> mWidgetList;
108 private List<ResolveInfo> mFolderList;
109 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700110 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700111 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700112
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700113 private int mCellCountX;
114 private int mCellCountY;
115 private int mPageLayoutPaddingTop;
116 private int mPageLayoutPaddingBottom;
117 private int mPageLayoutPaddingLeft;
118 private int mPageLayoutPaddingRight;
Winson Chunge3193b92010-09-10 11:44:42 -0700119 private static final int sMinWidgetCellHSpan = 2;
120 private static final int sMaxWidgetCellHSpan = 4;
121
122 // The scale factor for widget previews inside the widget drawer
123 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700124
125 private final Canvas mCanvas = new Canvas();
126 private final LayoutInflater mInflater;
127
128 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700129 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 }
131
132 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700133 this(context, attrs, 0);
134 }
135
136 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
138
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700139 TypedArray a;
140 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700141 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700142 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
143 a.recycle();
144 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
145 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
146 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
147 mPageLayoutPaddingTop = a.getDimensionPixelSize(
148 R.styleable.PagedView_pageLayoutPaddingTop, 10);
149 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
150 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
151 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
152 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
153 mPageLayoutPaddingRight = a.getDimensionPixelSize(
154 R.styleable.PagedView_pageLayoutPaddingRight, 10);
155 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700157 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
158 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700159 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700160
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 setVisibility(View.GONE);
162 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700163 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 }
165
166 public void setLauncher(Launcher launcher) {
167 Context context = getContext();
168 mLauncher = launcher;
169 mPackageManager = context.getPackageManager();
170 }
171
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700172 /**
173 * Sets the list of applications that launcher has loaded.
174 */
175 public void setApps(ArrayList<ApplicationInfo> list) {
176 mApps = list;
177 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
178 mPageViewIconCache.clear();
179 invalidatePageData();
180 }
181
182 /**
183 * Convenience function to add new items to the set of applications that were previously loaded.
184 * Called by both updateApps() and setApps().
185 */
186 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
187 // we add it in place, in alphabetical order
188 final int count = list.size();
189 for (int i = 0; i < count; ++i) {
190 final ApplicationInfo info = list.get(i);
191 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
192 if (index < 0) {
193 mApps.add(-(index + 1), info);
194 }
195 }
196 }
197
198 /**
199 * Adds new applications to the loaded list, and notifies the paged view to update itself.
200 */
201 public void addApps(ArrayList<ApplicationInfo> list) {
202 addAppsWithoutInvalidate(list);
203 invalidatePageData();
204 }
205
206 /**
207 * Convenience function to remove items to the set of applications that were previously loaded.
208 * Called by both updateApps() and removeApps().
209 */
210 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
211 // loop through all the apps and remove apps that have the same component
212 final int length = list.size();
213 for (int i = 0; i < length; ++i) {
214 final ApplicationInfo info = list.get(i);
215 int removeIndex = findAppByComponent(mApps, info);
216 if (removeIndex > -1) {
217 mApps.remove(removeIndex);
218 mPageViewIconCache.removeOutline(info);
219 }
220 }
221 }
222
223 /**
224 * Removes applications from the loaded list, and notifies the paged view to update itself.
225 */
226 public void removeApps(ArrayList<ApplicationInfo> list) {
227 removeAppsWithoutInvalidate(list);
228 invalidatePageData();
229 }
230
231 /**
232 * Updates a set of applications from the loaded list, and notifies the paged view to update
233 * itself.
234 */
235 public void updateApps(ArrayList<ApplicationInfo> list) {
236 // We remove and re-add the updated applications list because it's properties may have
237 // changed (ie. the title), and this will ensure that the items will be in their proper
238 // place in the list.
239 removeAppsWithoutInvalidate(list);
240 addAppsWithoutInvalidate(list);
241 invalidatePageData();
242 }
243
244 /**
245 * Convenience function to find matching ApplicationInfos for removal.
246 */
247 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
248 ComponentName removeComponent = item.intent.getComponent();
249 final int length = list.size();
250 for (int i = 0; i < length; ++i) {
251 ApplicationInfo info = list.get(i);
252 if (info.intent.getComponent().equals(removeComponent)) {
253 return i;
254 }
255 }
256 return -1;
257 }
258
Winson Chung80baf5a2010-08-09 16:03:15 -0700259 public void update() {
260 Context context = getContext();
261
262 // get the list of widgets
263 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
264 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
265 @Override
266 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
267 return object1.label.compareTo(object2.label);
268 }
269 });
270
271 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
272 @Override
273 public int compare(ResolveInfo object1, ResolveInfo object2) {
274 return object1.loadLabel(mPackageManager).toString().compareTo(
275 object2.loadLabel(mPackageManager).toString());
276 }
277 };
278
279 // get the list of live folder intents
280 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
281 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
282
283 // manually create a separate entry for creating a folder in Launcher
284 ResolveInfo folder = new ResolveInfo();
285 folder.icon = R.drawable.ic_launcher_folder;
286 folder.labelRes = R.string.group_folder;
287 folder.resolvePackageName = context.getPackageName();
288 mFolderList.add(0, folder);
289 Collections.sort(mFolderList, resolveInfoComparator);
290
291 // get the list of shortcuts
292 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
293 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
294 Collections.sort(mShortcutList, resolveInfoComparator);
295
Winson Chunge8878e32010-09-15 20:37:09 -0700296 // get the list of wallpapers
297 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
298 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
299 Collections.sort(mWallpaperList, resolveInfoComparator);
300
Winson Chung241c3b42010-08-25 16:53:03 -0700301 // reset the icon cache
302 mPageViewIconCache.clear();
303
Winson Chunge3193b92010-09-10 11:44:42 -0700304 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 invalidatePageData();
306 }
307
308 public void setDragController(DragController dragger) {
309 mDragController = dragger;
310 }
311
312 public void setCustomizationFilter(CustomizationType filterType) {
313 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700314 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 invalidatePageData();
316 }
317
318 @Override
319 public void onDropCompleted(View target, boolean success) {
320 // do nothing
321 }
322
323 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700324 public void onClick(View v) {
325 if (!v.isInTouchMode()) {
326 return;
327 }
328
329 final View animView = v;
330 switch (mCustomizationType) {
331 case WallpaperCustomization:
332 // animate some feedback to the long press
333 animateClickFeedback(v, new Runnable() {
334 @Override
335 public void run() {
336 // add the shortcut
337 ResolveInfo info = (ResolveInfo) animView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700338 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
339 ComponentName name = new ComponentName(info.activityInfo.packageName,
340 info.activityInfo.name);
341 createWallpapersIntent.setComponent(name);
342 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700343 }
344 });
345 }
346 }
347
348 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700349 public boolean onLongClick(View v) {
350 if (!v.isInTouchMode()) {
351 return false;
352 }
353
354 final View animView = v;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700355 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
Winson Chung80baf5a2010-08-09 16:03:15 -0700356 switch (mCustomizationType) {
357 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700358 // Get the icon as the drag representation
359 final LinearLayout l = (LinearLayout) animView;
360 final Drawable icon = ((ImageView) l.findViewById(R.id.icon)).getDrawable();
361 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
362 Bitmap.Config.ARGB_8888);
363 Canvas c = new Canvas(b);
364 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700365
Winson Chung80baf5a2010-08-09 16:03:15 -0700366 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700367 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
368 createItemInfo.componentName = appWidgetInfo.provider;
369 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700370
371 // Cleanup the icon
372 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700373 return true;
374 case FolderCustomization:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700375 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
376 if (resolveInfo.labelRes == R.string.group_folder) {
377 UserFolderInfo folderInfo = new UserFolderInfo();
378 folderInfo.title = getResources().getText(R.string.folder_name);
379 mDragController.startDrag(
380 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
381 } else {
382 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
383 createItemInfo.componentName = new ComponentName(
384 resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
385 mDragController.startDrag(
386 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
387 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 return true;
389 case ShortcutCustomization:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700390 ResolveInfo info = (ResolveInfo) animView.getTag();
391 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
392 createItemInfo.componentName = new ComponentName(
393 info.activityInfo.packageName, info.activityInfo.name);
394 mDragController.startDrag(
395 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700396 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700397 case ApplicationCustomization:
398 // Pick up the application for dropping
399 ApplicationInfo app = (ApplicationInfo) v.getTag();
400 app = new ApplicationInfo(app);
401
402 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
403 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700404 }
405 return false;
406 }
407
Winson Chunge3193b92010-09-10 11:44:42 -0700408 /**
409 * Pre-processes the layout of the different widget pages.
410 * @return the number of pages of widgets that we have
411 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700412 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700413 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700414
Winson Chunge3193b92010-09-10 11:44:42 -0700415 // create a new page for the first set of widgets
416 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700417 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700418 mWidgetPages.add(newPage);
419
420 // do this until we have no more widgets to lay out
421 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
422 final int widgetCount = mWidgetList.size();
423 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700424 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700425 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700426
Winson Chunge3193b92010-09-10 11:44:42 -0700427 // determine the size of the current widget
428 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
429 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700430
Winson Chunge3193b92010-09-10 11:44:42 -0700431 // create a new page if necessary
432 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
433 numCellsInRow = 0;
434 newPage = new ArrayList<AppWidgetProviderInfo>();
435 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700436 }
437
Winson Chunge3193b92010-09-10 11:44:42 -0700438 // add the item to the current page
439 newPage.add(info);
440 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700441 }
Winson Chunge3193b92010-09-10 11:44:42 -0700442
Winson Chung80baf5a2010-08-09 16:03:15 -0700443 return mWidgetPages.size();
444 }
445
Winson Chunge3193b92010-09-10 11:44:42 -0700446 /**
447 * This method will extract the preview image specified by the widget developer (if it exists),
448 * otherwise, it will try to generate a default image preview with the widget's package icon.
449 * @return the drawable will be used and sized in the ImageView to represent the widget
450 */
451 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700452 PackageManager packageManager = mLauncher.getPackageManager();
453 String packageName = info.provider.getPackageName();
454 Drawable drawable = null;
455 if (info.previewImage != 0) {
456 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
457 if (drawable == null) {
458 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
459 + " for provider: " + info.provider);
460 } else {
461 return drawable;
462 }
463 }
464
465 // If we don't have a preview image, create a default one
466 if (drawable == null) {
467 Resources resources = mLauncher.getResources();
468
Winson Chung80baf5a2010-08-09 16:03:15 -0700469 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700470 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
471 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
472 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
473 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700474 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
475 mCanvas.setBitmap(bitmap);
476 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
477 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
478
479 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
480 background.setBounds(0, 0, width, height);
481 background.draw(mCanvas);
482
483 // Draw the icon vertically centered, flush left
484 try {
485 Rect tmpRect = new Rect();
486 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700487 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700488 icon = packageManager.getDrawable(packageName, info.icon, null);
489 } else {
490 icon = resources.getDrawable(R.drawable.ic_launcher_application);
491 }
492 background.getPadding(tmpRect);
493
Winson Chunge3193b92010-09-10 11:44:42 -0700494 final int iconSize = minDim / 2;
495 final int offset = iconSize / 4;
496 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700497 icon.draw(mCanvas);
498 } catch (Resources.NotFoundException e) {
499 // if we can't find the icon, then just don't draw it
500 }
501
Winson Chungb3347bb2010-08-19 14:51:28 -0700502 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700503 }
504 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
505 return drawable;
506 }
507
508 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700509 layout.setCellCount(mCellCountX, mCellCountY);
510 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
511 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700512 }
513
Winson Chunge3193b92010-09-10 11:44:42 -0700514 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700515 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700516 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
517
518 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
519 }
520
Winson Chung80baf5a2010-08-09 16:03:15 -0700521 private void syncWidgetPages() {
522 if (mWidgetList == null) return;
523
Winson Chunge3193b92010-09-10 11:44:42 -0700524 // we need to repopulate with the LinearLayout layout for the widget pages
525 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700527 for (int i = 0; i < numPages; ++i) {
528 LinearLayout layout = new WidgetLayout(getContext());
529 layout.setGravity(Gravity.CENTER_HORIZONTAL);
530
531 // Temporary change to prevent the last page from being too small (and items bleeding
532 // onto it). We can remove this once we properly fix the fading algorithm
533 if (i < numPages - 1) {
534 addView(layout, new LinearLayout.LayoutParams(
535 LinearLayout.LayoutParams.WRAP_CONTENT,
536 LinearLayout.LayoutParams.MATCH_PARENT));
537 } else {
538 addView(layout, new LinearLayout.LayoutParams(
539 LinearLayout.LayoutParams.MATCH_PARENT,
540 LinearLayout.LayoutParams.MATCH_PARENT));
541 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 }
543 }
544
545 private void syncWidgetPageItems(int page) {
546 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700547 LinearLayout layout = (LinearLayout) getChildAt(page);
548 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700549 final int count = list.size();
550 layout.removeAllViews();
551 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700552 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
553 LinearLayout l = (LinearLayout) mInflater.inflate(
554 R.layout.customize_paged_view_widget, layout, false);
555 l.setTag(info);
556 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700557
Winson Chunge3193b92010-09-10 11:44:42 -0700558 final Drawable icon = getWidgetIcon(info);
559 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
560 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
561
562 ImageView image = (ImageView) l.findViewById(R.id.icon);
563 image.setMaxWidth(mMaxWidgetWidth);
564 image.setImageDrawable(icon);
565 TextView name = (TextView) l.findViewById(R.id.name);
566 name.setText(info.label);
567 TextView dims = (TextView) l.findViewById(R.id.dims);
Winson Chung3a476782010-09-15 15:21:55 -0700568 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700569
570 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700571 }
572 }
573
574 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700575 // we need to repopulate with PagedViewCellLayouts
576 removeAllViews();
577
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700579 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700580 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700581 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
582 setupPage(layout);
583 addView(layout);
584 }
585 }
586
587 private void syncListPageItems(int page, List<ResolveInfo> list) {
588 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700589 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700590 int startIndex = page * numCells;
591 int endIndex = Math.min(startIndex + numCells, list.size());
592 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
593 // TODO: we can optimize by just re-applying to existing views
594 layout.removeAllViews();
595 for (int i = startIndex; i < endIndex; ++i) {
596 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700597 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
598 R.layout.customize_paged_view_item, layout, false);
599 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chunge8878e32010-09-15 20:37:09 -0700600 if (mCustomizationType == CustomizationType.WallpaperCustomization) {
601 icon.setOnClickListener(this);
602 } else {
603 icon.setOnLongClickListener(this);
604 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700605
606 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700607 final int x = index % mCellCountX;
608 final int y = index / mCellCountX;
609 setupPage(layout);
610 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
611 }
612 }
613
614 private void syncAppPages() {
615 if (mApps == null) return;
616
617 // We need to repopulate with PagedViewCellLayouts
618 removeAllViews();
619
620 // Ensure that we have the right number of pages
621 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
622 for (int i = 0; i < numPages; ++i) {
623 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
624 setupPage(layout);
625 addView(layout);
626 }
627 }
628
629 private void syncAppPageItems(int page) {
630 if (mApps == null) return;
631
632 // ensure that we have the right number of items on the pages
633 int numCells = mCellCountX * mCellCountY;
634 int startIndex = page * numCells;
635 int endIndex = Math.min(startIndex + numCells, mApps.size());
636 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
637 // TODO: we can optimize by just re-applying to existing views
638 layout.removeAllViews();
639 for (int i = startIndex; i < endIndex; ++i) {
640 final ApplicationInfo info = mApps.get(i);
641 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
642 R.layout.all_apps_paged_view_application, layout, false);
643 icon.applyFromApplicationInfo(info, mPageViewIconCache);
644 icon.setOnLongClickListener(this);
645
646 final int index = i - startIndex;
647 final int x = index % mCellCountX;
648 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700649 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700650 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700651 }
652 }
653
Winson Chung80baf5a2010-08-09 16:03:15 -0700654 @Override
655 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700656 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700657 switch (mCustomizationType) {
658 case WidgetCustomization:
659 syncWidgetPages();
660 break;
661 case FolderCustomization:
662 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700663 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700664 break;
665 case ShortcutCustomization:
666 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700667 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700668 break;
669 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700670 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700671 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700672 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700673 case ApplicationCustomization:
674 syncAppPages();
675 centerPagedViewCellLayouts = false;
676 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700677 default:
678 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700679 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700680 break;
681 }
682
683 // only try and center the page if there is one page
684 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700685 if (centerPagedViewCellLayouts) {
686 if (childCount == 1) {
687 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
688 layout.enableCenteredContent(true);
689 } else {
690 for (int i = 0; i < childCount; ++i) {
691 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
692 layout.enableCenteredContent(false);
693 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700694 }
695 }
696
697 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700698 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700699 }
700
701 @Override
702 public void syncPageItems(int page) {
703 switch (mCustomizationType) {
704 case WidgetCustomization:
705 syncWidgetPageItems(page);
706 break;
707 case FolderCustomization:
708 syncListPageItems(page, mFolderList);
709 break;
710 case ShortcutCustomization:
711 syncListPageItems(page, mShortcutList);
712 break;
713 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700714 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700715 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700716 case ApplicationCustomization:
717 syncAppPageItems(page);
718 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700719 }
720 }
Winson Chunge3193b92010-09-10 11:44:42 -0700721
722 protected int getAssociatedLowerPageBound(int page) {
723 return 0;
724 }
725 protected int getAssociatedUpperPageBound(int page) {
726 return getChildCount();
727 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700728}