blob: 03c4bce7ee8f54244754ee5f34b571775cdad5a9 [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
Michael Jurkae17e19c2010-09-28 11:01:39 -0700371 /*if (!c.isChecked()) {
Winson Chungd0d43012010-09-26 17:26:45 -0700372 endChoiceMode();
Michael Jurka3125d9d2010-09-27 11:30:20 -0700373 } else if (isChoiceMode(CHOICE_MODE_NONE)) {
374 endChoiceMode();
375 startChoiceMode(CHOICE_MODE_SINGLE, this);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700376 }*/
377 mChoiceMode = CHOICE_MODE_SINGLE;
378
379 Workspace w = mLauncher.getWorkspace();
380 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
381 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
382 cl.setHover(true);
383
384 animateClickFeedback(v, new Runnable() {
385 @Override
386 public void run() {
387 cl.setHover(false);
388 mLauncher.onWorkspaceClick(cl);
389 mChoiceMode = CHOICE_MODE_NONE;
390 }
391 });
Winson Chungd0d43012010-09-26 17:26:45 -0700392 }
393 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700394 }
395
396 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700397 switch (mCustomizationType) {
398 case WallpaperCustomization:
399 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700400 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700401 animateClickFeedback(v, new Runnable() {
402 @Override
403 public void run() {
404 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700405 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700406 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
407 ComponentName name = new ComponentName(info.activityInfo.packageName,
408 info.activityInfo.name);
409 createWallpapersIntent.setComponent(name);
410 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700411 }
412 });
Winson Chungd0d43012010-09-26 17:26:45 -0700413 break;
414 default:
415 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700416 }
417 }
418
419 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700420 public boolean onLongClick(View v) {
421 if (!v.isInTouchMode()) {
422 return false;
423 }
424
Winson Chungd0d43012010-09-26 17:26:45 -0700425 // End the current choice mode before we start dragging anything
426 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
427 endChoiceMode();
428 }
429
430 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700431 switch (mCustomizationType) {
432 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700433 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700434 final LinearLayout l = (LinearLayout) v;
435 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700436 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
437 Bitmap.Config.ARGB_8888);
438 Canvas c = new Canvas(b);
439 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700440
Winson Chungd0d43012010-09-26 17:26:45 -0700441 createItemInfo = (PendingAddItemInfo) v.getTag();
442 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
443 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700444
445 // Cleanup the icon
446 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700447 return true;
448 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700449 if (v.getTag() instanceof UserFolderInfo) {
450 // The UserFolderInfo tag is only really used for live folders
451 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700452 mDragController.startDrag(
453 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
454 } else {
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);
458 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700459 return true;
460 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700461 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700462 mDragController.startDrag(
463 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700464 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700465 case ApplicationCustomization:
466 // Pick up the application for dropping
467 ApplicationInfo app = (ApplicationInfo) v.getTag();
468 app = new ApplicationInfo(app);
469
470 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
471 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700472 }
473 return false;
474 }
475
Winson Chunge3193b92010-09-10 11:44:42 -0700476 /**
477 * Pre-processes the layout of the different widget pages.
478 * @return the number of pages of widgets that we have
479 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700480 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700481 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700482
Winson Chunge3193b92010-09-10 11:44:42 -0700483 // create a new page for the first set of widgets
484 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700485 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700486 mWidgetPages.add(newPage);
487
488 // do this until we have no more widgets to lay out
489 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
490 final int widgetCount = mWidgetList.size();
491 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700492 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700493 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700494
Winson Chunge3193b92010-09-10 11:44:42 -0700495 // determine the size of the current widget
496 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
497 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700498
Winson Chunge3193b92010-09-10 11:44:42 -0700499 // create a new page if necessary
500 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
501 numCellsInRow = 0;
502 newPage = new ArrayList<AppWidgetProviderInfo>();
503 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700504 }
505
Winson Chunge3193b92010-09-10 11:44:42 -0700506 // add the item to the current page
507 newPage.add(info);
508 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700509 }
Winson Chunge3193b92010-09-10 11:44:42 -0700510
Winson Chung80baf5a2010-08-09 16:03:15 -0700511 return mWidgetPages.size();
512 }
513
Winson Chunge3193b92010-09-10 11:44:42 -0700514 /**
515 * This method will extract the preview image specified by the widget developer (if it exists),
516 * otherwise, it will try to generate a default image preview with the widget's package icon.
517 * @return the drawable will be used and sized in the ImageView to represent the widget
518 */
519 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700520 PackageManager packageManager = mLauncher.getPackageManager();
521 String packageName = info.provider.getPackageName();
522 Drawable drawable = null;
523 if (info.previewImage != 0) {
524 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
525 if (drawable == null) {
526 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
527 + " for provider: " + info.provider);
528 } else {
529 return drawable;
530 }
531 }
532
533 // If we don't have a preview image, create a default one
534 if (drawable == null) {
535 Resources resources = mLauncher.getResources();
536
Winson Chung80baf5a2010-08-09 16:03:15 -0700537 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700538 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
539 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
540 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
541 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
543 mCanvas.setBitmap(bitmap);
544 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
545 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
546
547 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
548 background.setBounds(0, 0, width, height);
549 background.draw(mCanvas);
550
551 // Draw the icon vertically centered, flush left
552 try {
553 Rect tmpRect = new Rect();
554 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700555 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700556 icon = packageManager.getDrawable(packageName, info.icon, null);
557 } else {
558 icon = resources.getDrawable(R.drawable.ic_launcher_application);
559 }
560 background.getPadding(tmpRect);
561
Winson Chunge3193b92010-09-10 11:44:42 -0700562 final int iconSize = minDim / 2;
563 final int offset = iconSize / 4;
564 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700565 icon.draw(mCanvas);
566 } catch (Resources.NotFoundException e) {
567 // if we can't find the icon, then just don't draw it
568 }
569
Winson Chungb3347bb2010-08-19 14:51:28 -0700570 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700571 }
572 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
573 return drawable;
574 }
575
576 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700577 layout.setCellCount(mCellCountX, mCellCountY);
578 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
579 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700580 }
581
Winson Chunge3193b92010-09-10 11:44:42 -0700582 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700583 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700584 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
585
586 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
587 }
588
Winson Chung80baf5a2010-08-09 16:03:15 -0700589 private void syncWidgetPages() {
590 if (mWidgetList == null) return;
591
Winson Chunge3193b92010-09-10 11:44:42 -0700592 // we need to repopulate with the LinearLayout layout for the widget pages
593 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700594 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700595 for (int i = 0; i < numPages; ++i) {
596 LinearLayout layout = new WidgetLayout(getContext());
597 layout.setGravity(Gravity.CENTER_HORIZONTAL);
598
599 // Temporary change to prevent the last page from being too small (and items bleeding
600 // onto it). We can remove this once we properly fix the fading algorithm
601 if (i < numPages - 1) {
602 addView(layout, new LinearLayout.LayoutParams(
603 LinearLayout.LayoutParams.WRAP_CONTENT,
604 LinearLayout.LayoutParams.MATCH_PARENT));
605 } else {
606 addView(layout, new LinearLayout.LayoutParams(
607 LinearLayout.LayoutParams.MATCH_PARENT,
608 LinearLayout.LayoutParams.MATCH_PARENT));
609 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700610 }
611 }
612
613 private void syncWidgetPageItems(int page) {
614 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700615 LinearLayout layout = (LinearLayout) getChildAt(page);
616 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700617 final int count = list.size();
618 layout.removeAllViews();
619 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700620 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700621 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
622 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
623 createItemInfo.componentName = info.provider;
624
Winson Chunge3193b92010-09-10 11:44:42 -0700625 LinearLayout l = (LinearLayout) mInflater.inflate(
626 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700627 l.setTag(createItemInfo);
628 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700629 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700630
Winson Chunge3193b92010-09-10 11:44:42 -0700631 final Drawable icon = getWidgetIcon(info);
632 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
633 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
634
Winson Chungd0d43012010-09-26 17:26:45 -0700635 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700636 image.setMaxWidth(mMaxWidgetWidth);
637 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700638 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700639 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700640 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700641 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700642
643 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700644 }
645 }
646
647 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700648 // we need to repopulate with PagedViewCellLayouts
649 removeAllViews();
650
Winson Chung80baf5a2010-08-09 16:03:15 -0700651 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700652 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700653 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700654 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
655 setupPage(layout);
656 addView(layout);
657 }
658 }
659
660 private void syncListPageItems(int page, List<ResolveInfo> list) {
661 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700662 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700663 int startIndex = page * numCells;
664 int endIndex = Math.min(startIndex + numCells, list.size());
665 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
666 // TODO: we can optimize by just re-applying to existing views
667 layout.removeAllViews();
668 for (int i = startIndex; i < endIndex; ++i) {
669 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700670 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
671
Winson Chung241c3b42010-08-25 16:53:03 -0700672 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
673 R.layout.customize_paged_view_item, layout, false);
674 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700675 switch (mCustomizationType) {
676 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700677 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700678 break;
679 case FolderCustomization:
680 if (info.labelRes != R.string.group_folder) {
681 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
682 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
683 info.activityInfo.name);
684 icon.setTag(createItemInfo);
685 } else {
686 UserFolderInfo folderInfo = new UserFolderInfo();
687 folderInfo.title = getResources().getText(R.string.folder_name);
688 icon.setTag(folderInfo);
689 }
690 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700691 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700692 break;
693 case ShortcutCustomization:
694 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
695 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
696 info.activityInfo.name);
697 icon.setTag(createItemInfo);
698 icon.setOnClickListener(this);
699 icon.setOnLongClickListener(this);
700 break;
701 default:
702 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700703 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700704
705 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700706 final int x = index % mCellCountX;
707 final int y = index / mCellCountX;
708 setupPage(layout);
709 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
710 }
711 }
712
713 private void syncAppPages() {
714 if (mApps == null) return;
715
716 // We need to repopulate with PagedViewCellLayouts
717 removeAllViews();
718
719 // Ensure that we have the right number of pages
720 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
721 for (int i = 0; i < numPages; ++i) {
722 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
723 setupPage(layout);
724 addView(layout);
725 }
726 }
727
728 private void syncAppPageItems(int page) {
729 if (mApps == null) return;
730
731 // ensure that we have the right number of items on the pages
732 int numCells = mCellCountX * mCellCountY;
733 int startIndex = page * numCells;
734 int endIndex = Math.min(startIndex + numCells, mApps.size());
735 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
736 // TODO: we can optimize by just re-applying to existing views
737 layout.removeAllViews();
738 for (int i = startIndex; i < endIndex; ++i) {
739 final ApplicationInfo info = mApps.get(i);
740 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
741 R.layout.all_apps_paged_view_application, layout, false);
742 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700743 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700744 icon.setOnLongClickListener(this);
745
746 final int index = i - startIndex;
747 final int x = index % mCellCountX;
748 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700749 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700750 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700751 }
752 }
753
Winson Chung80baf5a2010-08-09 16:03:15 -0700754 @Override
755 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700756 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700757 switch (mCustomizationType) {
758 case WidgetCustomization:
759 syncWidgetPages();
760 break;
761 case FolderCustomization:
762 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700763 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700764 break;
765 case ShortcutCustomization:
766 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700767 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700768 break;
769 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700770 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700771 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700772 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700773 case ApplicationCustomization:
774 syncAppPages();
775 centerPagedViewCellLayouts = false;
776 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700777 default:
778 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700779 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700780 break;
781 }
782
783 // only try and center the page if there is one page
784 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700785 if (centerPagedViewCellLayouts) {
786 if (childCount == 1) {
787 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
788 layout.enableCenteredContent(true);
789 } else {
790 for (int i = 0; i < childCount; ++i) {
791 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
792 layout.enableCenteredContent(false);
793 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700794 }
795 }
796
797 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700798 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700799 }
800
801 @Override
802 public void syncPageItems(int page) {
803 switch (mCustomizationType) {
804 case WidgetCustomization:
805 syncWidgetPageItems(page);
806 break;
807 case FolderCustomization:
808 syncListPageItems(page, mFolderList);
809 break;
810 case ShortcutCustomization:
811 syncListPageItems(page, mShortcutList);
812 break;
813 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700814 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700815 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700816 case ApplicationCustomization:
817 syncAppPageItems(page);
818 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700819 }
820 }
Winson Chunge3193b92010-09-10 11:44:42 -0700821
Winson Chungd0d43012010-09-26 17:26:45 -0700822 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700823 protected int getAssociatedLowerPageBound(int page) {
824 return 0;
825 }
Winson Chungd0d43012010-09-26 17:26:45 -0700826 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700827 protected int getAssociatedUpperPageBound(int page) {
828 return getChildCount();
829 }
Winson Chungd0d43012010-09-26 17:26:45 -0700830
831 @Override
832 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700833 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700834 return true;
835 }
836
837 @Override
838 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
839 return true;
840 }
841
842 @Override
843 public void onDestroyActionMode(ActionMode mode) {
844 endChoiceMode();
845 }
846
847 @Override
848 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
849 return false;
850 }
851
Winson Chung80baf5a2010-08-09 16:03:15 -0700852}