blob: cc21276be60a814ba4aa7c9215bac2c493002f2e [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 Chung80baf5a2010-08-09 16:03:15 -0700542 }
543
Winson Chunge3193b92010-09-10 11:44:42 -0700544 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700545 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700546 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
547
548 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
549 }
550
Winson Chung80baf5a2010-08-09 16:03:15 -0700551 private void syncWidgetPages() {
552 if (mWidgetList == null) return;
553
Winson Chunge3193b92010-09-10 11:44:42 -0700554 // we need to repopulate with the LinearLayout layout for the widget pages
555 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700556 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700557 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700558 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700559 layout.setGravity(Gravity.CENTER_HORIZONTAL);
560
561 // Temporary change to prevent the last page from being too small (and items bleeding
562 // onto it). We can remove this once we properly fix the fading algorithm
563 if (i < numPages - 1) {
564 addView(layout, new LinearLayout.LayoutParams(
565 LinearLayout.LayoutParams.WRAP_CONTENT,
566 LinearLayout.LayoutParams.MATCH_PARENT));
567 } else {
568 addView(layout, new LinearLayout.LayoutParams(
569 LinearLayout.LayoutParams.MATCH_PARENT,
570 LinearLayout.LayoutParams.MATCH_PARENT));
571 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700572 }
573 }
574
575 private void syncWidgetPageItems(int page) {
576 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700577 LinearLayout layout = (LinearLayout) getChildAt(page);
578 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 final int count = list.size();
580 layout.removeAllViews();
581 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700582 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700583 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700584 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
585 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700586 createItemInfo.minWidth = info.minWidth;
587 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700588
Winson Chunge3193b92010-09-10 11:44:42 -0700589 LinearLayout l = (LinearLayout) mInflater.inflate(
590 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700591 l.setTag(createItemInfo);
592 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700593 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700594
Winson Chunge3193b92010-09-10 11:44:42 -0700595 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700596
597 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
598 final int hSpan = spans[0];
599 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700600
Winson Chungd0d43012010-09-26 17:26:45 -0700601 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700602 image.setMaxWidth(mMaxWidgetWidth);
603 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700604 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700605 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700607 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700608
609 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700610 }
611 }
612
613 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700614 // we need to repopulate with PagedViewCellLayouts
615 removeAllViews();
616
Winson Chung80baf5a2010-08-09 16:03:15 -0700617 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700618 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700619 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700620 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
621 setupPage(layout);
622 addView(layout);
623 }
624 }
625
626 private void syncListPageItems(int page, List<ResolveInfo> list) {
627 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700628 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700629 int startIndex = page * numCells;
630 int endIndex = Math.min(startIndex + numCells, list.size());
631 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
632 // TODO: we can optimize by just re-applying to existing views
633 layout.removeAllViews();
634 for (int i = startIndex; i < endIndex; ++i) {
635 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700636 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
637
Winson Chung241c3b42010-08-25 16:53:03 -0700638 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
639 R.layout.customize_paged_view_item, layout, false);
640 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700641 switch (mCustomizationType) {
642 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700643 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700644 break;
645 case FolderCustomization:
646 if (info.labelRes != R.string.group_folder) {
647 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER;
648 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
649 info.activityInfo.name);
650 icon.setTag(createItemInfo);
651 } else {
652 UserFolderInfo folderInfo = new UserFolderInfo();
653 folderInfo.title = getResources().getText(R.string.folder_name);
654 icon.setTag(folderInfo);
655 }
656 icon.setOnClickListener(this);
Winson Chunge8878e32010-09-15 20:37:09 -0700657 icon.setOnLongClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700658 break;
659 case ShortcutCustomization:
660 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
661 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
662 info.activityInfo.name);
663 icon.setTag(createItemInfo);
664 icon.setOnClickListener(this);
665 icon.setOnLongClickListener(this);
666 break;
667 default:
668 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700669 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700670
671 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700672 final int x = index % mCellCountX;
673 final int y = index / mCellCountX;
674 setupPage(layout);
675 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
676 }
677 }
678
679 private void syncAppPages() {
680 if (mApps == null) return;
681
682 // We need to repopulate with PagedViewCellLayouts
683 removeAllViews();
684
685 // Ensure that we have the right number of pages
686 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
687 for (int i = 0; i < numPages; ++i) {
688 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
689 setupPage(layout);
690 addView(layout);
691 }
692 }
693
694 private void syncAppPageItems(int page) {
695 if (mApps == null) return;
696
697 // ensure that we have the right number of items on the pages
698 int numCells = mCellCountX * mCellCountY;
699 int startIndex = page * numCells;
700 int endIndex = Math.min(startIndex + numCells, mApps.size());
701 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
702 // TODO: we can optimize by just re-applying to existing views
703 layout.removeAllViews();
704 for (int i = startIndex; i < endIndex; ++i) {
705 final ApplicationInfo info = mApps.get(i);
706 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
707 R.layout.all_apps_paged_view_application, layout, false);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700708 icon.applyFromApplicationInfo(info, mPageViewIconCache, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700709 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700710 icon.setOnLongClickListener(this);
711
712 final int index = i - startIndex;
713 final int x = index % mCellCountX;
714 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700715 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700716 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 }
718 }
719
Winson Chung80baf5a2010-08-09 16:03:15 -0700720 @Override
721 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700722 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 switch (mCustomizationType) {
724 case WidgetCustomization:
725 syncWidgetPages();
726 break;
727 case FolderCustomization:
728 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700729 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700730 break;
731 case ShortcutCustomization:
732 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700733 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 break;
735 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700736 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700737 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700738 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700739 case ApplicationCustomization:
740 syncAppPages();
741 centerPagedViewCellLayouts = false;
742 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700743 default:
744 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700745 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 break;
747 }
748
749 // only try and center the page if there is one page
750 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700751 if (centerPagedViewCellLayouts) {
752 if (childCount == 1) {
753 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
754 layout.enableCenteredContent(true);
755 } else {
756 for (int i = 0; i < childCount; ++i) {
757 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
758 layout.enableCenteredContent(false);
759 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700760 }
761 }
762
763 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700764 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700765 }
766
767 @Override
768 public void syncPageItems(int page) {
769 switch (mCustomizationType) {
770 case WidgetCustomization:
771 syncWidgetPageItems(page);
772 break;
773 case FolderCustomization:
774 syncListPageItems(page, mFolderList);
775 break;
776 case ShortcutCustomization:
777 syncListPageItems(page, mShortcutList);
778 break;
779 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700780 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700781 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700782 case ApplicationCustomization:
783 syncAppPageItems(page);
784 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700785 }
786 }
Winson Chunge3193b92010-09-10 11:44:42 -0700787
Winson Chungd0d43012010-09-26 17:26:45 -0700788 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700789 protected int getAssociatedLowerPageBound(int page) {
790 return 0;
791 }
Winson Chungd0d43012010-09-26 17:26:45 -0700792 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700793 protected int getAssociatedUpperPageBound(int page) {
794 return getChildCount();
795 }
Winson Chungd0d43012010-09-26 17:26:45 -0700796
797 @Override
798 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700799 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700800 return true;
801 }
802
803 @Override
804 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
805 return true;
806 }
807
808 @Override
809 public void onDestroyActionMode(ActionMode mode) {
810 endChoiceMode();
811 }
812
813 @Override
814 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
815 return false;
816 }
817
Winson Chung80baf5a2010-08-09 16:03:15 -0700818}