blob: f6cff05ed55b9e338c0b47173fc7c12d00e4d250 [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 Chungd0d43012010-09-26 17:26:45 -070039import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070040import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070041import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070042import android.view.Menu;
43import android.view.MenuItem;
Winson Chunge3193b92010-09-10 11:44:42 -070044import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.view.View;
Winson Chungd0d43012010-09-26 17:26:45 -070046import android.view.ViewGroup;
47import android.widget.Checkable;
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
Michael Jurka0280c3b2010-09-17 15:00:07 -070052import java.util.ArrayList;
53import java.util.Collections;
54import java.util.Comparator;
55import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070056
57public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070058 implements View.OnLongClickListener, View.OnClickListener,
Winson Chungd0d43012010-09-26 17:26:45 -070059 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070060
61 public enum CustomizationType {
62 WidgetCustomization,
63 FolderCustomization,
64 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070065 WallpaperCustomization,
66 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070067 }
68
Winson Chunge3193b92010-09-10 11:44:42 -070069 /**
70 * The linear layout used strictly for the widget tab of the customization tray
71 */
72 private class WidgetLayout extends LinearLayout {
73 public WidgetLayout(Context context) {
74 super(context);
75 }
76
77 @Override
78 public boolean onTouchEvent(MotionEvent event) {
79 // We eat up the touch events here, since the PagedView (which uses the same swiping
80 // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
81 // the user is scrolling between pages. This means that if the pages themselves don't
82 // handle touch events, it gets forwarded up to PagedView itself, and it's own
83 // onTouchEvent() handling will prevent further intercept touch events from being called
84 // (it's the same view in that case). This is not ideal, but to prevent more changes,
85 // we just always mark the touch event as handled.
86 return super.onTouchEvent(event) || true;
87 }
88 }
89
Winson Chung80baf5a2010-08-09 16:03:15 -070090 private static final String TAG = "CustomizeWorkspace";
91 private static final boolean DEBUG = false;
92
93 private Launcher mLauncher;
94 private DragController mDragController;
95 private PackageManager mPackageManager;
96
97 private CustomizationType mCustomizationType;
98
Winson Chunge3193b92010-09-10 11:44:42 -070099 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
100 private PagedViewCellLayout mWorkspaceWidgetLayout;
101
102 // The mapping between the pages and the widgets that will be laid out on them
103 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
104
105 // The max dimensions for the ImageView we use for displaying the widget
106 private int mMaxWidgetWidth;
107
108 // The max number of widget cells to take a "page" of widget
109 private int mMaxWidgetsCellHSpan;
110
111 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 private List<AppWidgetProviderInfo> mWidgetList;
113 private List<ResolveInfo> mFolderList;
114 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700115 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700116 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700117
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700118 private int mCellCountX;
119 private int mCellCountY;
120 private int mPageLayoutPaddingTop;
121 private int mPageLayoutPaddingBottom;
122 private int mPageLayoutPaddingLeft;
123 private int mPageLayoutPaddingRight;
Winson Chunge3193b92010-09-10 11:44:42 -0700124 private static final int sMinWidgetCellHSpan = 2;
125 private static final int sMaxWidgetCellHSpan = 4;
126
127 // The scale factor for widget previews inside the widget drawer
128 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700129
130 private final Canvas mCanvas = new Canvas();
131 private final LayoutInflater mInflater;
132
133 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700134 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 }
136
137 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700138 this(context, attrs, 0);
139 }
140
141 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
142 super(context, attrs, defStyle);
143
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700144 TypedArray a;
145 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700146 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700147 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
148 a.recycle();
149 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
150 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
151 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
152 mPageLayoutPaddingTop = a.getDimensionPixelSize(
153 R.styleable.PagedView_pageLayoutPaddingTop, 10);
154 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
155 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
156 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
157 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
158 mPageLayoutPaddingRight = a.getDimensionPixelSize(
159 R.styleable.PagedView_pageLayoutPaddingRight, 10);
160 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700162 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
163 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700165
Winson Chung80baf5a2010-08-09 16:03:15 -0700166 setVisibility(View.GONE);
167 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700168 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 }
170
171 public void setLauncher(Launcher launcher) {
172 Context context = getContext();
173 mLauncher = launcher;
174 mPackageManager = context.getPackageManager();
175 }
176
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700177 /**
178 * Sets the list of applications that launcher has loaded.
179 */
180 public void setApps(ArrayList<ApplicationInfo> list) {
181 mApps = list;
182 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
183 mPageViewIconCache.clear();
184 invalidatePageData();
185 }
186
187 /**
188 * Convenience function to add new items to the set of applications that were previously loaded.
189 * Called by both updateApps() and setApps().
190 */
191 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
192 // we add it in place, in alphabetical order
193 final int count = list.size();
194 for (int i = 0; i < count; ++i) {
195 final ApplicationInfo info = list.get(i);
196 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
197 if (index < 0) {
198 mApps.add(-(index + 1), info);
199 }
200 }
201 }
202
203 /**
204 * Adds new applications to the loaded list, and notifies the paged view to update itself.
205 */
206 public void addApps(ArrayList<ApplicationInfo> list) {
207 addAppsWithoutInvalidate(list);
208 invalidatePageData();
209 }
210
211 /**
212 * Convenience function to remove items to the set of applications that were previously loaded.
213 * Called by both updateApps() and removeApps().
214 */
215 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
216 // loop through all the apps and remove apps that have the same component
217 final int length = list.size();
218 for (int i = 0; i < length; ++i) {
219 final ApplicationInfo info = list.get(i);
220 int removeIndex = findAppByComponent(mApps, info);
221 if (removeIndex > -1) {
222 mApps.remove(removeIndex);
223 mPageViewIconCache.removeOutline(info);
224 }
225 }
226 }
227
228 /**
229 * Removes applications from the loaded list, and notifies the paged view to update itself.
230 */
231 public void removeApps(ArrayList<ApplicationInfo> list) {
232 removeAppsWithoutInvalidate(list);
233 invalidatePageData();
234 }
235
236 /**
237 * Updates a set of applications from the loaded list, and notifies the paged view to update
238 * itself.
239 */
240 public void updateApps(ArrayList<ApplicationInfo> list) {
241 // We remove and re-add the updated applications list because it's properties may have
242 // changed (ie. the title), and this will ensure that the items will be in their proper
243 // place in the list.
244 removeAppsWithoutInvalidate(list);
245 addAppsWithoutInvalidate(list);
246 invalidatePageData();
247 }
248
249 /**
250 * Convenience function to find matching ApplicationInfos for removal.
251 */
252 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
253 ComponentName removeComponent = item.intent.getComponent();
254 final int length = list.size();
255 for (int i = 0; i < length; ++i) {
256 ApplicationInfo info = list.get(i);
257 if (info.intent.getComponent().equals(removeComponent)) {
258 return i;
259 }
260 }
261 return -1;
262 }
263
Winson Chung80baf5a2010-08-09 16:03:15 -0700264 public void update() {
265 Context context = getContext();
266
267 // get the list of widgets
268 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
269 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
270 @Override
271 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
272 return object1.label.compareTo(object2.label);
273 }
274 });
275
276 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
277 @Override
278 public int compare(ResolveInfo object1, ResolveInfo object2) {
279 return object1.loadLabel(mPackageManager).toString().compareTo(
280 object2.loadLabel(mPackageManager).toString());
281 }
282 };
283
284 // get the list of live folder intents
285 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
286 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
287
288 // manually create a separate entry for creating a folder in Launcher
289 ResolveInfo folder = new ResolveInfo();
290 folder.icon = R.drawable.ic_launcher_folder;
291 folder.labelRes = R.string.group_folder;
292 folder.resolvePackageName = context.getPackageName();
293 mFolderList.add(0, folder);
294 Collections.sort(mFolderList, resolveInfoComparator);
295
296 // get the list of shortcuts
297 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
298 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
299 Collections.sort(mShortcutList, resolveInfoComparator);
300
Winson Chunge8878e32010-09-15 20:37:09 -0700301 // get the list of wallpapers
302 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
303 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
304 Collections.sort(mWallpaperList, resolveInfoComparator);
305
Winson Chung241c3b42010-08-25 16:53:03 -0700306 // reset the icon cache
307 mPageViewIconCache.clear();
308
Winson Chunge3193b92010-09-10 11:44:42 -0700309 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 invalidatePageData();
311 }
312
313 public void setDragController(DragController dragger) {
314 mDragController = dragger;
315 }
316
317 public void setCustomizationFilter(CustomizationType filterType) {
318 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700319 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700321
322 // End the current choice mode so that we don't carry selections across tabs
323 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 }
325
326 @Override
327 public void onDropCompleted(View target, boolean success) {
328 // do nothing
329 }
330
331 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700332 public void onClick(View v) {
333 if (!v.isInTouchMode()) {
334 return;
335 }
336
Winson Chungd0d43012010-09-26 17:26:45 -0700337 // On certain pages, we allow single tap to mark items as selected so that they can be
338 // dropped onto the mini workspaces
339 switch (mCustomizationType) {
340 case WidgetCustomization:
341 case ApplicationCustomization:
342 case FolderCustomization:
343 case ShortcutCustomization:
344 if (isChoiceMode(CHOICE_MODE_NONE)) {
345 startChoiceMode(CHOICE_MODE_SINGLE, this);
346 }
347
348 if (v instanceof Checkable) {
349 final Checkable c = (Checkable) v;
350 final boolean wasChecked = c.isChecked();
351 resetCheckedGrandchildren();
352 c.setChecked(!wasChecked);
353
354 // End the current choice mode when we have no items selected
355 if (!c.isChecked()) {
356 endChoiceMode();
357 }
358 }
359 return;
360 default:
361 break;
362 }
363
364 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700365 switch (mCustomizationType) {
366 case WallpaperCustomization:
367 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700368 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700369 animateClickFeedback(v, new Runnable() {
370 @Override
371 public void run() {
372 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700373 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700374 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
375 ComponentName name = new ComponentName(info.activityInfo.packageName,
376 info.activityInfo.name);
377 createWallpapersIntent.setComponent(name);
378 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700379 }
380 });
Winson Chungd0d43012010-09-26 17:26:45 -0700381 break;
382 default:
383 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700384 }
385 }
386
387 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 public boolean onLongClick(View v) {
389 if (!v.isInTouchMode()) {
390 return false;
391 }
392
Winson Chungd0d43012010-09-26 17:26:45 -0700393 // End the current choice mode before we start dragging anything
394 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
395 endChoiceMode();
396 }
397
398 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700399 switch (mCustomizationType) {
400 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700401 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700402 final LinearLayout l = (LinearLayout) v;
403 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700404 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
405 Bitmap.Config.ARGB_8888);
406 Canvas c = new Canvas(b);
407 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700408
Winson Chungd0d43012010-09-26 17:26:45 -0700409 createItemInfo = (PendingAddItemInfo) v.getTag();
410 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
411 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700412
413 // Cleanup the icon
414 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700415 return true;
416 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700417 if (v.getTag() instanceof UserFolderInfo) {
418 // The UserFolderInfo tag is only really used for live folders
419 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700420 mDragController.startDrag(
421 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
422 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700423 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700424 mDragController.startDrag(
425 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
426 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 return true;
428 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700429 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700430 mDragController.startDrag(
431 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700432 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700433 case ApplicationCustomization:
434 // Pick up the application for dropping
435 ApplicationInfo app = (ApplicationInfo) v.getTag();
436 app = new ApplicationInfo(app);
437
438 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
439 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700440 }
441 return false;
442 }
443
Winson Chunge3193b92010-09-10 11:44:42 -0700444 /**
445 * Pre-processes the layout of the different widget pages.
446 * @return the number of pages of widgets that we have
447 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700448 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700449 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700450
Winson Chunge3193b92010-09-10 11:44:42 -0700451 // create a new page for the first set of widgets
452 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700454 mWidgetPages.add(newPage);
455
456 // do this until we have no more widgets to lay out
457 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
458 final int widgetCount = mWidgetList.size();
459 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700460 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700461 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700462
Winson Chunge3193b92010-09-10 11:44:42 -0700463 // determine the size of the current widget
464 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
465 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700466
Winson Chunge3193b92010-09-10 11:44:42 -0700467 // create a new page if necessary
468 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
469 numCellsInRow = 0;
470 newPage = new ArrayList<AppWidgetProviderInfo>();
471 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700472 }
473
Winson Chunge3193b92010-09-10 11:44:42 -0700474 // add the item to the current page
475 newPage.add(info);
476 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700477 }
Winson Chunge3193b92010-09-10 11:44:42 -0700478
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 return mWidgetPages.size();
480 }
481
Winson Chunge3193b92010-09-10 11:44:42 -0700482 /**
483 * This method will extract the preview image specified by the widget developer (if it exists),
484 * otherwise, it will try to generate a default image preview with the widget's package icon.
485 * @return the drawable will be used and sized in the ImageView to represent the widget
486 */
487 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700488 PackageManager packageManager = mLauncher.getPackageManager();
489 String packageName = info.provider.getPackageName();
490 Drawable drawable = null;
491 if (info.previewImage != 0) {
492 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
493 if (drawable == null) {
494 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
495 + " for provider: " + info.provider);
496 } else {
497 return drawable;
498 }
499 }
500
501 // If we don't have a preview image, create a default one
502 if (drawable == null) {
503 Resources resources = mLauncher.getResources();
504
Winson Chung80baf5a2010-08-09 16:03:15 -0700505 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700506 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
507 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
508 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
509 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
511 mCanvas.setBitmap(bitmap);
512 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
513 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
514
515 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
516 background.setBounds(0, 0, width, height);
517 background.draw(mCanvas);
518
519 // Draw the icon vertically centered, flush left
520 try {
521 Rect tmpRect = new Rect();
522 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700523 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700524 icon = packageManager.getDrawable(packageName, info.icon, null);
525 } else {
526 icon = resources.getDrawable(R.drawable.ic_launcher_application);
527 }
528 background.getPadding(tmpRect);
529
Winson Chunge3193b92010-09-10 11:44:42 -0700530 final int iconSize = minDim / 2;
531 final int offset = iconSize / 4;
532 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700533 icon.draw(mCanvas);
534 } catch (Resources.NotFoundException e) {
535 // if we can't find the icon, then just don't draw it
536 }
537
Winson Chungb3347bb2010-08-19 14:51:28 -0700538 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700539 }
540 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
541 return drawable;
542 }
543
544 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700545 layout.setCellCount(mCellCountX, mCellCountY);
546 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
547 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700548 }
549
Winson Chunge3193b92010-09-10 11:44:42 -0700550 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700551 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700552 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
553
554 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
555 }
556
Winson Chung80baf5a2010-08-09 16:03:15 -0700557 private void syncWidgetPages() {
558 if (mWidgetList == null) return;
559
Winson Chunge3193b92010-09-10 11:44:42 -0700560 // we need to repopulate with the LinearLayout layout for the widget pages
561 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700562 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700563 for (int i = 0; i < numPages; ++i) {
564 LinearLayout layout = new WidgetLayout(getContext());
565 layout.setGravity(Gravity.CENTER_HORIZONTAL);
566
567 // Temporary change to prevent the last page from being too small (and items bleeding
568 // onto it). We can remove this once we properly fix the fading algorithm
569 if (i < numPages - 1) {
570 addView(layout, new LinearLayout.LayoutParams(
571 LinearLayout.LayoutParams.WRAP_CONTENT,
572 LinearLayout.LayoutParams.MATCH_PARENT));
573 } else {
574 addView(layout, new LinearLayout.LayoutParams(
575 LinearLayout.LayoutParams.MATCH_PARENT,
576 LinearLayout.LayoutParams.MATCH_PARENT));
577 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 }
579 }
580
581 private void syncWidgetPageItems(int page) {
582 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700583 LinearLayout layout = (LinearLayout) getChildAt(page);
584 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700585 final int count = list.size();
586 layout.removeAllViews();
587 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700588 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700589 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
590 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
591 createItemInfo.componentName = info.provider;
592
Winson Chunge3193b92010-09-10 11:44:42 -0700593 LinearLayout l = (LinearLayout) mInflater.inflate(
594 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700595 l.setTag(createItemInfo);
596 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700597 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700598
Winson Chunge3193b92010-09-10 11:44:42 -0700599 final Drawable icon = getWidgetIcon(info);
600 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
601 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
602
Winson Chungd0d43012010-09-26 17:26:45 -0700603 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700604 image.setMaxWidth(mMaxWidgetWidth);
605 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700607 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700608 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700609 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700610
611 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 }
613 }
614
615 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700616 // we need to repopulate with PagedViewCellLayouts
617 removeAllViews();
618
Winson Chung80baf5a2010-08-09 16:03:15 -0700619 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700620 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700621 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700622 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
623 setupPage(layout);
624 addView(layout);
625 }
626 }
627
628 private void syncListPageItems(int page, List<ResolveInfo> list) {
629 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700630 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700631 int startIndex = page * numCells;
632 int endIndex = Math.min(startIndex + numCells, list.size());
633 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
634 // TODO: we can optimize by just re-applying to existing views
635 layout.removeAllViews();
636 for (int i = startIndex; i < endIndex; ++i) {
637 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700638 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
639
Winson Chung241c3b42010-08-25 16:53:03 -0700640 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
641 R.layout.customize_paged_view_item, layout, false);
642 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700643 switch (mCustomizationType) {
644 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700645 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700646 break;
647 case FolderCustomization:
648 if (info.labelRes != R.string.group_folder) {
649 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
650 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
651 info.activityInfo.name);
652 icon.setTag(createItemInfo);
653 } else {
654 UserFolderInfo folderInfo = new UserFolderInfo();
655 folderInfo.title = getResources().getText(R.string.folder_name);
656 icon.setTag(folderInfo);
657 }
658 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700659 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700660 break;
661 case ShortcutCustomization:
662 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
663 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
664 info.activityInfo.name);
665 icon.setTag(createItemInfo);
666 icon.setOnClickListener(this);
667 icon.setOnLongClickListener(this);
668 break;
669 default:
670 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700671 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700672
673 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700674 final int x = index % mCellCountX;
675 final int y = index / mCellCountX;
676 setupPage(layout);
677 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
678 }
679 }
680
681 private void syncAppPages() {
682 if (mApps == null) return;
683
684 // We need to repopulate with PagedViewCellLayouts
685 removeAllViews();
686
687 // Ensure that we have the right number of pages
688 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
689 for (int i = 0; i < numPages; ++i) {
690 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
691 setupPage(layout);
692 addView(layout);
693 }
694 }
695
696 private void syncAppPageItems(int page) {
697 if (mApps == null) return;
698
699 // ensure that we have the right number of items on the pages
700 int numCells = mCellCountX * mCellCountY;
701 int startIndex = page * numCells;
702 int endIndex = Math.min(startIndex + numCells, mApps.size());
703 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
704 // TODO: we can optimize by just re-applying to existing views
705 layout.removeAllViews();
706 for (int i = startIndex; i < endIndex; ++i) {
707 final ApplicationInfo info = mApps.get(i);
708 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
709 R.layout.all_apps_paged_view_application, layout, false);
710 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700711 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700712 icon.setOnLongClickListener(this);
713
714 final int index = i - startIndex;
715 final int x = index % mCellCountX;
716 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700717 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700718 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700719 }
720 }
721
Winson Chung80baf5a2010-08-09 16:03:15 -0700722 @Override
723 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700724 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700725 switch (mCustomizationType) {
726 case WidgetCustomization:
727 syncWidgetPages();
728 break;
729 case FolderCustomization:
730 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700731 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700732 break;
733 case ShortcutCustomization:
734 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700735 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700736 break;
737 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700738 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700739 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700740 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700741 case ApplicationCustomization:
742 syncAppPages();
743 centerPagedViewCellLayouts = false;
744 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700745 default:
746 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700747 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700748 break;
749 }
750
751 // only try and center the page if there is one page
752 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700753 if (centerPagedViewCellLayouts) {
754 if (childCount == 1) {
755 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
756 layout.enableCenteredContent(true);
757 } else {
758 for (int i = 0; i < childCount; ++i) {
759 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
760 layout.enableCenteredContent(false);
761 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700762 }
763 }
764
765 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700766 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700767 }
768
769 @Override
770 public void syncPageItems(int page) {
771 switch (mCustomizationType) {
772 case WidgetCustomization:
773 syncWidgetPageItems(page);
774 break;
775 case FolderCustomization:
776 syncListPageItems(page, mFolderList);
777 break;
778 case ShortcutCustomization:
779 syncListPageItems(page, mShortcutList);
780 break;
781 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700782 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700783 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700784 case ApplicationCustomization:
785 syncAppPageItems(page);
786 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700787 }
788 }
Winson Chunge3193b92010-09-10 11:44:42 -0700789
Winson Chungd0d43012010-09-26 17:26:45 -0700790 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700791 protected int getAssociatedLowerPageBound(int page) {
792 return 0;
793 }
Winson Chungd0d43012010-09-26 17:26:45 -0700794 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700795 protected int getAssociatedUpperPageBound(int page) {
796 return getChildCount();
797 }
Winson Chungd0d43012010-09-26 17:26:45 -0700798
799 @Override
800 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
801 mode.setTitle(R.string.cab_selection_text);
802 return true;
803 }
804
805 @Override
806 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
807 return true;
808 }
809
810 @Override
811 public void onDestroyActionMode(ActionMode mode) {
812 endChoiceMode();
813 }
814
815 @Override
816 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
817 return false;
818 }
819
Winson Chung80baf5a2010-08-09 16:03:15 -0700820}