blob: f3de74b360b75075fca8677536d917dc6807642a [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 Chunge3193b92010-09-10 11:44:42 -0700118 private static final int sMinWidgetCellHSpan = 2;
119 private static final int sMaxWidgetCellHSpan = 4;
120
Michael Jurka3125d9d2010-09-27 11:30:20 -0700121 private int mChoiceModeTitleText;
122
Winson Chunge3193b92010-09-10 11:44:42 -0700123 // The scale factor for widget previews inside the widget drawer
124 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700125
126 private final Canvas mCanvas = new Canvas();
127 private final LayoutInflater mInflater;
128
129 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700130 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 }
132
133 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700134 this(context, attrs, 0);
135 }
136
137 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
139
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700140 TypedArray a;
141 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700142 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700143 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
144 a.recycle();
145 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
146 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
147 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700148
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700149 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700150 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700151 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
152 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700153 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700154
Winson Chung80baf5a2010-08-09 16:03:15 -0700155 setVisibility(View.GONE);
156 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700157 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 }
159
160 public void setLauncher(Launcher launcher) {
161 Context context = getContext();
162 mLauncher = launcher;
163 mPackageManager = context.getPackageManager();
164 }
165
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700166 /**
167 * Sets the list of applications that launcher has loaded.
168 */
169 public void setApps(ArrayList<ApplicationInfo> list) {
170 mApps = list;
171 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700172
173 // Update the widgets/shortcuts to reflect changes in the set of available apps
174 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700175 }
176
177 /**
178 * Convenience function to add new items to the set of applications that were previously loaded.
179 * Called by both updateApps() and setApps().
180 */
181 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
182 // we add it in place, in alphabetical order
183 final int count = list.size();
184 for (int i = 0; i < count; ++i) {
185 final ApplicationInfo info = list.get(i);
186 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
187 if (index < 0) {
188 mApps.add(-(index + 1), info);
189 }
190 }
191 }
192
193 /**
194 * Adds new applications to the loaded list, and notifies the paged view to update itself.
195 */
196 public void addApps(ArrayList<ApplicationInfo> list) {
197 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700198
199 // Update the widgets/shortcuts to reflect changes in the set of available apps
200 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700201 }
202
203 /**
204 * Convenience function to remove items to the set of applications that were previously loaded.
205 * Called by both updateApps() and removeApps().
206 */
207 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
208 // loop through all the apps and remove apps that have the same component
209 final int length = list.size();
210 for (int i = 0; i < length; ++i) {
211 final ApplicationInfo info = list.get(i);
212 int removeIndex = findAppByComponent(mApps, info);
213 if (removeIndex > -1) {
214 mApps.remove(removeIndex);
215 mPageViewIconCache.removeOutline(info);
216 }
217 }
218 }
219
220 /**
221 * Removes applications from the loaded list, and notifies the paged view to update itself.
222 */
223 public void removeApps(ArrayList<ApplicationInfo> list) {
224 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700225
226 // Update the widgets/shortcuts to reflect changes in the set of available apps
227 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700228 }
229
230 /**
231 * Updates a set of applications from the loaded list, and notifies the paged view to update
232 * itself.
233 */
234 public void updateApps(ArrayList<ApplicationInfo> list) {
235 // We remove and re-add the updated applications list because it's properties may have
236 // changed (ie. the title), and this will ensure that the items will be in their proper
237 // place in the list.
238 removeAppsWithoutInvalidate(list);
239 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700240
241 // Update the widgets/shortcuts to reflect changes in the set of available apps
242 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700243 }
244
245 /**
246 * Convenience function to find matching ApplicationInfos for removal.
247 */
248 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
249 ComponentName removeComponent = item.intent.getComponent();
250 final int length = list.size();
251 for (int i = 0; i < length; ++i) {
252 ApplicationInfo info = list.get(i);
253 if (info.intent.getComponent().equals(removeComponent)) {
254 return i;
255 }
256 }
257 return -1;
258 }
259
Winson Chung80baf5a2010-08-09 16:03:15 -0700260 public void update() {
261 Context context = getContext();
262
263 // get the list of widgets
264 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
265 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
266 @Override
267 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
268 return object1.label.compareTo(object2.label);
269 }
270 });
271
272 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
273 @Override
274 public int compare(ResolveInfo object1, ResolveInfo object2) {
275 return object1.loadLabel(mPackageManager).toString().compareTo(
276 object2.loadLabel(mPackageManager).toString());
277 }
278 };
279
280 // get the list of live folder intents
281 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
282 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
283
284 // manually create a separate entry for creating a folder in Launcher
285 ResolveInfo folder = new ResolveInfo();
286 folder.icon = R.drawable.ic_launcher_folder;
287 folder.labelRes = R.string.group_folder;
288 folder.resolvePackageName = context.getPackageName();
289 mFolderList.add(0, folder);
290 Collections.sort(mFolderList, resolveInfoComparator);
291
292 // get the list of shortcuts
293 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
294 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
295 Collections.sort(mShortcutList, resolveInfoComparator);
296
Winson Chunge8878e32010-09-15 20:37:09 -0700297 // get the list of wallpapers
298 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
299 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
300 Collections.sort(mWallpaperList, resolveInfoComparator);
301
Winson Chung241c3b42010-08-25 16:53:03 -0700302 // reset the icon cache
303 mPageViewIconCache.clear();
304
Winson Chunge3193b92010-09-10 11:44:42 -0700305 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700306 invalidatePageData();
307 }
308
309 public void setDragController(DragController dragger) {
310 mDragController = dragger;
311 }
312
313 public void setCustomizationFilter(CustomizationType filterType) {
314 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700315 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700317
318 // End the current choice mode so that we don't carry selections across tabs
319 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 }
321
322 @Override
323 public void onDropCompleted(View target, boolean success) {
324 // do nothing
325 }
326
327 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700328 public void onClick(View v) {
329 if (!v.isInTouchMode()) {
330 return;
331 }
332
Winson Chungd0d43012010-09-26 17:26:45 -0700333 // On certain pages, we allow single tap to mark items as selected so that they can be
334 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700335 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700336 switch (mCustomizationType) {
337 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700338 mChoiceModeTitleText = R.string.cab_widget_selection_text;
339 enterChoiceMode = true;
340 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700341 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700342 mChoiceModeTitleText = R.string.cab_app_selection_text;
343 enterChoiceMode = true;
344 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700345 case FolderCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700346 mChoiceModeTitleText = R.string.cab_folder_selection_text;
347 enterChoiceMode = true;
348 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700349 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700350 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
351 enterChoiceMode = true;
352 break;
353 default:
354 break;
355 }
Winson Chungd0d43012010-09-26 17:26:45 -0700356
Michael Jurka3125d9d2010-09-27 11:30:20 -0700357 if (enterChoiceMode) {
Winson Chungd0d43012010-09-26 17:26:45 -0700358 if (v instanceof Checkable) {
359 final Checkable c = (Checkable) v;
360 final boolean wasChecked = c.isChecked();
361 resetCheckedGrandchildren();
362 c.setChecked(!wasChecked);
363
364 // End the current choice mode when we have no items selected
Michael Jurkae17e19c2010-09-28 11:01:39 -0700365 /*if (!c.isChecked()) {
Winson Chungd0d43012010-09-26 17:26:45 -0700366 endChoiceMode();
Michael Jurka3125d9d2010-09-27 11:30:20 -0700367 } else if (isChoiceMode(CHOICE_MODE_NONE)) {
368 endChoiceMode();
369 startChoiceMode(CHOICE_MODE_SINGLE, this);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700370 }*/
371 mChoiceMode = CHOICE_MODE_SINGLE;
372
373 Workspace w = mLauncher.getWorkspace();
374 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
375 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
376 cl.setHover(true);
377
378 animateClickFeedback(v, new Runnable() {
379 @Override
380 public void run() {
381 cl.setHover(false);
382 mLauncher.onWorkspaceClick(cl);
383 mChoiceMode = CHOICE_MODE_NONE;
384 }
385 });
Winson Chungd0d43012010-09-26 17:26:45 -0700386 }
387 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700388 }
389
390 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700391 switch (mCustomizationType) {
392 case WallpaperCustomization:
393 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700394 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700395 animateClickFeedback(v, new Runnable() {
396 @Override
397 public void run() {
398 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700399 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700400 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
401 ComponentName name = new ComponentName(info.activityInfo.packageName,
402 info.activityInfo.name);
403 createWallpapersIntent.setComponent(name);
404 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700405 }
406 });
Winson Chungd0d43012010-09-26 17:26:45 -0700407 break;
408 default:
409 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700410 }
411 }
412
413 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 public boolean onLongClick(View v) {
415 if (!v.isInTouchMode()) {
416 return false;
417 }
418
Winson Chungd0d43012010-09-26 17:26:45 -0700419 // End the current choice mode before we start dragging anything
420 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
421 endChoiceMode();
422 }
423
424 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700425 switch (mCustomizationType) {
426 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700427 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700428 final LinearLayout l = (LinearLayout) v;
429 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700430 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
431 Bitmap.Config.ARGB_8888);
432 Canvas c = new Canvas(b);
433 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700434
Winson Chungd0d43012010-09-26 17:26:45 -0700435 createItemInfo = (PendingAddItemInfo) v.getTag();
436 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
437 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700438
439 // Cleanup the icon
440 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700441 return true;
442 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700443 if (v.getTag() instanceof UserFolderInfo) {
444 // The UserFolderInfo tag is only really used for live folders
445 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700446 mDragController.startDrag(
447 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
448 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700449 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700450 mDragController.startDrag(
451 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
452 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 return true;
454 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700455 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700456 mDragController.startDrag(
457 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700458 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700459 case ApplicationCustomization:
460 // Pick up the application for dropping
461 ApplicationInfo app = (ApplicationInfo) v.getTag();
462 app = new ApplicationInfo(app);
463
464 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
465 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700466 }
467 return false;
468 }
469
Winson Chunge3193b92010-09-10 11:44:42 -0700470 /**
471 * Pre-processes the layout of the different widget pages.
472 * @return the number of pages of widgets that we have
473 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700474 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700475 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700476
Winson Chunge3193b92010-09-10 11:44:42 -0700477 // create a new page for the first set of widgets
478 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700480 mWidgetPages.add(newPage);
481
482 // do this until we have no more widgets to lay out
483 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
484 final int widgetCount = mWidgetList.size();
485 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700486 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700487 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700488
Winson Chunge3193b92010-09-10 11:44:42 -0700489 // determine the size of the current widget
490 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
491 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700492
Winson Chunge3193b92010-09-10 11:44:42 -0700493 // create a new page if necessary
494 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
495 numCellsInRow = 0;
496 newPage = new ArrayList<AppWidgetProviderInfo>();
497 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 }
499
Winson Chunge3193b92010-09-10 11:44:42 -0700500 // add the item to the current page
501 newPage.add(info);
502 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700503 }
Winson Chunge3193b92010-09-10 11:44:42 -0700504
Winson Chung80baf5a2010-08-09 16:03:15 -0700505 return mWidgetPages.size();
506 }
507
Winson Chunge3193b92010-09-10 11:44:42 -0700508 /**
509 * This method will extract the preview image specified by the widget developer (if it exists),
510 * otherwise, it will try to generate a default image preview with the widget's package icon.
511 * @return the drawable will be used and sized in the ImageView to represent the widget
512 */
513 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700514 PackageManager packageManager = mLauncher.getPackageManager();
515 String packageName = info.provider.getPackageName();
516 Drawable drawable = null;
517 if (info.previewImage != 0) {
518 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
519 if (drawable == null) {
520 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
521 + " for provider: " + info.provider);
522 } else {
523 return drawable;
524 }
525 }
526
527 // If we don't have a preview image, create a default one
528 if (drawable == null) {
529 Resources resources = mLauncher.getResources();
530
Winson Chung80baf5a2010-08-09 16:03:15 -0700531 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700532 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
533 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
534 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
535 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700536 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
537 mCanvas.setBitmap(bitmap);
538 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
539 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
540
541 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
542 background.setBounds(0, 0, width, height);
543 background.draw(mCanvas);
544
545 // Draw the icon vertically centered, flush left
546 try {
547 Rect tmpRect = new Rect();
548 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700549 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700551 }
552 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700553 icon = resources.getDrawable(R.drawable.ic_launcher_application);
554 }
555 background.getPadding(tmpRect);
556
Winson Chunge3193b92010-09-10 11:44:42 -0700557 final int iconSize = minDim / 2;
558 final int offset = iconSize / 4;
559 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700560 icon.draw(mCanvas);
561 } catch (Resources.NotFoundException e) {
562 // if we can't find the icon, then just don't draw it
563 }
564
Winson Chungb3347bb2010-08-19 14:51:28 -0700565 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700566 }
567 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
568 return drawable;
569 }
570
571 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700572 layout.setCellCount(mCellCountX, mCellCountY);
573 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
574 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700575 }
576
Winson Chunge3193b92010-09-10 11:44:42 -0700577 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700578 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700579 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
580
581 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
582 }
583
Winson Chung80baf5a2010-08-09 16:03:15 -0700584 private void syncWidgetPages() {
585 if (mWidgetList == null) return;
586
Winson Chunge3193b92010-09-10 11:44:42 -0700587 // we need to repopulate with the LinearLayout layout for the widget pages
588 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700589 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700590 for (int i = 0; i < numPages; ++i) {
591 LinearLayout layout = new WidgetLayout(getContext());
592 layout.setGravity(Gravity.CENTER_HORIZONTAL);
593
594 // Temporary change to prevent the last page from being too small (and items bleeding
595 // onto it). We can remove this once we properly fix the fading algorithm
596 if (i < numPages - 1) {
597 addView(layout, new LinearLayout.LayoutParams(
598 LinearLayout.LayoutParams.WRAP_CONTENT,
599 LinearLayout.LayoutParams.MATCH_PARENT));
600 } else {
601 addView(layout, new LinearLayout.LayoutParams(
602 LinearLayout.LayoutParams.MATCH_PARENT,
603 LinearLayout.LayoutParams.MATCH_PARENT));
604 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700605 }
606 }
607
608 private void syncWidgetPageItems(int page) {
609 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700610 LinearLayout layout = (LinearLayout) getChildAt(page);
611 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 final int count = list.size();
613 layout.removeAllViews();
614 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700615 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700616 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
617 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
618 createItemInfo.componentName = info.provider;
619
Winson Chunge3193b92010-09-10 11:44:42 -0700620 LinearLayout l = (LinearLayout) mInflater.inflate(
621 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700622 l.setTag(createItemInfo);
623 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700624 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700625
Winson Chunge3193b92010-09-10 11:44:42 -0700626 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700627
628 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
629 final int hSpan = spans[0];
630 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700631
Winson Chungd0d43012010-09-26 17:26:45 -0700632 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700633 image.setMaxWidth(mMaxWidgetWidth);
634 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700635 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700636 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700637 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700638 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700639
640 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700641 }
642 }
643
644 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700645 // we need to repopulate with PagedViewCellLayouts
646 removeAllViews();
647
Winson Chung80baf5a2010-08-09 16:03:15 -0700648 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700649 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700650 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700651 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
652 setupPage(layout);
653 addView(layout);
654 }
655 }
656
657 private void syncListPageItems(int page, List<ResolveInfo> list) {
658 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700659 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700660 int startIndex = page * numCells;
661 int endIndex = Math.min(startIndex + numCells, list.size());
662 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
663 // TODO: we can optimize by just re-applying to existing views
664 layout.removeAllViews();
665 for (int i = startIndex; i < endIndex; ++i) {
666 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700667 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
668
Winson Chung241c3b42010-08-25 16:53:03 -0700669 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
670 R.layout.customize_paged_view_item, layout, false);
671 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700672 switch (mCustomizationType) {
673 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700674 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700675 break;
676 case FolderCustomization:
677 if (info.labelRes != R.string.group_folder) {
678 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
679 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
680 info.activityInfo.name);
681 icon.setTag(createItemInfo);
682 } else {
683 UserFolderInfo folderInfo = new UserFolderInfo();
684 folderInfo.title = getResources().getText(R.string.folder_name);
685 icon.setTag(folderInfo);
686 }
687 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700688 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700689 break;
690 case ShortcutCustomization:
691 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
692 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
693 info.activityInfo.name);
694 icon.setTag(createItemInfo);
695 icon.setOnClickListener(this);
696 icon.setOnLongClickListener(this);
697 break;
698 default:
699 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700700 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700701
702 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700703 final int x = index % mCellCountX;
704 final int y = index / mCellCountX;
705 setupPage(layout);
706 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
707 }
708 }
709
710 private void syncAppPages() {
711 if (mApps == null) return;
712
713 // We need to repopulate with PagedViewCellLayouts
714 removeAllViews();
715
716 // Ensure that we have the right number of pages
717 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
718 for (int i = 0; i < numPages; ++i) {
719 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
720 setupPage(layout);
721 addView(layout);
722 }
723 }
724
725 private void syncAppPageItems(int page) {
726 if (mApps == null) return;
727
728 // ensure that we have the right number of items on the pages
729 int numCells = mCellCountX * mCellCountY;
730 int startIndex = page * numCells;
731 int endIndex = Math.min(startIndex + numCells, mApps.size());
732 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
733 // TODO: we can optimize by just re-applying to existing views
734 layout.removeAllViews();
735 for (int i = startIndex; i < endIndex; ++i) {
736 final ApplicationInfo info = mApps.get(i);
737 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
738 R.layout.all_apps_paged_view_application, layout, false);
739 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700740 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700741 icon.setOnLongClickListener(this);
742
743 final int index = i - startIndex;
744 final int x = index % mCellCountX;
745 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700746 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700747 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700748 }
749 }
750
Winson Chung80baf5a2010-08-09 16:03:15 -0700751 @Override
752 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700753 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700754 switch (mCustomizationType) {
755 case WidgetCustomization:
756 syncWidgetPages();
757 break;
758 case FolderCustomization:
759 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700760 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700761 break;
762 case ShortcutCustomization:
763 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700764 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700765 break;
766 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700767 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700768 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700769 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700770 case ApplicationCustomization:
771 syncAppPages();
772 centerPagedViewCellLayouts = false;
773 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700774 default:
775 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700776 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700777 break;
778 }
779
780 // only try and center the page if there is one page
781 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700782 if (centerPagedViewCellLayouts) {
783 if (childCount == 1) {
784 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
785 layout.enableCenteredContent(true);
786 } else {
787 for (int i = 0; i < childCount; ++i) {
788 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
789 layout.enableCenteredContent(false);
790 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700791 }
792 }
793
794 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700795 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700796 }
797
798 @Override
799 public void syncPageItems(int page) {
800 switch (mCustomizationType) {
801 case WidgetCustomization:
802 syncWidgetPageItems(page);
803 break;
804 case FolderCustomization:
805 syncListPageItems(page, mFolderList);
806 break;
807 case ShortcutCustomization:
808 syncListPageItems(page, mShortcutList);
809 break;
810 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700811 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700812 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700813 case ApplicationCustomization:
814 syncAppPageItems(page);
815 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700816 }
817 }
Winson Chunge3193b92010-09-10 11:44:42 -0700818
Winson Chungd0d43012010-09-26 17:26:45 -0700819 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700820 protected int getAssociatedLowerPageBound(int page) {
821 return 0;
822 }
Winson Chungd0d43012010-09-26 17:26:45 -0700823 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700824 protected int getAssociatedUpperPageBound(int page) {
825 return getChildCount();
826 }
Winson Chungd0d43012010-09-26 17:26:45 -0700827
828 @Override
829 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700830 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700831 return true;
832 }
833
834 @Override
835 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
836 return true;
837 }
838
839 @Override
840 public void onDestroyActionMode(ActionMode mode) {
841 endChoiceMode();
842 }
843
844 @Override
845 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
846 return false;
847 }
848
Winson Chung80baf5a2010-08-09 16:03:15 -0700849}