blob: 93aa109657feff15b550f0090b4cda584e509ec0 [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Michael Jurka0280c3b2010-09-17 15:00:07 -070019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
21import android.appwidget.AppWidgetManager;
22import android.appwidget.AppWidgetProviderInfo;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070029import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070030import android.graphics.Bitmap;
Winson Chung86f77532010-08-24 11:08:22 -070031import android.graphics.Canvas;
32import android.graphics.Rect;
Michael Jurka0280c3b2010-09-17 15:00:07 -070033import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070034import android.graphics.Region.Op;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.graphics.drawable.Drawable;
36import android.provider.LiveFolders;
37import android.util.AttributeSet;
38import android.util.Log;
Winson Chungd0d43012010-09-26 17:26:45 -070039import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070040import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070041import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070042import android.view.Menu;
43import android.view.MenuItem;
Winson Chunge3193b92010-09-10 11:44:42 -070044import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.view.View;
Winson Chungd0d43012010-09-26 17:26:45 -070046import android.view.ViewGroup;
47import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070048import android.widget.ImageView;
49import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.widget.TextView;
51
Michael Jurka0280c3b2010-09-17 15:00:07 -070052import java.util.ArrayList;
53import java.util.Collections;
54import java.util.Comparator;
55import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070056
57public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070058 implements View.OnLongClickListener, View.OnClickListener,
Winson Chungd0d43012010-09-26 17:26:45 -070059 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070060
61 public enum CustomizationType {
62 WidgetCustomization,
63 FolderCustomization,
64 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070065 WallpaperCustomization,
66 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070067 }
68
Winson Chunge3193b92010-09-10 11:44:42 -070069 /**
70 * The linear layout used strictly for the widget tab of the customization tray
71 */
72 private class WidgetLayout extends LinearLayout {
73 public WidgetLayout(Context context) {
74 super(context);
75 }
76
77 @Override
78 public boolean onTouchEvent(MotionEvent event) {
79 // We eat up the touch events here, since the PagedView (which uses the same swiping
80 // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
81 // the user is scrolling between pages. This means that if the pages themselves don't
82 // handle touch events, it gets forwarded up to PagedView itself, and it's own
83 // onTouchEvent() handling will prevent further intercept touch events from being called
84 // (it's the same view in that case). This is not ideal, but to prevent more changes,
85 // we just always mark the touch event as handled.
86 return super.onTouchEvent(event) || true;
87 }
88 }
89
Winson Chung80baf5a2010-08-09 16:03:15 -070090 private static final String TAG = "CustomizeWorkspace";
91 private static final boolean DEBUG = false;
92
93 private Launcher mLauncher;
94 private DragController mDragController;
95 private PackageManager mPackageManager;
96
97 private CustomizationType mCustomizationType;
98
Winson Chunge3193b92010-09-10 11:44:42 -070099 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
100 private PagedViewCellLayout mWorkspaceWidgetLayout;
101
102 // The mapping between the pages and the widgets that will be laid out on them
103 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
104
105 // The max dimensions for the ImageView we use for displaying the widget
106 private int mMaxWidgetWidth;
107
108 // The max number of widget cells to take a "page" of widget
109 private int mMaxWidgetsCellHSpan;
110
111 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 private List<AppWidgetProviderInfo> mWidgetList;
113 private List<ResolveInfo> mFolderList;
114 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700115 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700116 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700117
Winson Chunge3193b92010-09-10 11:44:42 -0700118 private static final int sMinWidgetCellHSpan = 2;
119 private static final int sMaxWidgetCellHSpan = 4;
120
Michael Jurka3125d9d2010-09-27 11:30:20 -0700121 private int mChoiceModeTitleText;
122
Winson Chunge3193b92010-09-10 11:44:42 -0700123 // The scale factor for widget previews inside the widget drawer
124 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700125
126 private final Canvas mCanvas = new Canvas();
127 private final LayoutInflater mInflater;
128
129 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700130 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 }
132
133 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700134 this(context, attrs, 0);
135 }
136
137 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
139
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700140 TypedArray a;
141 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700142 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700143 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
144 a.recycle();
145 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
146 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
147 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700148
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700149 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700150 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700151 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
152 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700153 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700154
Winson Chung80baf5a2010-08-09 16:03:15 -0700155 setVisibility(View.GONE);
156 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700157 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 }
159
160 public void setLauncher(Launcher launcher) {
161 Context context = getContext();
162 mLauncher = launcher;
163 mPackageManager = context.getPackageManager();
164 }
165
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700166 /**
167 * Sets the list of applications that launcher has loaded.
168 */
169 public void setApps(ArrayList<ApplicationInfo> list) {
170 mApps = list;
171 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700172
173 // Update the widgets/shortcuts to reflect changes in the set of available apps
174 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700175 }
176
177 /**
178 * Convenience function to add new items to the set of applications that were previously loaded.
179 * Called by both updateApps() and setApps().
180 */
181 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
182 // we add it in place, in alphabetical order
183 final int count = list.size();
184 for (int i = 0; i < count; ++i) {
185 final ApplicationInfo info = list.get(i);
186 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
187 if (index < 0) {
188 mApps.add(-(index + 1), info);
189 }
190 }
191 }
192
193 /**
194 * Adds new applications to the loaded list, and notifies the paged view to update itself.
195 */
196 public void addApps(ArrayList<ApplicationInfo> list) {
197 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700198
199 // Update the widgets/shortcuts to reflect changes in the set of available apps
200 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700201 }
202
203 /**
204 * Convenience function to remove items to the set of applications that were previously loaded.
205 * Called by both updateApps() and removeApps().
206 */
207 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
208 // loop through all the apps and remove apps that have the same component
209 final int length = list.size();
210 for (int i = 0; i < length; ++i) {
211 final ApplicationInfo info = list.get(i);
212 int removeIndex = findAppByComponent(mApps, info);
213 if (removeIndex > -1) {
214 mApps.remove(removeIndex);
215 mPageViewIconCache.removeOutline(info);
216 }
217 }
218 }
219
220 /**
221 * Removes applications from the loaded list, and notifies the paged view to update itself.
222 */
223 public void removeApps(ArrayList<ApplicationInfo> list) {
224 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700225
226 // Update the widgets/shortcuts to reflect changes in the set of available apps
227 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700228 }
229
230 /**
231 * Updates a set of applications from the loaded list, and notifies the paged view to update
232 * itself.
233 */
234 public void updateApps(ArrayList<ApplicationInfo> list) {
235 // We remove and re-add the updated applications list because it's properties may have
236 // changed (ie. the title), and this will ensure that the items will be in their proper
237 // place in the list.
238 removeAppsWithoutInvalidate(list);
239 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700240
241 // Update the widgets/shortcuts to reflect changes in the set of available apps
242 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700243 }
244
245 /**
246 * Convenience function to find matching ApplicationInfos for removal.
247 */
248 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
249 ComponentName removeComponent = item.intent.getComponent();
250 final int length = list.size();
251 for (int i = 0; i < length; ++i) {
252 ApplicationInfo info = list.get(i);
253 if (info.intent.getComponent().equals(removeComponent)) {
254 return i;
255 }
256 }
257 return -1;
258 }
259
Winson Chung80baf5a2010-08-09 16:03:15 -0700260 public void update() {
261 Context context = getContext();
262
263 // get the list of widgets
264 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
265 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
266 @Override
267 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
268 return object1.label.compareTo(object2.label);
269 }
270 });
271
272 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
273 @Override
274 public int compare(ResolveInfo object1, ResolveInfo object2) {
275 return object1.loadLabel(mPackageManager).toString().compareTo(
276 object2.loadLabel(mPackageManager).toString());
277 }
278 };
279
280 // get the list of live folder intents
281 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
282 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
283
284 // manually create a separate entry for creating a folder in Launcher
285 ResolveInfo folder = new ResolveInfo();
286 folder.icon = R.drawable.ic_launcher_folder;
287 folder.labelRes = R.string.group_folder;
288 folder.resolvePackageName = context.getPackageName();
289 mFolderList.add(0, folder);
290 Collections.sort(mFolderList, resolveInfoComparator);
291
292 // get the list of shortcuts
293 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
294 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
295 Collections.sort(mShortcutList, resolveInfoComparator);
296
Winson Chunge8878e32010-09-15 20:37:09 -0700297 // get the list of wallpapers
298 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
299 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
300 Collections.sort(mWallpaperList, resolveInfoComparator);
301
Winson Chung241c3b42010-08-25 16:53:03 -0700302 // reset the icon cache
303 mPageViewIconCache.clear();
304
Winson Chunge3193b92010-09-10 11:44:42 -0700305 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700306 invalidatePageData();
307 }
308
309 public void setDragController(DragController dragger) {
310 mDragController = dragger;
311 }
312
313 public void setCustomizationFilter(CustomizationType filterType) {
314 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700315 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700317
318 // End the current choice mode so that we don't carry selections across tabs
319 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 }
321
322 @Override
323 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700324 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 }
326
327 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700328 public void onClick(View v) {
329 if (!v.isInTouchMode()) {
330 return;
331 }
332
Winson Chungd0d43012010-09-26 17:26:45 -0700333 // On certain pages, we allow single tap to mark items as selected so that they can be
334 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700335 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700336 switch (mCustomizationType) {
337 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700338 mChoiceModeTitleText = R.string.cab_widget_selection_text;
339 enterChoiceMode = true;
340 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700341 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700342 mChoiceModeTitleText = R.string.cab_app_selection_text;
343 enterChoiceMode = true;
344 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700345 case FolderCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700346 mChoiceModeTitleText = R.string.cab_folder_selection_text;
347 enterChoiceMode = true;
348 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700349 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700350 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
351 enterChoiceMode = true;
352 break;
353 default:
354 break;
355 }
Winson Chungd0d43012010-09-26 17:26:45 -0700356
Michael Jurka3125d9d2010-09-27 11:30:20 -0700357 if (enterChoiceMode) {
Winson Chungd0d43012010-09-26 17:26:45 -0700358 if (v instanceof Checkable) {
359 final Checkable c = (Checkable) v;
360 final boolean wasChecked = c.isChecked();
361 resetCheckedGrandchildren();
362 c.setChecked(!wasChecked);
363
364 // End the current choice mode when we have no items selected
Michael Jurkae17e19c2010-09-28 11:01:39 -0700365 /*if (!c.isChecked()) {
Winson Chungd0d43012010-09-26 17:26:45 -0700366 endChoiceMode();
Michael Jurka3125d9d2010-09-27 11:30:20 -0700367 } else if (isChoiceMode(CHOICE_MODE_NONE)) {
368 endChoiceMode();
369 startChoiceMode(CHOICE_MODE_SINGLE, this);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700370 }*/
371 mChoiceMode = CHOICE_MODE_SINGLE;
372
373 Workspace w = mLauncher.getWorkspace();
374 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
375 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
376 cl.setHover(true);
377
378 animateClickFeedback(v, new Runnable() {
379 @Override
380 public void run() {
381 cl.setHover(false);
382 mLauncher.onWorkspaceClick(cl);
383 mChoiceMode = CHOICE_MODE_NONE;
384 }
385 });
Winson Chungd0d43012010-09-26 17:26:45 -0700386 }
387 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700388 }
389
390 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700391 switch (mCustomizationType) {
392 case WallpaperCustomization:
393 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700394 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700395 animateClickFeedback(v, new Runnable() {
396 @Override
397 public void run() {
398 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700399 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700400 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
401 ComponentName name = new ComponentName(info.activityInfo.packageName,
402 info.activityInfo.name);
403 createWallpapersIntent.setComponent(name);
404 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700405 }
406 });
Winson Chungd0d43012010-09-26 17:26:45 -0700407 break;
408 default:
409 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700410 }
411 }
412
413 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 public boolean onLongClick(View v) {
415 if (!v.isInTouchMode()) {
416 return false;
417 }
418
Winson Chungd0d43012010-09-26 17:26:45 -0700419 // End the current choice mode before we start dragging anything
420 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
421 endChoiceMode();
422 }
423
424 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700425 switch (mCustomizationType) {
426 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700427 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700428 final LinearLayout l = (LinearLayout) v;
429 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700430 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
431 Bitmap.Config.ARGB_8888);
432 Canvas c = new Canvas(b);
433 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700434 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700435
Michael Jurka3e7c7632010-10-02 16:01:03 -0700436 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
437 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
438 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700439 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700440
441 // Cleanup the icon
442 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700443 return true;
444 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700445 if (v.getTag() instanceof UserFolderInfo) {
446 // The UserFolderInfo tag is only really used for live folders
447 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700448 mDragController.startDrag(
449 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
450 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700451 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700452 mDragController.startDrag(
453 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
454 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700455 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700456 return true;
457 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700458 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700459 mDragController.startDrag(
460 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700461 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700462 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700463 case ApplicationCustomization:
464 // Pick up the application for dropping
465 ApplicationInfo app = (ApplicationInfo) v.getTag();
466 app = new ApplicationInfo(app);
467
468 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700469 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700470 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700471 }
472 return false;
473 }
474
Winson Chunge3193b92010-09-10 11:44:42 -0700475 /**
476 * Pre-processes the layout of the different widget pages.
477 * @return the number of pages of widgets that we have
478 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700480 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700481
Winson Chunge3193b92010-09-10 11:44:42 -0700482 // create a new page for the first set of widgets
483 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700484 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700485 mWidgetPages.add(newPage);
486
487 // do this until we have no more widgets to lay out
488 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
489 final int widgetCount = mWidgetList.size();
490 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700491 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700492 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700493
Winson Chunge3193b92010-09-10 11:44:42 -0700494 // determine the size of the current widget
495 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
496 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700497
Winson Chunge3193b92010-09-10 11:44:42 -0700498 // create a new page if necessary
499 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
500 numCellsInRow = 0;
501 newPage = new ArrayList<AppWidgetProviderInfo>();
502 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700503 }
504
Winson Chunge3193b92010-09-10 11:44:42 -0700505 // add the item to the current page
506 newPage.add(info);
507 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700508 }
Winson Chunge3193b92010-09-10 11:44:42 -0700509
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 return mWidgetPages.size();
511 }
512
Winson Chunge3193b92010-09-10 11:44:42 -0700513 /**
514 * This method will extract the preview image specified by the widget developer (if it exists),
515 * otherwise, it will try to generate a default image preview with the widget's package icon.
516 * @return the drawable will be used and sized in the ImageView to represent the widget
517 */
518 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700519 PackageManager packageManager = mLauncher.getPackageManager();
520 String packageName = info.provider.getPackageName();
521 Drawable drawable = null;
522 if (info.previewImage != 0) {
523 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
524 if (drawable == null) {
525 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
526 + " for provider: " + info.provider);
527 } else {
528 return drawable;
529 }
530 }
531
532 // If we don't have a preview image, create a default one
533 if (drawable == null) {
534 Resources resources = mLauncher.getResources();
535
Winson Chung80baf5a2010-08-09 16:03:15 -0700536 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700537 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
538 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
539 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
540 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700541 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
542 mCanvas.setBitmap(bitmap);
543 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
544 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
545
546 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
547 background.setBounds(0, 0, width, height);
548 background.draw(mCanvas);
549
550 // Draw the icon vertically centered, flush left
551 try {
552 Rect tmpRect = new Rect();
553 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700554 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700555 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700556 }
557 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700558 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);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700621 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700622 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
623 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700624 createItemInfo.minWidth = info.minWidth;
625 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700626
Winson Chunge3193b92010-09-10 11:44:42 -0700627 LinearLayout l = (LinearLayout) mInflater.inflate(
628 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700629 l.setTag(createItemInfo);
630 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700631 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700632
Winson Chunge3193b92010-09-10 11:44:42 -0700633 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700634
635 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
636 final int hSpan = spans[0];
637 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700638
Winson Chungd0d43012010-09-26 17:26:45 -0700639 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700640 image.setMaxWidth(mMaxWidgetWidth);
641 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700642 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700643 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700644 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700645 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700646
647 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700648 }
649 }
650
651 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700652 // we need to repopulate with PagedViewCellLayouts
653 removeAllViews();
654
Winson Chung80baf5a2010-08-09 16:03:15 -0700655 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700656 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700657 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700658 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
659 setupPage(layout);
660 addView(layout);
661 }
662 }
663
664 private void syncListPageItems(int page, List<ResolveInfo> list) {
665 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700666 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700667 int startIndex = page * numCells;
668 int endIndex = Math.min(startIndex + numCells, list.size());
669 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
670 // TODO: we can optimize by just re-applying to existing views
671 layout.removeAllViews();
672 for (int i = startIndex; i < endIndex; ++i) {
673 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700674 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
675
Winson Chung241c3b42010-08-25 16:53:03 -0700676 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
677 R.layout.customize_paged_view_item, layout, false);
678 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700679 switch (mCustomizationType) {
680 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700681 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700682 break;
683 case FolderCustomization:
684 if (info.labelRes != R.string.group_folder) {
685 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
686 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
687 info.activityInfo.name);
688 icon.setTag(createItemInfo);
689 } else {
690 UserFolderInfo folderInfo = new UserFolderInfo();
691 folderInfo.title = getResources().getText(R.string.folder_name);
692 icon.setTag(folderInfo);
693 }
694 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700695 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700696 break;
697 case ShortcutCustomization:
698 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
699 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
700 info.activityInfo.name);
701 icon.setTag(createItemInfo);
702 icon.setOnClickListener(this);
703 icon.setOnLongClickListener(this);
704 break;
705 default:
706 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700707 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700708
709 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700710 final int x = index % mCellCountX;
711 final int y = index / mCellCountX;
712 setupPage(layout);
713 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
714 }
715 }
716
717 private void syncAppPages() {
718 if (mApps == null) return;
719
720 // We need to repopulate with PagedViewCellLayouts
721 removeAllViews();
722
723 // Ensure that we have the right number of pages
724 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
725 for (int i = 0; i < numPages; ++i) {
726 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
727 setupPage(layout);
728 addView(layout);
729 }
730 }
731
732 private void syncAppPageItems(int page) {
733 if (mApps == null) return;
734
735 // ensure that we have the right number of items on the pages
736 int numCells = mCellCountX * mCellCountY;
737 int startIndex = page * numCells;
738 int endIndex = Math.min(startIndex + numCells, mApps.size());
739 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
740 // TODO: we can optimize by just re-applying to existing views
741 layout.removeAllViews();
742 for (int i = startIndex; i < endIndex; ++i) {
743 final ApplicationInfo info = mApps.get(i);
744 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
745 R.layout.all_apps_paged_view_application, layout, false);
746 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700747 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700748 icon.setOnLongClickListener(this);
749
750 final int index = i - startIndex;
751 final int x = index % mCellCountX;
752 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700753 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700754 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700755 }
756 }
757
Winson Chung80baf5a2010-08-09 16:03:15 -0700758 @Override
759 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700760 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700761 switch (mCustomizationType) {
762 case WidgetCustomization:
763 syncWidgetPages();
764 break;
765 case FolderCustomization:
766 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700767 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700768 break;
769 case ShortcutCustomization:
770 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700771 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700772 break;
773 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700774 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700775 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700776 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700777 case ApplicationCustomization:
778 syncAppPages();
779 centerPagedViewCellLayouts = false;
780 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700781 default:
782 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700783 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700784 break;
785 }
786
787 // only try and center the page if there is one page
788 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700789 if (centerPagedViewCellLayouts) {
790 if (childCount == 1) {
791 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
792 layout.enableCenteredContent(true);
793 } else {
794 for (int i = 0; i < childCount; ++i) {
795 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
796 layout.enableCenteredContent(false);
797 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700798 }
799 }
800
801 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700802 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 }
804
805 @Override
806 public void syncPageItems(int page) {
807 switch (mCustomizationType) {
808 case WidgetCustomization:
809 syncWidgetPageItems(page);
810 break;
811 case FolderCustomization:
812 syncListPageItems(page, mFolderList);
813 break;
814 case ShortcutCustomization:
815 syncListPageItems(page, mShortcutList);
816 break;
817 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700818 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700819 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700820 case ApplicationCustomization:
821 syncAppPageItems(page);
822 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700823 }
824 }
Winson Chunge3193b92010-09-10 11:44:42 -0700825
Winson Chungd0d43012010-09-26 17:26:45 -0700826 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700827 protected int getAssociatedLowerPageBound(int page) {
828 return 0;
829 }
Winson Chungd0d43012010-09-26 17:26:45 -0700830 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700831 protected int getAssociatedUpperPageBound(int page) {
832 return getChildCount();
833 }
Winson Chungd0d43012010-09-26 17:26:45 -0700834
835 @Override
836 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700837 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700838 return true;
839 }
840
841 @Override
842 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
843 return true;
844 }
845
846 @Override
847 public void onDestroyActionMode(ActionMode mode) {
848 endChoiceMode();
849 }
850
851 @Override
852 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
853 return false;
854 }
855
Winson Chung80baf5a2010-08-09 16:03:15 -0700856}