blob: f55b46e26c917b4ac4b5c3aa09c9a1da93cfd8b5 [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
Michael Jurka3125d9d2010-09-27 11:30:20 -0700127 private int mChoiceModeTitleText;
128
Winson Chunge3193b92010-09-10 11:44:42 -0700129 // The scale factor for widget previews inside the widget drawer
130 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700131
132 private final Canvas mCanvas = new Canvas();
133 private final LayoutInflater mInflater;
134
135 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700136 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700137 }
138
139 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700140 this(context, attrs, 0);
141 }
142
143 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
144 super(context, attrs, defStyle);
145
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700146 TypedArray a;
147 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700148 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700149 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
150 a.recycle();
151 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
152 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
153 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
154 mPageLayoutPaddingTop = a.getDimensionPixelSize(
155 R.styleable.PagedView_pageLayoutPaddingTop, 10);
156 mPageLayoutPaddingBottom = a.getDimensionPixelSize(
157 R.styleable.PagedView_pageLayoutPaddingBottom, 10);
158 mPageLayoutPaddingLeft = a.getDimensionPixelSize(
159 R.styleable.PagedView_pageLayoutPaddingLeft, 10);
160 mPageLayoutPaddingRight = a.getDimensionPixelSize(
161 R.styleable.PagedView_pageLayoutPaddingRight, 10);
162 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700164 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
165 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700166 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700167
Winson Chung80baf5a2010-08-09 16:03:15 -0700168 setVisibility(View.GONE);
169 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700170 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700171 }
172
173 public void setLauncher(Launcher launcher) {
174 Context context = getContext();
175 mLauncher = launcher;
176 mPackageManager = context.getPackageManager();
177 }
178
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700179 /**
180 * Sets the list of applications that launcher has loaded.
181 */
182 public void setApps(ArrayList<ApplicationInfo> list) {
183 mApps = list;
184 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
185 mPageViewIconCache.clear();
186 invalidatePageData();
187 }
188
189 /**
190 * Convenience function to add new items to the set of applications that were previously loaded.
191 * Called by both updateApps() and setApps().
192 */
193 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
194 // we add it in place, in alphabetical order
195 final int count = list.size();
196 for (int i = 0; i < count; ++i) {
197 final ApplicationInfo info = list.get(i);
198 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
199 if (index < 0) {
200 mApps.add(-(index + 1), info);
201 }
202 }
203 }
204
205 /**
206 * Adds new applications to the loaded list, and notifies the paged view to update itself.
207 */
208 public void addApps(ArrayList<ApplicationInfo> list) {
209 addAppsWithoutInvalidate(list);
210 invalidatePageData();
211 }
212
213 /**
214 * Convenience function to remove items to the set of applications that were previously loaded.
215 * Called by both updateApps() and removeApps().
216 */
217 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
218 // loop through all the apps and remove apps that have the same component
219 final int length = list.size();
220 for (int i = 0; i < length; ++i) {
221 final ApplicationInfo info = list.get(i);
222 int removeIndex = findAppByComponent(mApps, info);
223 if (removeIndex > -1) {
224 mApps.remove(removeIndex);
225 mPageViewIconCache.removeOutline(info);
226 }
227 }
228 }
229
230 /**
231 * Removes applications from the loaded list, and notifies the paged view to update itself.
232 */
233 public void removeApps(ArrayList<ApplicationInfo> list) {
234 removeAppsWithoutInvalidate(list);
235 invalidatePageData();
236 }
237
238 /**
239 * Updates a set of applications from the loaded list, and notifies the paged view to update
240 * itself.
241 */
242 public void updateApps(ArrayList<ApplicationInfo> list) {
243 // We remove and re-add the updated applications list because it's properties may have
244 // changed (ie. the title), and this will ensure that the items will be in their proper
245 // place in the list.
246 removeAppsWithoutInvalidate(list);
247 addAppsWithoutInvalidate(list);
248 invalidatePageData();
249 }
250
251 /**
252 * Convenience function to find matching ApplicationInfos for removal.
253 */
254 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
255 ComponentName removeComponent = item.intent.getComponent();
256 final int length = list.size();
257 for (int i = 0; i < length; ++i) {
258 ApplicationInfo info = list.get(i);
259 if (info.intent.getComponent().equals(removeComponent)) {
260 return i;
261 }
262 }
263 return -1;
264 }
265
Winson Chung80baf5a2010-08-09 16:03:15 -0700266 public void update() {
267 Context context = getContext();
268
269 // get the list of widgets
270 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
271 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
272 @Override
273 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
274 return object1.label.compareTo(object2.label);
275 }
276 });
277
278 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
279 @Override
280 public int compare(ResolveInfo object1, ResolveInfo object2) {
281 return object1.loadLabel(mPackageManager).toString().compareTo(
282 object2.loadLabel(mPackageManager).toString());
283 }
284 };
285
286 // get the list of live folder intents
287 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
288 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
289
290 // manually create a separate entry for creating a folder in Launcher
291 ResolveInfo folder = new ResolveInfo();
292 folder.icon = R.drawable.ic_launcher_folder;
293 folder.labelRes = R.string.group_folder;
294 folder.resolvePackageName = context.getPackageName();
295 mFolderList.add(0, folder);
296 Collections.sort(mFolderList, resolveInfoComparator);
297
298 // get the list of shortcuts
299 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
300 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
301 Collections.sort(mShortcutList, resolveInfoComparator);
302
Winson Chunge8878e32010-09-15 20:37:09 -0700303 // get the list of wallpapers
304 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
305 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
306 Collections.sort(mWallpaperList, resolveInfoComparator);
307
Winson Chung241c3b42010-08-25 16:53:03 -0700308 // reset the icon cache
309 mPageViewIconCache.clear();
310
Winson Chunge3193b92010-09-10 11:44:42 -0700311 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700312 invalidatePageData();
313 }
314
315 public void setDragController(DragController dragger) {
316 mDragController = dragger;
317 }
318
319 public void setCustomizationFilter(CustomizationType filterType) {
320 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700321 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700323
324 // End the current choice mode so that we don't carry selections across tabs
325 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700326 }
327
328 @Override
329 public void onDropCompleted(View target, boolean success) {
330 // do nothing
331 }
332
333 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700334 public void onClick(View v) {
335 if (!v.isInTouchMode()) {
336 return;
337 }
338
Winson Chungd0d43012010-09-26 17:26:45 -0700339 // On certain pages, we allow single tap to mark items as selected so that they can be
340 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700341 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700342 switch (mCustomizationType) {
343 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700344 mChoiceModeTitleText = R.string.cab_widget_selection_text;
345 enterChoiceMode = true;
346 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700347 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700348 mChoiceModeTitleText = R.string.cab_app_selection_text;
349 enterChoiceMode = true;
350 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700351 case FolderCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700352 mChoiceModeTitleText = R.string.cab_folder_selection_text;
353 enterChoiceMode = true;
354 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700355 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700356 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
357 enterChoiceMode = true;
358 break;
359 default:
360 break;
361 }
Winson Chungd0d43012010-09-26 17:26:45 -0700362
Michael Jurka3125d9d2010-09-27 11:30:20 -0700363 if (enterChoiceMode) {
Winson Chungd0d43012010-09-26 17:26:45 -0700364 if (v instanceof Checkable) {
365 final Checkable c = (Checkable) v;
366 final boolean wasChecked = c.isChecked();
367 resetCheckedGrandchildren();
368 c.setChecked(!wasChecked);
369
370 // End the current choice mode when we have no items selected
371 if (!c.isChecked()) {
372 endChoiceMode();
Michael Jurka3125d9d2010-09-27 11:30:20 -0700373 } else if (isChoiceMode(CHOICE_MODE_NONE)) {
374 endChoiceMode();
375 startChoiceMode(CHOICE_MODE_SINGLE, this);
Winson Chungd0d43012010-09-26 17:26:45 -0700376 }
377 }
378 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700379 }
380
381 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700382 switch (mCustomizationType) {
383 case WallpaperCustomization:
384 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700385 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700386 animateClickFeedback(v, new Runnable() {
387 @Override
388 public void run() {
389 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700390 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700391 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
392 ComponentName name = new ComponentName(info.activityInfo.packageName,
393 info.activityInfo.name);
394 createWallpapersIntent.setComponent(name);
395 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700396 }
397 });
Winson Chungd0d43012010-09-26 17:26:45 -0700398 break;
399 default:
400 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700401 }
402 }
403
404 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 public boolean onLongClick(View v) {
406 if (!v.isInTouchMode()) {
407 return false;
408 }
409
Winson Chungd0d43012010-09-26 17:26:45 -0700410 // End the current choice mode before we start dragging anything
411 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
412 endChoiceMode();
413 }
414
415 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700416 switch (mCustomizationType) {
417 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700418 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700419 final LinearLayout l = (LinearLayout) v;
420 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700421 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
422 Bitmap.Config.ARGB_8888);
423 Canvas c = new Canvas(b);
424 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700425
Winson Chungd0d43012010-09-26 17:26:45 -0700426 createItemInfo = (PendingAddItemInfo) v.getTag();
427 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
428 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700429
430 // Cleanup the icon
431 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700432 return true;
433 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700434 if (v.getTag() instanceof UserFolderInfo) {
435 // The UserFolderInfo tag is only really used for live folders
436 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700437 mDragController.startDrag(
438 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
439 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700440 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700441 mDragController.startDrag(
442 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
443 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700444 return true;
445 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700446 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700447 mDragController.startDrag(
448 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700449 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700450 case ApplicationCustomization:
451 // Pick up the application for dropping
452 ApplicationInfo app = (ApplicationInfo) v.getTag();
453 app = new ApplicationInfo(app);
454
455 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
456 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700457 }
458 return false;
459 }
460
Winson Chunge3193b92010-09-10 11:44:42 -0700461 /**
462 * Pre-processes the layout of the different widget pages.
463 * @return the number of pages of widgets that we have
464 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700465 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700466 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700467
Winson Chunge3193b92010-09-10 11:44:42 -0700468 // create a new page for the first set of widgets
469 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700470 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700471 mWidgetPages.add(newPage);
472
473 // do this until we have no more widgets to lay out
474 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
475 final int widgetCount = mWidgetList.size();
476 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700477 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700478 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700479
Winson Chunge3193b92010-09-10 11:44:42 -0700480 // determine the size of the current widget
481 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
482 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700483
Winson Chunge3193b92010-09-10 11:44:42 -0700484 // create a new page if necessary
485 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
486 numCellsInRow = 0;
487 newPage = new ArrayList<AppWidgetProviderInfo>();
488 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700489 }
490
Winson Chunge3193b92010-09-10 11:44:42 -0700491 // add the item to the current page
492 newPage.add(info);
493 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700494 }
Winson Chunge3193b92010-09-10 11:44:42 -0700495
Winson Chung80baf5a2010-08-09 16:03:15 -0700496 return mWidgetPages.size();
497 }
498
Winson Chunge3193b92010-09-10 11:44:42 -0700499 /**
500 * This method will extract the preview image specified by the widget developer (if it exists),
501 * otherwise, it will try to generate a default image preview with the widget's package icon.
502 * @return the drawable will be used and sized in the ImageView to represent the widget
503 */
504 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700505 PackageManager packageManager = mLauncher.getPackageManager();
506 String packageName = info.provider.getPackageName();
507 Drawable drawable = null;
508 if (info.previewImage != 0) {
509 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
510 if (drawable == null) {
511 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
512 + " for provider: " + info.provider);
513 } else {
514 return drawable;
515 }
516 }
517
518 // If we don't have a preview image, create a default one
519 if (drawable == null) {
520 Resources resources = mLauncher.getResources();
521
Winson Chung80baf5a2010-08-09 16:03:15 -0700522 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700523 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
524 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
525 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
526 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
528 mCanvas.setBitmap(bitmap);
529 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
530 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
531
532 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
533 background.setBounds(0, 0, width, height);
534 background.draw(mCanvas);
535
536 // Draw the icon vertically centered, flush left
537 try {
538 Rect tmpRect = new Rect();
539 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700540 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700541 icon = packageManager.getDrawable(packageName, info.icon, null);
542 } else {
543 icon = resources.getDrawable(R.drawable.ic_launcher_application);
544 }
545 background.getPadding(tmpRect);
546
Winson Chunge3193b92010-09-10 11:44:42 -0700547 final int iconSize = minDim / 2;
548 final int offset = iconSize / 4;
549 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 icon.draw(mCanvas);
551 } catch (Resources.NotFoundException e) {
552 // if we can't find the icon, then just don't draw it
553 }
554
Winson Chungb3347bb2010-08-19 14:51:28 -0700555 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700556 }
557 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
558 return drawable;
559 }
560
561 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700562 layout.setCellCount(mCellCountX, mCellCountY);
563 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
564 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700565 }
566
Winson Chunge3193b92010-09-10 11:44:42 -0700567 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700568 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700569 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
570
571 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
572 }
573
Winson Chung80baf5a2010-08-09 16:03:15 -0700574 private void syncWidgetPages() {
575 if (mWidgetList == null) return;
576
Winson Chunge3193b92010-09-10 11:44:42 -0700577 // we need to repopulate with the LinearLayout layout for the widget pages
578 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700580 for (int i = 0; i < numPages; ++i) {
581 LinearLayout layout = new WidgetLayout(getContext());
582 layout.setGravity(Gravity.CENTER_HORIZONTAL);
583
584 // Temporary change to prevent the last page from being too small (and items bleeding
585 // onto it). We can remove this once we properly fix the fading algorithm
586 if (i < numPages - 1) {
587 addView(layout, new LinearLayout.LayoutParams(
588 LinearLayout.LayoutParams.WRAP_CONTENT,
589 LinearLayout.LayoutParams.MATCH_PARENT));
590 } else {
591 addView(layout, new LinearLayout.LayoutParams(
592 LinearLayout.LayoutParams.MATCH_PARENT,
593 LinearLayout.LayoutParams.MATCH_PARENT));
594 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700595 }
596 }
597
598 private void syncWidgetPageItems(int page) {
599 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700600 LinearLayout layout = (LinearLayout) getChildAt(page);
601 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 final int count = list.size();
603 layout.removeAllViews();
604 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700605 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
607 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
608 createItemInfo.componentName = info.provider;
609
Winson Chunge3193b92010-09-10 11:44:42 -0700610 LinearLayout l = (LinearLayout) mInflater.inflate(
611 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700612 l.setTag(createItemInfo);
613 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700614 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700615
Winson Chunge3193b92010-09-10 11:44:42 -0700616 final Drawable icon = getWidgetIcon(info);
617 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
618 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
619
Winson Chungd0d43012010-09-26 17:26:45 -0700620 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700621 image.setMaxWidth(mMaxWidgetWidth);
622 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700623 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700624 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700625 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700626 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700627
628 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700629 }
630 }
631
632 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700633 // we need to repopulate with PagedViewCellLayouts
634 removeAllViews();
635
Winson Chung80baf5a2010-08-09 16:03:15 -0700636 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700637 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700638 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700639 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
640 setupPage(layout);
641 addView(layout);
642 }
643 }
644
645 private void syncListPageItems(int page, List<ResolveInfo> list) {
646 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700647 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700648 int startIndex = page * numCells;
649 int endIndex = Math.min(startIndex + numCells, list.size());
650 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
651 // TODO: we can optimize by just re-applying to existing views
652 layout.removeAllViews();
653 for (int i = startIndex; i < endIndex; ++i) {
654 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700655 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
656
Winson Chung241c3b42010-08-25 16:53:03 -0700657 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
658 R.layout.customize_paged_view_item, layout, false);
659 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700660 switch (mCustomizationType) {
661 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700662 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700663 break;
664 case FolderCustomization:
665 if (info.labelRes != R.string.group_folder) {
666 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
667 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
668 info.activityInfo.name);
669 icon.setTag(createItemInfo);
670 } else {
671 UserFolderInfo folderInfo = new UserFolderInfo();
672 folderInfo.title = getResources().getText(R.string.folder_name);
673 icon.setTag(folderInfo);
674 }
675 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700676 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700677 break;
678 case ShortcutCustomization:
679 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
680 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
681 info.activityInfo.name);
682 icon.setTag(createItemInfo);
683 icon.setOnClickListener(this);
684 icon.setOnLongClickListener(this);
685 break;
686 default:
687 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700688 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700689
690 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700691 final int x = index % mCellCountX;
692 final int y = index / mCellCountX;
693 setupPage(layout);
694 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
695 }
696 }
697
698 private void syncAppPages() {
699 if (mApps == null) return;
700
701 // We need to repopulate with PagedViewCellLayouts
702 removeAllViews();
703
704 // Ensure that we have the right number of pages
705 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
706 for (int i = 0; i < numPages; ++i) {
707 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
708 setupPage(layout);
709 addView(layout);
710 }
711 }
712
713 private void syncAppPageItems(int page) {
714 if (mApps == null) return;
715
716 // ensure that we have the right number of items on the pages
717 int numCells = mCellCountX * mCellCountY;
718 int startIndex = page * numCells;
719 int endIndex = Math.min(startIndex + numCells, mApps.size());
720 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
721 // TODO: we can optimize by just re-applying to existing views
722 layout.removeAllViews();
723 for (int i = startIndex; i < endIndex; ++i) {
724 final ApplicationInfo info = mApps.get(i);
725 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
726 R.layout.all_apps_paged_view_application, layout, false);
727 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700728 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700729 icon.setOnLongClickListener(this);
730
731 final int index = i - startIndex;
732 final int x = index % mCellCountX;
733 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700734 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700735 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700736 }
737 }
738
Winson Chung80baf5a2010-08-09 16:03:15 -0700739 @Override
740 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700741 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700742 switch (mCustomizationType) {
743 case WidgetCustomization:
744 syncWidgetPages();
745 break;
746 case FolderCustomization:
747 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700748 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700749 break;
750 case ShortcutCustomization:
751 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700752 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700753 break;
754 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700755 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700756 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700757 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700758 case ApplicationCustomization:
759 syncAppPages();
760 centerPagedViewCellLayouts = false;
761 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700762 default:
763 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700764 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700765 break;
766 }
767
768 // only try and center the page if there is one page
769 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700770 if (centerPagedViewCellLayouts) {
771 if (childCount == 1) {
772 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
773 layout.enableCenteredContent(true);
774 } else {
775 for (int i = 0; i < childCount; ++i) {
776 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
777 layout.enableCenteredContent(false);
778 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700779 }
780 }
781
782 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700783 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700784 }
785
786 @Override
787 public void syncPageItems(int page) {
788 switch (mCustomizationType) {
789 case WidgetCustomization:
790 syncWidgetPageItems(page);
791 break;
792 case FolderCustomization:
793 syncListPageItems(page, mFolderList);
794 break;
795 case ShortcutCustomization:
796 syncListPageItems(page, mShortcutList);
797 break;
798 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700799 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700800 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700801 case ApplicationCustomization:
802 syncAppPageItems(page);
803 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700804 }
805 }
Winson Chunge3193b92010-09-10 11:44:42 -0700806
Winson Chungd0d43012010-09-26 17:26:45 -0700807 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700808 protected int getAssociatedLowerPageBound(int page) {
809 return 0;
810 }
Winson Chungd0d43012010-09-26 17:26:45 -0700811 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700812 protected int getAssociatedUpperPageBound(int page) {
813 return getChildCount();
814 }
Winson Chungd0d43012010-09-26 17:26:45 -0700815
816 @Override
817 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700818 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700819 return true;
820 }
821
822 @Override
823 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
824 return true;
825 }
826
827 @Override
828 public void onDestroyActionMode(ActionMode mode) {
829 endChoiceMode();
830 }
831
832 @Override
833 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
834 return false;
835 }
836
Winson Chung80baf5a2010-08-09 16:03:15 -0700837}