blob: 60f1c90c30bacb6dbd75222c3198b5c087611aa2 [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 Chunga12a2502010-12-20 14:41:35 -0800314 if (getChildCount() > 0) {
315 setCurrentPage(0);
316 updateCurrentPageScroll();
317 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700318
Winson Chunga12a2502010-12-20 14:41:35 -0800319 // End the current choice mode so that we don't carry selections across tabs
320 endChoiceMode();
321 }
322 }
323
324 public CustomizationType getCustomizationFilter() {
325 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700326 }
327
328 @Override
329 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700330 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 }
332
333 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800334 public void onDragViewVisible() {
335 }
336
Adam Cohen120980b2010-12-08 11:05:37 -0800337 class ScaleAlphaInterpolator implements Interpolator {
338 public float getInterpolation(float input) {
339 float pivot = 0.5f;
340 if (input < pivot) {
341 return 0;
342 } else {
343 return (input - pivot)/(1 - pivot);
344 }
345 }
346 }
347
348 private void animateItemOntoScreen(View dragView,
349 final CellLayout layout, final ItemInfo info) {
350 mTmpFloatPos[0] = layout.getWidth() / 2;
351 mTmpFloatPos[1] = layout.getHeight() / 2;
352 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
353
354 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
355 final View dragCopy = dragLayer.createDragView(dragView);
356 dragCopy.setAlpha(1.0f);
357
358 int dragViewWidth = dragView.getMeasuredWidth();
359 int dragViewHeight = dragView.getMeasuredHeight();
360 float heightOffset = 0;
361 float widthOffset = 0;
362
363 if (dragView instanceof ImageView) {
364 Drawable d = ((ImageView) dragView).getDrawable();
365 int width = d.getIntrinsicWidth();
366 int height = d.getIntrinsicHeight();
367
368 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
369 float f = (dragViewWidth / (width * 1.0f));
370 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
371 } else {
372 float f = (dragViewHeight / (height * 1.0f));
373 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
374 }
375 }
376
377 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
378 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
379
380 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
381 PropertyValuesHolder.ofFloat("x", toX),
382 PropertyValuesHolder.ofFloat("y", toY));
383 posAnim.setInterpolator(mQuintEaseOutInterpolator);
384 posAnim.setDuration(ANIMATION_DURATION);
385
Patrick Dubroy047379a2010-12-19 22:02:04 -0800386 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800387 public void onAnimationEnd(Animator animation) {
388 dragLayer.removeView(dragCopy);
389 mLauncher.addExternalItemToScreen(info, layout);
390 post(new Runnable() {
391 public void run() {
392 layout.animateDrop();
393 }
394 });
395 }
396 });
397
398 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
399 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
400 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
401 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
402 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
403 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
404
405 posAnim.start();
406 scaleAlphaAnim.start();
407 }
408
Patrick Dubroya669d792010-11-23 14:40:33 -0800409 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800410 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800411 // Return early if this is not initiated from a touch
412 if (!v.isInTouchMode()) return;
413 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800414 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700415
Winson Chungd0d43012010-09-26 17:26:45 -0700416 // On certain pages, we allow single tap to mark items as selected so that they can be
417 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700418 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700419 switch (mCustomizationType) {
420 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700421 mChoiceModeTitleText = R.string.cab_widget_selection_text;
422 enterChoiceMode = true;
423 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700424 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700425 mChoiceModeTitleText = R.string.cab_app_selection_text;
426 enterChoiceMode = true;
427 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700428 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700429 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
430 enterChoiceMode = true;
431 break;
432 default:
433 break;
434 }
Winson Chungd0d43012010-09-26 17:26:45 -0700435
Michael Jurka3125d9d2010-09-27 11:30:20 -0700436 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700437 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700438
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700439 Workspace w = mLauncher.getWorkspace();
440 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
441 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800442 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700443
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700444 animateClickFeedback(v, new Runnable() {
445 @Override
446 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800447 cl.calculateSpans(itemInfo);
448 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
449 animateItemOntoScreen(dragView, cl, itemInfo);
450 } else {
451 mLauncher.showOutOfSpaceMessage();
452 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700453 }
454 });
Winson Chungd0d43012010-09-26 17:26:45 -0700455 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700456 }
457
458 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700459 switch (mCustomizationType) {
460 case WallpaperCustomization:
461 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700462 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700463 animateClickFeedback(v, new Runnable() {
464 @Override
465 public void run() {
466 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700467 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700468 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
469 ComponentName name = new ComponentName(info.activityInfo.packageName,
470 info.activityInfo.name);
471 createWallpapersIntent.setComponent(name);
472 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700473 }
474 });
Winson Chungd0d43012010-09-26 17:26:45 -0700475 break;
476 default:
477 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700478 }
479 }
480
Winson Chungcd4bc492010-12-09 18:52:32 -0800481 Bitmap drawableToBitmap(Drawable d, View v) {
482 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800483 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800484 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800485 d.draw(c);
486 return b;
487 }
488
Adam Cohen120980b2010-12-08 11:05:37 -0800489 private View getDragView(View v) {
490 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
491 v.findViewById(R.id.widget_preview) : v;
492 }
493
Michael Jurka72b079e2010-12-10 01:03:53 -0800494 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700495 // End the current choice mode before we start dragging anything
496 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
497 endChoiceMode();
498 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800499 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700500
Winson Chung80baf5a2010-08-09 16:03:15 -0700501 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800502 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800503 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800504 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800505 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
506 final Drawable icon = i.getDrawable();
507 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700508 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700509
Michael Jurkad3ef3062010-11-23 16:23:58 -0800510 int[] spanXY = CellLayout.rectToCell(
511 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
512 createWidgetInfo.spanX = spanXY[0];
513 createWidgetInfo.spanY = spanXY[1];
514 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
515 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800516 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
517 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700518 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800519 }
520 case ShortcutCustomization: {
521 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800522 final TextView tv = (TextView) v;
523 final Drawable icon = tv.getCompoundDrawables()[1];
524 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800525 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800526
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800528 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
529 null);
530 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700531 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800532 }
533 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700534 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800536 final TextView tv = (TextView) v;
537 final Drawable icon = tv.getCompoundDrawables()[1];
538 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700539 ApplicationInfo app = (ApplicationInfo) v.getTag();
540 app = new ApplicationInfo(app);
541
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800543 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
544 b.recycle();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700545 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700546 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800547 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700548 return false;
549 }
550
Winson Chunge3193b92010-09-10 11:44:42 -0700551 /**
552 * Pre-processes the layout of the different widget pages.
553 * @return the number of pages of widgets that we have
554 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700555 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700556 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700557
Winson Chunge3193b92010-09-10 11:44:42 -0700558 // create a new page for the first set of widgets
559 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700560 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700561 mWidgetPages.add(newPage);
562
563 // do this until we have no more widgets to lay out
564 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
565 final int widgetCount = mWidgetList.size();
566 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700567 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700568 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700569
Winson Chunge3193b92010-09-10 11:44:42 -0700570 // determine the size of the current widget
571 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
572 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700573
Winson Chunge3193b92010-09-10 11:44:42 -0700574 // create a new page if necessary
575 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
576 numCellsInRow = 0;
577 newPage = new ArrayList<AppWidgetProviderInfo>();
578 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 }
580
Winson Chunge3193b92010-09-10 11:44:42 -0700581 // add the item to the current page
582 newPage.add(info);
583 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700584 }
Winson Chunge3193b92010-09-10 11:44:42 -0700585
Winson Chung80baf5a2010-08-09 16:03:15 -0700586 return mWidgetPages.size();
587 }
588
Winson Chunge3193b92010-09-10 11:44:42 -0700589 /**
Winson Chung7da10252010-10-28 16:07:04 -0700590 * Helper function to draw a drawable to the specified canvas with the specified bounds.
591 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800592 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700593 if (bitmap != null) mCanvas.setBitmap(bitmap);
594 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800595 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700596 d.draw(mCanvas);
597 mCanvas.restore();
598 }
599
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800600 /*
601 * This method fetches an xml file specified in the manifest identified by
602 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
603 * an image which will be used as the wallpaper preview for an activity
604 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
605 * in the customize drawer.
606 */
607 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800608 ActivityInfo activityInfo = ri.activityInfo;
609 XmlResourceParser parser = null;
610 try {
611 parser = activityInfo.loadXmlMetaData(mPackageManager,
612 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
613 if (parser == null) {
614 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
615 + "wallpaper provider '" + component + '\'');
616 return null;
617 }
618
619 AttributeSet attrs = Xml.asAttributeSet(parser);
620
621 int type;
622 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
623 && type != XmlPullParser.START_TAG) {
624 // drain whitespace, comments, etc.
625 }
626
627 String nodeName = parser.getName();
628 if (!"wallpaper-preview".equals(nodeName)) {
629 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
630 + "wallpaper provider '" + component + '\'');
631 return null;
632 }
633
634 // If metaData was null, we would have returned earlier when getting
635 // the parser No need to do the check here
636 Resources res = mPackageManager.getResourcesForApplication(
637 activityInfo.applicationInfo);
638
639 TypedArray sa = res.obtainAttributes(attrs,
640 com.android.internal.R.styleable.WallpaperPreviewInfo);
641
642 TypedValue value = sa.peekValue(
643 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
644 if (value == null) return null;
645
646 return res.getDrawable(value.resourceId);
647 } catch (Exception e) {
648 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
649 return null;
650 } finally {
651 if (parser != null) parser.close();
652 }
653 }
654
Winson Chung7da10252010-10-28 16:07:04 -0700655 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800656 * This method will extract the preview image specified by the wallpaper source provider (if it
657 * exists) otherwise, it will try to generate a default image preview.
658 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800659 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800660 // To be implemented later: resolving the up-to-date wallpaper thumbnail
661
662 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
663 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
664 Resources resources = mLauncher.getResources();
665
666 // Create a new bitmap to hold the widget preview
667 int width = (int) (dim * sScaleFactor);
668 int height = (int) (dim * sScaleFactor);
669 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800670
671 Drawable background = parseWallpaperPreviewXml(
672 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
673 boolean foundCustomDrawable = background != null;
674
675 if (!foundCustomDrawable) {
676 background = resources.getDrawable(R.drawable.default_widget_preview);
677 }
678
Winson Chung45e1d6e2010-11-09 17:19:49 -0800679 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
680
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800681 // If we don't have a custom icon, we use the app icon on the default background
682 if (!foundCustomDrawable) {
683 try {
684 final IconCache iconCache =
685 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
686 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
687 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800688
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800689 final int iconSize = minDim / 2;
690 final int offset = iconSize / 4;
691 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
692 } catch (Resources.NotFoundException e) {
693 // if we can't find the icon, then just don't draw it
694 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800695 }
696
Winson Chung29d6fea2010-12-01 15:47:31 -0800697 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800698 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
699 return drawable;
700 }
701
702 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700703 * This method will extract the preview image specified by the widget developer (if it exists),
704 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800705 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700706 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800707 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800708 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700709 String packageName = info.provider.getPackageName();
710 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800711 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 if (info.previewImage != 0) {
713 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
714 if (drawable == null) {
715 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
716 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 }
718 }
719
720 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700721 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
722 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 if (drawable == null) {
724 Resources resources = mLauncher.getResources();
725
Winson Chung80baf5a2010-08-09 16:03:15 -0700726 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700727 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
728 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700729 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
730 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
731 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700732
Winson Chung45e1d6e2010-11-09 17:19:49 -0800733 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700735 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700736 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700737 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700738 }
739 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700740 icon = resources.getDrawable(R.drawable.ic_launcher_application);
741 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700742
Winson Chunge3193b92010-09-10 11:44:42 -0700743 final int iconSize = minDim / 2;
744 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800745 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 } catch (Resources.NotFoundException e) {
747 // if we can't find the icon, then just don't draw it
748 }
749
Winson Chung29d6fea2010-12-01 15:47:31 -0800750 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700751 } else {
752 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800753 final float imageWidth = drawable.getIntrinsicWidth();
754 final float imageHeight = drawable.getIntrinsicHeight();
755 final float aspect = (float) imageWidth / imageHeight;
756 final int scaledWidth =
757 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
758 final int scaledHeight =
759 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700760 int width;
761 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800762 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700763 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800764 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700765 } else {
766 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800767 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700768 }
769
770 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
771 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
772
Winson Chung29d6fea2010-12-01 15:47:31 -0800773 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700774 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800775 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
776 newDrawable.getIntrinsicHeight());
777 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700778 }
779
780 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700781 layout.setCellCount(mCellCountX, mCellCountY);
782 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
783 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700784 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700785 }
786
Winson Chunge3193b92010-09-10 11:44:42 -0700787 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700788 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700789 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
790
791 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
792 }
793
Winson Chung80baf5a2010-08-09 16:03:15 -0700794 private void syncWidgetPages() {
795 if (mWidgetList == null) return;
796
Winson Chunge3193b92010-09-10 11:44:42 -0700797 // we need to repopulate with the LinearLayout layout for the widget pages
798 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700799 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700800 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800801 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700802 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700803 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
804 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700805
Winson Chunge22a8e92010-11-12 13:40:58 -0800806 addView(layout, new LinearLayout.LayoutParams(
807 LinearLayout.LayoutParams.WRAP_CONTENT,
808 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700809 }
810 }
811
812 private void syncWidgetPageItems(int page) {
813 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700814 LinearLayout layout = (LinearLayout) getChildAt(page);
815 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700816 final int count = list.size();
817 layout.removeAllViews();
818 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700819 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
820 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800821 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
822 info.minHeight, null);
823 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700824
Winson Chung29d6fea2010-12-01 15:47:31 -0800825 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700826 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800827 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700828 l.setTag(createItemInfo);
829 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800830 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700831 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700832
Winson Chunge3193b92010-09-10 11:44:42 -0700833 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700834 }
835 }
836
Winson Chung45e1d6e2010-11-09 17:19:49 -0800837 private void syncWallpaperPages() {
838 if (mWallpaperList == null) return;
839
840 // We need to repopulate the LinearLayout for the wallpaper pages
841 removeAllViews();
842 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800843 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800844 for (int i = 0; i < numPages; ++i) {
845 LinearLayout layout = new PagedViewExtendedLayout(getContext());
846 layout.setGravity(Gravity.CENTER_HORIZONTAL);
847 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
848 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
849
Winson Chunge22a8e92010-11-12 13:40:58 -0800850 addView(layout, new LinearLayout.LayoutParams(
851 LinearLayout.LayoutParams.WRAP_CONTENT,
852 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800853 }
854 }
855
856 private void syncWallpaperPageItems(int page) {
857 // Load the items on to the pages
858 LinearLayout layout = (LinearLayout) getChildAt(page);
859 layout.removeAllViews();
860 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800861 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800862 final int startIndex = page * numItemsPerPage;
863 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
864 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800865 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800866 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800867
Winson Chung29d6fea2010-12-01 15:47:31 -0800868 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800869 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800870 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800871 l.setTag(info);
872 l.setOnClickListener(this);
873
Winson Chung45e1d6e2010-11-09 17:19:49 -0800874 layout.addView(l);
875 }
876 }
877
Winson Chung80baf5a2010-08-09 16:03:15 -0700878 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700879 // we need to repopulate with PagedViewCellLayouts
880 removeAllViews();
881
Winson Chung80baf5a2010-08-09 16:03:15 -0700882 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700883 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700884 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
886 setupPage(layout);
887 addView(layout);
888 }
889 }
890
891 private void syncListPageItems(int page, List<ResolveInfo> list) {
892 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700893 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700894 int startIndex = page * numCells;
895 int endIndex = Math.min(startIndex + numCells, list.size());
896 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
897 // TODO: we can optimize by just re-applying to existing views
898 layout.removeAllViews();
899 for (int i = startIndex; i < endIndex; ++i) {
900 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700901 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
902
Winson Chung241c3b42010-08-25 16:53:03 -0700903 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
904 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700905 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800906 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700907 switch (mCustomizationType) {
908 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700909 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700910 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700911 case ShortcutCustomization:
912 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
913 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
914 info.activityInfo.name);
915 icon.setTag(createItemInfo);
916 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800917 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700918 icon.setOnLongClickListener(this);
919 break;
920 default:
921 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700922 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700923
924 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700925 final int x = index % mCellCountX;
926 final int y = index / mCellCountX;
927 setupPage(layout);
928 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
929 }
930 }
931
932 private void syncAppPages() {
933 if (mApps == null) return;
934
935 // We need to repopulate with PagedViewCellLayouts
936 removeAllViews();
937
938 // Ensure that we have the right number of pages
939 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
940 for (int i = 0; i < numPages; ++i) {
941 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
942 setupPage(layout);
943 addView(layout);
944 }
945 }
946
947 private void syncAppPageItems(int page) {
948 if (mApps == null) return;
949
950 // ensure that we have the right number of items on the pages
951 int numCells = mCellCountX * mCellCountY;
952 int startIndex = page * numCells;
953 int endIndex = Math.min(startIndex + numCells, mApps.size());
954 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
955 // TODO: we can optimize by just re-applying to existing views
956 layout.removeAllViews();
957 for (int i = startIndex; i < endIndex; ++i) {
958 final ApplicationInfo info = mApps.get(i);
959 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
960 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700961 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700962 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800963 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700964 icon.setOnLongClickListener(this);
965
966 final int index = i - startIndex;
967 final int x = index % mCellCountX;
968 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700969 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700970 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 }
972 }
973
Winson Chung80baf5a2010-08-09 16:03:15 -0700974 @Override
975 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700976 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700977 switch (mCustomizationType) {
978 case WidgetCustomization:
979 syncWidgetPages();
980 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700981 case ShortcutCustomization:
982 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700983 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700984 break;
985 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800986 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700987 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700988 case ApplicationCustomization:
989 syncAppPages();
990 centerPagedViewCellLayouts = false;
991 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700992 default:
993 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700994 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700995 break;
996 }
997
998 // only try and center the page if there is one page
999 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001000 if (centerPagedViewCellLayouts) {
1001 if (childCount == 1) {
1002 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1003 layout.enableCenteredContent(true);
1004 } else {
1005 for (int i = 0; i < childCount; ++i) {
1006 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1007 layout.enableCenteredContent(false);
1008 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001009 }
1010 }
1011
1012 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001013 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001014 }
1015
1016 @Override
1017 public void syncPageItems(int page) {
1018 switch (mCustomizationType) {
1019 case WidgetCustomization:
1020 syncWidgetPageItems(page);
1021 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001022 case ShortcutCustomization:
1023 syncListPageItems(page, mShortcutList);
1024 break;
1025 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001026 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001027 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001028 case ApplicationCustomization:
1029 syncAppPageItems(page);
1030 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001031 }
1032 }
Winson Chunge3193b92010-09-10 11:44:42 -07001033
Winson Chungd0d43012010-09-26 17:26:45 -07001034 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001035 protected int getAssociatedLowerPageBound(int page) {
1036 return 0;
1037 }
Winson Chungd0d43012010-09-26 17:26:45 -07001038 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001039 protected int getAssociatedUpperPageBound(int page) {
1040 return getChildCount();
1041 }
Winson Chungd0d43012010-09-26 17:26:45 -07001042
1043 @Override
1044 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001045 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001046 return true;
1047 }
1048
1049 @Override
1050 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1051 return true;
1052 }
1053
1054 @Override
1055 public void onDestroyActionMode(ActionMode mode) {
1056 endChoiceMode();
1057 }
1058
1059 @Override
1060 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1061 return false;
1062 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001063}