blob: 4eb852c60aa489254a097909fd2fdcaa9ff08cb0 [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 Jurka72b079e2010-12-10 01:03:53 -080019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
Adam Cohen7b9d3a62010-12-07 21:49:34 -080021import org.xmlpull.v1.XmlPullParser;
22
Adam Cohen120980b2010-12-08 11:05:37 -080023import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080024import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
27import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080028import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070029import android.appwidget.AppWidgetManager;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080039import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.graphics.Bitmap;
Michael Jurkaaf91de02010-11-23 16:23:58 -080041import android.graphics.Bitmap.Config;
Winson Chungcd4bc492010-12-09 18:52:32 -080042import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.util.AttributeSet;
45import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080046import android.util.Slog;
47import android.util.TypedValue;
48import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070049import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070050import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070051import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070052import android.view.Menu;
53import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Winson Chunge3193b92010-09-10 11:44:42 -070057import android.widget.ImageView;
58import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080059import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070060
Michael Jurka72b079e2010-12-10 01:03:53 -080061import java.util.ArrayList;
62import java.util.Collections;
63import java.util.Comparator;
64import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070065
Adam Cohen7b9d3a62010-12-07 21:49:34 -080066
Michael Jurka72b079e2010-12-10 01:03:53 -080067public class CustomizePagedView extends PagedViewWithDraggableItems
68 implements View.OnClickListener, DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070069
70 public enum CustomizationType {
71 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070072 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070073 WallpaperCustomization,
74 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070075 }
76
77 private static final String TAG = "CustomizeWorkspace";
Winson Chung80baf5a2010-08-09 16:03:15 -070078
79 private Launcher mLauncher;
80 private DragController mDragController;
81 private PackageManager mPackageManager;
82
83 private CustomizationType mCustomizationType;
84
Winson Chunge3193b92010-09-10 11:44:42 -070085 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
86 private PagedViewCellLayout mWorkspaceWidgetLayout;
87
88 // The mapping between the pages and the widgets that will be laid out on them
89 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
90
Winson Chung45e1d6e2010-11-09 17:19:49 -080091 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070092 private int mMaxWidgetWidth;
93
Winson Chung45e1d6e2010-11-09 17:19:49 -080094 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070095 private int mMaxWidgetsCellHSpan;
96
Winson Chung45e1d6e2010-11-09 17:19:49 -080097 // The size of the items on the wallpaper tab
98 private int mWallpaperCellHSpan;
99
Winson Chung78bd53c2010-12-09 13:50:24 -0800100 // The max number of wallpaper cells to take a "page" of wallpaper items
101 private int mMaxWallpaperCellHSpan;
102
Winson Chunge3193b92010-09-10 11:44:42 -0700103 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700105 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700106 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700107 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700108
Winson Chunge3193b92010-09-10 11:44:42 -0700109 private static final int sMinWidgetCellHSpan = 2;
110 private static final int sMaxWidgetCellHSpan = 4;
111
Michael Jurka3125d9d2010-09-27 11:30:20 -0700112 private int mChoiceModeTitleText;
113
Winson Chunge3193b92010-09-10 11:44:42 -0700114 // The scale factor for widget previews inside the widget drawer
115 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700116
117 private final Canvas mCanvas = new Canvas();
118 private final LayoutInflater mInflater;
119
Adam Cohen120980b2010-12-08 11:05:37 -0800120 private final float mTmpFloatPos[] = new float[2];
121 private final float ANIMATION_SCALE = 0.5f;
122 private final int ANIMATION_DURATION = 400;
123 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
124 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
125
Winson Chung80baf5a2010-08-09 16:03:15 -0700126 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700127 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700128 }
129
130 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700131 this(context, attrs, 0);
132 }
133
134 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
135 super(context, attrs, defStyle);
136
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700137 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800138 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
139 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800140 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700141 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
142 a.recycle();
143 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
144 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
145 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700146 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800147
Winson Chung80baf5a2010-08-09 16:03:15 -0700148 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700149 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
150 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700151 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700152
Michael Jurka7426c422010-11-11 15:23:47 -0800153 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800154 setDragSlopeThreshold(
155 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800156
Winson Chung80baf5a2010-08-09 16:03:15 -0700157 setVisibility(View.GONE);
158 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700159 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700160 }
161
Winson Chung7da10252010-10-28 16:07:04 -0700162 @Override
163 protected void init() {
164 super.init();
165 mCenterPagesVertically = false;
166 }
167
Winson Chung80baf5a2010-08-09 16:03:15 -0700168 public void setLauncher(Launcher launcher) {
169 Context context = getContext();
170 mLauncher = launcher;
171 mPackageManager = context.getPackageManager();
172 }
173
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700174 /**
175 * Sets the list of applications that launcher has loaded.
176 */
177 public void setApps(ArrayList<ApplicationInfo> list) {
178 mApps = list;
179 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700180
181 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700182 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700183 }
184
185 /**
186 * Convenience function to add new items to the set of applications that were previously loaded.
187 * Called by both updateApps() and setApps().
188 */
189 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
190 // we add it in place, in alphabetical order
191 final int count = list.size();
192 for (int i = 0; i < count; ++i) {
193 final ApplicationInfo info = list.get(i);
194 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
195 if (index < 0) {
196 mApps.add(-(index + 1), info);
197 }
198 }
199 }
200
201 /**
202 * Adds new applications to the loaded list, and notifies the paged view to update itself.
203 */
204 public void addApps(ArrayList<ApplicationInfo> list) {
205 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700206
207 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700208 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700209 }
210
211 /**
212 * Convenience function to remove items to the set of applications that were previously loaded.
213 * Called by both updateApps() and removeApps().
214 */
215 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
216 // loop through all the apps and remove apps that have the same component
217 final int length = list.size();
218 for (int i = 0; i < length; ++i) {
219 final ApplicationInfo info = list.get(i);
220 int removeIndex = findAppByComponent(mApps, info);
221 if (removeIndex > -1) {
222 mApps.remove(removeIndex);
223 mPageViewIconCache.removeOutline(info);
224 }
225 }
226 }
227
228 /**
229 * Removes applications from the loaded list, and notifies the paged view to update itself.
230 */
231 public void removeApps(ArrayList<ApplicationInfo> list) {
232 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700233
234 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700235 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700236 }
237
238 /**
239 * Updates a set of applications from the loaded list, and notifies the paged view to update
240 * itself.
241 */
242 public void updateApps(ArrayList<ApplicationInfo> list) {
243 // We remove and re-add the updated applications list because it's properties may have
244 // changed (ie. the title), and this will ensure that the items will be in their proper
245 // place in the list.
246 removeAppsWithoutInvalidate(list);
247 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700248
249 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700250 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700251 }
252
253 /**
254 * Convenience function to find matching ApplicationInfos for removal.
255 */
256 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
257 ComponentName removeComponent = item.intent.getComponent();
258 final int length = list.size();
259 for (int i = 0; i < length; ++i) {
260 ApplicationInfo info = list.get(i);
261 if (info.intent.getComponent().equals(removeComponent)) {
262 return i;
263 }
264 }
265 return -1;
266 }
267
Winson Chung80baf5a2010-08-09 16:03:15 -0700268 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 // get the list of widgets
270 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
271 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
272 @Override
273 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
274 return object1.label.compareTo(object2.label);
275 }
276 });
277
278 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
279 @Override
280 public int compare(ResolveInfo object1, ResolveInfo object2) {
281 return object1.loadLabel(mPackageManager).toString().compareTo(
282 object2.loadLabel(mPackageManager).toString());
283 }
284 };
285
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 // get the list of shortcuts
287 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
288 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
289 Collections.sort(mShortcutList, resolveInfoComparator);
290
Winson Chunge8878e32010-09-15 20:37:09 -0700291 // get the list of wallpapers
292 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800293 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
294 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700295 Collections.sort(mWallpaperList, resolveInfoComparator);
296
Winson Chung10fefb12010-11-01 11:57:06 -0700297 invalidatePageDataAndIconCache();
298 }
299
300 private void invalidatePageDataAndIconCache() {
301 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700302 mPageViewIconCache.clear();
303
Winson Chunge3193b92010-09-10 11:44:42 -0700304 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 invalidatePageData();
306 }
307
308 public void setDragController(DragController dragger) {
309 mDragController = dragger;
310 }
311
312 public void setCustomizationFilter(CustomizationType filterType) {
313 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700314 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800315 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700317
318 // End the current choice mode so that we don't carry selections across tabs
319 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 }
321
322 @Override
323 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700324 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 }
326
327 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800328 public void onDragViewVisible() {
329 }
330
Adam Cohen120980b2010-12-08 11:05:37 -0800331 class ScaleAlphaInterpolator implements Interpolator {
332 public float getInterpolation(float input) {
333 float pivot = 0.5f;
334 if (input < pivot) {
335 return 0;
336 } else {
337 return (input - pivot)/(1 - pivot);
338 }
339 }
340 }
341
342 private void animateItemOntoScreen(View dragView,
343 final CellLayout layout, final ItemInfo info) {
344 mTmpFloatPos[0] = layout.getWidth() / 2;
345 mTmpFloatPos[1] = layout.getHeight() / 2;
346 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
347
348 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
349 final View dragCopy = dragLayer.createDragView(dragView);
350 dragCopy.setAlpha(1.0f);
351
352 int dragViewWidth = dragView.getMeasuredWidth();
353 int dragViewHeight = dragView.getMeasuredHeight();
354 float heightOffset = 0;
355 float widthOffset = 0;
356
357 if (dragView instanceof ImageView) {
358 Drawable d = ((ImageView) dragView).getDrawable();
359 int width = d.getIntrinsicWidth();
360 int height = d.getIntrinsicHeight();
361
362 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
363 float f = (dragViewWidth / (width * 1.0f));
364 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
365 } else {
366 float f = (dragViewHeight / (height * 1.0f));
367 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
368 }
369 }
370
371 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
372 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
373
374 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
375 PropertyValuesHolder.ofFloat("x", toX),
376 PropertyValuesHolder.ofFloat("y", toY));
377 posAnim.setInterpolator(mQuintEaseOutInterpolator);
378 posAnim.setDuration(ANIMATION_DURATION);
379
Patrick Dubroy047379a2010-12-19 22:02:04 -0800380 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800381 public void onAnimationEnd(Animator animation) {
382 dragLayer.removeView(dragCopy);
383 mLauncher.addExternalItemToScreen(info, layout);
384 post(new Runnable() {
385 public void run() {
386 layout.animateDrop();
387 }
388 });
389 }
390 });
391
392 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
393 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
394 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
395 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
396 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
397 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
398
399 posAnim.start();
400 scaleAlphaAnim.start();
401 }
402
Patrick Dubroya669d792010-11-23 14:40:33 -0800403 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800404 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800405 // Return early if this is not initiated from a touch
406 if (!v.isInTouchMode()) return;
407 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800408 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700409
Winson Chungd0d43012010-09-26 17:26:45 -0700410 // On certain pages, we allow single tap to mark items as selected so that they can be
411 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700412 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700413 switch (mCustomizationType) {
414 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700415 mChoiceModeTitleText = R.string.cab_widget_selection_text;
416 enterChoiceMode = true;
417 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700418 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700419 mChoiceModeTitleText = R.string.cab_app_selection_text;
420 enterChoiceMode = true;
421 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700422 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700423 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
424 enterChoiceMode = true;
425 break;
426 default:
427 break;
428 }
Winson Chungd0d43012010-09-26 17:26:45 -0700429
Michael Jurka3125d9d2010-09-27 11:30:20 -0700430 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700431 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700432
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700433 Workspace w = mLauncher.getWorkspace();
434 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
435 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800436 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700437
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700438 animateClickFeedback(v, new Runnable() {
439 @Override
440 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800441 cl.calculateSpans(itemInfo);
442 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
443 animateItemOntoScreen(dragView, cl, itemInfo);
444 } else {
445 mLauncher.showOutOfSpaceMessage();
446 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700447 }
448 });
Winson Chungd0d43012010-09-26 17:26:45 -0700449 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700450 }
451
452 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700453 switch (mCustomizationType) {
454 case WallpaperCustomization:
455 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700456 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700457 animateClickFeedback(v, new Runnable() {
458 @Override
459 public void run() {
460 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700461 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700462 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
463 ComponentName name = new ComponentName(info.activityInfo.packageName,
464 info.activityInfo.name);
465 createWallpapersIntent.setComponent(name);
466 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700467 }
468 });
Winson Chungd0d43012010-09-26 17:26:45 -0700469 break;
470 default:
471 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700472 }
473 }
474
Winson Chungcd4bc492010-12-09 18:52:32 -0800475 Bitmap drawableToBitmap(Drawable d, View v) {
476 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800477 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800478 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800479 d.draw(c);
480 return b;
481 }
482
Adam Cohen120980b2010-12-08 11:05:37 -0800483 private View getDragView(View v) {
484 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
485 v.findViewById(R.id.widget_preview) : v;
486 }
487
Michael Jurka72b079e2010-12-10 01:03:53 -0800488 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700489 // End the current choice mode before we start dragging anything
490 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
491 endChoiceMode();
492 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800493 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700494
Winson Chung80baf5a2010-08-09 16:03:15 -0700495 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800496 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800497 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800498 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800499 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
500 final Drawable icon = i.getDrawable();
501 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700502 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700503
Michael Jurkad3ef3062010-11-23 16:23:58 -0800504 int[] spanXY = CellLayout.rectToCell(
505 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
506 createWidgetInfo.spanX = spanXY[0];
507 createWidgetInfo.spanY = spanXY[1];
508 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
509 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800510 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
511 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700512 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800513 }
514 case ShortcutCustomization: {
515 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800516 final TextView tv = (TextView) v;
517 final Drawable icon = tv.getCompoundDrawables()[1];
518 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800519 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800520
Michael Jurkad3ef3062010-11-23 16:23:58 -0800521 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800522 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
523 null);
524 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700525 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800526 }
527 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700528 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800529 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800530 final TextView tv = (TextView) v;
531 final Drawable icon = tv.getCompoundDrawables()[1];
532 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700533 ApplicationInfo app = (ApplicationInfo) v.getTag();
534 app = new ApplicationInfo(app);
535
Michael Jurkad3ef3062010-11-23 16:23:58 -0800536 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800537 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
538 b.recycle();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700539 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700540 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 return false;
543 }
544
Winson Chunge3193b92010-09-10 11:44:42 -0700545 /**
546 * Pre-processes the layout of the different widget pages.
547 * @return the number of pages of widgets that we have
548 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700549 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700550 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700551
Winson Chunge3193b92010-09-10 11:44:42 -0700552 // create a new page for the first set of widgets
553 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700554 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700555 mWidgetPages.add(newPage);
556
557 // do this until we have no more widgets to lay out
558 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
559 final int widgetCount = mWidgetList.size();
560 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700561 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700562 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700563
Winson Chunge3193b92010-09-10 11:44:42 -0700564 // determine the size of the current widget
565 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
566 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700567
Winson Chunge3193b92010-09-10 11:44:42 -0700568 // create a new page if necessary
569 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
570 numCellsInRow = 0;
571 newPage = new ArrayList<AppWidgetProviderInfo>();
572 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700573 }
574
Winson Chunge3193b92010-09-10 11:44:42 -0700575 // add the item to the current page
576 newPage.add(info);
577 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 }
Winson Chunge3193b92010-09-10 11:44:42 -0700579
Winson Chung80baf5a2010-08-09 16:03:15 -0700580 return mWidgetPages.size();
581 }
582
Winson Chunge3193b92010-09-10 11:44:42 -0700583 /**
Winson Chung7da10252010-10-28 16:07:04 -0700584 * Helper function to draw a drawable to the specified canvas with the specified bounds.
585 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800586 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700587 if (bitmap != null) mCanvas.setBitmap(bitmap);
588 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800589 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700590 d.draw(mCanvas);
591 mCanvas.restore();
592 }
593
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800594 /*
595 * This method fetches an xml file specified in the manifest identified by
596 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
597 * an image which will be used as the wallpaper preview for an activity
598 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
599 * in the customize drawer.
600 */
601 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800602 ActivityInfo activityInfo = ri.activityInfo;
603 XmlResourceParser parser = null;
604 try {
605 parser = activityInfo.loadXmlMetaData(mPackageManager,
606 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
607 if (parser == null) {
608 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
609 + "wallpaper provider '" + component + '\'');
610 return null;
611 }
612
613 AttributeSet attrs = Xml.asAttributeSet(parser);
614
615 int type;
616 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
617 && type != XmlPullParser.START_TAG) {
618 // drain whitespace, comments, etc.
619 }
620
621 String nodeName = parser.getName();
622 if (!"wallpaper-preview".equals(nodeName)) {
623 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
624 + "wallpaper provider '" + component + '\'');
625 return null;
626 }
627
628 // If metaData was null, we would have returned earlier when getting
629 // the parser No need to do the check here
630 Resources res = mPackageManager.getResourcesForApplication(
631 activityInfo.applicationInfo);
632
633 TypedArray sa = res.obtainAttributes(attrs,
634 com.android.internal.R.styleable.WallpaperPreviewInfo);
635
636 TypedValue value = sa.peekValue(
637 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
638 if (value == null) return null;
639
640 return res.getDrawable(value.resourceId);
641 } catch (Exception e) {
642 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
643 return null;
644 } finally {
645 if (parser != null) parser.close();
646 }
647 }
648
Winson Chung7da10252010-10-28 16:07:04 -0700649 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800650 * This method will extract the preview image specified by the wallpaper source provider (if it
651 * exists) otherwise, it will try to generate a default image preview.
652 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800653 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800654 // To be implemented later: resolving the up-to-date wallpaper thumbnail
655
656 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
657 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
658 Resources resources = mLauncher.getResources();
659
660 // Create a new bitmap to hold the widget preview
661 int width = (int) (dim * sScaleFactor);
662 int height = (int) (dim * sScaleFactor);
663 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800664
665 Drawable background = parseWallpaperPreviewXml(
666 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
667 boolean foundCustomDrawable = background != null;
668
669 if (!foundCustomDrawable) {
670 background = resources.getDrawable(R.drawable.default_widget_preview);
671 }
672
Winson Chung45e1d6e2010-11-09 17:19:49 -0800673 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
674
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800675 // If we don't have a custom icon, we use the app icon on the default background
676 if (!foundCustomDrawable) {
677 try {
678 final IconCache iconCache =
679 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
680 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
681 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800682
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800683 final int iconSize = minDim / 2;
684 final int offset = iconSize / 4;
685 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
686 } catch (Resources.NotFoundException e) {
687 // if we can't find the icon, then just don't draw it
688 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800689 }
690
Winson Chung29d6fea2010-12-01 15:47:31 -0800691 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800692 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
693 return drawable;
694 }
695
696 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700697 * This method will extract the preview image specified by the widget developer (if it exists),
698 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800699 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700700 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800701 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800702 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700703 String packageName = info.provider.getPackageName();
704 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800705 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700706 if (info.previewImage != 0) {
707 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
708 if (drawable == null) {
709 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
710 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700711 }
712 }
713
714 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700715 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
716 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 if (drawable == null) {
718 Resources resources = mLauncher.getResources();
719
Winson Chung80baf5a2010-08-09 16:03:15 -0700720 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700721 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
722 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700723 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
724 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
725 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700726
Winson Chung45e1d6e2010-11-09 17:19:49 -0800727 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700728 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700729 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700730 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700731 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700732 }
733 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 icon = resources.getDrawable(R.drawable.ic_launcher_application);
735 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700736
Winson Chunge3193b92010-09-10 11:44:42 -0700737 final int iconSize = minDim / 2;
738 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800739 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700740 } catch (Resources.NotFoundException e) {
741 // if we can't find the icon, then just don't draw it
742 }
743
Winson Chung29d6fea2010-12-01 15:47:31 -0800744 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700745 } else {
746 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800747 final float imageWidth = drawable.getIntrinsicWidth();
748 final float imageHeight = drawable.getIntrinsicHeight();
749 final float aspect = (float) imageWidth / imageHeight;
750 final int scaledWidth =
751 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
752 final int scaledHeight =
753 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700754 int width;
755 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800756 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700757 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800758 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700759 } else {
760 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800761 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700762 }
763
764 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
765 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
766
Winson Chung29d6fea2010-12-01 15:47:31 -0800767 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700768 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800769 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
770 newDrawable.getIntrinsicHeight());
771 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700772 }
773
774 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700775 layout.setCellCount(mCellCountX, mCellCountY);
776 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
777 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700778 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700779 }
780
Winson Chunge3193b92010-09-10 11:44:42 -0700781 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700782 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700783 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
784
785 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
786 }
787
Winson Chung80baf5a2010-08-09 16:03:15 -0700788 private void syncWidgetPages() {
789 if (mWidgetList == null) return;
790
Winson Chunge3193b92010-09-10 11:44:42 -0700791 // we need to repopulate with the LinearLayout layout for the widget pages
792 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700793 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700794 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800795 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700796 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700797 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
798 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700799
Winson Chunge22a8e92010-11-12 13:40:58 -0800800 addView(layout, new LinearLayout.LayoutParams(
801 LinearLayout.LayoutParams.WRAP_CONTENT,
802 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 }
804 }
805
806 private void syncWidgetPageItems(int page) {
807 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700808 LinearLayout layout = (LinearLayout) getChildAt(page);
809 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700810 final int count = list.size();
811 layout.removeAllViews();
812 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700813 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
814 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800815 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
816 info.minHeight, null);
817 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700818
Winson Chung29d6fea2010-12-01 15:47:31 -0800819 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700820 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800821 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700822 l.setTag(createItemInfo);
823 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800824 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700825 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700826
Winson Chunge3193b92010-09-10 11:44:42 -0700827 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700828 }
829 }
830
Winson Chung45e1d6e2010-11-09 17:19:49 -0800831 private void syncWallpaperPages() {
832 if (mWallpaperList == null) return;
833
834 // We need to repopulate the LinearLayout for the wallpaper pages
835 removeAllViews();
836 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800837 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800838 for (int i = 0; i < numPages; ++i) {
839 LinearLayout layout = new PagedViewExtendedLayout(getContext());
840 layout.setGravity(Gravity.CENTER_HORIZONTAL);
841 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
842 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
843
Winson Chunge22a8e92010-11-12 13:40:58 -0800844 addView(layout, new LinearLayout.LayoutParams(
845 LinearLayout.LayoutParams.WRAP_CONTENT,
846 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800847 }
848 }
849
850 private void syncWallpaperPageItems(int page) {
851 // Load the items on to the pages
852 LinearLayout layout = (LinearLayout) getChildAt(page);
853 layout.removeAllViews();
854 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800855 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800856 final int startIndex = page * numItemsPerPage;
857 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
858 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800859 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800860 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800861
Winson Chung29d6fea2010-12-01 15:47:31 -0800862 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800863 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800864 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800865 l.setTag(info);
866 l.setOnClickListener(this);
867
Winson Chung45e1d6e2010-11-09 17:19:49 -0800868 layout.addView(l);
869 }
870 }
871
Winson Chung80baf5a2010-08-09 16:03:15 -0700872 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700873 // we need to repopulate with PagedViewCellLayouts
874 removeAllViews();
875
Winson Chung80baf5a2010-08-09 16:03:15 -0700876 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700877 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700878 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700879 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
880 setupPage(layout);
881 addView(layout);
882 }
883 }
884
885 private void syncListPageItems(int page, List<ResolveInfo> list) {
886 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700887 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700888 int startIndex = page * numCells;
889 int endIndex = Math.min(startIndex + numCells, list.size());
890 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
891 // TODO: we can optimize by just re-applying to existing views
892 layout.removeAllViews();
893 for (int i = startIndex; i < endIndex; ++i) {
894 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700895 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
896
Winson Chung241c3b42010-08-25 16:53:03 -0700897 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
898 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700899 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800900 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700901 switch (mCustomizationType) {
902 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700903 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700904 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700905 case ShortcutCustomization:
906 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
907 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
908 info.activityInfo.name);
909 icon.setTag(createItemInfo);
910 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800911 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700912 icon.setOnLongClickListener(this);
913 break;
914 default:
915 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700916 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700917
918 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700919 final int x = index % mCellCountX;
920 final int y = index / mCellCountX;
921 setupPage(layout);
922 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
923 }
924 }
925
926 private void syncAppPages() {
927 if (mApps == null) return;
928
929 // We need to repopulate with PagedViewCellLayouts
930 removeAllViews();
931
932 // Ensure that we have the right number of pages
933 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
934 for (int i = 0; i < numPages; ++i) {
935 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
936 setupPage(layout);
937 addView(layout);
938 }
939 }
940
941 private void syncAppPageItems(int page) {
942 if (mApps == null) return;
943
944 // ensure that we have the right number of items on the pages
945 int numCells = mCellCountX * mCellCountY;
946 int startIndex = page * numCells;
947 int endIndex = Math.min(startIndex + numCells, mApps.size());
948 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
949 // TODO: we can optimize by just re-applying to existing views
950 layout.removeAllViews();
951 for (int i = startIndex; i < endIndex; ++i) {
952 final ApplicationInfo info = mApps.get(i);
953 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
954 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700955 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700956 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800957 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700958 icon.setOnLongClickListener(this);
959
960 final int index = i - startIndex;
961 final int x = index % mCellCountX;
962 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700963 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700964 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700965 }
966 }
967
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 @Override
969 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700970 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 switch (mCustomizationType) {
972 case WidgetCustomization:
973 syncWidgetPages();
974 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 case ShortcutCustomization:
976 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700977 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700978 break;
979 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800980 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700981 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700982 case ApplicationCustomization:
983 syncAppPages();
984 centerPagedViewCellLayouts = false;
985 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700986 default:
987 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700988 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700989 break;
990 }
991
992 // only try and center the page if there is one page
993 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700994 if (centerPagedViewCellLayouts) {
995 if (childCount == 1) {
996 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
997 layout.enableCenteredContent(true);
998 } else {
999 for (int i = 0; i < childCount; ++i) {
1000 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1001 layout.enableCenteredContent(false);
1002 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001003 }
1004 }
1005
1006 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001007 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001008 }
1009
1010 @Override
1011 public void syncPageItems(int page) {
1012 switch (mCustomizationType) {
1013 case WidgetCustomization:
1014 syncWidgetPageItems(page);
1015 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001016 case ShortcutCustomization:
1017 syncListPageItems(page, mShortcutList);
1018 break;
1019 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001020 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001021 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001022 case ApplicationCustomization:
1023 syncAppPageItems(page);
1024 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001025 }
1026 }
Winson Chunge3193b92010-09-10 11:44:42 -07001027
Winson Chungd0d43012010-09-26 17:26:45 -07001028 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001029 protected int getAssociatedLowerPageBound(int page) {
1030 return 0;
1031 }
Winson Chungd0d43012010-09-26 17:26:45 -07001032 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001033 protected int getAssociatedUpperPageBound(int page) {
1034 return getChildCount();
1035 }
Winson Chungd0d43012010-09-26 17:26:45 -07001036
1037 @Override
1038 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001039 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001040 return true;
1041 }
1042
1043 @Override
1044 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1045 return true;
1046 }
1047
1048 @Override
1049 public void onDestroyActionMode(ActionMode mode) {
1050 endChoiceMode();
1051 }
1052
1053 @Override
1054 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1055 return false;
1056 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001057}