blob: e9a82d61c5337f384acfd2a52abe953c93cd204f [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 Jurkaaf91de02010-11-23 16:23:58 -080019import 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;
Michael Jurkaaf91de02010-11-23 16:23:58 -080032import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070033import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070034import android.util.AttributeSet;
35import android.util.Log;
Winson Chungd0d43012010-09-26 17:26:45 -070036import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070037import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070039import android.view.Menu;
40import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080041import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070042import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.widget.ImageView;
44import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070045
Michael Jurkaaf91de02010-11-23 16:23:58 -080046import java.util.ArrayList;
47import java.util.Collections;
48import java.util.Comparator;
49import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070050
51public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080052 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070053 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070054
55 public enum CustomizationType {
56 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070057 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070058 WallpaperCustomization,
59 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070060 }
61
62 private static final String TAG = "CustomizeWorkspace";
63 private static final boolean DEBUG = false;
64
Michael Jurka7426c422010-11-11 15:23:47 -080065 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070066 private Launcher mLauncher;
67 private DragController mDragController;
68 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080069 private boolean mIsDragging;
70 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070071
72 private CustomizationType mCustomizationType;
73
Winson Chunge3193b92010-09-10 11:44:42 -070074 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
75 private PagedViewCellLayout mWorkspaceWidgetLayout;
76
77 // The mapping between the pages and the widgets that will be laid out on them
78 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
79
Winson Chung45e1d6e2010-11-09 17:19:49 -080080 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070081 private int mMaxWidgetWidth;
82
Winson Chung45e1d6e2010-11-09 17:19:49 -080083 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070084 private int mMaxWidgetsCellHSpan;
85
Winson Chung45e1d6e2010-11-09 17:19:49 -080086 // The size of the items on the wallpaper tab
87 private int mWallpaperCellHSpan;
88
Winson Chunge3193b92010-09-10 11:44:42 -070089 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070090 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -070091 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070092 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -070093 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -070094
Winson Chunge3193b92010-09-10 11:44:42 -070095 private static final int sMinWidgetCellHSpan = 2;
96 private static final int sMaxWidgetCellHSpan = 4;
97
Michael Jurka3125d9d2010-09-27 11:30:20 -070098 private int mChoiceModeTitleText;
99
Winson Chunge3193b92010-09-10 11:44:42 -0700100 // The scale factor for widget previews inside the widget drawer
101 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700102
103 private final Canvas mCanvas = new Canvas();
104 private final LayoutInflater mInflater;
105
106 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700107 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700108 }
109
110 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700111 this(context, attrs, 0);
112 }
113
114 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
115 super(context, attrs, defStyle);
116
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700117 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800118 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
119 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700120 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
121 a.recycle();
122 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
123 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
124 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700125 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800126
Winson Chung80baf5a2010-08-09 16:03:15 -0700127 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700128 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
129 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700131
Michael Jurka7426c422010-11-11 15:23:47 -0800132 final Resources r = context.getResources();
133 mDragSlopeThreshold =
134 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
135
Winson Chung80baf5a2010-08-09 16:03:15 -0700136 setVisibility(View.GONE);
137 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700138 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 }
140
Winson Chung7da10252010-10-28 16:07:04 -0700141 @Override
142 protected void init() {
143 super.init();
144 mCenterPagesVertically = false;
145 }
146
Winson Chung80baf5a2010-08-09 16:03:15 -0700147 public void setLauncher(Launcher launcher) {
148 Context context = getContext();
149 mLauncher = launcher;
150 mPackageManager = context.getPackageManager();
151 }
152
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700153 /**
154 * Sets the list of applications that launcher has loaded.
155 */
156 public void setApps(ArrayList<ApplicationInfo> list) {
157 mApps = list;
158 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700159
160 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700161 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700162 }
163
164 /**
165 * Convenience function to add new items to the set of applications that were previously loaded.
166 * Called by both updateApps() and setApps().
167 */
168 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
169 // we add it in place, in alphabetical order
170 final int count = list.size();
171 for (int i = 0; i < count; ++i) {
172 final ApplicationInfo info = list.get(i);
173 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
174 if (index < 0) {
175 mApps.add(-(index + 1), info);
176 }
177 }
178 }
179
180 /**
181 * Adds new applications to the loaded list, and notifies the paged view to update itself.
182 */
183 public void addApps(ArrayList<ApplicationInfo> list) {
184 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700185
186 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700187 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700188 }
189
190 /**
191 * Convenience function to remove items to the set of applications that were previously loaded.
192 * Called by both updateApps() and removeApps().
193 */
194 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
195 // loop through all the apps and remove apps that have the same component
196 final int length = list.size();
197 for (int i = 0; i < length; ++i) {
198 final ApplicationInfo info = list.get(i);
199 int removeIndex = findAppByComponent(mApps, info);
200 if (removeIndex > -1) {
201 mApps.remove(removeIndex);
202 mPageViewIconCache.removeOutline(info);
203 }
204 }
205 }
206
207 /**
208 * Removes applications from the loaded list, and notifies the paged view to update itself.
209 */
210 public void removeApps(ArrayList<ApplicationInfo> list) {
211 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700212
213 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700214 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700215 }
216
217 /**
218 * Updates a set of applications from the loaded list, and notifies the paged view to update
219 * itself.
220 */
221 public void updateApps(ArrayList<ApplicationInfo> list) {
222 // We remove and re-add the updated applications list because it's properties may have
223 // changed (ie. the title), and this will ensure that the items will be in their proper
224 // place in the list.
225 removeAppsWithoutInvalidate(list);
226 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700227
228 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700229 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700230 }
231
232 /**
233 * Convenience function to find matching ApplicationInfos for removal.
234 */
235 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
236 ComponentName removeComponent = item.intent.getComponent();
237 final int length = list.size();
238 for (int i = 0; i < length; ++i) {
239 ApplicationInfo info = list.get(i);
240 if (info.intent.getComponent().equals(removeComponent)) {
241 return i;
242 }
243 }
244 return -1;
245 }
246
Winson Chung80baf5a2010-08-09 16:03:15 -0700247 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700248 // get the list of widgets
249 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
250 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
251 @Override
252 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
253 return object1.label.compareTo(object2.label);
254 }
255 });
256
257 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
258 @Override
259 public int compare(ResolveInfo object1, ResolveInfo object2) {
260 return object1.loadLabel(mPackageManager).toString().compareTo(
261 object2.loadLabel(mPackageManager).toString());
262 }
263 };
264
Winson Chung80baf5a2010-08-09 16:03:15 -0700265 // get the list of shortcuts
266 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
267 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
268 Collections.sort(mShortcutList, resolveInfoComparator);
269
Winson Chunge8878e32010-09-15 20:37:09 -0700270 // get the list of wallpapers
271 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
272 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
273 Collections.sort(mWallpaperList, resolveInfoComparator);
274
Winson Chung10fefb12010-11-01 11:57:06 -0700275 invalidatePageDataAndIconCache();
276 }
277
278 private void invalidatePageDataAndIconCache() {
279 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700280 mPageViewIconCache.clear();
281
Winson Chunge3193b92010-09-10 11:44:42 -0700282 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700283 invalidatePageData();
284 }
285
286 public void setDragController(DragController dragger) {
287 mDragController = dragger;
288 }
289
290 public void setCustomizationFilter(CustomizationType filterType) {
291 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700292 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800293 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700295
296 // End the current choice mode so that we don't carry selections across tabs
297 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800298 // Reset the touch item (if we are mid-dragging)
299 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700300 }
301
302 @Override
303 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700304 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 }
306
307 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800308 public void onDragViewVisible() {
309 }
310
311 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700312 public void onClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800313 // Return early if this is not initiated from a touch
314 if (!v.isInTouchMode()) return;
315 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800316 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700317
Winson Chungd0d43012010-09-26 17:26:45 -0700318 // On certain pages, we allow single tap to mark items as selected so that they can be
319 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700320 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700321 switch (mCustomizationType) {
322 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700323 mChoiceModeTitleText = R.string.cab_widget_selection_text;
324 enterChoiceMode = true;
325 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700326 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700327 mChoiceModeTitleText = R.string.cab_app_selection_text;
328 enterChoiceMode = true;
329 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700330 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700331 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
332 enterChoiceMode = true;
333 break;
334 default:
335 break;
336 }
Winson Chungd0d43012010-09-26 17:26:45 -0700337
Michael Jurka3125d9d2010-09-27 11:30:20 -0700338 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700339 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700340
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700341 Workspace w = mLauncher.getWorkspace();
342 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
343 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700344
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700345 animateClickFeedback(v, new Runnable() {
346 @Override
347 public void run() {
348 mLauncher.addExternalItemToScreen(itemInfo, cl);
349 }
350 });
Winson Chungd0d43012010-09-26 17:26:45 -0700351 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700352 }
353
354 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700355 switch (mCustomizationType) {
356 case WallpaperCustomization:
357 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700358 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700359 animateClickFeedback(v, new Runnable() {
360 @Override
361 public void run() {
362 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700363 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700364 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
365 ComponentName name = new ComponentName(info.activityInfo.packageName,
366 info.activityInfo.name);
367 createWallpapersIntent.setComponent(name);
368 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700369 }
370 });
Winson Chungd0d43012010-09-26 17:26:45 -0700371 break;
372 default:
373 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700374 }
375 }
376
377 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700378 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800379 // Return early if this is not initiated from a touch
380 if (!v.isInTouchMode()) return false;
381 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800382 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800383 return beginDragging(v);
384 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700385
Michael Jurka7426c422010-11-11 15:23:47 -0800386 @Override
387 public boolean onTouch(View v, MotionEvent event) {
388 mLastTouchedItem = v;
389 return false;
390 }
391
392 /*
393 * Determines if we should change the touch state to start scrolling after the
394 * user moves their touch point too far.
395 */
396 protected void determineScrollingStart(MotionEvent ev) {
397 if (!mIsDragging) super.determineScrollingStart(ev);
398 }
399
400 /*
401 * Determines if we should change the touch state to start dragging after the
402 * user moves their touch point far enough.
403 */
404 protected void determineDraggingStart(MotionEvent ev) {
405 /*
406 * Locally do absolute value. mLastMotionX is set to the y value
407 * of the down event.
408 */
409 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
410 final float x = ev.getX(pointerIndex);
411 final float y = ev.getY(pointerIndex);
412 final int xDiff = (int) Math.abs(x - mLastMotionX);
413 final int yDiff = (int) Math.abs(y - mLastMotionY);
414
415 final int touchSlop = mTouchSlop;
416 boolean yMoved = yDiff > touchSlop;
417 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
418
Winson Chung649a4ca2010-11-18 10:38:13 -0800419 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800420 // Drag if the user moved far enough along the Y axis
421 beginDragging(mLastTouchedItem);
422
423 // Cancel any pending longpress
424 if (mAllowLongPress) {
425 mAllowLongPress = false;
426 // Try canceling the long press. It could also have been scheduled
427 // by a distant descendant, so use the mAllowLongPress flag to block
428 // everything
429 final View currentPage = getPageAt(mCurrentPage);
430 if (currentPage != null) {
431 currentPage.cancelLongPress();
432 }
433 }
434 }
435 }
436
437 @Override
438 public boolean onInterceptTouchEvent(MotionEvent ev) {
439 final int action = ev.getAction();
440 switch (action & MotionEvent.ACTION_MASK) {
441 case MotionEvent.ACTION_DOWN:
442 mIsDragging = false;
443 break;
444 case MotionEvent.ACTION_MOVE:
445 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
446 determineDraggingStart(ev);
447 }
448 break;
449 }
450 return super.onInterceptTouchEvent(ev);
451 }
452
453 @Override
454 public boolean onTouchEvent(MotionEvent ev) {
455 final int action = ev.getAction();
456 switch (action & MotionEvent.ACTION_MASK) {
457 case MotionEvent.ACTION_DOWN:
458 mIsDragging = false;
459 break;
460 case MotionEvent.ACTION_MOVE:
461 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
462 determineDraggingStart(ev);
463 }
464 break;
465 }
466 return super.onTouchEvent(ev);
467 }
468
Michael Jurkaaf91de02010-11-23 16:23:58 -0800469 Bitmap drawableToBitmap(Drawable d) {
470 Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(),
471 Bitmap.Config.ARGB_8888);
472 Canvas c = new Canvas(b);
473 d.draw(c);
474 return b;
475 }
476
Michael Jurka7426c422010-11-11 15:23:47 -0800477 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700478 // End the current choice mode before we start dragging anything
479 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
480 endChoiceMode();
481 }
Michael Jurka7426c422010-11-11 15:23:47 -0800482 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700483
484 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700485 switch (mCustomizationType) {
486 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700487 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700488 final LinearLayout l = (LinearLayout) v;
489 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800490 Bitmap b = drawableToBitmap(icon);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700491 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700492
Michael Jurka3e7c7632010-10-02 16:01:03 -0700493 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
494 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
Patrick Dubroya669d792010-11-23 14:40:33 -0800495 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700496
497 // Cleanup the icon
498 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700499 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700500 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700501 createItemInfo = (PendingAddItemInfo) v.getTag();
Patrick Dubroya669d792010-11-23 14:40:33 -0800502 mDragController.startDrag(v, this, createItemInfo, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700503 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700504 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700505 case ApplicationCustomization:
506 // Pick up the application for dropping
507 ApplicationInfo app = (ApplicationInfo) v.getTag();
508 app = new ApplicationInfo(app);
509
510 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700511 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700512 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700513 }
514 return false;
515 }
516
Winson Chunge3193b92010-09-10 11:44:42 -0700517 /**
518 * Pre-processes the layout of the different widget pages.
519 * @return the number of pages of widgets that we have
520 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700521 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700522 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700523
Winson Chunge3193b92010-09-10 11:44:42 -0700524 // create a new page for the first set of widgets
525 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700527 mWidgetPages.add(newPage);
528
529 // do this until we have no more widgets to lay out
530 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
531 final int widgetCount = mWidgetList.size();
532 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700533 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700534 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700535
Winson Chunge3193b92010-09-10 11:44:42 -0700536 // determine the size of the current widget
537 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
538 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700539
Winson Chunge3193b92010-09-10 11:44:42 -0700540 // create a new page if necessary
541 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
542 numCellsInRow = 0;
543 newPage = new ArrayList<AppWidgetProviderInfo>();
544 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700545 }
546
Winson Chunge3193b92010-09-10 11:44:42 -0700547 // add the item to the current page
548 newPage.add(info);
549 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 }
Winson Chunge3193b92010-09-10 11:44:42 -0700551
Winson Chung80baf5a2010-08-09 16:03:15 -0700552 return mWidgetPages.size();
553 }
554
Winson Chunge3193b92010-09-10 11:44:42 -0700555 /**
Winson Chung7da10252010-10-28 16:07:04 -0700556 * Helper function to draw a drawable to the specified canvas with the specified bounds.
557 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800558 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700559 if (bitmap != null) mCanvas.setBitmap(bitmap);
560 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800561 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700562 d.draw(mCanvas);
563 mCanvas.restore();
564 }
565
566 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800567 * This method will extract the preview image specified by the wallpaper source provider (if it
568 * exists) otherwise, it will try to generate a default image preview.
569 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800570 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800571 // To be implemented later: resolving the up-to-date wallpaper thumbnail
572
573 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
574 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
575 Resources resources = mLauncher.getResources();
576
577 // Create a new bitmap to hold the widget preview
578 int width = (int) (dim * sScaleFactor);
579 int height = (int) (dim * sScaleFactor);
580 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
581 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
582 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
583
584 // Draw the icon flush left
585 try {
586 final IconCache iconCache =
587 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
588 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
589 iconCache.getFullResIcon(info, mPackageManager), mContext));
590
591 final int iconSize = minDim / 2;
592 final int offset = iconSize / 4;
593 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
594 } catch (Resources.NotFoundException e) {
595 // if we can't find the icon, then just don't draw it
596 }
597
Winson Chung29d6fea2010-12-01 15:47:31 -0800598 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800599 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
600 return drawable;
601 }
602
603 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700604 * This method will extract the preview image specified by the widget developer (if it exists),
605 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800606 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700607 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800608 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800609 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700610 String packageName = info.provider.getPackageName();
611 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800612 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700613 if (info.previewImage != 0) {
614 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
615 if (drawable == null) {
616 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
617 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700618 }
619 }
620
621 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700622 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
623 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700624 if (drawable == null) {
625 Resources resources = mLauncher.getResources();
626
Winson Chung80baf5a2010-08-09 16:03:15 -0700627 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700628 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
629 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700630 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
631 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
632 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700633
Winson Chung45e1d6e2010-11-09 17:19:49 -0800634 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700635 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700636 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700637 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700638 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700639 }
640 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700641 icon = resources.getDrawable(R.drawable.ic_launcher_application);
642 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700643
Winson Chunge3193b92010-09-10 11:44:42 -0700644 final int iconSize = minDim / 2;
645 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800646 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700647 } catch (Resources.NotFoundException e) {
648 // if we can't find the icon, then just don't draw it
649 }
650
Winson Chung29d6fea2010-12-01 15:47:31 -0800651 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700652 } else {
653 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800654 final float imageWidth = drawable.getIntrinsicWidth();
655 final float imageHeight = drawable.getIntrinsicHeight();
656 final float aspect = (float) imageWidth / imageHeight;
657 final int scaledWidth =
658 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
659 final int scaledHeight =
660 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700661 int width;
662 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800663 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700664 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800665 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700666 } else {
667 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800668 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700669 }
670
671 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
672 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
673
Winson Chung29d6fea2010-12-01 15:47:31 -0800674 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700675 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800676 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
677 newDrawable.getIntrinsicHeight());
678 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700679 }
680
681 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700682 layout.setCellCount(mCellCountX, mCellCountY);
683 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
684 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700685 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 }
687
Winson Chunge3193b92010-09-10 11:44:42 -0700688 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700689 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700690 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
691
692 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
693 }
694
Winson Chung80baf5a2010-08-09 16:03:15 -0700695 private void syncWidgetPages() {
696 if (mWidgetList == null) return;
697
Winson Chunge3193b92010-09-10 11:44:42 -0700698 // we need to repopulate with the LinearLayout layout for the widget pages
699 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700700 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700701 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800702 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700703 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700704 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
705 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700706
Winson Chunge22a8e92010-11-12 13:40:58 -0800707 addView(layout, new LinearLayout.LayoutParams(
708 LinearLayout.LayoutParams.WRAP_CONTENT,
709 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 }
711 }
712
713 private void syncWidgetPageItems(int page) {
714 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700715 LinearLayout layout = (LinearLayout) getChildAt(page);
716 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 final int count = list.size();
718 layout.removeAllViews();
719 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700720 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
721 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800722 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
723 info.minHeight, null);
724 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700725
Winson Chung29d6fea2010-12-01 15:47:31 -0800726 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700727 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800728 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700729 l.setTag(createItemInfo);
730 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800731 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700732 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700733
Winson Chunge3193b92010-09-10 11:44:42 -0700734 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700735 }
736 }
737
Winson Chung45e1d6e2010-11-09 17:19:49 -0800738 private void syncWallpaperPages() {
739 if (mWallpaperList == null) return;
740
741 // We need to repopulate the LinearLayout for the wallpaper pages
742 removeAllViews();
743 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
744 mMaxWidgetsCellHSpan);
745 for (int i = 0; i < numPages; ++i) {
746 LinearLayout layout = new PagedViewExtendedLayout(getContext());
747 layout.setGravity(Gravity.CENTER_HORIZONTAL);
748 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
749 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
750
Winson Chunge22a8e92010-11-12 13:40:58 -0800751 addView(layout, new LinearLayout.LayoutParams(
752 LinearLayout.LayoutParams.WRAP_CONTENT,
753 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800754 }
755 }
756
757 private void syncWallpaperPageItems(int page) {
758 // Load the items on to the pages
759 LinearLayout layout = (LinearLayout) getChildAt(page);
760 layout.removeAllViews();
761 final int count = mWallpaperList.size();
Winson Chungd28ed492010-11-22 14:34:57 -0800762 final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan;
763 final int startIndex = page * numItemsPerPage;
764 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
765 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800766 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800767 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800768
Winson Chung29d6fea2010-12-01 15:47:31 -0800769 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800770 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800771 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800772 l.setTag(info);
773 l.setOnClickListener(this);
774
Winson Chung45e1d6e2010-11-09 17:19:49 -0800775 layout.addView(l);
776 }
777 }
778
Winson Chung80baf5a2010-08-09 16:03:15 -0700779 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700780 // we need to repopulate with PagedViewCellLayouts
781 removeAllViews();
782
Winson Chung80baf5a2010-08-09 16:03:15 -0700783 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700784 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700785 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700786 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
787 setupPage(layout);
788 addView(layout);
789 }
790 }
791
792 private void syncListPageItems(int page, List<ResolveInfo> list) {
793 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700794 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700795 int startIndex = page * numCells;
796 int endIndex = Math.min(startIndex + numCells, list.size());
797 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
798 // TODO: we can optimize by just re-applying to existing views
799 layout.removeAllViews();
800 for (int i = startIndex; i < endIndex; ++i) {
801 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700802 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
803
Winson Chung241c3b42010-08-25 16:53:03 -0700804 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
805 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700806 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
807 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700808 switch (mCustomizationType) {
809 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700810 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700811 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700812 case ShortcutCustomization:
813 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
814 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
815 info.activityInfo.name);
816 icon.setTag(createItemInfo);
817 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800818 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700819 icon.setOnLongClickListener(this);
820 break;
821 default:
822 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700823 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700824
825 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700826 final int x = index % mCellCountX;
827 final int y = index / mCellCountX;
828 setupPage(layout);
829 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
830 }
831 }
832
833 private void syncAppPages() {
834 if (mApps == null) return;
835
836 // We need to repopulate with PagedViewCellLayouts
837 removeAllViews();
838
839 // Ensure that we have the right number of pages
840 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
841 for (int i = 0; i < numPages; ++i) {
842 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
843 setupPage(layout);
844 addView(layout);
845 }
846 }
847
848 private void syncAppPageItems(int page) {
849 if (mApps == null) return;
850
851 // ensure that we have the right number of items on the pages
852 int numCells = mCellCountX * mCellCountY;
853 int startIndex = page * numCells;
854 int endIndex = Math.min(startIndex + numCells, mApps.size());
855 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
856 // TODO: we can optimize by just re-applying to existing views
857 layout.removeAllViews();
858 for (int i = startIndex; i < endIndex; ++i) {
859 final ApplicationInfo info = mApps.get(i);
860 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
861 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700862 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700863 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800864 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700865 icon.setOnLongClickListener(this);
866
867 final int index = i - startIndex;
868 final int x = index % mCellCountX;
869 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700870 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700871 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700872 }
873 }
874
Winson Chung80baf5a2010-08-09 16:03:15 -0700875 @Override
876 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700877 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700878 switch (mCustomizationType) {
879 case WidgetCustomization:
880 syncWidgetPages();
881 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700882 case ShortcutCustomization:
883 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700884 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 break;
886 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800887 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700888 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700889 case ApplicationCustomization:
890 syncAppPages();
891 centerPagedViewCellLayouts = false;
892 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700893 default:
894 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700895 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700896 break;
897 }
898
899 // only try and center the page if there is one page
900 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700901 if (centerPagedViewCellLayouts) {
902 if (childCount == 1) {
903 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
904 layout.enableCenteredContent(true);
905 } else {
906 for (int i = 0; i < childCount; ++i) {
907 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
908 layout.enableCenteredContent(false);
909 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700910 }
911 }
912
913 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700914 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700915 }
916
917 @Override
918 public void syncPageItems(int page) {
919 switch (mCustomizationType) {
920 case WidgetCustomization:
921 syncWidgetPageItems(page);
922 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 case ShortcutCustomization:
924 syncListPageItems(page, mShortcutList);
925 break;
926 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800927 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700928 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700929 case ApplicationCustomization:
930 syncAppPageItems(page);
931 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700932 }
933 }
Winson Chunge3193b92010-09-10 11:44:42 -0700934
Winson Chungd0d43012010-09-26 17:26:45 -0700935 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700936 protected int getAssociatedLowerPageBound(int page) {
937 return 0;
938 }
Winson Chungd0d43012010-09-26 17:26:45 -0700939 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700940 protected int getAssociatedUpperPageBound(int page) {
941 return getChildCount();
942 }
Winson Chungd0d43012010-09-26 17:26:45 -0700943
944 @Override
945 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700946 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700947 return true;
948 }
949
950 @Override
951 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
952 return true;
953 }
954
955 @Override
956 public void onDestroyActionMode(ActionMode mode) {
957 endChoiceMode();
958 }
959
960 @Override
961 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
962 return false;
963 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700964}