blob: 429c7fde906e621b979c91fe73c7a23c63bcc2da [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;
Winson Chungcd4bc492010-12-09 18:52:32 -080024import android.animation.Animator.AnimatorListener;
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;
43import android.graphics.Color;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.util.AttributeSet;
46import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080047import android.util.Slog;
48import android.util.TypedValue;
49import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070050import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070051import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070052import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070053import android.view.Menu;
54import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070055import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080056import android.view.animation.DecelerateInterpolator;
57import android.view.animation.Interpolator;
Winson Chunge3193b92010-09-10 11:44:42 -070058import android.widget.ImageView;
59import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080060import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070061
Michael Jurka72b079e2010-12-10 01:03:53 -080062import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Comparator;
65import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070066
Adam Cohen7b9d3a62010-12-07 21:49:34 -080067
Michael Jurka72b079e2010-12-10 01:03:53 -080068public class CustomizePagedView extends PagedViewWithDraggableItems
69 implements View.OnClickListener, DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070070
71 public enum CustomizationType {
72 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070073 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070074 WallpaperCustomization,
75 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070076 }
77
78 private static final String TAG = "CustomizeWorkspace";
79 private static final boolean DEBUG = false;
80
81 private Launcher mLauncher;
82 private DragController mDragController;
83 private PackageManager mPackageManager;
84
85 private CustomizationType mCustomizationType;
86
Winson Chunge3193b92010-09-10 11:44:42 -070087 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
88 private PagedViewCellLayout mWorkspaceWidgetLayout;
89
90 // The mapping between the pages and the widgets that will be laid out on them
91 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
92
Winson Chung45e1d6e2010-11-09 17:19:49 -080093 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070094 private int mMaxWidgetWidth;
95
Winson Chung45e1d6e2010-11-09 17:19:49 -080096 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070097 private int mMaxWidgetsCellHSpan;
98
Winson Chung45e1d6e2010-11-09 17:19:49 -080099 // The size of the items on the wallpaper tab
100 private int mWallpaperCellHSpan;
101
Winson Chung78bd53c2010-12-09 13:50:24 -0800102 // The max number of wallpaper cells to take a "page" of wallpaper items
103 private int mMaxWallpaperCellHSpan;
104
Winson Chunge3193b92010-09-10 11:44:42 -0700105 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700108 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700109 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700110
Winson Chunge3193b92010-09-10 11:44:42 -0700111 private static final int sMinWidgetCellHSpan = 2;
112 private static final int sMaxWidgetCellHSpan = 4;
113
Michael Jurka3125d9d2010-09-27 11:30:20 -0700114 private int mChoiceModeTitleText;
115
Winson Chunge3193b92010-09-10 11:44:42 -0700116 // The scale factor for widget previews inside the widget drawer
117 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700118
119 private final Canvas mCanvas = new Canvas();
120 private final LayoutInflater mInflater;
121
Adam Cohen120980b2010-12-08 11:05:37 -0800122 private final float mTmpFloatPos[] = new float[2];
123 private final float ANIMATION_SCALE = 0.5f;
124 private final int ANIMATION_DURATION = 400;
125 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
126 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
127
Winson Chung80baf5a2010-08-09 16:03:15 -0700128 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700129 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 }
131
132 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700133 this(context, attrs, 0);
134 }
135
136 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
138
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700139 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800140 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
141 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800142 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700143 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
144 a.recycle();
145 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
146 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
147 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700148 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800149
Winson Chung80baf5a2010-08-09 16:03:15 -0700150 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700151 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
152 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700153 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700154
Michael Jurka7426c422010-11-11 15:23:47 -0800155 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800156 setDragSlopeThreshold(
157 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800158
Winson Chung80baf5a2010-08-09 16:03:15 -0700159 setVisibility(View.GONE);
160 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700161 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700162 }
163
Winson Chung7da10252010-10-28 16:07:04 -0700164 @Override
165 protected void init() {
166 super.init();
167 mCenterPagesVertically = false;
168 }
169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 public void setLauncher(Launcher launcher) {
171 Context context = getContext();
172 mLauncher = launcher;
173 mPackageManager = context.getPackageManager();
174 }
175
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700176 /**
177 * Sets the list of applications that launcher has loaded.
178 */
179 public void setApps(ArrayList<ApplicationInfo> list) {
180 mApps = list;
181 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700182
183 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700184 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700185 }
186
187 /**
188 * Convenience function to add new items to the set of applications that were previously loaded.
189 * Called by both updateApps() and setApps().
190 */
191 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
192 // we add it in place, in alphabetical order
193 final int count = list.size();
194 for (int i = 0; i < count; ++i) {
195 final ApplicationInfo info = list.get(i);
196 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
197 if (index < 0) {
198 mApps.add(-(index + 1), info);
199 }
200 }
201 }
202
203 /**
204 * Adds new applications to the loaded list, and notifies the paged view to update itself.
205 */
206 public void addApps(ArrayList<ApplicationInfo> list) {
207 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700208
209 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700210 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700211 }
212
213 /**
214 * Convenience function to remove items to the set of applications that were previously loaded.
215 * Called by both updateApps() and removeApps().
216 */
217 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
218 // loop through all the apps and remove apps that have the same component
219 final int length = list.size();
220 for (int i = 0; i < length; ++i) {
221 final ApplicationInfo info = list.get(i);
222 int removeIndex = findAppByComponent(mApps, info);
223 if (removeIndex > -1) {
224 mApps.remove(removeIndex);
225 mPageViewIconCache.removeOutline(info);
226 }
227 }
228 }
229
230 /**
231 * Removes applications from the loaded list, and notifies the paged view to update itself.
232 */
233 public void removeApps(ArrayList<ApplicationInfo> list) {
234 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700235
236 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700237 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700238 }
239
240 /**
241 * Updates a set of applications from the loaded list, and notifies the paged view to update
242 * itself.
243 */
244 public void updateApps(ArrayList<ApplicationInfo> list) {
245 // We remove and re-add the updated applications list because it's properties may have
246 // changed (ie. the title), and this will ensure that the items will be in their proper
247 // place in the list.
248 removeAppsWithoutInvalidate(list);
249 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700250
251 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700252 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700253 }
254
255 /**
256 * Convenience function to find matching ApplicationInfos for removal.
257 */
258 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
259 ComponentName removeComponent = item.intent.getComponent();
260 final int length = list.size();
261 for (int i = 0; i < length; ++i) {
262 ApplicationInfo info = list.get(i);
263 if (info.intent.getComponent().equals(removeComponent)) {
264 return i;
265 }
266 }
267 return -1;
268 }
269
Winson Chung80baf5a2010-08-09 16:03:15 -0700270 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700271 // get the list of widgets
272 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
273 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
274 @Override
275 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
276 return object1.label.compareTo(object2.label);
277 }
278 });
279
280 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
281 @Override
282 public int compare(ResolveInfo object1, ResolveInfo object2) {
283 return object1.loadLabel(mPackageManager).toString().compareTo(
284 object2.loadLabel(mPackageManager).toString());
285 }
286 };
287
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 // get the list of shortcuts
289 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
290 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
291 Collections.sort(mShortcutList, resolveInfoComparator);
292
Winson Chunge8878e32010-09-15 20:37:09 -0700293 // get the list of wallpapers
294 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800295 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
296 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700297 Collections.sort(mWallpaperList, resolveInfoComparator);
298
Winson Chung10fefb12010-11-01 11:57:06 -0700299 invalidatePageDataAndIconCache();
300 }
301
302 private void invalidatePageDataAndIconCache() {
303 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700304 mPageViewIconCache.clear();
305
Winson Chunge3193b92010-09-10 11:44:42 -0700306 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 invalidatePageData();
308 }
309
310 public void setDragController(DragController dragger) {
311 mDragController = dragger;
312 }
313
314 public void setCustomizationFilter(CustomizationType filterType) {
315 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700316 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800317 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700318 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700319
320 // End the current choice mode so that we don't carry selections across tabs
321 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 }
323
324 @Override
325 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700326 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 }
328
329 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800330 public void onDragViewVisible() {
331 }
332
Adam Cohen120980b2010-12-08 11:05:37 -0800333 class ScaleAlphaInterpolator implements Interpolator {
334 public float getInterpolation(float input) {
335 float pivot = 0.5f;
336 if (input < pivot) {
337 return 0;
338 } else {
339 return (input - pivot)/(1 - pivot);
340 }
341 }
342 }
343
344 private void animateItemOntoScreen(View dragView,
345 final CellLayout layout, final ItemInfo info) {
346 mTmpFloatPos[0] = layout.getWidth() / 2;
347 mTmpFloatPos[1] = layout.getHeight() / 2;
348 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
349
350 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
351 final View dragCopy = dragLayer.createDragView(dragView);
352 dragCopy.setAlpha(1.0f);
353
354 int dragViewWidth = dragView.getMeasuredWidth();
355 int dragViewHeight = dragView.getMeasuredHeight();
356 float heightOffset = 0;
357 float widthOffset = 0;
358
359 if (dragView instanceof ImageView) {
360 Drawable d = ((ImageView) dragView).getDrawable();
361 int width = d.getIntrinsicWidth();
362 int height = d.getIntrinsicHeight();
363
364 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
365 float f = (dragViewWidth / (width * 1.0f));
366 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
367 } else {
368 float f = (dragViewHeight / (height * 1.0f));
369 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
370 }
371 }
372
373 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
374 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
375
376 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
377 PropertyValuesHolder.ofFloat("x", toX),
378 PropertyValuesHolder.ofFloat("y", toY));
379 posAnim.setInterpolator(mQuintEaseOutInterpolator);
380 posAnim.setDuration(ANIMATION_DURATION);
381
382 posAnim.addListener(new AnimatorListener() {
383 public void onAnimationCancel(Animator animation) {}
384 public void onAnimationRepeat(Animator animation) {}
385 public void onAnimationStart(Animator animation) {}
386
387 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() {
Adam Cohen120980b2010-12-08 11:05:37 -0800447 animateItemOntoScreen(dragView, cl, itemInfo);
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700448 }
449 });
Winson Chungd0d43012010-09-26 17:26:45 -0700450 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700451 }
452
453 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700454 switch (mCustomizationType) {
455 case WallpaperCustomization:
456 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700457 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700458 animateClickFeedback(v, new Runnable() {
459 @Override
460 public void run() {
461 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700462 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700463 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
464 ComponentName name = new ComponentName(info.activityInfo.packageName,
465 info.activityInfo.name);
466 createWallpapersIntent.setComponent(name);
467 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700468 }
469 });
Winson Chungd0d43012010-09-26 17:26:45 -0700470 break;
471 default:
472 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700473 }
474 }
475
Winson Chungcd4bc492010-12-09 18:52:32 -0800476 Bitmap drawableToBitmap(Drawable d, View v) {
477 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800478 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800479 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800480 d.draw(c);
481 return b;
482 }
483
Adam Cohen120980b2010-12-08 11:05:37 -0800484 private View getDragView(View v) {
485 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
486 v.findViewById(R.id.widget_preview) : v;
487 }
488
Michael Jurka72b079e2010-12-10 01:03:53 -0800489 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700490 // End the current choice mode before we start dragging anything
491 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
492 endChoiceMode();
493 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800494 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700495
Winson Chung80baf5a2010-08-09 16:03:15 -0700496 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800497 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800498 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800499 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800500 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
501 final Drawable icon = i.getDrawable();
502 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700503 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700504
Michael Jurkad3ef3062010-11-23 16:23:58 -0800505 int[] spanXY = CellLayout.rectToCell(
506 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
507 createWidgetInfo.spanX = spanXY[0];
508 createWidgetInfo.spanY = spanXY[1];
509 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
510 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800511 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
512 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700513 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800514 }
515 case ShortcutCustomization: {
516 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800517 final TextView tv = (TextView) v;
518 final Drawable icon = tv.getCompoundDrawables()[1];
519 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800520 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800521
Michael Jurkad3ef3062010-11-23 16:23:58 -0800522 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800523 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
524 null);
525 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800527 }
528 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700529 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800530 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800531 final TextView tv = (TextView) v;
532 final Drawable icon = tv.getCompoundDrawables()[1];
533 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700534 ApplicationInfo app = (ApplicationInfo) v.getTag();
535 app = new ApplicationInfo(app);
536
Michael Jurkad3ef3062010-11-23 16:23:58 -0800537 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800538 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
539 b.recycle();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700540 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700541 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800542 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700543 return false;
544 }
545
Winson Chunge3193b92010-09-10 11:44:42 -0700546 /**
547 * Pre-processes the layout of the different widget pages.
548 * @return the number of pages of widgets that we have
549 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700551 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700552
Winson Chunge3193b92010-09-10 11:44:42 -0700553 // create a new page for the first set of widgets
554 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700555 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700556 mWidgetPages.add(newPage);
557
558 // do this until we have no more widgets to lay out
559 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
560 final int widgetCount = mWidgetList.size();
561 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700562 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700563 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700564
Winson Chunge3193b92010-09-10 11:44:42 -0700565 // determine the size of the current widget
566 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
567 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700568
Winson Chunge3193b92010-09-10 11:44:42 -0700569 // create a new page if necessary
570 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
571 numCellsInRow = 0;
572 newPage = new ArrayList<AppWidgetProviderInfo>();
573 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700574 }
575
Winson Chunge3193b92010-09-10 11:44:42 -0700576 // add the item to the current page
577 newPage.add(info);
578 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 }
Winson Chunge3193b92010-09-10 11:44:42 -0700580
Winson Chung80baf5a2010-08-09 16:03:15 -0700581 return mWidgetPages.size();
582 }
583
Winson Chunge3193b92010-09-10 11:44:42 -0700584 /**
Winson Chung7da10252010-10-28 16:07:04 -0700585 * Helper function to draw a drawable to the specified canvas with the specified bounds.
586 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800587 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700588 if (bitmap != null) mCanvas.setBitmap(bitmap);
589 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800590 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700591 d.draw(mCanvas);
592 mCanvas.restore();
593 }
594
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800595 /*
596 * This method fetches an xml file specified in the manifest identified by
597 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
598 * an image which will be used as the wallpaper preview for an activity
599 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
600 * in the customize drawer.
601 */
602 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
603 Drawable d = null;
604
605 ActivityInfo activityInfo = ri.activityInfo;
606 XmlResourceParser parser = null;
607 try {
608 parser = activityInfo.loadXmlMetaData(mPackageManager,
609 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
610 if (parser == null) {
611 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
612 + "wallpaper provider '" + component + '\'');
613 return null;
614 }
615
616 AttributeSet attrs = Xml.asAttributeSet(parser);
617
618 int type;
619 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
620 && type != XmlPullParser.START_TAG) {
621 // drain whitespace, comments, etc.
622 }
623
624 String nodeName = parser.getName();
625 if (!"wallpaper-preview".equals(nodeName)) {
626 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
627 + "wallpaper provider '" + component + '\'');
628 return null;
629 }
630
631 // If metaData was null, we would have returned earlier when getting
632 // the parser No need to do the check here
633 Resources res = mPackageManager.getResourcesForApplication(
634 activityInfo.applicationInfo);
635
636 TypedArray sa = res.obtainAttributes(attrs,
637 com.android.internal.R.styleable.WallpaperPreviewInfo);
638
639 TypedValue value = sa.peekValue(
640 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
641 if (value == null) return null;
642
643 return res.getDrawable(value.resourceId);
644 } catch (Exception e) {
645 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
646 return null;
647 } finally {
648 if (parser != null) parser.close();
649 }
650 }
651
Winson Chung7da10252010-10-28 16:07:04 -0700652 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800653 * This method will extract the preview image specified by the wallpaper source provider (if it
654 * exists) otherwise, it will try to generate a default image preview.
655 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800656 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800657 // To be implemented later: resolving the up-to-date wallpaper thumbnail
658
659 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
660 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
661 Resources resources = mLauncher.getResources();
662
663 // Create a new bitmap to hold the widget preview
664 int width = (int) (dim * sScaleFactor);
665 int height = (int) (dim * sScaleFactor);
666 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800667
668 Drawable background = parseWallpaperPreviewXml(
669 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
670 boolean foundCustomDrawable = background != null;
671
672 if (!foundCustomDrawable) {
673 background = resources.getDrawable(R.drawable.default_widget_preview);
674 }
675
Winson Chung45e1d6e2010-11-09 17:19:49 -0800676 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
677
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800678 // If we don't have a custom icon, we use the app icon on the default background
679 if (!foundCustomDrawable) {
680 try {
681 final IconCache iconCache =
682 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
683 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
684 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800685
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800686 final int iconSize = minDim / 2;
687 final int offset = iconSize / 4;
688 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
689 } catch (Resources.NotFoundException e) {
690 // if we can't find the icon, then just don't draw it
691 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800692 }
693
Winson Chung29d6fea2010-12-01 15:47:31 -0800694 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800695 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
696 return drawable;
697 }
698
699 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700700 * This method will extract the preview image specified by the widget developer (if it exists),
701 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800702 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700703 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800704 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800705 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700706 String packageName = info.provider.getPackageName();
707 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800708 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700709 if (info.previewImage != 0) {
710 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
711 if (drawable == null) {
712 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
713 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700714 }
715 }
716
717 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700718 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
719 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700720 if (drawable == null) {
721 Resources resources = mLauncher.getResources();
722
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700724 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
725 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700726 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
727 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
728 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700729
Winson Chung45e1d6e2010-11-09 17:19:49 -0800730 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700731 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700732 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700733 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700735 }
736 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700737 icon = resources.getDrawable(R.drawable.ic_launcher_application);
738 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700739
Winson Chunge3193b92010-09-10 11:44:42 -0700740 final int iconSize = minDim / 2;
741 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800742 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700743 } catch (Resources.NotFoundException e) {
744 // if we can't find the icon, then just don't draw it
745 }
746
Winson Chung29d6fea2010-12-01 15:47:31 -0800747 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700748 } else {
749 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800750 final float imageWidth = drawable.getIntrinsicWidth();
751 final float imageHeight = drawable.getIntrinsicHeight();
752 final float aspect = (float) imageWidth / imageHeight;
753 final int scaledWidth =
754 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
755 final int scaledHeight =
756 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700757 int width;
758 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800759 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700760 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800761 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700762 } else {
763 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800764 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700765 }
766
767 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
768 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
769
Winson Chung29d6fea2010-12-01 15:47:31 -0800770 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700771 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800772 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
773 newDrawable.getIntrinsicHeight());
774 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700775 }
776
777 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700778 layout.setCellCount(mCellCountX, mCellCountY);
779 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
780 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700781 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700782 }
783
Winson Chunge3193b92010-09-10 11:44:42 -0700784 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700785 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700786 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
787
788 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
789 }
790
Winson Chung80baf5a2010-08-09 16:03:15 -0700791 private void syncWidgetPages() {
792 if (mWidgetList == null) return;
793
Winson Chunge3193b92010-09-10 11:44:42 -0700794 // we need to repopulate with the LinearLayout layout for the widget pages
795 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700796 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700797 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800798 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700799 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700800 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
801 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700802
Winson Chunge22a8e92010-11-12 13:40:58 -0800803 addView(layout, new LinearLayout.LayoutParams(
804 LinearLayout.LayoutParams.WRAP_CONTENT,
805 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700806 }
807 }
808
809 private void syncWidgetPageItems(int page) {
810 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700811 LinearLayout layout = (LinearLayout) getChildAt(page);
812 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700813 final int count = list.size();
814 layout.removeAllViews();
815 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700816 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
817 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800818 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
819 info.minHeight, null);
820 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700821
Winson Chung29d6fea2010-12-01 15:47:31 -0800822 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700823 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800824 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700825 l.setTag(createItemInfo);
826 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800827 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700828 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700829
Winson Chunge3193b92010-09-10 11:44:42 -0700830 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700831 }
832 }
833
Winson Chung45e1d6e2010-11-09 17:19:49 -0800834 private void syncWallpaperPages() {
835 if (mWallpaperList == null) return;
836
837 // We need to repopulate the LinearLayout for the wallpaper pages
838 removeAllViews();
839 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800840 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800841 for (int i = 0; i < numPages; ++i) {
842 LinearLayout layout = new PagedViewExtendedLayout(getContext());
843 layout.setGravity(Gravity.CENTER_HORIZONTAL);
844 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
845 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
846
Winson Chunge22a8e92010-11-12 13:40:58 -0800847 addView(layout, new LinearLayout.LayoutParams(
848 LinearLayout.LayoutParams.WRAP_CONTENT,
849 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800850 }
851 }
852
853 private void syncWallpaperPageItems(int page) {
854 // Load the items on to the pages
855 LinearLayout layout = (LinearLayout) getChildAt(page);
856 layout.removeAllViews();
857 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800858 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800859 final int startIndex = page * numItemsPerPage;
860 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
861 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800862 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800863 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800864
Winson Chung29d6fea2010-12-01 15:47:31 -0800865 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800866 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800867 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800868 l.setTag(info);
869 l.setOnClickListener(this);
870
Winson Chung45e1d6e2010-11-09 17:19:49 -0800871 layout.addView(l);
872 }
873 }
874
Winson Chung80baf5a2010-08-09 16:03:15 -0700875 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700876 // we need to repopulate with PagedViewCellLayouts
877 removeAllViews();
878
Winson Chung80baf5a2010-08-09 16:03:15 -0700879 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700880 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700881 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700882 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
883 setupPage(layout);
884 addView(layout);
885 }
886 }
887
888 private void syncListPageItems(int page, List<ResolveInfo> list) {
889 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700890 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700891 int startIndex = page * numCells;
892 int endIndex = Math.min(startIndex + numCells, list.size());
893 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
894 // TODO: we can optimize by just re-applying to existing views
895 layout.removeAllViews();
896 for (int i = startIndex; i < endIndex; ++i) {
897 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700898 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
899
Winson Chung241c3b42010-08-25 16:53:03 -0700900 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
901 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700902 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800903 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700904 switch (mCustomizationType) {
905 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700906 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700907 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700908 case ShortcutCustomization:
909 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
910 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
911 info.activityInfo.name);
912 icon.setTag(createItemInfo);
913 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800914 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700915 icon.setOnLongClickListener(this);
916 break;
917 default:
918 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700919 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700920
921 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700922 final int x = index % mCellCountX;
923 final int y = index / mCellCountX;
924 setupPage(layout);
925 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
926 }
927 }
928
929 private void syncAppPages() {
930 if (mApps == null) return;
931
932 // We need to repopulate with PagedViewCellLayouts
933 removeAllViews();
934
935 // Ensure that we have the right number of pages
936 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
937 for (int i = 0; i < numPages; ++i) {
938 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
939 setupPage(layout);
940 addView(layout);
941 }
942 }
943
944 private void syncAppPageItems(int page) {
945 if (mApps == null) return;
946
947 // ensure that we have the right number of items on the pages
948 int numCells = mCellCountX * mCellCountY;
949 int startIndex = page * numCells;
950 int endIndex = Math.min(startIndex + numCells, mApps.size());
951 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
952 // TODO: we can optimize by just re-applying to existing views
953 layout.removeAllViews();
954 for (int i = startIndex; i < endIndex; ++i) {
955 final ApplicationInfo info = mApps.get(i);
956 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
957 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700958 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700959 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800960 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700961 icon.setOnLongClickListener(this);
962
963 final int index = i - startIndex;
964 final int x = index % mCellCountX;
965 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700966 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700967 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 }
969 }
970
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 @Override
972 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700973 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700974 switch (mCustomizationType) {
975 case WidgetCustomization:
976 syncWidgetPages();
977 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700978 case ShortcutCustomization:
979 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700980 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700981 break;
982 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800983 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700984 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700985 case ApplicationCustomization:
986 syncAppPages();
987 centerPagedViewCellLayouts = false;
988 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700989 default:
990 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700991 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700992 break;
993 }
994
995 // only try and center the page if there is one page
996 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700997 if (centerPagedViewCellLayouts) {
998 if (childCount == 1) {
999 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1000 layout.enableCenteredContent(true);
1001 } else {
1002 for (int i = 0; i < childCount; ++i) {
1003 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1004 layout.enableCenteredContent(false);
1005 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001006 }
1007 }
1008
1009 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001010 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001011 }
1012
1013 @Override
1014 public void syncPageItems(int page) {
1015 switch (mCustomizationType) {
1016 case WidgetCustomization:
1017 syncWidgetPageItems(page);
1018 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001019 case ShortcutCustomization:
1020 syncListPageItems(page, mShortcutList);
1021 break;
1022 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001023 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001024 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001025 case ApplicationCustomization:
1026 syncAppPageItems(page);
1027 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001028 }
1029 }
Winson Chunge3193b92010-09-10 11:44:42 -07001030
Winson Chungd0d43012010-09-26 17:26:45 -07001031 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001032 protected int getAssociatedLowerPageBound(int page) {
1033 return 0;
1034 }
Winson Chungd0d43012010-09-26 17:26:45 -07001035 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001036 protected int getAssociatedUpperPageBound(int page) {
1037 return getChildCount();
1038 }
Winson Chungd0d43012010-09-26 17:26:45 -07001039
1040 @Override
1041 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001042 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001043 return true;
1044 }
1045
1046 @Override
1047 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1048 return true;
1049 }
1050
1051 @Override
1052 public void onDestroyActionMode(ActionMode mode) {
1053 endChoiceMode();
1054 }
1055
1056 @Override
1057 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1058 return false;
1059 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001060}