blob: 62e549624df3187ac081e7f48857d42bee382841 [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);
Winson Chung5f941722010-09-28 16:36:43 -0700185
186 // Update the widgets/shortcuts to reflect changes in the set of available apps
187 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700188 }
189
190 /**
191 * Convenience function to add new items to the set of applications that were previously loaded.
192 * Called by both updateApps() and setApps().
193 */
194 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
195 // we add it in place, in alphabetical order
196 final int count = list.size();
197 for (int i = 0; i < count; ++i) {
198 final ApplicationInfo info = list.get(i);
199 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
200 if (index < 0) {
201 mApps.add(-(index + 1), info);
202 }
203 }
204 }
205
206 /**
207 * Adds new applications to the loaded list, and notifies the paged view to update itself.
208 */
209 public void addApps(ArrayList<ApplicationInfo> list) {
210 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700211
212 // Update the widgets/shortcuts to reflect changes in the set of available apps
213 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700214 }
215
216 /**
217 * Convenience function to remove items to the set of applications that were previously loaded.
218 * Called by both updateApps() and removeApps().
219 */
220 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
221 // loop through all the apps and remove apps that have the same component
222 final int length = list.size();
223 for (int i = 0; i < length; ++i) {
224 final ApplicationInfo info = list.get(i);
225 int removeIndex = findAppByComponent(mApps, info);
226 if (removeIndex > -1) {
227 mApps.remove(removeIndex);
228 mPageViewIconCache.removeOutline(info);
229 }
230 }
231 }
232
233 /**
234 * Removes applications from the loaded list, and notifies the paged view to update itself.
235 */
236 public void removeApps(ArrayList<ApplicationInfo> list) {
237 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700238
239 // Update the widgets/shortcuts to reflect changes in the set of available apps
240 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700241 }
242
243 /**
244 * Updates a set of applications from the loaded list, and notifies the paged view to update
245 * itself.
246 */
247 public void updateApps(ArrayList<ApplicationInfo> list) {
248 // We remove and re-add the updated applications list because it's properties may have
249 // changed (ie. the title), and this will ensure that the items will be in their proper
250 // place in the list.
251 removeAppsWithoutInvalidate(list);
252 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700253
254 // Update the widgets/shortcuts to reflect changes in the set of available apps
255 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700256 }
257
258 /**
259 * Convenience function to find matching ApplicationInfos for removal.
260 */
261 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
262 ComponentName removeComponent = item.intent.getComponent();
263 final int length = list.size();
264 for (int i = 0; i < length; ++i) {
265 ApplicationInfo info = list.get(i);
266 if (info.intent.getComponent().equals(removeComponent)) {
267 return i;
268 }
269 }
270 return -1;
271 }
272
Winson Chung80baf5a2010-08-09 16:03:15 -0700273 public void update() {
274 Context context = getContext();
275
276 // get the list of widgets
277 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
278 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
279 @Override
280 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
281 return object1.label.compareTo(object2.label);
282 }
283 });
284
285 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
286 @Override
287 public int compare(ResolveInfo object1, ResolveInfo object2) {
288 return object1.loadLabel(mPackageManager).toString().compareTo(
289 object2.loadLabel(mPackageManager).toString());
290 }
291 };
292
293 // get the list of live folder intents
294 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
295 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
296
297 // manually create a separate entry for creating a folder in Launcher
298 ResolveInfo folder = new ResolveInfo();
299 folder.icon = R.drawable.ic_launcher_folder;
300 folder.labelRes = R.string.group_folder;
301 folder.resolvePackageName = context.getPackageName();
302 mFolderList.add(0, folder);
303 Collections.sort(mFolderList, resolveInfoComparator);
304
305 // get the list of shortcuts
306 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
307 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
308 Collections.sort(mShortcutList, resolveInfoComparator);
309
Winson Chunge8878e32010-09-15 20:37:09 -0700310 // get the list of wallpapers
311 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
312 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
313 Collections.sort(mWallpaperList, resolveInfoComparator);
314
Winson Chung241c3b42010-08-25 16:53:03 -0700315 // reset the icon cache
316 mPageViewIconCache.clear();
317
Winson Chunge3193b92010-09-10 11:44:42 -0700318 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700319 invalidatePageData();
320 }
321
322 public void setDragController(DragController dragger) {
323 mDragController = dragger;
324 }
325
326 public void setCustomizationFilter(CustomizationType filterType) {
327 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700328 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700329 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700330
331 // End the current choice mode so that we don't carry selections across tabs
332 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 }
334
335 @Override
336 public void onDropCompleted(View target, boolean success) {
337 // do nothing
338 }
339
340 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700341 public void onClick(View v) {
342 if (!v.isInTouchMode()) {
343 return;
344 }
345
Winson Chungd0d43012010-09-26 17:26:45 -0700346 // On certain pages, we allow single tap to mark items as selected so that they can be
347 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700348 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700349 switch (mCustomizationType) {
350 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700351 mChoiceModeTitleText = R.string.cab_widget_selection_text;
352 enterChoiceMode = true;
353 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700354 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700355 mChoiceModeTitleText = R.string.cab_app_selection_text;
356 enterChoiceMode = true;
357 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700358 case FolderCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700359 mChoiceModeTitleText = R.string.cab_folder_selection_text;
360 enterChoiceMode = true;
361 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700362 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700363 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
364 enterChoiceMode = true;
365 break;
366 default:
367 break;
368 }
Winson Chungd0d43012010-09-26 17:26:45 -0700369
Michael Jurka3125d9d2010-09-27 11:30:20 -0700370 if (enterChoiceMode) {
Winson Chungd0d43012010-09-26 17:26:45 -0700371 if (v instanceof Checkable) {
372 final Checkable c = (Checkable) v;
373 final boolean wasChecked = c.isChecked();
374 resetCheckedGrandchildren();
375 c.setChecked(!wasChecked);
376
377 // End the current choice mode when we have no items selected
Michael Jurkae17e19c2010-09-28 11:01:39 -0700378 /*if (!c.isChecked()) {
Winson Chungd0d43012010-09-26 17:26:45 -0700379 endChoiceMode();
Michael Jurka3125d9d2010-09-27 11:30:20 -0700380 } else if (isChoiceMode(CHOICE_MODE_NONE)) {
381 endChoiceMode();
382 startChoiceMode(CHOICE_MODE_SINGLE, this);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700383 }*/
384 mChoiceMode = CHOICE_MODE_SINGLE;
385
386 Workspace w = mLauncher.getWorkspace();
387 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
388 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
389 cl.setHover(true);
390
391 animateClickFeedback(v, new Runnable() {
392 @Override
393 public void run() {
394 cl.setHover(false);
395 mLauncher.onWorkspaceClick(cl);
396 mChoiceMode = CHOICE_MODE_NONE;
397 }
398 });
Winson Chungd0d43012010-09-26 17:26:45 -0700399 }
400 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700401 }
402
403 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700404 switch (mCustomizationType) {
405 case WallpaperCustomization:
406 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700407 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700408 animateClickFeedback(v, new Runnable() {
409 @Override
410 public void run() {
411 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700412 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700413 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
414 ComponentName name = new ComponentName(info.activityInfo.packageName,
415 info.activityInfo.name);
416 createWallpapersIntent.setComponent(name);
417 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700418 }
419 });
Winson Chungd0d43012010-09-26 17:26:45 -0700420 break;
421 default:
422 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700423 }
424 }
425
426 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 public boolean onLongClick(View v) {
428 if (!v.isInTouchMode()) {
429 return false;
430 }
431
Winson Chungd0d43012010-09-26 17:26:45 -0700432 // End the current choice mode before we start dragging anything
433 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
434 endChoiceMode();
435 }
436
437 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700438 switch (mCustomizationType) {
439 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700440 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700441 final LinearLayout l = (LinearLayout) v;
442 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700443 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
444 Bitmap.Config.ARGB_8888);
445 Canvas c = new Canvas(b);
446 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700447
Winson Chungd0d43012010-09-26 17:26:45 -0700448 createItemInfo = (PendingAddItemInfo) v.getTag();
449 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
450 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700451
452 // Cleanup the icon
453 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700454 return true;
455 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700456 if (v.getTag() instanceof UserFolderInfo) {
457 // The UserFolderInfo tag is only really used for live folders
458 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700459 mDragController.startDrag(
460 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
461 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700462 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700463 mDragController.startDrag(
464 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
465 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700466 return true;
467 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700468 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700469 mDragController.startDrag(
470 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chung80baf5a2010-08-09 16:03:15 -0700471 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700472 case ApplicationCustomization:
473 // Pick up the application for dropping
474 ApplicationInfo app = (ApplicationInfo) v.getTag();
475 app = new ApplicationInfo(app);
476
477 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
478 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 }
480 return false;
481 }
482
Winson Chunge3193b92010-09-10 11:44:42 -0700483 /**
484 * Pre-processes the layout of the different widget pages.
485 * @return the number of pages of widgets that we have
486 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700487 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700488 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700489
Winson Chunge3193b92010-09-10 11:44:42 -0700490 // create a new page for the first set of widgets
491 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700492 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700493 mWidgetPages.add(newPage);
494
495 // do this until we have no more widgets to lay out
496 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
497 final int widgetCount = mWidgetList.size();
498 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700499 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700500 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700501
Winson Chunge3193b92010-09-10 11:44:42 -0700502 // determine the size of the current widget
503 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
504 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700505
Winson Chunge3193b92010-09-10 11:44:42 -0700506 // create a new page if necessary
507 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
508 numCellsInRow = 0;
509 newPage = new ArrayList<AppWidgetProviderInfo>();
510 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700511 }
512
Winson Chunge3193b92010-09-10 11:44:42 -0700513 // add the item to the current page
514 newPage.add(info);
515 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700516 }
Winson Chunge3193b92010-09-10 11:44:42 -0700517
Winson Chung80baf5a2010-08-09 16:03:15 -0700518 return mWidgetPages.size();
519 }
520
Winson Chunge3193b92010-09-10 11:44:42 -0700521 /**
522 * This method will extract the preview image specified by the widget developer (if it exists),
523 * otherwise, it will try to generate a default image preview with the widget's package icon.
524 * @return the drawable will be used and sized in the ImageView to represent the widget
525 */
526 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 PackageManager packageManager = mLauncher.getPackageManager();
528 String packageName = info.provider.getPackageName();
529 Drawable drawable = null;
530 if (info.previewImage != 0) {
531 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
532 if (drawable == null) {
533 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
534 + " for provider: " + info.provider);
535 } else {
536 return drawable;
537 }
538 }
539
540 // If we don't have a preview image, create a default one
541 if (drawable == null) {
542 Resources resources = mLauncher.getResources();
543
Winson Chung80baf5a2010-08-09 16:03:15 -0700544 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700545 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
546 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
547 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
548 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700549 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
550 mCanvas.setBitmap(bitmap);
551 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
552 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
553
554 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
555 background.setBounds(0, 0, width, height);
556 background.draw(mCanvas);
557
558 // Draw the icon vertically centered, flush left
559 try {
560 Rect tmpRect = new Rect();
561 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700562 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700563 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700564 }
565 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700566 icon = resources.getDrawable(R.drawable.ic_launcher_application);
567 }
568 background.getPadding(tmpRect);
569
Winson Chunge3193b92010-09-10 11:44:42 -0700570 final int iconSize = minDim / 2;
571 final int offset = iconSize / 4;
572 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700573 icon.draw(mCanvas);
574 } catch (Resources.NotFoundException e) {
575 // if we can't find the icon, then just don't draw it
576 }
577
Winson Chungb3347bb2010-08-19 14:51:28 -0700578 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 }
580 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
581 return drawable;
582 }
583
584 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700585 layout.setCellCount(mCellCountX, mCellCountY);
586 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
587 mPageLayoutPaddingBottom);
Winson Chung80baf5a2010-08-09 16:03:15 -0700588 }
589
Winson Chunge3193b92010-09-10 11:44:42 -0700590 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700591 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700592 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
593
594 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
595 }
596
Winson Chung80baf5a2010-08-09 16:03:15 -0700597 private void syncWidgetPages() {
598 if (mWidgetList == null) return;
599
Winson Chunge3193b92010-09-10 11:44:42 -0700600 // we need to repopulate with the LinearLayout layout for the widget pages
601 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700603 for (int i = 0; i < numPages; ++i) {
604 LinearLayout layout = new WidgetLayout(getContext());
605 layout.setGravity(Gravity.CENTER_HORIZONTAL);
606
607 // Temporary change to prevent the last page from being too small (and items bleeding
608 // onto it). We can remove this once we properly fix the fading algorithm
609 if (i < numPages - 1) {
610 addView(layout, new LinearLayout.LayoutParams(
611 LinearLayout.LayoutParams.WRAP_CONTENT,
612 LinearLayout.LayoutParams.MATCH_PARENT));
613 } else {
614 addView(layout, new LinearLayout.LayoutParams(
615 LinearLayout.LayoutParams.MATCH_PARENT,
616 LinearLayout.LayoutParams.MATCH_PARENT));
617 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700618 }
619 }
620
621 private void syncWidgetPageItems(int page) {
622 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700623 LinearLayout layout = (LinearLayout) getChildAt(page);
624 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700625 final int count = list.size();
626 layout.removeAllViews();
627 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700628 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700629 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
630 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
631 createItemInfo.componentName = info.provider;
632
Winson Chunge3193b92010-09-10 11:44:42 -0700633 LinearLayout l = (LinearLayout) mInflater.inflate(
634 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700635 l.setTag(createItemInfo);
636 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700637 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700638
Winson Chunge3193b92010-09-10 11:44:42 -0700639 final Drawable icon = getWidgetIcon(info);
640 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
641 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
642
Winson Chungd0d43012010-09-26 17:26:45 -0700643 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700644 image.setMaxWidth(mMaxWidgetWidth);
645 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700646 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700647 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700648 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700649 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700650
651 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700652 }
653 }
654
655 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700656 // we need to repopulate with PagedViewCellLayouts
657 removeAllViews();
658
Winson Chung80baf5a2010-08-09 16:03:15 -0700659 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700660 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700661 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700662 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
663 setupPage(layout);
664 addView(layout);
665 }
666 }
667
668 private void syncListPageItems(int page, List<ResolveInfo> list) {
669 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700670 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700671 int startIndex = page * numCells;
672 int endIndex = Math.min(startIndex + numCells, list.size());
673 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
674 // TODO: we can optimize by just re-applying to existing views
675 layout.removeAllViews();
676 for (int i = startIndex; i < endIndex; ++i) {
677 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700678 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
679
Winson Chung241c3b42010-08-25 16:53:03 -0700680 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
681 R.layout.customize_paged_view_item, layout, false);
682 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700683 switch (mCustomizationType) {
684 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700685 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700686 break;
687 case FolderCustomization:
688 if (info.labelRes != R.string.group_folder) {
689 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
690 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
691 info.activityInfo.name);
692 icon.setTag(createItemInfo);
693 } else {
694 UserFolderInfo folderInfo = new UserFolderInfo();
695 folderInfo.title = getResources().getText(R.string.folder_name);
696 icon.setTag(folderInfo);
697 }
698 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700699 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700700 break;
701 case ShortcutCustomization:
702 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
703 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
704 info.activityInfo.name);
705 icon.setTag(createItemInfo);
706 icon.setOnClickListener(this);
707 icon.setOnLongClickListener(this);
708 break;
709 default:
710 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700711 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700712
713 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700714 final int x = index % mCellCountX;
715 final int y = index / mCellCountX;
716 setupPage(layout);
717 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
718 }
719 }
720
721 private void syncAppPages() {
722 if (mApps == null) return;
723
724 // We need to repopulate with PagedViewCellLayouts
725 removeAllViews();
726
727 // Ensure that we have the right number of pages
728 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
729 for (int i = 0; i < numPages; ++i) {
730 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
731 setupPage(layout);
732 addView(layout);
733 }
734 }
735
736 private void syncAppPageItems(int page) {
737 if (mApps == null) return;
738
739 // ensure that we have the right number of items on the pages
740 int numCells = mCellCountX * mCellCountY;
741 int startIndex = page * numCells;
742 int endIndex = Math.min(startIndex + numCells, mApps.size());
743 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
744 // TODO: we can optimize by just re-applying to existing views
745 layout.removeAllViews();
746 for (int i = startIndex; i < endIndex; ++i) {
747 final ApplicationInfo info = mApps.get(i);
748 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
749 R.layout.all_apps_paged_view_application, layout, false);
750 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700751 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700752 icon.setOnLongClickListener(this);
753
754 final int index = i - startIndex;
755 final int x = index % mCellCountX;
756 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700757 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700758 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700759 }
760 }
761
Winson Chung80baf5a2010-08-09 16:03:15 -0700762 @Override
763 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700764 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700765 switch (mCustomizationType) {
766 case WidgetCustomization:
767 syncWidgetPages();
768 break;
769 case FolderCustomization:
770 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700771 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700772 break;
773 case ShortcutCustomization:
774 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700775 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700776 break;
777 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700778 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700779 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700780 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700781 case ApplicationCustomization:
782 syncAppPages();
783 centerPagedViewCellLayouts = false;
784 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700785 default:
786 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700787 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700788 break;
789 }
790
791 // only try and center the page if there is one page
792 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700793 if (centerPagedViewCellLayouts) {
794 if (childCount == 1) {
795 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
796 layout.enableCenteredContent(true);
797 } else {
798 for (int i = 0; i < childCount; ++i) {
799 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
800 layout.enableCenteredContent(false);
801 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700802 }
803 }
804
805 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700806 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700807 }
808
809 @Override
810 public void syncPageItems(int page) {
811 switch (mCustomizationType) {
812 case WidgetCustomization:
813 syncWidgetPageItems(page);
814 break;
815 case FolderCustomization:
816 syncListPageItems(page, mFolderList);
817 break;
818 case ShortcutCustomization:
819 syncListPageItems(page, mShortcutList);
820 break;
821 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700822 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700823 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700824 case ApplicationCustomization:
825 syncAppPageItems(page);
826 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700827 }
828 }
Winson Chunge3193b92010-09-10 11:44:42 -0700829
Winson Chungd0d43012010-09-26 17:26:45 -0700830 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700831 protected int getAssociatedLowerPageBound(int page) {
832 return 0;
833 }
Winson Chungd0d43012010-09-26 17:26:45 -0700834 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700835 protected int getAssociatedUpperPageBound(int page) {
836 return getChildCount();
837 }
Winson Chungd0d43012010-09-26 17:26:45 -0700838
839 @Override
840 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700841 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700842 return true;
843 }
844
845 @Override
846 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
847 return true;
848 }
849
850 @Override
851 public void onDestroyActionMode(ActionMode mode) {
852 endChoiceMode();
853 }
854
855 @Override
856 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
857 return false;
858 }
859
Winson Chung80baf5a2010-08-09 16:03:15 -0700860}