blob: a01f1c083d6cacff3776c94b69512908d72b11d3 [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
69 private static final String TAG = "CustomizeWorkspace";
70 private static final boolean DEBUG = false;
71
72 private Launcher mLauncher;
73 private DragController mDragController;
74 private PackageManager mPackageManager;
75
76 private CustomizationType mCustomizationType;
77
Winson Chunge3193b92010-09-10 11:44:42 -070078 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
79 private PagedViewCellLayout mWorkspaceWidgetLayout;
80
81 // The mapping between the pages and the widgets that will be laid out on them
82 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
83
84 // The max dimensions for the ImageView we use for displaying the widget
85 private int mMaxWidgetWidth;
86
87 // The max number of widget cells to take a "page" of widget
88 private int mMaxWidgetsCellHSpan;
89
90 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070091 private List<AppWidgetProviderInfo> mWidgetList;
92 private List<ResolveInfo> mFolderList;
93 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070094 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -070095 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -070096
Winson Chunge3193b92010-09-10 11:44:42 -070097 private static final int sMinWidgetCellHSpan = 2;
98 private static final int sMaxWidgetCellHSpan = 4;
99
Michael Jurka3125d9d2010-09-27 11:30:20 -0700100 private int mChoiceModeTitleText;
101
Winson Chunge3193b92010-09-10 11:44:42 -0700102 // The scale factor for widget previews inside the widget drawer
103 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700104
105 private final Canvas mCanvas = new Canvas();
106 private final LayoutInflater mInflater;
107
108 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700109 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700110 }
111
112 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700113 this(context, attrs, 0);
114 }
115
116 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
117 super(context, attrs, defStyle);
118
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700119 TypedArray a;
120 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700121 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700122 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
123 a.recycle();
124 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
125 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
126 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700127
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700128 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700130 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
131 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700132 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700133
Winson Chung80baf5a2010-08-09 16:03:15 -0700134 setVisibility(View.GONE);
135 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700136 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700137 }
138
139 public void setLauncher(Launcher launcher) {
140 Context context = getContext();
141 mLauncher = launcher;
142 mPackageManager = context.getPackageManager();
143 }
144
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700145 /**
146 * Sets the list of applications that launcher has loaded.
147 */
148 public void setApps(ArrayList<ApplicationInfo> list) {
149 mApps = list;
150 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700151
152 // Update the widgets/shortcuts to reflect changes in the set of available apps
153 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700154 }
155
156 /**
157 * Convenience function to add new items to the set of applications that were previously loaded.
158 * Called by both updateApps() and setApps().
159 */
160 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
161 // we add it in place, in alphabetical order
162 final int count = list.size();
163 for (int i = 0; i < count; ++i) {
164 final ApplicationInfo info = list.get(i);
165 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
166 if (index < 0) {
167 mApps.add(-(index + 1), info);
168 }
169 }
170 }
171
172 /**
173 * Adds new applications to the loaded list, and notifies the paged view to update itself.
174 */
175 public void addApps(ArrayList<ApplicationInfo> list) {
176 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700177
178 // Update the widgets/shortcuts to reflect changes in the set of available apps
179 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700180 }
181
182 /**
183 * Convenience function to remove items to the set of applications that were previously loaded.
184 * Called by both updateApps() and removeApps().
185 */
186 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
187 // loop through all the apps and remove apps that have the same component
188 final int length = list.size();
189 for (int i = 0; i < length; ++i) {
190 final ApplicationInfo info = list.get(i);
191 int removeIndex = findAppByComponent(mApps, info);
192 if (removeIndex > -1) {
193 mApps.remove(removeIndex);
194 mPageViewIconCache.removeOutline(info);
195 }
196 }
197 }
198
199 /**
200 * Removes applications from the loaded list, and notifies the paged view to update itself.
201 */
202 public void removeApps(ArrayList<ApplicationInfo> list) {
203 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700204
205 // Update the widgets/shortcuts to reflect changes in the set of available apps
206 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700207 }
208
209 /**
210 * Updates a set of applications from the loaded list, and notifies the paged view to update
211 * itself.
212 */
213 public void updateApps(ArrayList<ApplicationInfo> list) {
214 // We remove and re-add the updated applications list because it's properties may have
215 // changed (ie. the title), and this will ensure that the items will be in their proper
216 // place in the list.
217 removeAppsWithoutInvalidate(list);
218 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700219
220 // Update the widgets/shortcuts to reflect changes in the set of available apps
221 update();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700222 }
223
224 /**
225 * Convenience function to find matching ApplicationInfos for removal.
226 */
227 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
228 ComponentName removeComponent = item.intent.getComponent();
229 final int length = list.size();
230 for (int i = 0; i < length; ++i) {
231 ApplicationInfo info = list.get(i);
232 if (info.intent.getComponent().equals(removeComponent)) {
233 return i;
234 }
235 }
236 return -1;
237 }
238
Winson Chung80baf5a2010-08-09 16:03:15 -0700239 public void update() {
240 Context context = getContext();
241
242 // get the list of widgets
243 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
244 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
245 @Override
246 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
247 return object1.label.compareTo(object2.label);
248 }
249 });
250
251 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
252 @Override
253 public int compare(ResolveInfo object1, ResolveInfo object2) {
254 return object1.loadLabel(mPackageManager).toString().compareTo(
255 object2.loadLabel(mPackageManager).toString());
256 }
257 };
258
259 // get the list of live folder intents
260 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
261 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
262
263 // manually create a separate entry for creating a folder in Launcher
264 ResolveInfo folder = new ResolveInfo();
265 folder.icon = R.drawable.ic_launcher_folder;
266 folder.labelRes = R.string.group_folder;
267 folder.resolvePackageName = context.getPackageName();
268 mFolderList.add(0, folder);
269 Collections.sort(mFolderList, resolveInfoComparator);
270
271 // get the list of shortcuts
272 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
273 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
274 Collections.sort(mShortcutList, resolveInfoComparator);
275
Winson Chunge8878e32010-09-15 20:37:09 -0700276 // get the list of wallpapers
277 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
278 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
279 Collections.sort(mWallpaperList, resolveInfoComparator);
280
Winson Chung241c3b42010-08-25 16:53:03 -0700281 // reset the icon cache
282 mPageViewIconCache.clear();
283
Winson Chunge3193b92010-09-10 11:44:42 -0700284 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700285 invalidatePageData();
286 }
287
288 public void setDragController(DragController dragger) {
289 mDragController = dragger;
290 }
291
292 public void setCustomizationFilter(CustomizationType filterType) {
293 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700294 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700296
297 // End the current choice mode so that we don't carry selections across tabs
298 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 }
300
301 @Override
302 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700303 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 }
305
306 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700307 public void onClick(View v) {
308 if (!v.isInTouchMode()) {
309 return;
310 }
311
Winson Chungd0d43012010-09-26 17:26:45 -0700312 // On certain pages, we allow single tap to mark items as selected so that they can be
313 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700314 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700315 switch (mCustomizationType) {
316 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700317 mChoiceModeTitleText = R.string.cab_widget_selection_text;
318 enterChoiceMode = true;
319 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700320 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700321 mChoiceModeTitleText = R.string.cab_app_selection_text;
322 enterChoiceMode = true;
323 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700324 case FolderCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700325 mChoiceModeTitleText = R.string.cab_folder_selection_text;
326 enterChoiceMode = true;
327 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700328 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700329 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
330 enterChoiceMode = true;
331 break;
332 default:
333 break;
334 }
Winson Chungd0d43012010-09-26 17:26:45 -0700335
Michael Jurka3125d9d2010-09-27 11:30:20 -0700336 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700337 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700338
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700339 Workspace w = mLauncher.getWorkspace();
340 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
341 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700342
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700343 animateClickFeedback(v, new Runnable() {
344 @Override
345 public void run() {
346 mLauncher.addExternalItemToScreen(itemInfo, cl);
347 }
348 });
Winson Chungd0d43012010-09-26 17:26:45 -0700349 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700350 }
351
352 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700353 switch (mCustomizationType) {
354 case WallpaperCustomization:
355 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700356 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700357 animateClickFeedback(v, new Runnable() {
358 @Override
359 public void run() {
360 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700361 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700362 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
363 ComponentName name = new ComponentName(info.activityInfo.packageName,
364 info.activityInfo.name);
365 createWallpapersIntent.setComponent(name);
366 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700367 }
368 });
Winson Chungd0d43012010-09-26 17:26:45 -0700369 break;
370 default:
371 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700372 }
373 }
374
375 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700376 public boolean onLongClick(View v) {
377 if (!v.isInTouchMode()) {
378 return false;
379 }
380
Winson Chungd0d43012010-09-26 17:26:45 -0700381 // End the current choice mode before we start dragging anything
382 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
383 endChoiceMode();
384 }
385
386 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700387 switch (mCustomizationType) {
388 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700389 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700390 final LinearLayout l = (LinearLayout) v;
391 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700392 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
393 Bitmap.Config.ARGB_8888);
394 Canvas c = new Canvas(b);
395 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700396 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700397
Michael Jurka3e7c7632010-10-02 16:01:03 -0700398 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
399 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
400 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700401 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700402
403 // Cleanup the icon
404 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 return true;
406 case FolderCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700407 if (v.getTag() instanceof UserFolderInfo) {
408 // The UserFolderInfo tag is only really used for live folders
409 UserFolderInfo folderInfo = (UserFolderInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700410 mDragController.startDrag(
411 v, this, folderInfo, DragController.DRAG_ACTION_COPY, null);
412 } else {
Winson Chungd0d43012010-09-26 17:26:45 -0700413 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700414 mDragController.startDrag(
415 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
416 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700417 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700418 return true;
419 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700420 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700421 mDragController.startDrag(
422 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700423 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700424 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700425 case ApplicationCustomization:
426 // Pick up the application for dropping
427 ApplicationInfo app = (ApplicationInfo) v.getTag();
428 app = new ApplicationInfo(app);
429
430 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700431 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700432 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700433 }
434 return false;
435 }
436
Winson Chunge3193b92010-09-10 11:44:42 -0700437 /**
438 * Pre-processes the layout of the different widget pages.
439 * @return the number of pages of widgets that we have
440 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700441 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700442 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700443
Winson Chunge3193b92010-09-10 11:44:42 -0700444 // create a new page for the first set of widgets
445 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700446 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700447 mWidgetPages.add(newPage);
448
449 // do this until we have no more widgets to lay out
450 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
451 final int widgetCount = mWidgetList.size();
452 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700454 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700455
Winson Chunge3193b92010-09-10 11:44:42 -0700456 // determine the size of the current widget
457 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
458 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700459
Winson Chunge3193b92010-09-10 11:44:42 -0700460 // create a new page if necessary
461 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
462 numCellsInRow = 0;
463 newPage = new ArrayList<AppWidgetProviderInfo>();
464 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700465 }
466
Winson Chunge3193b92010-09-10 11:44:42 -0700467 // add the item to the current page
468 newPage.add(info);
469 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700470 }
Winson Chunge3193b92010-09-10 11:44:42 -0700471
Winson Chung80baf5a2010-08-09 16:03:15 -0700472 return mWidgetPages.size();
473 }
474
Winson Chunge3193b92010-09-10 11:44:42 -0700475 /**
476 * This method will extract the preview image specified by the widget developer (if it exists),
477 * otherwise, it will try to generate a default image preview with the widget's package icon.
478 * @return the drawable will be used and sized in the ImageView to represent the widget
479 */
480 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700481 PackageManager packageManager = mLauncher.getPackageManager();
482 String packageName = info.provider.getPackageName();
483 Drawable drawable = null;
484 if (info.previewImage != 0) {
485 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
486 if (drawable == null) {
487 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
488 + " for provider: " + info.provider);
489 } else {
490 return drawable;
491 }
492 }
493
494 // If we don't have a preview image, create a default one
495 if (drawable == null) {
496 Resources resources = mLauncher.getResources();
497
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700499 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
500 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
501 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
502 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700503 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
504 mCanvas.setBitmap(bitmap);
505 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
506 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
507
508 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
509 background.setBounds(0, 0, width, height);
510 background.draw(mCanvas);
511
512 // Draw the icon vertically centered, flush left
513 try {
514 Rect tmpRect = new Rect();
515 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700516 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700517 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700518 }
519 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700520 icon = resources.getDrawable(R.drawable.ic_launcher_application);
521 }
522 background.getPadding(tmpRect);
523
Winson Chunge3193b92010-09-10 11:44:42 -0700524 final int iconSize = minDim / 2;
525 final int offset = iconSize / 4;
526 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 icon.draw(mCanvas);
528 } catch (Resources.NotFoundException e) {
529 // if we can't find the icon, then just don't draw it
530 }
531
Winson Chungb3347bb2010-08-19 14:51:28 -0700532 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700533 }
534 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
535 return drawable;
536 }
537
538 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700539 layout.setCellCount(mCellCountX, mCellCountY);
540 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
541 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700542 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700543 }
544
Winson Chunge3193b92010-09-10 11:44:42 -0700545 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700546 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700547 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
548
549 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
550 }
551
Winson Chung80baf5a2010-08-09 16:03:15 -0700552 private void syncWidgetPages() {
553 if (mWidgetList == null) return;
554
Winson Chunge3193b92010-09-10 11:44:42 -0700555 // we need to repopulate with the LinearLayout layout for the widget pages
556 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700557 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700558 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700559 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700560 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700561 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
562 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700563
564 // Temporary change to prevent the last page from being too small (and items bleeding
565 // onto it). We can remove this once we properly fix the fading algorithm
566 if (i < numPages - 1) {
567 addView(layout, new LinearLayout.LayoutParams(
568 LinearLayout.LayoutParams.WRAP_CONTENT,
569 LinearLayout.LayoutParams.MATCH_PARENT));
570 } else {
571 addView(layout, new LinearLayout.LayoutParams(
572 LinearLayout.LayoutParams.MATCH_PARENT,
573 LinearLayout.LayoutParams.MATCH_PARENT));
574 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700575 }
576 }
577
578 private void syncWidgetPageItems(int page) {
579 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700580 LinearLayout layout = (LinearLayout) getChildAt(page);
581 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700582 final int count = list.size();
583 layout.removeAllViews();
584 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700585 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700586 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700587 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
588 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700589 createItemInfo.minWidth = info.minWidth;
590 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700591
Winson Chunge3193b92010-09-10 11:44:42 -0700592 LinearLayout l = (LinearLayout) mInflater.inflate(
593 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700594 l.setTag(createItemInfo);
595 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700596 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700597
Winson Chunge3193b92010-09-10 11:44:42 -0700598 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700599
600 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
601 final int hSpan = spans[0];
602 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700603
Winson Chungd0d43012010-09-26 17:26:45 -0700604 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700605 image.setMaxWidth(mMaxWidgetWidth);
606 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700607 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700608 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700609 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700610 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700611
612 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700613 }
614 }
615
616 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700617 // we need to repopulate with PagedViewCellLayouts
618 removeAllViews();
619
Winson Chung80baf5a2010-08-09 16:03:15 -0700620 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700621 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700622 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700623 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
624 setupPage(layout);
625 addView(layout);
626 }
627 }
628
629 private void syncListPageItems(int page, List<ResolveInfo> list) {
630 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700631 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700632 int startIndex = page * numCells;
633 int endIndex = Math.min(startIndex + numCells, list.size());
634 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
635 // TODO: we can optimize by just re-applying to existing views
636 layout.removeAllViews();
637 for (int i = startIndex; i < endIndex; ++i) {
638 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700639 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
640
Winson Chung241c3b42010-08-25 16:53:03 -0700641 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
642 R.layout.customize_paged_view_item, layout, false);
643 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700644 switch (mCustomizationType) {
645 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700646 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700647 break;
648 case FolderCustomization:
649 if (info.labelRes != R.string.group_folder) {
650 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
651 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
652 info.activityInfo.name);
653 icon.setTag(createItemInfo);
654 } else {
655 UserFolderInfo folderInfo = new UserFolderInfo();
656 folderInfo.title = getResources().getText(R.string.folder_name);
657 icon.setTag(folderInfo);
658 }
659 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700660 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700661 break;
662 case ShortcutCustomization:
663 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
664 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
665 info.activityInfo.name);
666 icon.setTag(createItemInfo);
667 icon.setOnClickListener(this);
668 icon.setOnLongClickListener(this);
669 break;
670 default:
671 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700672 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700673
674 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700675 final int x = index % mCellCountX;
676 final int y = index / mCellCountX;
677 setupPage(layout);
678 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
679 }
680 }
681
682 private void syncAppPages() {
683 if (mApps == null) return;
684
685 // We need to repopulate with PagedViewCellLayouts
686 removeAllViews();
687
688 // Ensure that we have the right number of pages
689 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
690 for (int i = 0; i < numPages; ++i) {
691 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
692 setupPage(layout);
693 addView(layout);
694 }
695 }
696
697 private void syncAppPageItems(int page) {
698 if (mApps == null) return;
699
700 // ensure that we have the right number of items on the pages
701 int numCells = mCellCountX * mCellCountY;
702 int startIndex = page * numCells;
703 int endIndex = Math.min(startIndex + numCells, mApps.size());
704 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
705 // TODO: we can optimize by just re-applying to existing views
706 layout.removeAllViews();
707 for (int i = startIndex; i < endIndex; ++i) {
708 final ApplicationInfo info = mApps.get(i);
709 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
710 R.layout.all_apps_paged_view_application, layout, false);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700711 icon.applyFromApplicationInfo(info, mPageViewIconCache, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700712 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700713 icon.setOnLongClickListener(this);
714
715 final int index = i - startIndex;
716 final int x = index % mCellCountX;
717 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700718 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700719 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700720 }
721 }
722
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 @Override
724 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700725 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700726 switch (mCustomizationType) {
727 case WidgetCustomization:
728 syncWidgetPages();
729 break;
730 case FolderCustomization:
731 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700732 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700733 break;
734 case ShortcutCustomization:
735 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700736 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700737 break;
738 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700739 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700740 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700741 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700742 case ApplicationCustomization:
743 syncAppPages();
744 centerPagedViewCellLayouts = false;
745 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 default:
747 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700748 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700749 break;
750 }
751
752 // only try and center the page if there is one page
753 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700754 if (centerPagedViewCellLayouts) {
755 if (childCount == 1) {
756 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
757 layout.enableCenteredContent(true);
758 } else {
759 for (int i = 0; i < childCount; ++i) {
760 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
761 layout.enableCenteredContent(false);
762 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700763 }
764 }
765
766 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700767 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700768 }
769
770 @Override
771 public void syncPageItems(int page) {
772 switch (mCustomizationType) {
773 case WidgetCustomization:
774 syncWidgetPageItems(page);
775 break;
776 case FolderCustomization:
777 syncListPageItems(page, mFolderList);
778 break;
779 case ShortcutCustomization:
780 syncListPageItems(page, mShortcutList);
781 break;
782 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700783 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700784 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700785 case ApplicationCustomization:
786 syncAppPageItems(page);
787 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700788 }
789 }
Winson Chunge3193b92010-09-10 11:44:42 -0700790
Winson Chungd0d43012010-09-26 17:26:45 -0700791 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700792 protected int getAssociatedLowerPageBound(int page) {
793 return 0;
794 }
Winson Chungd0d43012010-09-26 17:26:45 -0700795 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700796 protected int getAssociatedUpperPageBound(int page) {
797 return getChildCount();
798 }
Winson Chungd0d43012010-09-26 17:26:45 -0700799
800 @Override
801 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700802 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700803 return true;
804 }
805
806 @Override
807 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
808 return true;
809 }
810
811 @Override
812 public void onDestroyActionMode(ActionMode mode) {
813 endChoiceMode();
814 }
815
816 @Override
817 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
818 return false;
819 }
820
Winson Chung80baf5a2010-08-09 16:03:15 -0700821}